Skip to content

Commit

Permalink
fix: enhance sum function
Browse files Browse the repository at this point in the history
  • Loading branch information
adyfk committed Oct 25, 2023
1 parent f97311a commit a9bb5ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/createParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,22 @@ export function createParser(props: ExpressionParserConstructor = {}) {
return (condition) ? truthy : falsy
},
// ARRAY ==================================================================================
includes: (_, value, arr: any[]) => arr.includes(value),
includes: (_, value: any, arr: any[]) => arr.includes(value),
min: (_, ...args) => Math.min(...args),
max: (_, ...args) => Math.max(...args),
sum: (_, arr) => arr.reduce((prev: number, curr: number) => prev + curr, 0),
sum: (state, arr, filterExpression: string) => arr.reduce((prev: number, curr: number, index: number) => {
if (!filterExpression) return prev + curr;

const result = parser.evaluate(
filterExpression,
{
...state.variables,
_item_: curr,
_index_: index
},
);
return prev + result;
}, 0),
length: (_, value) => value?.length || 0,
join: (_, arr: string[], arg: string) => arr.join(arg),
filter: (state, items: any[], filterExpression: string) => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"target": "es2016",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
Expand Down

0 comments on commit a9bb5ea

Please sign in to comment.