Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ export function COALESCE(...args) {
}
}
};

/**
* Return the size on an array as text, with an optional given prefix
*/
export function BADGE(array, prefix = '') {
const length = array?.length || '';
return `${prefix}${length}`;
}
43 changes: 41 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { describe, it } from "node:test";
import assert from "node:assert";

Expand Down Expand Up @@ -183,5 +182,45 @@ describe("flattenContext", () => {
a_0__b_1__c_3: 25,
},
)
})
});
});

describe("badge", () => {
it("should return the length of an array or string", () => {
const expression = `BADGE($var)`;
const context = {$var: [100]};
assert.equal(evaluate(expression, context), "1");
const context2 = {$var: [1,2,3]};
assert.equal(evaluate(expression, context2), "3");
const context3 = {$var: 'hello'};
assert.equal(evaluate(expression, context3), "5");
const context4 = {$var: {"a": "1", "b": "2"}};
assert.equal(evaluate(expression, context4), "");
});
it("should return 0 if the value is not an array or is empty or undefined", () => {
const expression = `BADGE($var)`;
const context = {$var: []};
assert.equal(evaluate(expression, context), "");
const context2 = {$var: undefined};
assert.equal(evaluate(expression, context2), "");
const context3 = {$var: 1};
assert.equal(evaluate(expression, context3), "");
});
it("should return the length of an array, which is populated with objects", () => {
const expression = `BADGE($local.add_media__media)`;
const context = {
$local: {
add_media__media: [
{ id: 1, name: "a" },
{ id: 2, name: { abc: 1 }},
]
}
};
assert.equal(evaluate(expression, context), "2");
});
it("should include the given prefix when provided", () => {
const expression = `BADGE($var,'count:')`;
const context = {$var: [100]};
assert.equal(evaluate(expression, context), "count:1");
});
});