@twin.org/data-json-ld 0.0.1-next.30 → 0.0.1-next.31

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.
@@ -1591,31 +1591,48 @@ class JsonLdProcessor {
1591
1591
  /**
1592
1592
  * Compact a document according to a particular context.
1593
1593
  * @param document The JSON-LD document to compact.
1594
- * @param context The context to compact the document to, if not provided will try and gather from the object.
1594
+ * @param context The context to compact the document to, if not provided will use the one in the document.
1595
+ * @param options The options for compacting the document.
1596
+ * @param options.itemListOverride Whether to override the itemListElement context with a set, defaults to true.
1595
1597
  * @returns The compacted JSON-LD document.
1596
1598
  */
1597
- static async compact(document, context) {
1599
+ static async compact(document, context, options) {
1598
1600
  try {
1599
1601
  if (core.Is.object(document)) {
1600
- if (core.Is.empty(context)) {
1601
- context = {};
1602
- if (core.Is.array(document)) {
1603
- for (const node of document) {
1604
- context = JsonLdProcessor.gatherContexts(node, context);
1605
- }
1606
- }
1607
- else if (core.Is.array(document["@graph"])) {
1608
- for (const node of document["@graph"]) {
1609
- context = JsonLdProcessor.gatherContexts(node, context);
1602
+ // If the user didn't provide a context, use the one from the document
1603
+ if (core.Is.empty(context) && !core.Is.empty(document["@context"])) {
1604
+ context = document["@context"];
1605
+ }
1606
+ const overrideListElementOption = options?.itemListOverride ?? true;
1607
+ let overrideContext;
1608
+ if (overrideListElementOption) {
1609
+ // The compactArrays flag doesn't work with the current version of jsonld.js
1610
+ // For list results we standardise on ItemList and itemListElement
1611
+ // so we modify the schema.org type for itemListElement to be a set which bypasses the issue
1612
+ // https://github.com/digitalbazaar/jsonld.js/issues/247
1613
+ overrideContext = {
1614
+ itemListElement: {
1615
+ "@id": "http://schema.org/itemListElement",
1616
+ "@container": "@set",
1617
+ "@protected": true
1610
1618
  }
1619
+ };
1620
+ if (core.Is.object(context) && "@context" in context) {
1621
+ // If the context is an object, we need to merge it with the override context
1622
+ context = JsonLdProcessor.combineContexts(context["@context"], overrideContext);
1611
1623
  }
1612
- else if (core.Is.object(document)) {
1613
- context = JsonLdProcessor.gatherContexts(document, context);
1624
+ else {
1625
+ // If the context is a string or an array, we need to merge it with the override context
1626
+ context = JsonLdProcessor.combineContexts(context, overrideContext);
1614
1627
  }
1615
1628
  }
1616
1629
  const compacted = await jsonLd.compact(core.ObjectHelper.removeEmptyProperties(document), context, {
1617
1630
  documentLoader: JsonLdProcessor.getDocumentLoader()
1618
1631
  });
1632
+ if (!core.Is.empty(overrideContext)) {
1633
+ // Remove the override context from the compacted document
1634
+ compacted["@context"] = JsonLdProcessor.removeContexts(compacted["@context"], [overrideContext]);
1635
+ }
1619
1636
  return compacted;
1620
1637
  }
1621
1638
  return document;
@@ -1589,31 +1589,48 @@ class JsonLdProcessor {
1589
1589
  /**
1590
1590
  * Compact a document according to a particular context.
1591
1591
  * @param document The JSON-LD document to compact.
1592
- * @param context The context to compact the document to, if not provided will try and gather from the object.
1592
+ * @param context The context to compact the document to, if not provided will use the one in the document.
1593
+ * @param options The options for compacting the document.
1594
+ * @param options.itemListOverride Whether to override the itemListElement context with a set, defaults to true.
1593
1595
  * @returns The compacted JSON-LD document.
1594
1596
  */
1595
- static async compact(document, context) {
1597
+ static async compact(document, context, options) {
1596
1598
  try {
1597
1599
  if (Is.object(document)) {
1598
- if (Is.empty(context)) {
1599
- context = {};
1600
- if (Is.array(document)) {
1601
- for (const node of document) {
1602
- context = JsonLdProcessor.gatherContexts(node, context);
1603
- }
1604
- }
1605
- else if (Is.array(document["@graph"])) {
1606
- for (const node of document["@graph"]) {
1607
- context = JsonLdProcessor.gatherContexts(node, context);
1600
+ // If the user didn't provide a context, use the one from the document
1601
+ if (Is.empty(context) && !Is.empty(document["@context"])) {
1602
+ context = document["@context"];
1603
+ }
1604
+ const overrideListElementOption = options?.itemListOverride ?? true;
1605
+ let overrideContext;
1606
+ if (overrideListElementOption) {
1607
+ // The compactArrays flag doesn't work with the current version of jsonld.js
1608
+ // For list results we standardise on ItemList and itemListElement
1609
+ // so we modify the schema.org type for itemListElement to be a set which bypasses the issue
1610
+ // https://github.com/digitalbazaar/jsonld.js/issues/247
1611
+ overrideContext = {
1612
+ itemListElement: {
1613
+ "@id": "http://schema.org/itemListElement",
1614
+ "@container": "@set",
1615
+ "@protected": true
1608
1616
  }
1617
+ };
1618
+ if (Is.object(context) && "@context" in context) {
1619
+ // If the context is an object, we need to merge it with the override context
1620
+ context = JsonLdProcessor.combineContexts(context["@context"], overrideContext);
1609
1621
  }
1610
- else if (Is.object(document)) {
1611
- context = JsonLdProcessor.gatherContexts(document, context);
1622
+ else {
1623
+ // If the context is a string or an array, we need to merge it with the override context
1624
+ context = JsonLdProcessor.combineContexts(context, overrideContext);
1612
1625
  }
1613
1626
  }
1614
1627
  const compacted = await jsonLd.compact(ObjectHelper.removeEmptyProperties(document), context, {
1615
1628
  documentLoader: JsonLdProcessor.getDocumentLoader()
1616
1629
  });
1630
+ if (!Is.empty(overrideContext)) {
1631
+ // Remove the override context from the compacted document
1632
+ compacted["@context"] = JsonLdProcessor.removeContexts(compacted["@context"], [overrideContext]);
1633
+ }
1617
1634
  return compacted;
1618
1635
  }
1619
1636
  return document;
@@ -45,10 +45,14 @@ export declare class JsonLdProcessor {
45
45
  /**
46
46
  * Compact a document according to a particular context.
47
47
  * @param document The JSON-LD document to compact.
48
- * @param context The context to compact the document to, if not provided will try and gather from the object.
48
+ * @param context The context to compact the document to, if not provided will use the one in the document.
49
+ * @param options The options for compacting the document.
50
+ * @param options.itemListOverride Whether to override the itemListElement context with a set, defaults to true.
49
51
  * @returns The compacted JSON-LD document.
50
52
  */
51
- static compact<T>(document: T, context?: IJsonLdContextDefinitionRoot): Promise<T>;
53
+ static compact<T>(document: T, context?: IJsonLdContextDefinitionRoot, options?: {
54
+ itemListOverride: boolean;
55
+ }): Promise<T>;
52
56
  /**
53
57
  * Expand a document, removing its context.
54
58
  * @param compacted The compacted JSON-LD document to expand.
package/docs/changelog.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @twin.org/data-json-ld - Changelog
2
2
 
3
+ ## [0.0.1-next.31](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.1-next.30...data-json-ld-v0.0.1-next.31) (2025-05-08)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * itemListElement compaction override ([d908a10](https://github.com/twinfoundation/data/commit/d908a1043d7792e31b3101221d17850757b6c2a6))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/data-core bumped from 0.0.1-next.30 to 0.0.1-next.31
16
+
3
17
  ## [0.0.1-next.30](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.1-next.29...data-json-ld-v0.0.1-next.30) (2025-04-17)
4
18
 
5
19
 
@@ -128,7 +128,7 @@ The registered redirects.
128
128
 
129
129
  ### compact()
130
130
 
131
- > `static` **compact**\<`T`\>(`document`, `context?`): `Promise`\<`T`\>
131
+ > `static` **compact**\<`T`\>(`document`, `context?`, `options?`): `Promise`\<`T`\>
132
132
 
133
133
  Compact a document according to a particular context.
134
134
 
@@ -150,7 +150,17 @@ The JSON-LD document to compact.
150
150
 
151
151
  [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
152
152
 
153
- The context to compact the document to, if not provided will try and gather from the object.
153
+ The context to compact the document to, if not provided will use the one in the document.
154
+
155
+ ##### options?
156
+
157
+ The options for compacting the document.
158
+
159
+ ###### itemListOverride
160
+
161
+ `boolean`
162
+
163
+ Whether to override the itemListElement context with a set, defaults to true.
154
164
 
155
165
  #### Returns
156
166
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/data-json-ld",
3
- "version": "0.0.1-next.30",
3
+ "version": "0.0.1-next.31",
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.30",
18
+ "@twin.org/data-core": "0.0.1-next.31",
19
19
  "@twin.org/entity": "next",
20
20
  "@twin.org/nameof": "next",
21
21
  "@twin.org/web": "next",