UNPKG

2.12 kBJavaScriptView Raw
1import { contains } from 'ramda';
2import { getTriple } from './utils';
3
4const triples = {};
5const previous = {};
6
7function ISTEXRemoveVerb(data, feed) {
8 const verbToRemove = this.getParam('verb', '');
9 function writeFilteredTriples() {
10 triples[verbToRemove] = triples[verbToRemove]
11 .filter(triple => !contains(triple.verb, verbToRemove));
12 triples[verbToRemove].forEach(
13 t => feed.write(`${t.subject} ${t.verb} ${t.complement} .\n`),
14 );
15 }
16
17 if (this.isLast()) {
18 writeFilteredTriples();
19 return feed.close();
20 }
21
22 const [subject, verb, complement] = getTriple(data);
23
24 if (this.isFirst()) {
25 triples[verbToRemove] = [];
26 previous[verbToRemove] = subject;
27 }
28
29 if (previous[verbToRemove] && subject !== previous[verbToRemove]) {
30 writeFilteredTriples();
31 triples[verbToRemove] = [];
32 previous[verbToRemove] = subject;
33 }
34
35 triples[verbToRemove].push({ subject, verb, complement });
36 feed.end();
37}
38
39/**
40 * Unconditionnaly remove triples which `verb` is given.
41 *
42 * @name ISTEXRemoveVerb
43 * @param {string} verb `"<https://data.istex.fr/ontology/istex#idIstex>"`
44 *
45 * @example
46 * <https://api.istex.fr/ark:/67375/QT4-D0J6VN6K-K> <https://data.istex.fr/ontology/istex#idIstex> "2FF3F5B1477986B9C617BB75CA3333DBEE99EB05" .
47 * <https://api.istex.fr/ark:/67375/QT4-D0J6VN6K-K> a <http://purl.org/ontology/bibo/Document> .
48 * <https://api.istex.fr/ark:/67375/QT4-D0J6VN6K-K> <host/genre> "journal" .
49 * <https://api.istex.fr/ark:/67375/QT4-D0J6VN6K-K> <https://data.istex.fr/fake#journalTitle> "Linguistic Typology" .
50 *
51 * @example
52 * [ISTEXRemoveIf]
53 * verb = <host/genre>
54 *
55 * @example
56 * <https://api.istex.fr/ark:/67375/QT4-D0J6VN6K-K> <https://data.istex.fr/ontology/istex#idIstex> "2FF3F5B1477986B9C617BB75CA3333DBEE99EB05" .
57 * <https://api.istex.fr/ark:/67375/QT4-D0J6VN6K-K> a <http://purl.org/ontology/bibo/Document> .
58 * <https://api.istex.fr/ark:/67375/QT4-D0J6VN6K-K> <https://data.istex.fr/fake#journalTitle> "Linguistic Typology" .
59 */
60export default {
61 ISTEXRemoveVerb,
62};