@inzero/expr-eval

Customization

Currently you can only customize aliases and functions.

Functions

Remove some built-in functions.
compileExpr("", {
	functions: (builder) => builder.remove("log", "localeCompare"), 
});

Built-in Functions:

import { getLastElement, hasKey, isArray, isInRange, isNonNullish, isOneOf } from "@inzero/ts-uf";
import type { Dict, DictKey, Func, ROList } from "@inzero/ts-ut";

const builtInFunctions = {
	startsWith,
	endsWith,
	includes,

	has,
	lacks,

	matches,
	isOneOf,
	isInRange,
	isNonNullish,

	localCompare,
	log,
} satisfies Functions;

function startsWith(string: string, prefix: string): boolean {
	return string.startsWith(prefix);
}
function endsWith(string: string, suffix: string): boolean {
	return string.endsWith(suffix);
}
function includes(string: string, substring: string): boolean {
	return string.includes(substring);
}

function has(arrayOrObject: ROList | Dict, elementOrKey: unknown): boolean {
	return isArray(arrayOrObject)
		? arrayOrObject.includes(elementOrKey)
		: hasKey(arrayOrObject, elementOrKey as DictKey);
}
function lacks(arrayOrObject: ROList | Dict, elementOrKey: unknown): boolean {
	return !has(arrayOrObject, elementOrKey);
}

function matches(string: string, regex: RegExp): boolean {
	return regex.test(string);
}

function localCompare(a: string, b: string): number {
	return a.localeCompare(b);
}
function log(...args: unknown[]) {
	console.log("[LOG]:", ...args);
	return getLastElement(args);
}

External Types

Aliases

Remove some built-in aliases.
compileExpr("", {
	aliases: (builder) => builder.remove("enabled", "disabled"), 
});

Built-in aliases:

const defaultAliasesGrouped = {
	boolean: [
		{
			kind: tk.boolean,
			alias: "enabled",
			value: true,
		},
		{
			kind: tk.boolean,
			alias: "disabled",
			value: false,
		},
	],
	logical: [
		{
			kind: tk.operator,
			alias: "and",
			operator: binaryOp.logicalAnd,
		},
		{
			kind: tk.operator,
			alias: "or",
			operator: binaryOp.logicalOr,
		},
	],
	equality: [
		{
			kind: tk.operator,
			alias: "is",
			operator: binaryOp.strictEqual,
		},
		{
			kind: tk.operator,
			alias: "is not",
			operator: binaryOp.strictNotEqual,
		},
	],
	unary: [
		{
			kind: tk.operator,
			alias: "not",
			operator: unaryOp.not,
		},
	],
	identifiers: [
		{
			kind: tk.identifier,
			alias: "isBetween",
			name: "isInRange",
		},
		{
			kind: tk.identifier,
			alias: "in",
			name: "isOneOf",
		},
		{
			kind: tk.identifier,
			alias: "has",
			name: "includes",
		},
	],
};

const defaultAliases = Object.values(defaultAliasesGrouped).flat();

On this page