Skip to content

Commit

Permalink
WIP: add full dataset functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Jul 28, 2024
1 parent 0ba9ec2 commit d9e894e
Showing 1 changed file with 164 additions and 2 deletions.
166 changes: 164 additions & 2 deletions src/N3Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ export default class N3Store {
// Setting any field to `undefined` or `null` indicates a wildcard.
forEach(callback, subject, predicate, object, graph) {
this.some(quad => {
callback(quad);
callback(quad, this);
return false;
}, subject, predicate, object, graph);
}
Expand All @@ -488,7 +488,7 @@ export default class N3Store {
let some = false;
const every = !this.some(quad => {
some = true;
return !callback(quad);
return !callback(quad, this);
}, subject, predicate, object, graph);
return some && every;
}
Expand Down Expand Up @@ -775,6 +775,168 @@ export default class N3Store {
return lists;
}

/**
* Returns `true` if the current instance is a superset of the given dataset; differently put: if the given dataset
* is a subset of, is contained in the current dataset.
*
* Blank Nodes will be normalized.
*/
addAll(quads) {
if (Array.isArray(quads))
this.addQuads(quads);
else {
for (const quad of quads)
this.add(quad);
}
return this;
}


/**
* Returns `true` if the current instance is a superset of the given dataset; differently put: if the given dataset
* is a subset of, is contained in the current dataset.
*
* Blank Nodes will be normalized.
*/
contains(other) {
return other.every(quad => this.has(quad));
}

/**
* This method removes the quads in the current instance that match the given arguments.
*
* The logic described in {@link https://rdf.js.org/dataset-spec/#quad-matching|Quad Matching} is applied for each
* quad in this dataset to select the quads which will be deleted.
*
* @param subject The optional exact subject to match.
* @param predicate The optional exact predicate to match.
* @param object The optional exact object to match.
* @param graph The optional exact graph to match.
*/
deleteMatches(subject, predicate, object, graph) {
this.removeMatches(subject, predicate, object, graph);
return this;
}

/**
* Returns a new dataset that contains all quads from the current dataset, not included in the given dataset.
*/
difference(other) {
const store = new N3Store();
for (const quad of this)
if (!other.has(quad))
store.add(quad);
return store;
}

/**
* Returns true if the current instance contains the same graph structure as the given dataset.
*
* Blank Nodes will be normalized.
*/
equals(other) {
return this.size === other.size && this.contains(other);
}

/**
* Creates a new dataset with all the quads that pass the test implemented by the provided `iteratee`.
*
* This method is aligned with Array.prototype.filter() in ECMAScript-262.
*/
filter(iteratee) {
const store = new N3Store();
for (const quad of this)
if (iteratee(quad, this))
store.add(quad);
return store;
}

/**
* Returns a new dataset containing alls quads from the current dataset that are also included in the given dataset.
*/
intersection(other) {
const store = new N3Store();
for (const quad of this)
if (other.has(quad))
store.add(quad);
return store;
}

/**
* Returns a new dataset containing all quads returned by applying `iteratee` to each quad in the current dataset.
*/
map(iteratee) {
const store = new N3Store();
for (const quad of this)
store.add(iteratee(quad, this));
return store;
}

/**
* This method calls the `iteratee` on each `quad` of the `Dataset`. The first time the `iteratee` is called, the
* `accumulator` value is the `initialValue` or, if not given, equals to the first quad of the `Dataset`. The return
* value of the `iteratee` is used as `accumulator` value for the next calls.
*
* This method returns the return value of the last `iteratee` call.
*
* This method is aligned with `Array.prototype.reduce()` in ECMAScript-262.
*/
reduce(callback, initialValue) {
let accumulator = initialValue;
for (const quad of this) {
if (accumulator === undefined)
accumulator = quad;
else
accumulator = callback(accumulator, quad, this);
}
return accumulator;
}

/**
* Returns the set of quads within the dataset as a host language native sequence, for example an `Array` in
* ECMAScript-262.
*
* Since a `Dataset` is an unordered set, the order of the quads within the returned sequence is arbitrary.
*/
toArray() {
return this.getQuads();
}

/**
* Returns an N-Quads string representation of the dataset, preprocessed with
* {@link https://json-ld.github.io/normalization/spec/|RDF Dataset Normalization} algorithm.
*/
toCanonical() {
throw new Error('not implemented');
}

/**
* Returns a stream that contains all quads of the dataset.
*/
toStream() {
return this.match();
}

/**
* Returns an N-Quads string representation of the dataset.
*
* No prior normalization is required, therefore the results for the same quads may vary depending on the `Dataset`
* implementation.
*/
toString() {
throw new Error('not implemented');
}

/**
* Returns a new `Dataset` that is a concatenation of this dataset and the quads given as an argument.
*/
union(quads) {
const store = new N3Store();
store.addAll(this);
store.addAll(quads);
return store;
}

// ### Store is an iterable.
// Can be used where iterables are expected: for...of loops, array spread operator,
// `yield*`, and destructuring assignment (order is not guaranteed).
Expand Down

0 comments on commit d9e894e

Please sign in to comment.