jexl-extended
Version:
Extended grammar for Javascript Expression Language (JEXL)
266 lines (265 loc) • 12.8 kB
TypeScript
/**
* Casts the input to a string.
*
* @example
* ```jexl
* string(123) // "123"
* 123|string // "123"
* ```
* @group Type Conversion
*
* @param input The input can be any type.
* @param prettify If true, the output will be pretty-printed.
*/
export declare const toString: (input: unknown, prettify?: boolean) => string;
/**
* Parses the string and returns a JSON object.
*
* @example
* ```jexl
* parseJson('{"key": "value"}') // { key: "value" }
* '{"key": "value"}'|toJson // { key: "value" }
*/
export declare const toJson: (input: string) => any;
/**
* Returns the number of characters in a string, or the length of an array.
*
* @example
* ```jexl
* length("hello") // 5
* length([1, 2, 3]) // 3
* ```
*
* @param input The input can be a string, an array, or an object.
* @returns The number of characters in a string, or the length of an array.
*/
export declare const length: (input: unknown) => number;
/**
* Gets a substring of a string.
*
* @example
* ```jexl
* substring("hello world", 0, 5) // "hello"
* ```
*
* @param input The input string.
* @param start The starting index of the substring.
* @param length The length of the substring.
* @returns The substring of the input string.
*/
export declare const substring: (input: unknown, start: number, length: number | undefined) => string;
/**
* Returns the substring before the first occurrence of the character sequence chars in str.
*
* @example
* ```jexl
* substringBefore("hello world", " ") // "hello"
* ```
* @param input The input string.
* @param chars The character sequence to search for.
* @returns The substring before the first occurrence of the character sequence chars in str.
*/
export declare const substringBefore: (input: unknown, chars: unknown) => string;
/**
* Returns the substring after the first occurrence of the character sequence chars in str.
*
* @example
* ```jexl
* substringAfter("hello world", " ") // "world"
* ```
*
* @param input The input string.
* @param chars The character sequence to search for.
* @returns The substring after the first occurrence of the character sequence chars in str.
*/
export declare const substringAfter: (input: unknown, chars: unknown) => string;
/** Converts the input string to uppercase. */
export declare const uppercase: (input: unknown) => string;
/** Converts the input string to lowercase. */
export declare const lowercase: (input: unknown) => string;
/** Converts the input string to camel case. */
export declare const camelCase: (input: unknown) => string;
/** Converts the input string to pascal case. */
export declare const pascalCase: (input: unknown) => string;
/** Trims whitespace from both ends of a string. */
export declare const trim: (input: unknown, trimChar?: string) => string;
/** Pads the input string on both sides to center it. */
export declare const pad: (input: unknown, width: number, char?: string) => string;
/** Checks if the input string contains the specified substring. */
export declare const contains: (input: unknown, search: string) => boolean;
/** Checks if the input string starts with the specified substring. */
export declare const startsWith: (input: unknown, search: string) => boolean;
/** Checks if the input string ends with the specified substring. */
export declare const endsWith: (input: unknown, search: string) => boolean;
/** Splits the input string into an array of substrings. */
export declare const split: (input: unknown, separator: string) => string[];
/** Joins elements of an array into a string. */
export declare const arrayJoin: (input: unknown, separator?: string) => string;
/** Replaces occurrences of a specified string. */
export declare const replace: (input: unknown, search: string, replacement: string) => string;
/** Encodes a string to Base64. */
export declare const base64Encode: (input: unknown) => string;
/** Decodes a Base64 encoded string. */
export declare const base64Decode: (input: unknown) => string;
/** Encodes a string or object to URI. */
export declare const formUrlEncoded: (input: unknown) => string;
/** Converts the input to a number. */
export declare const toNumber: (input: unknown) => number;
/** Parses a string and returns an integer. */
export declare const parseInteger: (input: unknown) => number;
/** Returns the absolute value of a number. */
export declare const absoluteValue: (input: unknown) => number;
/** Rounds a number down to the nearest integer. */
export declare const floor: (input: unknown) => number;
/** Rounds a number up to the nearest integer. */
export declare const ceil: (input: unknown) => number;
/** Rounds a number to the nearest integer. */
export declare const round: (input: unknown, decimals?: number) => number;
/** Returns the value of a number raised to a power. */
export declare const power: (input: unknown, exponent?: number) => number;
/** Returns the square root of a number. */
export declare const sqrt: (input: unknown) => number;
/** Generates a random number between 0 (inclusive) and 1 (exclusive). */
export declare const randomNumber: () => number;
/** Casts the number to a string and formats it to a decimal representation as specified by the format string. */
export declare const formatNumber: (input: unknown, format: string) => string;
/** Formats a number as a string in the specified base. */
export declare const formatBase: (input: unknown, base: number) => string;
/** Formats a number as an integer. */
export declare const formatInteger: (input: unknown, format: string) => string;
/** Calculates the sum of an array of numbers. */
export declare const sum: (...input: unknown[]) => number;
/** Finds the maximum value in an array of numbers. */
export declare const max: (...input: unknown[]) => number;
/** Finds the minimum value in an array of numbers. */
export declare const min: (...input: unknown[]) => number;
/** Calculates the average of an array of numbers. */
export declare const average: (...input: unknown[]) => number;
/** Converts the input to a boolean. */
export declare const toBoolean: (input: unknown) => boolean;
/** Returns the logical NOT of the input. */
export declare const not: (input: unknown) => boolean;
/**
* Evaluates a list of predicates and returns the first result expression whose predicate is satisfied.
*
* @example
* ```jexl
* switch(expression, case1, result1, case2, result2, ..., default)
* ```
*
* @param args The arguments array where the first element is the expression to evaluate, followed by pairs of case and result, and optionally a default value.
* @returns The result of the first case whose predicate is satisfied, or the default value if no case is satisfied.
*/
export declare const switchCase: (...args: unknown[]) => unknown;
/** Appends an element to an array. */
export declare const arrayAppend: (...input: unknown[]) => unknown[];
/** Reverses the elements of an array. */
export declare const arrayReverse: (...input: unknown[]) => unknown[];
/** Shuffles the elements of an array. */
export declare const arrayShuffle: (input: unknown[]) => unknown[];
/** Sorts the elements of an array. */
export declare const arraySort: (input: unknown[], expression?: string, descending?: boolean) => unknown[];
/** Returns a new array with the elements of the input array with duplicates removed. */
export declare const arrayDistinct: (input: unknown[]) => unknown[];
/** Create a new object based on an array of key-value pairs. */
export declare const arrayToObject: (input: unknown, val?: unknown) => any;
/** Returns a new array with the elements of the input array transformed by the specified map function. */
export declare const mapField: (input: unknown[], field: string) => any[];
/**
* Returns an array containing the results of applying the expression parameter to each value in the array parameter.
* The expression must be a valid JEXL expression string, which is applied to each element of the array.
* The relative context provided to the expression is an object with the properties value, index and array (the original array).
*/
export declare const arrayMap: (input: unknown[], expression: string) => any[];
/**
* Checks whether the provided array has any elements that match the specified expression.
* The expression must be a valid JEXL expression string, and is applied to each element of the array.
* The relative context provided to the expression is an object with the properties value and array (the original array).
*/
export declare const arrayAny: (input: unknown[], expression: string) => boolean;
/**
* Checks whether the provided array has all elements that match the specified expression.
* The expression must be a valid JEXL expression string, and is applied to each element of the array.
* The relative context provided to the expression is an object with the properties value and array (the original array).
*/
export declare const arrayEvery: (input: unknown[], expression: string) => boolean;
/**
* Returns a new array with the elements of the input array that match the specified expression.
* The expression must be a valid JEXL expression string, and is applied to each element of the array.
* The relative context provided to the expression is an object with the properties value and array (the original array).
*/
export declare const arrayFilter: (input: unknown[], expression: string) => unknown[];
/**
* Finds the first element in an array that matches the specified expression.
* The expression must be a valid JEXL expression string, and is applied to each element of the array.
* The relative context provided to the expression is an object with the properties value and array (the original array).
*/
export declare const arrayFind: (input: unknown[], expression: string) => unknown;
/**
* Returns an aggregated value derived from applying the function parameter successively to each value in array in combination with the result of the previous application of the function.
* The expression must be a valid JEXL expression string, and behaves like an infix operator between each value within the array.
* The relative context provided to the expression is an object with the properties accumulator, value, index and array (the original array).
*/
export declare const arrayReduce: (input: unknown[], expression: string, initialValue: unknown) => unknown;
/**
* Returns the keys of an object.
*/
export declare const objectKeys: (input: unknown) => string[];
/**
* Returns the values of an object.
*/
export declare const objectValues: (input: unknown) => any[];
/**
* Returns an array of key-value pairs from the input object.
*/
export declare const objectEntries: (input: unknown) => [string, any][];
/**
* Returns a new object with the properties of the input objects merged together.
*/
export declare const objectMerge: (...args: unknown[]) => Record<string, unknown>;
/**
* Returns the current date and time in the ISO 8601 format.
*/
export declare const now: () => string;
/**
* Returns the current date and time in milliseconds since the Unix epoch.
*/
export declare const millis: () => number;
/**
* Parses the number of milliseconds since the Unix epoch or parses a string (with or without specified format) and returns the date and time in the ISO 8601 format.
*/
export declare const toDateTime: (input?: number | string, format?: string) => string;
/**
* Converts a date and time to a provided format.
*
* @example
* ```typescript
* dateTimeFormat(datetime, format)
* $dateTimeFormat(datetime, format)
* datetime|dateTimeFormat(format)
* ```
*
* @param input The input date and time, either as a string or number.
* @param format The format to convert the date and time to.
* @returns The date and time in the specified format.
*/
export declare const dateTimeFormat: (input: number | string, format: string) => string | null;
/**
* Parses the date and time in the ISO 8601 format and returns the number of milliseconds since the Unix epoch.
*/
export declare const dateTimeToMillis: (input: string) => number;
/**
* Adds a time range to a date and time in the ISO 8601 format.
*/
export declare const dateTimeAdd: (input: string, unit: string, value: number) => string;
/**
* Evaluate provided and return the result.
* If only one argument is provided, it is expected that the first argument is a JEXL expression.
* If two arguments are provided, the first argument is the context (must be an object) and the second argument is the JEXL expression.
* The expression uses the default JEXL extended grammar and can't use any custom defined functions or transforms.
*/
export declare const _eval: (input: unknown, expression: string) => any;
/**
* Generate a new UUID (Universally Unique Identifier).
*/
export declare const uuid: () => string;