cordis 4.0.0-rc.6, plugin-loader, -include, -group, -timer, -hmr, -logger-console, cosmokit 1.8.1, schemastery 3.18.0 — copied from the cordis-workspace checkout, flattened under vendor/, original npm names, private: true. vendor/README.md is the manifest: upstream repos + commit SHAs, local-modification log, sync procedure. Local modification: hmr's locale YAML imports and .i18n() call removed (avoids a runtime YAML import hook we don't vendor).
33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import { valueMap } from 'cosmokit'
|
|
|
|
// eslint-disable-next-line no-new-func
|
|
/** Evaluate a JavaScript expression against a loader context scope. */
|
|
export const evaluate = new Function('ctx', 'expr', `
|
|
with (ctx) {
|
|
return eval(expr)
|
|
}
|
|
`) as ((ctx: object, expr: string) => any)
|
|
|
|
/** Recursively replace YAML `!js` expression nodes with evaluated values. */
|
|
export function interpolate(ctx: object, value: any) {
|
|
if (isJsExpr(value)) {
|
|
return evaluate(ctx, value.__jsExpr)
|
|
} else if (!value || typeof value !== 'object') {
|
|
return value
|
|
} else if (Array.isArray(value)) {
|
|
return value.map(item => interpolate(ctx, item))
|
|
} else {
|
|
return valueMap(value, item => interpolate(ctx, item))
|
|
}
|
|
}
|
|
|
|
/** Return true when a value is a serialized loader JavaScript expression. */
|
|
export function isJsExpr(value: any): value is JsExpr {
|
|
return value instanceof Object && '__jsExpr' in value
|
|
}
|
|
|
|
/** Serialized JavaScript expression produced by the include YAML tag. */
|
|
export interface JsExpr {
|
|
__jsExpr: string
|
|
}
|