Total Complexity | 10 |
Complexity/F | 5 |
Lines of Code | 35 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
1 | |||
2 | export function earlierOf (a, b) { |
||
3 | // inspects the .start property of a and b and returns the one |
||
4 | // with the lowest start position |
||
5 | if (b.start !== -1 && (a.start === -1 || b.start < a.start)) { |
||
6 | return b |
||
7 | } else { |
||
8 | return a |
||
9 | } |
||
10 | } |
||
11 | |||
12 | export function replaceLineEndings (txt, CRLF) { |
||
13 | // replaces line endings within txt, with CRLF if CRLF is true, otherwise just LF |
||
14 | if (CRLF === true) { |
||
15 | // NB can't do the replacement of '\n' with '\r\n' using regex due to javascript limitations |
||
16 | let p = 0 // current position in the string |
||
17 | let x = 0 // location of '\n' in the string |
||
18 | let t = '' // output string |
||
19 | while (true) { |
||
20 | x = txt.indexOf('\n', p) |
||
21 | if (x === -1) { |
||
22 | // we've not got any more '\n' in the string so complete and exit |
||
23 | t = t + txt.substr(p) |
||
24 | return t |
||
25 | } else if (x === 0 || txt.substr(x - 1, 1) !== '\r') { |
||
26 | t = t + txt.substring(p, x) + '\r\n' |
||
27 | p = x + 1 |
||
28 | } else { |
||
29 | t = t + txt.substring(p, x + 1) |
||
30 | p = x + 1 |
||
31 | } |
||
32 | } |
||
33 | } else { |
||
34 | return txt.replace(/(\r\n)/g, '\n') |
||
35 | } |
||
36 | } |
||
37 |