UNPKG

@stryke/json

Version:

A package containing JSON parsing/stringify utilities used by Storm Software.

72 lines (71 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.codeFrameColumns = codeFrameColumns; const k = /\r\n|[\n\r\u2028\u2029]/; function N(s, l, a = {}) { const c = { column: 0, line: -1, ...s.start }, m = { ...c, ...s.end }, { linesAbove: h = 2, linesBelow: f = 3 } = a || {}, i = c.line, t = c.column, b = m.line, o = m.column; let d = Math.max(i - (h + 1), 0), u = Math.min(l.length, b + f); i === -1 && (d = 0), b === -1 && (u = l.length); const L = b - i, e = {}; if (L) for (let n = 0; n <= L; n++) { const r = n + i; if (!t) e[r] = !0;else if (n === 0) { const g = l[r - 1]?.length ?? 0; e[r] = [t, g - t + 1]; } else if (n === L) e[r] = [0, o];else { const g = l[r - n]?.length ?? 0; e[r] = [0, g]; } } else t === o ? e[i] = t ? [t, 0] : !0 : e[i] = [t, o - t]; return { start: d, end: u, markerLines: e }; } function codeFrameColumns(s, l, a = {}) { const c = s.split(k), { start: m, end: h, markerLines: f } = N(l, c, a), i = String(h).length; return (a.highlight ? a.highlight(s) : s).split(k).slice(m, h).map((o, d) => { const u = m + 1 + d, e = ` ${` ${u}`.slice(-i)} | `, n = !!(f[u] ?? !1); if (n) { let r = ""; if (Array.isArray(n)) { const g = o.slice(0, Math.max(n[0] - 1, 0)).replace(/[^\t]/g, " "), p = n[1] || 1; r = [` `, e.replace(/\d/g, " "), g, "^".repeat(p)].join(""); } return [">", e, o, r].join(""); } return ` ${e}${o}`; }).join(` `); }