@rdfx/resource 0.0.11 → 0.0.13

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.
@@ -77,7 +77,11 @@ export var PropertyPath;
77
77
  default:
78
78
  return Left(new Error(`non-BlankNode/NamedNode property path object on path ${resource.identifier}: ${quad.object.termType} ${quad.object.value}`));
79
79
  }
80
- const objectResource = new Resource(resource.dataset, quad.object);
80
+ const objectResource = new Resource({
81
+ dataFactory: resource.dataFactory,
82
+ dataset: resource.dataset,
83
+ identifier: quad.object,
84
+ });
81
85
  // Alternative path
82
86
  // sh:path: [ sh:alternativePath ( ex:father ex:mother ) ]
83
87
  if (quad.predicate.equals(sh.alternativePath)) {
@@ -121,7 +125,7 @@ export var PropertyPath;
121
125
  function toResource(propertyPath, options) {
122
126
  const graph = options?.graph;
123
127
  const resourceSet = options?.resourceSet ??
124
- new ResourceSet(datasetFactory.dataset(), { dataFactory: dataFactory });
128
+ new ResourceSet({ dataFactory, dataset: datasetFactory.dataset() });
125
129
  if (propertyPath.termType === "NamedNode") {
126
130
  return resourceSet.resource(propertyPath);
127
131
  }
@@ -3,7 +3,6 @@ import { Either } from "purify-ts";
3
3
  import type { Identifier as _Identifier, Identifier } from "./Identifier.js";
4
4
  import { ListStructureError as _ListStructureError } from "./ListStructureError.js";
5
5
  import { MistypedTermValueError as _MistypedTermValueError } from "./MistypedTermValueError.js";
6
- import type { Primitive } from "./Primitive.js";
7
6
  import type { PropertyPath } from "./PropertyPath.js";
8
7
  import type { Term } from "./Term.js";
9
8
  import { Value as _Value } from "./Value.js";
@@ -13,13 +12,13 @@ import { Values as _Values } from "./Values.js";
13
12
  * A Resource abstraction over subjects or objects in an RDF/JS dataset.
14
13
  */
15
14
  export declare class Resource<IdentifierT extends Resource.Identifier = Resource.Identifier> {
15
+ readonly dataFactory: DataFactory;
16
16
  readonly dataset: DatasetCore;
17
17
  readonly identifier: IdentifierT;
18
- private readonly dataFactory;
19
- private readonly graph?;
20
- private readonly literalFactory;
21
- constructor(dataset: DatasetCore, identifier: IdentifierT, options?: {
22
- dataFactory?: DataFactory;
18
+ constructor({ dataFactory, dataset, identifier, }: {
19
+ dataFactory: DataFactory;
20
+ dataset: DatasetCore;
21
+ identifier: IdentifierT;
23
22
  });
24
23
  /**
25
24
  * Add zero or more values to this resource.
@@ -83,12 +82,9 @@ export declare class Resource<IdentifierT extends Resource.Identifier = Resource
83
82
  graph?: Graph;
84
83
  unique?: boolean;
85
84
  }): Resource.Values;
86
- private addableObjectToTerm;
87
- private addableObjectsToTerms;
88
- private addableSubjectsToTerms;
89
85
  }
86
+ type AddableObject = BlankNode | Literal | NamedNode;
90
87
  type AddableSubject = BlankNode | NamedNode;
91
- type AddableObject = BlankNode | Literal | NamedNode | Exclude<Primitive, Date>;
92
88
  type Graph = Exclude<Quad_Graph, Variable>;
93
89
  type InversePath = {
94
90
  readonly path: NamedNode;
package/dist/Resource.js CHANGED
@@ -1,6 +1,4 @@
1
1
  import TermSet from "@rdfjs/term-set";
2
- import defaultDataFactory from "@rdfx/data-factory";
3
- import { LiteralFactory } from "@rdfx/literal";
4
2
  import { Either, Left } from "purify-ts";
5
3
  import { DatasetValues } from "./DatasetValues.js";
6
4
  import { ListStructureError as _ListStructureError } from "./ListStructureError.js";
@@ -13,24 +11,20 @@ import { rdf, rdfs } from "./vocabularies.js";
13
11
  * A Resource abstraction over subjects or objects in an RDF/JS dataset.
14
12
  */
15
13
  export class Resource {
16
- constructor(dataset, identifier, options) {
14
+ constructor({ dataFactory, dataset, identifier, }) {
15
+ this.dataFactory = dataFactory;
17
16
  this.dataset = dataset;
18
17
  this.identifier = identifier;
19
- this.dataFactory = options?.dataFactory ?? defaultDataFactory;
20
- this.literalFactory = new LiteralFactory({ dataFactory: this.dataFactory });
21
18
  }
22
19
  add(propertyPath, value, graph) {
23
- if (!graph) {
24
- graph = this.graph;
25
- }
26
20
  switch (propertyPath.termType) {
27
21
  case "InversePath":
28
- for (const subject of this.addableSubjectsToTerms(value)) {
22
+ for (const subject of Array.isArray(value) ? value : [value]) {
29
23
  this.dataset.add(this.dataFactory.quad(subject, propertyPath.path, this.identifier, graph));
30
24
  }
31
25
  break;
32
26
  case "NamedNode":
33
- for (const object of this.addableObjectsToTerms(value)) {
27
+ for (const object of Array.isArray(value) ? value : [value]) {
34
28
  this.dataset.add(this.dataFactory.quad(this.identifier, propertyPath, object, graph));
35
29
  }
36
30
  break;
@@ -46,12 +40,18 @@ export class Resource {
46
40
  addList(predicate, items, options) {
47
41
  const itemsArray = [...items];
48
42
  if (itemsArray.length === 0) {
49
- return new Resource(this.dataset, rdf.nil, {
43
+ return new Resource({
50
44
  dataFactory: this.dataFactory,
45
+ dataset: this.dataset,
46
+ identifier: rdf.nil,
51
47
  });
52
48
  }
53
49
  const mintSubListIdentifier = options?.mintSubListIdentifier ?? (() => this.dataFactory.blankNode());
54
- const listResource = new Resource(this.dataset, mintSubListIdentifier(itemsArray[0], 0), { dataFactory: this.dataFactory });
50
+ const listResource = new Resource({
51
+ dataFactory: this.dataFactory,
52
+ dataset: this.dataset,
53
+ identifier: mintSubListIdentifier(itemsArray[0], 0),
54
+ });
55
55
  listResource.addListItems(itemsArray, {
56
56
  graph: options?.graph,
57
57
  mintSubListIdentifier,
@@ -64,14 +64,18 @@ export class Resource {
64
64
  */
65
65
  addListItems(items, options) {
66
66
  const addSubListResourceValues = options?.addSubListResourceValues ?? (() => { });
67
- const graph = options?.graph ?? this.graph;
67
+ const graph = options?.graph;
68
68
  const mintSubListIdentifier = options?.mintSubListIdentifier ?? (() => this.dataFactory.blankNode());
69
69
  let currentHead = this;
70
70
  let itemIndex = 0;
71
71
  for (const item of items) {
72
72
  if (itemIndex > 0) {
73
73
  // If currentHead !== this, then create a new head and point the current head's rdf:rest at it
74
- const newHead = new Resource(this.dataset, mintSubListIdentifier(item, itemIndex), { dataFactory: this.dataFactory });
74
+ const newHead = new Resource({
75
+ dataset: this.dataset,
76
+ dataFactory: this.dataFactory,
77
+ identifier: mintSubListIdentifier(item, itemIndex),
78
+ });
75
79
  addSubListResourceValues(newHead);
76
80
  currentHead.add(rdf.rest, newHead.identifier, graph);
77
81
  currentHead = newHead;
@@ -87,13 +91,10 @@ export class Resource {
87
91
  return this;
88
92
  }
89
93
  delete(propertyPath, value, graph) {
90
- if (!graph) {
91
- graph = this.graph;
92
- }
93
94
  switch (propertyPath.termType) {
94
95
  case "InversePath": {
95
96
  if (value) {
96
- for (const subject of this.addableSubjectsToTerms(value)) {
97
+ for (const subject of Array.isArray(value) ? value : [value]) {
97
98
  for (const quad of [
98
99
  ...this.dataset.match(subject, propertyPath.path, this.identifier, graph),
99
100
  ]) {
@@ -112,7 +113,7 @@ export class Resource {
112
113
  }
113
114
  case "NamedNode": {
114
115
  if (value) {
115
- for (const object of this.addableObjectsToTerms(value)) {
116
+ for (const object of Array.isArray(value) ? value : [value]) {
116
117
  for (const quad of [
117
118
  ...this.dataset.match(this.identifier, propertyPath, object, graph),
118
119
  ]) {
@@ -136,7 +137,7 @@ export class Resource {
136
137
  return isInstanceOfRecursive({
137
138
  class_,
138
139
  dataset: this.dataset,
139
- graph: options?.graph ?? this.graph,
140
+ graph: options?.graph,
140
141
  instance: this.identifier,
141
142
  visitedClasses: new TermSet(),
142
143
  });
@@ -173,7 +174,7 @@ export class Resource {
173
174
  return isSubClassOfRecursive({
174
175
  class_,
175
176
  dataset: this.dataset,
176
- graph: options?.graph ?? this.graph,
177
+ graph: options?.graph,
177
178
  thisIdentifier: this.identifier,
178
179
  visitedClasses: new TermSet(),
179
180
  });
@@ -224,7 +225,7 @@ export class Resource {
224
225
  values: [],
225
226
  }));
226
227
  }
227
- const graph = options?.graph ?? this.graph;
228
+ const graph = options?.graph;
228
229
  const firstObjects = [
229
230
  ...new TermSet([...this.dataset.match(this.identifier, rdf.first, null, graph)].map((quad) => quad.object)),
230
231
  ];
@@ -291,8 +292,10 @@ export class Resource {
291
292
  focusResource: this,
292
293
  propertyPath: rdf.first,
293
294
  term: firstObject,
294
- }).toValues()).chain((items) => new Resource(this.dataset, restObject, {
295
+ }).toValues()).chain((items) => new Resource({
295
296
  dataFactory: this.dataFactory,
297
+ dataset: this.dataset,
298
+ identifier: restObject,
296
299
  })
297
300
  .toList({ graph })
298
301
  .map((restItems) => items.concat(...restItems)));
@@ -310,37 +313,11 @@ export class Resource {
310
313
  return new DatasetValues({
311
314
  dataFactory: this.dataFactory,
312
315
  focusResource: this,
313
- graph: options?.graph ?? this.graph ?? null,
316
+ graph: options?.graph ?? null,
314
317
  propertyPath,
315
318
  unique: !!options?.unique,
316
319
  });
317
320
  }
318
- addableObjectToTerm(object) {
319
- switch (typeof object) {
320
- case "bigint":
321
- return this.literalFactory.bigint(object);
322
- case "boolean":
323
- return this.literalFactory.boolean(object);
324
- case "number":
325
- return this.literalFactory.number(object);
326
- case "string":
327
- return this.literalFactory.string(object);
328
- case "object":
329
- return object;
330
- }
331
- }
332
- addableObjectsToTerms(objects) {
333
- if (Array.isArray(objects)) {
334
- return objects.map((value) => this.addableObjectToTerm(value));
335
- }
336
- return [this.addableObjectToTerm(objects)];
337
- }
338
- addableSubjectsToTerms(subjects) {
339
- if (Array.isArray(subjects)) {
340
- return subjects;
341
- }
342
- return [subjects];
343
- }
344
321
  }
345
322
  (function (Resource) {
346
323
  Resource.ListStructureError = _ListStructureError;
@@ -4,12 +4,11 @@ import { Resource } from "./Resource.js";
4
4
  * A ResourceSet wraps an RDF/JS dataset with convenient resource factory methods.
5
5
  */
6
6
  export declare class ResourceSet {
7
- readonly dataset: DatasetCore;
8
7
  private readonly dataFactory;
9
- private readonly graph?;
10
- constructor(dataset: DatasetCore, options?: {
11
- dataFactory?: DataFactory;
12
- graph?: Exclude<Quad_Graph, Variable>;
8
+ readonly dataset: DatasetCore;
9
+ constructor({ dataFactory, dataset, }: {
10
+ dataFactory: DataFactory;
11
+ dataset: DatasetCore;
13
12
  });
14
13
  instancesOf(class_: NamedNode, options?: {
15
14
  excludeSubclasses?: boolean;
@@ -1,15 +1,13 @@
1
1
  import TermSet from "@rdfjs/term-set";
2
- import defaultDataFactory from "@rdfx/data-factory";
3
2
  import { Resource } from "./Resource.js";
4
3
  import { rdf, rdfs } from "./vocabularies.js";
5
4
  /**
6
5
  * A ResourceSet wraps an RDF/JS dataset with convenient resource factory methods.
7
6
  */
8
7
  export class ResourceSet {
9
- constructor(dataset, options) {
8
+ constructor({ dataFactory, dataset, }) {
9
+ this.dataFactory = dataFactory;
10
10
  this.dataset = dataset;
11
- this.dataFactory = options?.dataFactory ?? defaultDataFactory;
12
- this.graph = options?.graph;
13
11
  }
14
12
  *instancesOf(class_, options) {
15
13
  for (const identifier of this.instanceIdentifiers(class_, options)) {
@@ -23,15 +21,17 @@ export class ResourceSet {
23
21
  }
24
22
  }
25
23
  resource(identifier) {
26
- return new Resource(this.dataset, identifier, {
24
+ return new Resource({
27
25
  dataFactory: this.dataFactory,
26
+ dataset: this.dataset,
27
+ identifier,
28
28
  });
29
29
  }
30
30
  *instanceIdentifiers(class_, options) {
31
31
  yield* instanceIdentifiersRecursive({
32
32
  class_,
33
33
  dataset: this.dataset,
34
- graph: options?.graph ?? this.graph,
34
+ graph: options?.graph,
35
35
  visitedClasses: new TermSet(),
36
36
  yieldedInstanceIdentifiers: new TermSet(),
37
37
  });
package/dist/Value.js CHANGED
@@ -105,8 +105,10 @@ export class Value {
105
105
  .chain((value) => this.constrainPrimitive(value, in_));
106
106
  }
107
107
  toResource(in_) {
108
- return (in_ ? this.toIdentifier(in_) : this.toIdentifier()).map((identifier) => new Resource(this.focusResource.dataset, identifier, {
108
+ return (in_ ? this.toIdentifier(in_) : this.toIdentifier()).map((identifier) => new Resource({
109
109
  dataFactory: this.dataFactory,
110
+ dataset: this.focusResource.dataset,
111
+ identifier,
110
112
  }));
111
113
  }
112
114
  toString(in_) {
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "dependencies": {
3
- "@rdfx/data-factory": "0.0.11",
4
- "@rdfx/literal": "0.0.11",
5
- "@rdfx/string": "0.0.11",
3
+ "@rdfx/literal": "0.0.13",
4
+ "@rdfx/string": "0.0.13",
6
5
  "@rdfjs/term-set": "~2.0.3",
7
6
  "@rdfjs/types": "~2.0.1",
8
7
  "@types/rdfjs__term-set": "~2.0.9",
9
8
  "purify-ts": "~2.1.4"
10
9
  },
11
10
  "devDependencies": {
11
+ "@rdfx/data-factory": "0.0.13",
12
12
  "@rdfjs/dataset": "~2.0.2",
13
13
  "@tpluscode/rdf-ns-builders": "~4.3.0",
14
14
  "@types/rdfjs__dataset": "~2.0.7",
@@ -40,5 +40,5 @@
40
40
  },
41
41
  "type": "module",
42
42
  "types": "./dist/index.d.ts",
43
- "version": "0.0.11"
43
+ "version": "0.0.13"
44
44
  }