@hybridly/utils 0.9.0 → 0.10.0-beta.2
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 +39 -36
- package/dist/index.mjs +196 -202
- package/package.json +9 -11
- package/LICENSE +0 -19
- package/dist/index.cjs +0 -384
- package/dist/index.d.cts +0 -84
- package/dist/index.d.ts +0 -84
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { debounce, throttle } from "throttle-debounce";
|
|
2
|
+
import clone from "lodash.clonedeep";
|
|
3
|
+
import makeDebugger from "debug";
|
|
4
4
|
|
|
5
|
+
//#region src/form-data.d.ts
|
|
5
6
|
type RequestData = Record<string, FormDataConvertible> | FormDataConvertible | FormData;
|
|
6
7
|
type FormDataConvertible = {
|
|
7
|
-
|
|
8
|
+
[key: string]: FormDataConvertible;
|
|
8
9
|
} | Array<FormDataConvertible> | Set<FormDataConvertible> | Blob | File | FormDataEntryValue | Date | boolean | number | null | undefined | string;
|
|
9
10
|
/**
|
|
10
11
|
* Checks if the given object has a file.
|
|
@@ -14,35 +15,37 @@ declare function hasFiles(data?: RequestData): boolean;
|
|
|
14
15
|
* Converts an object literal to a `FormData` object.
|
|
15
16
|
*/
|
|
16
17
|
declare function objectToFormData(source?: RequestData, form?: FormData, parentKey?: string): FormData;
|
|
17
|
-
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/error-modal.d.ts
|
|
18
20
|
declare class Modal {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
21
|
+
private html;
|
|
22
|
+
private id;
|
|
23
|
+
private main;
|
|
24
|
+
private overlay;
|
|
25
|
+
private iframe;
|
|
26
|
+
private style;
|
|
27
|
+
private hideOnEscape?;
|
|
28
|
+
private animationDurationInMs;
|
|
29
|
+
constructor(html: string, id: string);
|
|
30
|
+
static fromException(response: string, id: string): Modal;
|
|
31
|
+
static forViewComponent(component: string, id: string): Modal;
|
|
32
|
+
initializeDOM(): false | undefined;
|
|
33
|
+
show(): void;
|
|
34
|
+
destroy(): void;
|
|
33
35
|
}
|
|
34
36
|
declare function showResponseErrorModal(response: string): Modal;
|
|
35
37
|
declare function showViewComponentErrorModal(response: string): Modal;
|
|
36
|
-
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/utils.d.ts
|
|
37
40
|
declare function random(length?: number): string;
|
|
38
41
|
/** Simple pattern matching util. */
|
|
39
42
|
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>;
|
|
40
43
|
declare function value<T>(value: T | (() => T)): T;
|
|
41
44
|
declare function when<T, D>(condition: any, data: T, _default?: D): T | D | undefined;
|
|
42
45
|
interface MergeOptions {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
overwriteArray?: boolean;
|
|
47
|
+
mergePlainObjects?: boolean;
|
|
48
|
+
arrayMerge?: (target: any[], source: any[]) => any[];
|
|
46
49
|
}
|
|
47
50
|
declare function merge<T>(x: Partial<T>, y: Partial<T>, options?: MergeOptions): T;
|
|
48
51
|
declare function removeTrailingSlash(string: string): string;
|
|
@@ -67,18 +70,18 @@ declare function setValueAtPath(obj: any, path: string, value: any): void;
|
|
|
67
70
|
* @param path a dot-separated path to the property to unset
|
|
68
71
|
*/
|
|
69
72
|
declare function unsetPropertyAtPath(obj: any, path: string): void;
|
|
70
|
-
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/debug.d.ts
|
|
71
75
|
declare const debug: {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
router: makeDebugger.Debugger;
|
|
77
|
+
history: makeDebugger.Debugger;
|
|
78
|
+
url: makeDebugger.Debugger;
|
|
79
|
+
context: makeDebugger.Debugger;
|
|
80
|
+
external: makeDebugger.Debugger;
|
|
81
|
+
scroll: makeDebugger.Debugger;
|
|
82
|
+
hook: makeDebugger.Debugger;
|
|
83
|
+
plugin: (name: string, ...args: any[]) => void;
|
|
84
|
+
adapter: (name: string, ...args: any[]) => void;
|
|
81
85
|
};
|
|
82
|
-
|
|
83
|
-
export { debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showResponseErrorModal, showViewComponentErrorModal, unsetPropertyAtPath, value, when };
|
|
84
|
-
export type { FormDataConvertible, RequestData };
|
|
86
|
+
//#endregion
|
|
87
|
+
export { FormDataConvertible, RequestData, clone, debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showResponseErrorModal, showViewComponentErrorModal, throttle, unsetPropertyAtPath, value, when };
|
package/dist/index.mjs
CHANGED
|
@@ -1,80 +1,65 @@
|
|
|
1
|
-
import baseMerge from
|
|
2
|
-
import { isPlainObject } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import makeDebugger from
|
|
1
|
+
import baseMerge from "deepmerge";
|
|
2
|
+
import { isPlainObject } from "is-plain-object";
|
|
3
|
+
import { debounce, throttle } from "throttle-debounce";
|
|
4
|
+
import clone from "lodash.clonedeep";
|
|
5
|
+
import makeDebugger from "debug";
|
|
6
6
|
|
|
7
|
+
//#region src/form-data.ts
|
|
8
|
+
/**
|
|
9
|
+
* Checks if the given object has a file.
|
|
10
|
+
*/
|
|
7
11
|
function hasFiles(data) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
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));
|
|
12
|
+
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$1) => hasFiles(value$1)) || typeof data === "object" && data !== null && Object.values(data).some((value$1) => hasFiles(value$1));
|
|
12
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts an object literal to a `FormData` object.
|
|
17
|
+
*/
|
|
13
18
|
function objectToFormData(source, form, parentKey) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
for (const key in source) {
|
|
20
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
21
|
-
append(form, composeKey(key, parentKey), source[key]);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return form;
|
|
19
|
+
source ??= {};
|
|
20
|
+
form ??= new FormData();
|
|
21
|
+
if (typeof source !== "object" || source instanceof Set || Array.isArray(source) || source instanceof Blob || source instanceof Date || source instanceof FormData) throw new TypeError("Source must be an object literal to be converted to a FormData object.");
|
|
22
|
+
for (const key in source) if (Object.prototype.hasOwnProperty.call(source, key)) append(form, composeKey(key, parentKey), source[key]);
|
|
23
|
+
return form;
|
|
25
24
|
}
|
|
26
25
|
function composeKey(key, parentKey) {
|
|
27
|
-
|
|
26
|
+
return parentKey ? `${parentKey}[${key}]` : key;
|
|
28
27
|
}
|
|
29
|
-
function append(form, key, value) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
} else if (typeof value === "boolean") {
|
|
43
|
-
return form.append(key, value ? "1" : "0");
|
|
44
|
-
} else if (typeof value === "string") {
|
|
45
|
-
return form.append(key, value);
|
|
46
|
-
} else if (typeof value === "number") {
|
|
47
|
-
return form.append(key, `${value}`);
|
|
48
|
-
} else if (value === null || value === void 0) {
|
|
49
|
-
return form.append(key, "");
|
|
50
|
-
}
|
|
51
|
-
objectToFormData(value, form, key);
|
|
28
|
+
function append(form, key, value$1) {
|
|
29
|
+
if (Array.isArray(value$1) || value$1 instanceof Set) {
|
|
30
|
+
const valueAsArray = value$1 instanceof Set ? [...value$1] : value$1;
|
|
31
|
+
if (!valueAsArray.length) return form.append(key, "");
|
|
32
|
+
return Array.from(valueAsArray.keys()).forEach((index) => append(form, composeKey(index.toString(), key), valueAsArray[index]));
|
|
33
|
+
} else if (value$1 instanceof Date) return form.append(key, value$1.toISOString());
|
|
34
|
+
else if (value$1 instanceof File) return form.append(key, value$1, value$1.name);
|
|
35
|
+
else if (value$1 instanceof Blob) return form.append(key, value$1);
|
|
36
|
+
else if (typeof value$1 === "boolean") return form.append(key, value$1 ? "1" : "0");
|
|
37
|
+
else if (typeof value$1 === "string") return form.append(key, value$1);
|
|
38
|
+
else if (typeof value$1 === "number") return form.append(key, `${value$1}`);
|
|
39
|
+
else if (value$1 === null || value$1 === void 0) return form.append(key, "");
|
|
40
|
+
objectToFormData(value$1, form, key);
|
|
52
41
|
}
|
|
53
42
|
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/error-modal.ts
|
|
54
45
|
const stack = [];
|
|
55
|
-
class Modal {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
animationDurationInMs = 200;
|
|
73
|
-
static fromException(response, id) {
|
|
74
|
-
if (typeof response === "string" && response.trim() !== "") {
|
|
75
|
-
return new Modal(`<style>${htmlStyle()}</style>${response.toString()}`, id);
|
|
76
|
-
}
|
|
77
|
-
return new Modal(`
|
|
46
|
+
var Modal = class Modal {
|
|
47
|
+
main;
|
|
48
|
+
overlay;
|
|
49
|
+
iframe;
|
|
50
|
+
style;
|
|
51
|
+
hideOnEscape;
|
|
52
|
+
animationDurationInMs = 200;
|
|
53
|
+
constructor(html, id) {
|
|
54
|
+
this.html = html;
|
|
55
|
+
this.id = id;
|
|
56
|
+
if (stack.includes(id)) return;
|
|
57
|
+
if (this.initializeDOM() === false) return;
|
|
58
|
+
this.show();
|
|
59
|
+
}
|
|
60
|
+
static fromException(response, id) {
|
|
61
|
+
if (typeof response === "string" && response.trim() !== "") return new Modal(`<style>${htmlStyle()}</style>${response.toString()}`, id);
|
|
62
|
+
return new Modal(`
|
|
78
63
|
<style>${style()}</style>
|
|
79
64
|
<div class="h-full text-center flex">
|
|
80
65
|
<div class="m-auto">
|
|
@@ -84,9 +69,9 @@ class Modal {
|
|
|
84
69
|
</div>
|
|
85
70
|
</div>
|
|
86
71
|
`, id);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
72
|
+
}
|
|
73
|
+
static forViewComponent(component, id) {
|
|
74
|
+
return new Modal(`
|
|
90
75
|
<style>${style()}</style>
|
|
91
76
|
<div class="h-full text-center flex">
|
|
92
77
|
<div class="m-auto">
|
|
@@ -96,36 +81,32 @@ class Modal {
|
|
|
96
81
|
</div>
|
|
97
82
|
</div>
|
|
98
83
|
`, id);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
iframe.style.borderRadius = "10px";
|
|
126
|
-
overlay.appendChild(iframe);
|
|
127
|
-
const style2 = document.createElement("style");
|
|
128
|
-
style2.innerHTML = `
|
|
84
|
+
}
|
|
85
|
+
initializeDOM() {
|
|
86
|
+
if (!this.html) return false;
|
|
87
|
+
if (document.querySelector("[data-hybridly-overlay=\"true\"]")) return false;
|
|
88
|
+
const main = document.createElement("html");
|
|
89
|
+
main.innerHTML = this.html;
|
|
90
|
+
main.querySelectorAll("a").forEach((a) => a.setAttribute("target", "_top"));
|
|
91
|
+
const overlay = document.createElement("div");
|
|
92
|
+
overlay.dataset.hybridly = "";
|
|
93
|
+
overlay.style.position = "fixed";
|
|
94
|
+
overlay.style.width = "100vw";
|
|
95
|
+
overlay.style.height = "100vh";
|
|
96
|
+
overlay.style.padding = "50px";
|
|
97
|
+
overlay.style.boxSizing = "border-box";
|
|
98
|
+
overlay.style.backgroundColor = "rgba(0, 0, 0, .35)";
|
|
99
|
+
overlay.style.color = "white";
|
|
100
|
+
overlay.style.zIndex = "2147483638";
|
|
101
|
+
overlay.style.overflow = "hidden";
|
|
102
|
+
const iframe = document.createElement("iframe");
|
|
103
|
+
iframe.style.backgroundColor = "#050505";
|
|
104
|
+
iframe.style.width = "100%";
|
|
105
|
+
iframe.style.height = "100%";
|
|
106
|
+
iframe.style.borderRadius = "10px";
|
|
107
|
+
overlay.appendChild(iframe);
|
|
108
|
+
const style$1 = document.createElement("style");
|
|
109
|
+
style$1.innerHTML = `
|
|
129
110
|
[data-hybridly] {
|
|
130
111
|
opacity: 0;
|
|
131
112
|
overflow: hidden;
|
|
@@ -149,46 +130,44 @@ class Modal {
|
|
|
149
130
|
opacity: 1;
|
|
150
131
|
}
|
|
151
132
|
`;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
}
|
|
133
|
+
this.main = main;
|
|
134
|
+
this.overlay = overlay;
|
|
135
|
+
this.iframe = iframe;
|
|
136
|
+
this.style = style$1;
|
|
137
|
+
}
|
|
138
|
+
show() {
|
|
139
|
+
stack.push(this.id);
|
|
140
|
+
this.overlay.addEventListener("click", () => this.destroy());
|
|
141
|
+
this.hideOnEscape = (event) => {
|
|
142
|
+
if (event.keyCode === 27) this.destroy();
|
|
143
|
+
};
|
|
144
|
+
document.addEventListener("keydown", this.hideOnEscape);
|
|
145
|
+
document.body.prepend(this.style);
|
|
146
|
+
document.body.prepend(this.overlay);
|
|
147
|
+
this.iframe.contentWindow?.document.open();
|
|
148
|
+
this.iframe.contentWindow?.document.write(this.main.outerHTML);
|
|
149
|
+
this.iframe.contentWindow?.document.close();
|
|
150
|
+
this.overlay.dataset.hybridly = "visible";
|
|
151
|
+
}
|
|
152
|
+
destroy() {
|
|
153
|
+
stack.splice(stack.indexOf(this.html), 1);
|
|
154
|
+
this.overlay.dataset.hybridly = "";
|
|
155
|
+
setTimeout(() => {
|
|
156
|
+
this.overlay.outerHTML = "";
|
|
157
|
+
this.overlay.remove();
|
|
158
|
+
this.style.remove();
|
|
159
|
+
document.removeEventListener("keydown", this.hideOnEscape);
|
|
160
|
+
}, this.animationDurationInMs);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
184
163
|
function showResponseErrorModal(response) {
|
|
185
|
-
|
|
164
|
+
return Modal.fromException(response, "non-hybrid-response");
|
|
186
165
|
}
|
|
187
166
|
function showViewComponentErrorModal(response) {
|
|
188
|
-
|
|
167
|
+
return Modal.forViewComponent(response, `view-component-${response}`);
|
|
189
168
|
}
|
|
190
169
|
function htmlStyle() {
|
|
191
|
-
|
|
170
|
+
return `
|
|
192
171
|
html {
|
|
193
172
|
background-color: #050505;
|
|
194
173
|
color: white;
|
|
@@ -204,7 +183,7 @@ function htmlStyle() {
|
|
|
204
183
|
`;
|
|
205
184
|
}
|
|
206
185
|
function style() {
|
|
207
|
-
|
|
186
|
+
return `
|
|
208
187
|
${htmlStyle()}
|
|
209
188
|
body {
|
|
210
189
|
padding: 5rem 2rem;
|
|
@@ -225,7 +204,7 @@ function style() {
|
|
|
225
204
|
.h-full {
|
|
226
205
|
height: 100%;
|
|
227
206
|
}
|
|
228
|
-
.max-h
|
|
207
|
+
.max-h-\[500px\] {
|
|
229
208
|
max-height: 500px;
|
|
230
209
|
}
|
|
231
210
|
.w-full {
|
|
@@ -277,85 +256,100 @@ function style() {
|
|
|
277
256
|
`;
|
|
278
257
|
}
|
|
279
258
|
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region src/utils.ts
|
|
280
261
|
function random(length = 10) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
return str;
|
|
262
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
263
|
+
let str = "";
|
|
264
|
+
for (let i = 0; i < length; i++) str += chars.charAt(Math.floor(Math.random() * 62));
|
|
265
|
+
return str;
|
|
287
266
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
267
|
+
/** Simple pattern matching util. */
|
|
268
|
+
function match(value$1, lookup, ...args) {
|
|
269
|
+
if (value$1 in lookup || "default" in lookup) {
|
|
270
|
+
const returnValue = value$1 in lookup ? lookup[value$1] : lookup.default;
|
|
271
|
+
return typeof returnValue === "function" ? returnValue(...args) : returnValue;
|
|
272
|
+
}
|
|
273
|
+
const handlers = Object.keys(lookup).map((key) => `"${key}"`).join(", ");
|
|
274
|
+
throw new Error(`Tried to handle "${value$1}" but there is no handler defined. Only defined handlers are: ${handlers}.`);
|
|
295
275
|
}
|
|
296
|
-
function value(
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
return value2;
|
|
276
|
+
function value(value$1) {
|
|
277
|
+
if (typeof value$1 === "function") return value$1?.();
|
|
278
|
+
return value$1;
|
|
301
279
|
}
|
|
302
280
|
function when(condition, data, _default) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
return data;
|
|
281
|
+
if (!condition) return _default;
|
|
282
|
+
return data;
|
|
307
283
|
}
|
|
308
284
|
function merge(x, y, options = {}) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
isMergeableObject
|
|
314
|
-
});
|
|
285
|
+
return baseMerge(x, y, {
|
|
286
|
+
arrayMerge: typeof options?.arrayMerge === "function" ? options.arrayMerge : options?.overwriteArray !== false ? (_, s) => s : void 0,
|
|
287
|
+
isMergeableObject: options?.mergePlainObjects ? isPlainObject : void 0
|
|
288
|
+
});
|
|
315
289
|
}
|
|
316
290
|
function removeTrailingSlash(string) {
|
|
317
|
-
|
|
291
|
+
return string.replace(/\/+$/, "");
|
|
318
292
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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;
|
|
331
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
|
+
*/
|
|
332
325
|
function unsetPropertyAtPath(obj, path) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
unsetPropertyAtPath(obj, segments.slice(0, -1).join("."));
|
|
346
|
-
}
|
|
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("."));
|
|
347
338
|
}
|
|
348
339
|
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region src/debug.ts
|
|
349
342
|
const debug = {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
343
|
+
router: makeDebugger("hybridly:core:router"),
|
|
344
|
+
history: makeDebugger("hybridly:core:history"),
|
|
345
|
+
url: makeDebugger("hybridly:core:url"),
|
|
346
|
+
context: makeDebugger("hybridly:core:context"),
|
|
347
|
+
external: makeDebugger("hybridly:core:external"),
|
|
348
|
+
scroll: makeDebugger("hybridly:core:scroll"),
|
|
349
|
+
hook: makeDebugger("hybridly:core:hook"),
|
|
350
|
+
plugin: (name, ...args) => makeDebugger("hybridly:plugin").extend(name.replace("hybridly:", ""))(args.shift(), ...args),
|
|
351
|
+
adapter: (name, ...args) => makeDebugger("hybridly:adapter").extend(name)(args.shift(), ...args)
|
|
359
352
|
};
|
|
360
353
|
|
|
361
|
-
|
|
354
|
+
//#endregion
|
|
355
|
+
export { clone, debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showResponseErrorModal, showViewComponentErrorModal, throttle, unsetPropertyAtPath, value, when };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hybridly/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.10.0-beta.2",
|
|
5
5
|
"description": "Utils used in Hybridly packages",
|
|
6
6
|
"author": "Enzo Innocenzi <enzo@innocenzi.dev>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -25,18 +25,20 @@
|
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
|
-
"types": "./dist/index.d.
|
|
29
|
-
"import": "./dist/index.mjs"
|
|
30
|
-
"require": "./dist/index.cjs"
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"import": "./dist/index.mjs"
|
|
31
30
|
}
|
|
32
31
|
},
|
|
33
|
-
"main": "dist/index.cjs",
|
|
34
32
|
"module": "dist/index.mjs",
|
|
35
|
-
"types": "dist/index.d.
|
|
33
|
+
"types": "dist/index.d.mts",
|
|
36
34
|
"files": [
|
|
37
35
|
"*.d.ts",
|
|
38
36
|
"dist"
|
|
39
37
|
],
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "obuild",
|
|
40
|
+
"build:stub": "obuild --stub"
|
|
41
|
+
},
|
|
40
42
|
"dependencies": {
|
|
41
43
|
"debug": "^4.4.1",
|
|
42
44
|
"deepmerge": "^4.3.1",
|
|
@@ -46,9 +48,5 @@
|
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
50
|
"@types/lodash.clonedeep": "^4.5.9"
|
|
49
|
-
},
|
|
50
|
-
"scripts": {
|
|
51
|
-
"build": "unbuild",
|
|
52
|
-
"stub": "unbuild --stub"
|
|
53
51
|
}
|
|
54
|
-
}
|
|
52
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
in the Software without restriction, including without limitation the rights
|
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
furnished to do so, subject to the following conditions:
|
|
9
|
-
|
|
10
|
-
The above copyright notice and this permission notice shall be included in all
|
|
11
|
-
copies or substantial portions of the Software.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
-
SOFTWARE.
|
package/dist/index.cjs
DELETED
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const baseMerge = require('deepmerge');
|
|
4
|
-
const isPlainObject = require('is-plain-object');
|
|
5
|
-
const throttleDebounce = require('throttle-debounce');
|
|
6
|
-
const lodash_clonedeep = require('lodash.clonedeep');
|
|
7
|
-
const makeDebugger = require('debug');
|
|
8
|
-
|
|
9
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
10
|
-
|
|
11
|
-
const baseMerge__default = /*#__PURE__*/_interopDefaultCompat(baseMerge);
|
|
12
|
-
const lodash_clonedeep__default = /*#__PURE__*/_interopDefaultCompat(lodash_clonedeep);
|
|
13
|
-
const makeDebugger__default = /*#__PURE__*/_interopDefaultCompat(makeDebugger);
|
|
14
|
-
|
|
15
|
-
function hasFiles(data) {
|
|
16
|
-
if (!data) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
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));
|
|
20
|
-
}
|
|
21
|
-
function objectToFormData(source, form, parentKey) {
|
|
22
|
-
source ??= {};
|
|
23
|
-
form ??= new FormData();
|
|
24
|
-
if (typeof source !== "object" || source instanceof Set || Array.isArray(source) || source instanceof Blob || source instanceof Date || source instanceof FormData) {
|
|
25
|
-
throw new TypeError("Source must be an object literal to be converted to a FormData object.");
|
|
26
|
-
}
|
|
27
|
-
for (const key in source) {
|
|
28
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
29
|
-
append(form, composeKey(key, parentKey), source[key]);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return form;
|
|
33
|
-
}
|
|
34
|
-
function composeKey(key, parentKey) {
|
|
35
|
-
return parentKey ? `${parentKey}[${key}]` : key;
|
|
36
|
-
}
|
|
37
|
-
function append(form, key, value) {
|
|
38
|
-
if (Array.isArray(value) || value instanceof Set) {
|
|
39
|
-
const valueAsArray = value instanceof Set ? [...value] : value;
|
|
40
|
-
if (!valueAsArray.length) {
|
|
41
|
-
return form.append(key, "");
|
|
42
|
-
}
|
|
43
|
-
return Array.from(valueAsArray.keys()).forEach((index) => append(form, composeKey(index.toString(), key), valueAsArray[index]));
|
|
44
|
-
} else if (value instanceof Date) {
|
|
45
|
-
return form.append(key, value.toISOString());
|
|
46
|
-
} else if (value instanceof File) {
|
|
47
|
-
return form.append(key, value, value.name);
|
|
48
|
-
} else if (value instanceof Blob) {
|
|
49
|
-
return form.append(key, value);
|
|
50
|
-
} else if (typeof value === "boolean") {
|
|
51
|
-
return form.append(key, value ? "1" : "0");
|
|
52
|
-
} else if (typeof value === "string") {
|
|
53
|
-
return form.append(key, value);
|
|
54
|
-
} else if (typeof value === "number") {
|
|
55
|
-
return form.append(key, `${value}`);
|
|
56
|
-
} else if (value === null || value === void 0) {
|
|
57
|
-
return form.append(key, "");
|
|
58
|
-
}
|
|
59
|
-
objectToFormData(value, form, key);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const stack = [];
|
|
63
|
-
class Modal {
|
|
64
|
-
constructor(html, id) {
|
|
65
|
-
this.html = html;
|
|
66
|
-
this.id = id;
|
|
67
|
-
if (stack.includes(id)) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (this.initializeDOM() === false) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
this.show();
|
|
74
|
-
}
|
|
75
|
-
main;
|
|
76
|
-
overlay;
|
|
77
|
-
iframe;
|
|
78
|
-
style;
|
|
79
|
-
hideOnEscape;
|
|
80
|
-
animationDurationInMs = 200;
|
|
81
|
-
static fromException(response, id) {
|
|
82
|
-
if (typeof response === "string" && response.trim() !== "") {
|
|
83
|
-
return new Modal(`<style>${htmlStyle()}</style>${response.toString()}`, id);
|
|
84
|
-
}
|
|
85
|
-
return new Modal(`
|
|
86
|
-
<style>${style()}</style>
|
|
87
|
-
<div class="h-full text-center flex">
|
|
88
|
-
<div class="m-auto">
|
|
89
|
-
<div class="text-5xl font-thin">Error</div>
|
|
90
|
-
<div class="opacity-30 text-lg font-thin m-1">The received response does not respect the Hybridly protocol.</div>
|
|
91
|
-
<pre class="text-sm opacity-80 max-h-[500px] w-full mx-auto text-left mt-6">${JSON.stringify(response, null, 2)}</pre>
|
|
92
|
-
</div>
|
|
93
|
-
</div>
|
|
94
|
-
`, id);
|
|
95
|
-
}
|
|
96
|
-
static forViewComponent(component, id) {
|
|
97
|
-
return new Modal(`
|
|
98
|
-
<style>${style()}</style>
|
|
99
|
-
<div class="h-full text-center flex">
|
|
100
|
-
<div class="m-auto">
|
|
101
|
-
<div class="text-5xl font-thin">Error</div>
|
|
102
|
-
<div class="opacity-30 text-lg font-thin m-1">The specified view component does not exist.</div>
|
|
103
|
-
<div class="m-2 flex justify-center text-xl opacity-30 underline underline-dotted">${component}</div>
|
|
104
|
-
</div>
|
|
105
|
-
</div>
|
|
106
|
-
`, id);
|
|
107
|
-
}
|
|
108
|
-
initializeDOM() {
|
|
109
|
-
if (!this.html) {
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
if (document.querySelector('[data-hybridly-overlay="true"]')) {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
const main = document.createElement("html");
|
|
116
|
-
main.innerHTML = this.html;
|
|
117
|
-
main.querySelectorAll("a").forEach((a) => a.setAttribute("target", "_top"));
|
|
118
|
-
const overlay = document.createElement("div");
|
|
119
|
-
overlay.dataset.hybridly = "";
|
|
120
|
-
overlay.style.position = "fixed";
|
|
121
|
-
overlay.style.width = "100vw";
|
|
122
|
-
overlay.style.height = "100vh";
|
|
123
|
-
overlay.style.padding = "50px";
|
|
124
|
-
overlay.style.boxSizing = "border-box";
|
|
125
|
-
overlay.style.backgroundColor = "rgba(0, 0, 0, .35)";
|
|
126
|
-
overlay.style.color = "white";
|
|
127
|
-
overlay.style.zIndex = "2147483638";
|
|
128
|
-
overlay.style.overflow = "hidden";
|
|
129
|
-
const iframe = document.createElement("iframe");
|
|
130
|
-
iframe.style.backgroundColor = "#050505";
|
|
131
|
-
iframe.style.width = "100%";
|
|
132
|
-
iframe.style.height = "100%";
|
|
133
|
-
iframe.style.borderRadius = "10px";
|
|
134
|
-
overlay.appendChild(iframe);
|
|
135
|
-
const style2 = document.createElement("style");
|
|
136
|
-
style2.innerHTML = `
|
|
137
|
-
[data-hybridly] {
|
|
138
|
-
opacity: 0;
|
|
139
|
-
overflow: hidden;
|
|
140
|
-
transition: opacity ${this.animationDurationInMs}ms ease-out;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
[data-hybridly="visible"] {
|
|
144
|
-
opacity: 1;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
[data-hybridly] iframe {
|
|
148
|
-
box-shadow: 0px 10px 35px 5px rgba(0,0,0,0.2);
|
|
149
|
-
opacity: 0;
|
|
150
|
-
overflow: hidden;
|
|
151
|
-
transform: scale(.85);
|
|
152
|
-
transition: all 100ms ease-out;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
[data-hybridly="visible"] iframe {
|
|
156
|
-
transform: scale(1);
|
|
157
|
-
opacity: 1;
|
|
158
|
-
}
|
|
159
|
-
`;
|
|
160
|
-
this.main = main;
|
|
161
|
-
this.overlay = overlay;
|
|
162
|
-
this.iframe = iframe;
|
|
163
|
-
this.style = style2;
|
|
164
|
-
}
|
|
165
|
-
show() {
|
|
166
|
-
stack.push(this.id);
|
|
167
|
-
this.overlay.addEventListener("click", () => this.destroy());
|
|
168
|
-
this.hideOnEscape = (event) => {
|
|
169
|
-
if (event.keyCode === 27) {
|
|
170
|
-
this.destroy();
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
document.addEventListener("keydown", this.hideOnEscape);
|
|
174
|
-
document.body.prepend(this.style);
|
|
175
|
-
document.body.prepend(this.overlay);
|
|
176
|
-
this.iframe.contentWindow?.document.open();
|
|
177
|
-
this.iframe.contentWindow?.document.write(this.main.outerHTML);
|
|
178
|
-
this.iframe.contentWindow?.document.close();
|
|
179
|
-
this.overlay.dataset.hybridly = "visible";
|
|
180
|
-
}
|
|
181
|
-
destroy() {
|
|
182
|
-
stack.splice(stack.indexOf(this.html), 1);
|
|
183
|
-
this.overlay.dataset.hybridly = "";
|
|
184
|
-
setTimeout(() => {
|
|
185
|
-
this.overlay.outerHTML = "";
|
|
186
|
-
this.overlay.remove();
|
|
187
|
-
this.style.remove();
|
|
188
|
-
document.removeEventListener("keydown", this.hideOnEscape);
|
|
189
|
-
}, this.animationDurationInMs);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
function showResponseErrorModal(response) {
|
|
193
|
-
return Modal.fromException(response, "non-hybrid-response");
|
|
194
|
-
}
|
|
195
|
-
function showViewComponentErrorModal(response) {
|
|
196
|
-
return Modal.forViewComponent(response, `view-component-${response}`);
|
|
197
|
-
}
|
|
198
|
-
function htmlStyle() {
|
|
199
|
-
return `
|
|
200
|
-
html {
|
|
201
|
-
background-color: #050505;
|
|
202
|
-
color: white;
|
|
203
|
-
font-family: ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji";
|
|
204
|
-
display: flex;
|
|
205
|
-
flex-direction: column;
|
|
206
|
-
height: 100%;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
a {
|
|
210
|
-
color: white;
|
|
211
|
-
}
|
|
212
|
-
`;
|
|
213
|
-
}
|
|
214
|
-
function style() {
|
|
215
|
-
return `
|
|
216
|
-
${htmlStyle()}
|
|
217
|
-
body {
|
|
218
|
-
padding: 5rem 2rem;
|
|
219
|
-
flex-grow: 1;
|
|
220
|
-
}
|
|
221
|
-
.m-1 {
|
|
222
|
-
margin: 0.25rem;
|
|
223
|
-
}
|
|
224
|
-
.m-2 {
|
|
225
|
-
margin: 0.5rem;
|
|
226
|
-
}
|
|
227
|
-
.mt-6 {
|
|
228
|
-
margin-top: 1.5rem;
|
|
229
|
-
}
|
|
230
|
-
.m-auto {
|
|
231
|
-
margin: auto;
|
|
232
|
-
}
|
|
233
|
-
.h-full {
|
|
234
|
-
height: 100%;
|
|
235
|
-
}
|
|
236
|
-
.max-h-[500px] {
|
|
237
|
-
max-height: 500px;
|
|
238
|
-
}
|
|
239
|
-
.w-full {
|
|
240
|
-
width: 100%;
|
|
241
|
-
}
|
|
242
|
-
.flex {
|
|
243
|
-
display: flex;
|
|
244
|
-
}
|
|
245
|
-
.justify-center {
|
|
246
|
-
justify-content: center;
|
|
247
|
-
}
|
|
248
|
-
.text-center {
|
|
249
|
-
text-align: center;
|
|
250
|
-
}
|
|
251
|
-
.text-left {
|
|
252
|
-
text-align: left;
|
|
253
|
-
}
|
|
254
|
-
.text-5xl {
|
|
255
|
-
font-size: 3rem;
|
|
256
|
-
line-height: 1;
|
|
257
|
-
}
|
|
258
|
-
.text-lg {
|
|
259
|
-
font-size: 1.125rem;
|
|
260
|
-
line-height: 1.75rem;
|
|
261
|
-
}
|
|
262
|
-
.text-xl {
|
|
263
|
-
font-size: 1.25rem;
|
|
264
|
-
line-height: 1.75rem;
|
|
265
|
-
}
|
|
266
|
-
.text-sm {
|
|
267
|
-
font-size: 0.875rem;
|
|
268
|
-
line-height: 1.25rem;
|
|
269
|
-
}
|
|
270
|
-
.font-thin {
|
|
271
|
-
font-weight: 100;
|
|
272
|
-
}
|
|
273
|
-
.underline {
|
|
274
|
-
text-decoration-line: underline;
|
|
275
|
-
}
|
|
276
|
-
.underline-dotted {
|
|
277
|
-
text-decoration-style: dotted;
|
|
278
|
-
}
|
|
279
|
-
.opacity-30 {
|
|
280
|
-
opacity: 0.3;
|
|
281
|
-
}
|
|
282
|
-
.opacity-80 {
|
|
283
|
-
opacity: 0.8;
|
|
284
|
-
}
|
|
285
|
-
`;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function random(length = 10) {
|
|
289
|
-
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
290
|
-
let str = "";
|
|
291
|
-
for (let i = 0; i < length; i++) {
|
|
292
|
-
str += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
293
|
-
}
|
|
294
|
-
return str;
|
|
295
|
-
}
|
|
296
|
-
function match(value2, lookup, ...args) {
|
|
297
|
-
if (value2 in lookup || "default" in lookup) {
|
|
298
|
-
const returnValue = value2 in lookup ? lookup[value2] : lookup.default;
|
|
299
|
-
return typeof returnValue === "function" ? returnValue(...args) : returnValue;
|
|
300
|
-
}
|
|
301
|
-
const handlers = Object.keys(lookup).map((key) => `"${key}"`).join(", ");
|
|
302
|
-
throw new Error(`Tried to handle "${value2}" but there is no handler defined. Only defined handlers are: ${handlers}.`);
|
|
303
|
-
}
|
|
304
|
-
function value(value2) {
|
|
305
|
-
if (typeof value2 === "function") {
|
|
306
|
-
return value2?.();
|
|
307
|
-
}
|
|
308
|
-
return value2;
|
|
309
|
-
}
|
|
310
|
-
function when(condition, data, _default) {
|
|
311
|
-
if (!condition) {
|
|
312
|
-
return _default;
|
|
313
|
-
}
|
|
314
|
-
return data;
|
|
315
|
-
}
|
|
316
|
-
function merge(x, y, options = {}) {
|
|
317
|
-
const arrayMerge = typeof options?.arrayMerge === "function" ? options.arrayMerge : options?.overwriteArray !== false ? (_, s) => s : void 0;
|
|
318
|
-
const isMergeableObject = options?.mergePlainObjects ? isPlainObject.isPlainObject : void 0;
|
|
319
|
-
return baseMerge__default(x, y, {
|
|
320
|
-
arrayMerge,
|
|
321
|
-
isMergeableObject
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
function removeTrailingSlash(string) {
|
|
325
|
-
return string.replace(/\/+$/, "");
|
|
326
|
-
}
|
|
327
|
-
function setValueAtPath(obj, path, value2) {
|
|
328
|
-
if (!path.includes(".")) {
|
|
329
|
-
obj[path] = value2;
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
const segments = path.split(".");
|
|
333
|
-
let nestedObject = obj;
|
|
334
|
-
for (let i = 0; i < segments.length - 1; i++) {
|
|
335
|
-
const key = segments[i];
|
|
336
|
-
nestedObject = nestedObject[key] = nestedObject[key] || {};
|
|
337
|
-
}
|
|
338
|
-
nestedObject[segments[segments.length - 1]] = value2;
|
|
339
|
-
}
|
|
340
|
-
function unsetPropertyAtPath(obj, path) {
|
|
341
|
-
if (!path.includes(".")) {
|
|
342
|
-
delete obj[path];
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
const segments = path.split(".");
|
|
346
|
-
let nestedObject = obj;
|
|
347
|
-
for (let i = 0; i < segments.length - 1; i++) {
|
|
348
|
-
const key = segments[i];
|
|
349
|
-
nestedObject = nestedObject[key] = nestedObject[key] || {};
|
|
350
|
-
}
|
|
351
|
-
delete nestedObject[segments[segments.length - 1]];
|
|
352
|
-
if (Object.keys(nestedObject).length === 0) {
|
|
353
|
-
unsetPropertyAtPath(obj, segments.slice(0, -1).join("."));
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const debug = {
|
|
358
|
-
router: makeDebugger__default("hybridly:core:router"),
|
|
359
|
-
history: makeDebugger__default("hybridly:core:history"),
|
|
360
|
-
url: makeDebugger__default("hybridly:core:url"),
|
|
361
|
-
context: makeDebugger__default("hybridly:core:context"),
|
|
362
|
-
external: makeDebugger__default("hybridly:core:external"),
|
|
363
|
-
scroll: makeDebugger__default("hybridly:core:scroll"),
|
|
364
|
-
hook: makeDebugger__default("hybridly:core:hook"),
|
|
365
|
-
plugin: (name, ...args) => makeDebugger__default("hybridly:plugin").extend(name.replace("hybridly:", ""))(args.shift(), ...args),
|
|
366
|
-
adapter: (name, ...args) => makeDebugger__default("hybridly:adapter").extend(name)(args.shift(), ...args)
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
exports.debounce = throttleDebounce.debounce;
|
|
370
|
-
exports.throttle = throttleDebounce.throttle;
|
|
371
|
-
exports.clone = lodash_clonedeep__default;
|
|
372
|
-
exports.debug = debug;
|
|
373
|
-
exports.hasFiles = hasFiles;
|
|
374
|
-
exports.match = match;
|
|
375
|
-
exports.merge = merge;
|
|
376
|
-
exports.objectToFormData = objectToFormData;
|
|
377
|
-
exports.random = random;
|
|
378
|
-
exports.removeTrailingSlash = removeTrailingSlash;
|
|
379
|
-
exports.setValueAtPath = setValueAtPath;
|
|
380
|
-
exports.showResponseErrorModal = showResponseErrorModal;
|
|
381
|
-
exports.showViewComponentErrorModal = showViewComponentErrorModal;
|
|
382
|
-
exports.unsetPropertyAtPath = unsetPropertyAtPath;
|
|
383
|
-
exports.value = value;
|
|
384
|
-
exports.when = when;
|
package/dist/index.d.cts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import makeDebugger from 'debug';
|
|
2
|
-
export { debounce, throttle } from 'throttle-debounce';
|
|
3
|
-
export { default as clone } from 'lodash.clonedeep';
|
|
4
|
-
|
|
5
|
-
type RequestData = Record<string, FormDataConvertible> | FormDataConvertible | FormData;
|
|
6
|
-
type FormDataConvertible = {
|
|
7
|
-
[key: string]: FormDataConvertible;
|
|
8
|
-
} | Array<FormDataConvertible> | Set<FormDataConvertible> | Blob | File | FormDataEntryValue | Date | boolean | number | null | undefined | string;
|
|
9
|
-
/**
|
|
10
|
-
* Checks if the given object has a file.
|
|
11
|
-
*/
|
|
12
|
-
declare function hasFiles(data?: RequestData): boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Converts an object literal to a `FormData` object.
|
|
15
|
-
*/
|
|
16
|
-
declare function objectToFormData(source?: RequestData, form?: FormData, parentKey?: string): FormData;
|
|
17
|
-
|
|
18
|
-
declare class Modal {
|
|
19
|
-
private html;
|
|
20
|
-
private id;
|
|
21
|
-
private main;
|
|
22
|
-
private overlay;
|
|
23
|
-
private iframe;
|
|
24
|
-
private style;
|
|
25
|
-
private hideOnEscape?;
|
|
26
|
-
private animationDurationInMs;
|
|
27
|
-
constructor(html: string, id: string);
|
|
28
|
-
static fromException(response: string, id: string): Modal;
|
|
29
|
-
static forViewComponent(component: string, id: string): Modal;
|
|
30
|
-
initializeDOM(): false | undefined;
|
|
31
|
-
show(): void;
|
|
32
|
-
destroy(): void;
|
|
33
|
-
}
|
|
34
|
-
declare function showResponseErrorModal(response: string): Modal;
|
|
35
|
-
declare function showViewComponentErrorModal(response: string): Modal;
|
|
36
|
-
|
|
37
|
-
declare function random(length?: number): string;
|
|
38
|
-
/** Simple pattern matching util. */
|
|
39
|
-
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>;
|
|
40
|
-
declare function value<T>(value: T | (() => T)): T;
|
|
41
|
-
declare function when<T, D>(condition: any, data: T, _default?: D): T | D | undefined;
|
|
42
|
-
interface MergeOptions {
|
|
43
|
-
overwriteArray?: boolean;
|
|
44
|
-
mergePlainObjects?: boolean;
|
|
45
|
-
arrayMerge?: (target: any[], source: any[]) => any[];
|
|
46
|
-
}
|
|
47
|
-
declare function merge<T>(x: Partial<T>, y: Partial<T>, options?: MergeOptions): T;
|
|
48
|
-
declare function removeTrailingSlash(string: string): string;
|
|
49
|
-
/**
|
|
50
|
-
* Sets a value at a path in an object
|
|
51
|
-
*
|
|
52
|
-
* This function will set a value at a path in an object, creating any missing
|
|
53
|
-
* objects along the way. The object is modified in place.
|
|
54
|
-
*
|
|
55
|
-
* @param obj the object to set the value in
|
|
56
|
-
* @param path a dot-separated path to the property to set
|
|
57
|
-
* @param value the value to set
|
|
58
|
-
*/
|
|
59
|
-
declare function setValueAtPath(obj: any, path: string, value: any): void;
|
|
60
|
-
/**
|
|
61
|
-
* Unsets a property at a path in an object
|
|
62
|
-
*
|
|
63
|
-
* This function will unset a property at a path in an object, deleting any
|
|
64
|
-
* objects along the way that are empty. The object is modified in place.
|
|
65
|
-
*
|
|
66
|
-
* @param obj the object to unset the property in
|
|
67
|
-
* @param path a dot-separated path to the property to unset
|
|
68
|
-
*/
|
|
69
|
-
declare function unsetPropertyAtPath(obj: any, path: string): void;
|
|
70
|
-
|
|
71
|
-
declare const debug: {
|
|
72
|
-
router: makeDebugger.Debugger;
|
|
73
|
-
history: makeDebugger.Debugger;
|
|
74
|
-
url: makeDebugger.Debugger;
|
|
75
|
-
context: makeDebugger.Debugger;
|
|
76
|
-
external: makeDebugger.Debugger;
|
|
77
|
-
scroll: makeDebugger.Debugger;
|
|
78
|
-
hook: makeDebugger.Debugger;
|
|
79
|
-
plugin: (name: string, ...args: any[]) => void;
|
|
80
|
-
adapter: (name: string, ...args: any[]) => void;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export { debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showResponseErrorModal, showViewComponentErrorModal, unsetPropertyAtPath, value, when };
|
|
84
|
-
export type { FormDataConvertible, RequestData };
|
package/dist/index.d.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import makeDebugger from 'debug';
|
|
2
|
-
export { debounce, throttle } from 'throttle-debounce';
|
|
3
|
-
export { default as clone } from 'lodash.clonedeep';
|
|
4
|
-
|
|
5
|
-
type RequestData = Record<string, FormDataConvertible> | FormDataConvertible | FormData;
|
|
6
|
-
type FormDataConvertible = {
|
|
7
|
-
[key: string]: FormDataConvertible;
|
|
8
|
-
} | Array<FormDataConvertible> | Set<FormDataConvertible> | Blob | File | FormDataEntryValue | Date | boolean | number | null | undefined | string;
|
|
9
|
-
/**
|
|
10
|
-
* Checks if the given object has a file.
|
|
11
|
-
*/
|
|
12
|
-
declare function hasFiles(data?: RequestData): boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Converts an object literal to a `FormData` object.
|
|
15
|
-
*/
|
|
16
|
-
declare function objectToFormData(source?: RequestData, form?: FormData, parentKey?: string): FormData;
|
|
17
|
-
|
|
18
|
-
declare class Modal {
|
|
19
|
-
private html;
|
|
20
|
-
private id;
|
|
21
|
-
private main;
|
|
22
|
-
private overlay;
|
|
23
|
-
private iframe;
|
|
24
|
-
private style;
|
|
25
|
-
private hideOnEscape?;
|
|
26
|
-
private animationDurationInMs;
|
|
27
|
-
constructor(html: string, id: string);
|
|
28
|
-
static fromException(response: string, id: string): Modal;
|
|
29
|
-
static forViewComponent(component: string, id: string): Modal;
|
|
30
|
-
initializeDOM(): false | undefined;
|
|
31
|
-
show(): void;
|
|
32
|
-
destroy(): void;
|
|
33
|
-
}
|
|
34
|
-
declare function showResponseErrorModal(response: string): Modal;
|
|
35
|
-
declare function showViewComponentErrorModal(response: string): Modal;
|
|
36
|
-
|
|
37
|
-
declare function random(length?: number): string;
|
|
38
|
-
/** Simple pattern matching util. */
|
|
39
|
-
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>;
|
|
40
|
-
declare function value<T>(value: T | (() => T)): T;
|
|
41
|
-
declare function when<T, D>(condition: any, data: T, _default?: D): T | D | undefined;
|
|
42
|
-
interface MergeOptions {
|
|
43
|
-
overwriteArray?: boolean;
|
|
44
|
-
mergePlainObjects?: boolean;
|
|
45
|
-
arrayMerge?: (target: any[], source: any[]) => any[];
|
|
46
|
-
}
|
|
47
|
-
declare function merge<T>(x: Partial<T>, y: Partial<T>, options?: MergeOptions): T;
|
|
48
|
-
declare function removeTrailingSlash(string: string): string;
|
|
49
|
-
/**
|
|
50
|
-
* Sets a value at a path in an object
|
|
51
|
-
*
|
|
52
|
-
* This function will set a value at a path in an object, creating any missing
|
|
53
|
-
* objects along the way. The object is modified in place.
|
|
54
|
-
*
|
|
55
|
-
* @param obj the object to set the value in
|
|
56
|
-
* @param path a dot-separated path to the property to set
|
|
57
|
-
* @param value the value to set
|
|
58
|
-
*/
|
|
59
|
-
declare function setValueAtPath(obj: any, path: string, value: any): void;
|
|
60
|
-
/**
|
|
61
|
-
* Unsets a property at a path in an object
|
|
62
|
-
*
|
|
63
|
-
* This function will unset a property at a path in an object, deleting any
|
|
64
|
-
* objects along the way that are empty. The object is modified in place.
|
|
65
|
-
*
|
|
66
|
-
* @param obj the object to unset the property in
|
|
67
|
-
* @param path a dot-separated path to the property to unset
|
|
68
|
-
*/
|
|
69
|
-
declare function unsetPropertyAtPath(obj: any, path: string): void;
|
|
70
|
-
|
|
71
|
-
declare const debug: {
|
|
72
|
-
router: makeDebugger.Debugger;
|
|
73
|
-
history: makeDebugger.Debugger;
|
|
74
|
-
url: makeDebugger.Debugger;
|
|
75
|
-
context: makeDebugger.Debugger;
|
|
76
|
-
external: makeDebugger.Debugger;
|
|
77
|
-
scroll: makeDebugger.Debugger;
|
|
78
|
-
hook: makeDebugger.Debugger;
|
|
79
|
-
plugin: (name: string, ...args: any[]) => void;
|
|
80
|
-
adapter: (name: string, ...args: any[]) => void;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export { debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showResponseErrorModal, showViewComponentErrorModal, unsetPropertyAtPath, value, when };
|
|
84
|
-
export type { FormDataConvertible, RequestData };
|