pacc 4.20.0 → 4.21.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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/expand.mjs +130 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.20.0",
3
+ "version": "4.21.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/expand.mjs CHANGED
@@ -2,32 +2,145 @@ import { parse } from "./expression.mjs";
2
2
  import { tokens } from "./tokens.mjs";
3
3
 
4
4
  export function expand(object, context) {
5
- switch (typeof object) {
6
- case "string":
7
- return object.replaceAll(/\$\{([^\}]*)\}/g, (match, m1) => {
8
- context.tokens = tokens(m1);
9
- return parse(context) ?? "${" + m1 + "}";
5
+ const promises = [];
6
+ const maxNestingLevel = 5;
7
+
8
+ function _expand(object, path) {
9
+ if (path.length >= maxNestingLevel) {
10
+ throw new Error(
11
+ `Max nesting level ${maxNestingLevel} reached: ${object}`
12
+ );
13
+ }
14
+
15
+ if (typeof object === "string" || object instanceof String) {
16
+ let wholeValue;
17
+
18
+ const localPromises = [];
19
+ const v = object.replace(/\$\{([^\}]*)\}/g, (match, expression, offset, string) => {
20
+ context.tokens = tokens(expression);
21
+ let value = parse(context);
22
+
23
+ if (typeof value === "string" || value instanceof String) {
24
+ value = _expand(value, path);
25
+ } else if (value === undefined) {
26
+ value = "${" + expression + "}";
27
+ }
28
+ if (string.length === expression.length + 3) {
29
+ wholeValue = value;
30
+ return "";
31
+ }
32
+
33
+ if (value instanceof Promise) {
34
+ localPromises.push(value);
35
+ return "${" + (localPromises.length - 1) + "}";
36
+ }
37
+ return value;
10
38
  });
11
39
 
12
- case "object":
13
- if (object instanceof Map) {
14
- return new Map(
15
- [...object].map(([k, v]) => [expand(k, context), expand(v, context)])
40
+ if (wholeValue !== undefined) {
41
+ return wholeValue;
42
+ }
43
+
44
+ if (localPromises.length !== 0) {
45
+ return Promise.all(localPromises).then(all =>
46
+ v.replace(/\$\{(\d+)\}/g, (match, key) => all[parseInt(key, 10)])
16
47
  );
17
48
  }
18
49
 
19
- if (object instanceof Set) {
20
- return new Set([...object].map(e => expand(e, context)));
50
+ return v;
51
+ }
52
+
53
+ switch (typeof object) {
54
+ case "undefined":
55
+ case "boolean":
56
+ case "number":
57
+ case "bigint":
58
+ case "function":
59
+ return object;
60
+ }
61
+
62
+ if (object === null || object instanceof Number || object instanceof Date) {
63
+ // TODO: find a better way to identify special cases
64
+ return object;
65
+ }
66
+
67
+ if (object instanceof Map) {
68
+ const r = new Map();
69
+ for (const [key, value] of object.entries()) {
70
+ const path2 = [
71
+ ...path,
72
+ {
73
+ key,
74
+ value
75
+ }
76
+ ];
77
+
78
+ r.set(_expand(key, path2), _expand(value, path2));
79
+ }
80
+
81
+ return r;
82
+ }
83
+
84
+ if (object instanceof Set) {
85
+ const r = new Set();
86
+ for (const value of object.values()) {
87
+ r.add(_expand(value, [...path, { value }]));
88
+ }
89
+
90
+ return r;
91
+ }
92
+
93
+ if (Array.isArray(object)) {
94
+ const array = new Array(object.length);
95
+
96
+ for (let index = 0; index < object.length; index++) {
97
+ const o = object[index];
98
+
99
+ const r = _expand(o, [
100
+ ...path,
101
+ {
102
+ key: index,
103
+ value: o
104
+ }
105
+ ]);
106
+ if (r instanceof Promise) {
107
+ promises.push(r);
108
+ r.then(f => (array[index] = f));
109
+ }
110
+ array[index] = r;
21
111
  }
22
112
 
23
- if (Array.isArray(object)) {
24
- return object.map(e => expand(e, context));
113
+ return array;
114
+ }
115
+
116
+ let newObject = {};
117
+
118
+ for (let [key, value] of Object.entries(object)) {
119
+ const newKey = _expand(key, path);
120
+ if (typeof newKey === "string" || newKey instanceof String) {
121
+ value = _expand(value, [
122
+ ...path,
123
+ {
124
+ key,
125
+ value
126
+ }
127
+ ]);
128
+ if (value instanceof Promise) {
129
+ promises.push(value);
130
+ value.then(v => (newObject[newKey] = v));
131
+ }
132
+ newObject[newKey] = value;
133
+ } else {
134
+ newObject = newKey;
25
135
  }
136
+ }
26
137
 
27
- return Object.fromEntries(
28
- Object.entries(object).map(([k, v]) => [k, expand(v, context)])
29
- );
138
+ return newObject;
30
139
  }
31
140
 
32
- return object;
141
+ const value = _expand(object, [], promises);
142
+ if (promises.length !== 0) {
143
+ return Promise.all(promises).then(() => value);
144
+ }
145
+ return value;
33
146
  }