@twin.org/data-json-ld 0.0.2-next.4 → 0.0.3-next.10
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.
- package/dist/es/dataTypes/jsonLdDataTypes.js +144 -0
- package/dist/es/dataTypes/jsonLdDataTypes.js.map +1 -0
- package/dist/es/helpers/jsonLdHelperTypes.js +2 -0
- package/dist/es/helpers/jsonLdHelperTypes.js.map +1 -0
- package/dist/es/index.js +34 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IJsonLdContainerType.js +4 -0
- package/dist/es/models/IJsonLdContainerType.js.map +1 -0
- package/dist/es/models/IJsonLdContainerTypeArray.js +2 -0
- package/dist/es/models/IJsonLdContainerTypeArray.js.map +1 -0
- package/dist/es/models/IJsonLdContextDefinition.js +2 -0
- package/dist/es/models/IJsonLdContextDefinition.js.map +1 -0
- package/dist/es/models/IJsonLdContextDefinitionElement.js +2 -0
- package/dist/es/models/IJsonLdContextDefinitionElement.js.map +1 -0
- package/dist/es/models/IJsonLdContextDefinitionRoot.js +2 -0
- package/dist/es/models/IJsonLdContextDefinitionRoot.js.map +1 -0
- package/dist/es/models/IJsonLdDocument.js +2 -0
- package/dist/es/models/IJsonLdDocument.js.map +1 -0
- package/dist/es/models/IJsonLdExpandedTermDefinition.js +2 -0
- package/dist/es/models/IJsonLdExpandedTermDefinition.js.map +1 -0
- package/dist/es/models/IJsonLdGraphObject.js +2 -0
- package/dist/es/models/IJsonLdGraphObject.js.map +1 -0
- package/dist/es/models/IJsonLdIdMap.js +2 -0
- package/dist/es/models/IJsonLdIdMap.js.map +1 -0
- package/dist/es/models/IJsonLdIncludedBlock.js +2 -0
- package/dist/es/models/IJsonLdIncludedBlock.js.map +1 -0
- package/dist/es/models/IJsonLdIndexMap.js +2 -0
- package/dist/es/models/IJsonLdIndexMap.js.map +1 -0
- package/dist/es/models/IJsonLdIndexMapItem.js +2 -0
- package/dist/es/models/IJsonLdIndexMapItem.js.map +1 -0
- package/dist/es/models/IJsonLdJsonArray.js +2 -0
- package/dist/es/models/IJsonLdJsonArray.js.map +1 -0
- package/dist/es/models/IJsonLdJsonObject.js +2 -0
- package/dist/es/models/IJsonLdJsonObject.js.map +1 -0
- package/dist/es/models/IJsonLdJsonPrimitive.js +4 -0
- package/dist/es/models/IJsonLdJsonPrimitive.js.map +1 -0
- package/dist/es/models/IJsonLdJsonValue.js +2 -0
- package/dist/es/models/IJsonLdJsonValue.js.map +1 -0
- package/dist/es/models/IJsonLdLanguageMap.js +4 -0
- package/dist/es/models/IJsonLdLanguageMap.js.map +1 -0
- package/dist/es/models/IJsonLdListObject.js +2 -0
- package/dist/es/models/IJsonLdListObject.js.map +1 -0
- package/dist/es/models/IJsonLdListOrSetItem.js +2 -0
- package/dist/es/models/IJsonLdListOrSetItem.js.map +1 -0
- package/dist/es/models/IJsonLdNodeObject.js +2 -0
- package/dist/es/models/IJsonLdNodeObject.js.map +1 -0
- package/dist/es/models/IJsonLdNodePrimitive.js +2 -0
- package/dist/es/models/IJsonLdNodePrimitive.js.map +1 -0
- package/dist/es/models/IJsonLdObject.js +2 -0
- package/dist/es/models/IJsonLdObject.js.map +1 -0
- package/dist/es/models/IJsonLdSetObject.js +2 -0
- package/dist/es/models/IJsonLdSetObject.js.map +1 -0
- package/dist/es/models/IJsonLdTypeMap.js +2 -0
- package/dist/es/models/IJsonLdTypeMap.js.map +1 -0
- package/dist/es/models/IJsonLdValueObject.js +2 -0
- package/dist/es/models/IJsonLdValueObject.js.map +1 -0
- package/dist/es/models/jsonLdContexts.js +21 -0
- package/dist/es/models/jsonLdContexts.js.map +1 -0
- package/dist/es/models/jsonLdTypes.js +113 -0
- package/dist/es/models/jsonLdTypes.js.map +1 -0
- package/dist/es/schemas/JsonLdContainerType.json +13 -0
- package/dist/es/schemas/JsonLdContainerTypeArray.json +179 -0
- package/dist/es/schemas/JsonLdContextDefinition.json +76 -0
- package/dist/es/schemas/JsonLdContextDefinitionElement.json +16 -0
- package/dist/es/schemas/JsonLdContextDefinitionRoot.json +16 -0
- package/dist/es/schemas/JsonLdDocument.json +48 -0
- package/dist/es/schemas/JsonLdExpandedTermDefinition.json +136 -0
- package/dist/es/schemas/JsonLdGraphObject.json +44 -0
- package/dist/es/schemas/JsonLdIdMap.json +9 -0
- package/dist/es/schemas/JsonLdIncludedBlock.json +16 -0
- package/dist/es/schemas/JsonLdIndexMap.json +19 -0
- package/dist/es/schemas/JsonLdIndexMapItem.json +31 -0
- package/dist/es/schemas/JsonLdJsonArray.json +9 -0
- package/dist/es/schemas/JsonLdJsonObject.json +16 -0
- package/dist/es/schemas/JsonLdJsonPrimitive.json +11 -0
- package/dist/es/schemas/JsonLdJsonValue.json +16 -0
- package/dist/es/schemas/JsonLdLanguageMap.json +22 -0
- package/dist/es/schemas/JsonLdListObject.json +28 -0
- package/dist/es/schemas/JsonLdListOrSetItem.json +25 -0
- package/dist/es/schemas/JsonLdNodeObject.json +100 -0
- package/dist/es/schemas/JsonLdNodePrimitive.json +34 -0
- package/dist/es/schemas/JsonLdObject.json +76 -0
- package/dist/es/schemas/JsonLdSetObject.json +28 -0
- package/dist/es/schemas/JsonLdTypeMap.json +16 -0
- package/dist/es/schemas/JsonLdValueObject.json +113 -0
- package/dist/es/utils/jsonLdHelper.js +287 -0
- package/dist/es/utils/jsonLdHelper.js.map +1 -0
- package/dist/es/utils/jsonLdProcessor.js +375 -0
- package/dist/es/utils/jsonLdProcessor.js.map +1 -0
- package/dist/types/helpers/jsonLdHelperTypes.d.ts +77 -0
- package/dist/types/index.d.ts +31 -30
- package/dist/types/models/IJsonLdContainerTypeArray.d.ts +1 -1
- package/dist/types/models/IJsonLdContextDefinition.d.ts +1 -1
- package/dist/types/models/IJsonLdContextDefinitionElement.d.ts +1 -1
- package/dist/types/models/IJsonLdContextDefinitionRoot.d.ts +1 -1
- package/dist/types/models/IJsonLdDocument.d.ts +3 -3
- package/dist/types/models/IJsonLdExpandedTermDefinition.d.ts +3 -3
- package/dist/types/models/IJsonLdGraphObject.d.ts +2 -2
- package/dist/types/models/IJsonLdIdMap.d.ts +1 -1
- package/dist/types/models/IJsonLdIncludedBlock.d.ts +1 -1
- package/dist/types/models/IJsonLdIndexMap.d.ts +1 -1
- package/dist/types/models/IJsonLdIndexMapItem.d.ts +4 -4
- package/dist/types/models/IJsonLdJsonArray.d.ts +1 -1
- package/dist/types/models/IJsonLdJsonObject.d.ts +1 -1
- package/dist/types/models/IJsonLdJsonValue.d.ts +3 -3
- package/dist/types/models/IJsonLdListObject.d.ts +1 -1
- package/dist/types/models/IJsonLdListOrSetItem.d.ts +2 -2
- package/dist/types/models/IJsonLdNodeObject.d.ts +7 -7
- package/dist/types/models/IJsonLdNodePrimitive.d.ts +5 -5
- package/dist/types/models/IJsonLdObject.d.ts +4 -4
- package/dist/types/models/IJsonLdSetObject.d.ts +1 -1
- package/dist/types/models/IJsonLdTypeMap.d.ts +1 -1
- package/dist/types/models/IJsonLdValueObject.d.ts +3 -3
- package/dist/types/models/jsonLdContexts.d.ts +10 -2
- package/dist/types/utils/jsonLdHelper.d.ts +70 -1
- package/dist/types/utils/jsonLdProcessor.d.ts +4 -4
- package/docs/changelog.md +162 -0
- package/docs/reference/classes/JsonLdHelper.md +274 -0
- package/docs/reference/classes/JsonLdProcessor.md +9 -9
- package/docs/reference/index.md +13 -0
- package/docs/reference/interfaces/IJsonLdContextDefinition.md +4 -4
- package/docs/reference/interfaces/IJsonLdJsonObject.md +1 -1
- package/docs/reference/interfaces/IJsonLdLanguageMap.md +1 -1
- package/docs/reference/interfaces/IJsonLdNodeObject.md +1 -1
- package/docs/reference/type-aliases/JsonLdAliasKey.md +15 -0
- package/docs/reference/type-aliases/JsonLdExistingProperty.md +19 -0
- package/docs/reference/type-aliases/JsonLdKeys.md +11 -0
- package/docs/reference/type-aliases/JsonLdObjectWithAliases.md +16 -0
- package/docs/reference/type-aliases/JsonLdObjectWithContext.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithId.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithOptionalContext.md +22 -0
- package/docs/reference/type-aliases/JsonLdObjectWithOptionalId.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithOptionalType.md +21 -0
- package/docs/reference/type-aliases/JsonLdObjectWithType.md +21 -0
- package/docs/reference/type-aliases/JsonLdOptionalKeys.md +11 -0
- package/docs/reference/type-aliases/JsonLdRequiredKeys.md +11 -0
- package/docs/reference/type-aliases/JsonLdWithAliases.md +17 -0
- package/docs/reference/variables/JsonLdContexts.md +15 -3
- package/package.json +6 -8
- package/dist/cjs/index.cjs +0 -2018
- package/dist/esm/index.mjs +0 -2012
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { ArrayHelper, Guards, Is } from "@twin.org/core";
|
|
4
|
+
import { DataTypeHelper } from "@twin.org/data-core";
|
|
5
|
+
import { JsonLdProcessor } from "./jsonLdProcessor.js";
|
|
6
|
+
/**
|
|
7
|
+
* Class to help with JSON LD.
|
|
8
|
+
*/
|
|
9
|
+
export class JsonLdHelper {
|
|
10
|
+
/**
|
|
11
|
+
* The class name.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
static CLASS_NAME = "JsonLdHelper";
|
|
15
|
+
/**
|
|
16
|
+
* Validate a JSON-LD document.
|
|
17
|
+
* @param document The JSON-LD document to validate.
|
|
18
|
+
* @param validationFailures The list of validation failures to add to.
|
|
19
|
+
* @param options Optional options for validation.
|
|
20
|
+
* @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.
|
|
21
|
+
* @param options.validationMode The validation mode to use, defaults to either.
|
|
22
|
+
* @returns True if the document was valid.
|
|
23
|
+
*/
|
|
24
|
+
static async validate(document, validationFailures, options) {
|
|
25
|
+
if (Is.array(document)) {
|
|
26
|
+
// If the document is an array of nodes, validate each node
|
|
27
|
+
for (const node of document) {
|
|
28
|
+
await JsonLdHelper.validate(node, validationFailures, options);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (Is.array(document["@graph"])) {
|
|
32
|
+
// If the graph is an array of nodes, validate each node
|
|
33
|
+
for (const node of document["@graph"]) {
|
|
34
|
+
await JsonLdHelper.validate(node, validationFailures, options);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (Is.object(document)) {
|
|
38
|
+
// Expand the document to ensure we have the full context for types
|
|
39
|
+
// As the data types in the factories are always fully qualified
|
|
40
|
+
const expandedDocs = await JsonLdProcessor.expand(document);
|
|
41
|
+
if (Is.arrayValue(expandedDocs)) {
|
|
42
|
+
for (const expandedDoc of expandedDocs) {
|
|
43
|
+
const expandedDataTypes = ArrayHelper.fromObjectOrArray(expandedDoc["@type"]);
|
|
44
|
+
if (Is.arrayValue(expandedDataTypes)) {
|
|
45
|
+
for (const expandedDataType of expandedDataTypes) {
|
|
46
|
+
await DataTypeHelper.validate("document", expandedDataType, document, validationFailures, options);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return validationFailures.length === 0;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Expand an object to a JSON-LD node object.
|
|
56
|
+
* @param object The object to expand.
|
|
57
|
+
* @returns The expanded JSON-LD node object.
|
|
58
|
+
*/
|
|
59
|
+
static toNodeObject(object) {
|
|
60
|
+
Guards.object(JsonLdHelper.CLASS_NAME, "object", object);
|
|
61
|
+
return object;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Expand the JSON-LD document.
|
|
65
|
+
* @param document The JSON-LD document to expand.
|
|
66
|
+
* @returns The expanded JSON-LD document.
|
|
67
|
+
*/
|
|
68
|
+
static async expand(document) {
|
|
69
|
+
Guards.object(JsonLdHelper.CLASS_NAME, "document", document);
|
|
70
|
+
return JsonLdProcessor.expand(document);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Expand the JSON-LD document and check if it is of a specific type.
|
|
74
|
+
* @param documentOrExpanded The JSON-LD document to check or already expanded document.
|
|
75
|
+
* @param type The type to check for.
|
|
76
|
+
* @returns True if the document is of the specified type.
|
|
77
|
+
*/
|
|
78
|
+
static async isType(documentOrExpanded, type) {
|
|
79
|
+
const expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);
|
|
80
|
+
Guards.arrayValue(JsonLdHelper.CLASS_NAME, "type", type);
|
|
81
|
+
if (Is.arrayValue(expanded)) {
|
|
82
|
+
for (const item of expanded) {
|
|
83
|
+
const types = ArrayHelper.fromObjectOrArray(item["@type"]);
|
|
84
|
+
if (Is.arrayValue(types)) {
|
|
85
|
+
// All required types must be present in this item's @type array
|
|
86
|
+
const itemTypes = new Set(types);
|
|
87
|
+
if (type.every(t => itemTypes.has(t))) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get the types from the document.
|
|
97
|
+
* @param documentOrExpanded The JSON-LD document to check or already expanded document.
|
|
98
|
+
* @returns The type(s) extracted from the document.
|
|
99
|
+
*/
|
|
100
|
+
static async getType(documentOrExpanded) {
|
|
101
|
+
const expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);
|
|
102
|
+
const types = new Set();
|
|
103
|
+
const props = ["@type", "type"];
|
|
104
|
+
for (const expandedDoc of expanded) {
|
|
105
|
+
for (const prop of props) {
|
|
106
|
+
const expandedProps = ArrayHelper.fromObjectOrArray(expandedDoc[prop]);
|
|
107
|
+
if (Is.arrayValue(expandedProps)) {
|
|
108
|
+
for (const expandedProp of expandedProps) {
|
|
109
|
+
const arr = ArrayHelper.fromObjectOrArray(expandedProp);
|
|
110
|
+
for (const arrValue of arr) {
|
|
111
|
+
if (Is.stringValue(arrValue)) {
|
|
112
|
+
types.add(arrValue);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return Array.from(types);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get the id from the document.
|
|
123
|
+
* @param documentOrExpanded The JSON-LD document to get the id from or already expanded document.
|
|
124
|
+
* @returns The id extracted from the document.
|
|
125
|
+
*/
|
|
126
|
+
static async getId(documentOrExpanded) {
|
|
127
|
+
const expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);
|
|
128
|
+
const props = ["@id", "id"];
|
|
129
|
+
for (const expandedDoc of expanded) {
|
|
130
|
+
for (const prop of props) {
|
|
131
|
+
const expandedProps = ArrayHelper.fromObjectOrArray(expandedDoc[prop]);
|
|
132
|
+
if (Is.arrayValue(expandedProps)) {
|
|
133
|
+
for (const expandedProp of expandedProps) {
|
|
134
|
+
const arr = ArrayHelper.fromObjectOrArray(expandedProp);
|
|
135
|
+
for (const arrValue of arr) {
|
|
136
|
+
if (Is.stringValue(arrValue)) {
|
|
137
|
+
return arrValue;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get property values by a single full expanded property name.
|
|
147
|
+
* @param documentOrExpanded The JSON-LD document to get the property from or already expanded document.
|
|
148
|
+
* @param propertyFullName The full expanded property name.
|
|
149
|
+
* @param language Optional filter values by their language property.
|
|
150
|
+
* @returns Matching property values for the input property.
|
|
151
|
+
*/
|
|
152
|
+
static async getPropertyValue(documentOrExpanded, propertyFullName, language) {
|
|
153
|
+
const results = await JsonLdHelper.getPropertyValues(documentOrExpanded, [propertyFullName], language);
|
|
154
|
+
return results[0];
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Get property values by their full expanded property names.
|
|
158
|
+
* @param documentOrExpanded The JSON-LD document to get the property from or already expanded document.
|
|
159
|
+
* @param propertyFullNames The full expanded property names.
|
|
160
|
+
* @param language Optional filter values by their language property.
|
|
161
|
+
* @returns Matching property values for each input property, in the same index order.
|
|
162
|
+
*/
|
|
163
|
+
static async getPropertyValues(documentOrExpanded, propertyFullNames, language) {
|
|
164
|
+
const expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);
|
|
165
|
+
Guards.arrayValue(JsonLdHelper.CLASS_NAME, "propertyFullNames", propertyFullNames);
|
|
166
|
+
if (Is.stringValue(language)) {
|
|
167
|
+
Guards.stringValue(JsonLdHelper.CLASS_NAME, "language", language);
|
|
168
|
+
}
|
|
169
|
+
const languageLower = Is.stringValue(language) ? language.toLowerCase() : undefined;
|
|
170
|
+
const result = [];
|
|
171
|
+
for (const propertyFullName of propertyFullNames) {
|
|
172
|
+
Guards.stringValue(JsonLdHelper.CLASS_NAME, "propertyFullName", propertyFullName);
|
|
173
|
+
const values = [];
|
|
174
|
+
for (const expandedDoc of expanded) {
|
|
175
|
+
const propValue = expandedDoc[propertyFullName];
|
|
176
|
+
if (!Is.empty(propValue)) {
|
|
177
|
+
const expandedProps = ArrayHelper.fromObjectOrArray(propValue);
|
|
178
|
+
if (Is.arrayValue(expandedProps)) {
|
|
179
|
+
for (const expandedProp of expandedProps) {
|
|
180
|
+
if (Is.object(expandedProp)) {
|
|
181
|
+
if (!Is.empty(expandedProp["@value"])) {
|
|
182
|
+
let shouldAdd = true;
|
|
183
|
+
if (Is.stringValue(languageLower)) {
|
|
184
|
+
const itemLanguage = Is.stringValue(expandedProp["@language"])
|
|
185
|
+
? expandedProp["@language"].toLowerCase()
|
|
186
|
+
: undefined;
|
|
187
|
+
shouldAdd = itemLanguage === languageLower;
|
|
188
|
+
}
|
|
189
|
+
if (shouldAdd) {
|
|
190
|
+
values.push(expandedProp["@value"]);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
else if (!Is.stringValue(languageLower)) {
|
|
194
|
+
values.push(expandedProp);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else if (Is.stringValue(expandedProp) ||
|
|
198
|
+
Is.boolean(expandedProp) ||
|
|
199
|
+
Is.number(expandedProp)) {
|
|
200
|
+
if (!Is.stringValue(languageLower)) {
|
|
201
|
+
values.push(expandedProp);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
result.push(values.length > 0 ? values : undefined);
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Prefix all properties in the document with the provided prefix, except for JSON-LD properties.
|
|
214
|
+
* This is useful for ensuring that all properties are fully qualified with a namespace.
|
|
215
|
+
* For example, if the prefix is "ex" and the document has a property "name", it will be transformed to "ex:name".
|
|
216
|
+
* @param nodeObject The JSON-LD node object to prefix properties on.
|
|
217
|
+
* @param prefix The prefix to add to the properties.
|
|
218
|
+
* @param properties Optional list of properties to prefix. If not provided, all properties except for JSON-LD properties.
|
|
219
|
+
* @returns A new JSON-LD node object with the properties prefixed.
|
|
220
|
+
*/
|
|
221
|
+
static prefixProperties(nodeObject, prefix, properties) {
|
|
222
|
+
Guards.object(JsonLdHelper.CLASS_NAME, "nodeObject", nodeObject);
|
|
223
|
+
Guards.stringValue(JsonLdHelper.CLASS_NAME, "prefix", prefix);
|
|
224
|
+
const normalizedPrefix = prefix.endsWith(":") ? prefix.slice(0, -1) : prefix;
|
|
225
|
+
const hasNoPropertiesFilter = !Is.arrayValue(properties);
|
|
226
|
+
const prefixedNodeObject = {};
|
|
227
|
+
for (const key in nodeObject) {
|
|
228
|
+
const value = nodeObject[key];
|
|
229
|
+
if (Is.stringValue(key) &&
|
|
230
|
+
!key.startsWith("@") &&
|
|
231
|
+
(hasNoPropertiesFilter || properties.includes(key))) {
|
|
232
|
+
prefixedNodeObject[`${normalizedPrefix}:${key}`] = value;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
prefixedNodeObject[key] = value;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return prefixedNodeObject;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Strip a prefix from properties in the document, except for JSON-LD properties.
|
|
242
|
+
* This is useful for converting fully qualified namespaced properties back to local names.
|
|
243
|
+
* For example, if the prefix is "ex" and the document has a property "ex:name", it will be transformed to "name".
|
|
244
|
+
* @param nodeObject The JSON-LD node object to strip prefixed properties from.
|
|
245
|
+
* @param prefix The prefix to remove from the properties.
|
|
246
|
+
* @param properties Optional list of unprefixed properties to strip. If not provided, all matching prefixed properties.
|
|
247
|
+
* @returns A new JSON-LD node object with the prefix stripped from matching properties.
|
|
248
|
+
*/
|
|
249
|
+
static stripPrefixProperties(nodeObject, prefix, properties) {
|
|
250
|
+
Guards.object(JsonLdHelper.CLASS_NAME, "nodeObject", nodeObject);
|
|
251
|
+
Guards.stringValue(JsonLdHelper.CLASS_NAME, "prefix", prefix);
|
|
252
|
+
const normalizedPrefix = prefix.endsWith(":") ? prefix.slice(0, -1) : prefix;
|
|
253
|
+
const hasNoPropertiesFilter = !Is.arrayValue(properties);
|
|
254
|
+
const strippedNodeObject = {};
|
|
255
|
+
for (const key in nodeObject) {
|
|
256
|
+
const value = nodeObject[key];
|
|
257
|
+
if (Is.stringValue(key) && !key.startsWith("@") && key.startsWith(`${normalizedPrefix}:`)) {
|
|
258
|
+
const strippedKey = key.slice(normalizedPrefix.length + 1);
|
|
259
|
+
if (hasNoPropertiesFilter || properties.includes(strippedKey)) {
|
|
260
|
+
strippedNodeObject[strippedKey] = value;
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
strippedNodeObject[key] = value;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
strippedNodeObject[key] = value;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return strippedNodeObject;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Get an expanded JSON-LD document from either compact or expanded input.
|
|
274
|
+
* @param documentOrExpanded The JSON-LD document or expanded document.
|
|
275
|
+
* @returns The expanded JSON-LD document.
|
|
276
|
+
* @internal
|
|
277
|
+
*/
|
|
278
|
+
static async getExpandedDocument(documentOrExpanded) {
|
|
279
|
+
if (Is.array(documentOrExpanded)) {
|
|
280
|
+
Guards.array(JsonLdHelper.CLASS_NAME, "documentOrExpanded", documentOrExpanded);
|
|
281
|
+
return documentOrExpanded;
|
|
282
|
+
}
|
|
283
|
+
Guards.object(JsonLdHelper.CLASS_NAME, "documentOrExpanded", documentOrExpanded);
|
|
284
|
+
return JsonLdProcessor.expand(documentOrExpanded);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
//# sourceMappingURL=jsonLdHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonLdHelper.js","sourceRoot":"","sources":["../../../src/utils/jsonLdHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,EAAE,EAA2B,MAAM,gBAAgB,CAAC;AAClF,OAAO,EAAE,cAAc,EAAuB,MAAM,qBAAqB,CAAC;AAE1E,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAKvD;;GAEG;AACH,MAAM,OAAO,YAAY;IACxB;;;OAGG;IACI,MAAM,CAAU,UAAU,kBAA0B;IAE3D;;;;;;;;OAQG;IACI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAC3B,QAAW,EACX,kBAAwC,EACxC,OAGC;QAED,IAAI,EAAE,CAAC,KAAK,CAAoB,QAAQ,CAAC,EAAE,CAAC;YAC3C,2DAA2D;YAC3D,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;gBAC7B,MAAM,YAAY,CAAC,QAAQ,CAAC,IAAI,EAAE,kBAAkB,EAAE,OAAO,CAAC,CAAC;YAChE,CAAC;QACF,CAAC;aAAM,IAAI,EAAE,CAAC,KAAK,CAAoB,QAA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,CAAC;YACjC,CAAC;QACF,CAAC;QAED,OAAO,kBAAkB,CAAC;IAC3B,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,KAAK,CAAC,mBAAmB,CACvC,kBAAyD;QAEzD,IAAI,EAAE,CAAC,KAAK,CAAoB,kBAAkB,CAAC,EAAE,CAAC;YACrD,MAAM,CAAC,KAAK,CACX,YAAY,CAAC,UAAU,wBAEvB,kBAAkB,CAClB,CAAC;YAEF,OAAO,kBAAkB,CAAC;QAC3B,CAAC;QAED,MAAM,CAAC,MAAM,CACZ,YAAY,CAAC,UAAU,wBAEvB,kBAAkB,CAClB,CAAC;QAEF,OAAO,eAAe,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC;IACnD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ArrayHelper, Guards, Is, type IValidationFailure } from \"@twin.org/core\";\nimport { DataTypeHelper, type ValidationMode } from \"@twin.org/data-core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { JsonLdProcessor } from \"./jsonLdProcessor.js\";\nimport type { IJsonLdDocument } from \"../models/IJsonLdDocument.js\";\nimport type { IJsonLdNodeObject } from \"../models/IJsonLdNodeObject.js\";\nimport type { IJsonLdNodePrimitive } from \"../models/IJsonLdNodePrimitive.js\";\n\n/**\n * Class to help with JSON LD.\n */\nexport class JsonLdHelper {\n\t/**\n\t * The class name.\n\t * @internal\n\t */\n\tpublic static readonly CLASS_NAME = nameof<JsonLdHelper>();\n\n\t/**\n\t * Validate a JSON-LD document.\n\t * @param document The JSON-LD document to validate.\n\t * @param validationFailures The list of validation failures to add to.\n\t * @param options Optional options for validation.\n\t * @param options.failOnMissingType If true, will fail validation if the data type is missing, defaults to false.\n\t * @param options.validationMode The validation mode to use, defaults to either.\n\t * @returns True if the document was valid.\n\t */\n\tpublic static async validate<T extends IJsonLdDocument = IJsonLdDocument>(\n\t\tdocument: T,\n\t\tvalidationFailures: IValidationFailure[],\n\t\toptions?: {\n\t\t\tvalidationMode?: ValidationMode;\n\t\t\tfailOnMissingType?: boolean;\n\t\t}\n\t): Promise<boolean> {\n\t\tif (Is.array<IJsonLdNodeObject>(document)) {\n\t\t\t// If the document is an array of nodes, validate each node\n\t\t\tfor (const node of document) {\n\t\t\t\tawait JsonLdHelper.validate(node, validationFailures, options);\n\t\t\t}\n\t\t} else if (Is.array<IJsonLdNodeObject>(document[\"@graph\"])) {\n\t\t\t// If the graph is an array of nodes, validate each node\n\t\t\tfor (const node of document[\"@graph\"]) {\n\t\t\t\tawait JsonLdHelper.validate(node, validationFailures, options);\n\t\t\t}\n\t\t} else if (Is.object<IJsonLdNodeObject>(document)) {\n\t\t\t// Expand the document to ensure we have the full context for types\n\t\t\t// As the data types in the factories are always fully qualified\n\t\t\tconst expandedDocs = await JsonLdProcessor.expand(document);\n\t\t\tif (Is.arrayValue(expandedDocs)) {\n\t\t\t\tfor (const expandedDoc of expandedDocs) {\n\t\t\t\t\tconst expandedDataTypes = ArrayHelper.fromObjectOrArray(expandedDoc[\"@type\"]);\n\t\t\t\t\tif (Is.arrayValue(expandedDataTypes)) {\n\t\t\t\t\t\tfor (const expandedDataType of expandedDataTypes) {\n\t\t\t\t\t\t\tawait DataTypeHelper.validate(\n\t\t\t\t\t\t\t\t\"document\",\n\t\t\t\t\t\t\t\texpandedDataType,\n\t\t\t\t\t\t\t\tdocument,\n\t\t\t\t\t\t\t\tvalidationFailures,\n\t\t\t\t\t\t\t\toptions\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn validationFailures.length === 0;\n\t}\n\n\t/**\n\t * Expand an object to a JSON-LD node object.\n\t * @param object The object to expand.\n\t * @returns The expanded JSON-LD node object.\n\t */\n\tpublic static toNodeObject<T = unknown>(object: T): T & IJsonLdNodeObject {\n\t\tGuards.object<T>(JsonLdHelper.CLASS_NAME, nameof(object), object);\n\t\treturn object as T & IJsonLdNodeObject;\n\t}\n\n\t/**\n\t * Expand the JSON-LD document.\n\t * @param document The JSON-LD document to expand.\n\t * @returns The expanded JSON-LD document.\n\t */\n\tpublic static async expand(document: IJsonLdDocument): Promise<IJsonLdNodeObject[]> {\n\t\tGuards.object<IJsonLdDocument>(JsonLdHelper.CLASS_NAME, nameof(document), document);\n\t\treturn JsonLdProcessor.expand(document);\n\t}\n\n\t/**\n\t * Expand the JSON-LD document and check if it is of a specific type.\n\t * @param documentOrExpanded The JSON-LD document to check or already expanded document.\n\t * @param type The type to check for.\n\t * @returns True if the document is of the specified type.\n\t */\n\tpublic static async isType(\n\t\tdocumentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[],\n\t\ttype: string[]\n\t): Promise<boolean> {\n\t\tconst expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);\n\t\tGuards.arrayValue(JsonLdHelper.CLASS_NAME, nameof(type), type);\n\n\t\tif (Is.arrayValue(expanded)) {\n\t\t\tfor (const item of expanded) {\n\t\t\t\tconst types = ArrayHelper.fromObjectOrArray(item[\"@type\"]);\n\t\t\t\tif (Is.arrayValue(types)) {\n\t\t\t\t\t// All required types must be present in this item's @type array\n\t\t\t\t\tconst itemTypes = new Set(types);\n\t\t\t\t\tif (type.every(t => itemTypes.has(t))) {\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Get the types from the document.\n\t * @param documentOrExpanded The JSON-LD document to check or already expanded document.\n\t * @returns The type(s) extracted from the document.\n\t */\n\tpublic static async getType(\n\t\tdocumentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[]\n\t): Promise<string[]> {\n\t\tconst expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);\n\n\t\tconst types: Set<string> = new Set<string>();\n\t\tconst props = [\"@type\", \"type\"];\n\n\t\tfor (const expandedDoc of expanded) {\n\t\t\tfor (const prop of props) {\n\t\t\t\tconst expandedProps = ArrayHelper.fromObjectOrArray(expandedDoc[prop]);\n\t\t\t\tif (Is.arrayValue(expandedProps)) {\n\t\t\t\t\tfor (const expandedProp of expandedProps) {\n\t\t\t\t\t\tconst arr = ArrayHelper.fromObjectOrArray(expandedProp);\n\t\t\t\t\t\tfor (const arrValue of arr) {\n\t\t\t\t\t\t\tif (Is.stringValue(arrValue)) {\n\t\t\t\t\t\t\t\ttypes.add(arrValue);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn Array.from(types);\n\t}\n\n\t/**\n\t * Get the id from the document.\n\t * @param documentOrExpanded The JSON-LD document to get the id from or already expanded document.\n\t * @returns The id extracted from the document.\n\t */\n\tpublic static async getId(\n\t\tdocumentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[]\n\t): Promise<string | undefined> {\n\t\tconst expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);\n\n\t\tconst props = [\"@id\", \"id\"];\n\n\t\tfor (const expandedDoc of expanded) {\n\t\t\tfor (const prop of props) {\n\t\t\t\tconst expandedProps = ArrayHelper.fromObjectOrArray(expandedDoc[prop]);\n\t\t\t\tif (Is.arrayValue(expandedProps)) {\n\t\t\t\t\tfor (const expandedProp of expandedProps) {\n\t\t\t\t\t\tconst arr = ArrayHelper.fromObjectOrArray(expandedProp);\n\t\t\t\t\t\tfor (const arrValue of arr) {\n\t\t\t\t\t\t\tif (Is.stringValue(arrValue)) {\n\t\t\t\t\t\t\t\treturn arrValue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Get property values by a single full expanded property name.\n\t * @param documentOrExpanded The JSON-LD document to get the property from or already expanded document.\n\t * @param propertyFullName The full expanded property name.\n\t * @param language Optional filter values by their language property.\n\t * @returns Matching property values for the input property.\n\t */\n\tpublic static async getPropertyValue(\n\t\tdocumentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[],\n\t\tpropertyFullName: string,\n\t\tlanguage?: string\n\t): Promise<IJsonLdNodePrimitive[] | undefined> {\n\t\tconst results = await JsonLdHelper.getPropertyValues(\n\t\t\tdocumentOrExpanded,\n\t\t\t[propertyFullName],\n\t\t\tlanguage\n\t\t);\n\n\t\treturn results[0];\n\t}\n\n\t/**\n\t * Get property values by their full expanded property names.\n\t * @param documentOrExpanded The JSON-LD document to get the property from or already expanded document.\n\t * @param propertyFullNames The full expanded property names.\n\t * @param language Optional filter values by their language property.\n\t * @returns Matching property values for each input property, in the same index order.\n\t */\n\tpublic static async getPropertyValues(\n\t\tdocumentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[],\n\t\tpropertyFullNames: string[],\n\t\tlanguage?: string\n\t): Promise<(IJsonLdNodePrimitive[] | undefined)[]> {\n\t\tconst expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);\n\n\t\tGuards.arrayValue(JsonLdHelper.CLASS_NAME, nameof(propertyFullNames), propertyFullNames);\n\t\tif (Is.stringValue(language)) {\n\t\t\tGuards.stringValue(JsonLdHelper.CLASS_NAME, nameof(language), language);\n\t\t}\n\t\tconst languageLower = Is.stringValue(language) ? language.toLowerCase() : undefined;\n\t\tconst result: (IJsonLdNodePrimitive[] | undefined)[] = [];\n\n\t\tfor (const propertyFullName of propertyFullNames) {\n\t\t\tGuards.stringValue(JsonLdHelper.CLASS_NAME, nameof(propertyFullName), propertyFullName);\n\t\t\tconst values: IJsonLdNodePrimitive[] = [];\n\n\t\t\tfor (const expandedDoc of expanded) {\n\t\t\t\tconst propValue = expandedDoc[propertyFullName];\n\t\t\t\tif (!Is.empty(propValue)) {\n\t\t\t\t\tconst expandedProps = ArrayHelper.fromObjectOrArray(propValue);\n\t\t\t\t\tif (Is.arrayValue(expandedProps)) {\n\t\t\t\t\t\tfor (const expandedProp of expandedProps) {\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tIs.object<{ \"@value\"?: IJsonLdNodePrimitive; \"@language\"?: string }>(expandedProp)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tif (!Is.empty(expandedProp[\"@value\"])) {\n\t\t\t\t\t\t\t\t\tlet shouldAdd = true;\n\t\t\t\t\t\t\t\t\tif (Is.stringValue(languageLower)) {\n\t\t\t\t\t\t\t\t\t\tconst itemLanguage = Is.stringValue(expandedProp[\"@language\"])\n\t\t\t\t\t\t\t\t\t\t\t? expandedProp[\"@language\"].toLowerCase()\n\t\t\t\t\t\t\t\t\t\t\t: undefined;\n\t\t\t\t\t\t\t\t\t\tshouldAdd = itemLanguage === languageLower;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif (shouldAdd) {\n\t\t\t\t\t\t\t\t\t\tvalues.push(expandedProp[\"@value\"]);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t} else if (!Is.stringValue(languageLower)) {\n\t\t\t\t\t\t\t\t\tvalues.push(expandedProp);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else if (\n\t\t\t\t\t\t\t\tIs.stringValue(expandedProp) ||\n\t\t\t\t\t\t\t\tIs.boolean(expandedProp) ||\n\t\t\t\t\t\t\t\tIs.number(expandedProp)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tif (!Is.stringValue(languageLower)) {\n\t\t\t\t\t\t\t\t\tvalues.push(expandedProp);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tresult.push(values.length > 0 ? values : undefined);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Prefix all properties in the document with the provided prefix, except for JSON-LD properties.\n\t * This is useful for ensuring that all properties are fully qualified with a namespace.\n\t * For example, if the prefix is \"ex\" and the document has a property \"name\", it will be transformed to \"ex:name\".\n\t * @param nodeObject The JSON-LD node object to prefix properties on.\n\t * @param prefix The prefix to add to the properties.\n\t * @param properties Optional list of properties to prefix. If not provided, all properties except for JSON-LD properties.\n\t * @returns A new JSON-LD node object with the properties prefixed.\n\t */\n\tpublic static prefixProperties<T extends IJsonLdNodeObject>(\n\t\tnodeObject: T,\n\t\tprefix: string,\n\t\tproperties?: string[]\n\t): IJsonLdNodeObject {\n\t\tGuards.object<IJsonLdNodeObject>(JsonLdHelper.CLASS_NAME, nameof(nodeObject), nodeObject);\n\t\tGuards.stringValue(JsonLdHelper.CLASS_NAME, nameof(prefix), prefix);\n\t\tconst normalizedPrefix = prefix.endsWith(\":\") ? prefix.slice(0, -1) : prefix;\n\t\tconst hasNoPropertiesFilter = !Is.arrayValue(properties);\n\n\t\tconst prefixedNodeObject: IJsonLdNodeObject = {};\n\n\t\tfor (const key in nodeObject) {\n\t\t\tconst value = nodeObject[key];\n\t\t\tif (\n\t\t\t\tIs.stringValue(key) &&\n\t\t\t\t!key.startsWith(\"@\") &&\n\t\t\t\t(hasNoPropertiesFilter || properties.includes(key))\n\t\t\t) {\n\t\t\t\tprefixedNodeObject[`${normalizedPrefix}:${key}`] = value;\n\t\t\t} else {\n\t\t\t\tprefixedNodeObject[key] = value;\n\t\t\t}\n\t\t}\n\n\t\treturn prefixedNodeObject;\n\t}\n\n\t/**\n\t * Strip a prefix from properties in the document, except for JSON-LD properties.\n\t * This is useful for converting fully qualified namespaced properties back to local names.\n\t * For example, if the prefix is \"ex\" and the document has a property \"ex:name\", it will be transformed to \"name\".\n\t * @param nodeObject The JSON-LD node object to strip prefixed properties from.\n\t * @param prefix The prefix to remove from the properties.\n\t * @param properties Optional list of unprefixed properties to strip. If not provided, all matching prefixed properties.\n\t * @returns A new JSON-LD node object with the prefix stripped from matching properties.\n\t */\n\tpublic static stripPrefixProperties<T extends IJsonLdNodeObject>(\n\t\tnodeObject: T,\n\t\tprefix: string,\n\t\tproperties?: string[]\n\t): IJsonLdNodeObject {\n\t\tGuards.object<IJsonLdNodeObject>(JsonLdHelper.CLASS_NAME, nameof(nodeObject), nodeObject);\n\t\tGuards.stringValue(JsonLdHelper.CLASS_NAME, nameof(prefix), prefix);\n\t\tconst normalizedPrefix = prefix.endsWith(\":\") ? prefix.slice(0, -1) : prefix;\n\t\tconst hasNoPropertiesFilter = !Is.arrayValue(properties);\n\n\t\tconst strippedNodeObject: IJsonLdNodeObject = {};\n\n\t\tfor (const key in nodeObject) {\n\t\t\tconst value = nodeObject[key];\n\t\t\tif (Is.stringValue(key) && !key.startsWith(\"@\") && key.startsWith(`${normalizedPrefix}:`)) {\n\t\t\t\tconst strippedKey = key.slice(normalizedPrefix.length + 1);\n\t\t\t\tif (hasNoPropertiesFilter || properties.includes(strippedKey)) {\n\t\t\t\t\tstrippedNodeObject[strippedKey] = value;\n\t\t\t\t} else {\n\t\t\t\t\tstrippedNodeObject[key] = value;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tstrippedNodeObject[key] = value;\n\t\t\t}\n\t\t}\n\n\t\treturn strippedNodeObject;\n\t}\n\n\t/**\n\t * Get an expanded JSON-LD document from either compact or expanded input.\n\t * @param documentOrExpanded The JSON-LD document or expanded document.\n\t * @returns The expanded JSON-LD document.\n\t * @internal\n\t */\n\tprivate static async getExpandedDocument(\n\t\tdocumentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[]\n\t): Promise<IJsonLdNodeObject[]> {\n\t\tif (Is.array<IJsonLdNodeObject>(documentOrExpanded)) {\n\t\t\tGuards.array<IJsonLdNodeObject>(\n\t\t\t\tJsonLdHelper.CLASS_NAME,\n\t\t\t\tnameof(documentOrExpanded),\n\t\t\t\tdocumentOrExpanded\n\t\t\t);\n\n\t\t\treturn documentOrExpanded;\n\t\t}\n\n\t\tGuards.object<IJsonLdDocument>(\n\t\t\tJsonLdHelper.CLASS_NAME,\n\t\t\tnameof(documentOrExpanded),\n\t\t\tdocumentOrExpanded\n\t\t);\n\n\t\treturn JsonLdProcessor.expand(documentOrExpanded);\n\t}\n}\n"]}
|