-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
154 lines (124 loc) · 3.51 KB
/
utils.js
File metadata and controls
154 lines (124 loc) · 3.51 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
export function debounce(fn, ms = 500) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), ms);
};
}
export function validateUrl(url, base = false) {
try {
url = new URL(url);
} catch {
return false;
}
if (base === true && (url.hash !== "" || url.search !== "")) {
return false;
}
if (/[^-a-zA-Z0-9._+]+/.test(url.hostname)) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
export function validateBackend(data) {
if (!data?.name) return false;
let isValid = validateUrl(data.base_url) && Array.isArray(data.headers);
if (!isValid) return false;
const contentType = data.headers.find(
({ name }) => name === "Content-Type"
)?.value;
if (!contentType) {
return false;
}
return isValid;
}
export function sortByNamesOrder(items, order) {
return items.sort((a, b) => {
if (!order.includes(a.name)) {
return 1;
} else if (!order.includes(b.name)) {
return -1;
} else {
return order.indexOf(a.name) - order.indexOf(b.name);
}
});
}
export function prependEmptyOption(options) {
if (options[0]?.value === "") return options;
return [{ label: "", value: "" }].concat(options);
}
export function downloadJson(data, fileName = "forms-bridge-export") {
const json = JSON.stringify(data);
const blob = new Blob([json], { type: "application/json" });
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = fileName + ".json";
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}
export function uploadJson() {
return new Promise((res, rej) => {
const input = document.createElement("input");
input.type = "file";
input.accept = "application/json";
input.addEventListener("cancel", function () {
document.body.removeChild(input);
rej();
});
input.addEventListener("change", function () {
if (input.files.length === 1) {
const reader = new FileReader();
reader.onerror = function (err) {
document.body.removeChild(input);
rej(err);
};
reader.onload = function () {
let data;
try {
data = JSON.parse(reader.result);
res(data);
} catch (err) {
rej(err);
} finally {
document.body.removeChild(input);
}
};
reader.readAsText(input.files[0]);
} else {
document.body.removeChild(input);
}
});
document.body.appendChild(input);
input.click();
});
}
export function defrost(obj) {
if (obj === null || typeof obj !== "object") return obj;
if (Array.isArray(obj)) {
return [...obj];
}
return { ...obj };
}
export function isset(obj, attr) {
if (!obj || typeof obj !== "object") {
return false;
}
if (Array.isArray(obj)) {
return obj.length > attr;
}
return Object.prototype.hasOwnProperty.call(obj, attr);
}
export function adminUrl(path = "", query = {}) {
/* global wpApiSettings */
const url = new URL(wpApiSettings.root.replace(/wp-json/, "wp-admin"));
url.pathname += path.replace(/^\/+/, "");
url.search = new URLSearchParams(query).toString();
return url.toString();
}
export function restUrl(path = "", query = {}) {
const url = new URL(wpApiSettings.root);
url.pathname += path.replace(/^\/+/, "");
url.search = new URLSearchParams(query).toString();
return url.toString();
}