From 91a2f6a1f201aadbcba935f1636e3455e9a54393 Mon Sep 17 00:00:00 2001 From: Kamil Dybicz Date: Sun, 26 Feb 2017 21:23:37 +0100 Subject: [PATCH 1/5] Extending destination directory building code with regex --- lib/ImageData.js | 9 ++++++++- test/image-data.js | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/ImageData.js b/lib/ImageData.js index b06a6d2..c1093e3 100644 --- a/lib/ImageData.js +++ b/lib/ImageData.js @@ -161,7 +161,12 @@ class ImageData { const suffix = fileSuffix || ""; const fileName = path.parse(this.baseName).name; const extension = "." + this.type.ext; - if ( directory != null ) { + + if ( typeof directory === "object" && "regex" in directory && "find" in directory["regex"] && "replace" in directory["regex"]) { + const regex = new RegExp(directory.regex.find); + const output = this.dirName.replace(regex, directory.regex.replace); + return path.join(output, prefix + fileName + suffix + extension); + } else if ( typeof directory === "string" ) { // ./X , ../X , . , .. if ( directory.match(/^\.\.?\//) || directory.match(/^\.\.?$/) ) { return path.join(this.dirName, directory, prefix + fileName + suffix + extension); @@ -172,6 +177,8 @@ class ImageData { return path.join(this.dirName, prefix + fileName + suffix + extension); } } + + } module.exports = ImageData; diff --git a/test/image-data.js b/test/image-data.js index 2386ece..eebbe70 100644 --- a/test/image-data.js +++ b/test/image-data.js @@ -3,7 +3,7 @@ const ImageData = require("../lib/ImageData"); const test = require("ava"); -test("ImageData combineWithDirectory Test", t => { +test("Build output path", t => { const image = new ImageData("a/b/c/key.png", "bucket", "data", {}); // No directory @@ -39,3 +39,42 @@ test("ImageData combineWithDirectory Test", t => { // With prefix and suffix t.is(image.combineWithDirectory("d/e", "prefix-", "_suffix"), "d/e/prefix-key_suffix.png"); }); + +test.only("Build output path with RegEx", t => { + const image = new ImageData("a/b/c/key.png", "bucket", "data", {}); + + // No directory + t.is(image.combineWithDirectory({}), "a/b/c/key.png"); + t.is(image.combineWithDirectory({regex: {}}), "a/b/c/key.png"); + t.is(image.combineWithDirectory({regex: {find: "", replace: ""}}), "a/b/c/key.png"); + + // Empty directory + t.is(image.combineWithDirectory({regex: {find: ".*", replace: ""}}), "key.png"); + + // Relative directory + t.is(image.combineWithDirectory({regex: {find: "$", replace: "/d"}}), "a/b/c/d/key.png"); + + // Internal directory + t.is(image.combineWithDirectory({regex: {find: "$", replace: "/d/e"}}), "a/b/c/d/e/key.png"); + + // External directory + t.is(image.combineWithDirectory({regex: {find: "[a-zA-Z]+$", replace: ""}}), "a/b/key.png"); + + // External internal directory + t.is(image.combineWithDirectory({regex: {find: "\/c", replace: "/d"}}), "a/b/d/key.png"); + + // Root directory + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d"}}), "d/key.png"); + + // Root internal directory + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}), "d/e/key.png"); + + // With prefix + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}, "prefix-"), "d/e/prefix-key.png"); + + // With suffix + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}, "", "-suffix"), "d/e/key-suffix.png"); + + // With prefix and suffix + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}, "prefix-", "_suffix"), "d/e/prefix-key_suffix.png"); +}); From 9d02263b6ba6415d0ec89cbe1bd2643dc2822be8 Mon Sep 17 00:00:00 2001 From: Kamil Dybicz Date: Mon, 27 Feb 2017 10:34:27 +0100 Subject: [PATCH 2/5] refactoring code --- lib/ImageData.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ImageData.js b/lib/ImageData.js index c1093e3..ca8a504 100644 --- a/lib/ImageData.js +++ b/lib/ImageData.js @@ -162,9 +162,9 @@ class ImageData { const fileName = path.parse(this.baseName).name; const extension = "." + this.type.ext; - if ( typeof directory === "object" && "regex" in directory && "find" in directory["regex"] && "replace" in directory["regex"]) { + if ( typeof directory === "object" && directory.regex && directory.regex.find ) { const regex = new RegExp(directory.regex.find); - const output = this.dirName.replace(regex, directory.regex.replace); + const output = this.dirName.replace(regex, directory.regex.replace || ""); return path.join(output, prefix + fileName + suffix + extension); } else if ( typeof directory === "string" ) { // ./X , ../X , . , .. From 7bbc3d31c5cbf407dd31885de497f90c1724798c Mon Sep 17 00:00:00 2001 From: Kamil Dybicz Date: Mon, 27 Feb 2017 10:36:59 +0100 Subject: [PATCH 3/5] remove ".only" from tests --- lib/ImageData.js | 2 -- test/image-data.js | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/ImageData.js b/lib/ImageData.js index ca8a504..dacc80c 100644 --- a/lib/ImageData.js +++ b/lib/ImageData.js @@ -177,8 +177,6 @@ class ImageData { return path.join(this.dirName, prefix + fileName + suffix + extension); } } - - } module.exports = ImageData; diff --git a/test/image-data.js b/test/image-data.js index eebbe70..7920691 100644 --- a/test/image-data.js +++ b/test/image-data.js @@ -40,7 +40,7 @@ test("Build output path", t => { t.is(image.combineWithDirectory("d/e", "prefix-", "_suffix"), "d/e/prefix-key_suffix.png"); }); -test.only("Build output path with RegEx", t => { +test("Build output path with RegEx", t => { const image = new ImageData("a/b/c/key.png", "bucket", "data", {}); // No directory From e6e0424a459bf849b812969c18c1454a3298b6f8 Mon Sep 17 00:00:00 2001 From: Kamil Dybicz Date: Tue, 28 Feb 2017 21:33:27 +0100 Subject: [PATCH 4/5] split all test cases into separate tests --- test/image-data.js | 84 +++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/test/image-data.js b/test/image-data.js index 7920691..a140045 100644 --- a/test/image-data.js +++ b/test/image-data.js @@ -3,78 +3,108 @@ const ImageData = require("../lib/ImageData"); const test = require("ava"); -test("Build output path", t => { - const image = new ImageData("a/b/c/key.png", "bucket", "data", {}); +let image; - // No directory +test.before(t => { + image = new ImageData("a/b/c/key.png", "bucket", "data", {}); +}); + +test("Build output path when directory is undefined", t => { t.is(image.combineWithDirectory(undefined), "a/b/c/key.png"); +}); - // Empty directory +test("Build output path when directory is empty", t => { t.is(image.combineWithDirectory(""), "key.png"); +}); - // Relative directory +test("Build output path when directory is relative deeper", t => { t.is(image.combineWithDirectory("./d"), "a/b/c/d/key.png"); +}); - // Internal directory +test("Build output path when directory is relative deeper - 2nd level", t => { t.is(image.combineWithDirectory("./d/e"), "a/b/c/d/e/key.png"); +}); - // External directory +test("Build output path when directory is relative backward", t => { t.is(image.combineWithDirectory(".."), "a/b/key.png"); +}); - // External internal directory +test("Build output path when directory is relative backward with new subdirectory branch", t => { t.is(image.combineWithDirectory("../d"), "a/b/d/key.png"); +}); - // Root directory +test("Build output path when directory is absolute", t => { t.is(image.combineWithDirectory("d"), "d/key.png"); +}); - // Root internal directory +test("Build output path when directory is absolute - 2nd level", t => { t.is(image.combineWithDirectory("d/e"), "d/e/key.png"); +}); - // With prefix +test("Build output path with prefix", t => { t.is(image.combineWithDirectory("d/e", "prefix-"), "d/e/prefix-key.png"); +}); - // With suffix +test("Build output path with suffix", t => { t.is(image.combineWithDirectory("d/e", "", "-suffix"), "d/e/key-suffix.png"); +}); - // With prefix and suffix +test("Build output path with prefix and suffix", t => { t.is(image.combineWithDirectory("d/e", "prefix-", "_suffix"), "d/e/prefix-key_suffix.png"); }); -test("Build output path with RegEx", t => { - const image = new ImageData("a/b/c/key.png", "bucket", "data", {}); - - // No directory +test("[RegEx] Build output path when directory is an empty object", t => { t.is(image.combineWithDirectory({}), "a/b/c/key.png"); +}); + +test("[RegEx] Build output path when directory is an empty regex map", t => { t.is(image.combineWithDirectory({regex: {}}), "a/b/c/key.png"); +}); + +test("[RegEx] Build output path when directory is an regex map with find and replace keys empty", t => { t.is(image.combineWithDirectory({regex: {find: "", replace: ""}}), "a/b/c/key.png"); +}); - // Empty directory +test("[RegEx] Build output path when directory is an regex map with replace keys is missing", t => { + t.is(image.combineWithDirectory({regex: {find: ""}}), "a/b/c/key.png"); +}); + +test("[RegEx] Build output path when regex replace whole directory", t => { t.is(image.combineWithDirectory({regex: {find: ".*", replace: ""}}), "key.png"); +}); - // Relative directory +test("[RegEx] Build output path when regex adds subdirectory", t => { t.is(image.combineWithDirectory({regex: {find: "$", replace: "/d"}}), "a/b/c/d/key.png"); +}); - // Internal directory +test("[RegEx] Build output path when regex adds subdirectory - 2nd level", t => { t.is(image.combineWithDirectory({regex: {find: "$", replace: "/d/e"}}), "a/b/c/d/e/key.png"); +}); - // External directory +test("[RegEx] Build output path when regex removes top subdirectory", t => { t.is(image.combineWithDirectory({regex: {find: "[a-zA-Z]+$", replace: ""}}), "a/b/key.png"); +}); - // External internal directory +test("[RegEx] Build output path when regex replaces top subdirectory with new one", t => { t.is(image.combineWithDirectory({regex: {find: "\/c", replace: "/d"}}), "a/b/d/key.png"); +}); - // Root directory +test("[RegEx] Build output path when regex replaces old path with new absolute one", t => { t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d"}}), "d/key.png"); +}); - // Root internal directory +test("[RegEx] Build output path when regex replaces old path with new absolute one - 2nd level", t => { t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}), "d/e/key.png"); +}); - // With prefix +test("[RegEx] Build output path with regex and prefix", t => { t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}, "prefix-"), "d/e/prefix-key.png"); +}); - // With suffix +test("[RegEx] Build output path with regex and suffix", t => { t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}, "", "-suffix"), "d/e/key-suffix.png"); +}); - // With prefix and suffix +test("[RegEx] Build output path with regex, prefix and suffix", t => { t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}, "prefix-", "_suffix"), "d/e/prefix-key_suffix.png"); }); From 0bf8b3c026ed76709d836d1e0d96f856f5054417 Mon Sep 17 00:00:00 2001 From: Kamil Dybicz Date: Tue, 28 Feb 2017 21:37:10 +0100 Subject: [PATCH 5/5] upgrading ava dependency to 0.18.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3a68a58..4824863 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "image-type": "2.1.0" }, "devDependencies": { - "ava": "^0.17.0", + "ava": "^0.18.2", "bind-all": "^1.0.0", "claudia": "^2.9.0", "coveralls": "^2.11.15",