all files / dist/utils/renameDefinitionAs/ indexOfAllDefinitionsInText.js

100% Statements 62/62
100% Branches 46/46
100% Functions 14/14
100% Lines 60/60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95  33×   33× 33× 33× 33×   33× 33× 527× 42× 33× 33× 749× 749× 298×     451×   749× 22× 20×   22×     33×   64×   749× 28× 28× 28× 14×     14×       749× 749× 749× 749×   745× 503×   242×   240×     238×     749×   777× 262×     515×     831×   1304×   22×          
"use strict";
function indexOfAllDefinitionsInText(text, searchingText) {
    return new DefinitionInTextFinder(text).indexOfAll(searchingText);
}
exports.indexOfAllDefinitionsInText = indexOfAllDefinitionsInText;
var DefinitionInTextFinder = (function () {
    function DefinitionInTextFinder(text) {
        this.text = text;
        this.stringCharStack = [];
        this.currentIndex = 0;
        this.validVarNameChars = /[A-Za-z0-9_]/;
    }
    DefinitionInTextFinder.prototype.indexOfAll = function (searchingText) {
        var _this = this;
        var currentMatchIndex = 0;
        var currentCharMatches = function () { return currentMatchIndex < searchingText.length && _this.getCurrentChar() === searchingText[currentMatchIndex]; };
        var isValidFirstChar = function () { return !_this.isValidVariableNameChar(_this.getLastChar()) && _this.getLastChar() !== "."; };
        var foundIndexes = [];
        for (this.currentIndex = 0; this.currentIndex < this.text.length; this.currentIndex++) {
            this.handleStringChar();
            if (!this.isInString() && currentCharMatches() && (currentMatchIndex !== 0 || isValidFirstChar())) {
                currentMatchIndex++;
            }
            else {
                currentMatchIndex = 0;
            }
            if (currentMatchIndex === searchingText.length) {
                if (!this.isValidVariableNameChar(this.getNextChar())) {
                    foundIndexes.push((this.currentIndex + 1) - searchingText.length);
                }
                currentMatchIndex = 0;
            }
        }
        return foundIndexes;
    };
    DefinitionInTextFinder.prototype.isValidVariableNameChar = function (char) {
        return char != null && this.validVarNameChars.test(char);
    };
    DefinitionInTextFinder.prototype.handleStringChar = function () {
        if (this.isCurrentStringChar()) {
            var lastStringChar = this.getLastStringCharOnStack();
            var currentChar = this.getCurrentChar();
            if (currentChar === lastStringChar) {
                this.stringCharStack.pop();
            }
            else {
                this.stringCharStack.push(currentChar === "{" ? "}" : currentChar);
            }
        }
    };
    DefinitionInTextFinder.prototype.isCurrentStringChar = function () {
        var lastChar = this.getLastChar();
        var currentChar = this.getCurrentChar();
        var lastStringChar = this.getLastStringCharOnStack();
        if (lastChar === "\\") {
            return false;
        }
        else if (lastStringChar == null) {
            return currentChar === "`" || currentChar === "'" || currentChar === "\"";
        }
        else if (lastStringChar === "`" && lastChar === "$" && currentChar === "{") {
            return true;
        }
        else if (lastStringChar === "}" && currentChar === "}") {
            return true;
        }
        else {
            return currentChar === lastStringChar;
        }
    };
    DefinitionInTextFinder.prototype.isInString = function () {
        return this.stringCharStack.length > 0 && this.stringCharStack[this.stringCharStack.length - 1] !== "}";
    };
    DefinitionInTextFinder.prototype.getLastStringCharOnStack = function () {
        if (this.stringCharStack.length > 0) {
            return this.stringCharStack[this.stringCharStack.length - 1];
        }
        else {
            return null;
        }
    };
    DefinitionInTextFinder.prototype.getLastChar = function () {
        return this.currentIndex - 1 < 0 ? null : this.text[this.currentIndex - 1];
    };
    DefinitionInTextFinder.prototype.getCurrentChar = function () {
        return this.text[this.currentIndex];
    };
    DefinitionInTextFinder.prototype.getNextChar = function () {
        return this.currentIndex + 1 >= this.text.length ? null : this.text[this.currentIndex + 1];
    };
    return DefinitionInTextFinder;
}());
 
//# sourceMappingURL=indexOfAllDefinitionsInText.js.map