UNPKG

code-replacer

Version:

Replace codes line by line with regex for target files

71 lines 3.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MatchingPoints = void 0; const template_1 = require("./template"); const matchAll_1 = __importDefault(require("./matchAll")); class MatchingPoints extends Array { constructor() { super(); } pushIndex({ matchingPtIdx, matchingStr, replacedString, }) { for (let otherPtsCandidateIdx = matchingPtIdx + 1; otherPtsCandidateIdx < this.length; otherPtsCandidateIdx++) { const otherPts = this[otherPtsCandidateIdx]; for (const candItem of otherPts) { candItem.index += replacedString.length - matchingStr.length; } } } sortMatchingPoints() { for (let matchingPtIdx = 0; matchingPtIdx < this.length; matchingPtIdx++) { const cands = this[matchingPtIdx]; cands.leastIdx = Number.MAX_SAFE_INTEGER; for (let candIdx = 0; candIdx < cands.length; candIdx++) { if (cands.leastIdx > cands[candIdx].index) { cands.leastIdx = cands[candIdx].index; } } } // Sort matching points to match in asc order this.sort((lPt, rPt) => { return lPt.leastIdx - rPt.leastIdx; }); } addMatchingPoint({ srcLine, replacingKey, template }) { // reg of replacingKey is already processed const escapedKey = template_1.handleSpecialCharEscapeInTemplateLValue(replacingKey); const lvalue = template.getGroupKeyForm(escapedKey); const replacingKeyReg = new RegExp(lvalue); const replacingKeyMatchingPts = matchAll_1.default(srcLine, replacingKeyReg); for (const replacingKeyMatchingPt of replacingKeyMatchingPts) { let existingMatchingPtIdx = -1; for (let matchingPtIdx = 0; matchingPtIdx < this.length; matchingPtIdx++) { const cands = this[matchingPtIdx]; const candsArr = cands[0]; const longestStrInMatchingPt = candsArr[0]; const replacingKeyMatchingStr = replacingKeyMatchingPt[0]; if (replacingKeyMatchingStr === longestStrInMatchingPt || !longestStrInMatchingPt.includes(replacingKeyMatchingStr)) { continue; } // Should be same matching point. if (longestStrInMatchingPt.length > replacingKeyMatchingPt.index - candsArr.index) { existingMatchingPtIdx = matchingPtIdx; break; } } this.replacingKey = replacingKey; if (existingMatchingPtIdx === -1) { this.push([replacingKeyMatchingPt]); } else { this[existingMatchingPtIdx].push(replacingKeyMatchingPt); } } } } exports.MatchingPoints = MatchingPoints; //# sourceMappingURL=matchingPoints.js.map