UNPKG

2.37 kBPlain TextView Raw
1/**
2 * Copyright 2020 Inrupt Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to use,
7 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 * Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22import { DatasetCore, Quad } from "rdf-js";
23import { quad, literal, namedNode, blankNode, dataset } from "@rdfjs/dataset";
24
25export { dataset } from "@rdfjs/dataset";
26
27/**
28 * @internal
29 */
30export const DataFactory = { quad, literal, namedNode, blankNode };
31
32/**
33 * Clone a Dataset.
34 *
35 * Note that the Quads are not cloned, i.e. if you modify the Quads in the output Dataset, the Quads
36 * in the input Dataset will also be changed.
37 *
38 * @internal
39 * @param input Dataset to clone.
40 * @returns A new Dataset with the same Quads as `input`.
41 */
42export function clone(input: DatasetCore): DatasetCore {
43 const output = dataset();
44
45 for (const quad of input) {
46 output.add(quad);
47 }
48
49 return output;
50}
51
52/**
53 * @internal
54 * @param input Dataset to clone.
55 * @param callback Function that takes a Quad, and returns a boolean indicating whether that Quad should be included in the cloned Dataset.
56 * @returns A new Dataset with the same Quads as `input`, excluding the ones for which `callback` returned `false`.
57 */
58export function filter(
59 input: DatasetCore,
60 callback: (quad: Quad) => boolean
61): DatasetCore {
62 const output = dataset();
63
64 for (const quad of input) {
65 if (callback(quad)) {
66 output.add(quad);
67 }
68 }
69
70 return output;
71}