@twin.org/data-json-ld 0.0.1-next.20 → 0.0.1-next.22

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.
@@ -1539,28 +1539,29 @@ class JsonLdProcessor {
1539
1539
  */
1540
1540
  static async compact(document, context) {
1541
1541
  try {
1542
- // There is a cast here because the jsonld types are not correct.
1543
- // A context definition can be an array or an object, but the types only allow an object.
1544
- if (core.Is.empty(context)) {
1545
- context = {};
1546
- if (core.Is.array(document)) {
1547
- for (const node of document) {
1548
- context = JsonLdProcessor.gatherContexts(node, context);
1542
+ if (core.Is.object(document)) {
1543
+ if (core.Is.empty(context)) {
1544
+ context = {};
1545
+ if (core.Is.array(document)) {
1546
+ for (const node of document) {
1547
+ context = JsonLdProcessor.gatherContexts(node, context);
1548
+ }
1549
1549
  }
1550
- }
1551
- else if (core.Is.array(document["@graph"])) {
1552
- for (const node of document["@graph"]) {
1553
- context = JsonLdProcessor.gatherContexts(node, context);
1550
+ else if (core.Is.array(document["@graph"])) {
1551
+ for (const node of document["@graph"]) {
1552
+ context = JsonLdProcessor.gatherContexts(node, context);
1553
+ }
1554
+ }
1555
+ else if (core.Is.object(document)) {
1556
+ context = JsonLdProcessor.gatherContexts(document, context);
1554
1557
  }
1555
1558
  }
1556
- else if (core.Is.object(document)) {
1557
- context = JsonLdProcessor.gatherContexts(document, context);
1558
- }
1559
+ const compacted = await jsonLd.compact(core.ObjectHelper.removeEmptyProperties(document), context, {
1560
+ documentLoader: JsonLdProcessor.DOCUMENT_LOADER
1561
+ });
1562
+ return compacted;
1559
1563
  }
1560
- const compacted = await jsonLd.compact(core.ObjectHelper.removeEmptyProperties(document), context, {
1561
- documentLoader: JsonLdProcessor.DOCUMENT_LOADER
1562
- });
1563
- return compacted;
1564
+ return document;
1564
1565
  }
1565
1566
  catch (err) {
1566
1567
  if (core.Is.object(err) &&
@@ -1670,18 +1671,20 @@ class JsonLdProcessor {
1670
1671
  */
1671
1672
  static gatherContexts(element, initial) {
1672
1673
  let combinedContexts = initial;
1673
- if (!core.Is.empty(element["@context"])) {
1674
- combinedContexts = JsonLdProcessor.combineContexts(initial, element["@context"]);
1675
- }
1676
- for (const prop of Object.keys(element)) {
1677
- const value = element[prop];
1678
- if (core.Is.object(value)) {
1679
- combinedContexts = this.gatherContexts(value, combinedContexts);
1674
+ if (core.Is.object(element)) {
1675
+ if (!core.Is.empty(element["@context"])) {
1676
+ combinedContexts = JsonLdProcessor.combineContexts(initial, element["@context"]);
1680
1677
  }
1681
- else if (core.Is.array(value)) {
1682
- for (const item of value) {
1683
- if (core.Is.object(item)) {
1684
- combinedContexts = this.gatherContexts(item, combinedContexts);
1678
+ for (const prop of Object.keys(element)) {
1679
+ const value = element[prop];
1680
+ if (core.Is.object(value)) {
1681
+ combinedContexts = this.gatherContexts(value, combinedContexts);
1682
+ }
1683
+ else if (core.Is.array(value)) {
1684
+ for (const item of value) {
1685
+ if (core.Is.object(item)) {
1686
+ combinedContexts = this.gatherContexts(item, combinedContexts);
1687
+ }
1685
1688
  }
1686
1689
  }
1687
1690
  }
@@ -1537,28 +1537,29 @@ class JsonLdProcessor {
1537
1537
  */
1538
1538
  static async compact(document, context) {
1539
1539
  try {
1540
- // There is a cast here because the jsonld types are not correct.
1541
- // A context definition can be an array or an object, but the types only allow an object.
1542
- if (Is.empty(context)) {
1543
- context = {};
1544
- if (Is.array(document)) {
1545
- for (const node of document) {
1546
- context = JsonLdProcessor.gatherContexts(node, context);
1540
+ if (Is.object(document)) {
1541
+ if (Is.empty(context)) {
1542
+ context = {};
1543
+ if (Is.array(document)) {
1544
+ for (const node of document) {
1545
+ context = JsonLdProcessor.gatherContexts(node, context);
1546
+ }
1547
1547
  }
1548
- }
1549
- else if (Is.array(document["@graph"])) {
1550
- for (const node of document["@graph"]) {
1551
- context = JsonLdProcessor.gatherContexts(node, context);
1548
+ else if (Is.array(document["@graph"])) {
1549
+ for (const node of document["@graph"]) {
1550
+ context = JsonLdProcessor.gatherContexts(node, context);
1551
+ }
1552
+ }
1553
+ else if (Is.object(document)) {
1554
+ context = JsonLdProcessor.gatherContexts(document, context);
1552
1555
  }
1553
1556
  }
1554
- else if (Is.object(document)) {
1555
- context = JsonLdProcessor.gatherContexts(document, context);
1556
- }
1557
+ const compacted = await jsonLd.compact(ObjectHelper.removeEmptyProperties(document), context, {
1558
+ documentLoader: JsonLdProcessor.DOCUMENT_LOADER
1559
+ });
1560
+ return compacted;
1557
1561
  }
1558
- const compacted = await jsonLd.compact(ObjectHelper.removeEmptyProperties(document), context, {
1559
- documentLoader: JsonLdProcessor.DOCUMENT_LOADER
1560
- });
1561
- return compacted;
1562
+ return document;
1562
1563
  }
1563
1564
  catch (err) {
1564
1565
  if (Is.object(err) &&
@@ -1668,18 +1669,20 @@ class JsonLdProcessor {
1668
1669
  */
1669
1670
  static gatherContexts(element, initial) {
1670
1671
  let combinedContexts = initial;
1671
- if (!Is.empty(element["@context"])) {
1672
- combinedContexts = JsonLdProcessor.combineContexts(initial, element["@context"]);
1673
- }
1674
- for (const prop of Object.keys(element)) {
1675
- const value = element[prop];
1676
- if (Is.object(value)) {
1677
- combinedContexts = this.gatherContexts(value, combinedContexts);
1672
+ if (Is.object(element)) {
1673
+ if (!Is.empty(element["@context"])) {
1674
+ combinedContexts = JsonLdProcessor.combineContexts(initial, element["@context"]);
1678
1675
  }
1679
- else if (Is.array(value)) {
1680
- for (const item of value) {
1681
- if (Is.object(item)) {
1682
- combinedContexts = this.gatherContexts(item, combinedContexts);
1676
+ for (const prop of Object.keys(element)) {
1677
+ const value = element[prop];
1678
+ if (Is.object(value)) {
1679
+ combinedContexts = this.gatherContexts(value, combinedContexts);
1680
+ }
1681
+ else if (Is.array(value)) {
1682
+ for (const item of value) {
1683
+ if (Is.object(item)) {
1684
+ combinedContexts = this.gatherContexts(item, combinedContexts);
1685
+ }
1683
1686
  }
1684
1687
  }
1685
1688
  }
@@ -1,7 +1,7 @@
1
1
  import type { RemoteDocument, Url } from "jsonld/jsonld-spec";
2
2
  import type { IJsonLdContextDefinitionElement } from "../models/IJsonLdContextDefinitionElement";
3
3
  import type { IJsonLdContextDefinitionRoot } from "../models/IJsonLdContextDefinitionRoot";
4
- import type { IJsonLdDocument } from "../models/IJsonLdDocument";
4
+ import type { IJsonLdNodeObject } from "../models/IJsonLdNodeObject";
5
5
  /**
6
6
  * JSON-LD Processor.
7
7
  */
@@ -20,13 +20,13 @@ export declare class JsonLdProcessor {
20
20
  * @param context The context to compact the document to, if not provided will try and gather from the object.
21
21
  * @returns The compacted JSON-LD document.
22
22
  */
23
- static compact(document: IJsonLdDocument, context?: IJsonLdContextDefinitionRoot): Promise<IJsonLdDocument>;
23
+ static compact<T>(document: T, context?: IJsonLdContextDefinitionRoot): Promise<T>;
24
24
  /**
25
25
  * Expand a document, removing its context.
26
26
  * @param compacted The compacted JSON-LD document to expand.
27
27
  * @returns The expanded JSON-LD document.
28
28
  */
29
- static expand(compacted: IJsonLdDocument): Promise<IJsonLdDocument>;
29
+ static expand<T extends IJsonLdNodeObject>(compacted: T): Promise<IJsonLdNodeObject[]>;
30
30
  /**
31
31
  * Add a redirect to use during document resolution.
32
32
  * @param from The URL to redirect from.
@@ -46,9 +46,7 @@ export declare class JsonLdProcessor {
46
46
  * @param initial The initial context.
47
47
  * @returns The combined contexts.
48
48
  */
49
- static gatherContexts(element: {
50
- [id: string]: unknown;
51
- }, initial?: IJsonLdContextDefinitionRoot): IJsonLdContextDefinitionRoot | undefined;
49
+ static gatherContexts<T>(element: T, initial?: IJsonLdContextDefinitionRoot): IJsonLdContextDefinitionRoot | undefined;
52
50
  /**
53
51
  * Remove all the contexts that match the pattern.
54
52
  * @param context The context to remove the entries from.
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/data-json-ld - Changelog
2
2
 
3
- ## v0.0.1-next.20
3
+ ## v0.0.1-next.22
4
4
 
5
5
  - Initial Release
@@ -34,15 +34,19 @@ The document loader to use.
34
34
 
35
35
  ### compact()
36
36
 
37
- > `static` **compact**(`document`, `context`?): `Promise`\<[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)\>
37
+ > `static` **compact**\<`T`\>(`document`, `context`?): `Promise`\<`T`\>
38
38
 
39
39
  Compact a document according to a particular context.
40
40
 
41
+ #### Type Parameters
42
+
43
+ • **T**
44
+
41
45
  #### Parameters
42
46
 
43
47
  ##### document
44
48
 
45
- [`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
49
+ `T`
46
50
 
47
51
  The JSON-LD document to compact.
48
52
 
@@ -54,7 +58,7 @@ The context to compact the document to, if not provided will try and gather from
54
58
 
55
59
  #### Returns
56
60
 
57
- `Promise`\<[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)\>
61
+ `Promise`\<`T`\>
58
62
 
59
63
  The compacted JSON-LD document.
60
64
 
@@ -62,21 +66,25 @@ The compacted JSON-LD document.
62
66
 
63
67
  ### expand()
64
68
 
65
- > `static` **expand**(`compacted`): `Promise`\<[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)\>
69
+ > `static` **expand**\<`T`\>(`compacted`): `Promise`\<[`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)[]\>
66
70
 
67
71
  Expand a document, removing its context.
68
72
 
73
+ #### Type Parameters
74
+
75
+ • **T** *extends* [`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)
76
+
69
77
  #### Parameters
70
78
 
71
79
  ##### compacted
72
80
 
73
- [`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
81
+ `T`
74
82
 
75
83
  The compacted JSON-LD document to expand.
76
84
 
77
85
  #### Returns
78
86
 
79
- `Promise`\<[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)\>
87
+ `Promise`\<[`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)[]\>
80
88
 
81
89
  The expanded JSON-LD document.
82
90
 
@@ -138,14 +146,20 @@ The combined context.
138
146
 
139
147
  ### gatherContexts()
140
148
 
141
- > `static` **gatherContexts**(`element`, `initial`?): `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
149
+ > `static` **gatherContexts**\<`T`\>(`element`, `initial`?): `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
142
150
 
143
151
  Gather all the contexts from the element and it's children.
144
152
 
153
+ #### Type Parameters
154
+
155
+ • **T**
156
+
145
157
  #### Parameters
146
158
 
147
159
  ##### element
148
160
 
161
+ `T`
162
+
149
163
  The element to gather the contexts from.
150
164
 
151
165
  ##### initial?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/data-json-ld",
3
- "version": "0.0.1-next.20",
3
+ "version": "0.0.1-next.22",
4
4
  "description": "Models which define the structure of JSON LD",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@twin.org/core": "next",
18
- "@twin.org/data-core": "0.0.1-next.20",
18
+ "@twin.org/data-core": "0.0.1-next.22",
19
19
  "@twin.org/entity": "next",
20
20
  "@twin.org/nameof": "next",
21
21
  "@twin.org/web": "next",