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