@twin.org/data-json-ld 0.0.3-next.7 → 0.0.3-next.9
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.
|
@@ -60,16 +60,24 @@ export class JsonLdHelper {
|
|
|
60
60
|
Guards.object(JsonLdHelper.CLASS_NAME, "object", object);
|
|
61
61
|
return object;
|
|
62
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
|
+
}
|
|
63
72
|
/**
|
|
64
73
|
* Expand the JSON-LD document and check if it is of a specific type.
|
|
65
|
-
* @param
|
|
74
|
+
* @param documentOrExpanded The JSON-LD document to check or already expanded document.
|
|
66
75
|
* @param type The type to check for.
|
|
67
76
|
* @returns True if the document is of the specified type.
|
|
68
77
|
*/
|
|
69
|
-
static async isType(
|
|
70
|
-
|
|
78
|
+
static async isType(documentOrExpanded, type) {
|
|
79
|
+
const expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);
|
|
71
80
|
Guards.arrayValue(JsonLdHelper.CLASS_NAME, "type", type);
|
|
72
|
-
const expanded = await JsonLdProcessor.expand(document);
|
|
73
81
|
if (Is.arrayValue(expanded)) {
|
|
74
82
|
for (const item of expanded) {
|
|
75
83
|
const types = ArrayHelper.fromObjectOrArray(item["@type"]);
|
|
@@ -86,15 +94,14 @@ export class JsonLdHelper {
|
|
|
86
94
|
}
|
|
87
95
|
/**
|
|
88
96
|
* Get the types from the document.
|
|
89
|
-
* @param
|
|
97
|
+
* @param documentOrExpanded The JSON-LD document to check or already expanded document.
|
|
90
98
|
* @returns The type(s) extracted from the document.
|
|
91
99
|
*/
|
|
92
|
-
static async getType(
|
|
93
|
-
|
|
94
|
-
const expandedDocs = await JsonLdProcessor.expand(document);
|
|
100
|
+
static async getType(documentOrExpanded) {
|
|
101
|
+
const expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);
|
|
95
102
|
const types = new Set();
|
|
96
103
|
const props = ["@type", "type"];
|
|
97
|
-
for (const expandedDoc of
|
|
104
|
+
for (const expandedDoc of expanded) {
|
|
98
105
|
for (const prop of props) {
|
|
99
106
|
const expandedProps = ArrayHelper.fromObjectOrArray(expandedDoc[prop]);
|
|
100
107
|
if (Is.arrayValue(expandedProps)) {
|
|
@@ -113,14 +120,13 @@ export class JsonLdHelper {
|
|
|
113
120
|
}
|
|
114
121
|
/**
|
|
115
122
|
* Get the id from the document.
|
|
116
|
-
* @param
|
|
123
|
+
* @param documentOrExpanded The JSON-LD document to get the id from or already expanded document.
|
|
117
124
|
* @returns The id extracted from the document.
|
|
118
125
|
*/
|
|
119
|
-
static async getId(
|
|
120
|
-
|
|
121
|
-
const expandedDocs = await JsonLdProcessor.expand(document);
|
|
126
|
+
static async getId(documentOrExpanded) {
|
|
127
|
+
const expanded = await JsonLdHelper.getExpandedDocument(documentOrExpanded);
|
|
122
128
|
const props = ["@id", "id"];
|
|
123
|
-
for (const expandedDoc of
|
|
129
|
+
for (const expandedDoc of expanded) {
|
|
124
130
|
for (const prop of props) {
|
|
125
131
|
const expandedProps = ArrayHelper.fromObjectOrArray(expandedDoc[prop]);
|
|
126
132
|
if (Is.arrayValue(expandedProps)) {
|
|
@@ -136,5 +142,86 @@ export class JsonLdHelper {
|
|
|
136
142
|
}
|
|
137
143
|
}
|
|
138
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
|
+
* Get an expanded JSON-LD document from either compact or expanded input.
|
|
214
|
+
* @param documentOrExpanded The JSON-LD document or expanded document.
|
|
215
|
+
* @returns The expanded JSON-LD document.
|
|
216
|
+
* @internal
|
|
217
|
+
*/
|
|
218
|
+
static async getExpandedDocument(documentOrExpanded) {
|
|
219
|
+
if (Is.array(documentOrExpanded)) {
|
|
220
|
+
Guards.array(JsonLdHelper.CLASS_NAME, "documentOrExpanded", documentOrExpanded);
|
|
221
|
+
return documentOrExpanded;
|
|
222
|
+
}
|
|
223
|
+
Guards.object(JsonLdHelper.CLASS_NAME, "documentOrExpanded", documentOrExpanded);
|
|
224
|
+
return JsonLdProcessor.expand(documentOrExpanded);
|
|
225
|
+
}
|
|
139
226
|
}
|
|
140
227
|
//# sourceMappingURL=jsonLdHelper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonLdHelper.js","sourceRoot":"","sources":["../../../src/utils/jsonLdHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,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,QAAQ,CAAC,CAAC;QAEpF,MAAM,YAAY,GAAG,MAAM,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAE5D,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAE5B,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE,CAAC;YACxC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBAC1B,MAAM,aAAa,GAAG,WAAW,CAAC,iBAAiB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;gBACvE,IAAI,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC,EAAE,CAAC;oBAClC,KAAK,MAAM,YAAY,IAAI,aAAa,EAAE,CAAC;wBAC1C,MAAM,GAAG,GAAG,WAAW,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAC;wBACxD,KAAK,MAAM,QAAQ,IAAI,GAAG,EAAE,CAAC;4BAC5B,IAAI,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;gCAC9B,OAAO,QAAQ,CAAC;4BACjB,CAAC;wBACF,CAAC;oBACF,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;IACF,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\";\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(object: unknown): IJsonLdNodeObject {\n\t\tGuards.object<unknown>(JsonLdHelper.CLASS_NAME, nameof(object), object);\n\t\treturn object as IJsonLdNodeObject;\n\t}\n\n\t/**\n\t * Expand the JSON-LD document and check if it is of a specific type.\n\t * @param document The JSON-LD document to check.\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(document: IJsonLdDocument, type: string[]): Promise<boolean> {\n\t\tGuards.object<IJsonLdDocument>(JsonLdHelper.CLASS_NAME, nameof(document), document);\n\t\tGuards.arrayValue(JsonLdHelper.CLASS_NAME, nameof(type), type);\n\n\t\tconst expanded = await JsonLdProcessor.expand(document);\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 document The JSON-LD document to check.\n\t * @returns The type(s) extracted from the document.\n\t */\n\tpublic static async getType(document: IJsonLdDocument): Promise<string[]> {\n\t\tGuards.object<IJsonLdDocument>(JsonLdHelper.CLASS_NAME, nameof(document), document);\n\n\t\tconst expandedDocs = await JsonLdProcessor.expand(document);\n\n\t\tconst types: Set<string> = new Set<string>();\n\t\tconst props = [\"@type\", \"type\"];\n\n\t\tfor (const expandedDoc of expandedDocs) {\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 document The JSON-LD document to get the id from.\n\t * @returns The id extracted from the document.\n\t */\n\tpublic static async getId(document: IJsonLdDocument): Promise<string | undefined> {\n\t\tGuards.object<IJsonLdDocument>(JsonLdHelper.CLASS_NAME, nameof(document), document);\n\n\t\tconst expandedDocs = await JsonLdProcessor.expand(document);\n\n\t\tconst props = [\"@id\", \"id\"];\n\n\t\tfor (const expandedDoc of expandedDocs) {\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"]}
|
|
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,EAA2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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 * 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"]}
|
|
@@ -2,6 +2,7 @@ import { type IValidationFailure } from "@twin.org/core";
|
|
|
2
2
|
import { type ValidationMode } from "@twin.org/data-core";
|
|
3
3
|
import type { IJsonLdDocument } from "../models/IJsonLdDocument.js";
|
|
4
4
|
import type { IJsonLdNodeObject } from "../models/IJsonLdNodeObject.js";
|
|
5
|
+
import type { IJsonLdNodePrimitive } from "../models/IJsonLdNodePrimitive.js";
|
|
5
6
|
/**
|
|
6
7
|
* Class to help with JSON LD.
|
|
7
8
|
*/
|
|
@@ -24,24 +25,46 @@ export declare class JsonLdHelper {
|
|
|
24
25
|
* @param object The object to expand.
|
|
25
26
|
* @returns The expanded JSON-LD node object.
|
|
26
27
|
*/
|
|
27
|
-
static toNodeObject(object:
|
|
28
|
+
static toNodeObject<T = unknown>(object: T): T & IJsonLdNodeObject;
|
|
29
|
+
/**
|
|
30
|
+
* Expand the JSON-LD document.
|
|
31
|
+
* @param document The JSON-LD document to expand.
|
|
32
|
+
* @returns The expanded JSON-LD document.
|
|
33
|
+
*/
|
|
34
|
+
static expand(document: IJsonLdDocument): Promise<IJsonLdNodeObject[]>;
|
|
28
35
|
/**
|
|
29
36
|
* Expand the JSON-LD document and check if it is of a specific type.
|
|
30
|
-
* @param
|
|
37
|
+
* @param documentOrExpanded The JSON-LD document to check or already expanded document.
|
|
31
38
|
* @param type The type to check for.
|
|
32
39
|
* @returns True if the document is of the specified type.
|
|
33
40
|
*/
|
|
34
|
-
static isType(
|
|
41
|
+
static isType(documentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[], type: string[]): Promise<boolean>;
|
|
35
42
|
/**
|
|
36
43
|
* Get the types from the document.
|
|
37
|
-
* @param
|
|
44
|
+
* @param documentOrExpanded The JSON-LD document to check or already expanded document.
|
|
38
45
|
* @returns The type(s) extracted from the document.
|
|
39
46
|
*/
|
|
40
|
-
static getType(
|
|
47
|
+
static getType(documentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[]): Promise<string[]>;
|
|
41
48
|
/**
|
|
42
49
|
* Get the id from the document.
|
|
43
|
-
* @param
|
|
50
|
+
* @param documentOrExpanded The JSON-LD document to get the id from or already expanded document.
|
|
44
51
|
* @returns The id extracted from the document.
|
|
45
52
|
*/
|
|
46
|
-
static getId(
|
|
53
|
+
static getId(documentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[]): Promise<string | undefined>;
|
|
54
|
+
/**
|
|
55
|
+
* Get property values by a single full expanded property name.
|
|
56
|
+
* @param documentOrExpanded The JSON-LD document to get the property from or already expanded document.
|
|
57
|
+
* @param propertyFullName The full expanded property name.
|
|
58
|
+
* @param language Optional filter values by their language property.
|
|
59
|
+
* @returns Matching property values for the input property.
|
|
60
|
+
*/
|
|
61
|
+
static getPropertyValue(documentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[], propertyFullName: string, language?: string): Promise<IJsonLdNodePrimitive[] | undefined>;
|
|
62
|
+
/**
|
|
63
|
+
* Get property values by their full expanded property names.
|
|
64
|
+
* @param documentOrExpanded The JSON-LD document to get the property from or already expanded document.
|
|
65
|
+
* @param propertyFullNames The full expanded property names.
|
|
66
|
+
* @param language Optional filter values by their language property.
|
|
67
|
+
* @returns Matching property values for each input property, in the same index order.
|
|
68
|
+
*/
|
|
69
|
+
static getPropertyValues(documentOrExpanded: IJsonLdDocument | IJsonLdNodeObject[], propertyFullNames: string[], language?: string): Promise<(IJsonLdNodePrimitive[] | undefined)[]>;
|
|
47
70
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @twin.org/data-json-ld - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.3-next.9](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.8...data-json-ld-v0.0.3-next.9) (2026-02-23)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* additional JsonLdHelper methods ([#44](https://github.com/twinfoundation/data/issues/44)) ([ebe2cf5](https://github.com/twinfoundation/data/commit/ebe2cf50d1a7fbe0474f0a556d49f43eb7767a2f))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/data-core bumped from 0.0.3-next.8 to 0.0.3-next.9
|
|
16
|
+
|
|
17
|
+
## [0.0.3-next.8](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.7...data-json-ld-v0.0.3-next.8) (2026-02-02)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Miscellaneous Chores
|
|
21
|
+
|
|
22
|
+
* **data-json-ld:** Synchronize repo versions
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Dependencies
|
|
26
|
+
|
|
27
|
+
* The following workspace dependencies were updated
|
|
28
|
+
* dependencies
|
|
29
|
+
* @twin.org/data-core bumped from 0.0.3-next.7 to 0.0.3-next.8
|
|
30
|
+
|
|
3
31
|
## [0.0.3-next.7](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.6...data-json-ld-v0.0.3-next.7) (2026-01-21)
|
|
4
32
|
|
|
5
33
|
|
|
@@ -66,39 +66,67 @@ True if the document was valid.
|
|
|
66
66
|
|
|
67
67
|
### toNodeObject()
|
|
68
68
|
|
|
69
|
-
> `static` **toNodeObject
|
|
69
|
+
> `static` **toNodeObject**\<`T`\>(`object`): `T` & [`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)
|
|
70
70
|
|
|
71
71
|
Expand an object to a JSON-LD node object.
|
|
72
72
|
|
|
73
|
+
#### Type Parameters
|
|
74
|
+
|
|
75
|
+
##### T
|
|
76
|
+
|
|
77
|
+
`T` = `unknown`
|
|
78
|
+
|
|
73
79
|
#### Parameters
|
|
74
80
|
|
|
75
81
|
##### object
|
|
76
82
|
|
|
77
|
-
`
|
|
83
|
+
`T`
|
|
78
84
|
|
|
79
85
|
The object to expand.
|
|
80
86
|
|
|
81
87
|
#### Returns
|
|
82
88
|
|
|
83
|
-
[`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)
|
|
89
|
+
`T` & [`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)
|
|
84
90
|
|
|
85
91
|
The expanded JSON-LD node object.
|
|
86
92
|
|
|
87
93
|
***
|
|
88
94
|
|
|
95
|
+
### expand()
|
|
96
|
+
|
|
97
|
+
> `static` **expand**(`document`): `Promise`\<[`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)[]\>
|
|
98
|
+
|
|
99
|
+
Expand the JSON-LD document.
|
|
100
|
+
|
|
101
|
+
#### Parameters
|
|
102
|
+
|
|
103
|
+
##### document
|
|
104
|
+
|
|
105
|
+
[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
|
|
106
|
+
|
|
107
|
+
The JSON-LD document to expand.
|
|
108
|
+
|
|
109
|
+
#### Returns
|
|
110
|
+
|
|
111
|
+
`Promise`\<[`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)[]\>
|
|
112
|
+
|
|
113
|
+
The expanded JSON-LD document.
|
|
114
|
+
|
|
115
|
+
***
|
|
116
|
+
|
|
89
117
|
### isType()
|
|
90
118
|
|
|
91
|
-
> `static` **isType**(`
|
|
119
|
+
> `static` **isType**(`documentOrExpanded`, `type`): `Promise`\<`boolean`\>
|
|
92
120
|
|
|
93
121
|
Expand the JSON-LD document and check if it is of a specific type.
|
|
94
122
|
|
|
95
123
|
#### Parameters
|
|
96
124
|
|
|
97
|
-
#####
|
|
125
|
+
##### documentOrExpanded
|
|
98
126
|
|
|
99
127
|
[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
|
|
100
128
|
|
|
101
|
-
The JSON-LD document to check.
|
|
129
|
+
The JSON-LD document to check or already expanded document.
|
|
102
130
|
|
|
103
131
|
##### type
|
|
104
132
|
|
|
@@ -116,17 +144,17 @@ True if the document is of the specified type.
|
|
|
116
144
|
|
|
117
145
|
### getType()
|
|
118
146
|
|
|
119
|
-
> `static` **getType**(`
|
|
147
|
+
> `static` **getType**(`documentOrExpanded`): `Promise`\<`string`[]\>
|
|
120
148
|
|
|
121
149
|
Get the types from the document.
|
|
122
150
|
|
|
123
151
|
#### Parameters
|
|
124
152
|
|
|
125
|
-
#####
|
|
153
|
+
##### documentOrExpanded
|
|
126
154
|
|
|
127
155
|
[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
|
|
128
156
|
|
|
129
|
-
The JSON-LD document to check.
|
|
157
|
+
The JSON-LD document to check or already expanded document.
|
|
130
158
|
|
|
131
159
|
#### Returns
|
|
132
160
|
|
|
@@ -138,20 +166,88 @@ The type(s) extracted from the document.
|
|
|
138
166
|
|
|
139
167
|
### getId()
|
|
140
168
|
|
|
141
|
-
> `static` **getId**(`
|
|
169
|
+
> `static` **getId**(`documentOrExpanded`): `Promise`\<`string` \| `undefined`\>
|
|
142
170
|
|
|
143
171
|
Get the id from the document.
|
|
144
172
|
|
|
145
173
|
#### Parameters
|
|
146
174
|
|
|
147
|
-
#####
|
|
175
|
+
##### documentOrExpanded
|
|
148
176
|
|
|
149
177
|
[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
|
|
150
178
|
|
|
151
|
-
The JSON-LD document to get the id from.
|
|
179
|
+
The JSON-LD document to get the id from or already expanded document.
|
|
152
180
|
|
|
153
181
|
#### Returns
|
|
154
182
|
|
|
155
183
|
`Promise`\<`string` \| `undefined`\>
|
|
156
184
|
|
|
157
185
|
The id extracted from the document.
|
|
186
|
+
|
|
187
|
+
***
|
|
188
|
+
|
|
189
|
+
### getPropertyValue()
|
|
190
|
+
|
|
191
|
+
> `static` **getPropertyValue**(`documentOrExpanded`, `propertyFullName`, `language?`): `Promise`\<[`IJsonLdNodePrimitive`](../type-aliases/IJsonLdNodePrimitive.md)[] \| `undefined`\>
|
|
192
|
+
|
|
193
|
+
Get property values by a single full expanded property name.
|
|
194
|
+
|
|
195
|
+
#### Parameters
|
|
196
|
+
|
|
197
|
+
##### documentOrExpanded
|
|
198
|
+
|
|
199
|
+
[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
|
|
200
|
+
|
|
201
|
+
The JSON-LD document to get the property from or already expanded document.
|
|
202
|
+
|
|
203
|
+
##### propertyFullName
|
|
204
|
+
|
|
205
|
+
`string`
|
|
206
|
+
|
|
207
|
+
The full expanded property name.
|
|
208
|
+
|
|
209
|
+
##### language?
|
|
210
|
+
|
|
211
|
+
`string`
|
|
212
|
+
|
|
213
|
+
Optional filter values by their language property.
|
|
214
|
+
|
|
215
|
+
#### Returns
|
|
216
|
+
|
|
217
|
+
`Promise`\<[`IJsonLdNodePrimitive`](../type-aliases/IJsonLdNodePrimitive.md)[] \| `undefined`\>
|
|
218
|
+
|
|
219
|
+
Matching property values for the input property.
|
|
220
|
+
|
|
221
|
+
***
|
|
222
|
+
|
|
223
|
+
### getPropertyValues()
|
|
224
|
+
|
|
225
|
+
> `static` **getPropertyValues**(`documentOrExpanded`, `propertyFullNames`, `language?`): `Promise`\<([`IJsonLdNodePrimitive`](../type-aliases/IJsonLdNodePrimitive.md)[] \| `undefined`)[]\>
|
|
226
|
+
|
|
227
|
+
Get property values by their full expanded property names.
|
|
228
|
+
|
|
229
|
+
#### Parameters
|
|
230
|
+
|
|
231
|
+
##### documentOrExpanded
|
|
232
|
+
|
|
233
|
+
[`IJsonLdDocument`](../type-aliases/IJsonLdDocument.md)
|
|
234
|
+
|
|
235
|
+
The JSON-LD document to get the property from or already expanded document.
|
|
236
|
+
|
|
237
|
+
##### propertyFullNames
|
|
238
|
+
|
|
239
|
+
`string`[]
|
|
240
|
+
|
|
241
|
+
The full expanded property names.
|
|
242
|
+
|
|
243
|
+
##### language?
|
|
244
|
+
|
|
245
|
+
`string`
|
|
246
|
+
|
|
247
|
+
Optional filter values by their language property.
|
|
248
|
+
|
|
249
|
+
#### Returns
|
|
250
|
+
|
|
251
|
+
`Promise`\<([`IJsonLdNodePrimitive`](../type-aliases/IJsonLdNodePrimitive.md)[] \| `undefined`)[]\>
|
|
252
|
+
|
|
253
|
+
Matching property values for each input property, in the same index order.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/data-json-ld",
|
|
3
|
-
"version": "0.0.3-next.
|
|
3
|
+
"version": "0.0.3-next.9",
|
|
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.3-next.
|
|
18
|
+
"@twin.org/data-core": "0.0.3-next.9",
|
|
19
19
|
"@twin.org/entity": "next",
|
|
20
20
|
"@twin.org/nameof": "next",
|
|
21
21
|
"@twin.org/web": "next",
|