locate.js
452 Bytes
export default function locate ( source, index ) {
var lines = source.split( '\n' );
var len = lines.length;
var lineStart = 0;
var i;
for ( i = 0; i < len; i += 1 ) {
var line = lines[i];
var lineEnd = lineStart + line.length + 1; // +1 for newline
if ( lineEnd > index ) {
return { line: i + 1, column: index - lineStart, char: i };
}
lineStart = lineEnd;
}
throw new Error( 'Could not determine location of character' );
}