zilla-util 1.2.27 → 1.2.28

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.
Files changed (2) hide show
  1. package/README.md +172 -0
  2. package/package.json +6 -2
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ # zilla-util
2
+
3
+ Simple utility library for TypeScript and modern JavaScript projects.
4
+
5
+ `zilla-util` provides small helpers for arrays, strings, dates, object traversal, retries, caches, hashing, URL cleanup, WebCrypto operations, and related application plumbing. The package is ESM-first and exports TypeScript declarations from its built output.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install zilla-util
11
+ ```
12
+
13
+ ```sh
14
+ pnpm add zilla-util
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Import utilities from the package root:
20
+
21
+ ```ts
22
+ import { retry, sleep, sluggize, LRUCache, deepGet } from "zilla-util";
23
+
24
+ const slug = sluggize("Hello, World!", "-");
25
+
26
+ const value = deepGet<string>(
27
+ {
28
+ user: {
29
+ profile: {
30
+ name: "Ada",
31
+ },
32
+ },
33
+ },
34
+ "user.profile.name"
35
+ );
36
+
37
+ const cache = new LRUCache<string, number>({ maxSize: 100, maxAge: 60_000 });
38
+ cache.set("answer", 42);
39
+
40
+ await retry(async () => {
41
+ await sleep(100);
42
+ return "ok";
43
+ });
44
+ ```
45
+
46
+ The package also exposes a subpath export for array helpers:
47
+
48
+ ```ts
49
+ import { containsAll } from "zilla-util/array";
50
+ ```
51
+
52
+ ## What's Included
53
+
54
+ ### Arrays
55
+
56
+ - Set and inclusion helpers: `setsEqual`, `containsAll`, `isAnyTrue`
57
+ - Collection helpers: `cartesianProduct`, `asyncFilter`, `shuffleArray`, `shuffleNumbers`
58
+ - Deduplication helpers: `deduplicateArray`, `deduplicateArrayByName`, `firstByProperty`
59
+ - Sorted set classes: `SortedSet`, `SortedIdSet`
60
+
61
+ ### Strings and Formatting
62
+
63
+ - Case and path helpers: `capitalize`, `uncapitalize`, `basefilename`, `ext`, `basefilenameWithoutExt`
64
+ - Slug and case conversion helpers: `sluggize`, `hyphenate`, `camel2kebab`, `camel2snake`, `kebab2camel`, `snake2camel`
65
+ - Date string helpers: `dateAsYYYYMMDD`, `dateAsUTCYYYYMMDD`, `nowAsYYYYMMDD`, `dateAsYYYYMMDDHHmmSS`
66
+ - Token and ID helpers: `uuidv4`, `randomSafeToken`, `randomSuperSafeToken`, `randomDigits`
67
+ - JSON and text helpers: `safeStringify`, `sortedStringify`, `trimSpaces`, `countVisibleChars`
68
+ - ANSI and browser CSS style constants: `ANSI`, `CSS`
69
+
70
+ ### Dates and Time
71
+
72
+ - UTC date formatter: `formatDate`
73
+ - Clock abstraction: `ZillaClock`, `DEFAULT_CLOCK`, `MockClock`, `mockClock`
74
+ - Async timing helpers: `sleep`, `nap`, `delay`
75
+ - Duration parsing: `parseSimpleTime`, with values such as `500`, `2s`, `5m`, `1h`, and `1d`
76
+
77
+ ### Deep Objects
78
+
79
+ - Path parsing and traversal: `parseDeep`, `deepGet`, `deepUpdate`
80
+ - Comparison helpers: `deepEquals`, `deepAtLeastEquals`, `deepEqualsForFields`
81
+ - Object cleanup and filtering: `stripNonAlphaNumericKeys`, `filterObject`, `filterProperties`
82
+ - Empty checks: `isEmpty`, `isNotEmpty`
83
+ - Immutable proxy wrapper: `immutify`
84
+ - Recursive transforms: `copyWithRegExp`, `deepTransform`
85
+
86
+ Deep paths support dot notation and array indexes:
87
+
88
+ ```ts
89
+ import { deepGet, deepUpdate } from "zilla-util";
90
+
91
+ const obj = { users: [{ name: "Ada" }] };
92
+
93
+ deepGet<string>(obj, "users[0].name"); // "Ada"
94
+ deepUpdate(obj, "users[].name", "Grace"); // appends an object to users
95
+ ```
96
+
97
+ ### Cache and Retry
98
+
99
+ - `LRUCache` provides bounded in-memory caching with optional max age and touch-on-get behavior.
100
+ - `withLRUCache` memoizes sync or async functions.
101
+ - `retry` retries async functions with configurable attempts, exponential backoff, and retry filtering.
102
+
103
+ ```ts
104
+ import { retry, withLRUCache } from "zilla-util";
105
+
106
+ const cachedLookup = withLRUCache(async (id: string) => {
107
+ return fetch(`/api/items/${id}`).then((r) => r.json());
108
+ });
109
+
110
+ const result = await retry(() => cachedLookup("abc"), {
111
+ maxAttempts: 5,
112
+ backoffBaseMillis: 250,
113
+ backoffMultiplier: 2,
114
+ });
115
+ ```
116
+
117
+ ### Encoding, Hashing, and Crypto
118
+
119
+ - URL-safe base64 helpers: `base64Encode`, `base64Decode`
120
+ - SHA-256 helpers: `sha256`, `shaLevels`, `dirLevels`
121
+ - WebCrypto RSA helpers: `CryptoUtil.generateKeyPair`, `CryptoUtil.encrypt`, `CryptoUtil.decrypt`, `CryptoUtil.sign`, `CryptoUtil.verifySignature`
122
+
123
+ `CryptoUtil` uses `globalThis.crypto.subtle`, so it requires a WebCrypto-capable runtime.
124
+
125
+ ### URLs
126
+
127
+ - `normalizeUrl` normalizes valid URL strings with the platform `URL` implementation.
128
+ - `canonicalizeUrl` lowercases protocol/host, removes common tracking parameters, follows redirects, and inspects HTML canonical metadata when available.
129
+
130
+ ### Other Utilities
131
+
132
+ - `substContext` replaces `{{variable}}` placeholders in strings, arrays, and objects.
133
+ - `wrapError` wraps unknown thrown values with contextual `Error` objects.
134
+ - `GenericLogger` and `DEFAULT_LOGGER` define a small logger interface.
135
+ - Message transport types are available through `ZillaMsgTransportType`, `ZillaMessage`, and `ZillaMsgTransport`.
136
+ - `packageVersion` attempts to discover the nearest `package.json` version from an import URL.
137
+
138
+ ## Development
139
+
140
+ Install dependencies:
141
+
142
+ ```sh
143
+ pnpm install
144
+ ```
145
+
146
+ Build the package:
147
+
148
+ ```sh
149
+ pnpm build
150
+ ```
151
+
152
+ Run tests:
153
+
154
+ ```sh
155
+ pnpm test
156
+ ```
157
+
158
+ Run linting:
159
+
160
+ ```sh
161
+ pnpm lint
162
+ ```
163
+
164
+ Run the full release check:
165
+
166
+ ```sh
167
+ pnpm release
168
+ ```
169
+
170
+ ## License
171
+
172
+ Apache-2.0. See [LICENSE.txt](LICENSE.txt).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zilla-util",
3
- "version": "1.2.27",
3
+ "version": "1.2.28",
4
4
  "type": "module",
5
5
  "description": "Simple zero-dependency utility library",
6
6
  "keywords": [
@@ -34,7 +34,11 @@
34
34
  "main": "./lib/esm/index.js",
35
35
  "module": "./lib/esm/index.js",
36
36
  "exports": {
37
- ".": "./lib/esm/index.js"
37
+ ".": "./lib/esm/index.js",
38
+ "./array": {
39
+ "types": "./lib/esm/array.d.ts",
40
+ "default": "./lib/esm/array.js"
41
+ }
38
42
  },
39
43
  "files": [
40
44
  "lib/",