chalk/source/util.js

40 lines
1 KiB
JavaScript
Raw Normal View History

2019-07-12 13:51:07 +07:00
'use strict';
2019-07-12 09:40:23 +03:00
const stringReplaceAll = (string, substring, replacer) => {
let index = string.indexOf(substring);
if (index === -1) {
return string;
}
2019-07-12 13:59:50 +07:00
const substringLength = substring.length;
let endIndex = 0;
let returnValue = '';
2019-07-12 09:40:23 +03:00
do {
2019-07-12 23:19:56 +04:30
returnValue += string.substr(endIndex, index - endIndex) + substring + replacer;
2019-07-12 13:59:50 +07:00
endIndex = index + substringLength;
index = string.indexOf(substring, endIndex);
2019-07-12 09:40:23 +03:00
} while (index !== -1);
2019-07-12 13:59:50 +07:00
returnValue += string.substr(endIndex);
return returnValue;
2019-07-12 09:40:23 +03:00
};
const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
2019-07-12 13:59:50 +07:00
let endIndex = 0;
let returnValue = '';
2019-07-12 09:40:23 +03:00
do {
const gotCR = string[index - 1] === '\r';
2019-07-12 13:59:50 +07:00
returnValue += string.substr(endIndex, (gotCR ? index - 1 : index) - endIndex) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
endIndex = index + 1;
index = string.indexOf('\n', endIndex);
2019-07-12 09:40:23 +03:00
} while (index !== -1);
2019-07-12 13:59:50 +07:00
returnValue += string.substr(endIndex);
return returnValue;
2019-07-12 09:40:23 +03:00
};
module.exports = {
stringReplaceAll,
stringEncaseCRLFWithFirstIndex
};