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.

147 lines (142 loc) 4.89 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 __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/type/type.utils.ts function isPrimitiveType(value, type) { if (type === "array") { return Array.isArray(value); } if (type === "null") { return value === null; } if (type === "undefined") { return value === void 0; } if (type === "object") { return typeof value === "object" && value !== null && !Array.isArray(value); } return typeof value === type; } __name(isPrimitiveType, "isPrimitiveType"); function getTypeOf(value) { if (value === null) return "null"; if (value === void 0) return "undefined"; if (Array.isArray(value)) return "array"; return typeof value; } __name(getTypeOf, "getTypeOf"); function isArrayOf(value, itemTypeGuard) { if (!Array.isArray(value)) return false; return value.every((item) => itemTypeGuard(item)); } __name(isArrayOf, "isArrayOf"); function toStr(value, defaultValue = "") { if (value === null || value === void 0) return defaultValue; try { if (typeof value === "object") { return JSON.stringify(value); } return String(value); } catch { return defaultValue; } } __name(toStr, "toStr"); function toNum(value, defaultValue = 0) { if (value === null || value === void 0) return defaultValue; if (typeof value === "number") return value; try { const num = Number(value); return Number.isNaN(num) ? defaultValue : num; } catch { return defaultValue; } } __name(toNum, "toNum"); function toBool(value, defaultValue = false) { if (value === null || value === void 0) return defaultValue; if (typeof value === "boolean") return value; if (typeof value === "string") { const lowercased = value.toLowerCase(); if (lowercased === "true" || lowercased === "yes" || lowercased === "y" || lowercased === "1") { return true; } if (lowercased === "false" || lowercased === "no" || lowercased === "n" || lowercased === "0") { return false; } } if (typeof value === "number") { return value !== 0; } return defaultValue; } __name(toBool, "toBool"); function ensureType(value, expectedType, defaultValue) { if (getTypeOf(value) === expectedType) { return value; } return defaultValue; } __name(ensureType, "ensureType"); function isDefined(value) { return value !== null && value !== void 0; } __name(isDefined, "isDefined"); function isEmpty(value) { if (value == null) return true; if (typeof value === "string") return value.trim().length === 0; if (Array.isArray(value)) return value.length === 0; if (value instanceof Map || value instanceof Set) return value.size === 0; if (typeof value === "object") return Object.keys(value).length === 0; return false; } __name(isEmpty, "isEmpty"); function isIterable(value) { return value !== null && value !== void 0 && typeof value[Symbol.iterator] === "function"; } __name(isIterable, "isIterable"); function isAsyncIterable(value) { return value !== null && value !== void 0 && typeof value[Symbol.asyncIterator] === "function"; } __name(isAsyncIterable, "isAsyncIterable"); function assertType(value, guard, message) { if (!guard(value)) { throw new TypeError(message || `Type assertion failed`); } } __name(assertType, "assertType"); exports.assertType = assertType; exports.ensureType = ensureType; exports.getTypeOf = getTypeOf; exports.isArrayOf = isArrayOf; exports.isAsyncIterable = isAsyncIterable; exports.isDefined = isDefined; exports.isEmpty = isEmpty; exports.isIterable = isIterable; exports.isPrimitiveType = isPrimitiveType; exports.toBool = toBool; exports.toNum = toNum; exports.toStr = toStr;