@wener/utils 1.1.53 → 1.1.54
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/lib/errors/Errors.js +201 -2
- package/lib/index.js +9 -8
- package/lib/io/ArrayBuffers.js +559 -2
- package/lib/langs/parseDate.js +20 -0
- package/package.json +2 -2
- package/src/errors/Errors.ts +106 -1
- package/src/index.ts +10 -8
- package/src/io/ArrayBuffers.ts +676 -1
- package/src/langs/parseDate.ts +13 -0
- package/tsconfig.json +4 -13
- package/lib/errors/Errors.mod.js +0 -206
- package/lib/io/ArrayBuffers.mod.js +0 -531
- package/src/errors/Errors.mod.ts +0 -104
- package/src/io/ArrayBuffers.mod.ts +0 -670
package/lib/io/ArrayBuffers.js
CHANGED
|
@@ -1,2 +1,559 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
function _instanceof(left, right) {
|
|
2
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
3
|
+
return !!right[Symbol.hasInstance](left);
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
return left instanceof right;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
import { classOf } from "../langs/classOf.js";
|
|
10
|
+
import { getGlobalThis } from "../web/getGlobalThis.js";
|
|
11
|
+
import { decodeBase64ToUint8Array, encodeArrayBufferToBase64 } from "./base64.js";
|
|
12
|
+
import { isBuffer } from "./isBuffer.js";
|
|
13
|
+
(function (ArrayBuffers) {
|
|
14
|
+
var nativeBufferAllowed = true;
|
|
15
|
+
var isBufferAvailable;
|
|
16
|
+
var textEncoder;
|
|
17
|
+
var textDecoder;
|
|
18
|
+
function decode(v) {
|
|
19
|
+
// need icu full data
|
|
20
|
+
// if (encoding) return new TextDecoder(encoding).decode(v);
|
|
21
|
+
return (textDecoder || (textDecoder = new TextDecoder())).decode(v);
|
|
22
|
+
}
|
|
23
|
+
function encode(v) {
|
|
24
|
+
if (typeof v === "string") {
|
|
25
|
+
return (textEncoder || (textEncoder = new TextEncoder())).encode(v);
|
|
26
|
+
}
|
|
27
|
+
return v;
|
|
28
|
+
}
|
|
29
|
+
function isNativeBufferAvailable() {
|
|
30
|
+
var _getGlobalThis_Buffer_isPollyfill, _getGlobalThis_Buffer;
|
|
31
|
+
// eslint-disable-next-line no-return-assign
|
|
32
|
+
return isBufferAvailable !== null && isBufferAvailable !== void 0 ? isBufferAvailable : isBufferAvailable = !((_getGlobalThis_Buffer = getGlobalThis().Buffer) === null || _getGlobalThis_Buffer === void 0 ? void 0 : (_getGlobalThis_Buffer_isPollyfill = _getGlobalThis_Buffer.isPollyfill) === null || _getGlobalThis_Buffer_isPollyfill === void 0 ? void 0 : _getGlobalThis_Buffer_isPollyfill.call(_getGlobalThis_Buffer));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* isNativeBufferAvailable check if the native {@link Buffer} is available
|
|
36
|
+
*/ ArrayBuffers.isNativeBufferAvailable = isNativeBufferAvailable;
|
|
37
|
+
function isNativeBufferAllowed() {
|
|
38
|
+
return Boolean(nativeBufferAllowed && isBufferAvailable);
|
|
39
|
+
}
|
|
40
|
+
ArrayBuffers.isNativeBufferAllowed = isNativeBufferAllowed;
|
|
41
|
+
function setNativeBufferAllowed(v) {
|
|
42
|
+
nativeBufferAllowed = v;
|
|
43
|
+
}
|
|
44
|
+
ArrayBuffers.setNativeBufferAllowed = setNativeBufferAllowed;
|
|
45
|
+
function isArrayBuffer(v) {
|
|
46
|
+
return _instanceof(v, ArrayBuffer);
|
|
47
|
+
}
|
|
48
|
+
ArrayBuffers.isArrayBuffer = isArrayBuffer;
|
|
49
|
+
function slice(o, start, end) {
|
|
50
|
+
// NodeJS Buffer slice is not the same as UInt8Array slice
|
|
51
|
+
// https://nodejs.org/api/buffer.html#bufslicestart-end
|
|
52
|
+
if (isBuffer(o)) {
|
|
53
|
+
return Uint8Array.prototype.slice.call(o, start, end);
|
|
54
|
+
}
|
|
55
|
+
return o.slice(start, end);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* slice the given view with the given offset and length, will handle the {@link Buffer} as well
|
|
59
|
+
*
|
|
60
|
+
* @see {@link https://nodejs.org/api/buffer.html#bufslicestart-end Buffer.slice}
|
|
61
|
+
*/ ArrayBuffers.slice = slice;
|
|
62
|
+
function asView(TypedArray, v, byteOffset, byteLength) {
|
|
63
|
+
if (_instanceof(v, TypedArray) && (byteOffset !== null && byteOffset !== void 0 ? byteOffset : 0) === 0 && byteLength === undefined) {
|
|
64
|
+
return v;
|
|
65
|
+
}
|
|
66
|
+
if (ArrayBuffer.isView(v) || isBuffer(v)) {
|
|
67
|
+
if (isNativeBufferAllowed() && TypedArray === Buffer) {
|
|
68
|
+
// new Buffer() is deprecated
|
|
69
|
+
return Buffer.from(v.buffer, byteOffset, byteLength);
|
|
70
|
+
}
|
|
71
|
+
return new TypedArray(v.buffer, v.byteOffset + (byteOffset !== null && byteOffset !== void 0 ? byteOffset : 0), byteLength !== null && byteLength !== void 0 ? byteLength : v.byteLength);
|
|
72
|
+
}
|
|
73
|
+
return new TypedArray(v, byteOffset, byteLength);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* asView convert the given value to given {@link TypedArray} view
|
|
77
|
+
*
|
|
78
|
+
* TypedArray can be {@link Buffer}, will avoid copy
|
|
79
|
+
*/ ArrayBuffers.asView = asView;
|
|
80
|
+
function toString(source) {
|
|
81
|
+
var encoding = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "utf8";
|
|
82
|
+
if (typeof source === "string") {
|
|
83
|
+
switch (encoding) {
|
|
84
|
+
case "base64":
|
|
85
|
+
return btoa(source);
|
|
86
|
+
case "utf-8":
|
|
87
|
+
case "utf8":
|
|
88
|
+
return source;
|
|
89
|
+
default:
|
|
90
|
+
throw new Error("[ArrayBuffers.toString] Unsupported encoding for string: ".concat(encoding));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
var u8 = asView(Uint8Array, source);
|
|
94
|
+
if (isNativeBufferAllowed()) {
|
|
95
|
+
return Buffer.from(u8).toString(encoding);
|
|
96
|
+
}
|
|
97
|
+
// reference: https://github.com/feross/buffer/blob/master/index.js
|
|
98
|
+
switch (encoding) {
|
|
99
|
+
case "hex":
|
|
100
|
+
{
|
|
101
|
+
return toHexString(u8);
|
|
102
|
+
}
|
|
103
|
+
case "base64":
|
|
104
|
+
{
|
|
105
|
+
return toBase64(u8);
|
|
106
|
+
}
|
|
107
|
+
case "utf8":
|
|
108
|
+
case "utf-8":
|
|
109
|
+
return decode(source);
|
|
110
|
+
case "ascii":
|
|
111
|
+
{
|
|
112
|
+
return toAsciiString(u8);
|
|
113
|
+
}
|
|
114
|
+
case "latin1":
|
|
115
|
+
case "binary":
|
|
116
|
+
{
|
|
117
|
+
return toLatin1String(u8);
|
|
118
|
+
}
|
|
119
|
+
case "ucs2":
|
|
120
|
+
case "ucs-2":
|
|
121
|
+
case "utf16le":
|
|
122
|
+
{
|
|
123
|
+
return toUtf16LeString(u8);
|
|
124
|
+
}
|
|
125
|
+
default:
|
|
126
|
+
throw new Error("[ArrayBuffers.toString] Unknown encoding: ".concat(encoding));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* toString convert the given {@link BufferSource} to string
|
|
131
|
+
*/ ArrayBuffers.toString = toString;
|
|
132
|
+
/**
|
|
133
|
+
* Normalize encoding string to standard form
|
|
134
|
+
* @param encoding - The encoding string to normalize
|
|
135
|
+
* @returns Normalized encoding or undefined if invalid
|
|
136
|
+
*/ function normalizeEncoding(encoding) {
|
|
137
|
+
switch (encoding === null || encoding === void 0 ? void 0 : encoding.toLowerCase()) {
|
|
138
|
+
case "utf-8":
|
|
139
|
+
case "utf8":
|
|
140
|
+
return "utf8";
|
|
141
|
+
case "utf-16le":
|
|
142
|
+
case "ucs2":
|
|
143
|
+
case "ucs-2":
|
|
144
|
+
return "utf16le";
|
|
145
|
+
case "hex":
|
|
146
|
+
case "ascii":
|
|
147
|
+
case "latin1":
|
|
148
|
+
case "binary":
|
|
149
|
+
case "base64":
|
|
150
|
+
case "utf16le":
|
|
151
|
+
return encoding;
|
|
152
|
+
default:
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function isEncoding(v) {
|
|
157
|
+
return normalizeEncoding(v) !== undefined;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Check if the given string is a supported character encoding
|
|
161
|
+
* @param v - The string to check
|
|
162
|
+
* @returns True if the encoding is supported, false otherwise
|
|
163
|
+
*/ ArrayBuffers.isEncoding = isEncoding;
|
|
164
|
+
function toJSON(v, reviver) {
|
|
165
|
+
return JSON.parse(toString(v), reviver);
|
|
166
|
+
}
|
|
167
|
+
ArrayBuffers.toJSON = toJSON;
|
|
168
|
+
function from(src) {
|
|
169
|
+
var encoding = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "utf8", view = arguments.length > 2 ? arguments[2] : void 0;
|
|
170
|
+
if (!src) {
|
|
171
|
+
return new (view || ArrayBuffer)(0);
|
|
172
|
+
}
|
|
173
|
+
if (isBufferSource(src)) {
|
|
174
|
+
return view ? asView(view, src) : src;
|
|
175
|
+
}
|
|
176
|
+
// Array<number> | Iterable<number>
|
|
177
|
+
if (typeof src !== "string" && isIterable(src) || Array.isArray(src)) {
|
|
178
|
+
return (view || Uint8Array).from(src);
|
|
179
|
+
}
|
|
180
|
+
if (view) {
|
|
181
|
+
return asView(view, from(src, encoding));
|
|
182
|
+
}
|
|
183
|
+
if (typeof src === "string") {
|
|
184
|
+
if (isNativeBufferAllowed()) {
|
|
185
|
+
return Buffer.from(src, encoding);
|
|
186
|
+
}
|
|
187
|
+
switch (encoding) {
|
|
188
|
+
case "utf-8":
|
|
189
|
+
case "utf8":
|
|
190
|
+
return encode(src).buffer;
|
|
191
|
+
case "base64":
|
|
192
|
+
return fromBase64(src);
|
|
193
|
+
case "hex":
|
|
194
|
+
return fromHex(src);
|
|
195
|
+
default:
|
|
196
|
+
throw new Error("ArrayBuffers.from unsupported encoding: ".concat(encoding));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
var type = classOf(src);
|
|
200
|
+
throw new TypeError("ArrayBuffers.from unsupported type ".concat(type));
|
|
201
|
+
}
|
|
202
|
+
ArrayBuffers.from = from;
|
|
203
|
+
function concat(buffers, result) {
|
|
204
|
+
var offset = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 0;
|
|
205
|
+
if (!Array.isArray(buffers) || buffers.length === 0) {
|
|
206
|
+
return new ArrayBuffer(0);
|
|
207
|
+
}
|
|
208
|
+
var length = buffers.reduce(function (a, b) {
|
|
209
|
+
var _b_byteLength;
|
|
210
|
+
return a + ((_b_byteLength = b === null || b === void 0 ? void 0 : b.byteLength) !== null && _b_byteLength !== void 0 ? _b_byteLength : 0);
|
|
211
|
+
}, 0);
|
|
212
|
+
var r = result ? new Uint8Array(result) : new Uint8Array(length);
|
|
213
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
214
|
+
try {
|
|
215
|
+
for (var _iterator = buffers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
216
|
+
var buffer = _step.value;
|
|
217
|
+
if (!(buffer === null || buffer === void 0 ? void 0 : buffer.byteLength))
|
|
218
|
+
continue;
|
|
219
|
+
var n = void 0;
|
|
220
|
+
if (_instanceof(buffer, ArrayBuffer)) {
|
|
221
|
+
n = new Uint8Array(buffer);
|
|
222
|
+
}
|
|
223
|
+
else if (ArrayBuffer.isView(buffer)) {
|
|
224
|
+
n = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
throw new Error("ArrayBuffers.concat unsupported type ".concat(classOf(buffer)));
|
|
228
|
+
}
|
|
229
|
+
r.set(n, offset);
|
|
230
|
+
offset += buffer.byteLength;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
catch (err) {
|
|
234
|
+
_didIteratorError = true;
|
|
235
|
+
_iteratorError = err;
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
try {
|
|
239
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
240
|
+
_iterator.return();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
if (_didIteratorError) {
|
|
245
|
+
throw _iteratorError;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return r.buffer;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* concat the given {@link BufferSource} to a new {@link ArrayBuffer}
|
|
253
|
+
*/ ArrayBuffers.concat = concat;
|
|
254
|
+
function fromBase64(v, encoding) {
|
|
255
|
+
if (encoding) {
|
|
256
|
+
return toString(fromBase64(v), encoding);
|
|
257
|
+
}
|
|
258
|
+
if ("fromBase64" in Uint8Array && typeof Uint8Array.fromBase64 === "function") {
|
|
259
|
+
return Uint8Array.fromBase64(v);
|
|
260
|
+
}
|
|
261
|
+
if (isNativeBufferAllowed()) {
|
|
262
|
+
return Buffer.from(v, "base64");
|
|
263
|
+
}
|
|
264
|
+
// Clean the base64 string by removing invalid characters
|
|
265
|
+
return decodeBase64ToUint8Array(v.replace(/[^0-9a-zA-Z=+/_]/g, ""));
|
|
266
|
+
}
|
|
267
|
+
ArrayBuffers.fromBase64 = fromBase64;
|
|
268
|
+
function fromHex(v, encoding) {
|
|
269
|
+
if (encoding) {
|
|
270
|
+
return toString(fromHex(v), encoding);
|
|
271
|
+
}
|
|
272
|
+
if ("fromHex" in Uint8Array && typeof Uint8Array.fromHex === "function") {
|
|
273
|
+
return Uint8Array.fromHex(v);
|
|
274
|
+
}
|
|
275
|
+
if (isNativeBufferAllowed()) {
|
|
276
|
+
return Buffer.from(v, "hex");
|
|
277
|
+
}
|
|
278
|
+
// Handle odd-length hex strings by padding with leading zero
|
|
279
|
+
var cleanHex = v.length % 2 === 1 ? "0" + v : v;
|
|
280
|
+
var matches = cleanHex.match(/.{1,2}/g);
|
|
281
|
+
if (!matches) {
|
|
282
|
+
throw new Error("Invalid hex string");
|
|
283
|
+
}
|
|
284
|
+
var num = matches.map(function (byte) {
|
|
285
|
+
return parseInt(byte, 16);
|
|
286
|
+
});
|
|
287
|
+
return new Uint8Array(num);
|
|
288
|
+
}
|
|
289
|
+
ArrayBuffers.fromHex = fromHex;
|
|
290
|
+
function toBase64(source) {
|
|
291
|
+
source = encode(source);
|
|
292
|
+
if ("toBase64" in Uint8Array.prototype) {
|
|
293
|
+
return toUint8Array(source).toBase64();
|
|
294
|
+
}
|
|
295
|
+
if (isNativeBufferAllowed()) {
|
|
296
|
+
return Buffer.from(asView(Uint8Array, source)).toString("base64");
|
|
297
|
+
}
|
|
298
|
+
return encodeArrayBufferToBase64(toArrayBuffer(source));
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* toBase64 convert the given {@link BufferSource} to base64 string
|
|
302
|
+
* @param source if string, will be encoded as utf8
|
|
303
|
+
*/ ArrayBuffers.toBase64 = toBase64;
|
|
304
|
+
function toHex(v) {
|
|
305
|
+
v = encode(v);
|
|
306
|
+
if ("toHex" in Uint8Array.prototype) {
|
|
307
|
+
return toUint8Array(v).toHex();
|
|
308
|
+
}
|
|
309
|
+
if (isNativeBufferAllowed()) {
|
|
310
|
+
return Buffer.from(asView(Uint8Array, v)).toString("hex");
|
|
311
|
+
}
|
|
312
|
+
return toString(v, "hex");
|
|
313
|
+
}
|
|
314
|
+
ArrayBuffers.toHex = toHex;
|
|
315
|
+
function resize(v, newByteLength, maxByteLength) {
|
|
316
|
+
if (newByteLength === undefined || newByteLength === null) {
|
|
317
|
+
return v;
|
|
318
|
+
}
|
|
319
|
+
// Chrome 111, Nodejs 20 - use native resize if available
|
|
320
|
+
if ("resize" in v && typeof v.resize === "function") {
|
|
321
|
+
if ("resizable" in v && v.resizable) {
|
|
322
|
+
if ("maxByteLength" in v && typeof v.maxByteLength === "number" && v.maxByteLength >= newByteLength) {
|
|
323
|
+
v.resize(newByteLength);
|
|
324
|
+
return v;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
// Fallback: create new buffer and copy data
|
|
329
|
+
var old = v;
|
|
330
|
+
var newBuf = new ArrayBuffer(newByteLength, {
|
|
331
|
+
maxByteLength: maxByteLength
|
|
332
|
+
});
|
|
333
|
+
var oldView = new Uint8Array(old);
|
|
334
|
+
var newView = new Uint8Array(newBuf);
|
|
335
|
+
newView.set(oldView);
|
|
336
|
+
return newBuf;
|
|
337
|
+
}
|
|
338
|
+
ArrayBuffers.resize = resize;
|
|
339
|
+
function toArrayBuffer(v) {
|
|
340
|
+
if (_instanceof(v, ArrayBuffer)) {
|
|
341
|
+
return v;
|
|
342
|
+
}
|
|
343
|
+
if (ArrayBuffer.isView(v)) {
|
|
344
|
+
if (v.byteOffset > 0) {
|
|
345
|
+
throw new Error("ArrayBuffers.toArrayBuffer does not support view with offset");
|
|
346
|
+
}
|
|
347
|
+
return v.buffer;
|
|
348
|
+
}
|
|
349
|
+
throw new Error("ArrayBuffers.toArrayBuffer unsupported type ".concat(classOf(v)));
|
|
350
|
+
}
|
|
351
|
+
ArrayBuffers.toArrayBuffer = toArrayBuffer;
|
|
352
|
+
function toUint8Array(v) {
|
|
353
|
+
return asView(Uint8Array, v);
|
|
354
|
+
}
|
|
355
|
+
ArrayBuffers.toUint8Array = toUint8Array;
|
|
356
|
+
function alloc(size, fill, encoding) {
|
|
357
|
+
if (fill !== undefined) {
|
|
358
|
+
if (typeof fill === "number") {
|
|
359
|
+
return new Uint8Array(size).fill(fill);
|
|
360
|
+
}
|
|
361
|
+
// Convert string to buffer and slice to size
|
|
362
|
+
// https://stackoverflow.com/questions/73994091
|
|
363
|
+
return asView(Uint8Array, from(fill, encoding)).slice(0, size);
|
|
364
|
+
}
|
|
365
|
+
return new ArrayBuffer(size);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Allocate a new ArrayBuffer or Uint8Array with optional fill value
|
|
369
|
+
* @param size - The size in bytes to allocate
|
|
370
|
+
* @param fill - Optional fill value (number or string)
|
|
371
|
+
* @param encoding - Encoding for string fill value (default: 'utf8')
|
|
372
|
+
* @returns ArrayBuffer or Uint8Array
|
|
373
|
+
*/ ArrayBuffers.alloc = alloc;
|
|
374
|
+
// Helper functions for string conversion
|
|
375
|
+
/**
|
|
376
|
+
* Convert Uint8Array to hex string efficiently
|
|
377
|
+
* @param u8 - The Uint8Array to convert
|
|
378
|
+
* @returns Hex string representation
|
|
379
|
+
*/ function toHexString(u8) {
|
|
380
|
+
var result = "";
|
|
381
|
+
for (var i = 0; i < u8.length; i++) {
|
|
382
|
+
result += hexLookupTable[u8[i]];
|
|
383
|
+
}
|
|
384
|
+
return result;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Convert Uint8Array to ASCII string
|
|
388
|
+
* @param u8 - The Uint8Array to convert
|
|
389
|
+
* @returns ASCII string representation
|
|
390
|
+
*/ function toAsciiString(u8) {
|
|
391
|
+
var result = "";
|
|
392
|
+
for (var i = 0; i < u8.length; i++) {
|
|
393
|
+
result += String.fromCharCode(u8[i] & 127);
|
|
394
|
+
}
|
|
395
|
+
return result;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Convert Uint8Array to Latin1 string
|
|
399
|
+
* @param u8 - The Uint8Array to convert
|
|
400
|
+
* @returns Latin1 string representation
|
|
401
|
+
*/ function toLatin1String(u8) {
|
|
402
|
+
var result = "";
|
|
403
|
+
for (var i = 0; i < u8.length; i++) {
|
|
404
|
+
result += String.fromCharCode(u8[i]);
|
|
405
|
+
}
|
|
406
|
+
return result;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Convert Uint8Array to UTF-16LE string
|
|
410
|
+
* @param u8 - The Uint8Array to convert
|
|
411
|
+
* @returns UTF-16LE string representation
|
|
412
|
+
*/ function toUtf16LeString(u8) {
|
|
413
|
+
var result = "";
|
|
414
|
+
// If length is odd, the last 8 bits must be ignored (same as node.js)
|
|
415
|
+
for (var i = 0; i < u8.length - 1; i += 2) {
|
|
416
|
+
result += String.fromCharCode(u8[i] + u8[i + 1] * 256);
|
|
417
|
+
}
|
|
418
|
+
return result;
|
|
419
|
+
}
|
|
420
|
+
// base16 lookup table for efficient hex conversion
|
|
421
|
+
var hexLookupTable = function () {
|
|
422
|
+
var alphabet = "0123456789abcdef";
|
|
423
|
+
var table = new Array(256);
|
|
424
|
+
for (var i = 0; i < 16; ++i) {
|
|
425
|
+
var i16 = i * 16;
|
|
426
|
+
for (var j = 0; j < 16; ++j) {
|
|
427
|
+
table[i16 + j] = alphabet[i] + alphabet[j];
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return table;
|
|
431
|
+
}();
|
|
432
|
+
// Helper functions for internal use
|
|
433
|
+
function isIterable(obj) {
|
|
434
|
+
return obj != null && typeof (obj === null || obj === void 0 ? void 0 : obj[Symbol.iterator]) === "function";
|
|
435
|
+
}
|
|
436
|
+
function isBufferSource(value) {
|
|
437
|
+
return ArrayBuffer.isView(value) || _instanceof(value, ArrayBuffer);
|
|
438
|
+
}
|
|
439
|
+
function equals(a, b) {
|
|
440
|
+
if (a === b)
|
|
441
|
+
return true;
|
|
442
|
+
var aView = asView(Uint8Array, a);
|
|
443
|
+
var bView = asView(Uint8Array, b);
|
|
444
|
+
if (aView.length !== bView.length) {
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
for (var i = 0; i < aView.length; i++) {
|
|
448
|
+
if (aView[i] !== bView[i]) {
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Check if two BufferSources are equal
|
|
456
|
+
* @param a - First buffer source
|
|
457
|
+
* @param b - Second buffer source
|
|
458
|
+
* @returns True if buffers are equal, false otherwise
|
|
459
|
+
*/ ArrayBuffers.equals = equals;
|
|
460
|
+
function compare(a, b) {
|
|
461
|
+
if (a === b)
|
|
462
|
+
return 0;
|
|
463
|
+
var aView = asView(Uint8Array, a);
|
|
464
|
+
var bView = asView(Uint8Array, b);
|
|
465
|
+
var minLength = Math.min(aView.length, bView.length);
|
|
466
|
+
for (var i = 0; i < minLength; i++) {
|
|
467
|
+
if (aView[i] < bView[i])
|
|
468
|
+
return -1;
|
|
469
|
+
if (aView[i] > bView[i])
|
|
470
|
+
return 1;
|
|
471
|
+
}
|
|
472
|
+
return aView.length - bView.length;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Compare two BufferSources lexicographically
|
|
476
|
+
* @param a - First buffer source
|
|
477
|
+
* @param b - Second buffer source
|
|
478
|
+
* @returns -1 if a < b, 0 if a === b, 1 if a > b
|
|
479
|
+
*/ ArrayBuffers.compare = compare;
|
|
480
|
+
function startsWith(buffer, prefix) {
|
|
481
|
+
var bufferView = asView(Uint8Array, buffer);
|
|
482
|
+
var prefixView = asView(Uint8Array, prefix);
|
|
483
|
+
if (prefixView.length > bufferView.length) {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
for (var i = 0; i < prefixView.length; i++) {
|
|
487
|
+
if (bufferView[i] !== prefixView[i]) {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Check if a BufferSource starts with another BufferSource
|
|
495
|
+
* @param buffer - The buffer to check
|
|
496
|
+
* @param prefix - The prefix to check for
|
|
497
|
+
* @returns True if buffer starts with prefix, false otherwise
|
|
498
|
+
*/ ArrayBuffers.startsWith = startsWith;
|
|
499
|
+
function endsWith(buffer, suffix) {
|
|
500
|
+
var bufferView = asView(Uint8Array, buffer);
|
|
501
|
+
var suffixView = asView(Uint8Array, suffix);
|
|
502
|
+
if (suffixView.length > bufferView.length) {
|
|
503
|
+
return false;
|
|
504
|
+
}
|
|
505
|
+
var offset = bufferView.length - suffixView.length;
|
|
506
|
+
for (var i = 0; i < suffixView.length; i++) {
|
|
507
|
+
if (bufferView[offset + i] !== suffixView[i]) {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return true;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Check if a BufferSource ends with another BufferSource
|
|
515
|
+
* @param buffer - The buffer to check
|
|
516
|
+
* @param suffix - The suffix to check for
|
|
517
|
+
* @returns True if buffer ends with suffix, false otherwise
|
|
518
|
+
*/ ArrayBuffers.endsWith = endsWith;
|
|
519
|
+
function indexOf(buffer, search) {
|
|
520
|
+
var startIndex = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 0;
|
|
521
|
+
var bufferView = asView(Uint8Array, buffer);
|
|
522
|
+
var searchView = asView(Uint8Array, search);
|
|
523
|
+
if (searchView.length === 0)
|
|
524
|
+
return startIndex;
|
|
525
|
+
if (searchView.length > bufferView.length)
|
|
526
|
+
return -1;
|
|
527
|
+
for (var i = startIndex; i <= bufferView.length - searchView.length; i++) {
|
|
528
|
+
var found = true;
|
|
529
|
+
for (var j = 0; j < searchView.length; j++) {
|
|
530
|
+
if (bufferView[i + j] !== searchView[j]) {
|
|
531
|
+
found = false;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
if (found)
|
|
536
|
+
return i;
|
|
537
|
+
}
|
|
538
|
+
return -1;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Find the index of a sub-buffer within a buffer
|
|
542
|
+
* @param buffer - The buffer to search in
|
|
543
|
+
* @param search - The sub-buffer to search for
|
|
544
|
+
* @param startIndex - Starting index for search (default: 0)
|
|
545
|
+
* @returns Index of first occurrence, or -1 if not found
|
|
546
|
+
*/ ArrayBuffers.indexOf = indexOf;
|
|
547
|
+
function subarray(buffer, start, end) {
|
|
548
|
+
var view = asView(Uint8Array, buffer);
|
|
549
|
+
return view.subarray(start, end);
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Get a sub-buffer from a buffer
|
|
553
|
+
* @param buffer - The source buffer
|
|
554
|
+
* @param start - Start index (inclusive)
|
|
555
|
+
* @param end - End index (exclusive, optional)
|
|
556
|
+
* @returns New Uint8Array containing the sub-buffer
|
|
557
|
+
*/ ArrayBuffers.subarray = subarray;
|
|
558
|
+
})(ArrayBuffers || (ArrayBuffers = {}));
|
|
559
|
+
export var ArrayBuffers;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function _instanceof(left, right) {
|
|
2
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
3
|
+
return !!right[Symbol.hasInstance](left);
|
|
4
|
+
} else {
|
|
5
|
+
return left instanceof right;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function parseDate(value) {
|
|
9
|
+
if (!value) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
if (_instanceof(value, Date)) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
var parsed = new Date(value);
|
|
16
|
+
if (isNaN(parsed.getTime())) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
return parsed;
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wener/utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.54",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Utils for daily use",
|
|
6
6
|
"repository": {
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"https-proxy-agent": "^7.0.6",
|
|
71
71
|
"node-fetch": "^3.3.2",
|
|
72
72
|
"undici": "^7.16.0",
|
|
73
|
-
"zod": "^4.1.
|
|
73
|
+
"zod": "^4.1.12"
|
|
74
74
|
},
|
|
75
75
|
"publishConfig": {
|
|
76
76
|
"registry": "https://registry.npmjs.org",
|