Skip to content
1 change: 1 addition & 0 deletions lib/ImageArchiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ImageArchiver {
image.combineWithDirectory({
directory: option.directory,
template: option.template,
regex: option.regex,
prefix: option.prefix,
suffix: option.suffix
}),
Expand Down
15 changes: 14 additions & 1 deletion lib/ImageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 );
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/ImageReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ImageReducer {
image.combineWithDirectory({
directory: option.directory,
template: option.template,
regex: option.regex,
prefix: option.prefix,
suffix: option.suffix
}),
Expand Down
60 changes: 60 additions & 0 deletions test/image-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});