pacc 9.0.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/README.md CHANGED
@@ -546,6 +546,7 @@ Retrive attribute values from an object.
546
546
 
547
547
  * `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** attribute value source
548
548
  * `definitions` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
549
+ * `filter` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** 
549
550
 
550
551
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** values
551
552
 
@@ -565,12 +566,12 @@ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
565
566
 
566
567
  ## tokens
567
568
 
568
- Split property path into tokens
569
+ Split expression path into tokens.
569
570
 
570
571
  ### Parameters
571
572
 
572
573
  * `string` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
573
- * `options` (optional, default `{}`)
574
+ * `context` (optional, default `{}`)
574
575
 
575
576
  ## setAttribute
576
577
 
@@ -582,7 +583,7 @@ The name may be a property path like 'a.b.c'.
582
583
  * `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
583
584
  * `expression` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
584
585
  * `value` **any** 
585
- * `definition` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** type def
586
+ * `definition` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** type def
586
587
 
587
588
  ## getAttribute
588
589
 
@@ -593,7 +594,7 @@ The name may be a property path like 'a.b.c' or a\[2]
593
594
 
594
595
  * `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
595
596
  * `expression` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
596
- * `definition`  
597
+ * `definition` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** 
597
598
 
598
599
  Returns **any** value associated with the given property name
599
600
 
@@ -654,8 +655,8 @@ Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa
654
655
  * `str` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
655
656
  * `precedence` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** (optional, default `0`)
656
657
  * `type` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** 
657
- * `led` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** (optional, default `()=>{}`)
658
- * `nud` (optional, default `()=>{}`)
658
+ * `led` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** (optional, default `(parser,node)=>node`)
659
+ * `nud` (optional, default `parser=>this`)
659
660
 
660
661
  Returns **[Token](#token)** 
661
662
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "9.0.0",
3
+ "version": "9.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -17,6 +17,7 @@
17
17
  "keywords": [
18
18
  "attributes",
19
19
  "filtering",
20
+ "pratt",
20
21
  "property path",
21
22
  "xpath"
22
23
  ],
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,25 +334,68 @@ 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)
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
+
390
+ return data.sort();
391
+ },
392
+ truncate: (args, current, context) => {
393
+ const data = evalOne(args[0], current, context);
394
+ const length = evalOne(args[1], current, context);
395
+
396
+ data.length = length;
397
+ return data;
398
+ }
341
399
  };
342
400
 
343
401
  /**
@@ -349,7 +407,7 @@ export const globals = {
349
407
  export function* tokens(string, context = {}) {
350
408
  context.keywords ||= keywords;
351
409
  context.parseFloat ||= parseFloat;
352
- context.valueFor ||= (name,at) => globals[name];
410
+ context.valueFor ||= (name, at) => globals[name];
353
411
 
354
412
  let state, value, hex, quote;
355
413
 
@@ -47,23 +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;
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;
67
69
  }
68
70
  export type Token = {
69
71
  str: string;