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