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

Enable inner parameters in a string #1

Merged
merged 1 commit into from
Oct 6, 2016

Conversation

t00f
Copy link

@t00f t00f commented Oct 6, 2016

This PR intends to add inner parameter support.

Here is an example:

var parse    = require("json-templates"),
    template = parse("Hello {{name:Chris}}");

console.log(template.parameters); // Prints [{ key: "name" }]
console.log(template({ name: "Curran" })); // Prints "Hello Curran"

Note: To avoid calling str.match(regex) twice, I updated the prototype of the function named Parameter to accept the result of the match call instead of a string.

Copy link
Member

@curran curran left a comment

Choose a reason for hiding this comment

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

Great changes! Thank you.

@@ -4,6 +4,7 @@
// By Curran Kelleher
// September 2016

const regex = new RegExp(/{{(\w+)(:?(.*))+}}/i);
Copy link
Member

Choose a reason for hiding this comment

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

Sweet change! Did you come up with that regex yourself? If not I'd like to cite the source.

&&
(str.substr(str.length - 2, 2) === "}}")
);
return regex.test(str);
Copy link
Member

Choose a reason for hiding this comment

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

Awesome simplification here.

@@ -58,13 +58,25 @@ describe("json-template", function() {
assert.equal(template({ unknownParam: "baz" }), "bar:baz");
});

it("should compute template for a string with inner parameter", function() {
Copy link
Member

Choose a reason for hiding this comment

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

Nice, thanks for adding these tests.

@curran curran merged commit ce61759 into datavis-tech:master Oct 6, 2016
@curran
Copy link
Member

curran commented Oct 6, 2016

Just released this in https://github.com/curran/json-templates/releases/tag/v1.1.0 and on NPM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants