Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory parameter extension - RegEx #114

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion lib/ImageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" && directory.regex && directory.regex.find ) {
const regex = new RegExp(directory.regex.find);
const output = this.dirName.replace(regex, directory.regex.replace || "");
return path.join(output, prefix + fileName + suffix + extension);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think RegExp is bad idea a bit.

Because:

  • Now configuration is json format. so we have to write regexp as string.
  • Configuration seems to become complicated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I agree with complexity part, at least partially. It's because I think that better flexibility always will introduce more complexity, in one or different form. Also, keep in mind that we are not replacing old simple approach, we are just adding extension to it. So the RegEx configuration could be used only by more advanced users. But I guess I must agree, that this could increase number of incoming questions and bugs :)

If you feel that the path-template approach is simpler, we can start from there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

@ysugimoto ysugimoto Mar 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, now we can use path-template approach.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I plan to do that

} else if ( typeof directory === "string" ) {
// ./X , ../X , . , ..
if ( directory.match(/^\.\.?\//) || directory.match(/^\.\.?$/) ) {
return path.join(this.dirName, directory, prefix + fileName + suffix + extension);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
95 changes: 82 additions & 13 deletions test/image-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,108 @@
const ImageData = require("../lib/ImageData");
const test = require("ava");

test("ImageData combineWithDirectory Test", 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("[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");
});

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");
});

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 with regex and prefix", t => {
t.is(image.combineWithDirectory({regex: {find: ".*", replace: "d/e"}}, "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"), "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-", "_suffix"), "d/e/prefix-key_suffix.png");
});