PromucFlow_constructor/app/shared/ast/src/utils.ts
ChandanBalajiBP e5cdfbe445
feat: AST based entity refactor (#17434)
* task: AST based entity refactor

* implemented refactor logic

* jest cases with string manipulation using AST logic

* comments and indentation

* added evalVersion to request
2022-10-18 12:07:06 +05:30

22 lines
930 B
TypeScript

import unescapeJS from 'unescape-js';
const beginsWithLineBreakRegex = /^\s+|\s+$/;
export function sanitizeScript(js: string, evaluationVersion: number) {
// We remove any line breaks from the beginning of the script because that
// makes the final function invalid. We also unescape any escaped characters
// so that eval can happen
//default value of evalutaion version is 2
evaluationVersion = evaluationVersion ? evaluationVersion : 2;
const trimmedJS = js.replace(beginsWithLineBreakRegex, '');
return evaluationVersion > 1 ? trimmedJS : unescapeJS(trimmedJS);
}
// For the times when you need to know if something truly an object like { a: 1, b: 2}
// typeof, lodash.isObject and others will return false positives for things like array, null, etc
export const isTrueObject = (
item: unknown
): item is Record<string, unknown> => {
return Object.prototype.toString.call(item) === '[object Object]';
};