@hyperjump/json-schema 0.23.3 → 0.23.4

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.
@@ -2290,6 +2290,38 @@ var moo$1 = {exports: {}};
2290
2290
  }
2291
2291
  }
2292
2292
 
2293
+ function pad(s, length) {
2294
+ if (s.length > length) {
2295
+ return s
2296
+ }
2297
+ return Array(length - s.length + 1).join(" ") + s
2298
+ }
2299
+
2300
+ function lastNLines(string, numLines) {
2301
+ var position = string.length;
2302
+ var lineBreaks = 0;
2303
+ while (true) {
2304
+ var idx = string.lastIndexOf("\n", position - 1);
2305
+ if (idx === -1) {
2306
+ break;
2307
+ } else {
2308
+ lineBreaks++;
2309
+ }
2310
+ position = idx;
2311
+ if (lineBreaks === numLines) {
2312
+ break;
2313
+ }
2314
+ if (position === 0) {
2315
+ break;
2316
+ }
2317
+ }
2318
+ var startPosition =
2319
+ lineBreaks < numLines ?
2320
+ 0 :
2321
+ position + 1;
2322
+ return string.substring(startPosition).split("\n")
2323
+ }
2324
+
2293
2325
  function objectToRules(object) {
2294
2326
  var keys = Object.getOwnPropertyNames(object);
2295
2327
  var result = [];
@@ -2572,39 +2604,31 @@ var moo$1 = {exports: {}};
2572
2604
  }
2573
2605
 
2574
2606
  function keywordTransform(map) {
2575
- var reverseMap = Object.create(null);
2576
- var byLength = Object.create(null);
2607
+
2608
+ // Use a JavaScript Map to map keywords to their corresponding token type
2609
+ // unless Map is unsupported, then fall back to using an Object:
2610
+ var isMap = typeof Map !== 'undefined';
2611
+ var reverseMap = isMap ? new Map : Object.create(null);
2612
+
2577
2613
  var types = Object.getOwnPropertyNames(map);
2578
2614
  for (var i = 0; i < types.length; i++) {
2579
2615
  var tokenType = types[i];
2580
2616
  var item = map[tokenType];
2581
2617
  var keywordList = Array.isArray(item) ? item : [item];
2582
2618
  keywordList.forEach(function(keyword) {
2583
- (byLength[keyword.length] = byLength[keyword.length] || []).push(keyword);
2584
2619
  if (typeof keyword !== 'string') {
2585
2620
  throw new Error("keyword must be string (in keyword '" + tokenType + "')")
2586
2621
  }
2587
- reverseMap[keyword] = tokenType;
2622
+ if (isMap) {
2623
+ reverseMap.set(keyword, tokenType);
2624
+ } else {
2625
+ reverseMap[keyword] = tokenType;
2626
+ }
2588
2627
  });
2589
2628
  }
2590
-
2591
- // fast string lookup
2592
- // https://jsperf.com/string-lookups
2593
- function str(x) { return JSON.stringify(x) }
2594
- var source = '';
2595
- source += 'switch (value.length) {\n';
2596
- for (var length in byLength) {
2597
- var keywords = byLength[length];
2598
- source += 'case ' + length + ':\n';
2599
- source += 'switch (value) {\n';
2600
- keywords.forEach(function(keyword) {
2601
- var tokenType = reverseMap[keyword];
2602
- source += 'case ' + str(keyword) + ': return ' + str(tokenType) + '\n';
2603
- });
2604
- source += '}\n';
2629
+ return function(k) {
2630
+ return isMap ? reverseMap.get(k) : reverseMap[k]
2605
2631
  }
2606
- source += '}\n';
2607
- return Function('value', source) // type
2608
2632
  }
2609
2633
 
2610
2634
  /***************************************************************************/
@@ -2623,6 +2647,7 @@ var moo$1 = {exports: {}};
2623
2647
  this.line = info ? info.line : 1;
2624
2648
  this.col = info ? info.col : 1;
2625
2649
  this.queuedToken = info ? info.queuedToken : null;
2650
+ this.queuedText = info ? info.queuedText: "";
2626
2651
  this.queuedThrow = info ? info.queuedThrow : null;
2627
2652
  this.setState(info ? info.state : this.startState);
2628
2653
  this.stack = info && info.stack ? info.stack.slice() : [];
@@ -2636,6 +2661,7 @@ var moo$1 = {exports: {}};
2636
2661
  state: this.state,
2637
2662
  stack: this.stack.slice(),
2638
2663
  queuedToken: this.queuedToken,
2664
+ queuedText: this.queuedText,
2639
2665
  queuedThrow: this.queuedThrow,
2640
2666
  }
2641
2667
  };
@@ -2767,7 +2793,8 @@ var moo$1 = {exports: {}};
2767
2793
 
2768
2794
  // throw, if no rule with {error: true}
2769
2795
  if (group.shouldThrow) {
2770
- throw new Error(this.formatError(token, "invalid syntax"))
2796
+ var err = new Error(this.formatError(token, "invalid syntax"));
2797
+ throw err;
2771
2798
  }
2772
2799
 
2773
2800
  if (group.pop) this.popState();
@@ -2808,13 +2835,28 @@ var moo$1 = {exports: {}};
2808
2835
  col: this.col,
2809
2836
  };
2810
2837
  }
2811
- var start = Math.max(0, token.offset - token.col + 1);
2812
- var eol = token.lineBreaks ? token.text.indexOf('\n') : token.text.length;
2813
- var firstLine = this.buffer.substring(start, token.offset + eol);
2814
- message += " at line " + token.line + " col " + token.col + ":\n\n";
2815
- message += " " + firstLine + "\n";
2816
- message += " " + Array(token.col).join(" ") + "^";
2817
- return message
2838
+
2839
+ var numLinesAround = 2;
2840
+ var firstDisplayedLine = Math.max(token.line - numLinesAround, 1);
2841
+ var lastDisplayedLine = token.line + numLinesAround;
2842
+ var lastLineDigits = String(lastDisplayedLine).length;
2843
+ var displayedLines = lastNLines(
2844
+ this.buffer,
2845
+ (this.line - token.line) + numLinesAround + 1
2846
+ )
2847
+ .slice(0, 5);
2848
+ var errorLines = [];
2849
+ errorLines.push(message + " at line " + token.line + " col " + token.col + ":");
2850
+ errorLines.push("");
2851
+ for (var i = 0; i < displayedLines.length; i++) {
2852
+ var line = displayedLines[i];
2853
+ var lineNo = firstDisplayedLine + i;
2854
+ errorLines.push(pad(String(lineNo), lastLineDigits) + " " + line);
2855
+ if (lineNo === token.line) {
2856
+ errorLines.push(pad("", lastLineDigits + token.col + 1) + "^");
2857
+ }
2858
+ }
2859
+ return errorLines.join("\n")
2818
2860
  };
2819
2861
 
2820
2862
  Lexer.prototype.clone = function() {