|
1 | | -'use strict'; |
2 | | - |
3 | 1 | const microdataToRdf = require('./microdataToRdf'); |
4 | 2 | const rdfToJsonld = require('./rdfToJsonld'); |
5 | 3 | const rdfToJson = require('./rdfToJson'); |
6 | 4 |
|
| 5 | +/** |
| 6 | + * @typedef {{ subPropertyOf?: string, equivalentProperty?: string }} VocabularyProperty |
| 7 | + * @typedef {{ properties?: { [name: string]: VocabularyProperty } }} Vocabulary |
| 8 | + * @typedef {{ [id: string]: Vocabulary }} Registry |
| 9 | + * @typedef {{ |
| 10 | + * base: string |
| 11 | + * registry: Registry |
| 12 | + * strict: boolean |
| 13 | + * useRdfType: boolean |
| 14 | + * useNativeTypes: boolean |
| 15 | + * }} Config |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {string} url |
| 20 | + * @returns {string} |
| 21 | + */ |
7 | 22 | function removeHashFragment (url) { |
8 | 23 | return url.replace(/#[^#/?]$/, ''); |
9 | 24 | } |
10 | 25 |
|
11 | | -function normalizeConfig (config) { |
12 | | - config = { |
13 | | - base: '', |
14 | | - registry: {}, |
15 | | - strict: false, |
16 | | - useRdfType: false, |
17 | | - useNativeTypes: true, |
18 | | - ...config |
| 26 | +/** |
| 27 | + * @param {Config} config |
| 28 | + * @returns {Config} |
| 29 | + */ |
| 30 | +function normalizeConfig ({ |
| 31 | + base = '', |
| 32 | + registry = /** @type {Registry} */({}), |
| 33 | + strict = false, |
| 34 | + useRdfType = false, |
| 35 | + useNativeTypes = true |
| 36 | +} = /** @type {Partial<Config>} */({})) { |
| 37 | + return { |
| 38 | + base: removeHashFragment(base || ''), |
| 39 | + registry, |
| 40 | + strict, |
| 41 | + useRdfType, |
| 42 | + useNativeTypes |
19 | 43 | }; |
20 | | - |
21 | | - config.base = removeHashFragment(config.base || ''); |
22 | | - return config; |
23 | 44 | } |
24 | 45 |
|
| 46 | +/** |
| 47 | + * @param {string} microdataHtml |
| 48 | + * @param {Config} config |
| 49 | + */ |
25 | 50 | function toJsonld (microdataHtml, config) { |
26 | 51 | config = normalizeConfig(config); |
27 | 52 | return rdfToJsonld(microdataToRdf(microdataHtml, config), config); |
28 | 53 | } |
29 | 54 |
|
| 55 | +/** |
| 56 | + * @param {string} microdataHtml |
| 57 | + * @param {Config} config |
| 58 | + */ |
30 | 59 | function toJson (microdataHtml, config) { |
31 | 60 | config = normalizeConfig(config); |
32 | 61 | return rdfToJson(microdataToRdf(microdataHtml, config), config); |
|
0 commit comments