Sha256: 1dbc1dc6b97db9901acc8f3a8b64446538ce7fcbdbff87441c08cb3b3bc83ebf
Contents?: true
Size: 753 Bytes
Versions: 3
Compression:
Stored size: 753 Bytes
Contents
export default function getLocator ( source ) { const originalLines = source.split( '\n' ); let start = 0; const lineRanges = originalLines.map( ( line, i ) => { const end = start + line.length + 1; const range = { start, end, line: i }; start = end; return range; }); let i = 0; function rangeContains ( range, index ) { return range.start <= index && index < range.end; } function getLocation ( range, index ) { return { line: range.line, column: index - range.start }; } return function locate ( index ) { let range = lineRanges[i]; const d = index >= range.end ? 1 : -1; while ( range ) { if ( rangeContains( range, index ) ) return getLocation( range, index ); i += d; range = lineRanges[i]; } }; }
Version data entries
3 entries across 3 versions & 1 rubygems