@bablr/agast-helpers 0.11.0 → 0.11.2

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 CHANGED
@@ -402,6 +402,8 @@ export const parseObject = (input) => {
402
402
 
403
403
  obj[key] = value;
404
404
 
405
+ while (chr === ' ') chr = str[++p.idx];
406
+
405
407
  sep = chr === ',' ? chr : null;
406
408
  if (sep) {
407
409
  chr = str[++p.idx];
@@ -429,13 +431,23 @@ export const parseArray = (input) => {
429
431
  chr = str[++p.idx];
430
432
  }
431
433
 
432
- let value = parseExpression(p);
433
- chr = str[p.idx];
434
+ let first = true;
435
+ while (first || chr === ',') {
436
+ if (!first && chr === ',') {
437
+ chr = str[++p.idx];
438
+ while (chr === ' ') {
439
+ chr = str[++p.idx];
440
+ }
441
+ }
442
+ let value = parseExpression(p);
443
+ chr = str[p.idx];
434
444
 
435
- arr.push(value);
445
+ arr.push(value);
436
446
 
437
- while (chr === ' ') {
438
- chr = str[++p.idx];
447
+ while (chr === ' ') {
448
+ chr = str[++p.idx];
449
+ }
450
+ first = false;
439
451
  }
440
452
 
441
453
  if (chr !== ']') throw new Error();
@@ -473,12 +485,31 @@ export const parseString = (input) => {
473
485
  return result.join('');
474
486
  };
475
487
 
488
+ export const parseDigits = (input) => {
489
+ let p = buildParser(input);
490
+ let { str } = p;
491
+ let chr = str[p.idx];
492
+ let digits = [];
493
+
494
+ while (chr >= '0' && chr <= '9') {
495
+ digits.push(chr);
496
+ chr = str[++p.idx];
497
+ }
498
+
499
+ return parseInt(digits.join(''), 10);
500
+ };
501
+
476
502
  export const parseUnsignedInteger = (input) => {
477
503
  let p = buildParser(input);
478
504
  let { str } = p;
479
505
  let chr = str[p.idx];
480
506
  let digits = [];
481
507
 
508
+ if (chr === '0') {
509
+ chr = str[++p.idx];
510
+ return 0;
511
+ }
512
+
482
513
  while (chr >= '0' && chr <= '9') {
483
514
  digits.push(chr);
484
515
  chr = str[++p.idx];
@@ -487,6 +518,58 @@ export const parseUnsignedInteger = (input) => {
487
518
  return parseInt(digits.join(''), 10);
488
519
  };
489
520
 
521
+ export const parseInteger = (input) => {
522
+ let p = buildParser(input);
523
+ let { str } = p;
524
+ let chr = str[p.idx];
525
+ let negative = false;
526
+
527
+ if (chr === '-') {
528
+ negative = true;
529
+ chr = str[++p.idx];
530
+ }
531
+
532
+ return parseUnsignedInteger(p) * (negative ? -1 : 1);
533
+ };
534
+
535
+ export const parseNumber = (input) => {
536
+ let p = buildParser(input);
537
+ let { str } = p;
538
+ let chr = str[p.idx];
539
+ let startIdx = p.idx;
540
+
541
+ if (chr === '-') {
542
+ chr = str[++p.idx];
543
+ }
544
+
545
+ if (chr == '0') {
546
+ chr = str[++p.idx];
547
+ } else {
548
+ parseDigits(p);
549
+ chr = str[p.idx];
550
+ }
551
+
552
+ if (chr === '.') {
553
+ chr = str[++p.idx];
554
+ parseDigits(p);
555
+ chr = str[p.idx];
556
+ }
557
+
558
+ if (chr === 'e' || chr === 'E') {
559
+ chr = str[++p.idx];
560
+
561
+ if (chr === '+' || chr === '-') {
562
+ chr = str[++p.idx];
563
+ }
564
+
565
+ parseDigits(p);
566
+ }
567
+
568
+ let endIdx = p.idx;
569
+
570
+ return parseFloat(str.slice(startIdx, endIdx));
571
+ };
572
+
490
573
  export const parseEscape = (input) => {
491
574
  let p = buildParser(input);
492
575
  let { str } = p;
@@ -601,7 +684,7 @@ export const parseExpression = (input) => {
601
684
  p.idx += 9;
602
685
  return -Infinity;
603
686
  } else {
604
- throw new Error();
687
+ return parseNumber(p);
605
688
  }
606
689
  case '+':
607
690
  if (str[p.idx + 1] === 'I' && match(p, '+Infinity')) {
@@ -612,7 +695,7 @@ export const parseExpression = (input) => {
612
695
  }
613
696
  default:
614
697
  if (chr >= '0' && chr <= '9') {
615
- return parseUnsignedInteger(p);
698
+ return parseNumber(p);
616
699
  } else {
617
700
  throw new Error();
618
701
  }
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
@@ -13,20 +13,23 @@ import {
13
13
  StreamTag,
14
14
  } from './symbols.js';
15
15
  import { parseObject } from './builders.js';
16
+ import { arrayValues } from './iterable.js';
17
+ import { arrayJoin, arrayMap } from './object.js';
16
18
 
17
19
  let { isInteger, isFinite } = Number;
18
20
  let { isArray } = Array;
19
- let { freeze } = Object;
21
+ let { freeze, entries } = Object;
20
22
  let isString = (val) => typeof val === 'string';
21
23
  let isNumber = (val) => typeof val === 'number';
22
24
  let isObject = (val) => val && typeof val === 'object' && !isArray(val);
23
25
 
24
- export const printArray = (arr) => `[${arr.map((v) => printExpression(v)).join(', ')}]`;
26
+ export const printArray = (arr) =>
27
+ `[${[...arrayValues(arr)].map((v) => printExpression(v)).join(', ')}]`;
25
28
 
26
29
  export const printObject = (obj) => {
27
- let entries = Object.entries(obj);
28
- return entries.length
29
- ? `{ ${entries.map(([k, v]) => `${k}: ${printExpression(v)}`).join(', ')} }`
30
+ let entries_ = entries(obj);
31
+ return entries_.length
32
+ ? `{ ${entries_.map(([k, v]) => `${k}: ${printExpression(v)}`).join(', ')} }`
30
33
  : '{}';
31
34
  };
32
35
 
@@ -62,20 +65,18 @@ export const printAttributes = (attributes) => {
62
65
  };
63
66
 
64
67
  export const printIdentifierPath = (path) => {
65
- return Array.prototype.map
66
- .call(path, (segment) => {
67
- let { name, type } = segment;
68
- if (name) {
69
- return printIdentifier(name);
70
- }
71
- if (type) {
72
- if (type !== '..') throw new Error();
73
- return type;
74
- } else {
75
- throw new Error();
76
- }
77
- })
78
- .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('.');
79
80
  };
80
81
 
81
82
  let escapeReplacer = (esc) => {
@@ -257,7 +258,7 @@ export const printStreamTag = (tag) => {
257
258
 
258
259
  let { processPath, stream } = tag.value;
259
260
 
260
- return `<${Array.prototype.join.call(processPath, '.')}-${stream}>`;
261
+ return `<${arrayJoin(processPath, '.')}-${stream}>`;
261
262
  };
262
263
 
263
264
  export const printCloseNodeTag = (tag) => {
@@ -272,7 +273,7 @@ export const printAttributeDefinition = (tag) => {
272
273
  let { path, value } = tag.value;
273
274
 
274
275
  return `{ ${printIdentifierPath(
275
- Array.prototype.map.call(path, (name) => freeze({ type: null, name })),
276
+ arrayMap(path, (name) => freeze({ type: null, name })),
276
277
  )}: ${printExpression(value)} }`;
277
278
  };
278
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 = reduce.call(
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(slice.call(idx, 1), module_.getAt(slice.call(idx, 0, 1), tags));
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
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bablr/agast-helpers",
3
3
  "description": "Helper functions for working with agAST trees",
4
- "version": "0.11.0",
4
+ "version": "0.11.2",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [