camote-utils
Version:
A comprehensive TypeScript utility library featuring advanced string and number formatting, data structures, and algorithms
390 lines (389 loc) • 12.9 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.checks = exports.formatters = exports._ = void 0;
const numberFormatters = __importStar(require("../formatters/number"));
const stringFormatters = __importStar(require("../formatters/string"));
const dateFormatters = __importStar(require("../formatters/date"));
const arrayFormatters = __importStar(require("../formatters/array"));
const objectFormatters = __importStar(require("../formatters/object"));
const deepFormatters = __importStar(require("../formatters/deep"));
const checkers = __importStar(require("../checkers"));
const random_1 = require("../random");
class _ {
constructor(value) {
this.value = value;
}
// Get the final value
valueOf() {
return this.value;
}
// Method to get the current value
getValue() {
return this.value;
}
removeDuplicates() {
this.value = arrayFormatters.removeDuplicates(this.value);
return this;
}
flattenArray() {
this.value = arrayFormatters.flattenArray(this.value);
return this;
}
filterArray(conditionFn) {
this.value = arrayFormatters.filterArray(this.value, conditionFn);
return this;
}
transformArray(transformFn) {
this.value = arrayFormatters.transformArray(this.value, transformFn);
return this;
}
implode(arr, delimiter) {
this.value = arrayFormatters.implode(arr, delimiter);
return this;
}
capitalizeEach() {
this.value = arrayFormatters.capitalizeEach(this.value);
return this;
}
removeEmptyKeysEntries() {
this.value = objectFormatters.removeEmptyKeysEntries(this.value);
return this;
}
deepClone() {
this.value = deepFormatters.deepClone(this.value);
return this;
}
deepSortAlphabetical() {
this.value = deepFormatters.deepSortAlphabetical(this.value);
return this;
}
deepCompareObjects(anotherValue) {
this.value = deepFormatters.deepCompareObjects(this.value, anotherValue);
return this;
}
deepCompare(anotherValue) {
this.value = deepFormatters.deepCompare(this.value, anotherValue);
return this;
}
deepMerge(anotherValue) {
this.value = deepFormatters.deepMerge(this.value, anotherValue);
return this;
}
objectToQueryString() {
this.value = objectFormatters.objectToQueryString(this.value);
return this;
}
objectFilterByKeys(keys) {
this.value = objectFormatters.objectFilterByKeys(this.value, keys);
return this;
}
// String operations
capitalize() {
this.value = stringFormatters.capitalize(String(this.value));
return this;
}
slugifyRevert() {
this.value = stringFormatters.slugifyRevert(String(this.value));
return this;
}
slugify() {
this.value = stringFormatters.slugify(String(this.value));
return this;
}
truncate(length, ellipsis) {
this.value = stringFormatters.truncate(String(this.value), length, ellipsis);
return this;
}
toCamelCase() {
this.value = stringFormatters.toCamelCase(String(this.value));
return this;
}
toKebabCase() {
this.value = stringFormatters.toKebabCase(String(this.value));
return this;
}
mask(str, maskChar = '*', visibleCount = 4, position = 'end', active = true) {
return stringFormatters.mask(str, maskChar, visibleCount, position, active);
}
explode(delimiter, limit) {
this.value = stringFormatters.explode(String(this.value), delimiter, limit);
return this;
}
toUnicodes(exclude = "") {
this.value = stringFormatters.toUnicodes(String(this.value), exclude);
return this;
}
toHTMLEntities(exclude = "") {
this.value = stringFormatters.toHtmlEntities(String(this.value), exclude);
return this;
}
swapCase() {
this.value = stringFormatters.swapCase(String(this.value));
return this;
}
// Date operations
isDateWithinRange(endDate) {
this.value = dateFormatters.isDateWithinRange(this.value, endDate);
return this;
}
// Array operations
map(fn) {
if (!Array.isArray(this.value)) {
throw new Error('Map can only be called on arrays');
}
this.value = this.value.map(fn);
return this;
}
filter(fn) {
if (!Array.isArray(this.value)) {
throw new Error('Filter can only be called on arrays');
}
this.value = this.value.filter(fn);
return this;
}
capitalizeWords() {
this.value = stringFormatters.capitalizeWords(String(this.value));
return this;
}
trim() {
this.value = stringFormatters.trim(String(this.value));
return this;
}
pluralize(count, customPlural) {
this.value = stringFormatters.pluralize(String(this.value), count, customPlural);
return this;
}
// Number operations
formatCurrency(currency, locale) {
this.value = numberFormatters.formatCurrency(Number(this.value), currency, locale);
return this;
}
formatWithCommas() {
this.value = numberFormatters.formatWithCommas(Number(this.value));
return this;
}
formatPercentage(decimals) {
this.value = numberFormatters.formatPercentage(Number(this.value), decimals);
return this;
}
// Static methods
static chain(value) {
return new _(value);
}
// Static checker methods
static isNil(value) {
return checkers.isNil(value);
}
static isEmpty(value) {
return checkers.isEmpty(value);
}
static isNumber(value) {
return checkers.isNumber(value);
}
static isString(value) {
return checkers.isString(value);
}
static isArray(value) {
return checkers.isArray(value);
}
static isObject(value) {
return checkers.isObject(value);
}
static isUrl(value) {
return checkers.isUrl(value);
}
static isUuid(value) {
return checkers.isUuid(value);
}
static isAlphanumeric(value) {
return checkers.isAlphanumeric(value);
}
static isEmail(value) {
return checkers.isEmail(value);
}
static isStrongPassword(value) {
return checkers.isStrongPassword(value);
}
static isValidTime(value) {
return checkers.isValidTime(value);
}
static isFunction(value) {
return checkers.isFunction(value);
}
static isDateWithinRange(startDate, endDate) {
return dateFormatters.isDateWithinRange(startDate, endDate);
}
static isBoolean(str) {
return checkers.isBoolean(str);
}
static isDataView(str) {
return checkers.isDataView(str);
}
static isNaN(str) {
return checkers.isNaN(str);
}
static isUndefined(str) {
return checkers.isUndefined(str);
}
static isFinite(str) {
return checkers.isFinite(str);
}
static isNull(str) {
return checkers.isNull(str);
}
// Number formatters
static formatCurrency(value, currency, locale) {
return numberFormatters.formatCurrency(value, currency, locale);
}
static formatWithCommas(value) {
return numberFormatters.formatWithCommas(value);
}
static formatPercentage(value, decimals) {
return numberFormatters.formatPercentage(value, decimals);
}
static formatDecimals(value, decimals) {
return numberFormatters.formatDecimals(value, decimals);
}
static formatOrdinal(value) {
return numberFormatters.formatOrdinal(value);
}
static calculateDiscountPrice(price, discount) {
return numberFormatters.calculateDiscountPrice(price, discount);
}
// String formatters
static capitalize(str) {
return stringFormatters.capitalize(str);
}
static truncate(str, length, ellipsis) {
return stringFormatters.truncate(str, length, ellipsis);
}
static toCamelCase(str) {
return stringFormatters.toCamelCase(str);
}
static toKebabCase(str) {
return stringFormatters.toKebabCase(str);
}
static pad(str, length, char = ' ', position = 'end') {
return stringFormatters.pad(str, length, char, position);
}
static mask(str, maskChar = '*', visibleCount = 4, position = 'end', active = true) {
return stringFormatters.mask(str, maskChar, visibleCount, position, active);
}
static toLowerCase(str, substr) {
return stringFormatters.toLowerCase(str, substr);
}
static toUpperCase(str, substr) {
return stringFormatters.toUpperCase(str, substr);
}
static chopEnd(str, count = 1) {
return stringFormatters.chopEnd(str, count);
}
static chopStart(str, count = 1) {
return stringFormatters.chopStart(str, count);
}
static toHtmlEntities(str, exclude = "") {
return stringFormatters.toHtmlEntities(str, exclude);
}
static toUnicodes(str, exclude = "") {
return stringFormatters.toUnicodes(str, exclude);
}
static swapCase(str) {
return stringFormatters.swapCase(str);
}
// Random generation functions
static generateRandomInteger(min, max) {
return (0, random_1.generateRandomInteger)(min, max);
}
static generateRandomIntegerArray(length, min, max) {
return (0, random_1.generateRandomIntegerArray)(length, min, max);
}
static generateRandomIntegerExcluding(min, max, exclude) {
return (0, random_1.generateRandomIntegerExcluding)(min, max, exclude);
}
static generateRandomString(length, options) {
return (0, random_1.generateRandomString)(length, options);
}
static generateRandomPassword(length, options) {
return (0, random_1.generateRandomPassword)(length, options);
}
static generateRandomHexColor(includeHash) {
return (0, random_1.generateRandomHexColor)(includeHash);
}
static generateRandom(options) {
return (0, random_1.generateRandom)(options);
}
static generateRandomHex(includeHash = true) {
return (0, random_1.generateRandomHexColor)(includeHash);
}
static generateRandomColor(format) {
return (0, random_1.generateRandomColor)(format);
}
static generateColorPalette(numColors, format) {
return (0, random_1.generateColorPalette)(numColors, format);
}
static generateRandomRGB(includeAlpha = false) {
return (0, random_1.generateRandomRGB)(includeAlpha);
}
static generateRandomHSL(includeAlpha = false) {
return (0, random_1.generateRandomHSL)(includeAlpha);
}
static generateUUID() {
return (0, random_1.generateUUID)();
}
static generateUUIDv4() {
return (0, random_1.generateUUIDv4)();
}
static humanReadableNumber(value, options) {
const instance = new _(value);
instance.value = numberFormatters.humanReadableNumber(value, options);
return instance;
}
}
exports._ = _;
_.generateStrongPassword = (length) => {
return (0, random_1.generateStrongPassword)(length);
};
exports.formatters = {
...numberFormatters,
...stringFormatters,
...dateFormatters,
...arrayFormatters,
...objectFormatters,
...deepFormatters
};
exports.checks = {
...checkers
};