feedsmith 2.1.0 → 2.2.0
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/LICENSE +18 -4
- package/README.md +32 -11
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/common/config.cjs +161 -0
- package/dist/common/config.js +157 -0
- package/dist/common/parse.cjs +33 -0
- package/dist/common/parse.d.cts +23 -0
- package/dist/common/parse.d.ts +23 -0
- package/dist/common/parse.js +33 -0
- package/dist/common/types.d.cts +25 -0
- package/dist/common/types.d.ts +25 -0
- package/dist/common/utils.cjs +361 -0
- package/dist/common/utils.js +331 -0
- package/dist/feeds/atom/common/types.d.cts +104 -0
- package/dist/feeds/atom/common/types.d.ts +104 -0
- package/dist/feeds/atom/detect/index.cjs +13 -0
- package/dist/feeds/atom/detect/index.d.cts +4 -0
- package/dist/feeds/atom/detect/index.d.ts +4 -0
- package/dist/feeds/atom/detect/index.js +13 -0
- package/dist/feeds/atom/generate/config.cjs +10 -0
- package/dist/feeds/atom/generate/config.js +8 -0
- package/dist/feeds/atom/generate/index.cjs +14 -0
- package/dist/feeds/atom/generate/index.d.cts +7 -0
- package/dist/feeds/atom/generate/index.d.ts +7 -0
- package/dist/feeds/atom/generate/index.js +14 -0
- package/dist/feeds/atom/generate/utils.cjs +153 -0
- package/dist/feeds/atom/generate/utils.js +152 -0
- package/dist/feeds/atom/parse/config.cjs +74 -0
- package/dist/feeds/atom/parse/config.js +72 -0
- package/dist/feeds/atom/parse/index.cjs +16 -0
- package/dist/feeds/atom/parse/index.d.cts +7 -0
- package/dist/feeds/atom/parse/index.d.ts +7 -0
- package/dist/feeds/atom/parse/index.js +16 -0
- package/dist/feeds/atom/parse/utils.cjs +181 -0
- package/dist/feeds/atom/parse/utils.js +179 -0
- package/dist/feeds/json/common/types.d.cts +59 -0
- package/dist/feeds/json/common/types.d.ts +59 -0
- package/dist/feeds/json/detect/index.cjs +17 -0
- package/dist/feeds/json/detect/index.d.cts +4 -0
- package/dist/feeds/json/detect/index.d.ts +4 -0
- package/dist/feeds/json/detect/index.js +17 -0
- package/dist/feeds/json/generate/index.cjs +9 -0
- package/dist/feeds/json/generate/index.d.cts +7 -0
- package/dist/feeds/json/generate/index.d.ts +7 -0
- package/dist/feeds/json/generate/index.js +9 -0
- package/dist/feeds/json/generate/utils.cjs +20 -0
- package/dist/feeds/json/generate/utils.js +20 -0
- package/dist/feeds/json/parse/index.cjs +14 -0
- package/dist/feeds/json/parse/index.d.cts +7 -0
- package/dist/feeds/json/parse/index.d.ts +7 -0
- package/dist/feeds/json/parse/index.js +14 -0
- package/dist/feeds/json/parse/utils.cjs +91 -0
- package/dist/feeds/json/parse/utils.js +90 -0
- package/dist/feeds/rdf/common/types.d.cts +54 -0
- package/dist/feeds/rdf/common/types.d.ts +54 -0
- package/dist/feeds/rdf/detect/index.cjs +14 -0
- package/dist/feeds/rdf/detect/index.d.cts +4 -0
- package/dist/feeds/rdf/detect/index.d.ts +4 -0
- package/dist/feeds/rdf/detect/index.js +14 -0
- package/dist/feeds/rdf/parse/config.cjs +28 -0
- package/dist/feeds/rdf/parse/config.js +26 -0
- package/dist/feeds/rdf/parse/index.cjs +16 -0
- package/dist/feeds/rdf/parse/index.d.cts +7 -0
- package/dist/feeds/rdf/parse/index.d.ts +7 -0
- package/dist/feeds/rdf/parse/index.js +16 -0
- package/dist/feeds/rdf/parse/utils.cjs +80 -0
- package/dist/feeds/rdf/parse/utils.js +80 -0
- package/dist/feeds/rss/common/types.d.cts +129 -0
- package/dist/feeds/rss/common/types.d.ts +129 -0
- package/dist/feeds/rss/detect/index.cjs +13 -0
- package/dist/feeds/rss/detect/index.d.cts +4 -0
- package/dist/feeds/rss/detect/index.d.ts +4 -0
- package/dist/feeds/rss/detect/index.js +13 -0
- package/dist/feeds/rss/generate/config.cjs +10 -0
- package/dist/feeds/rss/generate/config.js +8 -0
- package/dist/feeds/rss/generate/index.cjs +14 -0
- package/dist/feeds/rss/generate/index.d.cts +7 -0
- package/dist/feeds/rss/generate/index.d.ts +7 -0
- package/dist/feeds/rss/generate/index.js +14 -0
- package/dist/feeds/rss/generate/utils.cjs +163 -0
- package/dist/feeds/rss/generate/utils.js +163 -0
- package/dist/feeds/rss/parse/config.cjs +53 -0
- package/dist/feeds/rss/parse/config.js +51 -0
- package/dist/feeds/rss/parse/index.cjs +16 -0
- package/dist/feeds/rss/parse/index.d.cts +7 -0
- package/dist/feeds/rss/parse/index.d.ts +7 -0
- package/dist/feeds/rss/parse/index.js +16 -0
- package/dist/feeds/rss/parse/utils.cjs +159 -0
- package/dist/feeds/rss/parse/utils.js +159 -0
- package/dist/index.cjs +29 -3619
- package/dist/index.d.cts +34 -0
- package/dist/index.d.ts +34 -1016
- package/dist/index.js +14 -3579
- package/dist/namespaces/atom/common/types.d.cts +10 -0
- package/dist/namespaces/atom/common/types.d.ts +10 -0
- package/dist/namespaces/atom/generate/utils.cjs +19 -0
- package/dist/namespaces/atom/generate/utils.js +18 -0
- package/dist/namespaces/atom/parse/utils.cjs +19 -0
- package/dist/namespaces/atom/parse/utils.js +18 -0
- package/dist/namespaces/content/common/types.d.cts +8 -0
- package/dist/namespaces/content/common/types.d.ts +8 -0
- package/dist/namespaces/content/generate/utils.cjs +10 -0
- package/dist/namespaces/content/generate/utils.js +10 -0
- package/dist/namespaces/content/parse/utils.cjs +10 -0
- package/dist/namespaces/content/parse/utils.js +10 -0
- package/dist/namespaces/dc/common/types.d.cts +24 -0
- package/dist/namespaces/dc/common/types.d.ts +24 -0
- package/dist/namespaces/dc/generate/utils.cjs +26 -0
- package/dist/namespaces/dc/generate/utils.js +26 -0
- package/dist/namespaces/dc/parse/utils.cjs +26 -0
- package/dist/namespaces/dc/parse/utils.js +26 -0
- package/dist/namespaces/dcterms/common/types.d.cts +64 -0
- package/dist/namespaces/dcterms/common/types.d.ts +64 -0
- package/dist/namespaces/dcterms/generate/utils.cjs +66 -0
- package/dist/namespaces/dcterms/generate/utils.js +66 -0
- package/dist/namespaces/dcterms/parse/utils.cjs +66 -0
- package/dist/namespaces/dcterms/parse/utils.js +66 -0
- package/dist/namespaces/feedpress/common/types.d.cts +12 -0
- package/dist/namespaces/feedpress/common/types.d.ts +12 -0
- package/dist/namespaces/feedpress/generate/utils.cjs +16 -0
- package/dist/namespaces/feedpress/generate/utils.js +16 -0
- package/dist/namespaces/feedpress/parse/utils.cjs +16 -0
- package/dist/namespaces/feedpress/parse/utils.js +16 -0
- package/dist/namespaces/georss/common/types.d.cts +31 -0
- package/dist/namespaces/georss/common/types.d.ts +31 -0
- package/dist/namespaces/georss/generate/utils.cjs +51 -0
- package/dist/namespaces/georss/generate/utils.js +51 -0
- package/dist/namespaces/georss/parse/utils.cjs +65 -0
- package/dist/namespaces/georss/parse/utils.js +65 -0
- package/dist/namespaces/itunes/common/types.d.cts +49 -0
- package/dist/namespaces/itunes/common/types.d.ts +49 -0
- package/dist/namespaces/itunes/generate/utils.cjs +60 -0
- package/dist/namespaces/itunes/generate/utils.js +59 -0
- package/dist/namespaces/itunes/parse/utils.cjs +77 -0
- package/dist/namespaces/itunes/parse/utils.js +76 -0
- package/dist/namespaces/media/common/types.d.cts +178 -0
- package/dist/namespaces/media/common/types.d.ts +178 -0
- package/dist/namespaces/media/generate/utils.cjs +270 -0
- package/dist/namespaces/media/generate/utils.js +270 -0
- package/dist/namespaces/media/parse/utils.cjs +266 -0
- package/dist/namespaces/media/parse/utils.js +266 -0
- package/dist/namespaces/podcast/common/types.d.cts +201 -0
- package/dist/namespaces/podcast/common/types.d.ts +201 -0
- package/dist/namespaces/podcast/generate/utils.cjs +289 -0
- package/dist/namespaces/podcast/generate/utils.js +288 -0
- package/dist/namespaces/podcast/parse/utils.cjs +276 -0
- package/dist/namespaces/podcast/parse/utils.js +275 -0
- package/dist/namespaces/psc/common/types.d.cts +14 -0
- package/dist/namespaces/psc/common/types.d.ts +14 -0
- package/dist/namespaces/psc/generate/utils.cjs +22 -0
- package/dist/namespaces/psc/generate/utils.js +22 -0
- package/dist/namespaces/psc/parse/utils.cjs +22 -0
- package/dist/namespaces/psc/parse/utils.js +22 -0
- package/dist/namespaces/rawvoice/common/types.d.cts +54 -0
- package/dist/namespaces/rawvoice/common/types.d.ts +54 -0
- package/dist/namespaces/rawvoice/generate/utils.cjs +78 -0
- package/dist/namespaces/rawvoice/generate/utils.js +77 -0
- package/dist/namespaces/rawvoice/parse/utils.cjs +80 -0
- package/dist/namespaces/rawvoice/parse/utils.js +79 -0
- package/dist/namespaces/slash/common/types.d.cts +12 -0
- package/dist/namespaces/slash/common/types.d.ts +12 -0
- package/dist/namespaces/slash/generate/utils.cjs +15 -0
- package/dist/namespaces/slash/generate/utils.js +15 -0
- package/dist/namespaces/slash/parse/utils.cjs +18 -0
- package/dist/namespaces/slash/parse/utils.js +18 -0
- package/dist/namespaces/source/common/types.d.cts +37 -0
- package/dist/namespaces/source/common/types.d.ts +37 -0
- package/dist/namespaces/source/generate/utils.cjs +55 -0
- package/dist/namespaces/source/generate/utils.js +54 -0
- package/dist/namespaces/source/parse/utils.cjs +55 -0
- package/dist/namespaces/source/parse/utils.js +54 -0
- package/dist/namespaces/spotify/common/types.d.cts +12 -0
- package/dist/namespaces/spotify/common/types.d.ts +12 -0
- package/dist/namespaces/spotify/generate/utils.cjs +17 -0
- package/dist/namespaces/spotify/generate/utils.js +17 -0
- package/dist/namespaces/spotify/parse/utils.cjs +17 -0
- package/dist/namespaces/spotify/parse/utils.js +17 -0
- package/dist/namespaces/sy/common/types.d.cts +12 -0
- package/dist/namespaces/sy/common/types.d.ts +12 -0
- package/dist/namespaces/sy/generate/utils.cjs +14 -0
- package/dist/namespaces/sy/generate/utils.js +14 -0
- package/dist/namespaces/sy/parse/utils.cjs +14 -0
- package/dist/namespaces/sy/parse/utils.js +14 -0
- package/dist/namespaces/thr/common/types.d.cts +21 -0
- package/dist/namespaces/thr/common/types.d.ts +21 -0
- package/dist/namespaces/thr/generate/utils.cjs +30 -0
- package/dist/namespaces/thr/generate/utils.js +29 -0
- package/dist/namespaces/thr/parse/utils.cjs +30 -0
- package/dist/namespaces/thr/parse/utils.js +29 -0
- package/dist/namespaces/wfw/common/types.d.cts +9 -0
- package/dist/namespaces/wfw/common/types.d.ts +9 -0
- package/dist/namespaces/wfw/generate/utils.cjs +13 -0
- package/dist/namespaces/wfw/generate/utils.js +13 -0
- package/dist/namespaces/wfw/parse/utils.cjs +13 -0
- package/dist/namespaces/wfw/parse/utils.js +13 -0
- package/dist/namespaces/yt/common/types.d.cts +13 -0
- package/dist/namespaces/yt/common/types.d.ts +13 -0
- package/dist/namespaces/yt/generate/utils.cjs +21 -0
- package/dist/namespaces/yt/generate/utils.js +20 -0
- package/dist/namespaces/yt/parse/utils.cjs +21 -0
- package/dist/namespaces/yt/parse/utils.js +20 -0
- package/dist/opml/common/types.d.cts +48 -0
- package/dist/opml/common/types.d.ts +48 -0
- package/dist/opml/generate/config.cjs +10 -0
- package/dist/opml/generate/config.js +8 -0
- package/dist/opml/generate/index.cjs +14 -0
- package/dist/opml/generate/index.d.cts +7 -0
- package/dist/opml/generate/index.d.ts +7 -0
- package/dist/opml/generate/index.js +14 -0
- package/dist/opml/generate/utils.cjs +62 -0
- package/dist/opml/generate/utils.js +62 -0
- package/dist/opml/parse/config.cjs +29 -0
- package/dist/opml/parse/config.js +27 -0
- package/dist/opml/parse/index.cjs +13 -0
- package/dist/opml/parse/index.d.cts +7 -0
- package/dist/opml/parse/index.d.ts +7 -0
- package/dist/opml/parse/index.js +13 -0
- package/dist/opml/parse/utils.cjs +60 -0
- package/dist/opml/parse/utils.js +60 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +7 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +1 -0
- package/package.json +29 -23
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let entities = require("entities");
|
|
3
|
+
entities = require_rolldown_runtime.__toESM(entities);
|
|
4
|
+
|
|
5
|
+
//#region src/common/utils.ts
|
|
6
|
+
const isPresent = (value) => {
|
|
7
|
+
return value != null;
|
|
8
|
+
};
|
|
9
|
+
const isObject = (value) => {
|
|
10
|
+
return value != null && typeof value === "object" && !Array.isArray(value) && value.constructor === Object;
|
|
11
|
+
};
|
|
12
|
+
const whitespaceOnlyRegex = /^\p{White_Space}*$/u;
|
|
13
|
+
const isNonEmptyString = (value) => {
|
|
14
|
+
return typeof value === "string" && value !== "" && !whitespaceOnlyRegex.test(value);
|
|
15
|
+
};
|
|
16
|
+
const isNonEmptyStringOrNumber = (value) => {
|
|
17
|
+
return typeof value === "number" || isNonEmptyString(value);
|
|
18
|
+
};
|
|
19
|
+
const retrieveText = (value) => {
|
|
20
|
+
return value?.["#text"] ?? value;
|
|
21
|
+
};
|
|
22
|
+
const trimObject = (object) => {
|
|
23
|
+
let result;
|
|
24
|
+
for (const key in object) {
|
|
25
|
+
const value = object[key];
|
|
26
|
+
if (isPresent(value)) {
|
|
27
|
+
if (!result) result = {};
|
|
28
|
+
result[key] = value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
const trimArray = (value, parse) => {
|
|
34
|
+
if (!Array.isArray(value) || value.length === 0) return;
|
|
35
|
+
if (!parse) {
|
|
36
|
+
let needsTrimming = false;
|
|
37
|
+
for (let i = 0; i < value.length; i++) if (!isPresent(value[i])) {
|
|
38
|
+
needsTrimming = true;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
if (!needsTrimming) return value;
|
|
42
|
+
}
|
|
43
|
+
const result = [];
|
|
44
|
+
for (let i = 0; i < value.length; i++) {
|
|
45
|
+
const item = parse ? parse(value[i]) : value[i];
|
|
46
|
+
if (isPresent(item)) result.push(item);
|
|
47
|
+
}
|
|
48
|
+
return result.length > 0 ? result : void 0;
|
|
49
|
+
};
|
|
50
|
+
const cdataStartTag = "<![CDATA[";
|
|
51
|
+
const cdataEndTag = "]]>";
|
|
52
|
+
const stripCdata = (text) => {
|
|
53
|
+
if (typeof text !== "string") return text;
|
|
54
|
+
let currentIndex = text.indexOf(cdataStartTag);
|
|
55
|
+
if (currentIndex === -1) return text;
|
|
56
|
+
let result = "";
|
|
57
|
+
let lastIndex = 0;
|
|
58
|
+
while (currentIndex !== -1) {
|
|
59
|
+
result += text.substring(lastIndex, currentIndex);
|
|
60
|
+
lastIndex = text.indexOf(cdataEndTag, currentIndex + 9);
|
|
61
|
+
if (lastIndex === -1) return text;
|
|
62
|
+
result += text.substring(currentIndex + 9, lastIndex);
|
|
63
|
+
lastIndex += 3;
|
|
64
|
+
currentIndex = text.indexOf(cdataStartTag, lastIndex);
|
|
65
|
+
}
|
|
66
|
+
result += text.substring(lastIndex);
|
|
67
|
+
return result;
|
|
68
|
+
};
|
|
69
|
+
const hasEntities = (text) => {
|
|
70
|
+
const ampIndex = text.indexOf("&");
|
|
71
|
+
return ampIndex !== -1 && text.indexOf(";", ampIndex) !== -1;
|
|
72
|
+
};
|
|
73
|
+
const parseString = (value) => {
|
|
74
|
+
if (typeof value === "string") {
|
|
75
|
+
if (value === "") return;
|
|
76
|
+
let string = value;
|
|
77
|
+
if (value.indexOf(cdataStartTag) !== -1) string = stripCdata(value);
|
|
78
|
+
string = string.trim();
|
|
79
|
+
if (string === "") return;
|
|
80
|
+
if (hasEntities(string)) {
|
|
81
|
+
string = (0, entities.decodeXML)(string);
|
|
82
|
+
if (hasEntities(string)) string = (0, entities.decodeHTML)(string);
|
|
83
|
+
}
|
|
84
|
+
return string || void 0;
|
|
85
|
+
}
|
|
86
|
+
if (typeof value === "number") return value.toString();
|
|
87
|
+
};
|
|
88
|
+
const parseNumber = (value) => {
|
|
89
|
+
if (typeof value === "number") return value;
|
|
90
|
+
if (isNonEmptyString(value)) {
|
|
91
|
+
const numeric = +value;
|
|
92
|
+
return Number.isNaN(numeric) ? void 0 : numeric;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const trueRegex = /^\p{White_Space}*true\p{White_Space}*$/iu;
|
|
96
|
+
const falseRegex = /^\p{White_Space}*false\p{White_Space}*$/iu;
|
|
97
|
+
const yesRegex = /^\p{White_Space}*yes\p{White_Space}*$/iu;
|
|
98
|
+
const parseBoolean = (value) => {
|
|
99
|
+
if (typeof value === "boolean") return value;
|
|
100
|
+
if (isNonEmptyString(value)) {
|
|
101
|
+
if (trueRegex.test(value)) return true;
|
|
102
|
+
if (falseRegex.test(value)) return false;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const parseYesNoBoolean = (value) => {
|
|
106
|
+
const boolean = parseBoolean(value);
|
|
107
|
+
if (boolean !== void 0) return boolean;
|
|
108
|
+
if (isNonEmptyString(value)) return yesRegex.test(value);
|
|
109
|
+
};
|
|
110
|
+
const parseDate = (value) => {
|
|
111
|
+
return parseString(value);
|
|
112
|
+
};
|
|
113
|
+
const parseArray = (value) => {
|
|
114
|
+
if (Array.isArray(value)) return value;
|
|
115
|
+
if (!isObject(value)) return;
|
|
116
|
+
if (value.length) return Array.from(value);
|
|
117
|
+
const keys = Object.keys(value);
|
|
118
|
+
if (keys.length === 0) return;
|
|
119
|
+
for (let i = 0; i < keys.length; i++) {
|
|
120
|
+
const key = keys[i];
|
|
121
|
+
const n = Number(key);
|
|
122
|
+
if (!Number.isInteger(n) || n !== i) return;
|
|
123
|
+
}
|
|
124
|
+
return Object.values(value);
|
|
125
|
+
};
|
|
126
|
+
const parseArrayOf = (value, parse) => {
|
|
127
|
+
const array = parseArray(value);
|
|
128
|
+
if (array) return trimArray(array, parse);
|
|
129
|
+
const parsed = parse(value);
|
|
130
|
+
if (parsed) return [parsed];
|
|
131
|
+
};
|
|
132
|
+
const parseSingular = (value) => {
|
|
133
|
+
return Array.isArray(value) ? value[0] : value;
|
|
134
|
+
};
|
|
135
|
+
const parseSingularOf = (value, parse) => {
|
|
136
|
+
return parse(parseSingular(value));
|
|
137
|
+
};
|
|
138
|
+
const parseCsvOf = (value, parse) => {
|
|
139
|
+
if (!isNonEmptyStringOrNumber(value)) return;
|
|
140
|
+
const items = parseString(value)?.split(",");
|
|
141
|
+
if (items) return trimArray(items, parse);
|
|
142
|
+
};
|
|
143
|
+
const generateCsvOf = (value, generate) => {
|
|
144
|
+
if (!Array.isArray(value) || value.length === 0) return;
|
|
145
|
+
return trimArray(value, generate)?.join();
|
|
146
|
+
};
|
|
147
|
+
const generateXmlStylesheet = (stylesheet) => {
|
|
148
|
+
const generated = trimObject({
|
|
149
|
+
type: generatePlainString(stylesheet.type),
|
|
150
|
+
href: generatePlainString(stylesheet.href),
|
|
151
|
+
title: generatePlainString(stylesheet.title),
|
|
152
|
+
media: generatePlainString(stylesheet.media),
|
|
153
|
+
charset: generatePlainString(stylesheet.charset),
|
|
154
|
+
alternate: generateYesNoBoolean(stylesheet.alternate)
|
|
155
|
+
});
|
|
156
|
+
if (!generated) return;
|
|
157
|
+
let attributes = "";
|
|
158
|
+
for (const key in generated) {
|
|
159
|
+
const value = generated[key];
|
|
160
|
+
if (value !== void 0) attributes += ` ${key}="${value}"`;
|
|
161
|
+
}
|
|
162
|
+
return `<?xml-stylesheet${attributes}?>`;
|
|
163
|
+
};
|
|
164
|
+
const generateXml = (builder, value, options) => {
|
|
165
|
+
let body = builder.build(value);
|
|
166
|
+
if (body.includes("'")) body = body.replace(/'/g, "'");
|
|
167
|
+
let declaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
|
|
168
|
+
if (options?.stylesheets?.length) for (const stylesheetObject of options.stylesheets) {
|
|
169
|
+
const stylesheetString = generateXmlStylesheet(stylesheetObject);
|
|
170
|
+
if (stylesheetString) declaration += `\n${stylesheetString}`;
|
|
171
|
+
}
|
|
172
|
+
return `${declaration}\n${body}`;
|
|
173
|
+
};
|
|
174
|
+
const generateRfc822Date = (value) => {
|
|
175
|
+
if (!isPresent(value)) return;
|
|
176
|
+
const isString = typeof value === "string";
|
|
177
|
+
if (isString && !isNonEmptyString(value)) return;
|
|
178
|
+
const date = isString ? new Date(value) : value;
|
|
179
|
+
if (!Number.isNaN(date.getTime())) return date.toUTCString();
|
|
180
|
+
if (isString) return value;
|
|
181
|
+
};
|
|
182
|
+
const generateRfc3339Date = (value) => {
|
|
183
|
+
if (!isPresent(value)) return;
|
|
184
|
+
const isString = typeof value === "string";
|
|
185
|
+
if (isString && !isNonEmptyString(value)) return;
|
|
186
|
+
const date = isString ? new Date(value) : value;
|
|
187
|
+
if (!Number.isNaN(date.getTime())) return date.toISOString();
|
|
188
|
+
if (isString) return value;
|
|
189
|
+
};
|
|
190
|
+
const generateBoolean = (value) => {
|
|
191
|
+
if (typeof value === "boolean") return value;
|
|
192
|
+
};
|
|
193
|
+
const generateYesNoBoolean = (value) => {
|
|
194
|
+
if (typeof value !== "boolean") return;
|
|
195
|
+
return value ? "yes" : "no";
|
|
196
|
+
};
|
|
197
|
+
const detectNamespaces = (value, recursive = false) => {
|
|
198
|
+
const namespaces = /* @__PURE__ */ new Set();
|
|
199
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
200
|
+
const traverse = (current) => {
|
|
201
|
+
if (Array.isArray(current)) {
|
|
202
|
+
for (const item of current) traverse(item);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (isObject(current)) for (const key in current) {
|
|
206
|
+
if (seenKeys.has(key)) continue;
|
|
207
|
+
seenKeys.add(key);
|
|
208
|
+
const keyWithoutAt = key.indexOf("@") === 0 ? key.slice(1) : key;
|
|
209
|
+
const colonIndex = keyWithoutAt.indexOf(":");
|
|
210
|
+
if (colonIndex > 0) namespaces.add(keyWithoutAt.slice(0, colonIndex));
|
|
211
|
+
if (recursive) traverse(current[key]);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
traverse(value);
|
|
215
|
+
return namespaces;
|
|
216
|
+
};
|
|
217
|
+
const generateCdataString = (value) => {
|
|
218
|
+
if (!isNonEmptyString(value)) return;
|
|
219
|
+
if (value.indexOf("<") !== -1 || value.indexOf(">") !== -1 || value.indexOf("&") !== -1 || value.indexOf("]]>") !== -1) return { "#cdata": value.trim() };
|
|
220
|
+
return value.trim();
|
|
221
|
+
};
|
|
222
|
+
const generateTextOrCdataString = (value) => {
|
|
223
|
+
const result = generateCdataString(value);
|
|
224
|
+
if (!result || isObject(result)) return result;
|
|
225
|
+
return { "#text": result };
|
|
226
|
+
};
|
|
227
|
+
const generatePlainString = (value) => {
|
|
228
|
+
if (!isNonEmptyString(value)) return;
|
|
229
|
+
return value.trim();
|
|
230
|
+
};
|
|
231
|
+
const generateNumber = (value) => {
|
|
232
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
233
|
+
};
|
|
234
|
+
const generateNamespaceAttrs = (value, namespaceUris) => {
|
|
235
|
+
if (!isObject(value)) return;
|
|
236
|
+
let namespaceAttrs;
|
|
237
|
+
const valueNamespaces = detectNamespaces(value, true);
|
|
238
|
+
for (const slug in namespaceUris) {
|
|
239
|
+
if (!valueNamespaces.has(slug)) continue;
|
|
240
|
+
if (!namespaceAttrs) namespaceAttrs = {};
|
|
241
|
+
namespaceAttrs[`@xmlns:${slug}`] = namespaceUris[slug][0];
|
|
242
|
+
}
|
|
243
|
+
return namespaceAttrs;
|
|
244
|
+
};
|
|
245
|
+
const createNamespaceNormalizator = (namespaceUris, primaryNamespace) => {
|
|
246
|
+
const normalizeUri = (uri) => {
|
|
247
|
+
return typeof uri === "string" ? uri.trim().toLowerCase() : uri;
|
|
248
|
+
};
|
|
249
|
+
const namespacesMap = {};
|
|
250
|
+
for (const prefix in namespaceUris) {
|
|
251
|
+
const uris = namespaceUris[prefix];
|
|
252
|
+
for (const uri of uris) {
|
|
253
|
+
const normalizedUri = normalizeUri(uri);
|
|
254
|
+
namespacesMap[normalizedUri] = prefix;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const resolveNamespacePrefix = (uri, localName, fallback) => {
|
|
258
|
+
const normalizedUri = normalizeUri(uri);
|
|
259
|
+
if (primaryNamespace && normalizedUri === normalizeUri(primaryNamespace)) return localName;
|
|
260
|
+
const standardPrefix = namespacesMap[normalizedUri];
|
|
261
|
+
if (standardPrefix) return `${standardPrefix}:${localName}`;
|
|
262
|
+
return fallback;
|
|
263
|
+
};
|
|
264
|
+
const extractNamespaceDeclarations = (element) => {
|
|
265
|
+
const declarations = {};
|
|
266
|
+
if (isObject(element)) {
|
|
267
|
+
for (const key in element) if (key === "@xmlns") declarations[""] = normalizeUri(element[key]);
|
|
268
|
+
else if (key.indexOf("@xmlns:") === 0) {
|
|
269
|
+
const prefix = key.substring(7);
|
|
270
|
+
declarations[prefix] = normalizeUri(element[key]);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return declarations;
|
|
274
|
+
};
|
|
275
|
+
const normalizeWithContext = (name, context, useDefault = false) => {
|
|
276
|
+
const colonIndex = name.indexOf(":");
|
|
277
|
+
if (colonIndex === -1) {
|
|
278
|
+
if (useDefault && context[""]) return resolveNamespacePrefix(context[""], name, name);
|
|
279
|
+
return name;
|
|
280
|
+
}
|
|
281
|
+
const prefix = name.substring(0, colonIndex);
|
|
282
|
+
const unprefixedName = name.substring(colonIndex + 1);
|
|
283
|
+
const uri = context[prefix];
|
|
284
|
+
if (uri) return resolveNamespacePrefix(uri, unprefixedName, name);
|
|
285
|
+
return name;
|
|
286
|
+
};
|
|
287
|
+
const normalizeKey = (key, context) => {
|
|
288
|
+
if (key.indexOf("@") === 0) return `@${normalizeWithContext(key.substring(1), context, false)}`;
|
|
289
|
+
else return normalizeWithContext(key, context, true);
|
|
290
|
+
};
|
|
291
|
+
const traverseAndNormalize = (object, parentContext = {}) => {
|
|
292
|
+
if (!isObject(object)) return object;
|
|
293
|
+
if (Array.isArray(object)) return object.map((item) => traverseAndNormalize(item, parentContext));
|
|
294
|
+
const normalizedObject = {};
|
|
295
|
+
const keyGroups = /* @__PURE__ */ new Map();
|
|
296
|
+
const declarations = extractNamespaceDeclarations(object);
|
|
297
|
+
const currentContext = {
|
|
298
|
+
...parentContext,
|
|
299
|
+
...declarations
|
|
300
|
+
};
|
|
301
|
+
for (const key in object) {
|
|
302
|
+
const value = object[key];
|
|
303
|
+
if (key.indexOf("@xmlns") === 0) {
|
|
304
|
+
normalizedObject[key] = value;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const normalizedKey = normalizeKey(key, currentContext);
|
|
308
|
+
const normalizedValue = traverseAndNormalize(value, currentContext);
|
|
309
|
+
if (!keyGroups.has(normalizedKey)) keyGroups.set(normalizedKey, []);
|
|
310
|
+
const group = keyGroups.get(normalizedKey);
|
|
311
|
+
if (group) group.push(normalizedValue);
|
|
312
|
+
}
|
|
313
|
+
for (const [normalizedKey, values] of keyGroups) if (values.length === 1) normalizedObject[normalizedKey] = values[0];
|
|
314
|
+
else normalizedObject[normalizedKey] = values;
|
|
315
|
+
return normalizedObject;
|
|
316
|
+
};
|
|
317
|
+
const normalizeRoot = (object) => {
|
|
318
|
+
if (!isObject(object)) return object;
|
|
319
|
+
if (Array.isArray(object)) return object.map(normalizeRoot);
|
|
320
|
+
const normalizedObject = {};
|
|
321
|
+
for (const key in object) {
|
|
322
|
+
const value = object[key];
|
|
323
|
+
const declarations = extractNamespaceDeclarations(value);
|
|
324
|
+
const normalizedKey = Object.keys(declarations).length ? normalizeKey(key, declarations) : key;
|
|
325
|
+
normalizedObject[normalizedKey] = traverseAndNormalize(value);
|
|
326
|
+
}
|
|
327
|
+
return normalizedObject;
|
|
328
|
+
};
|
|
329
|
+
return normalizeRoot;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
//#endregion
|
|
333
|
+
exports.createNamespaceNormalizator = createNamespaceNormalizator;
|
|
334
|
+
exports.detectNamespaces = detectNamespaces;
|
|
335
|
+
exports.generateBoolean = generateBoolean;
|
|
336
|
+
exports.generateCdataString = generateCdataString;
|
|
337
|
+
exports.generateCsvOf = generateCsvOf;
|
|
338
|
+
exports.generateNamespaceAttrs = generateNamespaceAttrs;
|
|
339
|
+
exports.generateNumber = generateNumber;
|
|
340
|
+
exports.generatePlainString = generatePlainString;
|
|
341
|
+
exports.generateRfc3339Date = generateRfc3339Date;
|
|
342
|
+
exports.generateRfc822Date = generateRfc822Date;
|
|
343
|
+
exports.generateTextOrCdataString = generateTextOrCdataString;
|
|
344
|
+
exports.generateXml = generateXml;
|
|
345
|
+
exports.generateYesNoBoolean = generateYesNoBoolean;
|
|
346
|
+
exports.isNonEmptyString = isNonEmptyString;
|
|
347
|
+
exports.isNonEmptyStringOrNumber = isNonEmptyStringOrNumber;
|
|
348
|
+
exports.isObject = isObject;
|
|
349
|
+
exports.isPresent = isPresent;
|
|
350
|
+
exports.parseArrayOf = parseArrayOf;
|
|
351
|
+
exports.parseBoolean = parseBoolean;
|
|
352
|
+
exports.parseCsvOf = parseCsvOf;
|
|
353
|
+
exports.parseDate = parseDate;
|
|
354
|
+
exports.parseNumber = parseNumber;
|
|
355
|
+
exports.parseSingular = parseSingular;
|
|
356
|
+
exports.parseSingularOf = parseSingularOf;
|
|
357
|
+
exports.parseString = parseString;
|
|
358
|
+
exports.parseYesNoBoolean = parseYesNoBoolean;
|
|
359
|
+
exports.retrieveText = retrieveText;
|
|
360
|
+
exports.trimArray = trimArray;
|
|
361
|
+
exports.trimObject = trimObject;
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { decodeHTML, decodeXML } from "entities";
|
|
2
|
+
|
|
3
|
+
//#region src/common/utils.ts
|
|
4
|
+
const isPresent = (value) => {
|
|
5
|
+
return value != null;
|
|
6
|
+
};
|
|
7
|
+
const isObject = (value) => {
|
|
8
|
+
return value != null && typeof value === "object" && !Array.isArray(value) && value.constructor === Object;
|
|
9
|
+
};
|
|
10
|
+
const whitespaceOnlyRegex = /^\p{White_Space}*$/u;
|
|
11
|
+
const isNonEmptyString = (value) => {
|
|
12
|
+
return typeof value === "string" && value !== "" && !whitespaceOnlyRegex.test(value);
|
|
13
|
+
};
|
|
14
|
+
const isNonEmptyStringOrNumber = (value) => {
|
|
15
|
+
return typeof value === "number" || isNonEmptyString(value);
|
|
16
|
+
};
|
|
17
|
+
const retrieveText = (value) => {
|
|
18
|
+
return value?.["#text"] ?? value;
|
|
19
|
+
};
|
|
20
|
+
const trimObject = (object) => {
|
|
21
|
+
let result;
|
|
22
|
+
for (const key in object) {
|
|
23
|
+
const value = object[key];
|
|
24
|
+
if (isPresent(value)) {
|
|
25
|
+
if (!result) result = {};
|
|
26
|
+
result[key] = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
const trimArray = (value, parse) => {
|
|
32
|
+
if (!Array.isArray(value) || value.length === 0) return;
|
|
33
|
+
if (!parse) {
|
|
34
|
+
let needsTrimming = false;
|
|
35
|
+
for (let i = 0; i < value.length; i++) if (!isPresent(value[i])) {
|
|
36
|
+
needsTrimming = true;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (!needsTrimming) return value;
|
|
40
|
+
}
|
|
41
|
+
const result = [];
|
|
42
|
+
for (let i = 0; i < value.length; i++) {
|
|
43
|
+
const item = parse ? parse(value[i]) : value[i];
|
|
44
|
+
if (isPresent(item)) result.push(item);
|
|
45
|
+
}
|
|
46
|
+
return result.length > 0 ? result : void 0;
|
|
47
|
+
};
|
|
48
|
+
const cdataStartTag = "<![CDATA[";
|
|
49
|
+
const cdataEndTag = "]]>";
|
|
50
|
+
const stripCdata = (text) => {
|
|
51
|
+
if (typeof text !== "string") return text;
|
|
52
|
+
let currentIndex = text.indexOf(cdataStartTag);
|
|
53
|
+
if (currentIndex === -1) return text;
|
|
54
|
+
let result = "";
|
|
55
|
+
let lastIndex = 0;
|
|
56
|
+
while (currentIndex !== -1) {
|
|
57
|
+
result += text.substring(lastIndex, currentIndex);
|
|
58
|
+
lastIndex = text.indexOf(cdataEndTag, currentIndex + 9);
|
|
59
|
+
if (lastIndex === -1) return text;
|
|
60
|
+
result += text.substring(currentIndex + 9, lastIndex);
|
|
61
|
+
lastIndex += 3;
|
|
62
|
+
currentIndex = text.indexOf(cdataStartTag, lastIndex);
|
|
63
|
+
}
|
|
64
|
+
result += text.substring(lastIndex);
|
|
65
|
+
return result;
|
|
66
|
+
};
|
|
67
|
+
const hasEntities = (text) => {
|
|
68
|
+
const ampIndex = text.indexOf("&");
|
|
69
|
+
return ampIndex !== -1 && text.indexOf(";", ampIndex) !== -1;
|
|
70
|
+
};
|
|
71
|
+
const parseString = (value) => {
|
|
72
|
+
if (typeof value === "string") {
|
|
73
|
+
if (value === "") return;
|
|
74
|
+
let string = value;
|
|
75
|
+
if (value.indexOf(cdataStartTag) !== -1) string = stripCdata(value);
|
|
76
|
+
string = string.trim();
|
|
77
|
+
if (string === "") return;
|
|
78
|
+
if (hasEntities(string)) {
|
|
79
|
+
string = decodeXML(string);
|
|
80
|
+
if (hasEntities(string)) string = decodeHTML(string);
|
|
81
|
+
}
|
|
82
|
+
return string || void 0;
|
|
83
|
+
}
|
|
84
|
+
if (typeof value === "number") return value.toString();
|
|
85
|
+
};
|
|
86
|
+
const parseNumber = (value) => {
|
|
87
|
+
if (typeof value === "number") return value;
|
|
88
|
+
if (isNonEmptyString(value)) {
|
|
89
|
+
const numeric = +value;
|
|
90
|
+
return Number.isNaN(numeric) ? void 0 : numeric;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const trueRegex = /^\p{White_Space}*true\p{White_Space}*$/iu;
|
|
94
|
+
const falseRegex = /^\p{White_Space}*false\p{White_Space}*$/iu;
|
|
95
|
+
const yesRegex = /^\p{White_Space}*yes\p{White_Space}*$/iu;
|
|
96
|
+
const parseBoolean = (value) => {
|
|
97
|
+
if (typeof value === "boolean") return value;
|
|
98
|
+
if (isNonEmptyString(value)) {
|
|
99
|
+
if (trueRegex.test(value)) return true;
|
|
100
|
+
if (falseRegex.test(value)) return false;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const parseYesNoBoolean = (value) => {
|
|
104
|
+
const boolean = parseBoolean(value);
|
|
105
|
+
if (boolean !== void 0) return boolean;
|
|
106
|
+
if (isNonEmptyString(value)) return yesRegex.test(value);
|
|
107
|
+
};
|
|
108
|
+
const parseDate = (value) => {
|
|
109
|
+
return parseString(value);
|
|
110
|
+
};
|
|
111
|
+
const parseArray = (value) => {
|
|
112
|
+
if (Array.isArray(value)) return value;
|
|
113
|
+
if (!isObject(value)) return;
|
|
114
|
+
if (value.length) return Array.from(value);
|
|
115
|
+
const keys = Object.keys(value);
|
|
116
|
+
if (keys.length === 0) return;
|
|
117
|
+
for (let i = 0; i < keys.length; i++) {
|
|
118
|
+
const key = keys[i];
|
|
119
|
+
const n = Number(key);
|
|
120
|
+
if (!Number.isInteger(n) || n !== i) return;
|
|
121
|
+
}
|
|
122
|
+
return Object.values(value);
|
|
123
|
+
};
|
|
124
|
+
const parseArrayOf = (value, parse) => {
|
|
125
|
+
const array = parseArray(value);
|
|
126
|
+
if (array) return trimArray(array, parse);
|
|
127
|
+
const parsed = parse(value);
|
|
128
|
+
if (parsed) return [parsed];
|
|
129
|
+
};
|
|
130
|
+
const parseSingular = (value) => {
|
|
131
|
+
return Array.isArray(value) ? value[0] : value;
|
|
132
|
+
};
|
|
133
|
+
const parseSingularOf = (value, parse) => {
|
|
134
|
+
return parse(parseSingular(value));
|
|
135
|
+
};
|
|
136
|
+
const parseCsvOf = (value, parse) => {
|
|
137
|
+
if (!isNonEmptyStringOrNumber(value)) return;
|
|
138
|
+
const items = parseString(value)?.split(",");
|
|
139
|
+
if (items) return trimArray(items, parse);
|
|
140
|
+
};
|
|
141
|
+
const generateCsvOf = (value, generate) => {
|
|
142
|
+
if (!Array.isArray(value) || value.length === 0) return;
|
|
143
|
+
return trimArray(value, generate)?.join();
|
|
144
|
+
};
|
|
145
|
+
const generateXmlStylesheet = (stylesheet) => {
|
|
146
|
+
const generated = trimObject({
|
|
147
|
+
type: generatePlainString(stylesheet.type),
|
|
148
|
+
href: generatePlainString(stylesheet.href),
|
|
149
|
+
title: generatePlainString(stylesheet.title),
|
|
150
|
+
media: generatePlainString(stylesheet.media),
|
|
151
|
+
charset: generatePlainString(stylesheet.charset),
|
|
152
|
+
alternate: generateYesNoBoolean(stylesheet.alternate)
|
|
153
|
+
});
|
|
154
|
+
if (!generated) return;
|
|
155
|
+
let attributes = "";
|
|
156
|
+
for (const key in generated) {
|
|
157
|
+
const value = generated[key];
|
|
158
|
+
if (value !== void 0) attributes += ` ${key}="${value}"`;
|
|
159
|
+
}
|
|
160
|
+
return `<?xml-stylesheet${attributes}?>`;
|
|
161
|
+
};
|
|
162
|
+
const generateXml = (builder, value, options) => {
|
|
163
|
+
let body = builder.build(value);
|
|
164
|
+
if (body.includes("'")) body = body.replace(/'/g, "'");
|
|
165
|
+
let declaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
|
|
166
|
+
if (options?.stylesheets?.length) for (const stylesheetObject of options.stylesheets) {
|
|
167
|
+
const stylesheetString = generateXmlStylesheet(stylesheetObject);
|
|
168
|
+
if (stylesheetString) declaration += `\n${stylesheetString}`;
|
|
169
|
+
}
|
|
170
|
+
return `${declaration}\n${body}`;
|
|
171
|
+
};
|
|
172
|
+
const generateRfc822Date = (value) => {
|
|
173
|
+
if (!isPresent(value)) return;
|
|
174
|
+
const isString = typeof value === "string";
|
|
175
|
+
if (isString && !isNonEmptyString(value)) return;
|
|
176
|
+
const date = isString ? new Date(value) : value;
|
|
177
|
+
if (!Number.isNaN(date.getTime())) return date.toUTCString();
|
|
178
|
+
if (isString) return value;
|
|
179
|
+
};
|
|
180
|
+
const generateRfc3339Date = (value) => {
|
|
181
|
+
if (!isPresent(value)) return;
|
|
182
|
+
const isString = typeof value === "string";
|
|
183
|
+
if (isString && !isNonEmptyString(value)) return;
|
|
184
|
+
const date = isString ? new Date(value) : value;
|
|
185
|
+
if (!Number.isNaN(date.getTime())) return date.toISOString();
|
|
186
|
+
if (isString) return value;
|
|
187
|
+
};
|
|
188
|
+
const generateBoolean = (value) => {
|
|
189
|
+
if (typeof value === "boolean") return value;
|
|
190
|
+
};
|
|
191
|
+
const generateYesNoBoolean = (value) => {
|
|
192
|
+
if (typeof value !== "boolean") return;
|
|
193
|
+
return value ? "yes" : "no";
|
|
194
|
+
};
|
|
195
|
+
const detectNamespaces = (value, recursive = false) => {
|
|
196
|
+
const namespaces = /* @__PURE__ */ new Set();
|
|
197
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
198
|
+
const traverse = (current) => {
|
|
199
|
+
if (Array.isArray(current)) {
|
|
200
|
+
for (const item of current) traverse(item);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (isObject(current)) for (const key in current) {
|
|
204
|
+
if (seenKeys.has(key)) continue;
|
|
205
|
+
seenKeys.add(key);
|
|
206
|
+
const keyWithoutAt = key.indexOf("@") === 0 ? key.slice(1) : key;
|
|
207
|
+
const colonIndex = keyWithoutAt.indexOf(":");
|
|
208
|
+
if (colonIndex > 0) namespaces.add(keyWithoutAt.slice(0, colonIndex));
|
|
209
|
+
if (recursive) traverse(current[key]);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
traverse(value);
|
|
213
|
+
return namespaces;
|
|
214
|
+
};
|
|
215
|
+
const generateCdataString = (value) => {
|
|
216
|
+
if (!isNonEmptyString(value)) return;
|
|
217
|
+
if (value.indexOf("<") !== -1 || value.indexOf(">") !== -1 || value.indexOf("&") !== -1 || value.indexOf("]]>") !== -1) return { "#cdata": value.trim() };
|
|
218
|
+
return value.trim();
|
|
219
|
+
};
|
|
220
|
+
const generateTextOrCdataString = (value) => {
|
|
221
|
+
const result = generateCdataString(value);
|
|
222
|
+
if (!result || isObject(result)) return result;
|
|
223
|
+
return { "#text": result };
|
|
224
|
+
};
|
|
225
|
+
const generatePlainString = (value) => {
|
|
226
|
+
if (!isNonEmptyString(value)) return;
|
|
227
|
+
return value.trim();
|
|
228
|
+
};
|
|
229
|
+
const generateNumber = (value) => {
|
|
230
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
231
|
+
};
|
|
232
|
+
const generateNamespaceAttrs = (value, namespaceUris) => {
|
|
233
|
+
if (!isObject(value)) return;
|
|
234
|
+
let namespaceAttrs;
|
|
235
|
+
const valueNamespaces = detectNamespaces(value, true);
|
|
236
|
+
for (const slug in namespaceUris) {
|
|
237
|
+
if (!valueNamespaces.has(slug)) continue;
|
|
238
|
+
if (!namespaceAttrs) namespaceAttrs = {};
|
|
239
|
+
namespaceAttrs[`@xmlns:${slug}`] = namespaceUris[slug][0];
|
|
240
|
+
}
|
|
241
|
+
return namespaceAttrs;
|
|
242
|
+
};
|
|
243
|
+
const createNamespaceNormalizator = (namespaceUris, primaryNamespace) => {
|
|
244
|
+
const normalizeUri = (uri) => {
|
|
245
|
+
return typeof uri === "string" ? uri.trim().toLowerCase() : uri;
|
|
246
|
+
};
|
|
247
|
+
const namespacesMap = {};
|
|
248
|
+
for (const prefix in namespaceUris) {
|
|
249
|
+
const uris = namespaceUris[prefix];
|
|
250
|
+
for (const uri of uris) {
|
|
251
|
+
const normalizedUri = normalizeUri(uri);
|
|
252
|
+
namespacesMap[normalizedUri] = prefix;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const resolveNamespacePrefix = (uri, localName, fallback) => {
|
|
256
|
+
const normalizedUri = normalizeUri(uri);
|
|
257
|
+
if (primaryNamespace && normalizedUri === normalizeUri(primaryNamespace)) return localName;
|
|
258
|
+
const standardPrefix = namespacesMap[normalizedUri];
|
|
259
|
+
if (standardPrefix) return `${standardPrefix}:${localName}`;
|
|
260
|
+
return fallback;
|
|
261
|
+
};
|
|
262
|
+
const extractNamespaceDeclarations = (element) => {
|
|
263
|
+
const declarations = {};
|
|
264
|
+
if (isObject(element)) {
|
|
265
|
+
for (const key in element) if (key === "@xmlns") declarations[""] = normalizeUri(element[key]);
|
|
266
|
+
else if (key.indexOf("@xmlns:") === 0) {
|
|
267
|
+
const prefix = key.substring(7);
|
|
268
|
+
declarations[prefix] = normalizeUri(element[key]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return declarations;
|
|
272
|
+
};
|
|
273
|
+
const normalizeWithContext = (name, context, useDefault = false) => {
|
|
274
|
+
const colonIndex = name.indexOf(":");
|
|
275
|
+
if (colonIndex === -1) {
|
|
276
|
+
if (useDefault && context[""]) return resolveNamespacePrefix(context[""], name, name);
|
|
277
|
+
return name;
|
|
278
|
+
}
|
|
279
|
+
const prefix = name.substring(0, colonIndex);
|
|
280
|
+
const unprefixedName = name.substring(colonIndex + 1);
|
|
281
|
+
const uri = context[prefix];
|
|
282
|
+
if (uri) return resolveNamespacePrefix(uri, unprefixedName, name);
|
|
283
|
+
return name;
|
|
284
|
+
};
|
|
285
|
+
const normalizeKey = (key, context) => {
|
|
286
|
+
if (key.indexOf("@") === 0) return `@${normalizeWithContext(key.substring(1), context, false)}`;
|
|
287
|
+
else return normalizeWithContext(key, context, true);
|
|
288
|
+
};
|
|
289
|
+
const traverseAndNormalize = (object, parentContext = {}) => {
|
|
290
|
+
if (!isObject(object)) return object;
|
|
291
|
+
if (Array.isArray(object)) return object.map((item) => traverseAndNormalize(item, parentContext));
|
|
292
|
+
const normalizedObject = {};
|
|
293
|
+
const keyGroups = /* @__PURE__ */ new Map();
|
|
294
|
+
const declarations = extractNamespaceDeclarations(object);
|
|
295
|
+
const currentContext = {
|
|
296
|
+
...parentContext,
|
|
297
|
+
...declarations
|
|
298
|
+
};
|
|
299
|
+
for (const key in object) {
|
|
300
|
+
const value = object[key];
|
|
301
|
+
if (key.indexOf("@xmlns") === 0) {
|
|
302
|
+
normalizedObject[key] = value;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const normalizedKey = normalizeKey(key, currentContext);
|
|
306
|
+
const normalizedValue = traverseAndNormalize(value, currentContext);
|
|
307
|
+
if (!keyGroups.has(normalizedKey)) keyGroups.set(normalizedKey, []);
|
|
308
|
+
const group = keyGroups.get(normalizedKey);
|
|
309
|
+
if (group) group.push(normalizedValue);
|
|
310
|
+
}
|
|
311
|
+
for (const [normalizedKey, values] of keyGroups) if (values.length === 1) normalizedObject[normalizedKey] = values[0];
|
|
312
|
+
else normalizedObject[normalizedKey] = values;
|
|
313
|
+
return normalizedObject;
|
|
314
|
+
};
|
|
315
|
+
const normalizeRoot = (object) => {
|
|
316
|
+
if (!isObject(object)) return object;
|
|
317
|
+
if (Array.isArray(object)) return object.map(normalizeRoot);
|
|
318
|
+
const normalizedObject = {};
|
|
319
|
+
for (const key in object) {
|
|
320
|
+
const value = object[key];
|
|
321
|
+
const declarations = extractNamespaceDeclarations(value);
|
|
322
|
+
const normalizedKey = Object.keys(declarations).length ? normalizeKey(key, declarations) : key;
|
|
323
|
+
normalizedObject[normalizedKey] = traverseAndNormalize(value);
|
|
324
|
+
}
|
|
325
|
+
return normalizedObject;
|
|
326
|
+
};
|
|
327
|
+
return normalizeRoot;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
//#endregion
|
|
331
|
+
export { createNamespaceNormalizator, detectNamespaces, generateBoolean, generateCdataString, generateCsvOf, generateNamespaceAttrs, generateNumber, generatePlainString, generateRfc3339Date, generateRfc822Date, generateTextOrCdataString, generateXml, generateYesNoBoolean, isNonEmptyString, isNonEmptyStringOrNumber, isObject, isPresent, parseArrayOf, parseBoolean, parseCsvOf, parseDate, parseNumber, parseSingular, parseSingularOf, parseString, parseYesNoBoolean, retrieveText, trimArray, trimObject };
|