feedsmith 2.9.1 → 3.0.0-beta.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 (166) hide show
  1. package/README.md +3 -9
  2. package/dist/common/config.cjs +2 -1
  3. package/dist/common/config.js +2 -1
  4. package/dist/common/errors.cjs +30 -0
  5. package/dist/common/errors.d.cts +15 -0
  6. package/dist/common/errors.d.ts +15 -0
  7. package/dist/common/errors.js +27 -0
  8. package/dist/common/parse.cjs +2 -1
  9. package/dist/common/parse.d.cts +10 -11
  10. package/dist/common/parse.d.ts +10 -11
  11. package/dist/common/parse.js +2 -1
  12. package/dist/common/types.d.cts +24 -12
  13. package/dist/common/types.d.ts +24 -12
  14. package/dist/common/utils.cjs +65 -34
  15. package/dist/common/utils.js +66 -33
  16. package/dist/feeds/atom/common/types.d.cts +65 -52
  17. package/dist/feeds/atom/common/types.d.ts +65 -52
  18. package/dist/feeds/atom/detect/index.cjs +4 -2
  19. package/dist/feeds/atom/detect/index.js +4 -2
  20. package/dist/feeds/atom/generate/index.cjs +2 -1
  21. package/dist/feeds/atom/generate/index.d.cts +2 -2
  22. package/dist/feeds/atom/generate/index.d.ts +2 -2
  23. package/dist/feeds/atom/generate/index.js +2 -1
  24. package/dist/feeds/atom/generate/utils.cjs +23 -6
  25. package/dist/feeds/atom/generate/utils.js +20 -3
  26. package/dist/feeds/atom/parse/index.cjs +10 -3
  27. package/dist/feeds/atom/parse/index.d.cts +2 -2
  28. package/dist/feeds/atom/parse/index.d.ts +2 -2
  29. package/dist/feeds/atom/parse/index.js +10 -3
  30. package/dist/feeds/atom/parse/utils.cjs +61 -31
  31. package/dist/feeds/atom/parse/utils.js +59 -29
  32. package/dist/feeds/json/common/types.d.cts +19 -19
  33. package/dist/feeds/json/common/types.d.ts +19 -19
  34. package/dist/feeds/json/generate/index.cjs +5 -1
  35. package/dist/feeds/json/generate/index.d.cts +2 -2
  36. package/dist/feeds/json/generate/index.d.ts +2 -2
  37. package/dist/feeds/json/generate/index.js +5 -1
  38. package/dist/feeds/json/generate/utils.cjs +7 -2
  39. package/dist/feeds/json/generate/utils.js +8 -3
  40. package/dist/feeds/json/parse/index.cjs +3 -2
  41. package/dist/feeds/json/parse/index.d.cts +2 -2
  42. package/dist/feeds/json/parse/index.d.ts +2 -2
  43. package/dist/feeds/json/parse/index.js +3 -2
  44. package/dist/feeds/json/parse/utils.cjs +4 -4
  45. package/dist/feeds/json/parse/utils.js +4 -4
  46. package/dist/feeds/rdf/common/types.d.cts +31 -28
  47. package/dist/feeds/rdf/common/types.d.ts +31 -28
  48. package/dist/feeds/rdf/detect/index.cjs +4 -2
  49. package/dist/feeds/rdf/detect/index.js +4 -2
  50. package/dist/feeds/rdf/parse/index.cjs +10 -3
  51. package/dist/feeds/rdf/parse/index.d.cts +2 -2
  52. package/dist/feeds/rdf/parse/index.d.ts +2 -2
  53. package/dist/feeds/rdf/parse/index.js +10 -3
  54. package/dist/feeds/rdf/parse/utils.cjs +23 -20
  55. package/dist/feeds/rdf/parse/utils.js +15 -12
  56. package/dist/feeds/rss/common/types.d.cts +76 -74
  57. package/dist/feeds/rss/common/types.d.ts +76 -74
  58. package/dist/feeds/rss/detect/index.cjs +6 -3
  59. package/dist/feeds/rss/detect/index.js +6 -3
  60. package/dist/feeds/rss/generate/index.cjs +2 -1
  61. package/dist/feeds/rss/generate/index.d.cts +2 -2
  62. package/dist/feeds/rss/generate/index.d.ts +2 -2
  63. package/dist/feeds/rss/generate/index.js +2 -1
  64. package/dist/feeds/rss/generate/utils.cjs +36 -36
  65. package/dist/feeds/rss/generate/utils.js +10 -10
  66. package/dist/feeds/rss/parse/index.cjs +10 -3
  67. package/dist/feeds/rss/parse/index.d.cts +2 -2
  68. package/dist/feeds/rss/parse/index.d.ts +2 -2
  69. package/dist/feeds/rss/parse/index.js +10 -3
  70. package/dist/feeds/rss/parse/utils.cjs +138 -38
  71. package/dist/feeds/rss/parse/utils.js +119 -19
  72. package/dist/index.cjs +5 -0
  73. package/dist/index.d.cts +37 -16
  74. package/dist/index.d.ts +37 -16
  75. package/dist/index.js +2 -1
  76. package/dist/namespaces/app/common/types.d.cts +1 -3
  77. package/dist/namespaces/app/common/types.d.ts +1 -3
  78. package/dist/namespaces/app/parse/utils.cjs +2 -2
  79. package/dist/namespaces/app/parse/utils.js +2 -2
  80. package/dist/namespaces/atom/common/types.d.cts +3 -3
  81. package/dist/namespaces/atom/common/types.d.ts +3 -3
  82. package/dist/namespaces/atom/parse/utils.cjs +4 -2
  83. package/dist/namespaces/atom/parse/utils.js +4 -2
  84. package/dist/namespaces/dc/common/types.d.cts +4 -19
  85. package/dist/namespaces/dc/common/types.d.ts +4 -19
  86. package/dist/namespaces/dc/generate/utils.cjs +15 -15
  87. package/dist/namespaces/dc/generate/utils.js +16 -16
  88. package/dist/namespaces/dc/parse/utils.cjs +4 -17
  89. package/dist/namespaces/dc/parse/utils.js +5 -18
  90. package/dist/namespaces/dcterms/common/types.d.cts +23 -59
  91. package/dist/namespaces/dcterms/common/types.d.ts +23 -59
  92. package/dist/namespaces/dcterms/generate/utils.cjs +55 -55
  93. package/dist/namespaces/dcterms/generate/utils.js +56 -56
  94. package/dist/namespaces/dcterms/parse/utils.cjs +23 -57
  95. package/dist/namespaces/dcterms/parse/utils.js +24 -58
  96. package/dist/namespaces/georss/common/types.d.cts +21 -19
  97. package/dist/namespaces/georss/common/types.d.ts +21 -19
  98. package/dist/namespaces/georss/generate/utils.cjs +2 -2
  99. package/dist/namespaces/georss/generate/utils.js +2 -2
  100. package/dist/namespaces/georss/parse/utils.cjs +2 -1
  101. package/dist/namespaces/georss/parse/utils.js +2 -1
  102. package/dist/namespaces/googleplay/common/types.d.cts +9 -7
  103. package/dist/namespaces/googleplay/common/types.d.ts +9 -7
  104. package/dist/namespaces/itunes/common/types.d.cts +9 -5
  105. package/dist/namespaces/itunes/common/types.d.ts +9 -5
  106. package/dist/namespaces/itunes/parse/utils.cjs +3 -2
  107. package/dist/namespaces/itunes/parse/utils.js +3 -2
  108. package/dist/namespaces/media/common/types.d.cts +85 -84
  109. package/dist/namespaces/media/common/types.d.ts +85 -84
  110. package/dist/namespaces/media/generate/utils.cjs +1 -1
  111. package/dist/namespaces/media/generate/utils.js +2 -2
  112. package/dist/namespaces/media/parse/utils.cjs +1 -2
  113. package/dist/namespaces/media/parse/utils.js +1 -2
  114. package/dist/namespaces/opensearch/common/types.d.cts +7 -5
  115. package/dist/namespaces/opensearch/common/types.d.ts +7 -5
  116. package/dist/namespaces/podcast/common/types.d.cts +136 -142
  117. package/dist/namespaces/podcast/common/types.d.ts +136 -142
  118. package/dist/namespaces/podcast/generate/utils.cjs +6 -6
  119. package/dist/namespaces/podcast/generate/utils.js +7 -7
  120. package/dist/namespaces/podcast/parse/utils.cjs +17 -21
  121. package/dist/namespaces/podcast/parse/utils.js +17 -21
  122. package/dist/namespaces/prism/common/types.d.cts +2 -4
  123. package/dist/namespaces/prism/common/types.d.ts +2 -4
  124. package/dist/namespaces/prism/parse/utils.cjs +19 -19
  125. package/dist/namespaces/prism/parse/utils.js +19 -19
  126. package/dist/namespaces/psc/common/types.d.cts +8 -6
  127. package/dist/namespaces/psc/common/types.d.ts +8 -6
  128. package/dist/namespaces/rawvoice/common/types.d.cts +24 -24
  129. package/dist/namespaces/rawvoice/common/types.d.ts +24 -24
  130. package/dist/namespaces/rawvoice/parse/utils.cjs +8 -8
  131. package/dist/namespaces/rawvoice/parse/utils.js +8 -8
  132. package/dist/namespaces/source/common/types.d.cts +20 -18
  133. package/dist/namespaces/source/common/types.d.ts +20 -18
  134. package/dist/namespaces/spotify/common/types.d.cts +22 -20
  135. package/dist/namespaces/spotify/common/types.d.ts +22 -20
  136. package/dist/namespaces/sy/common/types.d.cts +1 -3
  137. package/dist/namespaces/sy/common/types.d.ts +1 -3
  138. package/dist/namespaces/sy/parse/utils.cjs +2 -2
  139. package/dist/namespaces/sy/parse/utils.js +2 -2
  140. package/dist/namespaces/thr/common/types.d.cts +7 -7
  141. package/dist/namespaces/thr/common/types.d.ts +7 -7
  142. package/dist/namespaces/thr/parse/utils.cjs +2 -2
  143. package/dist/namespaces/thr/parse/utils.js +2 -2
  144. package/dist/namespaces/xml/common/types.d.cts +11 -0
  145. package/dist/namespaces/xml/common/types.d.ts +11 -0
  146. package/dist/namespaces/xml/generate/utils.cjs +13 -0
  147. package/dist/namespaces/xml/generate/utils.js +13 -0
  148. package/dist/namespaces/xml/parse/utils.cjs +13 -0
  149. package/dist/namespaces/xml/parse/utils.js +13 -0
  150. package/dist/opml/common/types.d.cts +18 -13
  151. package/dist/opml/common/types.d.ts +18 -13
  152. package/dist/opml/generate/index.cjs +2 -1
  153. package/dist/opml/generate/index.d.cts +3 -3
  154. package/dist/opml/generate/index.d.ts +3 -3
  155. package/dist/opml/generate/index.js +2 -1
  156. package/dist/opml/parse/index.cjs +9 -2
  157. package/dist/opml/parse/index.d.cts +2 -3
  158. package/dist/opml/parse/index.d.ts +2 -3
  159. package/dist/opml/parse/index.js +9 -2
  160. package/dist/opml/parse/utils.cjs +5 -5
  161. package/dist/opml/parse/utils.js +5 -5
  162. package/package.json +7 -24
  163. package/dist/types.cjs +0 -0
  164. package/dist/types.d.cts +0 -7
  165. package/dist/types.d.ts +0 -7
  166. package/dist/types.js +0 -1
package/README.md CHANGED
@@ -18,13 +18,6 @@ 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
-
28
21
  ## Features
29
22
 
30
23
  ### Core
@@ -53,7 +46,7 @@ Feedsmith offers universal and format‑specific parsers that maintain the origi
53
46
 
54
47
  ## Supported Formats
55
48
 
56
- Feedsmith aims to fully support all major feed formats and namespaces in complete alignment with their specifications.
49
+ Feedsmith aims to fully support all major feed formats and namespaces in complete alignment with their specs.
57
50
 
58
51
  ✅ Available
59
52
    ·  
@@ -112,6 +105,7 @@ Feedsmith aims to fully support all major feed formats and namespaces in complet
112
105
  | [W3C Basic Geo](https://feedsmith.dev/reference/namespaces/geo) | `<geo:*>` | RSS, Atom | ✅ | ✅ |
113
106
  | [GeoRSS Simple](https://feedsmith.dev/reference/namespaces/georss) | `<georss:*>` | RSS, Atom, RDF | ✅ | ✅ |
114
107
  | [RDF](https://feedsmith.dev/reference/namespaces/rdf) | `<rdf:*>` | RDF | ✅ | ✅ |
108
+ | [XML](https://feedsmith.dev/reference/namespaces/xml) | `<xml:*>` | RSS, Atom, RDF | ✅ | ✅ |
115
109
 
116
110
  ## Quick Start
117
111
 
@@ -231,7 +225,7 @@ try {
231
225
  Feedsmith provides comprehensive TypeScript types for all feed formats:
232
226
 
233
227
  ```typescript
234
- import type { Rss, Atom, Json, Opml } from 'feedsmith/types'
228
+ import type { Rss, Atom, Json, Opml } from 'feedsmith'
235
229
 
236
230
  // Access all types for a format
237
231
  type Feed = Rss.Feed
@@ -61,7 +61,8 @@ const locales = {
61
61
  invalidOpmlFormat: "Invalid OPML format",
62
62
  invalidInputOpml: "Invalid input OPML",
63
63
  invalidInputAtom: "Invalid input Atom",
64
- invalidInputRss: "Invalid input RSS"
64
+ invalidInputRss: "Invalid input RSS",
65
+ invalidInputJson: "Invalid input JSON"
65
66
  };
66
67
  const namespaceUris = {
67
68
  admin: require_config$1.uris,
@@ -61,7 +61,8 @@ const locales = {
61
61
  invalidOpmlFormat: "Invalid OPML format",
62
62
  invalidInputOpml: "Invalid input OPML",
63
63
  invalidInputAtom: "Invalid input Atom",
64
- invalidInputRss: "Invalid input RSS"
64
+ invalidInputRss: "Invalid input RSS",
65
+ invalidInputJson: "Invalid input JSON"
65
66
  };
66
67
  const namespaceUris = {
67
68
  admin: uris$1,
@@ -0,0 +1,30 @@
1
+ //#region src/common/errors.ts
2
+ var DetectError = class extends Error {
3
+ constructor(message) {
4
+ super(message);
5
+ this.name = "DetectError";
6
+ }
7
+ };
8
+ var GenerateError = class extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = "GenerateError";
12
+ }
13
+ };
14
+ var MalformedError = class extends Error {
15
+ constructor(message) {
16
+ super(message);
17
+ this.name = "MalformedError";
18
+ }
19
+ };
20
+ var ParseError = class extends Error {
21
+ constructor(message) {
22
+ super(message);
23
+ this.name = "ParseError";
24
+ }
25
+ };
26
+ //#endregion
27
+ exports.DetectError = DetectError;
28
+ exports.GenerateError = GenerateError;
29
+ exports.MalformedError = MalformedError;
30
+ exports.ParseError = ParseError;
@@ -0,0 +1,15 @@
1
+ //#region src/common/errors.d.ts
2
+ declare class DetectError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ declare class GenerateError extends Error {
6
+ constructor(message: string);
7
+ }
8
+ declare class MalformedError extends Error {
9
+ constructor(message: string);
10
+ }
11
+ declare class ParseError extends Error {
12
+ constructor(message: string);
13
+ }
14
+ //#endregion
15
+ export { DetectError, GenerateError, MalformedError, ParseError };
@@ -0,0 +1,15 @@
1
+ //#region src/common/errors.d.ts
2
+ declare class DetectError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ declare class GenerateError extends Error {
6
+ constructor(message: string);
7
+ }
8
+ declare class MalformedError extends Error {
9
+ constructor(message: string);
10
+ }
11
+ declare class ParseError extends Error {
12
+ constructor(message: string);
13
+ }
14
+ //#endregion
15
+ export { DetectError, GenerateError, MalformedError, ParseError };
@@ -0,0 +1,27 @@
1
+ //#region src/common/errors.ts
2
+ var DetectError = class extends Error {
3
+ constructor(message) {
4
+ super(message);
5
+ this.name = "DetectError";
6
+ }
7
+ };
8
+ var GenerateError = class extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = "GenerateError";
12
+ }
13
+ };
14
+ var MalformedError = class extends Error {
15
+ constructor(message) {
16
+ super(message);
17
+ this.name = "MalformedError";
18
+ }
19
+ };
20
+ var ParseError = class extends Error {
21
+ constructor(message) {
22
+ super(message);
23
+ this.name = "ParseError";
24
+ }
25
+ };
26
+ //#endregion
27
+ export { DetectError, GenerateError, MalformedError, ParseError };
@@ -1,3 +1,4 @@
1
+ const require_errors = require("./errors.cjs");
1
2
  const require_config = require("./config.cjs");
2
3
  const require_utils = require("./utils.cjs");
3
4
  const require_index = require("../feeds/atom/detect/index.cjs");
@@ -27,7 +28,7 @@ const parse = (value, options) => {
27
28
  format: "json",
28
29
  feed: require_index$3.parse(json, options)
29
30
  };
30
- throw new Error(require_config.locales.unrecognizedFeedFormat);
31
+ throw new require_errors.DetectError(require_config.locales.unrecognizedFeedFormat);
31
32
  };
32
33
  //#endregion
33
34
  exports.parse = parse;
@@ -1,23 +1,22 @@
1
- import { DeepPartial, ParseOptions } from "./types.cjs";
1
+ import { ParseMainOptions } from "./types.cjs";
2
2
  import { Atom } from "../feeds/atom/common/types.cjs";
3
3
  import { Json } from "../feeds/json/common/types.cjs";
4
4
  import { Rdf } from "../feeds/rdf/common/types.cjs";
5
5
  import { Rss } from "../feeds/rss/common/types.cjs";
6
6
 
7
7
  //#region src/common/parse.d.ts
8
- type Parse = (value: unknown, options?: ParseOptions) => {
9
- format: 'rss';
10
- feed: DeepPartial<Rss.Feed<string>>;
8
+ declare const parse: <TDate = string>(value: unknown, options?: ParseMainOptions<TDate>) => {
9
+ format: "rss";
10
+ feed: Rss.Feed<TDate>;
11
11
  } | {
12
- format: 'atom';
13
- feed: DeepPartial<Atom.Feed<string>>;
12
+ format: "atom";
13
+ feed: Atom.Feed<TDate>;
14
14
  } | {
15
- format: 'rdf';
16
- feed: DeepPartial<Rdf.Feed<string>>;
15
+ format: "rdf";
16
+ feed: Rdf.Feed<TDate>;
17
17
  } | {
18
- format: 'json';
19
- feed: DeepPartial<Json.Feed<string>>;
18
+ format: "json";
19
+ feed: Json.Feed<TDate>;
20
20
  };
21
- declare const parse: Parse;
22
21
  //#endregion
23
22
  export { parse };
@@ -1,23 +1,22 @@
1
- import { DeepPartial, ParseOptions } from "./types.js";
1
+ import { ParseMainOptions } from "./types.js";
2
2
  import { Atom } from "../feeds/atom/common/types.js";
3
3
  import { Json } from "../feeds/json/common/types.js";
4
4
  import { Rdf } from "../feeds/rdf/common/types.js";
5
5
  import { Rss } from "../feeds/rss/common/types.js";
6
6
 
7
7
  //#region src/common/parse.d.ts
8
- type Parse = (value: unknown, options?: ParseOptions) => {
9
- format: 'rss';
10
- feed: DeepPartial<Rss.Feed<string>>;
8
+ declare const parse: <TDate = string>(value: unknown, options?: ParseMainOptions<TDate>) => {
9
+ format: "rss";
10
+ feed: Rss.Feed<TDate>;
11
11
  } | {
12
- format: 'atom';
13
- feed: DeepPartial<Atom.Feed<string>>;
12
+ format: "atom";
13
+ feed: Atom.Feed<TDate>;
14
14
  } | {
15
- format: 'rdf';
16
- feed: DeepPartial<Rdf.Feed<string>>;
15
+ format: "rdf";
16
+ feed: Rdf.Feed<TDate>;
17
17
  } | {
18
- format: 'json';
19
- feed: DeepPartial<Json.Feed<string>>;
18
+ format: "json";
19
+ feed: Json.Feed<TDate>;
20
20
  };
21
- declare const parse: Parse;
22
21
  //#endregion
23
22
  export { parse };
@@ -1,3 +1,4 @@
1
+ import { DetectError } from "./errors.js";
1
2
  import { locales } from "./config.js";
2
3
  import { parseJsonObject } from "./utils.js";
3
4
  import { detect } from "../feeds/atom/detect/index.js";
@@ -27,7 +28,7 @@ const parse = (value, options) => {
27
28
  format: "json",
28
29
  feed: parse$2(json, options)
29
30
  };
30
- throw new Error(locales.unrecognizedFeedFormat);
31
+ throw new DetectError(locales.unrecognizedFeedFormat);
31
32
  };
32
33
  //#endregion
33
34
  export { parse };
@@ -1,12 +1,24 @@
1
1
  //#region src/common/types.d.ts
2
- type DateLike = Date | string;
3
- type ExtraFields<F extends ReadonlyArray<string>, V = unknown> = { [K in F[number]]?: V };
2
+ type Simplify<T> = { [K in keyof T]: T[K] };
3
+ type Unwrap<T> = T extends {
4
+ __requirable: infer U;
5
+ } ? U : T;
6
+ type HasMarker<T> = [Extract<T, {
7
+ __requirable: unknown;
8
+ }>] extends [never] ? false : true;
9
+ type Optional<T> = { [K in keyof T]-?: object extends Pick<T, K> ? K : never }[keyof T];
10
+ type Marked<T> = { [K in keyof T]-?: HasMarker<T[K]> extends true ? K : never }[keyof T];
11
+ type Strict<T, S extends boolean> = Simplify<S extends true ? { [K in Exclude<keyof T, Optional<T>>]-?: Unwrap<T[K]> } & { [K in Optional<T>]?: Unwrap<T[K]> } : { [K in Exclude<keyof T, Optional<T> | Marked<T>>]-?: Unwrap<T[K]> } & { [K in Optional<T> | Marked<T>]?: Unwrap<T[K]> }>;
4
12
  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
- type RemoveUndefined<T> = T extends undefined ? never : T;
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
13
  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;
8
- type ParseOptions = {
14
+ type Requirable<T> = T | {
15
+ __requirable: T;
16
+ };
17
+ type DateLike = Date | string;
18
+ type ExtraFields<F extends ReadonlyArray<string>, V = unknown> = { [K in F[number]]?: V };
19
+ type ParseMainOptions<TDate> = {
9
20
  maxItems?: number;
21
+ parseDateFn?: (raw: string) => TDate;
10
22
  };
11
23
  type XmlStylesheet = {
12
24
  type: string;
@@ -16,14 +28,14 @@ type XmlStylesheet = {
16
28
  charset?: string;
17
29
  alternate?: boolean;
18
30
  };
19
- type XmlGenerateOptions<O, F extends boolean = false> = O & {
20
- lenient?: F;
31
+ type GenerateMainXmlOptions<O, S extends boolean> = O & {
32
+ strict?: S;
21
33
  stylesheets?: Array<XmlStylesheet>;
22
34
  };
23
- type JsonGenerateOptions<O, F extends boolean = false> = O & {
24
- lenient?: F;
35
+ type GenerateMainXml<LV, SV, O = Record<string, unknown>> = <S extends boolean = false>(value: S extends true ? SV : LV, options?: GenerateMainXmlOptions<O, S>) => string;
36
+ type GenerateMainJsonOptions<O, S extends boolean> = O & {
37
+ strict?: S;
25
38
  };
26
- type XmlGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: XmlGenerateOptions<O, F>) => string;
27
- type JsonGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: JsonGenerateOptions<O, F>) => unknown;
39
+ type GenerateMainJson<LV, SV, O = Record<string, unknown>> = <S extends boolean = false>(value: S extends true ? SV : LV, options?: GenerateMainJsonOptions<O, S>) => unknown;
28
40
  //#endregion
29
- export { DateLike, DeepOmit, DeepPartial, ExtraFields, JsonGenerateMain, ParseOptions, XmlGenerateMain, XmlGenerateOptions };
41
+ export { DateLike, DeepOmit, ExtraFields, GenerateMainJson, GenerateMainXml, GenerateMainXmlOptions, ParseMainOptions, Requirable, Strict, XmlStylesheet };
@@ -1,12 +1,24 @@
1
1
  //#region src/common/types.d.ts
2
- type DateLike = Date | string;
3
- type ExtraFields<F extends ReadonlyArray<string>, V = unknown> = { [K in F[number]]?: V };
2
+ type Simplify<T> = { [K in keyof T]: T[K] };
3
+ type Unwrap<T> = T extends {
4
+ __requirable: infer U;
5
+ } ? U : T;
6
+ type HasMarker<T> = [Extract<T, {
7
+ __requirable: unknown;
8
+ }>] extends [never] ? false : true;
9
+ type Optional<T> = { [K in keyof T]-?: object extends Pick<T, K> ? K : never }[keyof T];
10
+ type Marked<T> = { [K in keyof T]-?: HasMarker<T[K]> extends true ? K : never }[keyof T];
11
+ type Strict<T, S extends boolean> = Simplify<S extends true ? { [K in Exclude<keyof T, Optional<T>>]-?: Unwrap<T[K]> } & { [K in Optional<T>]?: Unwrap<T[K]> } : { [K in Exclude<keyof T, Optional<T> | Marked<T>>]-?: Unwrap<T[K]> } & { [K in Optional<T> | Marked<T>]?: Unwrap<T[K]> }>;
4
12
  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
- type RemoveUndefined<T> = T extends undefined ? never : T;
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
13
  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;
8
- type ParseOptions = {
14
+ type Requirable<T> = T | {
15
+ __requirable: T;
16
+ };
17
+ type DateLike = Date | string;
18
+ type ExtraFields<F extends ReadonlyArray<string>, V = unknown> = { [K in F[number]]?: V };
19
+ type ParseMainOptions<TDate> = {
9
20
  maxItems?: number;
21
+ parseDateFn?: (raw: string) => TDate;
10
22
  };
11
23
  type XmlStylesheet = {
12
24
  type: string;
@@ -16,14 +28,14 @@ type XmlStylesheet = {
16
28
  charset?: string;
17
29
  alternate?: boolean;
18
30
  };
19
- type XmlGenerateOptions<O, F extends boolean = false> = O & {
20
- lenient?: F;
31
+ type GenerateMainXmlOptions<O, S extends boolean> = O & {
32
+ strict?: S;
21
33
  stylesheets?: Array<XmlStylesheet>;
22
34
  };
23
- type JsonGenerateOptions<O, F extends boolean = false> = O & {
24
- lenient?: F;
35
+ type GenerateMainXml<LV, SV, O = Record<string, unknown>> = <S extends boolean = false>(value: S extends true ? SV : LV, options?: GenerateMainXmlOptions<O, S>) => string;
36
+ type GenerateMainJsonOptions<O, S extends boolean> = O & {
37
+ strict?: S;
25
38
  };
26
- type XmlGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: XmlGenerateOptions<O, F>) => string;
27
- type JsonGenerateMain<S, L, O = Record<string, unknown>> = <F extends boolean = false>(value: F extends true ? L : S, options?: JsonGenerateOptions<O, F>) => unknown;
39
+ type GenerateMainJson<LV, SV, O = Record<string, unknown>> = <S extends boolean = false>(value: S extends true ? SV : LV, options?: GenerateMainJsonOptions<O, S>) => unknown;
28
40
  //#endregion
29
- export { DateLike, DeepOmit, DeepPartial, ExtraFields, JsonGenerateMain, ParseOptions, XmlGenerateMain, XmlGenerateOptions };
41
+ export { DateLike, DeepOmit, ExtraFields, GenerateMainJson, GenerateMainXml, GenerateMainXmlOptions, ParseMainOptions, Requirable, Strict, XmlStylesheet };
@@ -26,13 +26,19 @@ const retrieveRdfResourceOrText = (value, parse) => {
26
26
  return parse(retrieveText(value));
27
27
  };
28
28
  const trimObject = (object) => {
29
- let result;
29
+ let hasPresent = false;
30
+ let hasAbsent = false;
31
+ for (const key in object) {
32
+ if (isPresent(object[key])) hasPresent = true;
33
+ else hasAbsent = true;
34
+ if (hasPresent && hasAbsent) break;
35
+ }
36
+ if (!hasPresent) return;
37
+ if (!hasAbsent) return object;
38
+ const result = {};
30
39
  for (const key in object) {
31
40
  const value = object[key];
32
- if (isPresent(value)) {
33
- if (!result) result = {};
34
- result[key] = value;
35
- }
41
+ if (isPresent(value)) result[key] = value;
36
42
  }
37
43
  return result;
38
44
  };
@@ -40,31 +46,23 @@ const trimArray = (value, parse) => {
40
46
  if (!Array.isArray(value) || value.length === 0) return;
41
47
  if (!parse) {
42
48
  let needsTrimming = false;
43
- for (let i = 0; i < value.length; i++) if (!isPresent(value[i])) {
49
+ for (const item of value) if (!isPresent(item)) {
44
50
  needsTrimming = true;
45
51
  break;
46
52
  }
47
53
  if (!needsTrimming) return value;
48
54
  }
49
55
  const result = [];
50
- for (let i = 0; i < value.length; i++) {
51
- const item = parse ? parse(value[i]) : value[i];
56
+ for (const element of value) {
57
+ const item = parse ? parse(element) : element;
52
58
  if (isPresent(item)) result.push(item);
53
59
  }
54
60
  return result.length > 0 ? result : void 0;
55
61
  };
56
- const generateArrayOrSingular = (pluralValues, singularValue, generator) => {
57
- if (isPresent(pluralValues)) return trimArray(pluralValues.map(generator));
58
- if (isPresent(singularValue)) return generator(singularValue);
59
- };
60
- const generateSingularOrArray = (singularValue, pluralValues, generator) => {
61
- if (isPresent(singularValue)) return generator(singularValue);
62
- if (isPresent(pluralValues)) return trimArray(pluralValues.map(generator));
63
- };
64
- const commentStartTag = "<!--";
65
- const commentEndTag = "-->";
66
62
  const cdataStartTag = "<![CDATA[";
67
63
  const cdataEndTag = "]]>";
64
+ const commentStartTag = "<!--";
65
+ const commentEndTag = "-->";
68
66
  const hasEntities = (text) => {
69
67
  const ampIndex = text.indexOf("&");
70
68
  return ampIndex !== -1 && text.indexOf(";", ampIndex) !== -1;
@@ -75,13 +73,13 @@ const stripComments = (text) => {
75
73
  let result = "";
76
74
  let lastIndex = 0;
77
75
  while (currentIndex !== -1) {
78
- result += text.substring(lastIndex, currentIndex);
76
+ result += text.slice(lastIndex, currentIndex);
79
77
  const endIndex = text.indexOf(commentEndTag, currentIndex + 4);
80
78
  if (endIndex === -1) return text;
81
79
  lastIndex = endIndex + 3;
82
80
  currentIndex = text.indexOf(commentStartTag, lastIndex);
83
81
  }
84
- result += text.substring(lastIndex);
82
+ result += text.slice(lastIndex);
85
83
  return result;
86
84
  };
87
85
  const decodeWithCdata = (text) => {
@@ -90,15 +88,15 @@ const decodeWithCdata = (text) => {
90
88
  let result = "";
91
89
  let lastIndex = 0;
92
90
  while (currentIndex !== -1) {
93
- const textBefore = text.substring(lastIndex, currentIndex);
91
+ const textBefore = text.slice(lastIndex, currentIndex);
94
92
  result += hasEntities(textBefore) ? (0, entities.decodeHTML)(textBefore) : textBefore;
95
93
  const endIndex = text.indexOf(cdataEndTag, currentIndex + 9);
96
94
  if (endIndex === -1) return hasEntities(text) ? (0, entities.decodeHTML)(text) : text;
97
- result += text.substring(currentIndex + 9, endIndex);
95
+ result += text.slice(currentIndex + 9, endIndex);
98
96
  lastIndex = endIndex + 3;
99
97
  currentIndex = text.indexOf(cdataStartTag, lastIndex);
100
98
  }
101
- const textAfter = text.substring(lastIndex);
99
+ const textAfter = text.slice(lastIndex);
102
100
  result += hasEntities(textAfter) ? (0, entities.decodeHTML)(textAfter) : textAfter;
103
101
  return result;
104
102
  };
@@ -131,8 +129,10 @@ const parseYesNoBoolean = (value) => {
131
129
  if (boolean !== void 0) return boolean;
132
130
  if (isNonEmptyString(value)) return yesRegex.test(value);
133
131
  };
134
- const parseDate = (value) => {
135
- return parseString(value);
132
+ const parseDate = (value, parseDateFn) => {
133
+ const raw = parseString(value);
134
+ if (raw === void 0) return;
135
+ return parseDateFn ? parseDateFn(raw) : raw;
136
136
  };
137
137
  const parseArray = (value) => {
138
138
  if (Array.isArray(value)) return value;
@@ -300,7 +300,7 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
300
300
  if (isObject(element)) {
301
301
  for (const key in element) if (key === "@xmlns") declarations[""] = normalizeNamespaceUri(element[key]);
302
302
  else if (key.indexOf("@xmlns:") === 0) {
303
- const prefix = key.substring(7);
303
+ const prefix = key.slice(7);
304
304
  declarations[prefix] = normalizeNamespaceUri(element[key]);
305
305
  }
306
306
  }
@@ -312,14 +312,14 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
312
312
  if (useDefault && context[""]) return resolveNamespacePrefix(context[""], name, name);
313
313
  return name;
314
314
  }
315
- const prefix = name.substring(0, colonIndex);
316
- const unprefixedName = name.substring(colonIndex + 1);
315
+ const prefix = name.slice(0, colonIndex);
316
+ const unprefixedName = name.slice(colonIndex + 1);
317
317
  const uri = context[prefix];
318
318
  if (uri) return resolveNamespacePrefix(uri, unprefixedName, name);
319
319
  return name;
320
320
  };
321
321
  const normalizeKey = (key, context) => {
322
- if (key.charCodeAt(0) === 64) return `@${normalizeWithContext(key.substring(1), context, false)}`;
322
+ if (key.charCodeAt(0) === 64) return `@${normalizeWithContext(key.slice(1), context, false)}`;
323
323
  return normalizeWithContext(key, context, true);
324
324
  };
325
325
  const traverseAndNormalize = (object, parentContext = {}) => {
@@ -352,9 +352,40 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
352
352
  }
353
353
  return normalizedObject;
354
354
  };
355
+ const needsNormalization = (object) => {
356
+ const check = (value) => {
357
+ if (Array.isArray(value)) {
358
+ for (const item of value) if (check(item)) return true;
359
+ return false;
360
+ }
361
+ if (!isObject(value)) return false;
362
+ for (const key in value) {
363
+ if (key[0] === "@") {
364
+ if (key === "@xmlns") {
365
+ const normalizedUri = normalizeNamespaceUri(value[key]);
366
+ if (primaryNamespaceUris?.includes(normalizedUri)) continue;
367
+ if (namespacePrefixes[normalizedUri]) return true;
368
+ } else if (key.startsWith("@xmlns:")) {
369
+ const prefix = key.slice(7);
370
+ const normalizedUri = normalizeNamespaceUri(value[key]);
371
+ const standardPrefix = namespacePrefixes[normalizedUri];
372
+ if (standardPrefix) {
373
+ if (primaryNamespaceUris?.includes(normalizedUri)) return true;
374
+ if (standardPrefix !== prefix) return true;
375
+ }
376
+ }
377
+ continue;
378
+ }
379
+ if (key[0] === "#") continue;
380
+ if (check(value[key])) return true;
381
+ }
382
+ return false;
383
+ };
384
+ return check(object);
385
+ };
355
386
  const normalizeRoot = (object) => {
356
387
  if (!isObject(object)) return object;
357
- if (Array.isArray(object)) return object.map(normalizeRoot);
388
+ if (!needsNormalization(object)) return object;
358
389
  const normalizedObject = {};
359
390
  for (const key in object) {
360
391
  const value = object[key];
@@ -366,11 +397,13 @@ const createNamespaceNormalizator = (namespaceUris, namespacePrefixes, primaryNa
366
397
  };
367
398
  return normalizeRoot;
368
399
  };
400
+ const startsWithBraceRegex = /^\s*\{/;
401
+ const endsWithBraceRegex = /\}\s*$/;
369
402
  const parseJsonObject = (value) => {
370
403
  if (isObject(value)) return value;
371
404
  if (!isNonEmptyString(value) || value.length < 2) return;
372
- const startsWithBrace = value.charAt(0) === "{" || /^\s*\{/.test(value);
373
- const endsWithBrace = value.charAt(value.length - 1) === "}" || /\}\s*$/.test(value);
405
+ const startsWithBrace = value.charAt(0) === "{" || startsWithBraceRegex.test(value);
406
+ const endsWithBrace = value.charAt(value.length - 1) === "}" || endsWithBraceRegex.test(value);
374
407
  if (!startsWithBrace || !endsWithBrace) return;
375
408
  try {
376
409
  return JSON.parse(value);
@@ -379,7 +412,6 @@ const parseJsonObject = (value) => {
379
412
  //#endregion
380
413
  exports.createNamespaceNormalizator = createNamespaceNormalizator;
381
414
  exports.detectNamespaces = detectNamespaces;
382
- exports.generateArrayOrSingular = generateArrayOrSingular;
383
415
  exports.generateBoolean = generateBoolean;
384
416
  exports.generateCdataString = generateCdataString;
385
417
  exports.generateCsvOf = generateCsvOf;
@@ -389,7 +421,6 @@ exports.generatePlainString = generatePlainString;
389
421
  exports.generateRdfResource = generateRdfResource;
390
422
  exports.generateRfc3339Date = generateRfc3339Date;
391
423
  exports.generateRfc822Date = generateRfc822Date;
392
- exports.generateSingularOrArray = generateSingularOrArray;
393
424
  exports.generateTextOrCdataString = generateTextOrCdataString;
394
425
  exports.generateXml = generateXml;
395
426
  exports.generateYesNoBoolean = generateYesNoBoolean;