1"use strict"; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.insertContentsAtOffset = insertContentsAtOffset; 7exports.replaceContentsWithOffset = replaceContentsWithOffset; 8exports.searchFromOffset = searchFromOffset; 9/** 10 * Insert contents at given offset 11 * @param srcContents source contents 12 * @param insertion content to insert 13 * @param offset `srcContents` offset to insert `insertion` 14 * @returns updated contents 15 */ 16function insertContentsAtOffset(srcContents, insertion, offset) { 17 const srcContentsLength = srcContents.length; 18 if (offset < 0 || offset > srcContentsLength) { 19 throw new Error('Invalid parameters.'); 20 } 21 if (offset === 0) { 22 return `${insertion}${srcContents}`; 23 } else if (offset === srcContentsLength) { 24 return `${srcContents}${insertion}`; 25 } 26 const prefix = srcContents.substring(0, offset); 27 const suffix = srcContents.substring(offset); 28 return `${prefix}${insertion}${suffix}`; 29} 30 31/** 32 * Replace contents at given start and end offset 33 * 34 * @param contents source contents 35 * @param replacement new contents to place in [startOffset:endOffset] 36 * @param startOffset `contents` start offset for replacement 37 * @param endOffset `contents` end offset for replacement 38 * @returns updated contents 39 */ 40function replaceContentsWithOffset(contents, replacement, startOffset, endOffset) { 41 const contentsLength = contents.length; 42 if (startOffset < 0 || endOffset < 0 || startOffset >= contentsLength || endOffset >= contentsLength || startOffset > endOffset) { 43 throw new Error('Invalid parameters.'); 44 } 45 const prefix = contents.substring(0, startOffset); 46 const suffix = contents.substring(endOffset + 1); 47 return `${prefix}${replacement}${suffix}`; 48} 49 50/** 51 * String.prototype.search() with offset support 52 * 53 * @param source source string to search 54 * @param regexp RegExp pattern to search 55 * @param offset start offset of `source` to search `regexp` pattern 56 * @returns The index of the first match between the regular expression and the given string, or -1 if no match was found. 57 */ 58function searchFromOffset(source, regexp, offset) { 59 const target = source.substring(offset); 60 const matchedIndex = target.search(regexp); 61 return matchedIndex < 0 ? matchedIndex : matchedIndex + offset; 62} 63//# sourceMappingURL=commonCodeMod.js.map