Passed
Push — master ( 9680fc...eb4eeb )
by Andrii
02:45
created

src/replaceMultiplicated.ts   A

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 37
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 31
mnd 4
bc 4
fnc 1
dl 0
loc 37
bpm 4
cpm 5
noi 0
c 0
b 0
f 0
rs 10
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.concat()
10
  , {length} = replacements
11
12
  for (let i = $return.length; i--;) {
13
    const line = sources[i]
14
    if (!line.includes(searchValue))
15
      continue
16
17
    const replaced: string[] = new Array(length)
18
19
    for (let j = length; j--;) {
20
      let next = line
21
      , pre: string
22
23
      //TODO Change to `.replaceAll` with common polyfill
24
      do {
25
        pre = next
26
        next = pre.replace(searchValue, replacements[j])
27
      } while(next !== pre)
28
29
      replaced[j] = next
30
    }
31
32
    $return[i] = replaced
33
  }
34
35
  //TODO Set up polyfill for `.flat()`
36
  return $return.flat()
37
}