ntts
Version:
A CLI tool for refactoring an existing NodeJs application to a fully functional TypeScript application.
48 lines (47 loc) • 1.86 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var reserved_keywords_1 = __importDefault(require("../../../assets/reserved-keywords"));
var VariableNameGenerator = /** @class */ (function () {
function VariableNameGenerator() {
}
var _a;
_a = VariableNameGenerator;
VariableNameGenerator.variableNameFromImportId = function (importId) {
var splitPath = importId.split('/');
var newName = splitPath[splitPath.length - 1]
.replace(/\.ts$/, '')
.replace(/[^_\d\w$]/g, '_')
.replace(/^_+/, '')
.replace(/_+$/, '');
if (newName.match(/^\d.*$/)) {
return "_".concat(newName);
}
if (!newName) {
return '_';
}
return newName;
};
VariableNameGenerator.getUsableVariableName = function (name, usedNames) {
if (!usedNames.includes(name) && !reserved_keywords_1.default.includes(name)) {
return name;
}
var underscoreName = "_".concat(name);
if (!usedNames.includes(underscoreName) && !reserved_keywords_1.default.includes(underscoreName)) {
return underscoreName;
}
return _a.iterateVariableNames(name, 0, usedNames);
};
VariableNameGenerator.iterateVariableNames = function (name, counter, usedNames) {
var newName = name + counter;
if (usedNames.includes(newName) || reserved_keywords_1.default.includes(newName)) {
var newCounter = counter + 1;
return _a.iterateVariableNames(name, newCounter, usedNames);
}
return newName;
};
return VariableNameGenerator;
}());
exports.default = VariableNameGenerator;