Skip to content

Commit f5ef394

Browse files
authored
Merge pull request #17 from Janpot/strict-types
Strictly type
2 parents de20f23 + 7444207 commit f5ef394

12 files changed

Lines changed: 488 additions & 183 deletions

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"description": "Microdata to json and json-ld parser",
55
"main": "src/index.js",
66
"scripts": {
7-
"test": "npm run lint && npm run jest",
7+
"test": "npm run lint && npm run tsc && npm run jest",
88
"jest": "jest",
9+
"tsc": "tsc",
910
"watch:jest": "npm run jest -- -w",
1011
"lint": "semistandard --verbose | snazzy",
1112
"download-tests": "node ./scripts/download-tests.js",
@@ -26,12 +27,14 @@
2627
},
2728
"homepage": "https://github.com/Janpot/microdata-node",
2829
"devDependencies": {
30+
"@types/node": "^13.13.5",
2931
"jest": "^26.0.1",
3032
"jsonld": "^3.1.0",
3133
"n3": "^1.3.6",
3234
"request": "^2.51.0",
3335
"semistandard": "^14.2.0",
34-
"snazzy": "^8.0.0"
36+
"snazzy": "^8.0.0",
37+
"typescript": "^3.8.3"
3538
},
3639
"dependencies": {
3740
"domutils": "^2.1.0",

src/constants.js

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,61 @@
1-
'use strict';
2-
31
const microdataToRdf = require('./microdataToRdf');
42
const rdfToJsonld = require('./rdfToJsonld');
53
const rdfToJson = require('./rdfToJson');
64

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+
*/
722
function removeHashFragment (url) {
823
return url.replace(/#[^#/?]$/, '');
924
}
1025

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
1943
};
20-
21-
config.base = removeHashFragment(config.base || '');
22-
return config;
2344
}
2445

46+
/**
47+
* @param {string} microdataHtml
48+
* @param {Config} config
49+
*/
2550
function toJsonld (microdataHtml, config) {
2651
config = normalizeConfig(config);
2752
return rdfToJsonld(microdataToRdf(microdataHtml, config), config);
2853
}
2954

55+
/**
56+
* @param {string} microdataHtml
57+
* @param {Config} config
58+
*/
3059
function toJson (microdataHtml, config) {
3160
config = normalizeConfig(config);
3261
return rdfToJson(microdataToRdf(microdataHtml, config), config);

0 commit comments

Comments
 (0)