@bablr/agast-helpers 0.11.1 → 0.11.3
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/lib/builders.js +2 -0
- package/lib/object.js +42 -0
- package/lib/print.js +15 -16
- package/lib/tags.js +3 -7
- package/package.json +1 -1
package/lib/builders.js
CHANGED
|
@@ -898,10 +898,12 @@ export const parseReferenceTag = (tag) => {
|
|
|
898
898
|
chr = str[++p.idx];
|
|
899
899
|
}
|
|
900
900
|
|
|
901
|
+
let quotedIdent = chr === '`';
|
|
901
902
|
if (!type || (type === '#' && canStartIdentifier(chr))) {
|
|
902
903
|
name = parseIdentifier(p);
|
|
903
904
|
chr = str[p.idx];
|
|
904
905
|
}
|
|
906
|
+
if (!type && !quotedIdent && name === 'null') throw new Error();
|
|
905
907
|
while (chr === ' ') chr = str[++p.idx];
|
|
906
908
|
|
|
907
909
|
let flags = parseReferenceFlags(p);
|
package/lib/object.js
CHANGED
|
@@ -123,6 +123,48 @@ export const when = (condition, value) => {
|
|
|
123
123
|
: emptySpreadable;
|
|
124
124
|
};
|
|
125
125
|
|
|
126
|
+
let arrayIncludes_ = Array.prototype.includes;
|
|
127
|
+
export const arrayIncludes = (arr, key) => {
|
|
128
|
+
if (!isArray(arr)) throw new Error();
|
|
129
|
+
|
|
130
|
+
return arrayIncludes_.call(arr, key);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
let arraySlice_ = Array.prototype.slice;
|
|
134
|
+
export const arraySlice = (arr, start, end) => {
|
|
135
|
+
if (!isArray(arr)) throw new Error();
|
|
136
|
+
|
|
137
|
+
return arraySlice_.call(arr, start, end);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
let arrayJoin_ = Array.prototype.join;
|
|
141
|
+
export const arrayJoin = (arr, sep) => {
|
|
142
|
+
if (!isArray(arr)) throw new Error();
|
|
143
|
+
|
|
144
|
+
return arrayJoin_.call(arr, sep);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
let arrayFindIndex_ = Array.prototype.findIndex;
|
|
148
|
+
export const arrayFindIndex = (arr, value) => {
|
|
149
|
+
if (!isArray(arr)) throw new Error();
|
|
150
|
+
|
|
151
|
+
return arrayFindIndex_.call(arr, value);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
let arrayMap_ = Array.prototype.map;
|
|
155
|
+
export const arrayMap = (arr, fn) => {
|
|
156
|
+
if (!isArray(arr)) throw new Error();
|
|
157
|
+
|
|
158
|
+
return arrayMap_.call(arr, fn);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
let arrayReduce_ = Array.prototype.reduce;
|
|
162
|
+
export const arrayReduce = (arr, fn, initial) => {
|
|
163
|
+
if (!isArray(arr)) throw new Error();
|
|
164
|
+
|
|
165
|
+
return arrayReduce_.call(arr, fn, initial);
|
|
166
|
+
};
|
|
167
|
+
|
|
126
168
|
export const isObject = (obj) => obj !== null && typeof obj === 'object';
|
|
127
169
|
export const isPlainObject = (val) => val && [Object.prototype, null].includes(getPrototypeOf(val));
|
|
128
170
|
export const isFunction = (obj) => typeof obj === 'function';
|
package/lib/print.js
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from './symbols.js';
|
|
15
15
|
import { parseObject } from './builders.js';
|
|
16
16
|
import { arrayValues } from './iterable.js';
|
|
17
|
+
import { arrayJoin, arrayMap } from './object.js';
|
|
17
18
|
|
|
18
19
|
let { isInteger, isFinite } = Number;
|
|
19
20
|
let { isArray } = Array;
|
|
@@ -64,20 +65,18 @@ export const printAttributes = (attributes) => {
|
|
|
64
65
|
};
|
|
65
66
|
|
|
66
67
|
export const printIdentifierPath = (path) => {
|
|
67
|
-
return
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (type)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
})
|
|
80
|
-
.join('.');
|
|
68
|
+
return arrayMap(path, (segment) => {
|
|
69
|
+
let { name, type } = segment;
|
|
70
|
+
if (name) {
|
|
71
|
+
return printIdentifier(name);
|
|
72
|
+
}
|
|
73
|
+
if (type) {
|
|
74
|
+
if (type !== '..') throw new Error();
|
|
75
|
+
return type;
|
|
76
|
+
} else {
|
|
77
|
+
throw new Error();
|
|
78
|
+
}
|
|
79
|
+
}).join('.');
|
|
81
80
|
};
|
|
82
81
|
|
|
83
82
|
let escapeReplacer = (esc) => {
|
|
@@ -259,7 +258,7 @@ export const printStreamTag = (tag) => {
|
|
|
259
258
|
|
|
260
259
|
let { processPath, stream } = tag.value;
|
|
261
260
|
|
|
262
|
-
return `<${
|
|
261
|
+
return `<${arrayJoin(processPath, '.')}-${stream}>`;
|
|
263
262
|
};
|
|
264
263
|
|
|
265
264
|
export const printCloseNodeTag = (tag) => {
|
|
@@ -274,7 +273,7 @@ export const printAttributeDefinition = (tag) => {
|
|
|
274
273
|
let { path, value } = tag.value;
|
|
275
274
|
|
|
276
275
|
return `{ ${printIdentifierPath(
|
|
277
|
-
|
|
276
|
+
arrayMap(path, (name) => freeze({ type: null, name })),
|
|
278
277
|
)}: ${printExpression(value)} }`;
|
|
279
278
|
};
|
|
280
279
|
|
package/lib/tags.js
CHANGED
|
@@ -14,13 +14,11 @@ import {
|
|
|
14
14
|
BindingTag,
|
|
15
15
|
Document,
|
|
16
16
|
} from './symbols.js';
|
|
17
|
-
import { freezeRecord, isArray, isObject, isString } from './object.js';
|
|
17
|
+
import { arrayReduce, arraySlice, freezeRecord, isArray, isObject, isString } from './object.js';
|
|
18
18
|
import { buildReference, parseTag, parseTagType } from './builders.js';
|
|
19
19
|
import { printTag } from './print.js';
|
|
20
20
|
import { arrayValues } from './iterable.js';
|
|
21
21
|
|
|
22
|
-
let { reduce } = Array.prototype;
|
|
23
|
-
|
|
24
22
|
export { defaultNodeSize };
|
|
25
23
|
|
|
26
24
|
export const referenceIsSingular = (ref) => {
|
|
@@ -189,7 +187,7 @@ const __getPropertyTagsIndex = (tags, type, name, index) => {
|
|
|
189
187
|
export const compareNames = (a, b) => (a > b ? 1 : b > a ? -1 : 0);
|
|
190
188
|
|
|
191
189
|
export const sumValues = (values, depth) => {
|
|
192
|
-
let mapStats =
|
|
190
|
+
let mapStats = arrayReduce(
|
|
193
191
|
values,
|
|
194
192
|
(acc, val) => {
|
|
195
193
|
const { names, types } = acc;
|
|
@@ -298,11 +296,9 @@ const {
|
|
|
298
296
|
findPath,
|
|
299
297
|
} = module_;
|
|
300
298
|
|
|
301
|
-
let { slice } = Array.prototype;
|
|
302
|
-
|
|
303
299
|
const getAt = (idx, tags) => {
|
|
304
300
|
if (isArray(idx) && idx.length > 1) {
|
|
305
|
-
return module_.getAt(
|
|
301
|
+
return module_.getAt(arraySlice(idx, 1), module_.getAt(arraySlice(idx, 0, 1), tags));
|
|
306
302
|
} else {
|
|
307
303
|
return module_.getAt(idx, tags);
|
|
308
304
|
}
|