Skip to content

Commit

Permalink
feat(jexl): add length transform
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Aug 30, 2024
1 parent 4329d13 commit 89fafb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/form/addon/lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ export default class Document extends Base {
});
documentJexl.addTransform("stringify", (input) => JSON.stringify(input));
documentJexl.addTransform("flatten", flatten);
documentJexl.addTransform("length", (input) => {
if (input?.length !== undefined) {
// strings, arrays
return input.length;
} else if (input instanceof Object) {
// objects
return Object.keys(input).length;
}

return null;
});

return documentJexl;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/form/tests/unit/lib/document-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ module("Unit | Library | document", function (hooks) {
);
});

test.each(
"it transforms correcty with count transform",
[
["['test1', 'test2']|length", 2],
["{key: 1}|length", 1],
["'foobar'|length", 6],
["1|length", null],
["1.1|length", null],
["null|length", null],
],
async function (assert, [expression, expected]) {
assert.strictEqual(
await this.document.jexl.eval(expression),
expected,
`Expected expression "${expression}" to evaluate to "${expected}"`,
);
},
);

test("computes the correct jexl context (task form)", async function (assert) {
assert.expect(1);

Expand Down

0 comments on commit 89fafb5

Please sign in to comment.