type-dexie
Version:
class oriented schema building tool for dexie.js
48 lines • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var must_be_camel_cased_error_1 = require("../error/must-be-camel-cased.error");
var Naming = /** @class */ (function () {
function Naming() {
}
Naming.prototype.camelCase = function (target) {
if (/^[A-Z]+$/.test(target.name)) {
return target.name.toLowerCase();
}
else {
return target.name.replace(/^[A-Z]/g, function (m) { return m.toLowerCase(); });
}
};
Naming.prototype.pluralize = function (input) {
for (var _i = 0, _a = Naming.PLURALIZE_RULES; _i < _a.length; _i++) {
var rule = _a[_i];
if (rule.pattern.test(input)) {
return input.replace(rule.pattern, rule.replace);
}
}
return input;
};
Naming.prototype.table = function (target) {
var camelCased = this.camelCase(target);
var lastWord = camelCased.match(Naming.LAST_WORD);
if (null == lastWord) {
throw new must_be_camel_cased_error_1.MustBeCamelCasedError();
}
return camelCased.replace(Naming.LAST_WORD, this.pluralize(lastWord[0]));
};
Naming.prototype.prop = function (target, propertyKey) {
return (target.name +
('symbol' === typeof propertyKey
? '[' + propertyKey.toString() + ']'
: '.' + propertyKey));
};
Naming.LAST_WORD = /([a-z]+|[A-Z][a-z]*)$/;
Naming.PLURALIZE_RULES = [
{ pattern: /(?<=s)$/i, replace: 'ses' },
{ pattern: /y$/i, replace: 'ies' },
{ pattern: /$/i, replace: 's' },
];
return Naming;
}());
var naming = new Naming();
exports.default = naming;
//# sourceMappingURL=naming.js.map