feedsmith 2.7.0 β†’ 2.9.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.
Files changed (58) hide show
  1. package/README.md +31 -24
  2. package/dist/common/parse.cjs +3 -3
  3. package/dist/common/parse.js +11 -11
  4. package/dist/common/types.d.cts +2 -1
  5. package/dist/common/types.d.ts +2 -1
  6. package/dist/common/utils.cjs +39 -34
  7. package/dist/common/utils.js +40 -35
  8. package/dist/feeds/atom/common/types.d.cts +0 -1
  9. package/dist/feeds/atom/common/types.d.ts +0 -1
  10. package/dist/feeds/atom/detect/index.cjs +2 -1
  11. package/dist/feeds/atom/detect/index.js +2 -1
  12. package/dist/feeds/atom/generate/index.cjs +1 -1
  13. package/dist/feeds/atom/generate/index.js +1 -1
  14. package/dist/feeds/atom/generate/utils.cjs +10 -10
  15. package/dist/feeds/atom/generate/utils.js +29 -29
  16. package/dist/feeds/atom/parse/index.cjs +1 -1
  17. package/dist/feeds/atom/parse/index.js +1 -1
  18. package/dist/feeds/atom/parse/utils.cjs +3 -11
  19. package/dist/feeds/atom/parse/utils.js +17 -25
  20. package/dist/feeds/json/parse/index.cjs +1 -1
  21. package/dist/feeds/json/parse/index.js +1 -1
  22. package/dist/feeds/rdf/detect/index.cjs +13 -2
  23. package/dist/feeds/rdf/detect/index.js +13 -2
  24. package/dist/feeds/rdf/parse/index.cjs +1 -1
  25. package/dist/feeds/rdf/parse/index.js +1 -1
  26. package/dist/feeds/rdf/parse/utils.cjs +2 -2
  27. package/dist/feeds/rdf/parse/utils.js +12 -12
  28. package/dist/feeds/rss/generate/index.cjs +1 -1
  29. package/dist/feeds/rss/generate/index.js +1 -1
  30. package/dist/feeds/rss/generate/utils.cjs +5 -5
  31. package/dist/feeds/rss/generate/utils.js +54 -54
  32. package/dist/feeds/rss/parse/index.cjs +1 -1
  33. package/dist/feeds/rss/parse/index.js +1 -1
  34. package/dist/feeds/rss/parse/utils.cjs +2 -2
  35. package/dist/feeds/rss/parse/utils.js +51 -51
  36. package/dist/namespaces/atom/common/types.d.cts +4 -3
  37. package/dist/namespaces/atom/common/types.d.ts +4 -3
  38. package/dist/namespaces/atom/generate/utils.cjs +4 -4
  39. package/dist/namespaces/atom/generate/utils.js +6 -6
  40. package/dist/namespaces/dc/common/types.d.cts +15 -30
  41. package/dist/namespaces/dc/common/types.d.ts +15 -30
  42. package/dist/namespaces/dcterms/common/types.d.cts +55 -110
  43. package/dist/namespaces/dcterms/common/types.d.ts +55 -110
  44. package/dist/namespaces/itunes/common/types.d.cts +7 -14
  45. package/dist/namespaces/itunes/common/types.d.ts +7 -14
  46. package/dist/namespaces/media/common/types.d.cts +1 -2
  47. package/dist/namespaces/media/common/types.d.ts +1 -2
  48. package/dist/namespaces/podcast/common/types.d.cts +9 -12
  49. package/dist/namespaces/podcast/common/types.d.ts +9 -12
  50. package/dist/namespaces/podcast/generate/utils.cjs +5 -2
  51. package/dist/namespaces/podcast/generate/utils.js +5 -2
  52. package/dist/namespaces/podcast/parse/utils.cjs +5 -2
  53. package/dist/namespaces/podcast/parse/utils.js +5 -2
  54. package/dist/namespaces/prism/common/types.d.cts +8 -16
  55. package/dist/namespaces/prism/common/types.d.ts +8 -16
  56. package/dist/opml/generate/index.cjs +1 -1
  57. package/dist/opml/generate/index.js +1 -1
  58. package/package.json +7 -9
package/README.md CHANGED
@@ -18,26 +18,33 @@ Feedsmith offers universal and format‑specific parsers that maintain the origi
18
18
 
19
19
  ---
20
20
 
21
+ > [!IMPORTANT]
22
+ > **Feedsmith 3.x is in final stages of development.** Check out the [v3.x guide](https://next.feedsmith.dev/migration/v2-to-v3) to explore new features and learn how to upgrade. Install it with:
23
+ >
24
+ > `npm install feedsmith@next`
25
+
26
+ ---
27
+
21
28
  ## Features
22
29
 
23
30
  ### Core
24
31
 
25
- * **Comprehensive support** 🎯 β€” Supports all major feed formats and namespaces.
26
- * **Preserves structure** πŸ“¦ β€” Parsed feed object maintains the original feed structure making it easy to access the data.
27
- * **Smart namespace handling** 🧠 β€” Normalizes custom namespace prefixes to standard ones (e.g., `<custom:creator>` becomes `dc.creator`).
28
- * **Parsing & generating** πŸ”© β€” Use one package for both parsing and generating feeds.
32
+ * **Comprehensive support** β€” Supports all major feed formats and namespaces.
33
+ * **Preserves structure** β€” Parsed feed object maintains the original feed structure making it easy to access the data.
34
+ * **Smart namespace handling** β€” Normalizes custom namespace prefixes to standard ones (e.g., `<custom:creator>` becomes `dc.creator`).
35
+ * **Parsing & generating** β€” Use one package for both parsing and generating feeds.
29
36
 
30
37
  ### Leniency
31
- * **Normalizes legacy elements** ✨ β€” Upgrades feed elements to their modern equivalents so that you never need to worry about reading feeds in older formats.
32
- * **CaSe INSENsiTive** 🐍 β€” Handles fields and attributes in any case (lowercase, uppercase, mixed).
33
- * **Namespace URI tolerance** πŸ”§ β€” Accepts non-official namespace URIs (HTTPS variants, case variations, trailing slashes, whitespace).
34
- * **Forgiving** 🀝 β€” Gracefully handles malformed or incomplete feeds and extracts valid data. This makes it suitable for use with real-world feeds that may not strictly follow specifications.
38
+ * **Normalizes legacy elements** β€” Upgrades feed elements to their modern equivalents so that you never need to worry about reading feeds in older formats.
39
+ * **CaSe INSENsiTive** β€” Handles fields and attributes in any case (lowercase, uppercase, mixed).
40
+ * **Namespace URI tolerance** β€” Accepts non-official namespace URIs (HTTPS variants, case variations, trailing slashes, whitespace).
41
+ * **Forgiving** β€” Gracefully handles malformed or incomplete feeds and extracts valid data. This makes it suitable for use with real-world feeds that may not strictly follow specifications.
35
42
 
36
- ### Performance and Type-Safety
37
- * **Ultrafast parsing** ⚑ β€” One of the fastest JavaScript feed parsers ([see benchmarks](/benchmarks)).
38
- * **Type-safe API** πŸ›Ÿ β€” Built with TypeScript from the ground up, it provides complete type definitions for every feed format and namespace.
39
- * **Tree-shakable** πŸƒ β€” Only include the parts of the library you need, reducing bundle size.
40
- * **Well-tested** πŸ”¬ β€” Comprehensive test suite with over 2000 tests and 99% code coverage.
43
+ ### Performance
44
+ * **Ultrafast parsing** β€” One of the fastest JavaScript feed parsers ([see benchmarks](/benchmarks)).
45
+ * **Type-safe API** β€” Built with TypeScript from the ground up, it provides complete type definitions for every feed format and namespace.
46
+ * **Tree-shakable** β€” Only include the parts of the library you need, reducing bundle size.
47
+ * **Well-tested** β€” Comprehensive test suite with over 2000 tests and 99% code coverage.
41
48
 
42
49
  ### Compatibility
43
50
  * Works in Node.js 14.0.0+ and modern browsers.
@@ -56,23 +63,23 @@ Feedsmith aims to fully support all major feed formats and namespaces in complet
56
63
 
57
64
  ### Feeds
58
65
 
59
- | Format | Versions | Parsing | Generating |
60
- |--------|----------|---------|------------|
61
- | [RSS](http://cyber.law.harvard.edu/rss/rss.html) | 0.9x, 2.0 | βœ… | βœ… |
62
- | [Atom](https://tools.ietf.org/html/rfc4287) | 0.3, 1.0 | βœ… | βœ… |
63
- | [RDF](https://web.resource.org/rss/1.0/spec) | 0.9, 1.0 | βœ… | πŸ“‹ |
64
- | [JSON Feed](https://jsonfeed.org) | 1.0, 1.1 | βœ… | βœ… |
66
+ | Format | Versions | Parse | Generate |
67
+ |--------|----------|-------|----------|
68
+ | [RSS](https://feedsmith.dev/reference/feeds/rss) | 0.9x, 2.0 | βœ… | βœ… |
69
+ | [Atom](https://feedsmith.dev/reference/feeds/atom) | 0.3, 1.0 | βœ… | βœ… |
70
+ | [RDF](https://feedsmith.dev/reference/feeds/rdf) | 0.9, 1.0 | βœ… | πŸ“‹ |
71
+ | [JSON Feed](https://feedsmith.dev/reference/feeds/json) | 1.0, 1.1 | βœ… | βœ… |
65
72
 
66
73
  ### Other
67
74
 
68
- | Format | Versions | Parsing | Generating |
69
- |--------|----------|---------|------------|
70
- | [OPML](https://opml.org/) | 1.0, 2.0 | βœ… | βœ… |
75
+ | Format | Versions | Parse | Generate |
76
+ |--------|----------|-------|----------|
77
+ | [OPML](https://feedsmith.dev/reference/other/opml) | 1.0, 2.0 | βœ… | βœ… |
71
78
 
72
79
  ### Feed Namespaces
73
80
 
74
- | Name | Prefix | Supported in | Parsing | Generating |
75
- |------|---------|--------------|---------|------------|
81
+ | Name | Prefix | Supported in | Parse | Generate |
82
+ |------|---------|--------------|-------|----------|
76
83
  | [Atom](https://feedsmith.dev/reference/namespaces/atom) | `<atom:*>` | RSS, RDF | βœ… | βœ… |
77
84
  | [Dublin Core](https://feedsmith.dev/reference/namespaces/dc) | `<dc:*>` | RSS, Atom, RDF | βœ… | βœ… |
78
85
  | [Dublin Core Terms](https://feedsmith.dev/reference/namespaces/dcterms) | `<dcterms:*>` | RSS, Atom, RDF | βœ… | βœ… |
@@ -1,6 +1,6 @@
1
+ const require_config = require('./config.cjs');
1
2
  const require_utils = require('./utils.cjs');
2
3
  const require_index = require('../feeds/atom/detect/index.cjs');
3
- const require_config = require('./config.cjs');
4
4
  const require_index$1 = require('../feeds/atom/parse/index.cjs');
5
5
  const require_index$2 = require('../feeds/json/detect/index.cjs');
6
6
  const require_index$3 = require('../feeds/json/parse/index.cjs');
@@ -10,7 +10,7 @@ const require_index$6 = require('../feeds/rss/detect/index.cjs');
10
10
  const require_index$7 = require('../feeds/rss/parse/index.cjs');
11
11
 
12
12
  //#region src/common/parse.ts
13
- const parse$4 = (value, options) => {
13
+ const parse = (value, options) => {
14
14
  if (require_index$6.detect(value)) return {
15
15
  format: "rss",
16
16
  feed: require_index$7.parse(value, options)
@@ -32,4 +32,4 @@ const parse$4 = (value, options) => {
32
32
  };
33
33
 
34
34
  //#endregion
35
- exports.parse = parse$4;
35
+ exports.parse = parse;
@@ -1,35 +1,35 @@
1
+ import { locales } from "./config.js";
1
2
  import { parseJsonObject } from "./utils.js";
2
3
  import { detect } from "../feeds/atom/detect/index.js";
3
- import { locales } from "./config.js";
4
- import { parse } from "../feeds/atom/parse/index.js";
4
+ import { parse as parse$1 } from "../feeds/atom/parse/index.js";
5
5
  import { detect as detect$1 } from "../feeds/json/detect/index.js";
6
- import { parse as parse$1 } from "../feeds/json/parse/index.js";
6
+ import { parse as parse$2 } from "../feeds/json/parse/index.js";
7
7
  import { detect as detect$2 } from "../feeds/rdf/detect/index.js";
8
- import { parse as parse$2 } from "../feeds/rdf/parse/index.js";
8
+ import { parse as parse$3 } from "../feeds/rdf/parse/index.js";
9
9
  import { detect as detect$3 } from "../feeds/rss/detect/index.js";
10
- import { parse as parse$3 } from "../feeds/rss/parse/index.js";
10
+ import { parse as parse$4 } from "../feeds/rss/parse/index.js";
11
11
 
12
12
  //#region src/common/parse.ts
13
- const parse$4 = (value, options) => {
13
+ const parse = (value, options) => {
14
14
  if (detect$3(value)) return {
15
15
  format: "rss",
16
- feed: parse$3(value, options)
16
+ feed: parse$4(value, options)
17
17
  };
18
18
  if (detect(value)) return {
19
19
  format: "atom",
20
- feed: parse(value, options)
20
+ feed: parse$1(value, options)
21
21
  };
22
22
  if (detect$2(value)) return {
23
23
  format: "rdf",
24
- feed: parse$2(value, options)
24
+ feed: parse$3(value, options)
25
25
  };
26
26
  const json = parseJsonObject(value);
27
27
  if (detect$1(json)) return {
28
28
  format: "json",
29
- feed: parse$1(json, options)
29
+ feed: parse$2(json, options)
30
30
  };
31
31
  throw new Error(locales.unrecognizedFeedFormat);
32
32
  };
33
33
 
34
34
  //#endregion
35
- export { parse$4 as parse };
35
+ export { parse };
@@ -4,6 +4,7 @@ type ExtraFields<F extends ReadonlyArray<string>, V = unknown> = { [K in F[numbe
4
4
  type IsPlainObject<T> = T extends Array<unknown> ? false : T extends ((...args: Array<unknown>) => unknown) ? false : T extends Date ? false : T extends object ? T extends null ? false : true : false;
5
5
  type RemoveUndefined<T> = T extends undefined ? never : T;
6
6
  type DeepPartial<T> = IsPlainObject<T> extends true ? { [P in keyof T]?: DeepPartial<RemoveUndefined<T[P]>> } : T extends Array<infer U> ? Array<DeepPartial<U>> : T;
7
+ type DeepOmit<T, K extends string> = T extends Array<infer U> ? Array<DeepOmit<U, K>> : IsPlainObject<T> extends true ? Pick<{ [P in keyof T]: DeepOmit<T[P], K> }, Exclude<keyof T, K>> : T;
7
8
  type ParseOptions = {
8
9
  maxItems?: number;
9
10
  };
@@ -25,4 +26,4 @@ type JsonGenerateOptions<O, F extends boolean = false> = O & {
25
26
  type XmlGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: XmlGenerateOptions<O, F>) => string;
26
27
  type JsonGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: JsonGenerateOptions<O, F>) => unknown;
27
28
  //#endregion
28
- export { DateLike, DeepPartial, ExtraFields, JsonGenerateMain, ParseOptions, XmlGenerateMain, XmlGenerateOptions };
29
+ export { DateLike, DeepOmit, DeepPartial, ExtraFields, JsonGenerateMain, ParseOptions, XmlGenerateMain, XmlGenerateOptions };
@@ -4,6 +4,7 @@ type ExtraFields<F extends ReadonlyArray<string>, V = unknown> = { [K in F[numbe
4
4
  type IsPlainObject<T> = T extends Array<unknown> ? false : T extends ((...args: Array<unknown>) => unknown) ? false : T extends Date ? false : T extends object ? T extends null ? false : true : false;
5
5
  type RemoveUndefined<T> = T extends undefined ? never : T;
6
6
  type DeepPartial<T> = IsPlainObject<T> extends true ? { [P in keyof T]?: DeepPartial<RemoveUndefined<T[P]>> } : T extends Array<infer U> ? Array<DeepPartial<U>> : T;
7
+ type DeepOmit<T, K extends string> = T extends Array<infer U> ? Array<DeepOmit<U, K>> : IsPlainObject<T> extends true ? Pick<{ [P in keyof T]: DeepOmit<T[P], K> }, Exclude<keyof T, K>> : T;
7
8
  type ParseOptions = {
8
9
  maxItems?: number;
9
10
  };
@@ -25,4 +26,4 @@ type JsonGenerateOptions<O, F extends boolean = false> = O & {
25
26
  type XmlGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: XmlGenerateOptions<O, F>) => string;
26
27
  type JsonGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: JsonGenerateOptions<O, F>) => unknown;
27
28
  //#endregion
28
- export { DateLike, DeepPartial, ExtraFields, JsonGenerateMain, ParseOptions, XmlGenerateMain, XmlGenerateOptions };
29
+ export { DateLike, DeepOmit, DeepPartial, ExtraFields, JsonGenerateMain, ParseOptions, XmlGenerateMain, XmlGenerateOptions };
@@ -64,39 +64,32 @@ const generateSingularOrArray = (singularValue, pluralValues, generator) => {
64
64
  };
65
65
  const cdataStartTag = "<![CDATA[";
66
66
  const cdataEndTag = "]]>";
67
- const stripCdata = (text) => {
68
- if (typeof text !== "string") return text;
67
+ const hasEntities = (text) => {
68
+ const ampIndex = text.indexOf("&");
69
+ return ampIndex !== -1 && text.indexOf(";", ampIndex) !== -1;
70
+ };
71
+ const decodeWithCdata = (text) => {
69
72
  let currentIndex = text.indexOf(cdataStartTag);
70
- if (currentIndex === -1) return text;
73
+ if (currentIndex === -1) return hasEntities(text) ? (0, entities.decodeHTML)(text) : text;
71
74
  let result = "";
72
75
  let lastIndex = 0;
73
76
  while (currentIndex !== -1) {
74
- result += text.substring(lastIndex, currentIndex);
75
- lastIndex = text.indexOf(cdataEndTag, currentIndex + 9);
76
- if (lastIndex === -1) return text;
77
- result += text.substring(currentIndex + 9, lastIndex);
78
- lastIndex += 3;
77
+ const textBefore = text.substring(lastIndex, currentIndex);
78
+ result += hasEntities(textBefore) ? (0, entities.decodeHTML)(textBefore) : textBefore;
79
+ const endIndex = text.indexOf(cdataEndTag, currentIndex + 9);
80
+ if (endIndex === -1) return hasEntities(text) ? (0, entities.decodeHTML)(text) : text;
81
+ result += text.substring(currentIndex + 9, endIndex);
82
+ lastIndex = endIndex + 3;
79
83
  currentIndex = text.indexOf(cdataStartTag, lastIndex);
80
84
  }
81
- result += text.substring(lastIndex);
85
+ const textAfter = text.substring(lastIndex);
86
+ result += hasEntities(textAfter) ? (0, entities.decodeHTML)(textAfter) : textAfter;
82
87
  return result;
83
88
  };
84
- const hasEntities = (text) => {
85
- const ampIndex = text.indexOf("&");
86
- return ampIndex !== -1 && text.indexOf(";", ampIndex) !== -1;
87
- };
88
89
  const parseString = (value) => {
89
90
  if (typeof value === "string") {
90
91
  if (value === "") return;
91
- let string = value;
92
- if (value.indexOf(cdataStartTag) !== -1) string = stripCdata(value);
93
- string = string.trim();
94
- if (string === "") return;
95
- if (hasEntities(string)) {
96
- string = (0, entities.decodeXML)(string);
97
- if (hasEntities(string)) string = (0, entities.decodeHTML)(string);
98
- }
99
- return string || void 0;
92
+ return decodeWithCdata(value).trim() || void 0;
100
93
  }
101
94
  if (typeof value === "number") return value.toString();
102
95
  };
@@ -215,15 +208,15 @@ const generateYesNoBoolean = (value) => {
215
208
  };
216
209
  const detectNamespaces = (value, recursive = false) => {
217
210
  const namespaces = /* @__PURE__ */ new Set();
218
- const seenKeys = /* @__PURE__ */ new Set();
211
+ const seenKeys = recursive ? /* @__PURE__ */ new Set() : void 0;
219
212
  const traverse = (current) => {
220
213
  if (Array.isArray(current)) {
221
214
  for (const item of current) traverse(item);
222
215
  return;
223
216
  }
224
217
  if (isObject(current)) for (const key in current) {
225
- if (seenKeys.has(key)) continue;
226
- seenKeys.add(key);
218
+ if (seenKeys?.has(key)) continue;
219
+ seenKeys?.add(key);
227
220
  const keyWithoutAt = key.charCodeAt(0) === 64 ? key.slice(1) : key;
228
221
  const colonIndex = keyWithoutAt.indexOf(":");
229
222
  if (colonIndex > 0) namespaces.add(keyWithoutAt.slice(0, colonIndex));
@@ -268,8 +261,15 @@ const generateNamespaceAttrs = (value, namespaceUris) => {
268
261
  return namespaceAttrs;
269
262
  };
270
263
  const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNamespace) => {
264
+ const normalizedUriCache = /* @__PURE__ */ new Map();
271
265
  const normalizeNamespaceUri = (uri) => {
272
- return typeof uri === "string" ? uri.trim().toLowerCase() : uri;
266
+ if (typeof uri !== "string") return uri;
267
+ let normalized = normalizedUriCache.get(uri);
268
+ if (normalized === void 0) {
269
+ normalized = uri.trim().toLowerCase();
270
+ normalizedUriCache.set(uri, normalized);
271
+ }
272
+ return normalized;
273
273
  };
274
274
  const primaryNamespaceUris = primaryNamespace && namespaceUris[primaryNamespace] ? namespaceUris[primaryNamespace].map(normalizeNamespaceUri) : void 0;
275
275
  const resolveNamespacePrefix = (uri, localName, fallback) => {
@@ -309,13 +309,17 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
309
309
  const traverseAndNormalize = (object, parentContext = {}) => {
310
310
  if (Array.isArray(object)) return object.map((item) => traverseAndNormalize(item, parentContext));
311
311
  if (!isObject(object)) return object;
312
- const normalizedObject = {};
313
- const keyGroups = /* @__PURE__ */ new Map();
314
312
  const declarations = extractNamespaceDeclarations(object);
315
- const currentContext = {
313
+ let hasDeclarations = false;
314
+ for (const _ in declarations) {
315
+ hasDeclarations = true;
316
+ break;
317
+ }
318
+ const currentContext = hasDeclarations ? {
316
319
  ...parentContext,
317
320
  ...declarations
318
- };
321
+ } : parentContext;
322
+ const normalizedObject = {};
319
323
  for (const key in object) {
320
324
  const value = object[key];
321
325
  if (key.indexOf("@xmlns") === 0) {
@@ -324,11 +328,12 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
324
328
  }
325
329
  const normalizedKey = normalizeKey(key, currentContext);
326
330
  const normalizedValue = traverseAndNormalize(value, currentContext);
327
- if (!keyGroups.has(normalizedKey)) keyGroups.set(normalizedKey, []);
328
- const group = keyGroups.get(normalizedKey);
329
- if (group) group.push(normalizedValue);
331
+ if (normalizedKey in normalizedObject) {
332
+ const existing = normalizedObject[normalizedKey];
333
+ if (Array.isArray(existing)) existing.push(normalizedValue);
334
+ else normalizedObject[normalizedKey] = [existing, normalizedValue];
335
+ } else normalizedObject[normalizedKey] = normalizedValue;
330
336
  }
331
- for (const [normalizedKey, values] of keyGroups) normalizedObject[normalizedKey] = values.length === 1 ? values[0] : values;
332
337
  return normalizedObject;
333
338
  };
334
339
  const normalizeRoot = (object) => {
@@ -1,4 +1,4 @@
1
- import { decodeHTML, decodeXML } from "entities";
1
+ import { decodeHTML } from "entities";
2
2
 
3
3
  //#region src/common/utils.ts
4
4
  const isPresent = (value) => {
@@ -64,39 +64,32 @@ const generateSingularOrArray = (singularValue, pluralValues, generator) => {
64
64
  };
65
65
  const cdataStartTag = "<![CDATA[";
66
66
  const cdataEndTag = "]]>";
67
- const stripCdata = (text) => {
68
- if (typeof text !== "string") return text;
67
+ const hasEntities = (text) => {
68
+ const ampIndex = text.indexOf("&");
69
+ return ampIndex !== -1 && text.indexOf(";", ampIndex) !== -1;
70
+ };
71
+ const decodeWithCdata = (text) => {
69
72
  let currentIndex = text.indexOf(cdataStartTag);
70
- if (currentIndex === -1) return text;
73
+ if (currentIndex === -1) return hasEntities(text) ? decodeHTML(text) : text;
71
74
  let result = "";
72
75
  let lastIndex = 0;
73
76
  while (currentIndex !== -1) {
74
- result += text.substring(lastIndex, currentIndex);
75
- lastIndex = text.indexOf(cdataEndTag, currentIndex + 9);
76
- if (lastIndex === -1) return text;
77
- result += text.substring(currentIndex + 9, lastIndex);
78
- lastIndex += 3;
77
+ const textBefore = text.substring(lastIndex, currentIndex);
78
+ result += hasEntities(textBefore) ? decodeHTML(textBefore) : textBefore;
79
+ const endIndex = text.indexOf(cdataEndTag, currentIndex + 9);
80
+ if (endIndex === -1) return hasEntities(text) ? decodeHTML(text) : text;
81
+ result += text.substring(currentIndex + 9, endIndex);
82
+ lastIndex = endIndex + 3;
79
83
  currentIndex = text.indexOf(cdataStartTag, lastIndex);
80
84
  }
81
- result += text.substring(lastIndex);
85
+ const textAfter = text.substring(lastIndex);
86
+ result += hasEntities(textAfter) ? decodeHTML(textAfter) : textAfter;
82
87
  return result;
83
88
  };
84
- const hasEntities = (text) => {
85
- const ampIndex = text.indexOf("&");
86
- return ampIndex !== -1 && text.indexOf(";", ampIndex) !== -1;
87
- };
88
89
  const parseString = (value) => {
89
90
  if (typeof value === "string") {
90
91
  if (value === "") return;
91
- let string = value;
92
- if (value.indexOf(cdataStartTag) !== -1) string = stripCdata(value);
93
- string = string.trim();
94
- if (string === "") return;
95
- if (hasEntities(string)) {
96
- string = decodeXML(string);
97
- if (hasEntities(string)) string = decodeHTML(string);
98
- }
99
- return string || void 0;
92
+ return decodeWithCdata(value).trim() || void 0;
100
93
  }
101
94
  if (typeof value === "number") return value.toString();
102
95
  };
@@ -215,15 +208,15 @@ const generateYesNoBoolean = (value) => {
215
208
  };
216
209
  const detectNamespaces = (value, recursive = false) => {
217
210
  const namespaces = /* @__PURE__ */ new Set();
218
- const seenKeys = /* @__PURE__ */ new Set();
211
+ const seenKeys = recursive ? /* @__PURE__ */ new Set() : void 0;
219
212
  const traverse = (current) => {
220
213
  if (Array.isArray(current)) {
221
214
  for (const item of current) traverse(item);
222
215
  return;
223
216
  }
224
217
  if (isObject(current)) for (const key in current) {
225
- if (seenKeys.has(key)) continue;
226
- seenKeys.add(key);
218
+ if (seenKeys?.has(key)) continue;
219
+ seenKeys?.add(key);
227
220
  const keyWithoutAt = key.charCodeAt(0) === 64 ? key.slice(1) : key;
228
221
  const colonIndex = keyWithoutAt.indexOf(":");
229
222
  if (colonIndex > 0) namespaces.add(keyWithoutAt.slice(0, colonIndex));
@@ -268,8 +261,15 @@ const generateNamespaceAttrs = (value, namespaceUris) => {
268
261
  return namespaceAttrs;
269
262
  };
270
263
  const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNamespace) => {
264
+ const normalizedUriCache = /* @__PURE__ */ new Map();
271
265
  const normalizeNamespaceUri = (uri) => {
272
- return typeof uri === "string" ? uri.trim().toLowerCase() : uri;
266
+ if (typeof uri !== "string") return uri;
267
+ let normalized = normalizedUriCache.get(uri);
268
+ if (normalized === void 0) {
269
+ normalized = uri.trim().toLowerCase();
270
+ normalizedUriCache.set(uri, normalized);
271
+ }
272
+ return normalized;
273
273
  };
274
274
  const primaryNamespaceUris = primaryNamespace && namespaceUris[primaryNamespace] ? namespaceUris[primaryNamespace].map(normalizeNamespaceUri) : void 0;
275
275
  const resolveNamespacePrefix = (uri, localName, fallback) => {
@@ -309,13 +309,17 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
309
309
  const traverseAndNormalize = (object, parentContext = {}) => {
310
310
  if (Array.isArray(object)) return object.map((item) => traverseAndNormalize(item, parentContext));
311
311
  if (!isObject(object)) return object;
312
- const normalizedObject = {};
313
- const keyGroups = /* @__PURE__ */ new Map();
314
312
  const declarations = extractNamespaceDeclarations(object);
315
- const currentContext = {
313
+ let hasDeclarations = false;
314
+ for (const _ in declarations) {
315
+ hasDeclarations = true;
316
+ break;
317
+ }
318
+ const currentContext = hasDeclarations ? {
316
319
  ...parentContext,
317
320
  ...declarations
318
- };
321
+ } : parentContext;
322
+ const normalizedObject = {};
319
323
  for (const key in object) {
320
324
  const value = object[key];
321
325
  if (key.indexOf("@xmlns") === 0) {
@@ -324,11 +328,12 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
324
328
  }
325
329
  const normalizedKey = normalizeKey(key, currentContext);
326
330
  const normalizedValue = traverseAndNormalize(value, currentContext);
327
- if (!keyGroups.has(normalizedKey)) keyGroups.set(normalizedKey, []);
328
- const group = keyGroups.get(normalizedKey);
329
- if (group) group.push(normalizedValue);
331
+ if (normalizedKey in normalizedObject) {
332
+ const existing = normalizedObject[normalizedKey];
333
+ if (Array.isArray(existing)) existing.push(normalizedValue);
334
+ else normalizedObject[normalizedKey] = [existing, normalizedValue];
335
+ } else normalizedObject[normalizedKey] = normalizedValue;
330
336
  }
331
- for (const [normalizedKey, values] of keyGroups) normalizedObject[normalizedKey] = values.length === 1 ? values[0] : values;
332
337
  return normalizedObject;
333
338
  };
334
339
  const normalizeRoot = (object) => {
@@ -22,7 +22,6 @@ import { WfwNs } from "../../../namespaces/wfw/common/types.cjs";
22
22
  import { YtNs } from "../../../namespaces/yt/common/types.cjs";
23
23
 
24
24
  //#region src/feeds/atom/common/types.d.ts
25
-
26
25
  declare namespace Atom {
27
26
  type Text = string;
28
27
  type Link<TDate extends DateLike> = {
@@ -22,7 +22,6 @@ import { WfwNs } from "../../../namespaces/wfw/common/types.js";
22
22
  import { YtNs } from "../../../namespaces/yt/common/types.js";
23
23
 
24
24
  //#region src/feeds/atom/common/types.d.ts
25
-
26
25
  declare namespace Atom {
27
26
  type Text = string;
28
27
  type Link<TDate extends DateLike> = {
@@ -1,10 +1,11 @@
1
+ const require_config = require('../../../common/config.cjs');
1
2
  const require_utils = require('../../../common/utils.cjs');
2
3
 
3
4
  //#region src/feeds/atom/detect/index.ts
4
5
  const detect = (value) => {
5
6
  if (!require_utils.isNonEmptyString(value)) return false;
6
7
  if (!/(?:^|[\s>])<(?:\w+:)?feed[\s>]/im.test(value)) return false;
7
- const hasAtomNamespace = value.includes("http://www.w3.org/2005/Atom");
8
+ const hasAtomNamespace = require_config.namespaceUris.atom.some((uri) => value.includes(uri));
8
9
  const hasAtomElements = /(<(?:\w+:)?(entry|title|link|id|updated|summary)[\s>])/i.test(value);
9
10
  return hasAtomNamespace || hasAtomElements;
10
11
  };
@@ -1,10 +1,11 @@
1
+ import { namespaceUris } from "../../../common/config.js";
1
2
  import { isNonEmptyString } from "../../../common/utils.js";
2
3
 
3
4
  //#region src/feeds/atom/detect/index.ts
4
5
  const detect = (value) => {
5
6
  if (!isNonEmptyString(value)) return false;
6
7
  if (!/(?:^|[\s>])<(?:\w+:)?feed[\s>]/im.test(value)) return false;
7
- const hasAtomNamespace = value.includes("http://www.w3.org/2005/Atom");
8
+ const hasAtomNamespace = namespaceUris.atom.some((uri) => value.includes(uri));
8
9
  const hasAtomElements = /(<(?:\w+:)?(entry|title|link|id|updated|summary)[\s>])/i.test(value);
9
10
  return hasAtomNamespace || hasAtomElements;
10
11
  };
@@ -1,5 +1,5 @@
1
- const require_utils = require('../../../common/utils.cjs');
2
1
  const require_config = require('../../../common/config.cjs');
2
+ const require_utils = require('../../../common/utils.cjs');
3
3
  const require_config$1 = require('./config.cjs');
4
4
  const require_utils$1 = require('./utils.cjs');
5
5
 
@@ -1,5 +1,5 @@
1
- import { generateXml } from "../../../common/utils.js";
2
1
  import { locales } from "../../../common/config.js";
2
+ import { generateXml } from "../../../common/utils.js";
3
3
  import { builder } from "./config.js";
4
4
  import { generateFeed } from "./utils.js";
5
5
 
@@ -1,5 +1,5 @@
1
- const require_utils = require('../../../common/utils.cjs');
2
1
  const require_config = require('../../../common/config.cjs');
2
+ const require_utils = require('../../../common/utils.cjs');
3
3
  const require_utils$1 = require('../../../namespaces/admin/generate/utils.cjs');
4
4
  const require_utils$2 = require('../../../namespaces/app/generate/utils.cjs');
5
5
  const require_utils$3 = require('../../../namespaces/arxiv/generate/utils.cjs');
@@ -29,7 +29,7 @@ const createNamespaceSetter = (prefix) => {
29
29
  const generateText = (text) => {
30
30
  return require_utils.generateCdataString(text);
31
31
  };
32
- const generateLink$1 = (link) => {
32
+ const generateLink = (link) => {
33
33
  if (!require_utils.isObject(link)) return;
34
34
  return require_utils.trimObject({
35
35
  "@href": require_utils.generatePlainString(link.href),
@@ -77,7 +77,7 @@ const generateSource = (source, options) => {
77
77
  [key("generator")]: generateGenerator(source.generator),
78
78
  [key("icon")]: require_utils.generateCdataString(source.icon),
79
79
  [key("id")]: require_utils.generateCdataString(source.id),
80
- [key("link")]: require_utils.trimArray(source.links, (link) => generateLink$1(link, options)),
80
+ [key("link")]: require_utils.trimArray(source.links, (link) => generateLink(link, options)),
81
81
  [key("logo")]: require_utils.generateCdataString(source.logo),
82
82
  [key("rights")]: generateText(source.rights),
83
83
  [key("subtitle")]: generateText(source.subtitle),
@@ -85,7 +85,7 @@ const generateSource = (source, options) => {
85
85
  [key("updated")]: require_utils.generateRfc3339Date(source.updated)
86
86
  });
87
87
  };
88
- const generateEntry$2 = (entry, options) => {
88
+ const generateEntry = (entry, options) => {
89
89
  if (!require_utils.isObject(entry)) return;
90
90
  const key = createNamespaceSetter(options?.prefix);
91
91
  const trimmedValue = require_utils.trimObject({
@@ -94,7 +94,7 @@ const generateEntry$2 = (entry, options) => {
94
94
  [key("content")]: generateText(entry.content),
95
95
  [key("contributor")]: require_utils.trimArray(entry.contributors, (contributor) => generatePerson(contributor, options)),
96
96
  [key("id")]: require_utils.generateCdataString(entry.id),
97
- [key("link")]: require_utils.trimArray(entry.links, (link) => generateLink$1(link, options)),
97
+ [key("link")]: require_utils.trimArray(entry.links, (link) => generateLink(link, options)),
98
98
  [key("published")]: require_utils.generateRfc3339Date(entry.published),
99
99
  [key("rights")]: generateText(entry.rights),
100
100
  [key("source")]: generateSource(entry.source),
@@ -126,7 +126,7 @@ const generateEntry$2 = (entry, options) => {
126
126
  ...require_utils$19.generateItem(entry.trackback)
127
127
  };
128
128
  };
129
- const generateFeed$7 = (feed, options) => {
129
+ const generateFeed = (feed, options) => {
130
130
  if (!require_utils.isObject(feed)) return;
131
131
  const key = createNamespaceSetter(options?.prefix);
132
132
  const valueFeed = require_utils.trimObject({
@@ -136,14 +136,14 @@ const generateFeed$7 = (feed, options) => {
136
136
  [key("generator")]: generateGenerator(feed.generator),
137
137
  [key("icon")]: require_utils.generateCdataString(feed.icon),
138
138
  [key("id")]: require_utils.generateCdataString(feed.id),
139
- [key("link")]: require_utils.trimArray(feed.links, (link) => generateLink$1(link, options)),
139
+ [key("link")]: require_utils.trimArray(feed.links, (link) => generateLink(link, options)),
140
140
  [key("logo")]: require_utils.generateCdataString(feed.logo),
141
141
  [key("rights")]: generateText(feed.rights),
142
142
  [key("subtitle")]: generateText(feed.subtitle),
143
143
  [key("title")]: generateText(feed.title),
144
144
  [key("updated")]: require_utils.generateRfc3339Date(feed.updated)
145
145
  });
146
- const valueEntries = require_utils.trimObject({ [key("entry")]: require_utils.trimArray(feed.entries, (entry) => generateEntry$2(entry, options)) });
146
+ const valueEntries = require_utils.trimObject({ [key("entry")]: require_utils.trimArray(feed.entries, (entry) => generateEntry(entry, options)) });
147
147
  if (!valueFeed && !valueEntries) return;
148
148
  if (options?.asNamespace) return { feed: {
149
149
  ...valueFeed,
@@ -175,5 +175,5 @@ const generateFeed$7 = (feed, options) => {
175
175
  };
176
176
 
177
177
  //#endregion
178
- exports.generateEntry = generateEntry$2;
179
- exports.generateFeed = generateFeed$7;
178
+ exports.generateEntry = generateEntry;
179
+ exports.generateFeed = generateFeed;