lion/packages-node/providence-analytics/inlined-swc-to-babel/lib/swc/get-position-by-offset.cjs
2024-05-14 13:47:11 +02:00

24 lines
519 B
JavaScript

'use strict';
module.exports.getPositionByOffset = (offset, source) => {
let line = 1;
let column = 0;
if (offset > source.length)
throw Error('end cannot be more then length ' + offset + ', ' + source.length);
for (let i = 0; i < offset; i++) {
if (source[i] === '\n' && i !== offset - 1) {
line++;
column = 0;
} else {
column++;
}
}
return {
line,
column,
index: offset - 1,
};
};