Comparing version 1.5.9 to 1.6.0
@@ -803,15 +803,21 @@ 'use strict'; | ||
const queue = []; | ||
keys.sort((a, b) => data[a].count - data[b].count); | ||
const tryRemove = once(() => { | ||
let l = queue.length; | ||
let extra = max - tempSize - diff; | ||
while (~--l) { | ||
if (extra > 0 && extra >= queue[l][1]) { | ||
extra -= queue[l][1]; | ||
} else { | ||
remove(queue[l][0]); | ||
} | ||
} | ||
}); | ||
keys.sort((a, b) => { | ||
const d = data[a].count - data[b].count; | ||
return d === 0 ? data[a].size - data[b].size : d; | ||
}); | ||
for (let i = 0; i < keys.length; i++) { | ||
const u = data[keys[i]]; | ||
if (canSet(tempSize)) { | ||
let l = queue.length; | ||
let extra = max - tempSize - diff; | ||
while (~--l) { | ||
if (extra > 0 && extra >= queue[l][1]) { | ||
extra -= queue[l][1]; | ||
} else { | ||
remove(queue[l][0]); | ||
} | ||
} | ||
tryRemove(); | ||
break; | ||
@@ -831,2 +837,5 @@ } | ||
} | ||
if (canSet(tempSize)) { | ||
tryRemove(); | ||
} | ||
} | ||
@@ -897,3 +906,3 @@ if (canSet(allSize)) { | ||
const inline = (code) => { | ||
const inlineString = (code) => { | ||
const obj = {}; | ||
@@ -906,22 +915,19 @@ obj[code] = true; | ||
const id = `data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`; | ||
return (0, eval)(inline(`import("${id}")`)); | ||
return (0, eval)(inlineString(`import("${id}")`)); | ||
} | ||
const { require, useStrict } = options || {}; | ||
const codes = [ | ||
'function _$c_(module,exports,require){', | ||
useStrict ? '"use strict";' : '', | ||
code, | ||
'\n}', | ||
]; | ||
const ms = { exports: Object.create(null) }; | ||
const fn = (0, eval)(inline(`(()=>${codes.join('')})()`)); | ||
const strictCode = useStrict ? '"use strict";' : ''; | ||
code = `function _$c_(module,exports,require){${strictCode}${code}\n}`; | ||
const fn = (0, eval)(inlineString(`(()=>${code})()`)); | ||
if (type === 'cjs') { | ||
const ms = { exports: Object.create(null) }; | ||
fn(ms, ms.exports, require); | ||
return ms.exports; | ||
} else { | ||
fn(); | ||
} | ||
fn(); | ||
} | ||
// If need to filter keywords such as `function`, | ||
// need to process them at the upper level. | ||
const execMathExpression = (input, { units } = {}) => { | ||
// If need to filter keywords such as `function`, need to process them at the upper level. | ||
const execMathExpression = (input, options) => { | ||
const { units, actuator, exec: _exec = true } = options || {}; | ||
input = input.replace( | ||
@@ -931,9 +937,13 @@ /(-?\d+(\.\d+)?|NaN|Infinity)([^\d\s\+\-\*\/\.\(\)]+)?/g, | ||
if (!u) return n; | ||
const parser = units && units[u]; | ||
const parser = units && (units[u] || units['default']); | ||
if (!parser) throw new Error(`Invalid unit: "${u}"`); | ||
return String(parser(Number(n), u, input)); | ||
return String(parser(n, u, input)); | ||
}, | ||
); | ||
try { | ||
return exec(`module.exports=(${input});`, 'cjs'); | ||
return actuator | ||
? actuator(input, Boolean(_exec)) | ||
: _exec | ||
? exec(`module.exports=(${input});`, 'cjs') | ||
: input; | ||
} catch (e) { | ||
@@ -1820,2 +1830,3 @@ throw new Error(`Invalid expression: "${input}", error: "${e}"`); | ||
exports.idleCallback = idleCallback; | ||
exports.inlineString = inlineString; | ||
exports.isAbsolute = isAbsolute; | ||
@@ -1822,0 +1833,0 @@ exports.isArray = isArray; |
@@ -801,15 +801,21 @@ export { Queue } from 'small-queue'; | ||
const queue = []; | ||
keys.sort((a, b) => data[a].count - data[b].count); | ||
const tryRemove = once(() => { | ||
let l = queue.length; | ||
let extra = max - tempSize - diff; | ||
while (~--l) { | ||
if (extra > 0 && extra >= queue[l][1]) { | ||
extra -= queue[l][1]; | ||
} else { | ||
remove(queue[l][0]); | ||
} | ||
} | ||
}); | ||
keys.sort((a, b) => { | ||
const d = data[a].count - data[b].count; | ||
return d === 0 ? data[a].size - data[b].size : d; | ||
}); | ||
for (let i = 0; i < keys.length; i++) { | ||
const u = data[keys[i]]; | ||
if (canSet(tempSize)) { | ||
let l = queue.length; | ||
let extra = max - tempSize - diff; | ||
while (~--l) { | ||
if (extra > 0 && extra >= queue[l][1]) { | ||
extra -= queue[l][1]; | ||
} else { | ||
remove(queue[l][0]); | ||
} | ||
} | ||
tryRemove(); | ||
break; | ||
@@ -829,2 +835,5 @@ } | ||
} | ||
if (canSet(tempSize)) { | ||
tryRemove(); | ||
} | ||
} | ||
@@ -895,3 +904,3 @@ if (canSet(allSize)) { | ||
const inline = (code) => { | ||
const inlineString = (code) => { | ||
const obj = {}; | ||
@@ -904,22 +913,19 @@ obj[code] = true; | ||
const id = `data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`; | ||
return (0, eval)(inline(`import("${id}")`)); | ||
return (0, eval)(inlineString(`import("${id}")`)); | ||
} | ||
const { require, useStrict } = options || {}; | ||
const codes = [ | ||
'function _$c_(module,exports,require){', | ||
useStrict ? '"use strict";' : '', | ||
code, | ||
'\n}', | ||
]; | ||
const ms = { exports: Object.create(null) }; | ||
const fn = (0, eval)(inline(`(()=>${codes.join('')})()`)); | ||
const strictCode = useStrict ? '"use strict";' : ''; | ||
code = `function _$c_(module,exports,require){${strictCode}${code}\n}`; | ||
const fn = (0, eval)(inlineString(`(()=>${code})()`)); | ||
if (type === 'cjs') { | ||
const ms = { exports: Object.create(null) }; | ||
fn(ms, ms.exports, require); | ||
return ms.exports; | ||
} else { | ||
fn(); | ||
} | ||
fn(); | ||
} | ||
// If need to filter keywords such as `function`, | ||
// need to process them at the upper level. | ||
const execMathExpression = (input, { units } = {}) => { | ||
// If need to filter keywords such as `function`, need to process them at the upper level. | ||
const execMathExpression = (input, options) => { | ||
const { units, actuator, exec: _exec = true } = options || {}; | ||
input = input.replace( | ||
@@ -929,9 +935,13 @@ /(-?\d+(\.\d+)?|NaN|Infinity)([^\d\s\+\-\*\/\.\(\)]+)?/g, | ||
if (!u) return n; | ||
const parser = units && units[u]; | ||
const parser = units && (units[u] || units['default']); | ||
if (!parser) throw new Error(`Invalid unit: "${u}"`); | ||
return String(parser(Number(n), u, input)); | ||
return String(parser(n, u, input)); | ||
}, | ||
); | ||
try { | ||
return exec(`module.exports=(${input});`, 'cjs'); | ||
return actuator | ||
? actuator(input, Boolean(_exec)) | ||
: _exec | ||
? exec(`module.exports=(${input});`, 'cjs') | ||
: input; | ||
} catch (e) { | ||
@@ -1818,2 +1828,3 @@ throw new Error(`Invalid expression: "${input}", error: "${e}"`); | ||
idleCallback, | ||
inlineString, | ||
isAbsolute, | ||
@@ -1820,0 +1831,0 @@ isArray, |
@@ -860,15 +860,21 @@ (function (global, factory) { | ||
const queue = []; | ||
keys.sort((a, b) => data[a].count - data[b].count); | ||
const tryRemove = once(() => { | ||
let l = queue.length; | ||
let extra = max - tempSize - diff; | ||
while (~--l) { | ||
if (extra > 0 && extra >= queue[l][1]) { | ||
extra -= queue[l][1]; | ||
} else { | ||
remove(queue[l][0]); | ||
} | ||
} | ||
}); | ||
keys.sort((a, b) => { | ||
const d = data[a].count - data[b].count; | ||
return d === 0 ? data[a].size - data[b].size : d; | ||
}); | ||
for (let i = 0; i < keys.length; i++) { | ||
const u = data[keys[i]]; | ||
if (canSet(tempSize)) { | ||
let l = queue.length; | ||
let extra = max - tempSize - diff; | ||
while (~--l) { | ||
if (extra > 0 && extra >= queue[l][1]) { | ||
extra -= queue[l][1]; | ||
} else { | ||
remove(queue[l][0]); | ||
} | ||
} | ||
tryRemove(); | ||
break; | ||
@@ -888,2 +894,5 @@ } | ||
} | ||
if (canSet(tempSize)) { | ||
tryRemove(); | ||
} | ||
} | ||
@@ -954,3 +963,3 @@ if (canSet(allSize)) { | ||
const inline = (code) => { | ||
const inlineString = (code) => { | ||
const obj = {}; | ||
@@ -965,22 +974,19 @@ obj[code] = true; | ||
)}`; | ||
return (0, eval)(inline(`import("${id}")`)); | ||
return (0, eval)(inlineString(`import("${id}")`)); | ||
} | ||
const { require, useStrict } = options || {}; | ||
const codes = [ | ||
'function _$c_(module,exports,require){', | ||
useStrict ? '"use strict";' : '', | ||
code, | ||
'\n}', | ||
]; | ||
const ms = { exports: Object.create(null) }; | ||
const fn = (0, eval)(inline(`(()=>${codes.join('')})()`)); | ||
const strictCode = useStrict ? '"use strict";' : ''; | ||
code = `function _$c_(module,exports,require){${strictCode}${code}\n}`; | ||
const fn = (0, eval)(inlineString(`(()=>${code})()`)); | ||
if (type === 'cjs') { | ||
const ms = { exports: Object.create(null) }; | ||
fn(ms, ms.exports, require); | ||
return ms.exports; | ||
} else { | ||
fn(); | ||
} | ||
fn(); | ||
} | ||
// If need to filter keywords such as `function`, | ||
// need to process them at the upper level. | ||
const execMathExpression = (input, { units } = {}) => { | ||
// If need to filter keywords such as `function`, need to process them at the upper level. | ||
const execMathExpression = (input, options) => { | ||
const { units, actuator, exec: _exec = true } = options || {}; | ||
input = input.replace( | ||
@@ -990,9 +996,13 @@ /(-?\d+(\.\d+)?|NaN|Infinity)([^\d\s\+\-\*\/\.\(\)]+)?/g, | ||
if (!u) return n; | ||
const parser = units && units[u]; | ||
const parser = units && (units[u] || units['default']); | ||
if (!parser) throw new Error(`Invalid unit: "${u}"`); | ||
return String(parser(Number(n), u, input)); | ||
return String(parser(n, u, input)); | ||
}, | ||
); | ||
try { | ||
return exec(`module.exports=(${input});`, 'cjs'); | ||
return actuator | ||
? actuator(input, Boolean(_exec)) | ||
: _exec | ||
? exec(`module.exports=(${input});`, 'cjs') | ||
: input; | ||
} catch (e) { | ||
@@ -1883,2 +1893,3 @@ throw new Error(`Invalid expression: "${input}", error: "${e}"`); | ||
exports.idleCallback = idleCallback; | ||
exports.inlineString = inlineString; | ||
exports.isAbsolute = isAbsolute; | ||
@@ -1885,0 +1896,0 @@ exports.isArray = isArray; |
@@ -0,1 +1,2 @@ | ||
export declare const inlineString: (code: string) => string; | ||
export interface ExecOptions { | ||
@@ -5,3 +6,2 @@ useStrict?: boolean; | ||
} | ||
export declare const inline: (code: string) => string; | ||
export declare function exec<T = unknown, U extends never = never>( | ||
@@ -28,7 +28,12 @@ code: string, | ||
export interface ExecMathExpressionOptions { | ||
units?: Record<string, (num: number, unit: string, input: string) => number>; | ||
actuator?: (expr: string, exec: boolean) => number | string; | ||
exec?: boolean; | ||
units?: Record< | ||
string, | ||
(num: string, unit: string, input: string) => number | string | ||
>; | ||
} | ||
export declare const execMathExpression: ( | ||
export declare const execMathExpression: <T extends ExecMathExpressionOptions>( | ||
input: string, | ||
{ units }?: ExecMathExpressionOptions, | ||
) => number; | ||
options?: T | undefined, | ||
) => T['exec'] extends false ? string : number; |
@@ -12,2 +12,3 @@ export { Queue } from 'small-queue'; | ||
execMathExpression, | ||
inlineString, | ||
type ExecOptions, | ||
@@ -14,0 +15,0 @@ type ExecMathExpressionOptions, |
{ | ||
"name": "aidly", | ||
"version": "1.5.9", | ||
"version": "1.6.0", | ||
"description": "Tool library.", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -119,2 +119,3 @@ <div align="center"> | ||
`root` | Alias for global objects, compatible with different environments. | ||
`inlineString` | Quantify the string constant. | ||
`exec` | Execute JavaScript code, which can be run in `cjs`, `esm`, or `normal` mode, with normal mode being the default. | ||
@@ -121,0 +122,0 @@ `execMathExpression` | Execute mathematical expressions. |
@@ -119,2 +119,3 @@ <div align="center"> | ||
`root` | 为全局对象提供的别名,兼容不同环境。 | ||
`inlineString` | 将字符串常量化。 | ||
`exec` | 执行一段 js 代码,可以以 `cjs`,`esm` 和普通模式来执行代码,默认为普通模式。 | ||
@@ -121,0 +122,0 @@ `execMathExpression` | 执行数学计算表达式。 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
256107
8171
128