diff --git a/lib/ImageArchiver.js b/lib/ImageArchiver.js index baed4d5..6963df6 100644 --- a/lib/ImageArchiver.js +++ b/lib/ImageArchiver.js @@ -33,6 +33,7 @@ class ImageArchiver { image.combineWithDirectory({ directory: option.directory, template: option.template, + regex: option.regex, prefix: option.prefix, suffix: option.suffix }), diff --git a/lib/ImageData.js b/lib/ImageData.js index d949f62..af2e6ba 100644 --- a/lib/ImageData.js +++ b/lib/ImageData.js @@ -163,6 +163,19 @@ class ImageData { const fileName = path.parse(this.baseName).name; const extension = "." + this.type.ext; + const regex = output.regex; + if ( typeof regex === "object" && regex.find ) { + const expression = new RegExp( regex.find ); + + const match = this.dirName.match(expression); + if ( match ) { + const outputPath = this.dirName.replace( expression, regex.replace || "" ); + return path.join( outputPath, prefix + fileName + suffix + extension ); + } else { + console.log( "Directory " + this.dirName + " didn't match regex: " + regex.find ); + } + } + const template = output.template; if ( typeof template === "object" && template.pattern ) { const inputTemplate = PathTemplate.parse(template.pattern); @@ -173,7 +186,7 @@ class ImageData { const outputPath = PathTemplate.format(outputTemplate, match); return path.join(outputPath, prefix + fileName + suffix + extension); } else { - console.log( "Directory " + this.dirName + " didn't match template " + template.pattern ); + console.log( "Directory " + this.dirName + " didn't match template: " + template.pattern ); } } diff --git a/lib/ImageReducer.js b/lib/ImageReducer.js index b33e6d2..9a0f50e 100644 --- a/lib/ImageReducer.js +++ b/lib/ImageReducer.js @@ -41,6 +41,7 @@ class ImageReducer { image.combineWithDirectory({ directory: option.directory, template: option.template, + regex: option.regex, prefix: option.prefix, suffix: option.suffix }), diff --git a/test/image-data.js b/test/image-data.js index 09e036c..7b82bb6 100644 --- a/test/image-data.js +++ b/test/image-data.js @@ -108,3 +108,63 @@ test("[path-template] Build output path with template and suffix", t => { test("[path-template] Build output path with template, prefix and suffix", t => { t.is(image.combineWithDirectory({template: {pattern: "*", output: "d/e"}, prefix: "prefix-", suffix: "_suffix"}), "d/e/prefix-key_suffix.png"); }); + +test("[RegEx] Build output path when regex is undefined", t => { + t.is(image.combineWithDirectory({}), "a/b/c/key.png"); +}); + +test("[RegEx] Build output path when regex is an empty map", t => { + t.is(image.combineWithDirectory({regex: {}}), "a/b/c/key.png"); +}); + +test("[RegEx] Build output path when regex is an map with find and replace keys empty", t => { + t.is(image.combineWithDirectory({regex: {find: "", replace: ""}}), "a/b/c/key.png"); +}); + +test("[RegEx] Build output path when regex is an 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"); +}); + +test("[RegEx] Build output path when regex adds subdirectory", t => { + t.is(image.combineWithDirectory({regex: {find: "$", replace: "/d"}}), "a/b/c/d/key.png"); +}); + +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"); +}); + +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"); +}); + +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"); +}); + +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"); +}); + +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"); +}); + +test("[RegEx] Build output path when regex didn't match base directory", t => { + t.is(image.combineWithDirectory({regex: {find: "x/.*", replace: "d/e"}}), "a/b/c/key.png"); +}); + +test("[RegEx] Build output path with regex and prefix", t => { + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}, prefix: "prefix-"}), "d/e/prefix-key.png"); +}); + +test("[RegEx] Build output path with regex and suffix", t => { + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}, suffix: "-suffix"}), "d/e/key-suffix.png"); +}); + +test("[RegEx] Build output path with regex, prefix and suffix", t => { + t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}, prefix: "prefix-", suffix: "_suffix"}), "d/e/prefix-key_suffix.png"); +});