@ahsankhanamu/json-transformer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/ast.d.ts +259 -0
  2. package/dist/ast.d.ts.map +1 -0
  3. package/dist/ast.js +12 -0
  4. package/dist/ast.js.map +1 -0
  5. package/dist/codegen/base.d.ts +83 -0
  6. package/dist/codegen/base.d.ts.map +1 -0
  7. package/dist/codegen/base.js +494 -0
  8. package/dist/codegen/base.js.map +1 -0
  9. package/dist/codegen/index.d.ts +35 -0
  10. package/dist/codegen/index.d.ts.map +1 -0
  11. package/dist/codegen/index.js +42 -0
  12. package/dist/codegen/index.js.map +1 -0
  13. package/dist/codegen/library.d.ts +42 -0
  14. package/dist/codegen/library.d.ts.map +1 -0
  15. package/dist/codegen/library.js +406 -0
  16. package/dist/codegen/library.js.map +1 -0
  17. package/dist/codegen/native.d.ts +33 -0
  18. package/dist/codegen/native.d.ts.map +1 -0
  19. package/dist/codegen/native.js +452 -0
  20. package/dist/codegen/native.js.map +1 -0
  21. package/dist/codegen.d.ts +13 -0
  22. package/dist/codegen.d.ts.map +1 -0
  23. package/dist/codegen.js +12 -0
  24. package/dist/codegen.js.map +1 -0
  25. package/dist/index.d.ts +257 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +231 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/lexer.d.ts +37 -0
  30. package/dist/lexer.d.ts.map +1 -0
  31. package/dist/lexer.js +499 -0
  32. package/dist/lexer.js.map +1 -0
  33. package/dist/parser.d.ts +63 -0
  34. package/dist/parser.d.ts.map +1 -0
  35. package/dist/parser.js +1045 -0
  36. package/dist/parser.js.map +1 -0
  37. package/dist/playground.d.ts +6 -0
  38. package/dist/playground.d.ts.map +1 -0
  39. package/dist/playground.js +216 -0
  40. package/dist/playground.js.map +1 -0
  41. package/dist/runtime.d.ts +229 -0
  42. package/dist/runtime.d.ts.map +1 -0
  43. package/dist/runtime.js +933 -0
  44. package/dist/runtime.js.map +1 -0
  45. package/dist/tokens.d.ts +80 -0
  46. package/dist/tokens.d.ts.map +1 -0
  47. package/dist/tokens.js +88 -0
  48. package/dist/tokens.js.map +1 -0
  49. package/package.json +30 -0
@@ -0,0 +1,257 @@
1
+ /**
2
+ * JSON Transformer - Expression Language for JSON Data
3
+ *
4
+ * A simple, intuitive expression language for JSON data that compiles to JavaScript.
5
+ * Supports both strict (validation) and forgiving (production) modes.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { compile, evaluate, toJS } from '@anthropic/json-transformer';
10
+ *
11
+ * // Compile an expression
12
+ * const fn = compile('user.name | upper');
13
+ * const result = fn({ user: { name: 'john' } });
14
+ * // result: 'JOHN'
15
+ *
16
+ * // Or evaluate directly (data first)
17
+ * const result = evaluate({ price: 10, quantity: 5 }, 'price * quantity');
18
+ * // result: 50
19
+ *
20
+ * // Generate JavaScript code
21
+ * const code = toJS('user.name | upper');
22
+ * ```
23
+ */
24
+ import { FullCodeGenOptions } from './codegen.js';
25
+ import * as AST from './ast.js';
26
+ export type { Token } from './tokens.js';
27
+ export { TokenType } from './tokens.js';
28
+ export { Lexer, tokenize, LexerError } from './lexer.js';
29
+ export { Parser, parse, ParseError } from './parser.js';
30
+ export { CodeGenerator, generate } from './codegen.js';
31
+ export type { CodeGenOptions, FullCodeGenOptions } from './codegen.js';
32
+ export { helpers, TransformError } from './runtime.js';
33
+ export * as AST from './ast.js';
34
+ export interface CompileOptions extends FullCodeGenOptions {
35
+ /** Cache compiled functions */
36
+ cache?: boolean;
37
+ }
38
+ export interface EvaluateOptions {
39
+ /** Use strict mode */
40
+ strict?: boolean;
41
+ /** External bindings/context */
42
+ bindings?: Record<string, unknown>;
43
+ }
44
+ /** Compiled transform function */
45
+ export type TransformFunction = (input: unknown, bindings?: Record<string, unknown>) => unknown;
46
+ /**
47
+ * Compile an expression to a reusable function
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const transform = compile('orders[*].{ id, total: price * qty }');
52
+ * const result = transform(data);
53
+ * ```
54
+ */
55
+ export declare function compile(expression: string, options?: CompileOptions): TransformFunction;
56
+ /**
57
+ * Evaluate an expression directly (data first)
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const result = evaluate(
62
+ * { firstName: 'John', lastName: 'Doe' },
63
+ * 'firstName & " " & lastName'
64
+ * );
65
+ * // result: 'John Doe'
66
+ * ```
67
+ */
68
+ export declare function evaluate(input: unknown, expression: string, options?: EvaluateOptions): unknown;
69
+ /**
70
+ * Parse an expression and return the AST
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const ast = parseExpression('user.name | upper');
75
+ * console.log(JSON.stringify(ast, null, 2));
76
+ * ```
77
+ */
78
+ export declare function parseExpression(expression: string): AST.Program;
79
+ /**
80
+ * Generate JavaScript code from an expression
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * const code = toJS('price * quantity');
85
+ * console.log(code);
86
+ * // function transform(input, bindings = {}) {
87
+ * // return input?.price * input?.quantity;
88
+ * // }
89
+ *
90
+ * // Native JS (no helpers)
91
+ * const native = toJS('orders | sort(.price)', { native: true });
92
+ * ```
93
+ */
94
+ export declare function toJS(expression: string, options?: FullCodeGenOptions): string;
95
+ /** @deprecated Use toJS instead */
96
+ export declare const toJavaScript: typeof toJS;
97
+ /**
98
+ * Validate an expression without executing it
99
+ *
100
+ * @returns null if valid, or an Error with details
101
+ */
102
+ export declare function validate(expression: string): Error | null;
103
+ /**
104
+ * Clear the expression cache
105
+ */
106
+ export declare function clearCache(): void;
107
+ /**
108
+ * Get cache statistics
109
+ */
110
+ export declare function getCacheStats(): {
111
+ size: number;
112
+ keys: string[];
113
+ };
114
+ /**
115
+ * Template literal tag for expressions
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * const transform = jt`orders[*].{ id, total: price * qty }`;
120
+ * const result = transform(data);
121
+ * ```
122
+ */
123
+ export declare function jt(strings: TemplateStringsArray, ...values: unknown[]): TransformFunction;
124
+ export declare const builder: {
125
+ number: (value: number) => AST.NumberLiteral;
126
+ string: (value: string) => AST.StringLiteral;
127
+ boolean: (value: boolean) => AST.BooleanLiteral;
128
+ null: () => AST.NullLiteral;
129
+ identifier: (name: string) => AST.Identifier;
130
+ member: (object: AST.Expression, property: string, optional?: boolean) => AST.MemberAccess;
131
+ index: (object: AST.Expression, index: AST.Expression, optional?: boolean) => AST.IndexAccess;
132
+ binary: (operator: string, left: AST.Expression, right: AST.Expression) => AST.BinaryExpression;
133
+ call: (callee: AST.Expression, args: AST.Expression[]) => AST.CallExpression;
134
+ object: (properties: AST.ObjectProperty[]) => AST.ObjectLiteral;
135
+ array: (elements: AST.Expression[]) => AST.ArrayLiteral;
136
+ program: (expression: AST.Expression, statements?: AST.LetBinding[]) => AST.Program;
137
+ };
138
+ declare const _default: {
139
+ compile: typeof compile;
140
+ evaluate: typeof evaluate;
141
+ parse: typeof parseExpression;
142
+ toJS: typeof toJS;
143
+ toJavaScript: typeof toJS;
144
+ validate: typeof validate;
145
+ clearCache: typeof clearCache;
146
+ getCacheStats: typeof getCacheStats;
147
+ jt: typeof jt;
148
+ builder: {
149
+ number: (value: number) => AST.NumberLiteral;
150
+ string: (value: string) => AST.StringLiteral;
151
+ boolean: (value: boolean) => AST.BooleanLiteral;
152
+ null: () => AST.NullLiteral;
153
+ identifier: (name: string) => AST.Identifier;
154
+ member: (object: AST.Expression, property: string, optional?: boolean) => AST.MemberAccess;
155
+ index: (object: AST.Expression, index: AST.Expression, optional?: boolean) => AST.IndexAccess;
156
+ binary: (operator: string, left: AST.Expression, right: AST.Expression) => AST.BinaryExpression;
157
+ call: (callee: AST.Expression, args: AST.Expression[]) => AST.CallExpression;
158
+ object: (properties: AST.ObjectProperty[]) => AST.ObjectLiteral;
159
+ array: (elements: AST.Expression[]) => AST.ArrayLiteral;
160
+ program: (expression: AST.Expression, statements?: AST.LetBinding[]) => AST.Program;
161
+ };
162
+ helpers: {
163
+ upper: typeof import("./runtime.js").upper;
164
+ lower: typeof import("./runtime.js").lower;
165
+ trim: typeof import("./runtime.js").trim;
166
+ split: typeof import("./runtime.js").split;
167
+ join: typeof import("./runtime.js").join;
168
+ substring: typeof import("./runtime.js").substring;
169
+ replace: typeof import("./runtime.js").replace;
170
+ replaceAll: typeof import("./runtime.js").replaceAll;
171
+ matches: typeof import("./runtime.js").matches;
172
+ startsWith: typeof import("./runtime.js").startsWith;
173
+ endsWith: typeof import("./runtime.js").endsWith;
174
+ contains: typeof import("./runtime.js").contains;
175
+ padStart: typeof import("./runtime.js").padStart;
176
+ padEnd: typeof import("./runtime.js").padEnd;
177
+ capitalize: typeof import("./runtime.js").capitalize;
178
+ camelCase: typeof import("./runtime.js").camelCase;
179
+ snakeCase: typeof import("./runtime.js").snakeCase;
180
+ kebabCase: typeof import("./runtime.js").kebabCase;
181
+ round: typeof import("./runtime.js").round;
182
+ floor: typeof import("./runtime.js").floor;
183
+ ceil: typeof import("./runtime.js").ceil;
184
+ abs: typeof import("./runtime.js").abs;
185
+ min: typeof import("./runtime.js").min;
186
+ max: typeof import("./runtime.js").max;
187
+ clamp: typeof import("./runtime.js").clamp;
188
+ random: typeof import("./runtime.js").random;
189
+ randomInt: typeof import("./runtime.js").randomInt;
190
+ map: typeof import("./runtime.js").map;
191
+ filter: typeof import("./runtime.js").filter;
192
+ find: typeof import("./runtime.js").find;
193
+ some: typeof import("./runtime.js").some;
194
+ every: typeof import("./runtime.js").every;
195
+ reduce: typeof import("./runtime.js").reduce;
196
+ sum: typeof import("./runtime.js").sum;
197
+ avg: typeof import("./runtime.js").avg;
198
+ count: typeof import("./runtime.js").count;
199
+ first: typeof import("./runtime.js").first;
200
+ last: typeof import("./runtime.js").last;
201
+ unique: typeof import("./runtime.js").unique;
202
+ flatten: typeof import("./runtime.js").flatten;
203
+ reverse: typeof import("./runtime.js").reverse;
204
+ sort: typeof import("./runtime.js").sort;
205
+ sortDesc: typeof import("./runtime.js").sortDesc;
206
+ groupBy: typeof import("./runtime.js").groupBy;
207
+ keyBy: typeof import("./runtime.js").keyBy;
208
+ zip: typeof import("./runtime.js").zip;
209
+ compact: typeof import("./runtime.js").compact;
210
+ take: typeof import("./runtime.js").take;
211
+ drop: typeof import("./runtime.js").drop;
212
+ range: typeof import("./runtime.js").range;
213
+ keys: typeof import("./runtime.js").keys;
214
+ values: typeof import("./runtime.js").values;
215
+ entries: typeof import("./runtime.js").entries;
216
+ merge: typeof import("./runtime.js").merge;
217
+ pick: typeof import("./runtime.js").pick;
218
+ omit: typeof import("./runtime.js").omit;
219
+ get: typeof import("./runtime.js").get;
220
+ set: typeof import("./runtime.js").set;
221
+ type: typeof import("./runtime.js").type;
222
+ isString: typeof import("./runtime.js").isString;
223
+ isNumber: typeof import("./runtime.js").isNumber;
224
+ isBoolean: typeof import("./runtime.js").isBoolean;
225
+ isArray: typeof import("./runtime.js").isArray;
226
+ isObject: typeof import("./runtime.js").isObject;
227
+ isNull: typeof import("./runtime.js").isNull;
228
+ isUndefined: typeof import("./runtime.js").isUndefined;
229
+ isEmpty: typeof import("./runtime.js").isEmpty;
230
+ toString: typeof import("./runtime.js").toString;
231
+ toNumber: typeof import("./runtime.js").toNumber;
232
+ toBoolean: typeof import("./runtime.js").toBoolean;
233
+ toArray: typeof import("./runtime.js").toArray;
234
+ toJSON: typeof import("./runtime.js").toJSON;
235
+ fromJSON: typeof import("./runtime.js").fromJSON;
236
+ now: typeof import("./runtime.js").now;
237
+ today: typeof import("./runtime.js").today;
238
+ formatDate: typeof import("./runtime.js").formatDate;
239
+ parseDate: typeof import("./runtime.js").parseDate;
240
+ coalesce: typeof import("./runtime.js").coalesce;
241
+ default: typeof import("./runtime.js").defaultValue;
242
+ if: typeof import("./runtime.js").ifThen;
243
+ uuid: typeof import("./runtime.js").uuid;
244
+ assertNonNull: typeof import("./runtime.js").assertNonNull;
245
+ assertType: typeof import("./runtime.js").assertType;
246
+ assertArray: typeof import("./runtime.js").assertArray;
247
+ strictGet: typeof import("./runtime.js").strictGet;
248
+ strictIndex: typeof import("./runtime.js").strictIndex;
249
+ strictArray: typeof import("./runtime.js").strictArray;
250
+ strictNonNull: typeof import("./runtime.js").strictNonNull;
251
+ strictType: typeof import("./runtime.js").strictType;
252
+ strictFilter: typeof import("./runtime.js").strictFilter;
253
+ strictMap: typeof import("./runtime.js").strictMap;
254
+ };
255
+ };
256
+ export default _default;
257
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAAY,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAE5D,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAGhC,YAAY,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACvD,YAAY,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAMhC,MAAM,WAAW,cAAe,SAAQ,kBAAkB;IACxD,+BAA+B;IAC/B,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B,sBAAsB;IACtB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,gCAAgC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,kCAAkC;AAClC,MAAM,MAAM,iBAAiB,GAAG,CAAC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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWvF,wBAYE"}
package/dist/index.js ADDED
@@ -0,0 +1,231 @@
1
+ /**
2
+ * JSON Transformer - Expression Language for JSON Data
3
+ *
4
+ * A simple, intuitive expression language for JSON data that compiles to JavaScript.
5
+ * Supports both strict (validation) and forgiving (production) modes.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { compile, evaluate, toJS } from '@anthropic/json-transformer';
10
+ *
11
+ * // Compile an expression
12
+ * const fn = compile('user.name | upper');
13
+ * const result = fn({ user: { name: 'john' } });
14
+ * // result: 'JOHN'
15
+ *
16
+ * // Or evaluate directly (data first)
17
+ * const result = evaluate({ price: 10, quantity: 5 }, 'price * quantity');
18
+ * // result: 50
19
+ *
20
+ * // Generate JavaScript code
21
+ * const code = toJS('user.name | upper');
22
+ * ```
23
+ */
24
+ import { parse } from './parser.js';
25
+ import { generate } from './codegen.js';
26
+ import { helpers } from './runtime.js';
27
+ export { TokenType } from './tokens.js';
28
+ export { Lexer, tokenize, LexerError } from './lexer.js';
29
+ export { Parser, parse, ParseError } from './parser.js';
30
+ export { CodeGenerator, generate } from './codegen.js';
31
+ export { helpers, TransformError } from './runtime.js';
32
+ export * as AST from './ast.js';
33
+ // Simple expression cache
34
+ const expressionCache = new Map();
35
+ /**
36
+ * Compile an expression to a reusable function
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const transform = compile('orders[*].{ id, total: price * qty }');
41
+ * const result = transform(data);
42
+ * ```
43
+ */
44
+ export function compile(expression, options = {}) {
45
+ const cacheKey = options.strict ? `strict:${expression}` : expression;
46
+ // Check cache
47
+ if (options.cache !== false && expressionCache.has(cacheKey)) {
48
+ return expressionCache.get(cacheKey);
49
+ }
50
+ // Parse the expression
51
+ const ast = parse(expression);
52
+ // Generate JavaScript code
53
+ const code = generate(ast, {
54
+ strict: options.strict ?? false,
55
+ wrapInFunction: false,
56
+ inputVar: 'input',
57
+ bindingsVar: 'bindings',
58
+ });
59
+ // Create the function
60
+ const fn = new Function('input', 'bindings', '__helpers', `"use strict";\n${code}`);
61
+ // Wrap with helpers
62
+ const transform = (input, bindings = {}) => {
63
+ return fn(input, bindings, helpers);
64
+ };
65
+ // Cache it
66
+ if (options.cache !== false) {
67
+ expressionCache.set(cacheKey, transform);
68
+ }
69
+ return transform;
70
+ }
71
+ /**
72
+ * Evaluate an expression directly (data first)
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const result = evaluate(
77
+ * { firstName: 'John', lastName: 'Doe' },
78
+ * 'firstName & " " & lastName'
79
+ * );
80
+ * // result: 'John Doe'
81
+ * ```
82
+ */
83
+ export function evaluate(input, expression, options = {}) {
84
+ const fn = compile(expression, { strict: options.strict, cache: true });
85
+ return fn(input, options.bindings);
86
+ }
87
+ /**
88
+ * Parse an expression and return the AST
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const ast = parseExpression('user.name | upper');
93
+ * console.log(JSON.stringify(ast, null, 2));
94
+ * ```
95
+ */
96
+ export function parseExpression(expression) {
97
+ return parse(expression);
98
+ }
99
+ /**
100
+ * Generate JavaScript code from an expression
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const code = toJS('price * quantity');
105
+ * console.log(code);
106
+ * // function transform(input, bindings = {}) {
107
+ * // return input?.price * input?.quantity;
108
+ * // }
109
+ *
110
+ * // Native JS (no helpers)
111
+ * const native = toJS('orders | sort(.price)', { native: true });
112
+ * ```
113
+ */
114
+ export function toJS(expression, options = {}) {
115
+ const ast = parse(expression);
116
+ return generate(ast, options);
117
+ }
118
+ /** @deprecated Use toJS instead */
119
+ export const toJavaScript = toJS;
120
+ /**
121
+ * Validate an expression without executing it
122
+ *
123
+ * @returns null if valid, or an Error with details
124
+ */
125
+ export function validate(expression) {
126
+ try {
127
+ parse(expression);
128
+ return null;
129
+ }
130
+ catch (error) {
131
+ return error;
132
+ }
133
+ }
134
+ /**
135
+ * Clear the expression cache
136
+ */
137
+ export function clearCache() {
138
+ expressionCache.clear();
139
+ }
140
+ /**
141
+ * Get cache statistics
142
+ */
143
+ export function getCacheStats() {
144
+ return {
145
+ size: expressionCache.size,
146
+ keys: Array.from(expressionCache.keys()),
147
+ };
148
+ }
149
+ // =============================================================================
150
+ // TEMPLATE LITERAL TAG
151
+ // =============================================================================
152
+ /**
153
+ * Template literal tag for expressions
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * const transform = jt`orders[*].{ id, total: price * qty }`;
158
+ * const result = transform(data);
159
+ * ```
160
+ */
161
+ export function jt(strings, ...values) {
162
+ // Combine template literal parts
163
+ let expression = strings[0];
164
+ for (let i = 0; i < values.length; i++) {
165
+ expression += String(values[i]) + strings[i + 1];
166
+ }
167
+ return compile(expression);
168
+ }
169
+ // =============================================================================
170
+ // BUILDER API (for programmatic AST construction)
171
+ // =============================================================================
172
+ export const builder = {
173
+ number: (value) => ({ type: 'NumberLiteral', value }),
174
+ string: (value) => ({ type: 'StringLiteral', value }),
175
+ boolean: (value) => ({ type: 'BooleanLiteral', value }),
176
+ null: () => ({ type: 'NullLiteral' }),
177
+ identifier: (name) => ({ type: 'Identifier', name }),
178
+ member: (object, property, optional = false) => ({
179
+ type: 'MemberAccess',
180
+ object,
181
+ property,
182
+ optional,
183
+ }),
184
+ index: (object, index, optional = false) => ({
185
+ type: 'IndexAccess',
186
+ object,
187
+ index,
188
+ optional,
189
+ }),
190
+ binary: (operator, left, right) => ({
191
+ type: 'BinaryExpression',
192
+ operator,
193
+ left,
194
+ right,
195
+ }),
196
+ call: (callee, args) => ({
197
+ type: 'CallExpression',
198
+ callee,
199
+ arguments: args,
200
+ }),
201
+ object: (properties) => ({
202
+ type: 'ObjectLiteral',
203
+ properties,
204
+ }),
205
+ array: (elements) => ({
206
+ type: 'ArrayLiteral',
207
+ elements,
208
+ }),
209
+ program: (expression, statements = []) => ({
210
+ type: 'Program',
211
+ statements,
212
+ expression,
213
+ }),
214
+ };
215
+ // =============================================================================
216
+ // DEFAULT EXPORT
217
+ // =============================================================================
218
+ export default {
219
+ compile,
220
+ evaluate,
221
+ parse: parseExpression,
222
+ toJS,
223
+ toJavaScript, // deprecated alias
224
+ validate,
225
+ clearCache,
226
+ getCacheStats,
227
+ jt,
228
+ builder,
229
+ helpers,
230
+ };
231
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAsB,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAKvC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAEvD,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAqBhC,0BAA0B;AAC1B,MAAM,eAAe,GAAG,IAAI,GAAG,EAA6B,CAAC;AAE7D;;;;;;;;GAQG;AACH,MAAM,UAAU,OAAO,CAAC,UAAkB,EAAE,UAA0B,EAAE;IACtE,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,UAAU,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;IAEtE,cAAc;IACd,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK,IAAI,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC7D,OAAO,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAC;IACxC,CAAC;IAED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E,CAAC,QAA0B,EAAoB,EAAE,CAAC,CAAC;QACxD,IAAI,EAAE,cAAc;QACpB,QAAQ;KACT,CAAC;IAEF,OAAO,EAAE,CAAC,UAA0B,EAAE,aAA+B,EAAE,EAAe,EAAE,CAAC,CAAC;QACxF,IAAI,EAAE,SAAS;QACf,UAAU;QACV,UAAU;KACX,CAAC;CACH,CAAC;AAEF,gFAAgF;AAChF,iBAAiB;AACjB,gFAAgF;AAEhF,eAAe;IACb,OAAO;IACP,QAAQ;IACR,KAAK,EAAE,eAAe;IACtB,IAAI;IACJ,YAAY,EAAE,mBAAmB;IACjC,QAAQ;IACR,UAAU;IACV,aAAa;IACb,EAAE;IACF,OAAO;IACP,OAAO;CACR,CAAC"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Lexer - Tokenizes input string into tokens
3
+ */
4
+ import { Token } from './tokens.js';
5
+ export declare class LexerError extends Error {
6
+ line: number;
7
+ column: number;
8
+ offset: number;
9
+ constructor(message: string, line: number, column: number, offset: number);
10
+ }
11
+ export declare class Lexer {
12
+ private input;
13
+ private pos;
14
+ private line;
15
+ private column;
16
+ private tokens;
17
+ constructor(input: string);
18
+ tokenize(): Token[];
19
+ private scanToken;
20
+ private scanString;
21
+ private scanTemplateLiteral;
22
+ private scanNumber;
23
+ private scanIdentifier;
24
+ private skipWhitespaceAndComments;
25
+ private isAtEnd;
26
+ private peek;
27
+ private peekNext;
28
+ private advance;
29
+ private match;
30
+ private isDigit;
31
+ private isIdentifierStart;
32
+ private isIdentifierPart;
33
+ private makeToken;
34
+ private addToken;
35
+ }
36
+ export declare function tokenize(input: string): Token[];
37
+ //# sourceMappingURL=lexer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lexer.d.ts","sourceRoot":"","sources":["../src/lexer.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,KAAK,EAAuB,MAAM,aAAa,CAAC;AAEzD,qBAAa,UAAW,SAAQ,KAAK;IAG1B,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,MAAM;IACd,MAAM,EAAE,MAAM;gBAHrB,OAAO,EAAE,MAAM,EACR,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM;CAKxB;AAED,qBAAa,KAAK;IAChB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,GAAG,CAAa;IACxB,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,MAAM,CAAe;gBAEjB,KAAK,EAAE,MAAM;IAIzB,QAAQ,IAAI,KAAK,EAAE;IAWnB,OAAO,CAAC,SAAS;IAsMjB,OAAO,CAAC,UAAU;IAqElB,OAAO,CAAC,mBAAmB;IAmE3B,OAAO,CAAC,UAAU;IAmClB,OAAO,CAAC,cAAc;IActB,OAAO,CAAC,yBAAyB;IAqCjC,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,IAAI;IAKZ,OAAO,CAAC,QAAQ;IAKhB,OAAO,CAAC,OAAO;IAOf,OAAO,CAAC,KAAK;IASb,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,iBAAiB;IAMzB,OAAO,CAAC,gBAAgB;IAIxB,OAAO,CAAC,SAAS;IAWjB,OAAO,CAAC,QAAQ;CAgBjB;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,EAAE,CAE/C"}