-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.ts
83 lines (67 loc) · 2.1 KB
/
utilities.ts
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
import type { AtomJsonLink } from './types/atomTypes.js'
import type { GreenButtonLinks } from './types/entryTypes.js'
const numberRegExp = /^-?(?:\d*\.)?\d+$/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function cleanContentJson(contentJson: any): void {
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: AtomJsonLink[],
includeSelfUid: boolean
): GreenButtonLinks {
const linksJson: GreenButtonLinks = {
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
}
type XmlProperty = string[] | Array<{ _: string }>
export function getFirstXmlString(xmlProperty?: XmlProperty): string {
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: any, objectKey: string): void {
if (object[objectKey] !== undefined && !Array.isArray(object[objectKey])) {
object[objectKey] = [object[objectKey]]
}
}