@demicodes/utils 0.10.3 → 0.11.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/dist/index.d.mts +13 -1
- package/dist/index.mjs +29 -2
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -34,6 +34,8 @@ declare function throwIfAborted(signal: AbortSignal): void;
|
|
|
34
34
|
* promise is not canceled; this only stops the caller from awaiting it past the abort.
|
|
35
35
|
*/
|
|
36
36
|
declare function abortable<T>(promise: Promise<T>, signal: AbortSignal): Promise<T>;
|
|
37
|
+
/** Returns the string `code` of an errno-style error (`ENOENT`, `EEXIST`, …), or null. */
|
|
38
|
+
declare function errorCode(error: unknown): string | null;
|
|
37
39
|
/** Reports whether an unknown thrown value is a file-not-found error (ENOENT). */
|
|
38
40
|
declare function isFileNotFoundError(error: unknown): boolean;
|
|
39
41
|
//#endregion
|
|
@@ -68,6 +70,12 @@ declare function encodeUtf8(text: string): Uint8Array;
|
|
|
68
70
|
declare function decodeUtf8(data: Uint8Array): string;
|
|
69
71
|
/** Returns the UTF-8 byte length of a string. */
|
|
70
72
|
declare function utf8Bytes(text: string): number;
|
|
73
|
+
/** Packs a latin1 byte-string (each char = one byte, 0–255) into bytes. */
|
|
74
|
+
declare function encodeLatin1(text: string): Uint8Array;
|
|
75
|
+
/** Unpacks bytes into a latin1 byte-string (each char = one byte). */
|
|
76
|
+
declare function decodeLatin1(bytes: Uint8Array): string;
|
|
77
|
+
/** Strictly decodes UTF-8; returns null when the bytes are not valid UTF-8. */
|
|
78
|
+
declare function decodeUtf8Strict(bytes: Uint8Array): string | null;
|
|
71
79
|
/** Slices a string by UTF-8 byte offsets, returning the decoded substring. */
|
|
72
80
|
declare function utf8Slice(text: string, start: number, end: number): string;
|
|
73
81
|
/** Concatenates byte chunks into a single `Uint8Array`. */
|
|
@@ -90,6 +98,10 @@ declare function tail(text: string, maxChars: number): string;
|
|
|
90
98
|
declare function shortHash(value: string): string;
|
|
91
99
|
//#endregion
|
|
92
100
|
//#region src/json.d.ts
|
|
101
|
+
/** Values preserved by Demi's transport-neutral portable JSON codec. */
|
|
102
|
+
type PortableJsonValue = null | boolean | number | string | bigint | Uint8Array | readonly PortableJsonValue[] | {
|
|
103
|
+
readonly [key: string]: PortableJsonValue;
|
|
104
|
+
};
|
|
93
105
|
/** Parses JSON, returning the value, or the original string if it is not valid JSON. */
|
|
94
106
|
declare function parseJsonOrString(value: string): unknown;
|
|
95
107
|
/** Parses JSON and returns it only when it is a plain object; otherwise `null`. */
|
|
@@ -125,4 +137,4 @@ declare function isAbsolutePath(path: string): boolean;
|
|
|
125
137
|
/** Generates a random UUID. */
|
|
126
138
|
declare function createId(): string;
|
|
127
139
|
//#endregion
|
|
128
|
-
export { AbortError, Deferred, abortable, asError, asRecord, asString, base64ToBytes, bytesToBase64, clamp, concatBytes, createId, decodeUtf8, deferred, delay, dirnamePath, encodeUtf8, errorMessage, isAbortError, isAbsolutePath, isFileNotFoundError, isRecord, nonEmptyString, noop, normalizeBaseUrl, normalizePath, numberOrNull, numberOrZero, parseJsonObject, parseJsonOrString, parsePortableJson, safeJsonStringify, shortHash, stringOrNull, stringifyPortableJson, tail, throwIfAborted, truncate, utf8Bytes, utf8Slice, waitFor, withTimeout };
|
|
140
|
+
export { AbortError, Deferred, PortableJsonValue, abortable, asError, asRecord, asString, base64ToBytes, bytesToBase64, clamp, concatBytes, createId, decodeLatin1, decodeUtf8, decodeUtf8Strict, deferred, delay, dirnamePath, encodeLatin1, encodeUtf8, errorCode, errorMessage, isAbortError, isAbsolutePath, isFileNotFoundError, isRecord, nonEmptyString, noop, normalizeBaseUrl, normalizePath, numberOrNull, numberOrZero, parseJsonObject, parseJsonOrString, parsePortableJson, safeJsonStringify, shortHash, stringOrNull, stringifyPortableJson, tail, throwIfAborted, truncate, utf8Bytes, utf8Slice, waitFor, withTimeout };
|
package/dist/index.mjs
CHANGED
|
@@ -84,9 +84,15 @@ function abortable(promise, signal) {
|
|
|
84
84
|
});
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
|
+
/** Returns the string `code` of an errno-style error (`ENOENT`, `EEXIST`, …), or null. */
|
|
88
|
+
function errorCode(error) {
|
|
89
|
+
if (typeof error !== "object" || error === null) return null;
|
|
90
|
+
const code = error.code;
|
|
91
|
+
return typeof code === "string" ? code : null;
|
|
92
|
+
}
|
|
87
93
|
/** Reports whether an unknown thrown value is a file-not-found error (ENOENT). */
|
|
88
94
|
function isFileNotFoundError(error) {
|
|
89
|
-
return
|
|
95
|
+
return errorCode(error) === "ENOENT";
|
|
90
96
|
}
|
|
91
97
|
//#endregion
|
|
92
98
|
//#region src/async.ts
|
|
@@ -154,6 +160,27 @@ function decodeUtf8(data) {
|
|
|
154
160
|
function utf8Bytes(text) {
|
|
155
161
|
return encoder.encode(text).byteLength;
|
|
156
162
|
}
|
|
163
|
+
/** Packs a latin1 byte-string (each char = one byte, 0–255) into bytes. */
|
|
164
|
+
function encodeLatin1(text) {
|
|
165
|
+
const out = new Uint8Array(text.length);
|
|
166
|
+
for (let i = 0; i < text.length; i += 1) out[i] = text.charCodeAt(i) & 255;
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
/** Unpacks bytes into a latin1 byte-string (each char = one byte). */
|
|
170
|
+
function decodeLatin1(bytes) {
|
|
171
|
+
let out = "";
|
|
172
|
+
const chunk = 32768;
|
|
173
|
+
for (let i = 0; i < bytes.length; i += chunk) out += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
/** Strictly decodes UTF-8; returns null when the bytes are not valid UTF-8. */
|
|
177
|
+
function decodeUtf8Strict(bytes) {
|
|
178
|
+
try {
|
|
179
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
180
|
+
} catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
157
184
|
/** Slices a string by UTF-8 byte offsets, returning the decoded substring. */
|
|
158
185
|
function utf8Slice(text, start, end) {
|
|
159
186
|
if (start <= 0 && end >= utf8Bytes(text)) return text;
|
|
@@ -362,4 +389,4 @@ function createId() {
|
|
|
362
389
|
return globalThis.crypto.randomUUID();
|
|
363
390
|
}
|
|
364
391
|
//#endregion
|
|
365
|
-
export { AbortError, abortable, asError, asRecord, asString, base64ToBytes, bytesToBase64, clamp, concatBytes, createId, decodeUtf8, deferred, delay, dirnamePath, encodeUtf8, errorMessage, isAbortError, isAbsolutePath, isFileNotFoundError, isRecord, nonEmptyString, noop, normalizeBaseUrl, normalizePath, numberOrNull, numberOrZero, parseJsonObject, parseJsonOrString, parsePortableJson, safeJsonStringify, shortHash, stringOrNull, stringifyPortableJson, tail, throwIfAborted, truncate, utf8Bytes, utf8Slice, waitFor, withTimeout };
|
|
392
|
+
export { AbortError, abortable, asError, asRecord, asString, base64ToBytes, bytesToBase64, clamp, concatBytes, createId, decodeLatin1, decodeUtf8, decodeUtf8Strict, deferred, delay, dirnamePath, encodeLatin1, encodeUtf8, errorCode, errorMessage, isAbortError, isAbsolutePath, isFileNotFoundError, isRecord, nonEmptyString, noop, normalizeBaseUrl, normalizePath, numberOrNull, numberOrZero, parseJsonObject, parseJsonOrString, parsePortableJson, safeJsonStringify, shortHash, stringOrNull, stringifyPortableJson, tail, throwIfAborted, truncate, utf8Bytes, utf8Slice, waitFor, withTimeout };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@demicodes/utils",
|
|
3
3
|
"description": "Shared zero-dependency utilities for Demi (type guards, errors, async, bytes, strings, paths, JSON).",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.11.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.mjs",
|