@kumix/utils 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,275 @@
1
+ # @kumix/utils
2
+
3
+ [![Version](https://img.shields.io/npm/v/@kumix/utils.svg)](https://www.npmjs.com/package/@kumix/utils)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A comprehensive library of utility functions for building modern SaaS applications. This package provides type-safe utilities for string manipulation, date/time formatting, cryptography, validation, URL handling, and more.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @kumix/utils
12
+ # or
13
+ bun add @kumix/utils
14
+ ```
15
+
16
+ ## Quick Start
17
+
18
+ ```typescript
19
+ import { cn, formatDate, validateEmail, nanoid, truncate } from "@kumix/utils";
20
+
21
+ // Tailwind-aware class merging
22
+ const className = cn("px-4 py-2", isActive && "bg-blue-500");
23
+
24
+ // Date formatting
25
+ const formatted = formatDate(new Date()); // "November 26, 2025"
26
+
27
+ // Email validation
28
+ const result = validateEmail("user@example.com");
29
+ if (result.isValid) {
30
+ console.log(result.normalized); // Normalized email
31
+ }
32
+
33
+ // Generate unique IDs
34
+ const id = nanoid(); // "a1B2c3D" (7 chars by default)
35
+
36
+ // Smart text truncation
37
+ const short = truncate("Long text here...", 20); // "Long text here..."
38
+ ```
39
+
40
+ ## Key Features
41
+
42
+ - **Type-Safe**: Full TypeScript support with comprehensive types
43
+ - **Tree-Shakeable**: Import only what you need
44
+ - **Zero Dependencies**: Most utilities have no external dependencies
45
+ - **Client & Server**: Works in both browser and Node.js environments
46
+ - **Well-Tested**: Comprehensive test coverage
47
+ - **ESM-Only**: Modern ES modules format
48
+
49
+ ## Available Utilities
50
+
51
+ ### String Manipulation
52
+
53
+ ```typescript
54
+ import {
55
+ cn, // Tailwind-aware class merging
56
+ capitalize, // Capitalize strings
57
+ toCamelCase, // Convert snake_case to camelCase
58
+ slugify, // Create URL-friendly slugs
59
+ truncate, // Truncate text
60
+ smartTruncate, // Smart truncation (URL-aware)
61
+ pluralize, // Pluralize words
62
+ getInitials, // Extract initials from names
63
+ } from "@kumix/utils";
64
+
65
+ // Examples
66
+ cn("text-sm", isActive && "font-bold"); // "text-sm font-bold"
67
+ capitalize("hello world"); // "Hello world"
68
+ toCamelCase("user_name"); // "userName"
69
+ slugify("Hello World!"); // "hello-world"
70
+ getInitials("John Doe"); // "JD"
71
+ ```
72
+
73
+ ### Date & Time
74
+
75
+ ```typescript
76
+ import {
77
+ formatDate,
78
+ formatDateTime,
79
+ formatTime,
80
+ timeAgo,
81
+ getDateTimeLocal,
82
+ } from "@kumix/utils";
83
+
84
+ // Examples
85
+ formatDate(new Date()); // "November 26, 2025"
86
+ formatDateTime(new Date()); // "Nov 26, 2025, 3:45 PM"
87
+ timeAgo(new Date(Date.now() - 3600000)); // "1 hour ago"
88
+ ```
89
+
90
+ ### Email Validation
91
+
92
+ ```typescript
93
+ import {
94
+ validateEmail,
95
+ normalizeEmail,
96
+ isDisposableEmail,
97
+ isBusinessEmail,
98
+ getEmailDomain,
99
+ } from "@kumix/utils";
100
+
101
+ // Comprehensive validation
102
+ const result = validateEmail("user+tag@gmail.com", {
103
+ allowDisposable: false,
104
+ });
105
+ // result.isValid, result.normalized, result.details
106
+
107
+ // Check disposable emails
108
+ isDisposableEmail("test@10minutemail.com"); // true
109
+
110
+ // Check business emails
111
+ isBusinessEmail("user@company.com"); // true
112
+ ```
113
+
114
+ ### Cryptography & IDs
115
+
116
+ ```typescript
117
+ import { nanoid, uid, cuid, hashStringSHA256 } from "@kumix/utils";
118
+
119
+ // Generate IDs
120
+ nanoid(); // "a1B2c3D" (7 chars, customizable)
121
+ nanoid(12); // "a1B2c3De5F6g" (12 chars)
122
+ uid(); // "1732632000123" (timestamp-based)
123
+ cuid(); // "clj5...abc" (collision-resistant)
124
+
125
+ // Hash strings (async, SHA-256 hex digest)
126
+ await hashStringSHA256("secret"); // "2bb80d537b1da3e3..."
127
+ ```
128
+
129
+ ### URL Utilities
130
+
131
+ ```typescript
132
+ import {
133
+ constructMetadata,
134
+ getUrlFromString,
135
+ isValidUrl,
136
+ getDomainWithoutWWW,
137
+ } from "@kumix/utils";
138
+
139
+ // Build metadata for SEO
140
+ const metadata = constructMetadata({
141
+ title: "My Page",
142
+ description: "Page description",
143
+ image: "/og-image.png",
144
+ });
145
+
146
+ // URL validation
147
+ isValidUrl("https://example.com"); // true
148
+ getDomainWithoutWWW("www.example.com"); // "example.com"
149
+ ```
150
+
151
+ ### File Utilities
152
+
153
+ ```typescript
154
+ import { formatFileSize } from "@kumix/utils";
155
+
156
+ formatFileSize(1024); // "1 KB"
157
+ formatFileSize(1048576); // "1 MB"
158
+ formatFileSize(1073741824); // "1 GB"
159
+ ```
160
+
161
+ ### Number Formatting
162
+
163
+ ```typescript
164
+ import { nFormatter, currencyFormatter } from "@kumix/utils";
165
+
166
+ nFormatter(1234); // "1.2K"
167
+ nFormatter(1234567); // "1.2M"
168
+ currencyFormatter(1234.56); // "$1,234.56"
169
+ ```
170
+
171
+ ### Array Utilities
172
+
173
+ ```typescript
174
+ import { chunk, randomValue, stableSort } from "@kumix/utils";
175
+
176
+ // Split array into chunks
177
+ chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
178
+
179
+ // Get random element
180
+ randomValue([1, 2, 3, 4]); // Random element
181
+
182
+ // Stable sorting
183
+ stableSort(array, (a, b) => a.name.localeCompare(b.name));
184
+ ```
185
+
186
+ ### Validation
187
+
188
+ ```typescript
189
+ import { deepEqual, keys, isIframeable } from "@kumix/utils";
190
+
191
+ // Deep object comparison
192
+ deepEqual(obj1, obj2); // true/false
193
+
194
+ // Type-safe object keys
195
+ const myKeys = keys({ a: 1, b: 2 }); // ('a' | 'b')[]
196
+
197
+ // Check if URL can be embedded
198
+ isIframeable("https://example.com"); // true/false
199
+ ```
200
+
201
+ ### Browser Utilities (Client-side only)
202
+
203
+ ```typescript
204
+ import {
205
+ setStorageItem,
206
+ getStorageItem,
207
+ setCookie,
208
+ getCookie,
209
+ getHeight,
210
+ resizeImage,
211
+ } from "@kumix/utils";
212
+
213
+ // localStorage wrapper
214
+ setStorageItem("key", { value: "data" });
215
+ const data = getStorageItem("key");
216
+
217
+ // Cookie management
218
+ setCookie("name", "value", { maxAge: 3600 });
219
+ const value = getCookie("name");
220
+
221
+ // DOM utilities
222
+ const height = getHeight(element);
223
+ const resized = await resizeImage(file, { width: 800, height: 600 });
224
+ ```
225
+
226
+ ## Configuration
227
+
228
+ ### Client vs Server
229
+
230
+ Most utilities work in both environments. For server-only utilities:
231
+
232
+ ```typescript
233
+ // Server-only exports (Node.js/Bun)
234
+ import { ... } from '@kumix/utils/server';
235
+ ```
236
+
237
+ ## API Categories
238
+
239
+ - **String**: Text manipulation, formatting, slugification
240
+ - **Date/Time**: Date formatting, time ago, period calculations
241
+ - **Crypto**: ID generation, hashing, random strings
242
+ - **Email**: Validation, normalization, disposable detection
243
+ - **URL**: URL parsing, validation, metadata construction
244
+ - **File**: File size formatting, type detection
245
+ - **Array**: Chunking, sorting, random selection
246
+ - **Validation**: Email, URL, deep equality checks
247
+ - **Browser**: Storage, cookies, DOM utilities (client-only)
248
+ - **Number**: Formatting, currency, abbreviations
249
+
250
+ ## Runtime Compatibility
251
+
252
+ | Export | Node.js | Bun | CF Workers | Deno | Browser |
253
+ | ------------------- | -------- | -------- | ---------- | -------- | --------- |
254
+ | `.` (main) | Yes | Yes | Yes | Yes | Yes |
255
+ | `./server` | Yes | Yes | No | Yes | No |
256
+ | Browser functions\* | SSR-safe | SSR-safe | SSR-safe | SSR-safe | Yes |
257
+ | Constants (env) | Filled | Filled | Undefined | Filled | Undefined |
258
+
259
+ \* `getHeight`, `resizeImage`, `resizeAndCropImage`, `loadImage`, `fileToBase64` — all have runtime guards, return `0` or throw descriptive errors when called outside browser.
260
+
261
+ ### Entry Points
262
+
263
+ - **`@kumix/utils`** — Safe everywhere. Constants use runtime-guarded `process.env` access (returns `undefined` when process is unavailable). Browser-only functions have SSR guards.
264
+ - **`@kumix/utils/server`** — Node.js / Bun / Deno only. Contains `generateRandomString` (node:crypto), JWT utilities (jsonwebtoken), password hashing (bcryptjs), and `parseDatetime` (chrono-node).
265
+
266
+ ## Links
267
+
268
+ - [npm Package](https://www.npmjs.com/package/@kumix/utils)
269
+ - [Contributing Guide](../../CONTRIBUTING.md)
270
+ - [Code of Conduct](../../CODE_OF_CONDUCT.md)
271
+ - [License](../../LICENSE)
272
+
273
+ ## License
274
+
275
+ MIT © [Kumix Labs](../../LICENSE)