UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

103 lines (97 loc) 3.11 kB
/*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/string.ts var string_exports = {}; __export(string_exports, { camel2snake: () => camel2snake, camelize: () => camelize, capitalizeFirst: () => capitalizeFirst, cutString: () => cutString, getRandomString: () => getRandomString, hyphenate: () => hyphenate, slugify: () => slugify, snake2camel: () => snake2camel }); module.exports = __toCommonJS(string_exports); // src/cache.ts var cacheStringFunction = (fn) => { const cache = /* @__PURE__ */ Object.create(null); return (str) => { const hit = cache[str]; return hit || (cache[str] = fn(str)); }; }; // src/string.ts var camelizeRE = /-(\w)/g; var camelize = cacheStringFunction((str) => { return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : ""); }); var hyphenateRE = /\B([A-Z])/g; var hyphenate = cacheStringFunction( (str) => str.replace(hyphenateRE, "-$1").toLowerCase() ); var snake2camel = cacheStringFunction((str) => { return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase()); }); var camel2snake = cacheStringFunction((str) => { return str.replace(/[A-Z0-9]/g, (char) => `_${char.toLocaleLowerCase()}`); }); var capitalizeFirst = cacheStringFunction((value) => { return value.replace(/^./, value[0].toUpperCase()); }); function slugify(text) { if (!text) { return ""; } return text.toString().normalize("NFKD").toLowerCase().trim().replace(/\s+/g, "-").replace(/[Đđ]/g, "d").replace(/[^\w-]+/g, "").replace(/--+/g, "-"); } function getRandomString(length, alphanumeric) { let str = ""; let i = 0; const min = alphanumeric === "a" ? 10 : 0; const max = alphanumeric === "n" ? 10 : 62; while (i++ < length) { let r = Math.trunc(Math.random() * (max - min) + min); str += String.fromCodePoint(r += r > 9 ? r < 36 ? 55 : 61 : 48); } return str; } function cutString(value, limit) { if (!value && typeof value !== "string") return void 0; if (value.length === 0) return value; return value.split("").slice(0, limit).join(""); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { camel2snake, camelize, capitalizeFirst, cutString, getRandomString, hyphenate, slugify, snake2camel });