@hybridly/utils 0.10.0-beta.1 → 0.10.0-beta.10
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.mjs +27 -71
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3,18 +3,10 @@ import { isPlainObject } from "is-plain-object";
|
|
|
3
3
|
import { debounce, throttle } from "throttle-debounce";
|
|
4
4
|
import clone from "lodash.clonedeep";
|
|
5
5
|
import makeDebugger from "debug";
|
|
6
|
-
|
|
7
|
-
//#region src/form-data.ts
|
|
8
|
-
/**
|
|
9
|
-
* Checks if the given object has a file.
|
|
10
|
-
*/
|
|
11
6
|
function hasFiles(data) {
|
|
12
7
|
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
|
|
8
|
+
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
9
|
}
|
|
15
|
-
/**
|
|
16
|
-
* Converts an object literal to a `FormData` object.
|
|
17
|
-
*/
|
|
18
10
|
function objectToFormData(source, form, parentKey) {
|
|
19
11
|
source ??= {};
|
|
20
12
|
form ??= new FormData();
|
|
@@ -25,34 +17,26 @@ function objectToFormData(source, form, parentKey) {
|
|
|
25
17
|
function composeKey(key, parentKey) {
|
|
26
18
|
return parentKey ? `${parentKey}[${key}]` : key;
|
|
27
19
|
}
|
|
28
|
-
function append(form, key, value
|
|
29
|
-
if (Array.isArray(value
|
|
30
|
-
const valueAsArray = value
|
|
20
|
+
function append(form, key, value) {
|
|
21
|
+
if (Array.isArray(value) || value instanceof Set) {
|
|
22
|
+
const valueAsArray = value instanceof Set ? [...value] : value;
|
|
31
23
|
if (!valueAsArray.length) return form.append(key, "");
|
|
32
24
|
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
|
|
25
|
+
} else if (value instanceof Date) return form.append(key, value.toISOString());
|
|
26
|
+
else if (value instanceof File) return form.append(key, value, value.name);
|
|
27
|
+
else if (value instanceof Blob) return form.append(key, value);
|
|
28
|
+
else if (typeof value === "boolean") return form.append(key, value ? "1" : "0");
|
|
29
|
+
else if (typeof value === "string") return form.append(key, value);
|
|
30
|
+
else if (typeof value === "number") return form.append(key, `${value}`);
|
|
31
|
+
else if (value === null || value === void 0) return form.append(key, "");
|
|
32
|
+
objectToFormData(value, form, key);
|
|
41
33
|
}
|
|
42
|
-
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region src/error-modal.ts
|
|
45
34
|
const stack = [];
|
|
46
35
|
var Modal = class Modal {
|
|
47
|
-
main;
|
|
48
|
-
overlay;
|
|
49
|
-
iframe;
|
|
50
|
-
style;
|
|
51
|
-
hideOnEscape;
|
|
52
|
-
animationDurationInMs = 200;
|
|
53
36
|
constructor(html, id) {
|
|
54
37
|
this.html = html;
|
|
55
38
|
this.id = id;
|
|
39
|
+
this.animationDurationInMs = 200;
|
|
56
40
|
if (stack.includes(id)) return;
|
|
57
41
|
if (this.initializeDOM() === false) return;
|
|
58
42
|
this.show();
|
|
@@ -105,8 +89,8 @@ var Modal = class Modal {
|
|
|
105
89
|
iframe.style.height = "100%";
|
|
106
90
|
iframe.style.borderRadius = "10px";
|
|
107
91
|
overlay.appendChild(iframe);
|
|
108
|
-
const style
|
|
109
|
-
style
|
|
92
|
+
const style = document.createElement("style");
|
|
93
|
+
style.innerHTML = `
|
|
110
94
|
[data-hybridly] {
|
|
111
95
|
opacity: 0;
|
|
112
96
|
overflow: hidden;
|
|
@@ -133,7 +117,7 @@ var Modal = class Modal {
|
|
|
133
117
|
this.main = main;
|
|
134
118
|
this.overlay = overlay;
|
|
135
119
|
this.iframe = iframe;
|
|
136
|
-
this.style = style
|
|
120
|
+
this.style = style;
|
|
137
121
|
}
|
|
138
122
|
show() {
|
|
139
123
|
stack.push(this.id);
|
|
@@ -255,27 +239,23 @@ function style() {
|
|
|
255
239
|
}
|
|
256
240
|
`;
|
|
257
241
|
}
|
|
258
|
-
|
|
259
|
-
//#endregion
|
|
260
|
-
//#region src/utils.ts
|
|
261
242
|
function random(length = 10) {
|
|
262
243
|
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
263
244
|
let str = "";
|
|
264
245
|
for (let i = 0; i < length; i++) str += chars.charAt(Math.floor(Math.random() * 62));
|
|
265
246
|
return str;
|
|
266
247
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const returnValue = value$1 in lookup ? lookup[value$1] : lookup.default;
|
|
248
|
+
function match(value, lookup, ...args) {
|
|
249
|
+
if (value in lookup || "default" in lookup) {
|
|
250
|
+
const returnValue = value in lookup ? lookup[value] : lookup.default;
|
|
271
251
|
return typeof returnValue === "function" ? returnValue(...args) : returnValue;
|
|
272
252
|
}
|
|
273
253
|
const handlers = Object.keys(lookup).map((key) => `"${key}"`).join(", ");
|
|
274
|
-
throw new Error(`Tried to handle "${value
|
|
254
|
+
throw new Error(`Tried to handle "${value}" but there is no handler defined. Only defined handlers are: ${handlers}.`);
|
|
275
255
|
}
|
|
276
|
-
function value(value
|
|
277
|
-
if (typeof value
|
|
278
|
-
return value
|
|
256
|
+
function value(value) {
|
|
257
|
+
if (typeof value === "function") return value?.();
|
|
258
|
+
return value;
|
|
279
259
|
}
|
|
280
260
|
function when(condition, data, _default) {
|
|
281
261
|
if (!condition) return _default;
|
|
@@ -290,19 +270,9 @@ function merge(x, y, options = {}) {
|
|
|
290
270
|
function removeTrailingSlash(string) {
|
|
291
271
|
return string.replace(/\/+$/, "");
|
|
292
272
|
}
|
|
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) {
|
|
273
|
+
function setValueAtPath(obj, path, value) {
|
|
304
274
|
if (!path.includes(".")) {
|
|
305
|
-
obj[path] = value
|
|
275
|
+
obj[path] = value;
|
|
306
276
|
return;
|
|
307
277
|
}
|
|
308
278
|
const segments = path.split(".");
|
|
@@ -311,17 +281,8 @@ function setValueAtPath(obj, path, value$1) {
|
|
|
311
281
|
const key = segments[i];
|
|
312
282
|
nestedObject = nestedObject[key] = nestedObject[key] || {};
|
|
313
283
|
}
|
|
314
|
-
nestedObject[segments[segments.length - 1]] = value
|
|
284
|
+
nestedObject[segments[segments.length - 1]] = value;
|
|
315
285
|
}
|
|
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
286
|
function unsetPropertyAtPath(obj, path) {
|
|
326
287
|
if (!path.includes(".")) {
|
|
327
288
|
delete obj[path];
|
|
@@ -336,9 +297,6 @@ function unsetPropertyAtPath(obj, path) {
|
|
|
336
297
|
delete nestedObject[segments[segments.length - 1]];
|
|
337
298
|
if (Object.keys(nestedObject).length === 0) unsetPropertyAtPath(obj, segments.slice(0, -1).join("."));
|
|
338
299
|
}
|
|
339
|
-
|
|
340
|
-
//#endregion
|
|
341
|
-
//#region src/debug.ts
|
|
342
300
|
const debug = {
|
|
343
301
|
router: makeDebugger("hybridly:core:router"),
|
|
344
302
|
history: makeDebugger("hybridly:core:history"),
|
|
@@ -350,6 +308,4 @@ const debug = {
|
|
|
350
308
|
plugin: (name, ...args) => makeDebugger("hybridly:plugin").extend(name.replace("hybridly:", ""))(args.shift(), ...args),
|
|
351
309
|
adapter: (name, ...args) => makeDebugger("hybridly:adapter").extend(name)(args.shift(), ...args)
|
|
352
310
|
};
|
|
353
|
-
|
|
354
|
-
//#endregion
|
|
355
|
-
export { clone, debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showResponseErrorModal, showViewComponentErrorModal, throttle, unsetPropertyAtPath, value, when };
|
|
311
|
+
export { clone, debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showResponseErrorModal, showViewComponentErrorModal, throttle, unsetPropertyAtPath, value, when };
|