1 | 'use strict';
|
2 |
|
3 | const stringReplaceAll = (string, substring, replacer) => {
|
4 | let index = string.indexOf(substring);
|
5 | if (index === -1) {
|
6 | return string;
|
7 | }
|
8 |
|
9 | const substringLength = substring.length;
|
10 | let endIndex = 0;
|
11 | let returnValue = '';
|
12 | do {
|
13 | returnValue += string.substr(endIndex, index - endIndex) + substring + replacer;
|
14 | endIndex = index + substringLength;
|
15 | index = string.indexOf(substring, endIndex);
|
16 | } while (index !== -1);
|
17 |
|
18 | returnValue += string.substr(endIndex);
|
19 | return returnValue;
|
20 | };
|
21 |
|
22 | const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
|
23 | let endIndex = 0;
|
24 | let returnValue = '';
|
25 | do {
|
26 | const gotCR = string[index - 1] === '\r';
|
27 | returnValue += string.substr(endIndex, (gotCR ? index - 1 : index) - endIndex) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
|
28 | endIndex = index + 1;
|
29 | index = string.indexOf('\n', endIndex);
|
30 | } while (index !== -1);
|
31 |
|
32 | returnValue += string.substr(endIndex);
|
33 | return returnValue;
|
34 | };
|
35 |
|
36 | module.exports = {
|
37 | stringReplaceAll,
|
38 | stringEncaseCRLFWithFirstIndex
|
39 | };
|