UNPKG

3.4 kBJavaScriptView Raw
1/**
2 * Take `Object` containing flatten hits from ISTEXResult.
3 *
4 * If the environment variable DEBUG is set, some errors could appear on stderr.
5 *
6 * @see ISTEXResult
7 * @see OBJFlatten (from ezs-basics)
8 *
9 * @example
10 *
11 * data: {
12 * 'author/0/name': 'Geoffrey Strickland',
13 * 'author/0/affiliations/0': 'University of Reading',
14 * 'host/issn/0': '0047-2441',
15 * 'host/eissn/0': '1740-2379',
16 * 'title': 'Maupassant, Zola, Jules Vallès and the Paris Commune of 1871',
17 * 'publicationDate': '1983',
18 * 'doi/0': '10.1177/004724418301305203',
19 * 'id': 'F6CB7249E90BD96D5F7E3C4E80CC1C3FEE4FF483',
20 * 'score': 1 }
21 *
22 * @example
23 *
24 * .pipe(ezs('ISTEXTriplify', {
25 * property: [
26 * 'doi/0 -> http://purl.org/ontology/bibo/doi',
27 * 'language -> http://purl.org/dc/terms/language',
28 * 'author/\\d+/name -> http://purl.org/dc/terms/creator',
29 * 'author/\\d+/affiliations -> https://data.istex.fr/ontology/istex#affiliation',
30 * ],
31 * ));
32 *
33 * @example
34 *
35 * <https://data.istex.fr/document/F6CB7249E90BD96D5F7E3C4E80CC1C3FEE4FF483>
36 * a <http://purl.org/ontology/bibo/Document> ;
37 * "10.1002/zaac.19936190205" ;
38 * <https://data.istex.fr/ontology/istex#idIstex> "F6CB7249E90BD96D5F7E3C4E80CC1C3FEE4FF483" ;
39 * <http://purl.org/dc/terms/creator> "Geoffrey Strickland" ;
40 * <https://data.istex.fr/ontology/istex#affiliation> "University of Reading" ;
41 *
42 * @param {Object} [property=[]] path to uri for the properties to output (property and uri separated by ` -> `)
43 *
44 * @param {string} [source=""] the root of the keys (ex: `istex/`)
45 * @returns {string}
46 */
47function ISTEXTriplify(data, feed) {
48 if (this.isLast()) {
49 return feed.close();
50 }
51 const source = this.getParam('source', '');
52 const property = this.getParam('property', []);
53 const properties = Array.isArray(property)
54 ? property.map(prop => prop.split(' -> '))
55 : [property].map(prop => prop.split(' -> '));
56 const regexps = properties
57 .map(([path, prop]) => ([
58 new RegExp(path),
59 prop,
60 ]));
61
62 // Ignore empty objects (hoping that next objects will still arrive)
63 if (!data[`${source}id`]) {
64 if (process.env.DEBUG) {
65 // eslint-disable-next-line no-console
66 console.error(`#${this.getIndex()}: Empty object`);
67 }
68 return feed.end();
69 }
70
71 feed.write(`<https://api.istex.fr/${data[`${source}arkIstex`]}> <https://data.istex.fr/ontology/istex#idIstex> "${data[`${source}id`]}" .\n`);
72 feed.write(`<https://api.istex.fr/${data[`${source}arkIstex`]}> <https://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/ontology/bibo/Document> .\n`);
73
74 const dataArray = Object.entries(data);
75
76 regexps.forEach(([regex, prop]) => {
77 dataArray
78 .filter(([key]) => key.match(regex))
79 .forEach(([, value]) => {
80 if (!value) return;
81 const escapedValue = value.replace(/"/g, '\\"');
82 if (value.startsWith('http')) {
83 feed.write(`<https://api.istex.fr/${data[`${source}arkIstex`]}> <${prop}> <${value}> .\n`);
84 } else {
85 feed.write(`<https://api.istex.fr/${data[`${source}arkIstex`]}> <${prop}> "${escapedValue}" .\n`);
86 }
87 });
88 });
89
90 feed.end();
91}
92
93export default {
94 ISTEXTriplify,
95};