UNPKG

526 BJavaScriptView Raw
1/**
2 * Represents a location in a Source.
3 */
4
5/**
6 * Takes a Source and a UTF-8 character offset, and returns the corresponding
7 * line and column as a SourceLocation.
8 */
9export function getLocation(source, position) {
10 var lineRegexp = /\r\n|[\n\r]/g;
11 var line = 1;
12 var column = position + 1;
13 var match;
14
15 while ((match = lineRegexp.exec(source.body)) && match.index < position) {
16 line += 1;
17 column = position + 1 - (match.index + match[0].length);
18 }
19
20 return {
21 line: line,
22 column: column
23 };
24}