export as namespace jsonFactory; // Disable auto-export export {}; type RenameToIn = { [K in keyof T as K extends `in${Uppercase}${Lowercase}` ? `in` : K]: T[K]; }; /** * This is a utility type used below for the "if" operation. * Original: https://stackoverflow.com/a/68373774/765987 */ type MAXIMUM_ALLOWED_BOUNDARY = 80; type Mapped< Tuple extends unknown[], Result extends unknown[] = [], Count extends readonly number[] = [], > = Count["length"] extends MAXIMUM_ALLOWED_BOUNDARY ? Result : Tuple extends [] ? [] : Result extends [] ? Mapped : Mapped; /** * Used for the "if" operation, which takes an array of odd length * and a minimum of three (3) elements. */ type AnyArrayOfOddLengthMin3 = [any, ...Mapped<[any, any]>]; export type ReservedOperations = | "var" | "missing" | "missing_some" | "if" | "==" | "===" | "!=" | "!==" | "!" | "!!" | "or" | "and" | ">" | ">=" | "<" | "<=" | "max" | "min" | "+" | "-" | "*" | "/" | "%" | "map" | "filter" | "reduce" | "all" | "none" | "some" | "merge" | "in" | "cat" | "substr" | "log"; /** * This can be an object with any key except the reserved keys. * TODO: Find a way to limit this type to exactly one (1) key, since * json-logic-js enforces it. See: * https://github.com/jwadhams/json-logic-js/blob/2.0.2/logic.js#L180 */ export type AdditionalOperation = Partial> & { [k: string]: any }; export interface AllReservedOperationsInterface { var: RulesLogic | [RulesLogic] | [RulesLogic, any] | [RulesLogic, any]; missing: RulesLogic | any[]; missing_some: [RulesLogic, RulesLogic | any[]]; if: AnyArrayOfOddLengthMin3; "==": [any, any]; "===": [any, any]; "!=": [any, any]; "!==": [any, any]; "!": any; "!!": any; or: Array>; and: Array>; ">": [RulesLogic, RulesLogic]; ">=": [RulesLogic, RulesLogic]; "<": [RulesLogic, RulesLogic] | [RulesLogic, RulesLogic, RulesLogic]; "<=": [RulesLogic, RulesLogic] | [RulesLogic, RulesLogic, RulesLogic]; max: Array>; min: Array>; "+": Array> | RulesLogic; "-": Array> | RulesLogic; "*": Array> | RulesLogic; "/": Array> | RulesLogic; "%": [RulesLogic, RulesLogic]; map: [RulesLogic, RulesLogic]; filter: [RulesLogic, RulesLogic]; reduce: [RulesLogic, RulesLogic, RulesLogic]; all: [Array>, RulesLogic] | [RulesLogic, RulesLogic]; none: [Array>, RulesLogic] | [RulesLogic, RulesLogic]; some: [Array>, RulesLogic] | [RulesLogic, RulesLogic]; merge: Array> | RulesLogic>; inArray: [RulesLogic, Array>]; inString: [RulesLogic, RulesLogic]; cat: Array>; substr: [RulesLogic, RulesLogic] | [RulesLogic, RulesLogic, RulesLogic]; log: RulesLogic; } export type JsonLogicVar = Pick< AllReservedOperationsInterface, "var" >; export type JsonLogicMissing = Pick< AllReservedOperationsInterface, "missing" >; export type JsonLogicMissingSome = Pick< AllReservedOperationsInterface, "missing_some" >; export type JsonLogicIf = Pick; export type JsonLogicEqual = Pick; export type JsonLogicStrictEqual = Pick; export type JsonLogicNotEqual = Pick; export type JsonLogicNegation = Pick; export type JsonLogicDoubleNegation = Pick; export type JsonLogicOr = Pick< AllReservedOperationsInterface, "or" >; export type JsonLogicAnd = Pick< AllReservedOperationsInterface, "and" >; export type JsonLogicGreaterThan = Pick< AllReservedOperationsInterface, ">" >; export type JsonLogicGreaterThanOrEqual = Pick< AllReservedOperationsInterface, ">=" >; export type JsonLogicLessThan = Pick< AllReservedOperationsInterface, "<" >; export type JsonLogicLessThanOrEqual = Pick< AllReservedOperationsInterface, "<=" >; export type JsonLogicMax = Pick< AllReservedOperationsInterface, "max" >; export type JsonLogicMin = Pick< AllReservedOperationsInterface, "min" >; export type JsonLogicSum = Pick< AllReservedOperationsInterface, "+" >; export type JsonLogicDifference = Pick< AllReservedOperationsInterface, "-" >; export type JsonLogicProduct = Pick< AllReservedOperationsInterface, "*" >; export type JsonLogicQuotient = Pick< AllReservedOperationsInterface, "/" >; export type JsonLogicRemainder = Pick< AllReservedOperationsInterface, "%" >; export type JsonLogicMap = Pick< AllReservedOperationsInterface, "map" >; export type JsonLogicFilter = Pick< AllReservedOperationsInterface, "filter" >; export type JsonLogicReduce = Pick< AllReservedOperationsInterface, "reduce" >; export type JsonLogicAll = Pick< AllReservedOperationsInterface, "all" >; export type JsonLogicNone = Pick< AllReservedOperationsInterface, "none" >; export type JsonLogicSome = Pick< AllReservedOperationsInterface, "some" >; export type JsonLogicMerge = Pick< AllReservedOperationsInterface, "merge" >; export type JsonLogicInArray = RenameToIn< Pick, "inArray"> >; export type JsonLogicInString = RenameToIn< Pick, "inString"> >; export type JsonLogicCat = Pick< AllReservedOperationsInterface, "cat" >; export type JsonLogicSubstr = Pick< AllReservedOperationsInterface, "substr" >; export type JsonLogicLog = Pick< AllReservedOperationsInterface, "log" >; export type RulesLogic = | boolean | string | number | null // Accessing Data - https://jsonlogic.com/operations.html#accessing-data | JsonLogicVar | JsonLogicMissing | JsonLogicMissingSome // Logic and Boolean Operations - https://jsonlogic.com/operations.html#logic-and-boolean-operations | JsonLogicIf | JsonLogicEqual | JsonLogicStrictEqual | JsonLogicNotEqual | JsonLogicStrictNotEqual | JsonLogicNegation | JsonLogicDoubleNegation | JsonLogicOr | JsonLogicAnd // Numeric Operations - https://jsonlogic.com/operations.html#numeric-operations | JsonLogicGreaterThan | JsonLogicGreaterThanOrEqual | JsonLogicLessThan | JsonLogicLessThanOrEqual | JsonLogicMax | JsonLogicMin | JsonLogicSum | JsonLogicDifference | JsonLogicProduct | JsonLogicQuotient | JsonLogicRemainder // Array Operations - https://jsonlogic.com/operations.html#array-operations | JsonLogicMap | JsonLogicFilter | JsonLogicReduce | JsonLogicAll | JsonLogicNone | JsonLogicSome | JsonLogicMerge | JsonLogicInArray // String Operations - https://jsonlogic.com/operations.html#string-operations | JsonLogicInString | JsonLogicCat | JsonLogicSubstr // Miscellaneous - https://jsonlogic.com/operations.html#miscellaneous | JsonLogicLog // Adding Operations (https://jsonlogic.com/add_operation.html) | AddOps; export function add_operation(name: string, code: (...args: any[]) => any): void; export function apply(logic: RulesLogic, data?: unknown): any; export function rm_operation(name: string): void; // These functions are undocumented, but are exported by the real package // so they're typed here for completeness. export function is_logic(logic: any): logic is RulesLogic; export function truthy(value: any): boolean; export function get_operator(logic: Record): string; export function get_values(logic: Record): any; export function uses_data(logic: Record): any[]; export function rule_like(rule: any, pattern: any): boolean;