Skip to content

Commit 8cd1b8d

Browse files
committed
Initial bunification
1 parent 9c805e9 commit 8cd1b8d

File tree

11 files changed

+1408
-192
lines changed

11 files changed

+1408
-192
lines changed

.editorconfig

Lines changed: 0 additions & 21 deletions
This file was deleted.

.eslintrc.json

Lines changed: 0 additions & 33 deletions
This file was deleted.

bower.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

bun.lockb

5.13 KB
Binary file not shown.

logic.d.ts

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
export as namespace jsonFactory;
2+
3+
// Disable auto-export
4+
export {};
5+
6+
type RenameToIn<T> = {
7+
[K in keyof T as K extends `in${Uppercase<string>}${Lowercase<string>}`
8+
? `in`
9+
: K]: T[K];
10+
};
11+
12+
/**
13+
* This is a utility type used below for the "if" operation.
14+
* Original: https://stackoverflow.com/a/68373774/765987
15+
*/
16+
type MAXIMUM_ALLOWED_BOUNDARY = 80;
17+
type Mapped<
18+
Tuple extends unknown[],
19+
Result extends unknown[] = [],
20+
Count extends readonly number[] = []
21+
> = Count["length"] extends MAXIMUM_ALLOWED_BOUNDARY
22+
? Result
23+
: Tuple extends []
24+
? []
25+
: Result extends []
26+
? Mapped<Tuple, Tuple, [...Count, 1]>
27+
: Mapped<Tuple, Result | [...Result, ...Tuple], [...Count, 1]>;
28+
/**
29+
* Used for the "if" operation, which takes an array of odd length
30+
* and a minimum of three (3) elements.
31+
*/
32+
type AnyArrayOfOddLengthMin3 = [any, ...Mapped<[any, any]>];
33+
34+
export type ReservedOperations =
35+
| "var"
36+
| "missing"
37+
| "missing_some"
38+
| "if"
39+
| "=="
40+
| "==="
41+
| "!="
42+
| "!=="
43+
| "!"
44+
| "!!"
45+
| "or"
46+
| "and"
47+
| ">"
48+
| ">="
49+
| "<"
50+
| "<="
51+
| "max"
52+
| "min"
53+
| "+"
54+
| "-"
55+
| "*"
56+
| "/"
57+
| "%"
58+
| "map"
59+
| "filter"
60+
| "reduce"
61+
| "all"
62+
| "none"
63+
| "some"
64+
| "merge"
65+
| "in"
66+
| "cat"
67+
| "substr"
68+
| "log";
69+
70+
/**
71+
* This can be an object with any key except the reserved keys.
72+
* TODO: Find a way to limit this type to exactly one (1) key, since
73+
* json-logic-js enforces it. See:
74+
* https://github.com/jwadhams/json-logic-js/blob/2.0.2/logic.js#L180
75+
*/
76+
export type AdditionalOperation = Partial<Record<ReservedOperations, never>> & {
77+
[k: string]: any;
78+
};
79+
80+
export interface AllReservedOperationsInterface<
81+
AddOps extends AdditionalOperation = never
82+
> {
83+
var:
84+
| RulesLogic<AddOps>
85+
| [RulesLogic<AddOps>]
86+
| [RulesLogic<AddOps>, any]
87+
| [RulesLogic<AddOps>, any];
88+
missing: RulesLogic<AddOps> | any[];
89+
missing_some: [RulesLogic<AddOps>, RulesLogic<AddOps> | any[]];
90+
if: AnyArrayOfOddLengthMin3;
91+
"==": [any, any];
92+
"===": [any, any];
93+
"!=": [any, any];
94+
"!==": [any, any];
95+
"!": any;
96+
"!!": any;
97+
or: Array<RulesLogic<AddOps>>;
98+
and: Array<RulesLogic<AddOps>>;
99+
">": [RulesLogic<AddOps>, RulesLogic<AddOps>];
100+
">=": [RulesLogic<AddOps>, RulesLogic<AddOps>];
101+
"<":
102+
| [RulesLogic<AddOps>, RulesLogic<AddOps>]
103+
| [RulesLogic<AddOps>, RulesLogic<AddOps>, RulesLogic<AddOps>];
104+
"<=":
105+
| [RulesLogic<AddOps>, RulesLogic<AddOps>]
106+
| [RulesLogic<AddOps>, RulesLogic<AddOps>, RulesLogic<AddOps>];
107+
max: Array<RulesLogic<AddOps>>;
108+
min: Array<RulesLogic<AddOps>>;
109+
"+": Array<RulesLogic<AddOps>> | RulesLogic<AddOps>;
110+
"-": Array<RulesLogic<AddOps>> | RulesLogic<AddOps>;
111+
"*": Array<RulesLogic<AddOps>> | RulesLogic<AddOps>;
112+
"/": Array<RulesLogic<AddOps>> | RulesLogic<AddOps>;
113+
"%": [RulesLogic<AddOps>, RulesLogic<AddOps>];
114+
map: [RulesLogic<AddOps>, RulesLogic<AddOps>];
115+
filter: [RulesLogic<AddOps>, RulesLogic<AddOps>];
116+
reduce: [RulesLogic<AddOps>, RulesLogic<AddOps>, RulesLogic<AddOps>];
117+
all:
118+
| [Array<RulesLogic<AddOps>>, RulesLogic<AddOps>]
119+
| [RulesLogic<AddOps>, RulesLogic<AddOps>];
120+
none:
121+
| [Array<RulesLogic<AddOps>>, RulesLogic<AddOps>]
122+
| [RulesLogic<AddOps>, RulesLogic<AddOps>];
123+
some:
124+
| [Array<RulesLogic<AddOps>>, RulesLogic<AddOps>]
125+
| [RulesLogic<AddOps>, RulesLogic<AddOps>];
126+
merge: Array<Array<RulesLogic<AddOps>> | RulesLogic<AddOps>>;
127+
inArray: [RulesLogic<AddOps>, Array<RulesLogic<AddOps>>];
128+
inString: [RulesLogic<AddOps>, RulesLogic<AddOps>];
129+
cat: Array<RulesLogic<AddOps>>;
130+
substr:
131+
| [RulesLogic<AddOps>, RulesLogic<AddOps>]
132+
| [RulesLogic<AddOps>, RulesLogic<AddOps>, RulesLogic<AddOps>];
133+
log: RulesLogic<AddOps>;
134+
}
135+
136+
export type JsonLogicVar<AddOps extends AdditionalOperation = never> = Pick<
137+
AllReservedOperationsInterface<AddOps>,
138+
"var"
139+
>;
140+
export type JsonLogicMissing<AddOps extends AdditionalOperation = never> = Pick<
141+
AllReservedOperationsInterface<AddOps>,
142+
"missing"
143+
>;
144+
export type JsonLogicMissingSome<AddOps extends AdditionalOperation = never> =
145+
Pick<AllReservedOperationsInterface<AddOps>, "missing_some">;
146+
export type JsonLogicIf = Pick<AllReservedOperationsInterface, "if">;
147+
export type JsonLogicEqual = Pick<AllReservedOperationsInterface, "==">;
148+
export type JsonLogicStrictEqual = Pick<AllReservedOperationsInterface, "===">;
149+
export type JsonLogicNotEqual = Pick<AllReservedOperationsInterface, "!=">;
150+
export type JsonLogicStrictNotEqual = Pick<
151+
AllReservedOperationsInterface,
152+
"!=="
153+
>;
154+
export type JsonLogicNegation = Pick<AllReservedOperationsInterface, "!">;
155+
export type JsonLogicDoubleNegation = Pick<
156+
AllReservedOperationsInterface,
157+
"!!"
158+
>;
159+
export type JsonLogicOr<AddOps extends AdditionalOperation = never> = Pick<
160+
AllReservedOperationsInterface<AddOps>,
161+
"or"
162+
>;
163+
export type JsonLogicAnd<AddOps extends AdditionalOperation = never> = Pick<
164+
AllReservedOperationsInterface<AddOps>,
165+
"and"
166+
>;
167+
export type JsonLogicGreaterThan<AddOps extends AdditionalOperation = never> =
168+
Pick<AllReservedOperationsInterface<AddOps>, ">">;
169+
export type JsonLogicGreaterThanOrEqual<
170+
AddOps extends AdditionalOperation = never
171+
> = Pick<AllReservedOperationsInterface<AddOps>, ">=">;
172+
export type JsonLogicLessThan<AddOps extends AdditionalOperation = never> =
173+
Pick<AllReservedOperationsInterface<AddOps>, "<">;
174+
export type JsonLogicLessThanOrEqual<
175+
AddOps extends AdditionalOperation = never
176+
> = Pick<AllReservedOperationsInterface<AddOps>, "<=">;
177+
export type JsonLogicMax<AddOps extends AdditionalOperation = never> = Pick<
178+
AllReservedOperationsInterface<AddOps>,
179+
"max"
180+
>;
181+
export type JsonLogicMin<AddOps extends AdditionalOperation = never> = Pick<
182+
AllReservedOperationsInterface<AddOps>,
183+
"min"
184+
>;
185+
export type JsonLogicSum<AddOps extends AdditionalOperation = never> = Pick<
186+
AllReservedOperationsInterface<AddOps>,
187+
"+"
188+
>;
189+
export type JsonLogicDifference<AddOps extends AdditionalOperation = never> =
190+
Pick<AllReservedOperationsInterface<AddOps>, "-">;
191+
export type JsonLogicProduct<AddOps extends AdditionalOperation = never> = Pick<
192+
AllReservedOperationsInterface<AddOps>,
193+
"*"
194+
>;
195+
export type JsonLogicQuotient<AddOps extends AdditionalOperation = never> =
196+
Pick<AllReservedOperationsInterface<AddOps>, "/">;
197+
export type JsonLogicRemainder<AddOps extends AdditionalOperation = never> =
198+
Pick<AllReservedOperationsInterface<AddOps>, "%">;
199+
export type JsonLogicMap<AddOps extends AdditionalOperation = never> = Pick<
200+
AllReservedOperationsInterface<AddOps>,
201+
"map"
202+
>;
203+
export type JsonLogicFilter<AddOps extends AdditionalOperation = never> = Pick<
204+
AllReservedOperationsInterface<AddOps>,
205+
"filter"
206+
>;
207+
export type JsonLogicReduce<AddOps extends AdditionalOperation = never> = Pick<
208+
AllReservedOperationsInterface<AddOps>,
209+
"reduce"
210+
>;
211+
export type JsonLogicAll<AddOps extends AdditionalOperation = never> = Pick<
212+
AllReservedOperationsInterface<AddOps>,
213+
"all"
214+
>;
215+
export type JsonLogicNone<AddOps extends AdditionalOperation = never> = Pick<
216+
AllReservedOperationsInterface<AddOps>,
217+
"none"
218+
>;
219+
export type JsonLogicSome<AddOps extends AdditionalOperation = never> = Pick<
220+
AllReservedOperationsInterface<AddOps>,
221+
"some"
222+
>;
223+
export type JsonLogicMerge<AddOps extends AdditionalOperation = never> = Pick<
224+
AllReservedOperationsInterface<AddOps>,
225+
"merge"
226+
>;
227+
export type JsonLogicInArray<AddOps extends AdditionalOperation = never> =
228+
RenameToIn<Pick<AllReservedOperationsInterface<AddOps>, "inArray">>;
229+
export type JsonLogicInString<AddOps extends AdditionalOperation = never> =
230+
RenameToIn<Pick<AllReservedOperationsInterface<AddOps>, "inString">>;
231+
export type JsonLogicCat<AddOps extends AdditionalOperation = never> = Pick<
232+
AllReservedOperationsInterface<AddOps>,
233+
"cat"
234+
>;
235+
export type JsonLogicSubstr<AddOps extends AdditionalOperation = never> = Pick<
236+
AllReservedOperationsInterface<AddOps>,
237+
"substr"
238+
>;
239+
export type JsonLogicLog<AddOps extends AdditionalOperation = never> = Pick<
240+
AllReservedOperationsInterface<AddOps>,
241+
"log"
242+
>;
243+
244+
export type RulesLogic<AddOps extends AdditionalOperation = never> =
245+
| boolean
246+
| string
247+
| number
248+
// Accessing Data - https://jsonlogic.com/operations.html#accessing-data
249+
| JsonLogicVar<AddOps>
250+
| JsonLogicMissing<AddOps>
251+
| JsonLogicMissingSome<AddOps>
252+
// Logic and Boolean Operations - https://jsonlogic.com/operations.html#logic-and-boolean-operations
253+
| JsonLogicIf
254+
| JsonLogicEqual
255+
| JsonLogicStrictEqual
256+
| JsonLogicNotEqual
257+
| JsonLogicStrictNotEqual
258+
| JsonLogicNegation
259+
| JsonLogicDoubleNegation
260+
| JsonLogicOr<AddOps>
261+
| JsonLogicAnd<AddOps>
262+
// Numeric Operations - https://jsonlogic.com/operations.html#numeric-operations
263+
| JsonLogicGreaterThan<AddOps>
264+
| JsonLogicGreaterThanOrEqual<AddOps>
265+
| JsonLogicLessThan<AddOps>
266+
| JsonLogicLessThanOrEqual<AddOps>
267+
| JsonLogicMax<AddOps>
268+
| JsonLogicMin<AddOps>
269+
| JsonLogicSum<AddOps>
270+
| JsonLogicDifference<AddOps>
271+
| JsonLogicProduct<AddOps>
272+
| JsonLogicQuotient<AddOps>
273+
| JsonLogicRemainder<AddOps>
274+
// Array Operations - https://jsonlogic.com/operations.html#array-operations
275+
| JsonLogicMap<AddOps>
276+
| JsonLogicFilter<AddOps>
277+
| JsonLogicReduce<AddOps>
278+
| JsonLogicAll<AddOps>
279+
| JsonLogicNone<AddOps>
280+
| JsonLogicSome<AddOps>
281+
| JsonLogicMerge<AddOps>
282+
| JsonLogicInArray<AddOps>
283+
// String Operations - https://jsonlogic.com/operations.html#string-operations
284+
| JsonLogicInString<AddOps>
285+
| JsonLogicCat<AddOps>
286+
| JsonLogicSubstr<AddOps>
287+
// Miscellaneous - https://jsonlogic.com/operations.html#miscellaneous
288+
| JsonLogicLog<AddOps>
289+
// Adding Operations (https://jsonlogic.com/add_operation.html)
290+
| AddOps;
291+
292+
export function add_operation(
293+
name: string,
294+
code: ((...args: any[]) => any) | Record<string, (...args: any[]) => any>
295+
): void;
296+
export function apply(
297+
logic: RulesLogic<AdditionalOperation>,
298+
data?: unknown
299+
): any;
300+
export function rm_operation(name: string): void;
301+
302+
// These functions are undocumented, but are exported by the real package
303+
// so they're typed here for completeness.
304+
export function is_logic(logic: any): logic is RulesLogic;
305+
export function truthy(value: any): boolean;
306+
export function get_operator(logic: Record<string, any>): string;
307+
export function get_values(logic: Record<string, any>): any;
308+
export function uses_data(logic: Record<string, any>): any[];
309+
export function rule_like(rule: any, pattern: any): boolean;

0 commit comments

Comments
 (0)