augmented-string
Version:
Extends String methods adding: - lcFirst(): string; - ucFirst(): string; - ucWords(): string; - toFloat(): number; - toInt(): number; - toCamel(): string; - toKebab(): string; - toPascal(): string; - toSnake(): string; - repeat(count): string;
38 lines (37 loc) • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
String.prototype.lcFirst = function () {
return String(this)[0].toLowerCase() + String(this).slice(1);
};
String.prototype.ucFirst = function () {
return String(this)[0].toUpperCase() + String(this).slice(1);
};
String.prototype.ucWords = function () {
return String(this)
.replace(/^([a-z])|\s+([a-z])/g, $1 => $1.toUpperCase());
};
String.prototype.toInt = function () {
return parseInt(String(this), 10);
};
String.prototype.toFloat = function () {
return parseFloat(String(this));
};
String.prototype.toCamel = function () {
return String(this)
.replace(/[-_]/g, ' ')
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => index ? word.toUpperCase() : word.toLowerCase())
.replace(/\s+/g, '');
};
String.prototype.toPascal = function () {
return String(this)
.replace(/[-_]/g, ' ')
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => ' ' + word)
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase())
.replace(/\s/g, '');
};
String.prototype.toSnake = function () {
return String(this)
.replace(/[-\ ]/g, '_')
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => (index ? '_' : '') + word.toLowerCase())
.replace(/_+/g, '_');
};