pacc 9.2.3 → 9.2.5

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.
@@ -0,0 +1,48 @@
1
+ import { run, bench, boxplot, summary } from "mitata";
2
+ import { tokens } from "../src/tokens.mjs";
3
+ import { parse, parseOnly } from "../src/parser.mjs";
4
+
5
+ const root = new Map([
6
+ ["a", { n: 1, l: [1, 2] }],
7
+ ["b", { n: 3, x: 7, l: [3, 4] }],
8
+ [
9
+ "c",
10
+ {
11
+ d: {
12
+ e: { f: { g: { h: { i: { j: { k: { l: { m: { n: "xyz" } } } } } } } } }
13
+ }
14
+ }
15
+ ]
16
+ ]);
17
+
18
+ const root2 = {
19
+ a: {
20
+ b: {
21
+ c: {
22
+ d: {
23
+ e: {
24
+ f: { g: { h: { i: { j: { k: { l: { m: { n: "xyz" } } } } } } } }
25
+ }
26
+ }
27
+ }
28
+ }
29
+ }
30
+ };
31
+
32
+ bench('"[n<5].l"', () => parse("[n<5].l", { root }));
33
+
34
+
35
+ const expression = "a.b.c.d.e.f.g.h.i.j.k.l.m.n";
36
+
37
+ bench('"a.b.c.d.e.f.g.h.i.j.k.l.m.n"', () =>
38
+ parse(expression, { root: root2 })
39
+ );
40
+
41
+ bench('parseOnly "a.b.c.d.e.f.g.h.i.j.k.l.m.n"', () =>
42
+ parseOnly(expression, { })
43
+ );
44
+ bench('tokens "a.b.c.d.e.f.g.h.i.j.k.l.m.n"', () =>
45
+ Array.from(tokens(expression, { }))
46
+ );
47
+
48
+ await run();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "9.2.3",
3
+ "version": "9.2.5",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -29,6 +29,7 @@
29
29
  ],
30
30
  "license": "0BSD",
31
31
  "scripts": {
32
+ "benchmark": "node benchmark/*.mjs",
32
33
  "prepare": "node --run prepare:typescript",
33
34
  "prepare:typescript": "tsc --allowJs --declaration --emitDeclarationOnly --declarationDir types --resolveJsonModule --target esnext -m esnext --module nodenext --moduleResolution nodenext --rootDir src ./src**/*.mjs",
34
35
  "test": "node --run test:browser-ava && node --run test:ava",
@@ -41,10 +42,12 @@
41
42
  "lint:typescript": "tsc --allowJs --checkJs --noEmit --resolveJsonModule --target esnext -m esnext --module nodenext --moduleResolution nodenext ./src**/*.mjs"
42
43
  },
43
44
  "devDependencies": {
45
+ "@mitata/counters": "^0.0.8",
44
46
  "ava": "^6.4.1",
45
47
  "browser-ava": "^2.3.53",
46
48
  "c8": "^10.1.3",
47
49
  "documentation": "^14.0.3",
50
+ "mitata": "^1.0.34",
48
51
  "semantic-release": "^25.0.3",
49
52
  "typescript": "^5.9.3"
50
53
  },
package/src/ast.mjs CHANGED
@@ -19,26 +19,7 @@ export function keyedAccessOrGlobalEval(node, current, context) {
19
19
  return keyedAccessEval(node, current, context) ?? context.valueFor(node.key);
20
20
  }
21
21
 
22
- export function keyedAccessEval(node, current, context) {
23
- if (current === undefined) {
24
- return undefined;
25
- }
26
- if (current instanceof Map) {
27
- return current.get(node.key);
28
- }
29
- if (current instanceof Set) {
30
- return current.has(node.key) ? node.key : undefined;
31
- }
32
- if (current instanceof Iterator) {
33
- if(typeof node.key === 'number') {
34
- for(const item of current.drop(node.key)) {
35
- return item;
36
- }
37
- }
38
-
39
- return current.map(item => item[node.key]);
40
- }
41
-
22
+ function scalarAccessEval(node, current, context) {
42
23
  switch (typeof current[node.key]) {
43
24
  case "function": {
44
25
  const value = current[node.key]();
@@ -56,6 +37,37 @@ export function keyedAccessEval(node, current, context) {
56
37
  return current[node.key];
57
38
  }
58
39
 
40
+ function plain(value) {
41
+ if (typeof value === "function") {
42
+ return value();
43
+ }
44
+
45
+ return value;
46
+ }
47
+
48
+ export function keyedAccessEval(node, current, context) {
49
+ if (current === undefined) {
50
+ return undefined;
51
+ }
52
+ if (current instanceof Map) {
53
+ return plain(current.get(node.key));
54
+ }
55
+ if (current instanceof Set) {
56
+ return current.has(node.key) ? node.key : undefined;
57
+ }
58
+ if (current instanceof Iterator) {
59
+ if (typeof node.key === "number") {
60
+ for (const item of current.drop(node.key)) {
61
+ return plain(item);
62
+ }
63
+ }
64
+
65
+ return current.map(item => scalarAccessEval(node, item, context));
66
+ }
67
+
68
+ return scalarAccessEval(node, current, context);
69
+ }
70
+
59
71
  export function filterEval(node, current, context) {
60
72
  if (typeof current.values === "function") {
61
73
  current = current.values();
package/src/parser.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { tokens, EOF } from "./tokens.mjs";
2
2
 
3
- export function parseOnly(input, context = {}) {
3
+ export function parseOnly(input, context) {
4
4
  input = tokens(input, context);
5
5
 
6
6
  let token, value;
@@ -54,7 +54,7 @@ export function parseOnly(input, context = {}) {
54
54
  return parser.expression(0);
55
55
  }
56
56
 
57
- export function parse(input, context) {
57
+ export function parse(input, context={}) {
58
58
  const result = parseOnly(input, context);
59
59
  return result.eval ? result.eval(result, context.root, context) : result;
60
60
  }
package/src/tokens.mjs CHANGED
@@ -187,6 +187,13 @@ function createFilter(parser) {
187
187
  case "number":
188
188
  return { eval: keyedAccessEval, key: filter };
189
189
  default:
190
+ if (
191
+ filter.eval === keyedAccessEval ||
192
+ filter.eval === keyedAccessOrGlobalEval
193
+ ) {
194
+ return filter;
195
+ }
196
+
190
197
  return { eval: filterEval, filter };
191
198
  }
192
199
  }
@@ -321,7 +328,7 @@ function evalOne(arg, current, context) {
321
328
  }
322
329
 
323
330
  export const globals = {
324
- in: (args,current,context) => {
331
+ in: (args, current, context) => {
325
332
  const a = evalOne(args[0], current, context);
326
333
  const b = evalOne(args[1], current, context);
327
334
 
@@ -373,9 +380,9 @@ export const globals = {
373
380
  const data = evalOne(args[0], current, context);
374
381
  if (args.length >= 2) {
375
382
  let order = 1;
376
- if(args.length > 2) {
383
+ if (args.length > 2) {
377
384
  const str = evalOne(args[2], current, context);
378
- if(str === 'descending') {
385
+ if (str === "descending") {
379
386
  order = -1;
380
387
  }
381
388
  }
@@ -383,7 +390,8 @@ export const globals = {
383
390
  return data.sort(
384
391
  (a, b) =>
385
392
  (selector.eval(selector, a, context) -
386
- selector.eval(selector, b, context)) * order
393
+ selector.eval(selector, b, context)) *
394
+ order
387
395
  );
388
396
  }
389
397
 
@@ -393,7 +401,7 @@ export const globals = {
393
401
  const data = evalOne(args[0], current, context);
394
402
  const length = evalOne(args[1], current, context);
395
403
 
396
- if(data instanceof Iterator) {
404
+ if (data instanceof Iterator) {
397
405
  return data.take(length);
398
406
  }
399
407
 
@@ -1,2 +1,2 @@
1
- export function parseOnly(input: any, context?: {}): any;
2
- export function parse(input: any, context: any): any;
1
+ export function parseOnly(input: any, context: any): any;
2
+ export function parse(input: any, context?: {}): any;