You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
937 B
39 lines
937 B
2 years ago
|
const {inspect} = require('util');
|
||
|
|
||
|
/////////////////////////////////////////////////////////////
|
||
|
// Returns {line, column} of an index within multi-line text.
|
||
|
function getIndexPos(text, index) {
|
||
|
let lineIdx = 0, colIdx = index, pos = 0;
|
||
|
do {
|
||
|
pos = text.indexOf('\n', pos);
|
||
|
if (pos === -1 || index < pos + 1) {
|
||
|
break;
|
||
|
}
|
||
|
lineIdx++;
|
||
|
pos++;
|
||
|
colIdx = index - pos;
|
||
|
} while (pos < index);
|
||
|
return {
|
||
|
line: lineIdx + 1,
|
||
|
column: colIdx + 1
|
||
|
};
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////////
|
||
|
// Returns a space gap for console output.
|
||
|
function messageGap(level) {
|
||
|
return ' '.repeat(level * 4);
|
||
|
}
|
||
|
|
||
|
////////////////////////////////////////////////////
|
||
|
// Type inspection
|
||
|
function addInspection(type, cb) {
|
||
|
type[inspect.custom] = cb;
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
getIndexPos,
|
||
|
messageGap,
|
||
|
addInspection
|
||
|
};
|