@leawind/rop
Version:
Evaluate expression with operator overloading and Python-style array slicing using tagged template literal.
264 lines (261 loc) • 7.14 kB
TypeScript
// Generated by dts-bundle-generator v9.5.1
declare const UNARY_OPERATOR_NAMES: readonly [
"!",
"~",
"-x",
"+x"
];
declare const BINARY_OPERATOR_NAMES: readonly [
"+",
"-",
"*",
"/",
"%",
"**",
"&",
"|",
"^",
"<<",
">>",
">>>",
"&&",
"||",
"==",
"===",
"!=",
"!==",
"<",
">",
"<=",
">="
];
declare const OPERATOR_NAMES: readonly [
"[i]",
"[:]",
"!",
"~",
"-x",
"+x",
"+",
"-",
"*",
"/",
"%",
"**",
"&",
"|",
"^",
"<<",
">>",
">>>",
"&&",
"||",
"==",
"===",
"!=",
"!==",
"<",
">",
"<=",
">="
];
export type UnaryOperationName = (typeof UNARY_OPERATOR_NAMES)[number];
export type BinaryOperationName = (typeof BINARY_OPERATOR_NAMES)[number];
export type OperationName = (typeof OPERATOR_NAMES)[number];
export type UnaryOperationArrowFn<T = any, R = any> = ((self: T) => R) | (() => R);
export type BinaryOperationArrowFn<T = any, R = any> = ((self: T, other: any) => R) | ((other: any) => R);
export type OperationFn<R = any> = (...args: any[]) => R;
export type Clazz<T = any> = new (...args: any[]) => T;
export declare class Rop {
/**
* { Clazz.prototype --> { Operation.symbol --> OperationFn } }
*/
private readonly overloadings;
/**
* { identifier_name --> value } []
*
* An array of binding maps. Each binding map contains map of identifier name to value.
*
* Those identifiers can be used in the template string.
*/
readonly bindings: Map<string, any>;
constructor();
/**
* Get the template evaluator function.
*
* This property returns a tagged template function that can evaluate expressions.
*
* ### Example
* ```ts
* const rop = new Rop();
* const result = rop.o`1 + 2`; // 3
* ```
*/
o<T = any>(strs: TemplateStringsArray, ...args: unknown[]): T;
/**
* Bind a value to an identifier, or bind multiple values from an object or Map.
*
* ### Example
*
* ```ts
* const rop = new Rop();
* // Bind a single value
* rop.bind('a', 1).bind('b', 2);
* // Bind multiple values from an object
* rop.bind({ a: 1, b: 2 });
* // Bind multiple values from a Map
* rop.bind(new Map([['a', 1], ['b', 2]]));
*
* const result = rop.o`a + b`; // 3
* ```
*
* @param args - Either a key-value pair, an object with multiple key-value pairs, or a Map
*/
bind(key: string, value: any): Rop;
bind(bindings: Record<string, any>): Rop;
bind(bindings: Map<string, any>): Rop;
/**
* Remove bindings for the specified keys.
*
* ### Example
*
* ```ts
* const rop = new Rop().bind({ a: 1, b: 2 });
* rop.unbind('a');
* rop.unbind('b', 'c');
* ```
*
* @param keys - The keys to unbind
*/
unbind(...keys: string[]): Rop;
/**
* Get the operation symbol for the given operation name.
*
* @param name - The operation name.
* @returns The operation symbol.
* @throws {Error} If the operation name is not valid.
*/
static op(name: OperationName): symbol;
/**
* Set the overloaded operation to a prototype object
*
* @param prototype - The prototype object.
* @param symbol - The operation symbol.
* @param operationFn - The operation function.
*/
private setOverload;
/**
* Overload a single operation for a class.
*
* This method stores the operation function in the Rop instance, not the Class.prototype.
*
* ### Example
*
* ```ts
* class Vec2 {
* constructor(public x: number, public y: number) {}
* }
*
* const rop = new Rop();
* rop.overload(Vec2, '+', (self: Vec2, other: Vec2) => {
* return new Vec2(self.x + other.x, self.y + other.y);
* });
* ```
*
* @param clazz - The class to overload the operation for
* @param operation - The operation name or symbol to overload
* @param operationFn - The function that implements the operation
*
*/
overload<T>(clazz: Clazz, operation: UnaryOperationName, operationFn: UnaryOperationArrowFn<T>): Rop;
overload<T>(clazz: Clazz, operation: BinaryOperationName, operationFn: BinaryOperationArrowFn<T>): Rop;
overload<T>(clazz: Clazz, operation: symbol, operationFn: OperationFn<T>): Rop;
overload<T>(clazz: Clazz, operation: OperationName | symbol, operationFn: OperationFn<T>): Rop;
/**
* Overload multiple operations for a class at once.
*
* ### Examples
*
* ```ts
* rop.overloads(Vec2, {
* // Method (Recommended style)
* '+'(this: Vec2, other: Vec2) {
* return new Vec2(this.x + other.x, this.y + other.y);
* },
* // Normal function
* '*': function (this: Vec2, other: Vec2) {
* return new Vec2(this.x * other.x, this.y * other.y);
* },
* // Arrow function
* '/': (self: Vec2, other: Vec2) => {
* return new Vec2(self.x / other.x, self.y / other.y);
* },
* })
* ```
*
* @param clazz - The class to overload operations for
* @param def - An object mapping operation names or symbols to their implementation functions
*/
overloads<T>(clazz: Clazz, def: Partial<Record<OperationName | symbol, OperationFn<T>>>): Rop;
/**
* Get the overloaded operation function on a prototype chain.
*
* @param prototype prototype object. Get by `Object.getPrototypeOf(inst)` or `Clazz.prototype`
* @param symbol operation symbol. Get by `Operations.symbol(op)` or `Operations.meta(op).symbol`
*/
private getOverloadFromPrototypeChain;
/**
* Get the overloaded operation function for a class.
*
* @param clazz - The class to check for operation overload
* @param symbol - The operation symbol
* @returns The overloaded operation function, or null if not found
*/
getOverloadOnClass<T>(clazz: Clazz<T>, symbol: symbol): OperationFn<T> | null;
/**
* Get the overloaded operation function for an instance.
*
* @param inst - The object instance to check for operation overload
* @param symbol - The operation symbol
* @returns The overloaded operation function, or null if not found
*/
getOverloadOnInstance<T>(inst: T, symbol: symbol): OperationFn | null;
/**
* Bind built-in identifiers like `true`, `false`, `null`, `undefined`, `Infinity`, `NaN`,
* and all properties of the `Math` object.
*/
bindDefaults(): Rop;
/**
* Bind all properties of the `Math` object.
*/
bindMaths(): Rop;
/**
* Overload built-in operations for common classes like Array, String, and Set.
*
* Currently includes:
* - Array `+` for concatenation
* - String `*` for repetition
* - Set `+` for union
* - Set `-` for difference
*/
overloadDefaults(): Rop;
private static instance?;
/**
* The default Rop instance.
*
* - This instance provides default bindings and operations
* - This instance can be modified by calling `Rop#bind`, {@link Rop.overload} or {@link Rop.overloads} on the instance.
* - You can use {@link Rop.resetDefaultInstance()} to reset the default instance to its initial state.
* - You can create your own Rop instance by calling `new Rop()`.
*/
static get INST(): Rop;
/**
* Reset the default Rop instance {@link Rop.INST} to its initial state.
*/
static resetDefaultInstance(): void;
}
/**
* A quick alias for `Rop.INST.o`
*/
export declare const o: <T = any>(strs: TemplateStringsArray, ...args: unknown[]) => T;
export {};