UNPKG

@catbee/utils

Version:

A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.

63 lines (58 loc) 2.32 kB
/* * The MIT License * * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ 'use strict'; var crypto = require('crypto'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); function uuid() { return crypto.randomUUID(); } __name(uuid, "uuid"); function nanoId(length = 21) { if (length <= 0) return ""; let id = ""; while (id.length < length) { id += crypto.randomBytes(Math.ceil(length * 3 / 4)).toString("base64url").replace(/[+/=]/g, ""); } return id.slice(0, length); } __name(nanoId, "nanoId"); function randomHex(byteLength = 16) { return crypto.randomBytes(byteLength).toString("hex").padStart(byteLength * 2, "0").slice(0, byteLength * 2); } __name(randomHex, "randomHex"); function randomInt(min, max) { const range = max - min + 1; return Math.floor(Math.random() * range) + min; } __name(randomInt, "randomInt"); function randomBase64(byteLength = 16) { return crypto.randomBytes(byteLength).toString("base64url"); } __name(randomBase64, "randomBase64"); exports.nanoId = nanoId; exports.randomBase64 = randomBase64; exports.randomHex = randomHex; exports.randomInt = randomInt; exports.uuid = uuid;