@hybridly/utils 0.10.0-beta.2 → 0.10.0-beta.20
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 +10 -27
- package/dist/index.mjs +34 -88
- package/package.json +46 -50
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import clone from "lodash.clonedeep";
|
|
1
|
+
import { Path, PathValue, SearchableObject, getByPath, setByPath } from "@clickbar/dot-diver";
|
|
3
2
|
import makeDebugger from "debug";
|
|
4
3
|
|
|
5
4
|
//#region src/form-data.d.ts
|
|
@@ -40,48 +39,32 @@ declare function showViewComponentErrorModal(response: string): Modal;
|
|
|
40
39
|
declare function random(length?: number): string;
|
|
41
40
|
/** Simple pattern matching util. */
|
|
42
41
|
declare function match<TValue extends string | number = string, TReturnValue = unknown, TArgs extends readonly unknown[] = []>(value: TValue, lookup: Record<TValue | 'default', TReturnValue | ((...args: TArgs) => TReturnValue | Promise<TReturnValue>)>, ...args: TArgs): TReturnValue | Promise<TReturnValue>;
|
|
43
|
-
declare function
|
|
44
|
-
|
|
42
|
+
declare function wrap<T>(value: undefined | T | T[]): T[];
|
|
43
|
+
/**
|
|
44
|
+
* Returns the object only if the condition is true. Useful for conditionally merging in an object using the spread operator.
|
|
45
|
+
*/
|
|
46
|
+
declare function mergeObject<T extends object>(condition: any, data: T): T | object;
|
|
45
47
|
interface MergeOptions {
|
|
46
48
|
overwriteArray?: boolean;
|
|
47
49
|
mergePlainObjects?: boolean;
|
|
48
50
|
arrayMerge?: (target: any[], source: any[]) => any[];
|
|
49
51
|
}
|
|
50
52
|
declare function merge<T>(x: Partial<T>, y: Partial<T>, options?: MergeOptions): T;
|
|
51
|
-
declare function removeTrailingSlash(string: string): string;
|
|
52
|
-
/**
|
|
53
|
-
* Sets a value at a path in an object
|
|
54
|
-
*
|
|
55
|
-
* This function will set a value at a path in an object, creating any missing
|
|
56
|
-
* objects along the way. The object is modified in place.
|
|
57
|
-
*
|
|
58
|
-
* @param obj the object to set the value in
|
|
59
|
-
* @param path a dot-separated path to the property to set
|
|
60
|
-
* @param value the value to set
|
|
61
|
-
*/
|
|
62
|
-
declare function setValueAtPath(obj: any, path: string, value: any): void;
|
|
63
|
-
/**
|
|
64
|
-
* Unsets a property at a path in an object
|
|
65
|
-
*
|
|
66
|
-
* This function will unset a property at a path in an object, deleting any
|
|
67
|
-
* objects along the way that are empty. The object is modified in place.
|
|
68
|
-
*
|
|
69
|
-
* @param obj the object to unset the property in
|
|
70
|
-
* @param path a dot-separated path to the property to unset
|
|
71
|
-
*/
|
|
72
|
-
declare function unsetPropertyAtPath(obj: any, path: string): void;
|
|
73
53
|
//#endregion
|
|
74
54
|
//#region src/debug.d.ts
|
|
75
55
|
declare const debug: {
|
|
76
56
|
router: makeDebugger.Debugger;
|
|
57
|
+
queue: makeDebugger.Debugger;
|
|
77
58
|
history: makeDebugger.Debugger;
|
|
78
59
|
url: makeDebugger.Debugger;
|
|
79
60
|
context: makeDebugger.Debugger;
|
|
80
61
|
external: makeDebugger.Debugger;
|
|
81
62
|
scroll: makeDebugger.Debugger;
|
|
82
63
|
hook: makeDebugger.Debugger;
|
|
64
|
+
layout: makeDebugger.Debugger;
|
|
65
|
+
config: makeDebugger.Debugger;
|
|
83
66
|
plugin: (name: string, ...args: any[]) => void;
|
|
84
67
|
adapter: (name: string, ...args: any[]) => void;
|
|
85
68
|
};
|
|
86
69
|
//#endregion
|
|
87
|
-
export { FormDataConvertible,
|
|
70
|
+
export { FormDataConvertible, type Path, type PathValue, RequestData, type SearchableObject, debug, getByPath, hasFiles, match, merge, mergeObject, objectToFormData, random, setByPath, showResponseErrorModal, showViewComponentErrorModal, wrap };
|
package/dist/index.mjs
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import baseMerge from "deepmerge";
|
|
2
|
-
import { isPlainObject } from "
|
|
3
|
-
import {
|
|
4
|
-
import clone from "lodash.clonedeep";
|
|
2
|
+
import { isPlainObject } from "es-toolkit/predicate";
|
|
3
|
+
import { getByPath, setByPath } from "@clickbar/dot-diver";
|
|
5
4
|
import makeDebugger from "debug";
|
|
6
|
-
|
|
7
5
|
//#region src/form-data.ts
|
|
8
6
|
/**
|
|
9
7
|
* Checks if the given object has a file.
|
|
10
8
|
*/
|
|
11
9
|
function hasFiles(data) {
|
|
12
10
|
if (!data) return false;
|
|
13
|
-
return data instanceof File || data instanceof Blob || data instanceof FileList && data.length > 0 || data instanceof FormData && Array.from(data.values()).some((value
|
|
11
|
+
return data instanceof File || data instanceof Blob || data instanceof FileList && data.length > 0 || data instanceof FormData && Array.from(data.values()).some((value) => hasFiles(value)) || typeof data === "object" && data !== null && Object.values(data).some((value) => hasFiles(value));
|
|
14
12
|
}
|
|
15
13
|
/**
|
|
16
14
|
* Converts an object literal to a `FormData` object.
|
|
@@ -25,34 +23,28 @@ function objectToFormData(source, form, parentKey) {
|
|
|
25
23
|
function composeKey(key, parentKey) {
|
|
26
24
|
return parentKey ? `${parentKey}[${key}]` : key;
|
|
27
25
|
}
|
|
28
|
-
function append(form, key, value
|
|
29
|
-
if (Array.isArray(value
|
|
30
|
-
const valueAsArray = value
|
|
26
|
+
function append(form, key, value) {
|
|
27
|
+
if (Array.isArray(value) || value instanceof Set) {
|
|
28
|
+
const valueAsArray = value instanceof Set ? [...value] : value;
|
|
31
29
|
if (!valueAsArray.length) return form.append(key, "");
|
|
32
30
|
return Array.from(valueAsArray.keys()).forEach((index) => append(form, composeKey(index.toString(), key), valueAsArray[index]));
|
|
33
|
-
} else if (value
|
|
34
|
-
else if (value
|
|
35
|
-
else if (value
|
|
36
|
-
else if (typeof value
|
|
37
|
-
else if (typeof value
|
|
38
|
-
else if (typeof value
|
|
39
|
-
else if (value
|
|
40
|
-
objectToFormData(value
|
|
31
|
+
} else if (value instanceof Date) return form.append(key, value.toISOString());
|
|
32
|
+
else if (value instanceof File) return form.append(key, value, value.name);
|
|
33
|
+
else if (value instanceof Blob) return form.append(key, value);
|
|
34
|
+
else if (typeof value === "boolean") return form.append(key, value ? "1" : "0");
|
|
35
|
+
else if (typeof value === "string") return form.append(key, value);
|
|
36
|
+
else if (typeof value === "number") return form.append(key, `${value}`);
|
|
37
|
+
else if (value === null || value === void 0) return form.append(key, "");
|
|
38
|
+
objectToFormData(value, form, key);
|
|
41
39
|
}
|
|
42
|
-
|
|
43
40
|
//#endregion
|
|
44
41
|
//#region src/error-modal.ts
|
|
45
42
|
const stack = [];
|
|
46
43
|
var Modal = class Modal {
|
|
47
|
-
main;
|
|
48
|
-
overlay;
|
|
49
|
-
iframe;
|
|
50
|
-
style;
|
|
51
|
-
hideOnEscape;
|
|
52
|
-
animationDurationInMs = 200;
|
|
53
44
|
constructor(html, id) {
|
|
54
45
|
this.html = html;
|
|
55
46
|
this.id = id;
|
|
47
|
+
this.animationDurationInMs = 200;
|
|
56
48
|
if (stack.includes(id)) return;
|
|
57
49
|
if (this.initializeDOM() === false) return;
|
|
58
50
|
this.show();
|
|
@@ -105,8 +97,8 @@ var Modal = class Modal {
|
|
|
105
97
|
iframe.style.height = "100%";
|
|
106
98
|
iframe.style.borderRadius = "10px";
|
|
107
99
|
overlay.appendChild(iframe);
|
|
108
|
-
const style
|
|
109
|
-
style
|
|
100
|
+
const style = document.createElement("style");
|
|
101
|
+
style.innerHTML = `
|
|
110
102
|
[data-hybridly] {
|
|
111
103
|
opacity: 0;
|
|
112
104
|
overflow: hidden;
|
|
@@ -133,7 +125,7 @@ var Modal = class Modal {
|
|
|
133
125
|
this.main = main;
|
|
134
126
|
this.overlay = overlay;
|
|
135
127
|
this.iframe = iframe;
|
|
136
|
-
this.style = style
|
|
128
|
+
this.style = style;
|
|
137
129
|
}
|
|
138
130
|
show() {
|
|
139
131
|
stack.push(this.id);
|
|
@@ -255,7 +247,6 @@ function style() {
|
|
|
255
247
|
}
|
|
256
248
|
`;
|
|
257
249
|
}
|
|
258
|
-
|
|
259
250
|
//#endregion
|
|
260
251
|
//#region src/utils.ts
|
|
261
252
|
function random(length = 10) {
|
|
@@ -265,20 +256,23 @@ function random(length = 10) {
|
|
|
265
256
|
return str;
|
|
266
257
|
}
|
|
267
258
|
/** Simple pattern matching util. */
|
|
268
|
-
function match(value
|
|
269
|
-
if (value
|
|
270
|
-
const returnValue = value
|
|
259
|
+
function match(value, lookup, ...args) {
|
|
260
|
+
if (value in lookup || "default" in lookup) {
|
|
261
|
+
const returnValue = value in lookup ? lookup[value] : lookup.default;
|
|
271
262
|
return typeof returnValue === "function" ? returnValue(...args) : returnValue;
|
|
272
263
|
}
|
|
273
264
|
const handlers = Object.keys(lookup).map((key) => `"${key}"`).join(", ");
|
|
274
|
-
throw new Error(`Tried to handle "${value
|
|
265
|
+
throw new Error(`Tried to handle "${value}" but there is no handler defined. Only defined handlers are: ${handlers}.`);
|
|
275
266
|
}
|
|
276
|
-
function
|
|
277
|
-
if (
|
|
278
|
-
return value
|
|
267
|
+
function wrap(value) {
|
|
268
|
+
if (value === void 0) return [];
|
|
269
|
+
return Array.isArray(value) ? value : [value];
|
|
279
270
|
}
|
|
280
|
-
|
|
281
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Returns the object only if the condition is true. Useful for conditionally merging in an object using the spread operator.
|
|
273
|
+
*/
|
|
274
|
+
function mergeObject(condition, data) {
|
|
275
|
+
if (!condition) return {};
|
|
282
276
|
return data;
|
|
283
277
|
}
|
|
284
278
|
function merge(x, y, options = {}) {
|
|
@@ -287,69 +281,21 @@ function merge(x, y, options = {}) {
|
|
|
287
281
|
isMergeableObject: options?.mergePlainObjects ? isPlainObject : void 0
|
|
288
282
|
});
|
|
289
283
|
}
|
|
290
|
-
function removeTrailingSlash(string) {
|
|
291
|
-
return string.replace(/\/+$/, "");
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Sets a value at a path in an object
|
|
295
|
-
*
|
|
296
|
-
* This function will set a value at a path in an object, creating any missing
|
|
297
|
-
* objects along the way. The object is modified in place.
|
|
298
|
-
*
|
|
299
|
-
* @param obj the object to set the value in
|
|
300
|
-
* @param path a dot-separated path to the property to set
|
|
301
|
-
* @param value the value to set
|
|
302
|
-
*/
|
|
303
|
-
function setValueAtPath(obj, path, value$1) {
|
|
304
|
-
if (!path.includes(".")) {
|
|
305
|
-
obj[path] = value$1;
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
const segments = path.split(".");
|
|
309
|
-
let nestedObject = obj;
|
|
310
|
-
for (let i = 0; i < segments.length - 1; i++) {
|
|
311
|
-
const key = segments[i];
|
|
312
|
-
nestedObject = nestedObject[key] = nestedObject[key] || {};
|
|
313
|
-
}
|
|
314
|
-
nestedObject[segments[segments.length - 1]] = value$1;
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Unsets a property at a path in an object
|
|
318
|
-
*
|
|
319
|
-
* This function will unset a property at a path in an object, deleting any
|
|
320
|
-
* objects along the way that are empty. The object is modified in place.
|
|
321
|
-
*
|
|
322
|
-
* @param obj the object to unset the property in
|
|
323
|
-
* @param path a dot-separated path to the property to unset
|
|
324
|
-
*/
|
|
325
|
-
function unsetPropertyAtPath(obj, path) {
|
|
326
|
-
if (!path.includes(".")) {
|
|
327
|
-
delete obj[path];
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
const segments = path.split(".");
|
|
331
|
-
let nestedObject = obj;
|
|
332
|
-
for (let i = 0; i < segments.length - 1; i++) {
|
|
333
|
-
const key = segments[i];
|
|
334
|
-
nestedObject = nestedObject[key] = nestedObject[key] || {};
|
|
335
|
-
}
|
|
336
|
-
delete nestedObject[segments[segments.length - 1]];
|
|
337
|
-
if (Object.keys(nestedObject).length === 0) unsetPropertyAtPath(obj, segments.slice(0, -1).join("."));
|
|
338
|
-
}
|
|
339
|
-
|
|
340
284
|
//#endregion
|
|
341
285
|
//#region src/debug.ts
|
|
342
286
|
const debug = {
|
|
343
287
|
router: makeDebugger("hybridly:core:router"),
|
|
288
|
+
queue: makeDebugger("hybridly:core:router:queue"),
|
|
344
289
|
history: makeDebugger("hybridly:core:history"),
|
|
345
290
|
url: makeDebugger("hybridly:core:url"),
|
|
346
291
|
context: makeDebugger("hybridly:core:context"),
|
|
347
292
|
external: makeDebugger("hybridly:core:external"),
|
|
348
293
|
scroll: makeDebugger("hybridly:core:scroll"),
|
|
349
294
|
hook: makeDebugger("hybridly:core:hook"),
|
|
295
|
+
layout: makeDebugger("hybridly:plugin:layout"),
|
|
296
|
+
config: makeDebugger("hybridly:vite:config"),
|
|
350
297
|
plugin: (name, ...args) => makeDebugger("hybridly:plugin").extend(name.replace("hybridly:", ""))(args.shift(), ...args),
|
|
351
298
|
adapter: (name, ...args) => makeDebugger("hybridly:adapter").extend(name)(args.shift(), ...args)
|
|
352
299
|
};
|
|
353
|
-
|
|
354
300
|
//#endregion
|
|
355
|
-
export {
|
|
301
|
+
export { debug, getByPath, hasFiles, match, merge, mergeObject, objectToFormData, random, setByPath, showResponseErrorModal, showViewComponentErrorModal, wrap };
|
package/package.json
CHANGED
|
@@ -1,52 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
},
|
|
49
|
-
"devDependencies": {
|
|
50
|
-
"@types/lodash.clonedeep": "^4.5.9"
|
|
51
|
-
}
|
|
2
|
+
"name": "@hybridly/utils",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.10.0-beta.20",
|
|
5
|
+
"description": "Utils used in Hybridly packages",
|
|
6
|
+
"author": "Enzo Innocenzi <enzo@innocenzi.dev>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"funding": "https://github.com/sponsors/innocenzi",
|
|
9
|
+
"homepage": "https://github.com/hybridly/hybridly/tree/main/packages/config#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/hybridly/hybridly.git",
|
|
13
|
+
"directory": "packages/utils"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/hybridly/hybridly/issues"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"hybridly"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"provenance": true,
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"import": "./dist/index.mjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"module": "dist/index.mjs",
|
|
33
|
+
"types": "dist/index.d.mts",
|
|
34
|
+
"files": [
|
|
35
|
+
"*.d.ts",
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "obuild",
|
|
40
|
+
"build:stub": "obuild --stub"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@clickbar/dot-diver": "^1.0.7",
|
|
44
|
+
"debug": "^4.4.1",
|
|
45
|
+
"deepmerge": "^4.3.1",
|
|
46
|
+
"es-toolkit": "^1.45.1"
|
|
47
|
+
}
|
|
52
48
|
}
|