-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
59 lines (59 loc) · 1.97 KB
/
utilities.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
const numberRegExp = /^-?(?:\d*\.)?\d+$/;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function cleanContentJson(contentJson) {
delete contentJson.$;
if (Object.keys(contentJson).length === 1 && contentJson._ !== undefined) {
contentJson = contentJson._;
}
for (const key of Object.keys(contentJson)) {
if (Array.isArray(contentJson[key]) &&
contentJson[key].length === 1 &&
key !== 'IntervalBlock') {
contentJson[key] = contentJson[key][0];
}
if (typeof contentJson[key] === 'string' &&
contentJson[key].length > 0 &&
numberRegExp.test(contentJson[key])) {
contentJson[key] = Number(contentJson[key]);
}
else if (typeof contentJson[key] === 'object') {
cleanContentJson(contentJson[key]);
}
}
}
export function atomLinksToGreenButtonLinks(atomLinks, includeSelfUid) {
const linksJson = {
related: []
};
for (const atomLink of atomLinks ?? []) {
if (atomLink.$.rel === 'related') {
linksJson.related?.push(atomLink.$.href);
}
else {
linksJson[atomLink.$.rel] = atomLink.$.href;
}
}
if (includeSelfUid && linksJson.self !== undefined) {
linksJson.selfUid = linksJson.self.slice(linksJson.self.lastIndexOf('/') + 1);
}
if (linksJson.related?.length === 0) {
delete linksJson.related;
}
return linksJson;
}
export function getFirstXmlString(xmlProperty) {
if (xmlProperty === undefined || xmlProperty.length === 0) {
return '';
}
const first = xmlProperty[0];
if (typeof first === 'string') {
return first;
}
return first._;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function ensureArray(object, objectKey) {
if (object[objectKey] !== undefined && !Array.isArray(object[objectKey])) {
object[objectKey] = [object[objectKey]];
}
}