pacc 9.1.0 → 9.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "9.1.0",
3
+ "version": "9.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ast.mjs CHANGED
@@ -12,10 +12,7 @@ export function pathEval(node, current, context) {
12
12
  }
13
13
 
14
14
  export function functionEval(node, current, context) {
15
- const args = node.args.map(a =>
16
- typeof a === "object" ? a.eval(a, current, context) : a
17
- );
18
- return context.valueFor(node.name)(...args);
15
+ return context.valueFor(node.name)(node.args, current, context);
19
16
  }
20
17
 
21
18
  export function keyedAccessOrGlobalEval(node, current, context) {
package/src/tokens.mjs CHANGED
@@ -308,8 +308,23 @@ export const keywords = {
308
308
  false: [BOOLEAN, false]
309
309
  };
310
310
 
311
+ function evalAll(args, current, context) {
312
+ return args.map(a =>
313
+ typeof a?.eval === "function" ? a.eval(a, current, context) : a
314
+ );
315
+ }
316
+
317
+ function evalOne(arg, current, context) {
318
+ return typeof arg?.eval === "function"
319
+ ? arg.eval(arg, current, context)
320
+ : arg;
321
+ }
322
+
311
323
  export const globals = {
312
- in: (a, b) => {
324
+ in: (args,current,context) => {
325
+ const a = evalOne(args[0], current, context);
326
+ const b = evalOne(args[1], current, context);
327
+
313
328
  if (b?.[Symbol.iterator]) {
314
329
  for (const x of b) {
315
330
  if (x === a) {
@@ -319,29 +334,65 @@ export const globals = {
319
334
  }
320
335
  return false;
321
336
  },
322
- ceil: Math.ceil,
323
- floor: Math.floor,
324
- abs: Math.abs,
325
- min: Math.min,
326
- max: Math.max,
327
- encodeURI: encodeURI,
328
- decodeURI: decodeURI,
329
- encodeURIComponent: encodeURIComponent,
330
- decodeURIComponent: decodeURIComponent,
331
- trim: a => a.trim(),
332
- uppercase: a => a.toUpperCase(),
333
- lowercase: a => a.toLowerCase(),
334
- substring: (s, a, b) => s.substring(a, b),
335
- length: s => s.length,
336
- join: (separator, ...args) =>
337
- args
337
+ ceil: (args, current, context) =>
338
+ Math.ceil(evalOne(args[0], current, context)),
339
+ floor: (args, current, context) =>
340
+ Math.floor(evalOne(args[0], current, context)),
341
+ abs: (args, current, context) => Math.abs(evalOne(args[0], current, context)),
342
+ min: (args, current, context) => Math.min(...evalAll(args, current, context)),
343
+ max: (args, current, context) => Math.max(...evalAll(args, current, context)),
344
+ encodeURI: (args, current, context) =>
345
+ encodeURI(...evalAll(args, current, context)),
346
+ decodeURI: (args, current, context) =>
347
+ decodeURI(...evalAll(args, current, context)),
348
+ encodeURIComponent: (args, current, context) =>
349
+ encodeURIComponent(...evalAll(args, current, context)),
350
+ decodeURIComponent: (args, current, context) =>
351
+ decodeURIComponent(...evalAll(args, current, context)),
352
+ trim: (args, current, context) => evalOne(args[0], current, context).trim(),
353
+ length: (args, current, context) => evalOne(args[0], current, context).length,
354
+ uppercase: (args, current, context) =>
355
+ evalOne(args[0], current, context).toUpperCase(),
356
+ lowercase: (args, current, context) =>
357
+ evalOne(args[0], current, context).toLowerCase(),
358
+ substring: (args, current, context) => {
359
+ const str = evalOne(args[0], current, context);
360
+ const n1 = evalOne(args[1], current, context);
361
+ return args.length > 2
362
+ ? str.substring(n1, evalOne(args[2], current, context))
363
+ : str.substring(n1);
364
+ },
365
+ join: (args, current, context) => {
366
+ const separator = evalOne(args.shift(), current, context);
367
+ return evalAll(args, current, context)
338
368
  .map(item => (item instanceof Iterator ? Array.from(item) : item))
339
369
  .flat()
340
- .join(separator),
341
- sort: data => {
370
+ .join(separator);
371
+ },
372
+ sort: (args, current, context) => {
373
+ const data = evalOne(args[0], current, context);
374
+ if (args.length >= 2) {
375
+ let order = 1;
376
+ if(args.length > 2) {
377
+ const str = evalOne(args[2], current, context);
378
+ if(str === 'descending') {
379
+ order = -1;
380
+ }
381
+ }
382
+ const selector = args[1];
383
+ return data.sort(
384
+ (a, b) =>
385
+ (selector.eval(selector, a, context) -
386
+ selector.eval(selector, b, context)) * order
387
+ );
388
+ }
389
+
342
390
  return data.sort();
343
391
  },
344
- truncate: (data, length) => {
392
+ truncate: (args, current, context) => {
393
+ const data = evalOne(args[0], current, context);
394
+ const length = evalOne(args[1], current, context);
395
+
345
396
  data.length = length;
346
397
  return data;
347
398
  }
@@ -47,25 +47,25 @@ export namespace keywords {
47
47
  export { _false as false };
48
48
  }
49
49
  export namespace globals {
50
- export function _in(a: any, b: any): boolean;
50
+ export function _in(args: any, current: any, context: any): boolean;
51
51
  export { _in as in };
52
- export let ceil: (x: number) => number;
53
- export let floor: (x: number) => number;
54
- export let abs: (x: number) => number;
55
- export let min: (...values: number[]) => number;
56
- export let max: (...values: number[]) => number;
57
- export let encodeURI: typeof globalThis.encodeURI;
58
- export let decodeURI: typeof globalThis.decodeURI;
59
- export let encodeURIComponent: typeof globalThis.encodeURIComponent;
60
- export let decodeURIComponent: typeof globalThis.decodeURIComponent;
61
- export function trim(a: any): any;
62
- export function uppercase(a: any): any;
63
- export function lowercase(a: any): any;
64
- export function substring(s: any, a: any, b: any): any;
65
- export function length(s: any): any;
66
- export function join(separator: any, ...args: any[]): string;
67
- export function sort(data: any): any;
68
- export function truncate(data: any, length: any): any;
52
+ export function ceil(args: any, current: any, context: any): number;
53
+ export function floor(args: any, current: any, context: any): number;
54
+ export function abs(args: any, current: any, context: any): number;
55
+ export function min(args: any, current: any, context: any): number;
56
+ export function max(args: any, current: any, context: any): number;
57
+ export function encodeURI(args: any, current: any, context: any): string;
58
+ export function decodeURI(args: any, current: any, context: any): string;
59
+ export function encodeURIComponent(args: any, current: any, context: any): string;
60
+ export function decodeURIComponent(args: any, current: any, context: any): string;
61
+ export function trim(args: any, current: any, context: any): any;
62
+ export function length(args: any, current: any, context: any): any;
63
+ export function uppercase(args: any, current: any, context: any): any;
64
+ export function lowercase(args: any, current: any, context: any): any;
65
+ export function substring(args: any, current: any, context: any): any;
66
+ export function join(args: any, current: any, context: any): any;
67
+ export function sort(args: any, current: any, context: any): any;
68
+ export function truncate(args: any, current: any, context: any): any;
69
69
  }
70
70
  export type Token = {
71
71
  str: string;