rdfjs-resource 3.0.5 → 3.0.7

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.
@@ -5,7 +5,7 @@ export class MissingValueError extends ValueError {
5
5
  constructor({ focusResource, propertyPath, }) {
6
6
  super({
7
7
  focusResource,
8
- message: `${Identifier.toString(focusResource.identifier)} missing ${PropertyPath.$toString(propertyPath)}`,
8
+ message: `${Identifier.toString(focusResource.identifier)} missing ${PropertyPath.toString(propertyPath)}`,
9
9
  propertyPath,
10
10
  });
11
11
  }
@@ -7,7 +7,7 @@ export class MistypedPrimitiveValueError extends MistypedValueError {
7
7
  actualValue,
8
8
  expectedValueType,
9
9
  focusResource,
10
- message: `expected ${Identifier.toString(focusResource.identifier)} ${PropertyPath.$toString(propertyPath)} to be a ${expectedValueType}, was ${typeof actualValue}`,
10
+ message: `expected ${Identifier.toString(focusResource.identifier)} ${PropertyPath.toString(propertyPath)} to be a ${expectedValueType}, was ${typeof actualValue}`,
11
11
  propertyPath,
12
12
  });
13
13
  }
@@ -7,7 +7,7 @@ export class MistypedTermValueError extends MistypedValueError {
7
7
  actualValue,
8
8
  expectedValueType,
9
9
  focusResource,
10
- message: `expected ${Identifier.toString(focusResource.identifier)} ${PropertyPath.$toString(propertyPath)} to be a ${expectedValueType}, was ${actualValue.termType}`,
10
+ message: `expected ${Identifier.toString(focusResource.identifier)} ${PropertyPath.toString(propertyPath)} to be a ${expectedValueType}, was ${actualValue.termType}`,
11
11
  propertyPath,
12
12
  });
13
13
  }
@@ -33,18 +33,14 @@ interface ZeroOrOnePath {
33
33
  export type PropertyPath = AlternativePath | InversePath | OneOrMorePath | PredicatePath | SequencePath | ZeroOrMorePath | ZeroOrOnePath;
34
34
  export declare namespace PropertyPath {
35
35
  function equals(left: PropertyPath, right: PropertyPath): boolean;
36
- function $fromRdf(resource: Resource, options?: {
37
- [index: string]: unknown;
36
+ function fromResource(resource: Resource, options?: {
38
37
  graph?: Exclude<Quad_Graph, Variable>;
39
38
  }): Either<Error, PropertyPath>;
40
- type $Filter = object;
41
- function $filter(_filter: $Filter, _value: PropertyPath): boolean;
42
- const $schema: Readonly<object>;
43
- function $toString(propertyPath: PropertyPath): string;
44
- function $toRdf(propertyPath: PropertyPath, options?: {
39
+ function toResource(propertyPath: PropertyPath, options?: {
45
40
  graph?: Exclude<Quad_Graph, Variable>;
46
41
  resourceSet?: ResourceSet;
47
42
  }): Resource;
43
+ function toString(propertyPath: PropertyPath): string;
48
44
  }
49
45
  export {};
50
46
  //# sourceMappingURL=PropertyPath.d.ts.map
@@ -34,7 +34,7 @@ export var PropertyPath;
34
34
  }
35
35
  }
36
36
  PropertyPath.equals = equals;
37
- function $fromRdf(resource, options) {
37
+ function fromResource(resource, options) {
38
38
  // Predicate path
39
39
  // sh:path ex:parent
40
40
  if (resource.identifier.termType === "NamedNode") {
@@ -49,7 +49,7 @@ export var PropertyPath;
49
49
  if (resource.isNothing()) {
50
50
  return Left(new Error("non-identifier in property path list"));
51
51
  }
52
- const member = PropertyPath.$fromRdf(resource.unsafeCoerce());
52
+ const member = PropertyPath.fromResource(resource.unsafeCoerce(), options);
53
53
  if (member.isLeft()) {
54
54
  return member;
55
55
  }
@@ -89,27 +89,27 @@ export var PropertyPath;
89
89
  // Inverse path
90
90
  // sh:path: [ sh:inversePath ex:parent ]
91
91
  if (quad.predicate.equals(sh.inversePath)) {
92
- return PropertyPath.$fromRdf(objectResource).map((path) => ({
92
+ return PropertyPath.fromResource(objectResource, options).map((path) => ({
93
93
  termType: "InversePath",
94
94
  path,
95
95
  }));
96
96
  }
97
97
  // One or more path
98
98
  if (quad.predicate.equals(sh.oneOrMorePath)) {
99
- return PropertyPath.$fromRdf(objectResource).map((path) => ({
99
+ return PropertyPath.fromResource(objectResource, options).map((path) => ({
100
100
  termType: "OneOrMorePath",
101
101
  path,
102
102
  }));
103
103
  }
104
104
  // Zero or more path
105
105
  if (quad.predicate.equals(sh.zeroOrMorePath)) {
106
- return PropertyPath.$fromRdf(objectResource).map((path) => ({
106
+ return PropertyPath.fromResource(objectResource).map((path) => ({
107
107
  termType: "ZeroOrMorePath",
108
108
  path,
109
109
  }));
110
110
  }
111
111
  if (quad.predicate.equals(sh.zeroOrOnePath)) {
112
- return PropertyPath.$fromRdf(objectResource).map((path) => ({
112
+ return PropertyPath.fromResource(objectResource).map((path) => ({
113
113
  termType: "ZeroOrOnePath",
114
114
  path,
115
115
  }));
@@ -117,28 +117,8 @@ export var PropertyPath;
117
117
  }
118
118
  return Left(new Error(`unrecognized or ill-formed SHACL property path ${Resource.Identifier.toString(resource.identifier)}`));
119
119
  }
120
- PropertyPath.$fromRdf = $fromRdf;
121
- function $filter(_filter, _value) {
122
- return true;
123
- }
124
- PropertyPath.$filter = $filter;
125
- PropertyPath.$schema = {};
126
- function $toString(propertyPath) {
127
- switch (propertyPath.termType) {
128
- case "AlternativePath":
129
- case "SequencePath":
130
- return `${propertyPath.termType}([${propertyPath.members.map(PropertyPath.toString).join(", ")}])`;
131
- case "InversePath":
132
- case "OneOrMorePath":
133
- case "ZeroOrMorePath":
134
- case "ZeroOrOnePath":
135
- return `${propertyPath.termType}(${PropertyPath.$toString(propertyPath.path)})`;
136
- case "NamedNode":
137
- return `PredicatePath(${propertyPath.value})`;
138
- }
139
- }
140
- PropertyPath.$toString = $toString;
141
- function $toRdf(propertyPath, options) {
120
+ PropertyPath.fromResource = fromResource;
121
+ function toResource(propertyPath, options) {
142
122
  const graph = options?.graph;
143
123
  const resourceSet = options?.resourceSet ??
144
124
  new ResourceSet(datasetFactory.dataset(), { dataFactory: dataFactory });
@@ -149,7 +129,7 @@ export var PropertyPath;
149
129
  switch (propertyPath.termType) {
150
130
  case "AlternativePath":
151
131
  case "SequencePath": {
152
- const members = propertyPath.members.map((member) => PropertyPath.$toRdf(member, { graph, resourceSet }).identifier);
132
+ const members = propertyPath.members.map((member) => PropertyPath.toResource(member, { graph, resourceSet }).identifier);
153
133
  if (propertyPath.termType === "AlternativePath") {
154
134
  resource.addList(sh.alternativePath, members, { graph });
155
135
  }
@@ -174,9 +154,26 @@ export var PropertyPath;
174
154
  predicate = sh.zeroOrOnePath;
175
155
  break;
176
156
  }
177
- resource.add(predicate, PropertyPath.$toRdf(propertyPath.path, { graph, resourceSet }).identifier, graph);
157
+ resource.add(predicate, PropertyPath.toResource(propertyPath.path, { graph, resourceSet })
158
+ .identifier, graph);
178
159
  return resource;
179
160
  }
180
- PropertyPath.$toRdf = $toRdf;
161
+ PropertyPath.toResource = toResource;
162
+ // biome-ignore lint/suspicious/noShadowRestrictedNames: intentional
163
+ function toString(propertyPath) {
164
+ switch (propertyPath.termType) {
165
+ case "AlternativePath":
166
+ case "SequencePath":
167
+ return `${propertyPath.termType}([${propertyPath.members.map(PropertyPath.toString).join(", ")}])`;
168
+ case "InversePath":
169
+ case "OneOrMorePath":
170
+ case "ZeroOrMorePath":
171
+ case "ZeroOrOnePath":
172
+ return `${propertyPath.termType}(${PropertyPath.toString(propertyPath.path)})`;
173
+ case "NamedNode":
174
+ return `PredicatePath(${propertyPath.value})`;
175
+ }
176
+ }
177
+ PropertyPath.toString = toString;
181
178
  })(PropertyPath || (PropertyPath = {}));
182
179
  //# sourceMappingURL=PropertyPath.js.map
@@ -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.5"
50
+ "version": "3.0.7"
51
51
  }