-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutil.js
422 lines (394 loc) · 9.91 KB
/
util.js
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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
import { direction } from "direction";
/**
* @param {Object} td
* @returns {boolean}
*
* @description
* isThingModel takes an object as argument and checks wether
* it is a TD or a TM. If anything, but an object is passed,
* the function will return false by default.
*/
export const isThingModel = (td) => {
if (!(typeof td === "object" && !Array.isArray(td) && td !== null)) {
return false;
}
if (!td.hasOwnProperty("@type")) {
return false;
}
return td["@type"].indexOf("tm:ThingModel") > -1;
};
/**
*
* @param {*} firstAttribute
* @param {*} object
* @param {*} dontRender is a list of keys that shouldn't be packed into the attribute list.
*
* @description
* Parses all key-value pairs of an object into an object'.
*
*/
export const buildAttributeListObject = (
firstAttribute,
object,
dontRender
) => {
let attributeListObject = { ...firstAttribute };
for (const [key, value] of Object.entries(object)) {
if (!dontRender.includes(key)) {
attributeListObject[key] = value;
}
}
return attributeListObject;
};
/**
*
* @param {*} forms
*
* @description
* Converts Forms that have an array as the "op" value into multiple separate Forms
* which only have a string as "op" value.
*/
export const separateForms = (forms) => {
if (forms === undefined && !forms) {
return [];
}
const newForms = [];
for (let i = 0; i < forms.length; i++) {
const form = forms[i];
if (!Array.isArray(form.op)) {
form.actualIndex = i;
newForms.push(form);
continue;
}
for (let j = 0; j < form.op.length; j++) {
const temp = { ...form };
temp.op = form.op[j];
temp.actualIndex = i;
newForms.push(temp);
}
}
return newForms;
};
/**
*Check if link exists in the links section of iteamToCheck
*/
export const checkIfLinkIsInItem = (link, itemToCheck) => {
for (const element of itemToCheck.links) {
if (element.href === link.href) {
return true;
}
}
return false;
};
export const checkIfFormIsInItem = (form, itemToCheck) => {
for (const element of itemToCheck.forms) {
if (typeof form.op === "string") {
return checkIfFormIsInElement(form, element);
} else {
for (const x of form.op) {
if (typeof element.op === "string") {
if (element.op === x) {
return true;
}
} else {
if (element.op.includes(x)) {
let deepCompare = true;
for (const y in form) {
if (y !== "op") {
if (element[y] !== form[y]) {
deepCompare = false;
}
}
}
if (deepCompare) return true;
}
}
}
}
}
return false;
};
const checkIfFormIsInElement = (form, element) => {
if (typeof element.op === "string") {
if (element.op === form.op) {
return true;
}
} else {
if (element.op.includes(form.op)) {
let deepCompare = true;
for (const y in form) {
if (y !== "op") {
if (element[y] !== form[y]) {
deepCompare = false;
}
}
}
if (deepCompare) return true;
}
}
};
/**
* Display the selected Thing description
* Save the current Thing Description if wanted
* Method supports both fileHandler and jsonld file
*/
export const changeBetweenTd = async (context, href) => {
var writable;
if (context.linkedTd[href]["kind"] === "file") {
try {
if (context.isModified && context.fileHandle) {
writable = await context.fileHandle.createWritable();
await writable.write(context.offlineTD);
await writable.close();
}
} catch (e) {
console.error(e.message);
}
let fileHandle = context.linkedTd[href];
const file = await fileHandle.getFile();
const td = JSON.parse(await file.text());
let offlineTd = JSON.stringify(td, null, 2);
context.setFileHandle(fileHandle);
context.updateOfflineTD(offlineTd);
context.updateIsModified(false);
document.getElementById("linkedTd").value = href;
}
// If we create a TD using the New button then we don't have a file handler
// In that case the entry in linkedTd is not a file handler but a Thing Description Json
else if (Object.keys(context.linkedTd[href]).length) {
try {
if (context.isModified && context.fileHandle) {
writable = await context.fileHandle.createWritable();
await writable.write(context.offlineTD);
await writable.close();
}
} catch (e) {
console.error(e.message);
}
context.setFileHandle(undefined);
const td = context.linkedTd[href];
let offlineTd = JSON.stringify(td, null, 2);
context.updateOfflineTD(offlineTd);
context.updateIsModified(false);
document.getElementById("linkedTd").value = href;
}
};
/**
* @param {*} source Source object
* @param {string} key Source key
* @param {*} atContext Respective @context value
*
* @returns {string} String value of source[key] with prepended LRI or RLI symbol
*
* @description
* Returns the value of source[key] with the direction information (rtl/ltr).
*/
export const getDirectedValue = (source, key, atContext) => {
// if there is no value to be directed this function returns
// to prevent any further call on undefined errors.
if (!source[key]) {
return "";
}
const LRI = "\u2066";
const RLI = "\u2067";
const TABLE = {
ar: "rtl",
fa: "rtl",
ps: "rtl",
ur: "rtl",
hy: "ltr",
as: "ltr",
bn: "ltr",
zb: "ltr",
ab: "ltr",
be: "ltr",
bg: "ltr",
kk: "ltr",
mk: "ltr",
ru: "ltr",
uk: "ltr",
hi: "ltr",
mr: "ltr",
ne: "ltr",
ko: "ltr",
ma: "ltr",
am: "ltr",
ti: "ltr",
ka: "ltr",
el: "ltr",
gu: "ltr",
pa: "ltr",
he: "rtl",
iw: "rtl",
yi: "rtl",
ja: "ltr",
km: "ltr",
kn: "ltr",
lo: "ltr",
af: "ltr",
ay: "ltr",
bs: "ltr",
ca: "ltr",
ch: "ltr",
cs: "ltr",
cy: "ltr",
da: "ltr",
de: "ltr",
en: "ltr",
eo: "ltr",
es: "ltr",
et: "ltr",
eu: "ltr",
fi: "ltr",
fj: "ltr",
fo: "ltr",
fr: "ltr",
fy: "ltr",
ga: "ltr",
gl: "ltr",
gn: "ltr",
gv: "ltr",
hr: "ltr",
ht: "ltr",
hu: "ltr",
id: "ltr",
in: "ltr",
is: "ltr",
it: "ltr",
kl: "ltr",
la: "ltr",
lb: "ltr",
ln: "ltr",
lt: "ltr",
lv: "ltr",
mg: "ltr",
mh: "ltr",
mo: "ltr",
ms: "ltr",
mt: "ltr",
na: "ltr",
nb: "ltr",
nd: "ltr",
nl: "ltr",
nn: "ltr",
no: "ltr",
nr: "ltr",
ny: "ltr",
om: "ltr",
pl: "ltr",
pt: "ltr",
qu: "ltr",
rm: "ltr",
rn: "ltr",
ro: "ltr",
rw: "ltr",
sg: "ltr",
sk: "ltr",
sl: "ltr",
sm: "ltr",
so: "ltr",
sq: "ltr",
ss: "ltr",
st: "ltr",
sv: "ltr",
sw: "ltr",
tl: "ltr",
tn: "ltr",
to: "ltr",
tr: "ltr",
ts: "ltr",
ve: "ltr",
vi: "ltr",
xh: "ltr",
zu: "ltr",
ds: "ltr",
gs: "ltr",
hs: "ltr",
me: "ltr",
ni: "ltr",
ns: "ltr",
te: "ltr",
tk: "ltr",
tm: "ltr",
tp: "ltr",
tv: "ltr",
ml: "ltr",
my: "ltr",
nq: "ltr",
or: "ltr",
si: "ltr",
ta: "ltr",
dv: "rtl",
th: "ltr",
dz: "ltr",
};
const getDirectionSymbol = (dir) => (dir === "ltr" ? LRI : RLI);
// title, description and language tags (like "en" or "en-US") are treated differently
if (
!["title", "description"].includes(key) &&
!/^[A-Za-z]{2}(-[A-Za-z]{2})?$/.test(key)
) {
return getDirectionSymbol(direction(source[key].toString())) + source[key];
}
if (/^[A-Za-z]{2}(-[A-Za-z]{2})?$/.test(key)) {
// Language tags can be compound like ar-EG or en-US, split when needed
// Also, we ignore the case for language tags
const lookupKey = key.includes("-") ? key.split("-")[0] : key.toLowerCase();
const dir = TABLE[lookupKey];
if (dir) return getDirectionSymbol(dir) + source[key];
return getDirectionSymbol("ltr") + source[key];
}
let textDirection;
let lang;
if (!Array.isArray(atContext)) {
atContext = [atContext];
}
atContext.forEach((e) => {
if (typeof e === "object") {
if (e["@direction"]) textDirection = e["@direction"];
if (e["@language"]) lang = e["@language"];
}
});
if (key === "title" || key === "description") {
if (textDirection) return getDirectionSymbol(textDirection) + source[key];
if (lang) {
const lookupKey = lang.includes("-")
? lang.split("-")[0]
: lang.toLowerCase();
const dir = TABLE[lookupKey];
if (dir) return getDirectionSymbol(dir) + source[key];
return getDirectionSymbol("ltr") + source[key];
}
}
return getDirectionSymbol(direction(source[key].toString())) + source[key];
};
export const encodeBody = function encodeBody(
data,
encoding = "application/json"
) {
if (encoding === "application/x-www-form-urlencoded") {
let formBody = [];
for (const property in data) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(data[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
return formBody;
} else {
console.log("No contentType found in Form so default will be used.");
return JSON.stringify(data);
}
};