pacc 4.20.0 → 4.21.1

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