pacc 4.19.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.19.0",
3
+ "version": "4.21.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/expand.mjs ADDED
@@ -0,0 +1,146 @@
1
+ import { parse } from "./expression.mjs";
2
+ import { tokens } from "./tokens.mjs";
3
+
4
+ export function expand(object, context) {
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;
38
+ });
39
+
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)])
47
+ );
48
+ }
49
+
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;
111
+ }
112
+
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;
135
+ }
136
+ }
137
+
138
+ return newObject;
139
+ }
140
+
141
+ const value = _expand(object, [], promises);
142
+ if (promises.length !== 0) {
143
+ return Promise.all(promises).then(() => value);
144
+ }
145
+ return value;
146
+ }
@@ -172,8 +172,10 @@ export function parse(context) {
172
172
 
173
173
  case "infix": {
174
174
  let right = expression(token.precedence);
175
+
175
176
  if (typeof left === typeof right) {
176
177
  switch (typeof left) {
178
+ case "string":
177
179
  case "number":
178
180
  case "bigint":
179
181
  return binop(token, left, right, binopError);
package/src/module.mjs CHANGED
@@ -5,6 +5,7 @@ export * from "./attributes.mjs";
5
5
  export * from "./common-attributes.mjs";
6
6
  export * from "./properties.mjs";
7
7
  export * from "./expression.mjs";
8
+ export * from "./expand.mjs";
8
9
  export {
9
10
  setAttribute,
10
11
  getAttribute,
@@ -0,0 +1 @@
1
+ export function expand(object: any, context: any): any;
@@ -5,4 +5,5 @@ export * from "./attributes.mjs";
5
5
  export * from "./common-attributes.mjs";
6
6
  export * from "./properties.mjs";
7
7
  export * from "./expression.mjs";
8
+ export * from "./expand.mjs";
8
9
  export { setAttribute, getAttribute, getAttributeAndOperator } from "./settergetter.mjs";