UNPKG

@visulima/error

Version:

Error with more than just a message, stacktrace parsing.

55 lines (53 loc) 1.91 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const binarySearch = /* @__PURE__ */ __name((element, array) => { let m = 0; let n = array.length - 2; while (m < n) { const key = m + (n - m >> 1); if (element < array[key]) { n = key - 1; } else if (element >= array[key + 1]) { m = key + 1; } else { m = key; break; } } return m; }, "binarySearch"); const getLineStartIndexes = /* @__PURE__ */ __name((string_) => ( // eslint-disable-next-line unicorn/no-array-reduce string_.split(/\n|\r(?!\n)/).reduce( (accumulator, current) => { accumulator.push(accumulator.at(-1) + current.length + 1); return accumulator; }, [0] ) ), "getLineStartIndexes"); const indexToLineColumn = /* @__PURE__ */ __name((input, index, options) => { const skipChecks = options?.skipChecks ?? false; if (!skipChecks && (!Array.isArray(input) && typeof input !== "string" || (typeof input === "string" || Array.isArray(input)) && input.length === 0)) { return { column: 0, line: 0 }; } if (!skipChecks && (typeof index !== "number" || typeof input === "string" && index >= input.length || Array.isArray(input) && index + 1 >= input.at(-1))) { return { column: 0, line: 0 }; } if (typeof input === "string") { const startIndexesOfEachLine = getLineStartIndexes(input); const line2 = binarySearch(index, startIndexesOfEachLine); return { // eslint-disable-next-line security/detect-object-injection column: index - startIndexesOfEachLine[line2] + 1, line: line2 + 1 }; } const line = binarySearch(index, input); return { // eslint-disable-next-line security/detect-object-injection column: index - input[line] + 1, line: line + 1 }; }, "indexToLineColumn"); export { indexToLineColumn as default };