Comparing version 1.6.0 to 1.6.1
@@ -926,5 +926,20 @@ 'use strict'; | ||
} | ||
// If need to filter keywords such as `function`, need to process them at the upper level. | ||
const isLegalExpressions = (input) => { | ||
// prettier-ignore | ||
const keywords = ["'", '"', '`', ';', '[', '=', 'var', 'let', 'const', 'return']; | ||
for (const word of keywords) { | ||
if (input.includes(word)) { | ||
return false; | ||
} | ||
} | ||
if (/[^\+\-\*\/\%\s]+\(/.test(input)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const execMathExpression = (input, options) => { | ||
const { units, actuator, exec: _exec = true } = options || {}; | ||
const { units, verify, actuator, exec: _exec = true } = options || {}; | ||
if (verify && !isLegalExpressions(input)) { | ||
throw new Error(`Invalid expression: "${input}"`); | ||
} | ||
input = input.replace( | ||
@@ -940,7 +955,7 @@ /(-?\d+(\.\d+)?|NaN|Infinity)([^\d\s\+\-\*\/\.\(\)]+)?/g, | ||
try { | ||
return actuator | ||
? actuator(input, Boolean(_exec)) | ||
: _exec | ||
? exec(`module.exports=(${input});`, 'cjs') | ||
: input; | ||
if (actuator) { | ||
return actuator(input, Boolean(_exec)); | ||
} else { | ||
return _exec ? (0, eval)(`(${input})`) : input; | ||
} | ||
} catch (e) { | ||
@@ -947,0 +962,0 @@ throw new Error(`Invalid expression: "${input}", error: "${e}"`); |
@@ -924,5 +924,20 @@ export { Queue } from 'small-queue'; | ||
} | ||
// If need to filter keywords such as `function`, need to process them at the upper level. | ||
const isLegalExpressions = (input) => { | ||
// prettier-ignore | ||
const keywords = ["'", '"', '`', ';', '[', '=', 'var', 'let', 'const', 'return']; | ||
for (const word of keywords) { | ||
if (input.includes(word)) { | ||
return false; | ||
} | ||
} | ||
if (/[^\+\-\*\/\%\s]+\(/.test(input)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const execMathExpression = (input, options) => { | ||
const { units, actuator, exec: _exec = true } = options || {}; | ||
const { units, verify, actuator, exec: _exec = true } = options || {}; | ||
if (verify && !isLegalExpressions(input)) { | ||
throw new Error(`Invalid expression: "${input}"`); | ||
} | ||
input = input.replace( | ||
@@ -938,7 +953,7 @@ /(-?\d+(\.\d+)?|NaN|Infinity)([^\d\s\+\-\*\/\.\(\)]+)?/g, | ||
try { | ||
return actuator | ||
? actuator(input, Boolean(_exec)) | ||
: _exec | ||
? exec(`module.exports=(${input});`, 'cjs') | ||
: input; | ||
if (actuator) { | ||
return actuator(input, Boolean(_exec)); | ||
} else { | ||
return _exec ? (0, eval)(`(${input})`) : input; | ||
} | ||
} catch (e) { | ||
@@ -945,0 +960,0 @@ throw new Error(`Invalid expression: "${input}", error: "${e}"`); |
@@ -985,5 +985,20 @@ (function (global, factory) { | ||
} | ||
// If need to filter keywords such as `function`, need to process them at the upper level. | ||
const isLegalExpressions = (input) => { | ||
// prettier-ignore | ||
const keywords = ["'", '"', '`', ';', '[', '=', 'var', 'let', 'const', 'return']; | ||
for (const word of keywords) { | ||
if (input.includes(word)) { | ||
return false; | ||
} | ||
} | ||
if (/[^\+\-\*\/\%\s]+\(/.test(input)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const execMathExpression = (input, options) => { | ||
const { units, actuator, exec: _exec = true } = options || {}; | ||
const { units, verify, actuator, exec: _exec = true } = options || {}; | ||
if (verify && !isLegalExpressions(input)) { | ||
throw new Error(`Invalid expression: "${input}"`); | ||
} | ||
input = input.replace( | ||
@@ -999,7 +1014,7 @@ /(-?\d+(\.\d+)?|NaN|Infinity)([^\d\s\+\-\*\/\.\(\)]+)?/g, | ||
try { | ||
return actuator | ||
? actuator(input, Boolean(_exec)) | ||
: _exec | ||
? exec(`module.exports=(${input});`, 'cjs') | ||
: input; | ||
if (actuator) { | ||
return actuator(input, Boolean(_exec)); | ||
} else { | ||
return _exec ? (0, eval)(`(${input})`) : input; | ||
} | ||
} catch (e) { | ||
@@ -1006,0 +1021,0 @@ throw new Error(`Invalid expression: "${input}", error: "${e}"`); |
@@ -27,4 +27,4 @@ export declare const inlineString: (code: string) => string; | ||
export interface ExecMathExpressionOptions { | ||
actuator?: (expr: string, exec: boolean) => number | string; | ||
exec?: boolean; | ||
verify?: boolean; | ||
units?: Record< | ||
@@ -34,2 +34,3 @@ string, | ||
>; | ||
actuator?: (expr: string, exec: boolean) => number | string; | ||
} | ||
@@ -36,0 +37,0 @@ export declare const execMathExpression: <T extends ExecMathExpressionOptions>( |
{ | ||
"name": "aidly", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "Tool library.", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
257533
8232
9