Skip to content

Commit 7d5773f

Browse files
committed
Add a parse() function to composes denormalize() with JSON.parse().
1 parent f4fe1d6 commit 7d5773f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

linkset/src/index.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { denormalize } from './index';
1+
import { parse, denormalize } from './index';
22

33
const examples = {
44
'Figure 4': `{"linkset":[{"anchor":"https://example.org/article/view/7507","author":[{"href":"https://orcid.org/0000-0002-1825-0097"}],"item":[{"href":"https://example.org/article/7507/item/1","type":"application/pdf"},{"href":"https://example.org/article/7507/item/2","type":"text/csv"}],"cite-as":[{"href":"https://doi.org/10.5555/12345680","title":"AMethodologyfortheEmulationofArchitecture"}]},{"anchor":"https://example.com/links/article/7507","alternate":[{"href":"https://mirror.example.com/links/article/7507","type":"application/linkset"}]}]}`,
@@ -92,4 +92,10 @@ describe('denormalize()', () => {
9292
});
9393
});
9494

95+
describe.each(Object.entries(examples))('parse()', (label, sample) => {
96+
test(`${label} does not fail and can be renormalized`, () => {
97+
expect(JSON.stringify(parse(sample).normalize())).toBe(sample);
98+
})
99+
})
100+
95101
// vim: set nowrap:

linkset/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ import { Linkset, LinksetInterface, NormalizableLinksetInterface } from './core/
55
/**
66
* Denormalizes a set of links into an instance of a Linkset.
77
* {@inheritDoc Linkset.from}
8+
* {@see {@link NormalizedLinksetInterface}}
89
*/
910
function denormalize(normalized: NormalizedLinksetInterface): Linkset {
1011
return Linkset.from(normalized);
1112
}
1213

14+
/**
15+
* Parses an application/linkset+json document into a Linkset instance.
16+
* {@see {@link denormalize}}
17+
*/
18+
function parse(json: string): Linkset {
19+
return denormalize(JSON.parse(json));
20+
}
21+
1322
export {
23+
parse,
1424
denormalize,
1525
LinkInterface,
1626
LinksetInterface,

0 commit comments

Comments
 (0)