1 | /*
|
---|
2 | * MIT License http://opensource.org/licenses/MIT
|
---|
3 | * Author: Ben Holloway @bholloway
|
---|
4 | */
|
---|
5 | 'use strict';
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Given a sourcemap position create a new maybeObject with only line and column properties.
|
---|
9 | *
|
---|
10 | * @param {*|{line: number, column: number}} maybeObj Possible location hash
|
---|
11 | * @returns {{line: number, column: number}} Location hash with possible NaN values
|
---|
12 | */
|
---|
13 | function sanitise(maybeObj) {
|
---|
14 | var obj = !!maybeObj && typeof maybeObj === 'object' && maybeObj || {};
|
---|
15 | return {
|
---|
16 | line: isNaN(obj.line) ? NaN : obj.line,
|
---|
17 | column: isNaN(obj.column) ? NaN : obj.column
|
---|
18 | };
|
---|
19 | }
|
---|
20 |
|
---|
21 | exports.sanitise = sanitise;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Infer a line and position delta based on the linebreaks in the given string.
|
---|
25 | *
|
---|
26 | * @param candidate {string} A string with possible linebreaks
|
---|
27 | * @returns {{line: number, column: number}} A position object where line and column are deltas
|
---|
28 | */
|
---|
29 | function strToOffset(candidate) {
|
---|
30 | var split = candidate.split(/\r\n|\n/g);
|
---|
31 | var last = split[split.length - 1];
|
---|
32 | return {
|
---|
33 | line: split.length - 1,
|
---|
34 | column: last.length
|
---|
35 | };
|
---|
36 | }
|
---|
37 |
|
---|
38 | exports.strToOffset = strToOffset;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Add together a list of position elements.
|
---|
42 | *
|
---|
43 | * Lines are added. If the new line is zero the column is added otherwise it is overwritten.
|
---|
44 | *
|
---|
45 | * @param {{line: number, column: number}[]} list One or more sourcemap position elements to add
|
---|
46 | * @returns {{line: number, column: number}} Resultant position element
|
---|
47 | */
|
---|
48 | function add(list) {
|
---|
49 | return list
|
---|
50 | .slice(1)
|
---|
51 | .reduce(
|
---|
52 | function (accumulator, element) {
|
---|
53 | return {
|
---|
54 | line: accumulator.line + element.line,
|
---|
55 | column: element.line > 0 ? element.column : accumulator.column + element.column,
|
---|
56 | };
|
---|
57 | },
|
---|
58 | list[0]
|
---|
59 | );
|
---|
60 | }
|
---|
61 |
|
---|
62 | exports.add = add;
|
---|