Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[6a3a178] | 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 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.