rdfjs-resource 3.0.4 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,6 +32,7 @@ interface ZeroOrOnePath {
32
32
  */
33
33
  export type PropertyPath = AlternativePath | InversePath | OneOrMorePath | PredicatePath | SequencePath | ZeroOrMorePath | ZeroOrOnePath;
34
34
  export declare namespace PropertyPath {
35
+ function equals(left: PropertyPath, right: PropertyPath): boolean;
35
36
  function $fromRdf(resource: Resource, options?: {
36
37
  [index: string]: unknown;
37
38
  graph?: Exclude<Quad_Graph, Variable>;
@@ -6,6 +6,34 @@ import { ResourceSet } from "./ResourceSet.js";
6
6
  import { sh } from "./vocabularies.js";
7
7
  export var PropertyPath;
8
8
  (function (PropertyPath) {
9
+ function equals(left, right) {
10
+ if (left.termType !== right.termType) {
11
+ return false;
12
+ }
13
+ switch (left.termType) {
14
+ case "AlternativePath":
15
+ case "SequencePath": {
16
+ const right_ = right;
17
+ if (left.members.length !== right_.members.length) {
18
+ return false;
19
+ }
20
+ for (let memberI = 0; memberI < left.members.length; memberI++) {
21
+ if (!PropertyPath.equals(left.members[memberI], right_.members[memberI])) {
22
+ return false;
23
+ }
24
+ }
25
+ return true;
26
+ }
27
+ case "NamedNode":
28
+ return left.equals(right);
29
+ case "InversePath":
30
+ case "OneOrMorePath":
31
+ case "ZeroOrMorePath":
32
+ case "ZeroOrOnePath":
33
+ return PropertyPath.equals(left.path, right.path);
34
+ }
35
+ }
36
+ PropertyPath.equals = equals;
9
37
  function $fromRdf(resource, options) {
10
38
  // Predicate path
11
39
  // sh:path ex:parent
@@ -1,4 +1,4 @@
1
- import type { DataFactory, DatasetCore, NamedNode, Quad_Graph, Variable } from "@rdfjs/types";
1
+ import type { BlankNode, DataFactory, DatasetCore, Literal, NamedNode, Quad_Graph, Variable } from "@rdfjs/types";
2
2
  import { Either } from "purify-ts";
3
3
  import { Identifier as _Identifier, type Identifier } from "./Identifier.js";
4
4
  import { ListStructureError as _ListStructureError } from "./ListStructureError.js";
@@ -24,21 +24,22 @@ export declare class Resource<IdentifierT extends Resource.Identifier = Resource
24
24
  /**
25
25
  * Add zero or more values to this resource.
26
26
  */
27
- add(predicate: NamedNode, object: AddableValue | readonly AddableValue[], graph?: Exclude<Quad_Graph, Variable>): this;
27
+ add(propertyPath: InversePath, value: AddableSubject | readonly AddableSubject[], graph?: Graph): this;
28
+ add(propertyPath: NamedNode, value: AddableObject | readonly AddableObject[], graph?: Graph): this;
28
29
  /**
29
30
  * Create a new list, add items to it, and attach it to this resource via predicate in a
30
31
  * (this, predicate, newList) statement.
31
32
  *
32
33
  * Returns the list resource.
33
34
  */
34
- addList(predicate: NamedNode, items: Iterable<AddableValue>, options?: Parameters<Resource["addListItems"]>[1]): Resource;
35
+ addList(predicate: NamedNode, items: Iterable<AddableObject>, options?: Parameters<Resource["addListItems"]>[1]): Resource;
35
36
  /**
36
37
  * Add rdf:first and rdf:rest predicates to the current Resource.
37
38
  */
38
- addListItems(items: Iterable<AddableValue>, options?: {
39
+ addListItems(items: Iterable<AddableObject>, options?: {
39
40
  addSubListResourceValues?: (subListResource: Resource) => void;
40
- graph?: Exclude<Quad_Graph, Variable>;
41
- mintSubListIdentifier?: (item: AddableValue, itemIndex: number) => Identifier;
41
+ graph?: Graph;
42
+ mintSubListIdentifier?: (item: AddableObject, itemIndex: number) => Identifier;
42
43
  }): this;
43
44
  /**
44
45
  * Delete zero or more values from this resource.
@@ -46,44 +47,53 @@ export declare class Resource<IdentifierT extends Resource.Identifier = Resource
46
47
  * If value is empty, delete all values of p
47
48
  * Else delete (p, arrayValue) for each value in the array.
48
49
  */
49
- delete(predicate: NamedNode, object?: AddableValue | readonly AddableValue[], graph?: Exclude<Quad_Graph, Variable>): this;
50
+ delete(propertyPath: InversePath, value?: AddableSubject | readonly AddableSubject[], graph?: Graph): this;
51
+ delete(propertyPath: NamedNode, value?: AddableObject | readonly AddableObject[], graph?: Graph): this;
50
52
  isInstanceOf(class_: NamedNode, options?: {
51
53
  excludeSubclasses?: boolean;
52
- graph?: Exclude<Quad_Graph, Variable>;
54
+ graph?: Graph;
53
55
  instanceOfPredicate?: NamedNode;
54
56
  subClassOfPredicate?: NamedNode;
55
57
  }): boolean;
56
58
  isSubClassOf(class_: NamedNode, options?: {
57
- graph?: Exclude<Quad_Graph, Variable>;
59
+ graph?: Graph;
58
60
  subClassOfPredicate?: NamedNode;
59
61
  }): boolean;
60
62
  /**
61
63
  * Delete all existing values of p and then add the specified values.
62
64
  */
63
- set(predicate: NamedNode, object: AddableValue | readonly AddableValue[], graph?: Exclude<Quad_Graph, Variable>): this;
65
+ set(propertyPath: InversePath, value: AddableSubject | readonly AddableSubject[], graph?: Graph): this;
66
+ set(propertyPath: NamedNode, value: AddableObject | readonly AddableObject[], graph?: Graph): this;
64
67
  /**
65
68
  * Consider the resource itself as an RDF list.
66
69
  */
67
70
  toList(options?: {
68
- graph?: Exclude<Quad_Graph, Variable>;
71
+ graph?: Graph;
69
72
  }): Either<Resource.ValueError, Resource.Values>;
70
73
  /**
71
74
  * Get the first matching value for the property path.
72
75
  */
73
76
  value(propertyPath: PropertyPath, options?: {
74
- graph?: Exclude<Quad_Graph, Variable>;
77
+ graph?: Graph;
75
78
  }): Either<Resource.ValueError, Resource.Value>;
76
79
  /**
77
80
  * Get all values for the property path.
78
81
  */
79
82
  values(propertyPath: PropertyPath, options?: {
80
- graph?: Exclude<Quad_Graph, Variable>;
83
+ graph?: Graph;
81
84
  unique?: boolean;
82
85
  }): Resource.Values;
83
- private addableValueToTerm;
84
- private addableValuesToTerms;
86
+ private addableObjectToTerm;
87
+ private addableObjectsToTerms;
88
+ private addableSubjectsToTerms;
85
89
  }
86
- type AddableValue = Term | Exclude<Primitive, Date>;
90
+ type AddableSubject = BlankNode | NamedNode;
91
+ type AddableObject = BlankNode | Literal | NamedNode | Exclude<Primitive, Date>;
92
+ type Graph = Exclude<Quad_Graph, Variable>;
93
+ type InversePath = {
94
+ readonly path: NamedNode;
95
+ readonly termType: "InversePath";
96
+ };
87
97
  export declare namespace Resource {
88
98
  const Identifier: typeof _Identifier;
89
99
  type Identifier = _Identifier;
package/dist/Resource.js CHANGED
@@ -20,12 +20,21 @@ export class Resource {
20
20
  this.dataFactory = options?.dataFactory ?? DefaultDataFactory;
21
21
  this.literalFactory = new LiteralFactory({ dataFactory: this.dataFactory });
22
22
  }
23
- /**
24
- * Add zero or more values to this resource.
25
- */
26
- add(predicate, object, graph) {
27
- for (const term of this.addableValuesToTerms(object)) {
28
- this.dataset.add(this.dataFactory.quad(this.identifier, predicate, term, graph ?? this.graph));
23
+ add(propertyPath, value, graph) {
24
+ if (!graph) {
25
+ graph = this.graph;
26
+ }
27
+ switch (propertyPath.termType) {
28
+ case "InversePath":
29
+ for (const subject of this.addableSubjectsToTerms(value)) {
30
+ this.dataset.add(this.dataFactory.quad(subject, propertyPath.path, this.identifier, graph));
31
+ }
32
+ break;
33
+ case "NamedNode":
34
+ for (const object of this.addableObjectsToTerms(value)) {
35
+ this.dataset.add(this.dataFactory.quad(this.identifier, propertyPath, object, graph));
36
+ }
37
+ break;
29
38
  }
30
39
  return this;
31
40
  }
@@ -78,27 +87,48 @@ export class Resource {
78
87
  }
79
88
  return this;
80
89
  }
81
- /**
82
- * Delete zero or more values from this resource.
83
- *
84
- * If value is empty, delete all values of p
85
- * Else delete (p, arrayValue) for each value in the array.
86
- */
87
- delete(predicate, object, graph) {
88
- if (!object) {
89
- for (const quad of [
90
- ...this.dataset.match(this.identifier, predicate, null, graph ?? this.graph),
91
- ]) {
92
- this.dataset.delete(quad);
93
- }
90
+ delete(propertyPath, value, graph) {
91
+ if (!graph) {
92
+ graph = this.graph;
94
93
  }
95
- else {
96
- for (const term of this.addableValuesToTerms(object)) {
97
- for (const quad of [
98
- ...this.dataset.match(this.identifier, predicate, term, graph ?? this.graph),
99
- ]) {
100
- this.dataset.delete(quad);
94
+ switch (propertyPath.termType) {
95
+ case "InversePath": {
96
+ if (value) {
97
+ for (const subject of this.addableSubjectsToTerms(value)) {
98
+ for (const quad of [
99
+ ...this.dataset.match(subject, propertyPath.path, this.identifier, graph),
100
+ ]) {
101
+ this.dataset.delete(quad);
102
+ }
103
+ }
101
104
  }
105
+ else {
106
+ for (const quad of [
107
+ ...this.dataset.match(null, propertyPath.path, this.identifier, graph),
108
+ ]) {
109
+ this.dataset.delete(quad);
110
+ }
111
+ }
112
+ break;
113
+ }
114
+ case "NamedNode": {
115
+ if (value) {
116
+ for (const object of this.addableObjectsToTerms(value)) {
117
+ for (const quad of [
118
+ ...this.dataset.match(this.identifier, propertyPath, object, graph),
119
+ ]) {
120
+ this.dataset.delete(quad);
121
+ }
122
+ }
123
+ }
124
+ else {
125
+ for (const quad of [
126
+ ...this.dataset.match(this.identifier, propertyPath, null, graph),
127
+ ]) {
128
+ this.dataset.delete(quad);
129
+ }
130
+ }
131
+ break;
102
132
  }
103
133
  }
104
134
  return this;
@@ -174,12 +204,15 @@ export class Resource {
174
204
  return false;
175
205
  }
176
206
  }
177
- /**
178
- * Delete all existing values of p and then add the specified values.
179
- */
180
- set(predicate, object, graph) {
181
- this.delete(predicate, undefined, graph);
182
- return this.add(predicate, object, graph);
207
+ set(propertyPath, value, graph) {
208
+ switch (propertyPath.termType) {
209
+ case "InversePath":
210
+ this.delete(propertyPath, undefined, graph);
211
+ return this.add(propertyPath, value, graph);
212
+ case "NamedNode":
213
+ this.delete(propertyPath, undefined, graph);
214
+ return this.add(propertyPath, value, graph);
215
+ }
183
216
  }
184
217
  /**
185
218
  * Consider the resource itself as an RDF list.
@@ -283,25 +316,31 @@ export class Resource {
283
316
  unique: !!options?.unique,
284
317
  });
285
318
  }
286
- addableValueToTerm(value) {
287
- switch (typeof value) {
319
+ addableObjectToTerm(object) {
320
+ switch (typeof object) {
288
321
  case "bigint":
289
- return this.literalFactory.bigint(value);
322
+ return this.literalFactory.bigint(object);
290
323
  case "boolean":
291
- return this.literalFactory.boolean(value);
324
+ return this.literalFactory.boolean(object);
292
325
  case "number":
293
- return this.literalFactory.number(value);
326
+ return this.literalFactory.number(object);
294
327
  case "string":
295
- return this.literalFactory.string(value);
328
+ return this.literalFactory.string(object);
296
329
  case "object":
297
- return value;
330
+ return object;
331
+ }
332
+ }
333
+ addableObjectsToTerms(objects) {
334
+ if (Array.isArray(objects)) {
335
+ return objects.map((value) => this.addableObjectToTerm(value));
298
336
  }
337
+ return [this.addableObjectToTerm(objects)];
299
338
  }
300
- addableValuesToTerms(values) {
301
- if (Array.isArray(values)) {
302
- return values.map((value) => this.addableValueToTerm(value));
339
+ addableSubjectsToTerms(subjects) {
340
+ if (Array.isArray(subjects)) {
341
+ return subjects;
303
342
  }
304
- return [this.addableValueToTerm(values)];
343
+ return [subjects];
305
344
  }
306
345
  }
307
346
  (function (Resource) {
package/package.json CHANGED
@@ -47,5 +47,5 @@
47
47
  },
48
48
  "type": "module",
49
49
  "types": "dist/index.d.ts",
50
- "version": "3.0.4"
50
+ "version": "3.0.6"
51
51
  }