src/replaceMultiplicated.ts   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 39
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 32
mnd 4
bc 4
fnc 1
dl 0
loc 39
rs 10
bpm 4
cpm 5
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B replaceMultiplicated.ts ➔ replaceMultiplicated 0 36 5
1
export = replaceMultiplicated
2
3
function replaceMultiplicated(
4
  sources: string[],
5
  //TODO `searchValue: string[]` or `replacementMap`
6
  searchValue: string,
7
  replacements: string[]
8
) {
9
  const $return: (string|string[])[] = [...sources]
10
  , {length} = replacements
11
12
  for (let i = $return.length; i--;) {
13
    const line = sources[i]
14
15
    if (!line.includes(searchValue))
16
      continue
17
18
    const replaced: string[] = new Array(length)
19
20
    for (let j = length; j--;) {
21
      let next = line
22
      , pre: string
23
24
      //TODO Change to `.replaceAll` with common polyfill
25
      do {
26
        pre = next
27
        next = pre.replace(searchValue, replacements[j])
28
      } while (next !== pre)
29
30
      replaced[j] = next
31
    }
32
33
    $return[i] = replaced
34
  }
35
36
  //TODO Set up polyfill for `.flat()`
37
  return $return.flat()
38
}
39