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