feedsmith 2.0.0 → 2.1.1

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.
Files changed (160) hide show
  1. package/README.md +7 -4
  2. package/dist/index.cjs +3619 -0
  3. package/dist/index.d.cts +1016 -0
  4. package/dist/index.d.ts +1016 -19
  5. package/dist/index.js +3581 -14
  6. package/package.json +19 -11
  7. package/dist/common/config.d.ts +0 -23
  8. package/dist/common/config.js +0 -43
  9. package/dist/common/parse.d.ts +0 -19
  10. package/dist/common/parse.js +0 -24
  11. package/dist/common/types.d.ts +0 -41
  12. package/dist/common/types.js +0 -1
  13. package/dist/common/utils.d.ts +0 -37
  14. package/dist/common/utils.js +0 -477
  15. package/dist/feeds/atom/common/types.d.ts +0 -103
  16. package/dist/feeds/atom/common/types.js +0 -2
  17. package/dist/feeds/atom/detect/index.d.ts +0 -1
  18. package/dist/feeds/atom/detect/index.js +0 -13
  19. package/dist/feeds/atom/generate/config.d.ts +0 -2
  20. package/dist/feeds/atom/generate/config.js +0 -5
  21. package/dist/feeds/atom/generate/index.d.ts +0 -3
  22. package/dist/feeds/atom/generate/index.js +0 -10
  23. package/dist/feeds/atom/generate/utils.d.ts +0 -11
  24. package/dist/feeds/atom/generate/utils.js +0 -181
  25. package/dist/feeds/atom/parse/config.d.ts +0 -3
  26. package/dist/feeds/atom/parse/config.js +0 -68
  27. package/dist/feeds/atom/parse/index.d.ts +0 -3
  28. package/dist/feeds/atom/parse/index.js +0 -18
  29. package/dist/feeds/atom/parse/utils.d.ts +0 -16
  30. package/dist/feeds/atom/parse/utils.js +0 -206
  31. package/dist/feeds/json/common/types.d.ts +0 -53
  32. package/dist/feeds/json/common/types.js +0 -2
  33. package/dist/feeds/json/detect/index.d.ts +0 -1
  34. package/dist/feeds/json/detect/index.js +0 -16
  35. package/dist/feeds/json/generate/index.d.ts +0 -3
  36. package/dist/feeds/json/generate/index.js +0 -4
  37. package/dist/feeds/json/generate/utils.d.ts +0 -1971
  38. package/dist/feeds/json/generate/utils.js +0 -17
  39. package/dist/feeds/json/parse/index.d.ts +0 -3
  40. package/dist/feeds/json/parse/index.js +0 -13
  41. package/dist/feeds/json/parse/utils.d.ts +0 -9
  42. package/dist/feeds/json/parse/utils.js +0 -114
  43. package/dist/feeds/rdf/common/types.d.ts +0 -48
  44. package/dist/feeds/rdf/common/types.js +0 -2
  45. package/dist/feeds/rdf/detect/index.d.ts +0 -1
  46. package/dist/feeds/rdf/detect/index.js +0 -14
  47. package/dist/feeds/rdf/parse/config.d.ts +0 -3
  48. package/dist/feeds/rdf/parse/config.js +0 -22
  49. package/dist/feeds/rdf/parse/index.d.ts +0 -3
  50. package/dist/feeds/rdf/parse/index.js +0 -18
  51. package/dist/feeds/rdf/parse/utils.d.ts +0 -10
  52. package/dist/feeds/rdf/parse/utils.js +0 -90
  53. package/dist/feeds/rss/common/types.d.ts +0 -111
  54. package/dist/feeds/rss/common/types.js +0 -2
  55. package/dist/feeds/rss/detect/index.d.ts +0 -1
  56. package/dist/feeds/rss/detect/index.js +0 -13
  57. package/dist/feeds/rss/generate/config.d.ts +0 -2
  58. package/dist/feeds/rss/generate/config.js +0 -5
  59. package/dist/feeds/rss/generate/index.d.ts +0 -3
  60. package/dist/feeds/rss/generate/index.js +0 -10
  61. package/dist/feeds/rss/generate/utils.d.ts +0 -14
  62. package/dist/feeds/rss/generate/utils.js +0 -181
  63. package/dist/feeds/rss/parse/config.d.ts +0 -3
  64. package/dist/feeds/rss/parse/config.js +0 -49
  65. package/dist/feeds/rss/parse/index.d.ts +0 -3
  66. package/dist/feeds/rss/parse/index.js +0 -18
  67. package/dist/feeds/rss/parse/utils.d.ts +0 -15
  68. package/dist/feeds/rss/parse/utils.js +0 -163
  69. package/dist/namespaces/atom/common/types.d.ts +0 -4
  70. package/dist/namespaces/atom/common/types.js +0 -2
  71. package/dist/namespaces/atom/generate/utils.d.ts +0 -4
  72. package/dist/namespaces/atom/generate/utils.js +0 -7
  73. package/dist/namespaces/atom/parse/utils.d.ts +0 -4
  74. package/dist/namespaces/atom/parse/utils.js +0 -7
  75. package/dist/namespaces/content/common/types.d.ts +0 -3
  76. package/dist/namespaces/content/common/types.js +0 -2
  77. package/dist/namespaces/content/generate/utils.d.ts +0 -3
  78. package/dist/namespaces/content/generate/utils.js +0 -10
  79. package/dist/namespaces/content/parse/utils.d.ts +0 -3
  80. package/dist/namespaces/content/parse/utils.js +0 -10
  81. package/dist/namespaces/dc/common/types.d.ts +0 -18
  82. package/dist/namespaces/dc/common/types.js +0 -2
  83. package/dist/namespaces/dc/generate/utils.d.ts +0 -3
  84. package/dist/namespaces/dc/generate/utils.js +0 -24
  85. package/dist/namespaces/dc/parse/utils.d.ts +0 -3
  86. package/dist/namespaces/dc/parse/utils.js +0 -24
  87. package/dist/namespaces/dcterms/common/types.d.ts +0 -58
  88. package/dist/namespaces/dcterms/common/types.js +0 -2
  89. package/dist/namespaces/dcterms/generate/utils.d.ts +0 -3
  90. package/dist/namespaces/dcterms/generate/utils.js +0 -64
  91. package/dist/namespaces/dcterms/parse/utils.d.ts +0 -3
  92. package/dist/namespaces/dcterms/parse/utils.js +0 -64
  93. package/dist/namespaces/georss/common/types.d.ts +0 -26
  94. package/dist/namespaces/georss/common/types.js +0 -2
  95. package/dist/namespaces/georss/generate/utils.d.ts +0 -11
  96. package/dist/namespaces/georss/generate/utils.js +0 -60
  97. package/dist/namespaces/georss/parse/utils.d.ts +0 -11
  98. package/dist/namespaces/georss/parse/utils.js +0 -68
  99. package/dist/namespaces/itunes/common/types.d.ts +0 -44
  100. package/dist/namespaces/itunes/common/types.js +0 -2
  101. package/dist/namespaces/itunes/generate/utils.d.ts +0 -7
  102. package/dist/namespaces/itunes/generate/utils.js +0 -70
  103. package/dist/namespaces/itunes/parse/utils.d.ts +0 -9
  104. package/dist/namespaces/itunes/parse/utils.js +0 -99
  105. package/dist/namespaces/media/common/types.d.ts +0 -172
  106. package/dist/namespaces/media/common/types.js +0 -2
  107. package/dist/namespaces/media/generate/utils.d.ts +0 -33
  108. package/dist/namespaces/media/generate/utils.js +0 -350
  109. package/dist/namespaces/media/parse/utils.d.ts +0 -35
  110. package/dist/namespaces/media/parse/utils.js +0 -344
  111. package/dist/namespaces/podcast/common/types.d.ts +0 -182
  112. package/dist/namespaces/podcast/common/types.js +0 -2
  113. package/dist/namespaces/podcast/generate/utils.d.ts +0 -32
  114. package/dist/namespaces/podcast/generate/utils.js +0 -362
  115. package/dist/namespaces/podcast/parse/utils.d.ts +0 -31
  116. package/dist/namespaces/podcast/parse/utils.js +0 -330
  117. package/dist/namespaces/slash/common/types.d.ts +0 -7
  118. package/dist/namespaces/slash/common/types.js +0 -2
  119. package/dist/namespaces/slash/generate/utils.d.ts +0 -3
  120. package/dist/namespaces/slash/generate/utils.js +0 -13
  121. package/dist/namespaces/slash/parse/utils.d.ts +0 -4
  122. package/dist/namespaces/slash/parse/utils.js +0 -16
  123. package/dist/namespaces/sy/common/types.d.ts +0 -6
  124. package/dist/namespaces/sy/common/types.js +0 -2
  125. package/dist/namespaces/sy/generate/utils.d.ts +0 -3
  126. package/dist/namespaces/sy/generate/utils.js +0 -12
  127. package/dist/namespaces/sy/parse/utils.d.ts +0 -3
  128. package/dist/namespaces/sy/parse/utils.js +0 -12
  129. package/dist/namespaces/thr/common/types.d.ts +0 -15
  130. package/dist/namespaces/thr/common/types.js +0 -2
  131. package/dist/namespaces/thr/generate/utils.d.ts +0 -5
  132. package/dist/namespaces/thr/generate/utils.js +0 -33
  133. package/dist/namespaces/thr/parse/utils.d.ts +0 -5
  134. package/dist/namespaces/thr/parse/utils.js +0 -33
  135. package/dist/namespaces/wfw/common/types.d.ts +0 -4
  136. package/dist/namespaces/wfw/common/types.js +0 -2
  137. package/dist/namespaces/wfw/generate/utils.d.ts +0 -3
  138. package/dist/namespaces/wfw/generate/utils.js +0 -11
  139. package/dist/namespaces/wfw/parse/utils.d.ts +0 -3
  140. package/dist/namespaces/wfw/parse/utils.js +0 -11
  141. package/dist/namespaces/yt/common/types.d.ts +0 -8
  142. package/dist/namespaces/yt/common/types.js +0 -2
  143. package/dist/namespaces/yt/generate/utils.d.ts +0 -4
  144. package/dist/namespaces/yt/generate/utils.js +0 -21
  145. package/dist/namespaces/yt/parse/utils.d.ts +0 -4
  146. package/dist/namespaces/yt/parse/utils.js +0 -21
  147. package/dist/opml/common/types.d.ts +0 -39
  148. package/dist/opml/common/types.js +0 -2
  149. package/dist/opml/generate/config.d.ts +0 -2
  150. package/dist/opml/generate/config.js +0 -5
  151. package/dist/opml/generate/index.d.ts +0 -3
  152. package/dist/opml/generate/index.js +0 -10
  153. package/dist/opml/generate/utils.d.ts +0 -6
  154. package/dist/opml/generate/utils.js +0 -66
  155. package/dist/opml/parse/config.d.ts +0 -3
  156. package/dist/opml/parse/config.js +0 -22
  157. package/dist/opml/parse/index.d.ts +0 -3
  158. package/dist/opml/parse/index.js +0 -10
  159. package/dist/opml/parse/utils.d.ts +0 -6
  160. package/dist/opml/parse/utils.js +0 -63
@@ -1,477 +0,0 @@
1
- import { decodeHTML, decodeXML } from 'entities';
2
- export const isPresent = (value) => {
3
- return value != null;
4
- };
5
- export const isObject = (value) => {
6
- return (isPresent(value) &&
7
- typeof value === 'object' &&
8
- !Array.isArray(value) &&
9
- value.constructor === Object);
10
- };
11
- const whitespaceOnlyRegex = /^\p{White_Space}*$/u;
12
- export const isNonEmptyString = (value) => {
13
- return typeof value === 'string' && value !== '' && !whitespaceOnlyRegex.test(value);
14
- };
15
- export const isNonEmptyStringOrNumber = (value) => {
16
- return typeof value === 'number' || isNonEmptyString(value);
17
- };
18
- export const retrieveText = (value) => {
19
- return value?.['#text'] ?? value;
20
- };
21
- export const trimObject = (object) => {
22
- let result;
23
- for (const key in object) {
24
- const value = object[key];
25
- if (isPresent(value)) {
26
- if (!result) {
27
- result = {};
28
- }
29
- result[key] = value;
30
- }
31
- }
32
- return result;
33
- };
34
- export const trimArray = (value, parse) => {
35
- if (!Array.isArray(value) || value.length === 0) {
36
- return;
37
- }
38
- // Do not re-create the array if it all elements are present and no parsing is required.
39
- if (!parse) {
40
- let needsTrimming = false;
41
- for (let i = 0; i < value.length; i++) {
42
- if (!isPresent(value[i])) {
43
- needsTrimming = true;
44
- break;
45
- }
46
- }
47
- if (!needsTrimming) {
48
- return value;
49
- }
50
- }
51
- // Pre-allocation in case of Array is more performant than doing the lazy-allocation
52
- // similar to the one used in trimObject.
53
- const result = [];
54
- for (let i = 0; i < value.length; i++) {
55
- const item = parse ? parse(value[i]) : value[i];
56
- if (isPresent(item)) {
57
- result.push(item);
58
- }
59
- }
60
- return result.length > 0 ? result : undefined;
61
- };
62
- const cdataStartTag = '<![CDATA[';
63
- const cdataEndTag = ']]>';
64
- const cdataRegex = /<!\[CDATA\[([\s\S]*?)\]\]>/g;
65
- export const stripCdata = (text) => {
66
- if (typeof text !== 'string') {
67
- return text;
68
- }
69
- if (text.indexOf('[CDATA[') === -1) {
70
- return text;
71
- }
72
- // For simple cases with only one CDATA section (common case).
73
- const startPos = text.indexOf(cdataStartTag);
74
- // If we have just one simple CDATA section, handle it without regex.
75
- if (startPos !== -1) {
76
- const endPos = text.indexOf(cdataEndTag, startPos + cdataStartTag.length);
77
- if (endPos !== -1 && text.indexOf(cdataStartTag, startPos + cdataStartTag.length) === -1) {
78
- // Single CDATA section case - avoid regex.
79
- return (text.substring(0, startPos) +
80
- text.substring(startPos + cdataStartTag.length, endPos) +
81
- text.substring(endPos + cdataEndTag.length));
82
- }
83
- }
84
- // Fall back to regex for complex cases with multiple CDATA sections.
85
- // Reset lastIndex before use since the regex has global flag.
86
- cdataRegex.lastIndex = 0;
87
- return text.replace(cdataRegex, '$1');
88
- };
89
- export const hasEntities = (text) => {
90
- const ampIndex = text.indexOf('&');
91
- return ampIndex !== -1 && text.indexOf(';', ampIndex) !== -1;
92
- };
93
- export const parseString = (value) => {
94
- if (typeof value === 'string') {
95
- let string = stripCdata(value).trim();
96
- if (hasEntities(string)) {
97
- string = decodeXML(string);
98
- if (hasEntities(string)) {
99
- string = decodeHTML(string);
100
- }
101
- }
102
- return string || undefined;
103
- }
104
- if (typeof value === 'number') {
105
- return value.toString();
106
- }
107
- };
108
- export const parseNumber = (value) => {
109
- if (typeof value === 'number') {
110
- return value;
111
- }
112
- if (isNonEmptyString(value)) {
113
- const numeric = +value;
114
- return Number.isNaN(numeric) ? undefined : numeric;
115
- }
116
- };
117
- const trueRegex = /^\p{White_Space}*true\p{White_Space}*$/iu;
118
- const falseRegex = /^\p{White_Space}*false\p{White_Space}*$/iu;
119
- const yesRegex = /^\p{White_Space}*yes\p{White_Space}*$/iu;
120
- export const parseBoolean = (value) => {
121
- if (typeof value === 'boolean') {
122
- return value;
123
- }
124
- if (isNonEmptyString(value)) {
125
- if (trueRegex.test(value))
126
- return true;
127
- if (falseRegex.test(value))
128
- return false;
129
- }
130
- };
131
- export const parseYesNoBoolean = (value) => {
132
- const boolean = parseBoolean(value);
133
- if (boolean !== undefined) {
134
- return boolean;
135
- }
136
- if (isNonEmptyString(value)) {
137
- return yesRegex.test(value);
138
- }
139
- };
140
- export const parseDate = (value) => {
141
- // This function is currently a placeholder for the actual date parsing functionality
142
- // which might be added at some point in the future. Currently it just uses treats
143
- // the date as string.
144
- return parseString(value);
145
- };
146
- export const parseArray = (value) => {
147
- if (Array.isArray(value)) {
148
- return value;
149
- }
150
- if (!isObject(value)) {
151
- return;
152
- }
153
- if (value.length) {
154
- return Array.from(value);
155
- }
156
- const keys = Object.keys(value);
157
- if (keys.length === 0) {
158
- return;
159
- }
160
- for (let i = 0; i < keys.length; i++) {
161
- const key = keys[i];
162
- const n = Number(key);
163
- if (!Number.isInteger(n) || n !== i) {
164
- return;
165
- }
166
- }
167
- return Object.values(value);
168
- };
169
- export const parseArrayOf = (value, parse) => {
170
- const array = parseArray(value);
171
- if (array) {
172
- return trimArray(array, parse);
173
- }
174
- const parsed = parse(value);
175
- if (parsed) {
176
- return [parsed];
177
- }
178
- };
179
- export const parseSingular = (value) => {
180
- return Array.isArray(value) ? value[0] : value;
181
- };
182
- export const parseSingularOf = (value, parse) => {
183
- return parse(parseSingular(value));
184
- };
185
- export const parseCsvOf = (value, parse) => {
186
- if (!isNonEmptyStringOrNumber(value)) {
187
- return;
188
- }
189
- const items = parseString(value)?.split(',');
190
- if (items) {
191
- return trimArray(items, parse);
192
- }
193
- };
194
- export const generateCsvOf = (value, generate) => {
195
- if (!Array.isArray(value) || value.length === 0) {
196
- return;
197
- }
198
- return trimArray(value, generate)?.join();
199
- };
200
- export const generateXmlStylesheet = (stylesheet) => {
201
- const generated = trimObject({
202
- type: generatePlainString(stylesheet.type),
203
- href: generatePlainString(stylesheet.href),
204
- title: generatePlainString(stylesheet.title),
205
- media: generatePlainString(stylesheet.media),
206
- charset: generatePlainString(stylesheet.charset),
207
- alternate: generateYesNoBoolean(stylesheet.alternate),
208
- });
209
- if (!generated) {
210
- return undefined;
211
- }
212
- let attributes = '';
213
- for (const key in generated) {
214
- const value = generated[key];
215
- if (value !== undefined) {
216
- attributes += ` ${key}="${value}"`;
217
- }
218
- }
219
- return `<?xml-stylesheet${attributes}?>`;
220
- };
221
- export const generateXml = (builder, value, options) => {
222
- let body = builder.build(value);
223
- if (body.includes('&apos;')) {
224
- body = body.replace(/&apos;/g, "'");
225
- }
226
- let declaration = '<?xml version="1.0" encoding="utf-8"?>';
227
- if (options?.stylesheets?.length) {
228
- for (const stylesheetObject of options.stylesheets) {
229
- const stylesheetString = generateXmlStylesheet(stylesheetObject);
230
- if (stylesheetString) {
231
- declaration += `\n${stylesheetString}`;
232
- }
233
- }
234
- }
235
- return `${declaration}\n${body}`;
236
- };
237
- export const generateRfc822Date = (value) => {
238
- // This function generates RFC 822 format dates which is also compatible with RFC 2822.
239
- if (!isPresent(value)) {
240
- return;
241
- }
242
- const isString = typeof value === 'string';
243
- if (isString && !isNonEmptyString(value)) {
244
- return;
245
- }
246
- const date = isString ? new Date(value) : value;
247
- const isValid = !Number.isNaN(date.getTime());
248
- if (isValid) {
249
- return date.toUTCString();
250
- }
251
- if (isString) {
252
- return value;
253
- }
254
- };
255
- export const generateRfc3339Date = (value) => {
256
- // This function generates RFC 3339 format dates which is also compatible with W3C-DTF.
257
- // The only difference between ISO 8601 (produced by toISOString) and RFC 3339 is that
258
- // RFC 3339 allows a space between date and time parts instead of 'T', but the 'T' format
259
- // is actually valid in RFC 3339 as well, so we can just return the ISO string.
260
- if (!isPresent(value)) {
261
- return;
262
- }
263
- const isString = typeof value === 'string';
264
- if (isString && !isNonEmptyString(value)) {
265
- return;
266
- }
267
- const date = isString ? new Date(value) : value;
268
- const isValid = !Number.isNaN(date.getTime());
269
- if (isValid) {
270
- return date.toISOString();
271
- }
272
- if (isString) {
273
- return value;
274
- }
275
- };
276
- export const generateBoolean = (value) => {
277
- if (typeof value === 'boolean') {
278
- return value;
279
- }
280
- };
281
- export const generateYesNoBoolean = (value) => {
282
- if (typeof value !== 'boolean') {
283
- return;
284
- }
285
- return value ? 'yes' : 'no';
286
- };
287
- export const detectNamespaces = (value, recursive = false) => {
288
- const namespaces = new Set();
289
- const seenKeys = new Set();
290
- const traverse = (current) => {
291
- if (Array.isArray(current)) {
292
- for (const item of current) {
293
- traverse(item);
294
- }
295
- return;
296
- }
297
- if (isObject(current)) {
298
- for (const key in current) {
299
- if (seenKeys.has(key)) {
300
- continue;
301
- }
302
- seenKeys.add(key);
303
- const keyWithoutAt = key.indexOf('@') === 0 ? key.slice(1) : key;
304
- const colonIndex = keyWithoutAt.indexOf(':');
305
- if (colonIndex > 0) {
306
- namespaces.add(keyWithoutAt.slice(0, colonIndex));
307
- }
308
- if (recursive) {
309
- traverse(current[key]);
310
- }
311
- }
312
- }
313
- };
314
- traverse(value);
315
- return namespaces;
316
- };
317
- export const generateCdataString = (value) => {
318
- if (!isNonEmptyString(value)) {
319
- return;
320
- }
321
- if (value.indexOf('<') !== -1 ||
322
- value.indexOf('>') !== -1 ||
323
- value.indexOf('&') !== -1 ||
324
- value.indexOf(']]>') !== -1) {
325
- return { '#cdata': value.trim() };
326
- }
327
- return value.trim();
328
- };
329
- export const generatePlainString = (value) => {
330
- if (!isNonEmptyString(value)) {
331
- return;
332
- }
333
- return value.trim();
334
- };
335
- export const generateNumber = (value) => {
336
- if (typeof value === 'number' && Number.isFinite(value)) {
337
- return value;
338
- }
339
- };
340
- export const generateNamespaceAttrs = (value, namespaceUrls) => {
341
- if (!isObject(value)) {
342
- return;
343
- }
344
- let namespaceAttrs;
345
- const valueNamespaces = detectNamespaces(value, true);
346
- for (const slug in namespaceUrls) {
347
- if (!valueNamespaces.has(slug)) {
348
- continue;
349
- }
350
- if (!namespaceAttrs) {
351
- namespaceAttrs = {};
352
- }
353
- namespaceAttrs[`@xmlns:${slug}`] = namespaceUrls[slug];
354
- }
355
- return namespaceAttrs;
356
- };
357
- export const invertObject = (object) => {
358
- const inverted = {};
359
- for (const key in object) {
360
- inverted[object[key]] = key;
361
- }
362
- return inverted;
363
- };
364
- export const createNamespaceNormalizator = (namespaceUrls, primaryNamespace) => {
365
- const namespacesMap = invertObject(namespaceUrls);
366
- const resolveNamespacePrefix = (uri, localName, fallback) => {
367
- if (primaryNamespace && uri === primaryNamespace) {
368
- return localName;
369
- }
370
- const standardPrefix = namespacesMap[uri];
371
- if (standardPrefix) {
372
- return `${standardPrefix}:${localName}`;
373
- }
374
- return fallback;
375
- };
376
- const extractNamespaceDeclarations = (element) => {
377
- const declarations = {};
378
- if (isObject(element)) {
379
- for (const key in element) {
380
- if (key === '@xmlns') {
381
- declarations[''] = element[key];
382
- }
383
- else if (key.indexOf('@xmlns:') === 0) {
384
- const prefix = key.substring('@xmlns:'.length);
385
- declarations[prefix] = element[key];
386
- }
387
- }
388
- }
389
- return declarations;
390
- };
391
- const normalizeWithContext = (name, context, useDefault = false) => {
392
- const colonIndex = name.indexOf(':');
393
- if (colonIndex === -1) {
394
- if (useDefault && context['']) {
395
- return resolveNamespacePrefix(context[''], name, name);
396
- }
397
- return name;
398
- }
399
- const prefix = name.substring(0, colonIndex);
400
- const unprefixedName = name.substring(colonIndex + 1);
401
- const uri = context[prefix];
402
- if (uri) {
403
- return resolveNamespacePrefix(uri, unprefixedName, name);
404
- }
405
- return name;
406
- };
407
- const normalizeKey = (key, context) => {
408
- if (key.indexOf('@') === 0) {
409
- const attrName = key.substring(1);
410
- const normalizedAttrName = normalizeWithContext(attrName, context, false);
411
- return `@${normalizedAttrName}`;
412
- }
413
- else {
414
- return normalizeWithContext(key, context, true);
415
- }
416
- };
417
- const traverseAndNormalize = (object, parentContext = {}) => {
418
- if (!isObject(object)) {
419
- return object;
420
- }
421
- if (Array.isArray(object)) {
422
- return object.map((item) => traverseAndNormalize(item, parentContext));
423
- }
424
- const normalizedObject = {};
425
- const keyGroups = new Map();
426
- const declarations = extractNamespaceDeclarations(object);
427
- const currentContext = { ...parentContext, ...declarations };
428
- for (const key in object) {
429
- const value = object[key];
430
- if (key.indexOf('@xmlns') === 0) {
431
- normalizedObject[key] = value;
432
- continue;
433
- }
434
- const normalizedKey = normalizeKey(key, currentContext);
435
- const normalizedValue = traverseAndNormalize(value, currentContext);
436
- if (!keyGroups.has(normalizedKey)) {
437
- keyGroups.set(normalizedKey, []);
438
- }
439
- const group = keyGroups.get(normalizedKey);
440
- if (group) {
441
- group.push(normalizedValue);
442
- }
443
- }
444
- for (const [normalizedKey, values] of keyGroups) {
445
- if (values.length === 1) {
446
- normalizedObject[normalizedKey] = values[0];
447
- }
448
- else {
449
- normalizedObject[normalizedKey] = values;
450
- }
451
- }
452
- return normalizedObject;
453
- };
454
- // For the root level, we need to handle the special case where the root element
455
- // itself has namespace declarations that apply to its own normalization.
456
- const normalizeRoot = (object) => {
457
- if (!isObject(object)) {
458
- return object;
459
- }
460
- if (Array.isArray(object)) {
461
- return object.map(normalizeRoot);
462
- }
463
- const normalizedObject = {};
464
- for (const key in object) {
465
- const value = object[key];
466
- // Extract namespace declarations from the value to see if they apply to the key.
467
- const declarations = extractNamespaceDeclarations(value);
468
- // If this element has declarations, use them to normalize its own key.
469
- const normalizedKey = Object.keys(declarations).length ? normalizeKey(key, declarations) : key;
470
- // Process the value with empty parent context since this is root.
471
- const normalizedValue = traverseAndNormalize(value);
472
- normalizedObject[normalizedKey] = normalizedValue;
473
- }
474
- return normalizedObject;
475
- };
476
- return normalizeRoot;
477
- };
@@ -1,103 +0,0 @@
1
- import type { DateLike, DeepPartial, Unreliable } from '../../../common/types.js';
2
- import type { ItemOrFeed as DcItemOrFeed } from '../../../namespaces/dc/common/types.js';
3
- import type { ItemOrFeed as DctermsItemOrFeed } from '../../../namespaces/dcterms/common/types.js';
4
- import type { ItemOrFeed as GeoRssItemOrFeed } from '../../../namespaces/georss/common/types.js';
5
- import type { Feed as ItunesFeed, Item as ItunesItem } from '../../../namespaces/itunes/common/types.js';
6
- import type { ItemOrFeed as MediaItemOrFeed } from '../../../namespaces/media/common/types.js';
7
- import type { Item as SlashItem } from '../../../namespaces/slash/common/types.js';
8
- import type { Feed as SyFeed } from '../../../namespaces/sy/common/types.js';
9
- import type { Item as ThrItem, Link as ThrLink } from '../../../namespaces/thr/common/types.js';
10
- import type { Item as WfwItem } from '../../../namespaces/wfw/common/types.js';
11
- import type { Feed as YtFeed, Item as YtItem } from '../../../namespaces/yt/common/types.js';
12
- export type ParsePartialFunction<R> = (value: Unreliable, options?: {
13
- prefix?: string;
14
- asNamespace?: boolean;
15
- }) => DeepPartial<R> | undefined;
16
- export type GenerateFunction<V> = (value: V | undefined, options?: {
17
- prefix?: string;
18
- asNamespace?: boolean;
19
- }) => Unreliable | undefined;
20
- export type Text = string;
21
- export type Link<TDate extends DateLike> = {
22
- href: string;
23
- rel?: string;
24
- type?: string;
25
- hreflang?: string;
26
- title?: string;
27
- length?: number;
28
- thr?: ThrLink<TDate>;
29
- };
30
- export type Person = {
31
- name: string;
32
- uri?: string;
33
- email?: string;
34
- };
35
- export type Category = {
36
- term: string;
37
- scheme?: string;
38
- label?: string;
39
- };
40
- export type Generator = {
41
- text: string;
42
- uri?: string;
43
- version?: string;
44
- };
45
- export type Source<TDate extends DateLike> = {
46
- authors?: Array<Person>;
47
- categories?: Array<Category>;
48
- contributors?: Array<Person>;
49
- generator?: Generator;
50
- icon?: string;
51
- id?: string;
52
- links?: Array<Link<TDate>>;
53
- logo?: string;
54
- rights?: Text;
55
- subtitle?: Text;
56
- title?: Text;
57
- updated?: TDate;
58
- };
59
- export type Entry<TDate extends DateLike> = {
60
- authors?: Array<Person>;
61
- categories?: Array<Category>;
62
- content?: Text;
63
- contributors?: Array<Person>;
64
- id: string;
65
- links?: Array<Link<TDate>>;
66
- published?: TDate;
67
- rights?: Text;
68
- source?: Source<TDate>;
69
- summary?: Text;
70
- title: Text;
71
- updated: TDate;
72
- dc?: DcItemOrFeed<TDate>;
73
- dcterms?: DctermsItemOrFeed<TDate>;
74
- slash?: SlashItem;
75
- itunes?: ItunesItem;
76
- media?: MediaItemOrFeed;
77
- georss?: GeoRssItemOrFeed;
78
- thr?: ThrItem;
79
- wfw?: WfwItem;
80
- yt?: YtItem;
81
- };
82
- export type Feed<TDate extends DateLike> = {
83
- authors?: Array<Person>;
84
- categories?: Array<Category>;
85
- contributors?: Array<Person>;
86
- generator?: Generator;
87
- icon?: string;
88
- id: string;
89
- links?: Array<Link<TDate>>;
90
- logo?: string;
91
- rights?: Text;
92
- subtitle?: Text;
93
- title: Text;
94
- updated: TDate;
95
- entries?: Array<Entry<TDate>>;
96
- dc?: DcItemOrFeed<TDate>;
97
- dcterms?: DctermsItemOrFeed<TDate>;
98
- sy?: SyFeed<TDate>;
99
- itunes?: ItunesFeed;
100
- media?: MediaItemOrFeed;
101
- georss?: GeoRssItemOrFeed;
102
- yt?: YtFeed;
103
- };
@@ -1,2 +0,0 @@
1
- export {};
2
- // #endregion reference
@@ -1 +0,0 @@
1
- export declare const detect: (value: unknown) => value is string;
@@ -1,13 +0,0 @@
1
- import { isNonEmptyString } from '../../../common/utils.js';
2
- export const detect = (value) => {
3
- if (!isNonEmptyString(value)) {
4
- return false;
5
- }
6
- const hasFeedElement = /(?:^|\s|>)\s*<(?:\w+:)?feed[\s>]/im.test(value);
7
- if (!hasFeedElement) {
8
- return false;
9
- }
10
- const hasAtomNamespace = value.includes('http://www.w3.org/2005/Atom');
11
- const hasAtomElements = /(<(?:\w+:)?(entry|title|link|id|updated|summary)[\s>])/i.test(value);
12
- return hasAtomNamespace || hasAtomElements;
13
- };
@@ -1,2 +0,0 @@
1
- import { XMLBuilder } from 'fast-xml-parser';
2
- export declare const builder: XMLBuilder;
@@ -1,5 +0,0 @@
1
- import { XMLBuilder } from 'fast-xml-parser';
2
- import { builderConfig } from '../../../common/config.js';
3
- export const builder = new XMLBuilder({
4
- ...builderConfig,
5
- });
@@ -1,3 +0,0 @@
1
- import type { DateLike, DeepPartial, XmlGenerateFunction } from '../../../common/types.js';
2
- import type { Feed } from '../common/types.js';
3
- export declare const generate: XmlGenerateFunction<Feed<Date>, DeepPartial<Feed<DateLike>>>;
@@ -1,10 +0,0 @@
1
- import { generateXml } from '../../../common/utils.js';
2
- import { builder } from './config.js';
3
- import { generateFeed } from './utils.js';
4
- export const generate = (value, options) => {
5
- const generated = generateFeed(value);
6
- if (!generated) {
7
- throw new Error('Invalid input Atom');
8
- }
9
- return generateXml(builder, generated, options);
10
- };
@@ -1,11 +0,0 @@
1
- import type { DateLike } from '../../../common/types.js';
2
- import type { Category, Entry, Feed, GenerateFunction, Generator, Link, Person, Source, Text } from '../common/types.js';
3
- export declare const createNamespaceSetter: (prefix: string | undefined) => (key: string) => string;
4
- export declare const generateText: GenerateFunction<Text>;
5
- export declare const generateLink: GenerateFunction<Link<DateLike>>;
6
- export declare const generatePerson: GenerateFunction<Person>;
7
- export declare const generateCategory: GenerateFunction<Category>;
8
- export declare const generateGenerator: GenerateFunction<Generator>;
9
- export declare const generateSource: GenerateFunction<Source<DateLike>>;
10
- export declare const generateEntry: GenerateFunction<Entry<DateLike>>;
11
- export declare const generateFeed: GenerateFunction<Feed<DateLike>>;