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