Passed
Push — master ( fc2d57...76a657 )
by Barry
01:39
created

src/helpers.js   A

Complexity

Total Complexity 10
Complexity/F 5

Size

Lines of Code 35
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 0
c 4
b 0
f 0
nc 1
dl 0
loc 35
rs 10
wmc 10
mnd 4
bc 10
fnc 2
bpm 5
cpm 5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B helpers.js ➔ replaceLineEndings 0 25 6
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