ts-yutils
v1.0.0
Published
A small, growing set of type-safe TypeScript helpers.
Maintainers
Readme
YUtils (ts-yutils)
A small, growing set of type-safe TypeScript helpers.
A type-first utility toolkit for everyday TypeScript tasks
Features
- 🛡️ Type-safe: Strong generics and clear return types
- 🧩 Modular: Small, focused helpers you can pick and choose
- ⚡ Lightweight: Zero runtime dependencies
- ✨ Predictable: Immutable operations and readable naming
Installation
npm install ts-yutils
# or
yarn add ts-yutilsUsage
import YUtils from "ts-yutils";
// Deep vs shallow equality
YUtils.deepCompare({ a: [1, 2] }, { a: [1, 2] }); // true
YUtils.objectShallowCompare({ a: 1 }, { a: 1 }); // true
// Typed entries
const entries = YUtils.safeEntries({ a: 1, b: "x" });
// entries: Array<["a" | "b", number | string]>
// Naming case conversions
YUtils.setNamingCase("kebab-case", "helloWorld"); // "hello-world"
YUtils.setNamingCase("camelCase", "hello-world"); // "helloWorld"
// Stringify key/value pairs
YUtils.stringify({ color: "red", size: 12 }, ",");
// "color: red;,size: 12;"
// Hash short labels
YUtils.hashString("hello", 8); // e.g., "1y2lv0m0"Advanced example
import YUtils from "ts-yutils";
// Compare configs deeply
const cfgA = { theme: { primary: "#f00" }, flags: ["a", "b"] };
const cfgB = { theme: { primary: "#f00" }, flags: ["a", "b"] };
const isSame = YUtils.deepCompare(cfgA, cfgB); // true
// Extend object immutably and keep types
const nextCfg = YUtils.extendObject(cfgA, ["version", 2]);
// { theme: { primary: "#f00" }, flags: ["a","b"], version: 2 }
// Validate alpha channel with Regex helper
const isAlphaOk = YUtils.Regex.Validate("ALPHA_CHANNEL", 0.6); // true
// Serialize for logging with different separators
YUtils.stringify({ a: 1, b: 2 }, ","); // "a: 1;,b: 2;"
YUtils.stringify({ a: 1, b: 2 }, "white-space"); // "a: 1; b: 2;"
YUtils.stringify({ a: 1, b: 2 }, "new-line"); // "a: 1;\nb: 2;"
YUtils.stringify({ a: 1, b: 2 }); // "a: 1;b: 2;"API overview
- Equality:
deepCompare,objectDeepCompare,objectShallowCompare - Objects:
safeEntries,extendObject,containKeys - Strings:
setNamingCase,stringify,hashString - Regex:
Regex.Validate,Regex.Expressions
Types
SafeOmit<T, K extends keyof T>— Omit keys from a type with IntelliSense safetySafePick<T, K extends keyof T>— Pick keys from a type with IntelliSense safetySafeExclude<T, U extends T>— Exclude values from a unionSafeExtract<T, U extends T>— Extract values from a unionCollection<T>— Mutable array ofTReadonlyCollection<T>— Readonly array ofTDeepReadonly<T>— Recursively readonly object typeObjectKey<T extends object>— String keys ofTObjectEntry<K extends string, V>— Tuple[K, V]ObjectEntries<T extends object>— Array of[key, value]tuples fromTExtendObject<T, K extends string, V>—T & Record<K, V>NamingCase—"kebab-case" | "camelCase"StringSeparator—"," | "white-space" | "new-line" | "none"
// Examples
interface User { id: number; name: string; email: string; password: string; }
type PublicUser = SafeOmit<User, "password" | "id">; // { name; email }
type UserProfile = SafePick<User, "name" | "email">; // { name; email }
type Status = "active" | "archived" | "deleted";
type Active = SafeExclude<Status, "archived" | "deleted">; // "active"
type Permission = "read" | "write" | "admin";
type AdminOnly = SafeExtract<Permission, "admin">; // "admin"
type FrozenUser = DeepReadonly<User>;License
Apache-2.0 © YZA. See LICENSE.md.
