defuss-runtime 1.0.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.cjs ADDED
@@ -0,0 +1,548 @@
1
+ 'use strict';
2
+
3
+ const _HEX_PREFIX = "hex:";
4
+ function binaryToHex(buffer) {
5
+ const bytes = new Uint8Array(buffer);
6
+ let result = "";
7
+ for (let i = 0; i < bytes.byteLength; i++) {
8
+ result += bytes[i].toString(16).padStart(2, "0");
9
+ }
10
+ return `${_HEX_PREFIX}${result}`;
11
+ }
12
+ function hexToBinary(hexString) {
13
+ const bytes = new Uint8Array(hexString.length / 2);
14
+ for (let i = 0; i < bytes.length; i++) {
15
+ const hexByte = hexString.slice(i * 2, i * 2 + 2);
16
+ bytes[i] = Number.parseInt(hexByte, 16);
17
+ }
18
+ return bytes.buffer;
19
+ }
20
+ function textToBinary(text) {
21
+ return new TextEncoder().encode(text).buffer;
22
+ }
23
+ function binaryToText(buffer) {
24
+ return new TextDecoder().decode(new Uint8Array(buffer));
25
+ }
26
+
27
+ const _BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28
+ exports._base64LookupMap = null;
29
+ function _getBase64LookupMap() {
30
+ if (!exports._base64LookupMap) {
31
+ exports._base64LookupMap = {};
32
+ for (let i = 0; i < _BASE64_CHARS.length; i++) {
33
+ exports._base64LookupMap[_BASE64_CHARS[i]] = i;
34
+ }
35
+ }
36
+ return exports._base64LookupMap;
37
+ }
38
+ function binaryToBase64(buffer) {
39
+ const bytes = new Uint8Array(buffer);
40
+ if (bytes.length === 0) return "";
41
+ let result = "";
42
+ for (let i = 0; i < bytes.length; i += 3) {
43
+ const byte1 = bytes[i];
44
+ const byte2 = i + 1 < bytes.length ? bytes[i + 1] : 0;
45
+ const byte3 = i + 2 < bytes.length ? bytes[i + 2] : 0;
46
+ const chunk1 = byte1 >> 2;
47
+ const chunk2 = (byte1 & 3) << 4 | byte2 >> 4;
48
+ const chunk3 = (byte2 & 15) << 2 | byte3 >> 6;
49
+ const chunk4 = byte3 & 63;
50
+ result += _BASE64_CHARS[chunk1];
51
+ result += _BASE64_CHARS[chunk2];
52
+ result += i + 1 < bytes.length ? _BASE64_CHARS[chunk3] : "=";
53
+ result += i + 2 < bytes.length ? _BASE64_CHARS[chunk4] : "=";
54
+ }
55
+ return result;
56
+ }
57
+ function base64ToBinary(base64) {
58
+ if (base64.startsWith(_HEX_PREFIX)) {
59
+ return hexToBinary(base64.slice(_HEX_PREFIX.length));
60
+ }
61
+ try {
62
+ const input = base64.replace(/=+$/, "");
63
+ if (input.length === 0) return new ArrayBuffer(0);
64
+ const lookupMap = _getBase64LookupMap();
65
+ const outputLength = Math.floor(input.length * 3 / 4);
66
+ const bytes = new Uint8Array(outputLength);
67
+ let outputIndex = 0;
68
+ for (let i = 0; i < input.length; i += 4) {
69
+ const chunk1 = lookupMap[input[i]] || 0;
70
+ const chunk2 = lookupMap[input[i + 1]] || 0;
71
+ const chunk3 = i + 2 < input.length ? lookupMap[input[i + 2]] || 0 : 0;
72
+ const chunk4 = i + 3 < input.length ? lookupMap[input[i + 3]] || 0 : 0;
73
+ bytes[outputIndex++] = chunk1 << 2 | chunk2 >> 4;
74
+ if (i + 2 < input.length) {
75
+ bytes[outputIndex++] = (chunk2 & 15) << 4 | chunk3 >> 2;
76
+ }
77
+ if (i + 3 < input.length) {
78
+ bytes[outputIndex++] = (chunk3 & 3) << 6 | chunk4;
79
+ }
80
+ }
81
+ return bytes.buffer;
82
+ } catch (e) {
83
+ console.error("Failed to decode base64 data", e);
84
+ return new ArrayBuffer(0);
85
+ }
86
+ }
87
+
88
+ const ARRAY_INDEX = /(.*)\[(\d+)\]/;
89
+ const IS_NUMBER = /^\d+$/;
90
+ const getAllKeysFromPath = (path) => path.split(".").flatMap((key) => {
91
+ const match = key.match(ARRAY_INDEX);
92
+ return match ? [...getAllKeysFromPath(match[1]), Number(match[2])] : key;
93
+ });
94
+ const ensureKey = (obj, key, nextKey) => {
95
+ if (!(key in obj)) {
96
+ obj[key] = IS_NUMBER.test(String(nextKey)) ? [] : {};
97
+ }
98
+ };
99
+ const getByPath = (obj, path) => {
100
+ const keys = getAllKeysFromPath(path);
101
+ return keys.reduce(
102
+ (result, key) => result == null ? void 0 : result[key],
103
+ obj
104
+ );
105
+ };
106
+ const setByPath = (obj, path, value) => {
107
+ const keys = getAllKeysFromPath(path);
108
+ const key = keys[0];
109
+ const newObj = Array.isArray(obj) ? [...obj] : { ...obj };
110
+ if (keys.length === 1) {
111
+ if (value === void 0) {
112
+ Array.isArray(newObj) ? newObj.splice(Number(key), 1) : delete newObj[key];
113
+ } else {
114
+ newObj[key] = value;
115
+ }
116
+ return newObj;
117
+ }
118
+ ensureKey(newObj, key, keys[1]);
119
+ newObj[key] = setByPath(newObj[key], keys.slice(1).join("."), value);
120
+ return newObj;
121
+ };
122
+
123
+ function unique(a) {
124
+ return Array.from(new Set(a));
125
+ }
126
+
127
+ function pick(o, keys) {
128
+ const result = {};
129
+ for (const key of keys) {
130
+ if (key in o) {
131
+ result[key] = o[key];
132
+ }
133
+ }
134
+ return result;
135
+ }
136
+
137
+ function omit(o, keys) {
138
+ const result = {};
139
+ const keysToOmit = new Set(keys);
140
+ for (const key in o) {
141
+ if (Object.prototype.hasOwnProperty.call(o, key) && !keysToOmit.has(key)) {
142
+ result[key] = o[key];
143
+ }
144
+ }
145
+ return result;
146
+ }
147
+
148
+ function equalsJSON(a, b) {
149
+ if (typeof a === "undefined" && typeof b === "undefined") {
150
+ return true;
151
+ }
152
+ try {
153
+ const normalizedA = JSON.parse(JSON.stringify(a));
154
+ const normalizedB = JSON.parse(JSON.stringify(b));
155
+ return JSON.stringify(normalizedA) === JSON.stringify(normalizedB);
156
+ } catch {
157
+ return false;
158
+ }
159
+ }
160
+
161
+ const PATH_ACCESSOR_SYMBOL = Symbol("PathAccessor");
162
+ const createProxy = (currentPath = []) => {
163
+ const pathString = currentPath.reduce((acc, segment, index) => {
164
+ if (index === 0) {
165
+ return segment;
166
+ }
167
+ if (segment.startsWith("[")) {
168
+ return `${acc}${segment}`;
169
+ }
170
+ return `${acc}.${segment}`;
171
+ }, "");
172
+ const target = Object.create(String.prototype);
173
+ target.value = pathString;
174
+ return new Proxy(target, {
175
+ get(target2, prop) {
176
+ if (prop === PATH_ACCESSOR_SYMBOL) {
177
+ return true;
178
+ }
179
+ if (prop === "toString" || prop === "valueOf") {
180
+ return () => pathString;
181
+ }
182
+ if (prop === Symbol.toPrimitive) {
183
+ return (hint) => pathString;
184
+ }
185
+ if (typeof prop === "symbol") {
186
+ return void 0;
187
+ }
188
+ if (typeof prop === "string" && /^\d+$/.test(prop)) {
189
+ const newPath2 = [...currentPath, `[${prop}]`];
190
+ return createProxy(newPath2);
191
+ }
192
+ const newPath = [...currentPath, prop];
193
+ return createProxy(newPath);
194
+ },
195
+ // This is crucial for equality comparisons
196
+ getPrototypeOf(target2) {
197
+ return String.prototype;
198
+ },
199
+ // Make the proxy appear as a primitive string
200
+ has(target2, prop) {
201
+ if (prop === PATH_ACCESSOR_SYMBOL || prop === Symbol.toPrimitive || prop === "toString" || prop === "valueOf") {
202
+ return true;
203
+ }
204
+ return prop in String.prototype;
205
+ },
206
+ ownKeys(target2) {
207
+ return ["value"];
208
+ },
209
+ getOwnPropertyDescriptor(target2, prop) {
210
+ if (prop === PATH_ACCESSOR_SYMBOL) {
211
+ return {
212
+ configurable: true,
213
+ enumerable: false,
214
+ value: true,
215
+ writable: false
216
+ };
217
+ }
218
+ if (prop === "toString" || prop === "valueOf") {
219
+ return {
220
+ configurable: true,
221
+ enumerable: false,
222
+ value: () => pathString,
223
+ writable: false
224
+ };
225
+ }
226
+ if (prop === Symbol.toPrimitive) {
227
+ return {
228
+ configurable: true,
229
+ enumerable: false,
230
+ value: (hint) => pathString,
231
+ writable: false
232
+ };
233
+ }
234
+ if (prop === "value") {
235
+ return {
236
+ configurable: true,
237
+ enumerable: false,
238
+ value: pathString,
239
+ writable: false
240
+ };
241
+ }
242
+ return void 0;
243
+ }
244
+ });
245
+ };
246
+ const access = () => createProxy();
247
+ const isPathAccessor = (obj) => typeof obj !== "undefined" && obj !== null && obj[PATH_ACCESSOR_SYMBOL] === true;
248
+
249
+ function debounce(fn, wait) {
250
+ let timeout;
251
+ return function(...args) {
252
+ clearTimeout(timeout);
253
+ timeout = setTimeout(() => fn.apply(this, args), wait);
254
+ };
255
+ }
256
+
257
+ function throttle(fn, wait) {
258
+ let lastTime = 0;
259
+ return function(...args) {
260
+ const now = Date.now();
261
+ if (now - lastTime >= wait) {
262
+ lastTime = now;
263
+ fn.apply(this, args);
264
+ }
265
+ };
266
+ }
267
+
268
+ function wait(ms) {
269
+ return new Promise((resolve) => setTimeout(resolve, ms));
270
+ }
271
+ function createTimeoutPromise(timeoutMs, operation, timeoutCallback) {
272
+ return new Promise((resolve, reject) => {
273
+ const timeoutId = setTimeout(() => {
274
+ timeoutCallback?.(timeoutMs);
275
+ reject(new Error(`Timeout after ${timeoutMs}ms`));
276
+ }, timeoutMs);
277
+ Promise.resolve().then(async () => {
278
+ try {
279
+ const result = await operation();
280
+ clearTimeout(timeoutId);
281
+ resolve(result);
282
+ } catch (error) {
283
+ clearTimeout(timeoutId);
284
+ reject(error);
285
+ }
286
+ });
287
+ });
288
+ }
289
+ async function waitForWithPolling(check, timeout, interval = 1) {
290
+ return createTimeoutPromise(timeout, () => {
291
+ return new Promise((resolve, reject) => {
292
+ const timer = setInterval(() => {
293
+ try {
294
+ const result = check();
295
+ if (result != null) {
296
+ clearInterval(timer);
297
+ resolve(result);
298
+ }
299
+ } catch (err) {
300
+ clearInterval(timer);
301
+ reject(err);
302
+ }
303
+ }, interval);
304
+ });
305
+ });
306
+ }
307
+ async function waitForRef(ref, timeout) {
308
+ return waitForWithPolling(() => ref.current, timeout);
309
+ }
310
+
311
+ const asString = (value) => {
312
+ if (value === null || value === void 0) {
313
+ return "";
314
+ }
315
+ if (typeof value === "string") {
316
+ return value;
317
+ }
318
+ if (typeof value === "number" || typeof value === "boolean") {
319
+ return String(value);
320
+ }
321
+ if (value instanceof Date) {
322
+ return value.toISOString();
323
+ }
324
+ if (value instanceof RegExp) {
325
+ return value.toString();
326
+ }
327
+ if (Array.isArray(value)) {
328
+ return value.join(", ");
329
+ }
330
+ if (typeof value === "object") {
331
+ try {
332
+ return JSON.stringify(value);
333
+ } catch {
334
+ return String(value);
335
+ }
336
+ }
337
+ return String(value);
338
+ };
339
+
340
+ const asNumber = (value) => {
341
+ if (typeof value === "number") {
342
+ return value;
343
+ }
344
+ if (typeof value === "string") {
345
+ const parsed = Number.parseFloat(value);
346
+ return Number.isNaN(parsed) ? 0 : parsed;
347
+ }
348
+ if (value instanceof Date) {
349
+ return value.getTime();
350
+ }
351
+ return 0;
352
+ };
353
+
354
+ const asBoolean = (value) => {
355
+ if (typeof value === "boolean") return value;
356
+ if (typeof value === "string") {
357
+ const lowerValue = value.toLowerCase();
358
+ return lowerValue === "true" || lowerValue === "1" || lowerValue === "yes" || lowerValue === "on";
359
+ }
360
+ if (typeof value === "number") return value !== 0;
361
+ return Boolean(value);
362
+ };
363
+
364
+ const asArray = (value, transformerFn) => {
365
+ if (Array.isArray(value)) {
366
+ return value.map(transformerFn);
367
+ }
368
+ if (value === null || value === void 0) {
369
+ return [];
370
+ }
371
+ const transformedValue = transformerFn(value);
372
+ return Array.isArray(transformedValue) ? transformedValue : [transformedValue];
373
+ };
374
+
375
+ const asDate = (value) => {
376
+ if (value === null || value === void 0) return new Date(Number.NaN);
377
+ if (value instanceof Date) return value;
378
+ const date = new Date(value);
379
+ return Number.isNaN(date.getTime()) ? new Date(Number.NaN) : date;
380
+ };
381
+
382
+ const asInteger = (value) => {
383
+ const number = asNumber(value);
384
+ if (typeof number === "number" && Number.isInteger(number)) {
385
+ return number;
386
+ }
387
+ return asNumber(Number.parseInt(asString(number), 10).toFixed(0));
388
+ };
389
+
390
+ const isAfter = (value, minDate, inclusive = false) => {
391
+ return value instanceof Date && (inclusive ? value.getTime() >= minDate.getTime() : value.getTime() > minDate.getTime());
392
+ };
393
+
394
+ const isArray = (value) => Array.isArray(value);
395
+
396
+ const isBefore = (value, maxDate, inclusive = false) => value instanceof Date && (inclusive ? value.getTime() <= maxDate.getTime() : value.getTime() < maxDate.getTime());
397
+
398
+ const isBoolean = (value) => typeof value === "boolean";
399
+
400
+ const isDate = (value) => value instanceof Date && !Number.isNaN(value.getDate());
401
+
402
+ const isDefined = (value) => typeof value !== "undefined";
403
+
404
+ const isString = (value) => typeof value === "string";
405
+
406
+ const isEmail = (value) => isString(value) && /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\\x01\-\\x08\\x0b\\x0c\\x0e\-\\x1f\\x21\\x23\-\\x5b\\x5d\-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e\-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01\-\\x08\\x0b\\x0c\\x0e\-\\x1f\\x21\-\\x5a\\x53\-\\x7f]|\\[\\x01\-\\x09\\x0b\\x0c\\x0e\-\\x7f])+)\])/.test(
407
+ value
408
+ );
409
+
410
+ const isEmpty = (value) => {
411
+ if (value === null || value === void 0) return true;
412
+ if (typeof value === "string") return value === "";
413
+ if (Array.isArray(value)) return value.length === 0;
414
+ if (value instanceof Date) return false;
415
+ if (typeof value === "object") return Object.keys(value).length === 0;
416
+ return false;
417
+ };
418
+
419
+ const is = (value, valueB) => value === valueB;
420
+
421
+ const isSafeNumber = (value) => typeof value === "number" && !Number.isNaN(value) && Number.isFinite(value);
422
+
423
+ const isGreaterThan = (value, minValue, includeEqual = false) => isSafeNumber(value) && (includeEqual ? value >= minValue : value > minValue);
424
+
425
+ const isSafeNumeric = (value) => {
426
+ if (typeof value === "number") return isSafeNumber(value);
427
+ if (typeof value === "string") {
428
+ if (value.trim() === "") return false;
429
+ const num = Number(value);
430
+ return isSafeNumber(num);
431
+ }
432
+ return false;
433
+ };
434
+
435
+ const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
436
+
437
+ const isOneOf = (value, options) => options.includes(value);
438
+
439
+ const isPhoneNumber = (value) => isString(value) && /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/.test(
440
+ value
441
+ );
442
+
443
+ const isRequired = (value) => !!value;
444
+
445
+ const isSlug = (value) => isString(value) && /^[a-z0-9-]+$/.test(value);
446
+
447
+ const isLessThan = (value, maxValue, includeEqual = false) => isSafeNumber(value) && (includeEqual ? value <= maxValue : value < maxValue);
448
+
449
+ const isUrl = (value) => {
450
+ if (!isString(value)) return false;
451
+ try {
452
+ new URL(value);
453
+ return true;
454
+ } catch (_) {
455
+ return false;
456
+ }
457
+ };
458
+
459
+ const isUrlPath = (value) => isString(value) && value.length > 0 && /^[a-z0-9\-_\/]+$/.test(value);
460
+
461
+ const getDateValue = (date) => ({
462
+ year: date.getFullYear(),
463
+ month: date.getMonth(),
464
+ date: date.getDate(),
465
+ minute: date.getMinutes(),
466
+ hour: date.getHours(),
467
+ second: date.getSeconds(),
468
+ millisecond: date.getMilliseconds()
469
+ });
470
+
471
+ const isLongerThan = (value, minLength, includeEqual = false) => {
472
+ if (typeof value !== "string") return false;
473
+ return includeEqual ? value.length >= minLength : value.length > minLength;
474
+ };
475
+
476
+ const isShorterThan = (value, maxLength, includeEqual = false) => {
477
+ if (typeof value !== "string") return false;
478
+ return includeEqual ? value.length <= maxLength : value.length < maxLength;
479
+ };
480
+
481
+ const hasPattern = (value, pattern) => {
482
+ if (typeof value !== "string") return false;
483
+ return pattern.test(value);
484
+ };
485
+
486
+ const isInteger = (value) => isSafeNumber(value) && Number.isInteger(value);
487
+
488
+ const isEqual = (value, valueB) => equalsJSON(value, valueB);
489
+
490
+ exports.PATH_ACCESSOR_SYMBOL = PATH_ACCESSOR_SYMBOL;
491
+ exports._BASE64_CHARS = _BASE64_CHARS;
492
+ exports._HEX_PREFIX = _HEX_PREFIX;
493
+ exports._getBase64LookupMap = _getBase64LookupMap;
494
+ exports.access = access;
495
+ exports.asArray = asArray;
496
+ exports.asBoolean = asBoolean;
497
+ exports.asDate = asDate;
498
+ exports.asInteger = asInteger;
499
+ exports.asNumber = asNumber;
500
+ exports.asString = asString;
501
+ exports.base64ToBinary = base64ToBinary;
502
+ exports.binaryToBase64 = binaryToBase64;
503
+ exports.binaryToHex = binaryToHex;
504
+ exports.binaryToText = binaryToText;
505
+ exports.createTimeoutPromise = createTimeoutPromise;
506
+ exports.debounce = debounce;
507
+ exports.ensureKey = ensureKey;
508
+ exports.equalsJSON = equalsJSON;
509
+ exports.getAllKeysFromPath = getAllKeysFromPath;
510
+ exports.getByPath = getByPath;
511
+ exports.getDateValue = getDateValue;
512
+ exports.hasPattern = hasPattern;
513
+ exports.hexToBinary = hexToBinary;
514
+ exports.is = is;
515
+ exports.isAfter = isAfter;
516
+ exports.isArray = isArray;
517
+ exports.isBefore = isBefore;
518
+ exports.isBoolean = isBoolean;
519
+ exports.isDate = isDate;
520
+ exports.isDefined = isDefined;
521
+ exports.isEmail = isEmail;
522
+ exports.isEmpty = isEmpty;
523
+ exports.isEqual = isEqual;
524
+ exports.isGreaterThan = isGreaterThan;
525
+ exports.isInteger = isInteger;
526
+ exports.isLessThan = isLessThan;
527
+ exports.isLongerThan = isLongerThan;
528
+ exports.isObject = isObject;
529
+ exports.isOneOf = isOneOf;
530
+ exports.isPathAccessor = isPathAccessor;
531
+ exports.isPhoneNumber = isPhoneNumber;
532
+ exports.isRequired = isRequired;
533
+ exports.isSafeNumber = isSafeNumber;
534
+ exports.isSafeNumeric = isSafeNumeric;
535
+ exports.isShorterThan = isShorterThan;
536
+ exports.isSlug = isSlug;
537
+ exports.isString = isString;
538
+ exports.isUrl = isUrl;
539
+ exports.isUrlPath = isUrlPath;
540
+ exports.omit = omit;
541
+ exports.pick = pick;
542
+ exports.setByPath = setByPath;
543
+ exports.textToBinary = textToBinary;
544
+ exports.throttle = throttle;
545
+ exports.unique = unique;
546
+ exports.wait = wait;
547
+ exports.waitForRef = waitForRef;
548
+ exports.waitForWithPolling = waitForWithPolling;