Skip to content

Commit

Permalink
feat: allow fixed arguments values (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjuanes authored Jul 13, 2024
1 parent 752c581 commit 7c0d0a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const escape = s => s.toString().replace(/[&<>\"']/g, m => escapedChars[m]);

const get = (c, p) => (p === "." ? c : p.split(".").reduce((x, k) => x?.[k], c)) ?? "";

const parse = (v, context = {}, vars = {}) => {
if ((v.startsWith(`"`) && v.endsWith(`"`)) || /^-?\d*\.?\d*$/.test(v) || v === "true" || v === "false" || v === "null") {
return JSON.parse(v);
}
return (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(context, v || ".");
};

const defaultHelpers = {
"each": (value, opt) => {
return (typeof value === "object" ? Object.entries(value || {}) : [])
Expand All @@ -34,9 +41,9 @@ const compile = (tokens, output, context, partials, helpers, vars, fn = {}, inde
output.push(get(context, tokens[i].slice(1).trim()));
}
else if (tokens[i].startsWith("#") && typeof helpers[tokens[i].slice(1).trim().split(" ")[0]] === "function") {
const [t, ...args] = tokens[i].slice(1).trim().split(" ");
const [t, ...args] = tokens[i].slice(1).trim().match(/(?:[^\s"]+|"[^"]*")+/g);
const j = i + 1;
output.push(helpers[t](...args.map(v => (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(context, v || ".")), {
output.push(helpers[t](...args.map(v => parse(v, context, vars)), {
context: context,
fn: (blockContext = {}, blockVars = {}, blockOutput = []) => {
i = compile(tokens, blockOutput, blockContext, partials, helpers, {...vars, ...blockVars, root: vars.root}, fn, j, t);
Expand Down Expand Up @@ -70,9 +77,9 @@ const compile = (tokens, output, context, partials, helpers, vars, fn = {}, inde
}
}
else if (tokens[i].startsWith("=")) {
const [t, ...args] = tokens[i].slice(1).trim().split(" ");
const [t, ...args] = tokens[i].slice(1).trim().match(/(?:[^\s"]+|"[^"]*")+/g);
if (typeof fn[t] === "function") {
output.push(fn[t](...args.map(v => (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(context, v || "."))) || "");
output.push(fn[t](...args.map(v => parse(v, context, vars))) || "");
}
}
else if (tokens[i].startsWith("/")) {
Expand Down Expand Up @@ -101,5 +108,6 @@ const mikel = (str, context = {}, opt = {}, output = []) => {
// @description assign utilities
mikel.escape = escape;
mikel.get = get;
mikel.parse = parse;

export default mikel;
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ describe("{{#customHelper }}", () => {
};
assert.equal(m("{{#concat a b}}{{/concat}}!", {a: "hello", b: "world"}, options), "hello world!");
});

it("should allow to provide fixed arguments values", () => {
const options = {
helpers: {
customEqual: (arg1, arg2, opt) => arg1 === arg2 ? opt.fn(opt.context) : "",
},
};
assert.equal(m(`{{#customEqual value "yes"}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "Yes!!");
assert.equal(m(`{{#customEqual value "no"}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "");
assert.equal(m(`{{#customEqual value false}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "");
});
});

describe("{{@root}}", () => {
Expand Down Expand Up @@ -263,4 +274,8 @@ describe("{{=function }}", () => {
it("should render nothing if no function is provided", () => {
assert.equal(m("Result: {{=noop value}}", {value: "a"}), "Result: ");
});

it("should allow to provide fixed arguments values", () => {
assert.equal(m(`{{=concat "Hello" "World"}}!`, {}, options), "Hello World!");
});
});

0 comments on commit 7c0d0a1

Please sign in to comment.