feedsmith 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -45
- package/dist/common/config.js +2 -0
- package/dist/common/config.js.map +1 -1
- package/dist/common/utils.d.ts +6 -5
- package/dist/common/utils.js +83 -52
- package/dist/common/utils.js.map +1 -1
- package/dist/feeds/atom/parse/utils.js +76 -78
- package/dist/feeds/atom/parse/utils.js.map +1 -1
- package/dist/feeds/json/parse/utils.d.ts +0 -1
- package/dist/feeds/json/parse/utils.js +21 -36
- package/dist/feeds/json/parse/utils.js.map +1 -1
- package/dist/feeds/rdf/parse/types.d.ts +2 -2
- package/dist/feeds/rdf/parse/utils.d.ts +3 -3
- package/dist/feeds/rdf/parse/utils.js +24 -21
- package/dist/feeds/rdf/parse/utils.js.map +1 -1
- package/dist/feeds/rss/parse/config.js +3 -1
- package/dist/feeds/rss/parse/config.js.map +1 -1
- package/dist/feeds/rss/parse/utils.js +43 -36
- package/dist/feeds/rss/parse/utils.js.map +1 -1
- package/dist/namespaces/content/utils.js +3 -5
- package/dist/namespaces/content/utils.js.map +1 -1
- package/dist/namespaces/dc/utils.js +3 -5
- package/dist/namespaces/dc/utils.js.map +1 -1
- package/dist/namespaces/itunes/utils.js +16 -22
- package/dist/namespaces/itunes/utils.js.map +1 -1
- package/dist/namespaces/slash/utils.js +6 -11
- package/dist/namespaces/slash/utils.js.map +1 -1
- package/dist/namespaces/sy/utils.js +3 -5
- package/dist/namespaces/sy/utils.js.map +1 -1
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
# Feedsmith
|
|
2
2
|
|
|
3
|
-
Modern JavaScript utility for parsing JSON Feed, Atom, RSS, and RDF feeds, with support for popular namespaces.
|
|
4
|
-
|
|
5
3
|
[](https://github.com/macieklamberski/feedsmith/actions/workflows/test.yml)
|
|
6
4
|
[](https://www.npmjs.com/package/feedsmith)
|
|
7
5
|
[](https://github.com/macieklamberski/feedsmith/blob/main/LICENSE)
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
Modern JavaScript utility for parsing and generating JSON Feed, Atom, RSS, and RDF feeds, with support for popular namespaces. It provides both universal and format-specific parsers that preserve the original feed structure while offering helpful normalization.
|
|
8
|
+
|
|
9
|
+
Feedsmith preserves the original feed structure in a clean object-oriented format while intelligently normalizing legacy elements, giving you complete access to all feed data without sacrificing simplicity.
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
[Features](#supported-feeds-and-namespaces)
|
|
12
|
+
·
|
|
13
|
+
[Installation](#installation)
|
|
14
|
+
·
|
|
15
|
+
[Parsing feeds](#parsing-feeds)
|
|
16
|
+
·
|
|
17
|
+
[Generating feeds](#generating-feeds)
|
|
18
|
+
·
|
|
19
|
+
[Benchmarks](#benchmarks)
|
|
20
|
+
·
|
|
21
|
+
[FAQ](#faq)
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
* Get access to all the information in the feed, as Feedsmith preserves the original feed structure exactly as provided in each specific format. TypeScript type definitions are available for each feed type, making it easy to work with the data.
|
|
23
|
+
---
|
|
18
24
|
|
|
19
25
|
#### Leniency
|
|
20
26
|
* **Normalizes legacy elements** ✨ — Upgrades feed elements to their modern equivalents so that you never need to worry about reading feeds in older formats.
|
|
@@ -22,34 +28,51 @@ Modern JavaScript utility for parsing JSON Feed, Atom, RSS, and RDF feeds, with
|
|
|
22
28
|
|
|
23
29
|
#### Performance and type-safety
|
|
24
30
|
* **Fast parsing** — Built on [fast-xml-parser](https://www.npmjs.com/package/fast-xml-parser) for efficient XML processing.
|
|
25
|
-
* **Type-safe
|
|
26
|
-
* **Well tested** — Comprehensive test suite with over 450 tests and 99.9% code coverage based on `bun test --coverage`.
|
|
31
|
+
* **Type-safe API** — TypeScript type definitions are available for each feed format, making it easy to work with the data.
|
|
27
32
|
* **Tree-shakable** — Only include the parts of the library you need, reducing bundle size.
|
|
33
|
+
* **Well-tested** — Comprehensive test suite with over 450 tests and 99.9% code coverage.
|
|
34
|
+
|
|
35
|
+
#### Compatibility
|
|
36
|
+
* Works in Node.js and all modern browsers.
|
|
37
|
+
* Works with plain JavaScript, you don't need to use TypeScript.
|
|
28
38
|
|
|
29
39
|
## Supported feeds and namespaces
|
|
30
40
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
41
|
+
### Feeds
|
|
42
|
+
|
|
43
|
+
| Format | Versions | Parsing | Generating |
|
|
44
|
+
|--------|----------|---------|------------|
|
|
45
|
+
| [RSS](http://cyber.law.harvard.edu/rss/rss.html) | 0.9x, 2.0 | ✅ | ⏳ |
|
|
46
|
+
| [Atom](https://tools.ietf.org/html/rfc4287) | 0.3, 1.0 | ✅ | ⏳ |
|
|
47
|
+
| [JSON Feed](https://jsonfeed.org) | 1.0, 1.1 | ✅ | ⏳ |
|
|
48
|
+
| [RDF](https://web.resource.org/rss/1.0/spec) | 0.9, 1.0 | ✅ | ⏳ |
|
|
49
|
+
|
|
50
|
+
✅ Available
|
|
51
|
+
·
|
|
52
|
+
⌛️ Work in progress
|
|
53
|
+
|
|
54
|
+
### Namespaces
|
|
55
|
+
|
|
56
|
+
| Name | Prefix | Parsing | Generating |
|
|
57
|
+
|------|--------|---------|------------|
|
|
58
|
+
| [Atom](http://www.w3.org/2005/Atom) | `<atom:*>`, `<a10:*>` | ✅ | ⏳ |
|
|
59
|
+
| [Dublin Core](http://purl.org/dc/elements/1.1/) | `<dc:*>` | ✅ | ⏳ |
|
|
60
|
+
| [Syndication](http://purl.org/rss/1.0/modules/syndication/) | `<sy:*>` | ✅ | ⏳ |
|
|
61
|
+
| [Content](http://purl.org/rss/1.0/modules/content/) | `<content:*>` | ✅ | ⏳ |
|
|
62
|
+
| [Slash](http://purl.org/rss/1.0/modules/slash/) | `<slash:*>` | ✅ | ⏳ |
|
|
63
|
+
| [iTunes](http://www.itunes.com/dtds/podcast-1.0.dtd) | `<itunes:*>` | ✅ | ⏳ |
|
|
64
|
+
| [Podcast](https://podcastindex.org/namespace/1.0) | `<podcast:*>` | ⏳ | ⏳ |
|
|
65
|
+
| [Media RSS](http://search.yahoo.com/mrss/) | `<media:*>` | ⏳ | ⏳ |
|
|
66
|
+
| [Geo RSS](http://www.georss.org/georss) | `<georss:*>` | 📋 | 📋 |
|
|
67
|
+
| [Dublin Core Terms](http://purl.org/dc/terms/) | `<dcterms:*>` | 📋 | 📋 |
|
|
68
|
+
| [Administrative](https://web.resource.org/rss/1.0/modules/admin/) | `<admin:*>` | 📋 | 📋 |
|
|
69
|
+
| [Atom Threading](https://www.ietf.org/rfc/rfc4685.txt) | `<thr:*>` | 📋 | 📋 |
|
|
70
|
+
|
|
71
|
+
✅ Available
|
|
72
|
+
·
|
|
73
|
+
⌛️ Work in progress
|
|
74
|
+
·
|
|
75
|
+
📋 Planned
|
|
53
76
|
|
|
54
77
|
## Installation
|
|
55
78
|
|
|
@@ -59,7 +82,7 @@ npm install feedsmith
|
|
|
59
82
|
|
|
60
83
|
## Parsing feeds
|
|
61
84
|
|
|
62
|
-
### Universal
|
|
85
|
+
### Universal
|
|
63
86
|
|
|
64
87
|
The easiest way to parse any feed is to use the universal `parseFeed` function:
|
|
65
88
|
|
|
@@ -88,20 +111,20 @@ if (type === 'rdf') {
|
|
|
88
111
|
}
|
|
89
112
|
```
|
|
90
113
|
|
|
91
|
-
### Dedicated
|
|
114
|
+
### Dedicated
|
|
92
115
|
|
|
93
116
|
If you know the format in advance, you can use the format-specific parsers:
|
|
94
117
|
|
|
95
118
|
```ts
|
|
96
119
|
import { parseAtomFeed, parseJsonFeed, parseRssFeed, parseRdfFeed } from 'feedsmith'
|
|
97
120
|
|
|
98
|
-
// Parse the feed content
|
|
121
|
+
// Parse the feed content
|
|
99
122
|
const atomFeed = parseAtomFeed(atomContent)
|
|
100
123
|
const jsonFeed = parseJsonFeed(jsonContent)
|
|
101
124
|
const rssFeed = parseRssFeed(rssContent)
|
|
102
125
|
const rdfFeed = parseRdfFeed(rdfContent)
|
|
103
126
|
|
|
104
|
-
// Then read the TypeScript suggestions for the specific feed type
|
|
127
|
+
// Then read the TypeScript suggestions for the specific feed type
|
|
105
128
|
rssFeed.title
|
|
106
129
|
rssFeed.dc?.creator
|
|
107
130
|
rssFeed.dc?.date
|
|
@@ -352,26 +375,81 @@ if (detectRdfFeed(content)) {
|
|
|
352
375
|
}
|
|
353
376
|
```
|
|
354
377
|
|
|
355
|
-
|
|
378
|
+
> [!WARNING]
|
|
379
|
+
> Detect functions are designed to quickly identify the type of feed by searching for the signature of the feed, such as the `<rss>` tag in the case of RSS feeds. However, it is possible for the function to detect an RSS feed, even if that feed is invalid. Only when using the `parseRssFeed` function will the feed be fully validated.
|
|
380
|
+
|
|
381
|
+
## Generating feeds
|
|
382
|
+
|
|
383
|
+
The functionality for generating feeds is currently under development and will be gradually introduced for each feed format. For more information, see the [Supported feeds and namespaces](#supported-feeds-and-namespaces).
|
|
384
|
+
|
|
385
|
+
## Benchmarks
|
|
386
|
+
|
|
387
|
+
A comprehensive set of benchmarks, divided into multiple categories and file sizes, is available in the _/benchmarks_ directory. These benchmarks were performed using both Tinybench and Benchmark.js.
|
|
388
|
+
|
|
389
|
+
[See full benchmark results →](benchmarks/README.md)
|
|
390
|
+
|
|
391
|
+
For a quick overview, here are the results of parsing RSS, Atom and RDF feeds (100KB — 5MB) using various JS packages with Tinybench. Feedsmith's results are marked with an asterisk (`*`).
|
|
392
|
+
|
|
393
|
+
```
|
|
394
|
+
📊 RSS feed parsing (50 files × 100KB — 5MB)
|
|
395
|
+
┌───┬───────────────────────────────┬─────────┬──────────────┬──────────┬──────────┬──────┐
|
|
396
|
+
│ │ Package │ Ops/sec │ Average (ms) │ Min (ms) │ Max (ms) │ Runs │
|
|
397
|
+
├───┼───────────────────────────────┼─────────┼──────────────┼──────────┼──────────┼──────┤
|
|
398
|
+
│ 0 │ feedsmith * │ 7.34 │ 136.167 │ 128.479 │ 173.223 │ 111 │
|
|
399
|
+
│ 1 │ @rowanmanning/feed-parser │ 7.16 │ 139.678 │ 128.722 │ 170.903 │ 108 │
|
|
400
|
+
│ 2 │ @ulisesgascon/rss-feed-parser │ 4.14 │ 241.405 │ 230.806 │ 278.534 │ 63 │
|
|
401
|
+
│ 3 │ feedparser │ 2.50 │ 399.824 │ 374.049 │ 459.730 │ 38 │
|
|
402
|
+
│ 4 │ @extractus/feed-extractor │ 2.26 │ 443.065 │ 430.349 │ 460.195 │ 34 │
|
|
403
|
+
│ 5 │ feedme.js │ 2.05 │ 487.222 │ 443.837 │ 535.029 │ 31 │
|
|
404
|
+
│ 6 │ rss-parser │ 1.66 │ 603.044 │ 573.516 │ 653.683 │ 25 │
|
|
405
|
+
│ 7 │ @gaphub/feed │ 0.94 │ 1068.621 │ 995.044 │ 1138.913 │ 15 │
|
|
406
|
+
└───┴───────────────────────────────┴─────────┴──────────────┴──────────┴──────────┴──────┘
|
|
407
|
+
|
|
408
|
+
📊 Atom feed parsing (50 files × 100KB — 5MB)
|
|
409
|
+
┌───┬───────────────────────────┬─────────┬──────────────┬──────────┬──────────┬──────┐
|
|
410
|
+
│ │ Package │ Ops/sec │ Average (ms) │ Min (ms) │ Max (ms) │ Runs │
|
|
411
|
+
├───┼───────────────────────────┼─────────┼──────────────┼──────────┼──────────┼──────┤
|
|
412
|
+
│ 0 │ feedsmith * │ 0.98 │ 1020.035 │ 998.660 │ 1084.180 │ 15 │
|
|
413
|
+
│ 1 │ @gaphub/feed │ 0.95 │ 1058.126 │ 989.001 │ 1150.486 │ 15 │
|
|
414
|
+
│ 2 │ @rowanmanning/feed-parser │ 0.63 │ 1580.462 │ 1563.357 │ 1607.379 │ 10 │
|
|
415
|
+
│ 3 │ feedparser │ 0.37 │ 2687.488 │ 2624.427 │ 2751.504 │ 6 │
|
|
416
|
+
│ 4 │ @extractus/feed-extractor │ 0.32 │ 3136.880 │ 3107.170 │ 3228.099 │ 5 │
|
|
417
|
+
│ 5 │ feedme.js │ 0.26 │ 3812.545 │ 3759.928 │ 3843.974 │ 4 │
|
|
418
|
+
│ 6 │ rss-parser │ 0.18 │ 5539.014 │ 5479.560 │ 5609.397 │ 3 │
|
|
419
|
+
└───┴───────────────────────────┴─────────┴──────────────┴──────────┴──────────┴──────┘
|
|
420
|
+
|
|
421
|
+
📊 RDF feed parsing (50 files × 100KB — 5MB)
|
|
422
|
+
┌───┬───────────────────────────┬─────────┬──────────────┬──────────┬──────────┬──────┐
|
|
423
|
+
│ │ Package │ Ops/sec │ Average (ms) │ Min (ms) │ Max (ms) │ Runs │
|
|
424
|
+
├───┼───────────────────────────┼─────────┼──────────────┼──────────┼──────────┼──────┤
|
|
425
|
+
│ 0 │ @rowanmanning/feed-parser │ 13.52 │ 73.990 │ 69.404 │ 89.504 │ 203 │
|
|
426
|
+
│ 1 │ feedsmith * │ 10.16 │ 98.396 │ 92.418 │ 118.053 │ 153 │
|
|
427
|
+
│ 2 │ @extractus/feed-extractor │ 3.83 │ 260.946 │ 252.991 │ 274.432 │ 58 │
|
|
428
|
+
│ 3 │ feedparser │ 1.96 │ 509.686 │ 494.823 │ 530.224 │ 30 │
|
|
429
|
+
│ 4 │ feedme.js │ 1.40 │ 714.442 │ 661.440 │ 789.395 │ 22 │
|
|
430
|
+
│ 5 │ rss-parser │ 0.97 │ 1028.245 │ 985.521 │ 1107.122 │ 15 │
|
|
431
|
+
│ 6 │ @gaphub/feed │ 0.97 │ 1031.579 │ 1008.220 │ 1060.322 │ 15 │
|
|
432
|
+
└───┴───────────────────────────┴─────────┴──────────────┴──────────┴──────────┴──────┘
|
|
433
|
+
```
|
|
356
434
|
|
|
357
435
|
## FAQ
|
|
358
436
|
|
|
359
437
|
### Why should I use Feedsmith instead of alternative modules?
|
|
360
438
|
|
|
361
|
-
As stated in the
|
|
439
|
+
As stated in the overview section, the key advantage of Feedsmith is that it preserves the original feed structure exactly as provided in each specific feed format.
|
|
362
440
|
|
|
363
441
|
Many competing packages attempt to normalize data by:
|
|
364
|
-
* Merging distinct fields like `author`, `dc:creator`, and `creator` into a single property.
|
|
365
|
-
* Combining date fields such as `dc:date` and `pubDate` without preserving their source.
|
|
366
|
-
* Handling multiple `<atom:link>` elements inconsistently, sometimes only keeping the first/last one or ignoring different `rel` attributes.
|
|
367
442
|
|
|
368
|
-
|
|
443
|
+
* Merging distinct fields like `author`, `dc:creator`, and `creator` into a single property.
|
|
444
|
+
* Combining date fields such as `dc:date` and `pubDate` without preserving their sources.
|
|
445
|
+
* Handling multiple `<atom:link>` elements inconsistently, sometimes keeping only the first or last one or ignoring different `rel` attributes.
|
|
446
|
+
* Some libraries try to combine different feed formats into one universal structure.
|
|
369
447
|
|
|
370
|
-
|
|
448
|
+
While this approach can be useful for quick reading of feed data, it often results in a loss of information that may be crucial for certain applications, such as reading data from specific namespaces.
|
|
371
449
|
|
|
372
450
|
### Why are date fields returned as strings?
|
|
373
451
|
|
|
374
|
-
In the course of parsing hundreds of thousands of feeds,
|
|
452
|
+
In the course of parsing hundreds of thousands of feeds, I have found that dates in feeds use many different formats. Rather than attempting to parse them all (and potentially introducing errors), dates are returned in their original string form, allowing you to use your preferred date parsing library (or just `Date` object).
|
|
375
453
|
|
|
376
454
|
### Does Feedsmith validate feeds?
|
|
377
455
|
|
package/dist/common/config.js
CHANGED
|
@@ -6,6 +6,8 @@ export const parserConfig = {
|
|
|
6
6
|
parseAttributeValue: false,
|
|
7
7
|
alwaysCreateTextNode: true,
|
|
8
8
|
ignoreAttributes: false,
|
|
9
|
+
ignorePiTags: true,
|
|
10
|
+
ignoreDeclaration: true,
|
|
9
11
|
attributeNamePrefix: '@',
|
|
10
12
|
transformTagName: (name) => name.toLowerCase(),
|
|
11
13
|
transformAttributeName: (name) => name.toLowerCase(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/common/config.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,YAAY,GAAe;IACtC,UAAU,EAAE,KAAK;IACjB,eAAe,EAAE,KAAK;IACtB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,KAAK;IACpB,mBAAmB,EAAE,KAAK;IAC1B,oBAAoB,EAAE,IAAI;IAC1B,gBAAgB,EAAE,KAAK;IACvB,mBAAmB,EAAE,GAAG;IACxB,gBAAgB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE;IAC9C,sBAAsB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE;CACrD,CAAA;AAED,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,YAAY,EAAE,0BAA0B;IACxC,OAAO,EAAE,qBAAqB;CAC/B,CAAA"}
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/common/config.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,YAAY,GAAe;IACtC,UAAU,EAAE,KAAK;IACjB,eAAe,EAAE,KAAK;IACtB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,KAAK;IACpB,mBAAmB,EAAE,KAAK;IAC1B,oBAAoB,EAAE,IAAI;IAC1B,gBAAgB,EAAE,KAAK;IACvB,YAAY,EAAE,IAAI;IAClB,iBAAiB,EAAE,IAAI;IACvB,mBAAmB,EAAE,GAAG;IACxB,gBAAgB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE;IAC9C,sBAAsB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE;CACrD,CAAA;AAED,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,YAAY,EAAE,0BAA0B;IACxC,OAAO,EAAE,qBAAqB;CAC/B,CAAA"}
|
package/dist/common/utils.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import type { ParseFunction, Unreliable } from './types.js';
|
|
2
|
+
export declare const isPresent: (value: Unreliable) => value is string | number | boolean;
|
|
2
3
|
export declare const isObject: (value: Unreliable) => value is Record<string, Unreliable>;
|
|
3
|
-
export declare const isNonEmptyObject: (value: Unreliable) => value is Record<string, Unreliable>;
|
|
4
|
-
export declare const hasAnyProps: <T extends Record<string, Unreliable>, K extends keyof T>(value: T, props: Array<K>) => boolean;
|
|
5
|
-
export declare const hasAllProps: <T extends Record<string, Unreliable>, K extends keyof T>(value: T, props: Array<K>) => boolean;
|
|
6
4
|
export declare const isNonEmptyStringOrNumber: (value: Unreliable) => value is string | number;
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
5
|
+
export declare const trimObject: <T extends Record<string, unknown>>(object: T) => Partial<T> | undefined;
|
|
6
|
+
export declare const trimArray: <T, R = T>(value: Array<T>, parse?: ParseFunction<R>) => Array<R> | undefined;
|
|
9
7
|
export declare const stripCdata: (text: Unreliable) => any;
|
|
8
|
+
export declare const hasEntities: (text: string) => boolean;
|
|
10
9
|
export declare const parseString: ParseFunction<string>;
|
|
11
10
|
export declare const parseNumber: ParseFunction<number>;
|
|
12
11
|
export declare const parseBoolean: ParseFunction<boolean>;
|
|
13
12
|
export declare const parseSingular: <T>(value: T | Array<T>) => T;
|
|
14
13
|
export declare const parseArray: ParseFunction<Array<Unreliable>>;
|
|
15
14
|
export declare const parseArrayOf: <R>(value: Unreliable, parse: ParseFunction<R>) => Array<R> | undefined;
|
|
15
|
+
export declare const createNamespaceGetter: (value: Record<string, Unreliable>, prefix: string | undefined) => (key: string) => any;
|
|
16
|
+
export declare const createCaseInsensitiveGetter: (value: Record<string, unknown>) => (requestedKey: string) => unknown;
|
package/dist/common/utils.js
CHANGED
|
@@ -1,73 +1,89 @@
|
|
|
1
|
-
import { decodeHTML } from 'entities';
|
|
1
|
+
import { decodeHTML, decodeXML } from 'entities';
|
|
2
|
+
export const isPresent = (value) => {
|
|
3
|
+
return value !== undefined && value !== null;
|
|
4
|
+
};
|
|
2
5
|
export const isObject = (value) => {
|
|
3
|
-
return (value
|
|
6
|
+
return (isPresent(value) &&
|
|
4
7
|
typeof value === 'object' &&
|
|
5
8
|
!Array.isArray(value) &&
|
|
6
9
|
Object.getPrototypeOf(value) === Object.prototype);
|
|
7
10
|
};
|
|
8
|
-
export const isNonEmptyObject = (value) => {
|
|
9
|
-
if (!isObject(value)) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
for (const key in value) {
|
|
13
|
-
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return false;
|
|
18
|
-
};
|
|
19
|
-
export const hasAnyProps = (value, props) => {
|
|
20
|
-
for (const prop of props) {
|
|
21
|
-
if (value[prop] !== undefined) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return false;
|
|
26
|
-
};
|
|
27
|
-
export const hasAllProps = (value, props) => {
|
|
28
|
-
for (const prop of props) {
|
|
29
|
-
if (value[prop] === undefined) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return true;
|
|
34
|
-
};
|
|
35
11
|
export const isNonEmptyStringOrNumber = (value) => {
|
|
36
12
|
return value !== '' && (typeof value === 'string' || typeof value === 'number');
|
|
37
13
|
};
|
|
38
|
-
export const
|
|
14
|
+
export const trimObject = (object) => {
|
|
39
15
|
const result = {};
|
|
40
|
-
|
|
41
|
-
|
|
16
|
+
const keys = Object.keys(object);
|
|
17
|
+
let hasProperties = false;
|
|
18
|
+
for (let i = 0; i < keys.length; i++) {
|
|
19
|
+
const key = keys[i];
|
|
20
|
+
if (isPresent(object[key])) {
|
|
42
21
|
result[key] = object[key];
|
|
22
|
+
hasProperties = true;
|
|
43
23
|
}
|
|
44
24
|
}
|
|
45
|
-
|
|
25
|
+
if (hasProperties) {
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
46
28
|
};
|
|
47
|
-
export const
|
|
48
|
-
|
|
29
|
+
export const trimArray = (value, parse) => {
|
|
30
|
+
if (!Array.isArray(value)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const result = [];
|
|
34
|
+
for (let i = 0; i < value.length; i++) {
|
|
35
|
+
const item = parse ? parse(value[i]) : value[i];
|
|
36
|
+
if (isPresent(item)) {
|
|
37
|
+
result.push(item);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (result.length > 0) {
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
49
43
|
};
|
|
50
44
|
export const stripCdata = (text) => {
|
|
51
|
-
if (typeof text !== 'string'
|
|
45
|
+
if (typeof text !== 'string') {
|
|
46
|
+
return text;
|
|
47
|
+
}
|
|
48
|
+
if (text.indexOf('[CDATA[') === -1) {
|
|
52
49
|
return text;
|
|
53
50
|
}
|
|
54
|
-
|
|
51
|
+
// For simple cases with only one CDATA section (common case).
|
|
52
|
+
const startTag = '<![CDATA[';
|
|
53
|
+
const endTag = ']]>';
|
|
54
|
+
const startPos = text.indexOf(startTag);
|
|
55
|
+
// If we have just one simple CDATA section, handle it without regex.
|
|
56
|
+
if (startPos !== -1) {
|
|
57
|
+
const endPos = text.indexOf(endTag, startPos + startTag.length);
|
|
58
|
+
if (endPos !== -1 && text.indexOf(startTag, startPos + startTag.length) === -1) {
|
|
59
|
+
// Single CDATA section case - avoid regex.
|
|
60
|
+
return (text.substring(0, startPos) +
|
|
61
|
+
text.substring(startPos + startTag.length, endPos) +
|
|
62
|
+
text.substring(endPos + endTag.length));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// Fall back to regex for complex cases with multiple CDATA sections.
|
|
66
|
+
return text.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1');
|
|
67
|
+
};
|
|
68
|
+
export const hasEntities = (text) => {
|
|
69
|
+
return text.indexOf('&') !== -1 && text.indexOf(';') !== -1;
|
|
55
70
|
};
|
|
56
71
|
export const parseString = (value) => {
|
|
72
|
+
if (typeof value === 'string') {
|
|
73
|
+
return hasEntities(value)
|
|
74
|
+
? decodeHTML(decodeXML(stripCdata(value.trim())))
|
|
75
|
+
: stripCdata(value.trim());
|
|
76
|
+
}
|
|
57
77
|
if (typeof value === 'number') {
|
|
58
78
|
return value.toString();
|
|
59
79
|
}
|
|
60
|
-
if (typeof value === 'string') {
|
|
61
|
-
return decodeHTML(stripCdata(value).trim());
|
|
62
|
-
}
|
|
63
80
|
};
|
|
64
81
|
export const parseNumber = (value) => {
|
|
65
82
|
if (typeof value === 'number') {
|
|
66
83
|
return value;
|
|
67
84
|
}
|
|
68
85
|
if (typeof value === 'string' && value !== '') {
|
|
69
|
-
|
|
70
|
-
const numeric = Number(value);
|
|
86
|
+
const numeric = +value;
|
|
71
87
|
return Number.isNaN(numeric) ? undefined : numeric;
|
|
72
88
|
}
|
|
73
89
|
};
|
|
@@ -75,14 +91,12 @@ export const parseBoolean = (value) => {
|
|
|
75
91
|
if (typeof value === 'boolean') {
|
|
76
92
|
return value;
|
|
77
93
|
}
|
|
78
|
-
if (typeof value
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (value.toLowerCase() === 'false') {
|
|
85
|
-
return false;
|
|
94
|
+
if (typeof value === 'string') {
|
|
95
|
+
const lowercased = value.toLowerCase();
|
|
96
|
+
if (lowercased === 'true')
|
|
97
|
+
return true;
|
|
98
|
+
if (lowercased === 'false')
|
|
99
|
+
return false;
|
|
86
100
|
}
|
|
87
101
|
};
|
|
88
102
|
export const parseSingular = (value) => {
|
|
@@ -114,9 +128,26 @@ export const parseArray = (value) => {
|
|
|
114
128
|
export const parseArrayOf = (value, parse) => {
|
|
115
129
|
const array = parseArray(value);
|
|
116
130
|
if (array) {
|
|
117
|
-
return
|
|
131
|
+
return trimArray(array, parse);
|
|
118
132
|
}
|
|
119
133
|
const parsed = parse(value);
|
|
120
|
-
|
|
134
|
+
if (parsed) {
|
|
135
|
+
return [parsed];
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
export const createNamespaceGetter = (value, prefix) => {
|
|
139
|
+
return prefix ? (key) => value[prefix + key] : (key) => value[key];
|
|
140
|
+
};
|
|
141
|
+
export const createCaseInsensitiveGetter = (value) => {
|
|
142
|
+
const keyMap = new Map();
|
|
143
|
+
for (const key in value) {
|
|
144
|
+
if (Object.hasOwn(value, key)) {
|
|
145
|
+
keyMap.set(key.toLowerCase(), key);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return (requestedKey) => {
|
|
149
|
+
const originalKey = keyMap.get(requestedKey.toLowerCase());
|
|
150
|
+
return originalKey ? value[originalKey] : undefined;
|
|
151
|
+
};
|
|
121
152
|
};
|
|
122
153
|
//# sourceMappingURL=utils.js.map
|
package/dist/common/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAGhD,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAiB,EAAsC,EAAE;IACjF,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,CAAA;AAC9C,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAiB,EAAuC,EAAE;IACjF,OAAO,CACL,SAAS,CAAC,KAAK,CAAC;QAChB,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,KAAK,CA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