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