Skip to content

Commit

Permalink
fix: standard functions logic (#64)
Browse files Browse the repository at this point in the history
* fix: object index filters

* fix: checking of standard functions
  • Loading branch information
koladilip authored Jun 7, 2024
1 parent 669ec7a commit 5f068ea
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/operator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isStandardFunction, standardFunctions } from './operators';

describe('Operators tests', () => {
describe('isStandardFunction', () => {
it('should return true for standard functions', () => {
expect(Object.keys(standardFunctions).every(isStandardFunction)).toBeTruthy();
});
it('should return false for non standard function', () => {
const nonStandardFunctions = [
'toString',
'valueOf',
'toLocaleString',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor',
];
expect(Object.keys(nonStandardFunctions).every(isStandardFunction)).toBeFalsy();
});
});
});
4 changes: 4 additions & 0 deletions src/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,7 @@ export const standardFunctions = {
}`,
keys: 'function keys(obj) { return Object.keys(obj); }',
};

export function isStandardFunction(name: string): boolean {
return Object.prototype.hasOwnProperty.call(standardFunctions, name);
}
4 changes: 2 additions & 2 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
VARS_PREFIX,
} from './constants';
import { JsonTemplateTranslatorError } from './errors';
import { binaryOperators, standardFunctions } from './operators';
import { binaryOperators, isStandardFunction, standardFunctions } from './operators';
import {
ArrayExpression,
AssignmentExpression,
Expand Down Expand Up @@ -514,7 +514,7 @@ export class JsonTemplateTranslator {
}
const functionArgsStr = this.translateSpreadableExpressions(expr.args, result, code);
const functionName = this.getFunctionName(expr, result);
if (expr.id && standardFunctions[expr.id]) {
if (expr.id && isStandardFunction(expr.id)) {
this.standardFunctions[expr.id] = standardFunctions[expr.id];
code.push(`if(${functionName} && typeof ${functionName} === 'function'){`);
code.push(result, '=', functionName, '(', functionArgsStr, ');');
Expand Down
2 changes: 1 addition & 1 deletion test/scenarios/functions/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const data: Scenario[] = [
},
{
templatePath: 'js_date_function.jt',
output: [2022, 8, 19],
output: ['2022', 8, 19],
},
{
templatePath: 'new_operator.jt',
Expand Down
2 changes: 1 addition & 1 deletion test/scenarios/functions/js_date_function.jt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const date = new Date('2022-08-19');
[date.getFullYear(), date.getMonth() + 1, date.getDate()];
[date.getFullYear().toString(), date.getMonth() + 1, date.getDate()];

0 comments on commit 5f068ea

Please sign in to comment.