@twin.org/data-json-ld 0.0.1-next.3 → 0.0.1-next.5
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/cjs/index.cjs +132 -111
- package/dist/esm/index.mjs +133 -112
- package/dist/types/models/IJsonLdDocument.d.ts +9 -1
- package/dist/types/utils/jsonLdProcessor.d.ts +27 -3
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/JsonLdProcessor.md +82 -2
- package/docs/reference/index.md +2 -0
- package/docs/reference/interfaces/IJsonLdGraphObject.md +1 -1
- package/docs/reference/interfaces/IJsonLdNodeObject.md +1 -1
- package/docs/reference/type-aliases/IJsonLdContextDefinitionElement.md +5 -0
- package/docs/reference/type-aliases/IJsonLdContextDefinitionRoot.md +5 -0
- package/docs/reference/type-aliases/IJsonLdKeyword.md +1 -1
- package/locales/en.json +3 -2
- package/package.json +3 -33
package/dist/cjs/index.cjs
CHANGED
|
@@ -374,33 +374,7 @@ var anyOf$7 = [
|
|
|
374
374
|
type: "object",
|
|
375
375
|
properties: {
|
|
376
376
|
"@context": {
|
|
377
|
-
|
|
378
|
-
{
|
|
379
|
-
type: "null"
|
|
380
|
-
},
|
|
381
|
-
{
|
|
382
|
-
type: "string"
|
|
383
|
-
},
|
|
384
|
-
{
|
|
385
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
386
|
-
},
|
|
387
|
-
{
|
|
388
|
-
type: "array",
|
|
389
|
-
items: {
|
|
390
|
-
anyOf: [
|
|
391
|
-
{
|
|
392
|
-
type: "null"
|
|
393
|
-
},
|
|
394
|
-
{
|
|
395
|
-
type: "string"
|
|
396
|
-
},
|
|
397
|
-
{
|
|
398
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
399
|
-
}
|
|
400
|
-
]
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
]
|
|
377
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
404
378
|
},
|
|
405
379
|
"@graph": {
|
|
406
380
|
anyOf: [
|
|
@@ -562,33 +536,7 @@ var properties$4 = {
|
|
|
562
536
|
]
|
|
563
537
|
},
|
|
564
538
|
"@context": {
|
|
565
|
-
|
|
566
|
-
{
|
|
567
|
-
type: "null"
|
|
568
|
-
},
|
|
569
|
-
{
|
|
570
|
-
type: "string"
|
|
571
|
-
},
|
|
572
|
-
{
|
|
573
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
574
|
-
},
|
|
575
|
-
{
|
|
576
|
-
type: "array",
|
|
577
|
-
items: {
|
|
578
|
-
anyOf: [
|
|
579
|
-
{
|
|
580
|
-
type: "null"
|
|
581
|
-
},
|
|
582
|
-
{
|
|
583
|
-
type: "string"
|
|
584
|
-
},
|
|
585
|
-
{
|
|
586
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
587
|
-
}
|
|
588
|
-
]
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
]
|
|
539
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
592
540
|
}
|
|
593
541
|
};
|
|
594
542
|
var required$3 = [
|
|
@@ -784,33 +732,7 @@ var properties$3 = {
|
|
|
784
732
|
]
|
|
785
733
|
},
|
|
786
734
|
"@context": {
|
|
787
|
-
|
|
788
|
-
{
|
|
789
|
-
type: "null"
|
|
790
|
-
},
|
|
791
|
-
{
|
|
792
|
-
type: "string"
|
|
793
|
-
},
|
|
794
|
-
{
|
|
795
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
796
|
-
},
|
|
797
|
-
{
|
|
798
|
-
type: "array",
|
|
799
|
-
items: {
|
|
800
|
-
anyOf: [
|
|
801
|
-
{
|
|
802
|
-
type: "null"
|
|
803
|
-
},
|
|
804
|
-
{
|
|
805
|
-
type: "string"
|
|
806
|
-
},
|
|
807
|
-
{
|
|
808
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
809
|
-
}
|
|
810
|
-
]
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
]
|
|
735
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
814
736
|
},
|
|
815
737
|
"@direction": {
|
|
816
738
|
type: [
|
|
@@ -1062,33 +984,7 @@ var JsonLdListOrSetItemSchema = {
|
|
|
1062
984
|
var type$2 = "object";
|
|
1063
985
|
var properties$1 = {
|
|
1064
986
|
"@context": {
|
|
1065
|
-
|
|
1066
|
-
{
|
|
1067
|
-
type: "null"
|
|
1068
|
-
},
|
|
1069
|
-
{
|
|
1070
|
-
type: "string"
|
|
1071
|
-
},
|
|
1072
|
-
{
|
|
1073
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1074
|
-
},
|
|
1075
|
-
{
|
|
1076
|
-
type: "array",
|
|
1077
|
-
items: {
|
|
1078
|
-
anyOf: [
|
|
1079
|
-
{
|
|
1080
|
-
type: "null"
|
|
1081
|
-
},
|
|
1082
|
-
{
|
|
1083
|
-
type: "string"
|
|
1084
|
-
},
|
|
1085
|
-
{
|
|
1086
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1087
|
-
}
|
|
1088
|
-
]
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
]
|
|
987
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
1092
988
|
},
|
|
1093
989
|
"@id": {
|
|
1094
990
|
anyOf: [
|
|
@@ -1531,16 +1427,39 @@ class JsonLdProcessor {
|
|
|
1531
1427
|
/**
|
|
1532
1428
|
* Compact a document according to a particular context.
|
|
1533
1429
|
* @param document The JSON-LD document to compact.
|
|
1534
|
-
* @param context The context to compact the document to.
|
|
1430
|
+
* @param context The context to compact the document to, if not provided will try and gather from the object.
|
|
1535
1431
|
* @returns The compacted JSON-LD document.
|
|
1536
1432
|
*/
|
|
1537
1433
|
static async compact(document, context) {
|
|
1538
1434
|
try {
|
|
1539
|
-
|
|
1435
|
+
// There is a cast here because the jsonld types are not correct.
|
|
1436
|
+
// A context definition can be an array or an object, but the types only allow an object.
|
|
1437
|
+
if (core.Is.empty(context)) {
|
|
1438
|
+
context = {};
|
|
1439
|
+
if (core.Is.array(document)) {
|
|
1440
|
+
for (const node of document) {
|
|
1441
|
+
context = JsonLdProcessor.gatherContexts(node, context);
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
else if (core.Is.array(document["@graph"])) {
|
|
1445
|
+
for (const node of document["@graph"]) {
|
|
1446
|
+
context = JsonLdProcessor.gatherContexts(node, context);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
else if (core.Is.object(document)) {
|
|
1450
|
+
context = JsonLdProcessor.gatherContexts(document, context);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
const compacted = await jsonLd.compact(document, context, {
|
|
1540
1454
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1541
1455
|
});
|
|
1456
|
+
return compacted;
|
|
1542
1457
|
}
|
|
1543
1458
|
catch (err) {
|
|
1459
|
+
if (core.Is.object(err) &&
|
|
1460
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1461
|
+
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1462
|
+
}
|
|
1544
1463
|
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "compact", undefined, err);
|
|
1545
1464
|
}
|
|
1546
1465
|
}
|
|
@@ -1551,11 +1470,16 @@ class JsonLdProcessor {
|
|
|
1551
1470
|
*/
|
|
1552
1471
|
static async expand(compacted) {
|
|
1553
1472
|
try {
|
|
1554
|
-
|
|
1473
|
+
const expanded = await jsonLd.expand(compacted, {
|
|
1555
1474
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1556
1475
|
});
|
|
1476
|
+
return expanded;
|
|
1557
1477
|
}
|
|
1558
1478
|
catch (err) {
|
|
1479
|
+
if (core.Is.object(err) &&
|
|
1480
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1481
|
+
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1482
|
+
}
|
|
1559
1483
|
throw new core.GeneralError(JsonLdProcessor._CLASS_NAME, "expand", undefined, err);
|
|
1560
1484
|
}
|
|
1561
1485
|
}
|
|
@@ -1569,6 +1493,103 @@ class JsonLdProcessor {
|
|
|
1569
1493
|
this._redirects.push({ from, to });
|
|
1570
1494
|
}
|
|
1571
1495
|
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Extract a property from the JSON-LD.
|
|
1498
|
+
* @param nodeObject The JSON-LD node object to extract from.
|
|
1499
|
+
* @param propertyNames The possible names for the property.
|
|
1500
|
+
* @param deleteProperty Delete the property from the object, defaults to true.
|
|
1501
|
+
* @returns The properties if available.
|
|
1502
|
+
*/
|
|
1503
|
+
static extractProperty(nodeObject, propertyNames, deleteProperty = true) {
|
|
1504
|
+
let retVal;
|
|
1505
|
+
for (const prop of propertyNames) {
|
|
1506
|
+
retVal ??= nodeObject[prop];
|
|
1507
|
+
if (deleteProperty) {
|
|
1508
|
+
delete nodeObject[prop];
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
return retVal;
|
|
1512
|
+
}
|
|
1513
|
+
/**
|
|
1514
|
+
* Combine contexts.
|
|
1515
|
+
* @param context1 The first JSON-LD context to combine.
|
|
1516
|
+
* @param context2 The second JSON-LD context to combine.
|
|
1517
|
+
* @returns The combined context.
|
|
1518
|
+
*/
|
|
1519
|
+
static combineContexts(context1, context2) {
|
|
1520
|
+
const combinedContext = [];
|
|
1521
|
+
if (core.Is.string(context1)) {
|
|
1522
|
+
if (!combinedContext.includes(context1)) {
|
|
1523
|
+
combinedContext.push(context1);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
else if (core.Is.array(context1)) {
|
|
1527
|
+
for (const context of context1) {
|
|
1528
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context));
|
|
1529
|
+
if (!hasMatch) {
|
|
1530
|
+
combinedContext.push(context);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
else if (core.Is.object(context1)) {
|
|
1535
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context1));
|
|
1536
|
+
if (!hasMatch) {
|
|
1537
|
+
combinedContext.push(context1);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
if (core.Is.string(context2)) {
|
|
1541
|
+
if (!combinedContext.includes(context2)) {
|
|
1542
|
+
combinedContext.push(context2);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
else if (core.Is.array(context2)) {
|
|
1546
|
+
for (const context of context2) {
|
|
1547
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context));
|
|
1548
|
+
if (!hasMatch) {
|
|
1549
|
+
combinedContext.push(context);
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
else if (core.Is.object(context2)) {
|
|
1554
|
+
const hasMatch = combinedContext.some(c => core.ObjectHelper.equal(c, context2));
|
|
1555
|
+
if (!hasMatch) {
|
|
1556
|
+
combinedContext.push(context2);
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
if (combinedContext.length === 0) {
|
|
1560
|
+
return null;
|
|
1561
|
+
}
|
|
1562
|
+
if (combinedContext.length === 1) {
|
|
1563
|
+
return combinedContext[0];
|
|
1564
|
+
}
|
|
1565
|
+
return combinedContext;
|
|
1566
|
+
}
|
|
1567
|
+
/**
|
|
1568
|
+
* Gather all the contexts from the element and it's children.
|
|
1569
|
+
* @param element The element to gather the contexts from.
|
|
1570
|
+
* @param initial The initial context.
|
|
1571
|
+
* @returns The combined contexts.
|
|
1572
|
+
*/
|
|
1573
|
+
static gatherContexts(element, initial) {
|
|
1574
|
+
let combinedContexts = initial;
|
|
1575
|
+
if (!core.Is.empty(element["@context"])) {
|
|
1576
|
+
combinedContexts = JsonLdProcessor.combineContexts(initial, element["@context"]);
|
|
1577
|
+
}
|
|
1578
|
+
for (const prop of Object.keys(element)) {
|
|
1579
|
+
const value = element[prop];
|
|
1580
|
+
if (core.Is.object(value)) {
|
|
1581
|
+
combinedContexts = this.gatherContexts(value, combinedContexts);
|
|
1582
|
+
}
|
|
1583
|
+
else if (core.Is.array(value)) {
|
|
1584
|
+
for (const item of value) {
|
|
1585
|
+
if (core.Is.object(item)) {
|
|
1586
|
+
combinedContexts = this.gatherContexts(item, combinedContexts);
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
return combinedContexts;
|
|
1592
|
+
}
|
|
1572
1593
|
/**
|
|
1573
1594
|
* Document loader which uses a caching mechanism.
|
|
1574
1595
|
* @param url The document url to load.
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataTypeHandlerFactory, DataTypeHelper } from '@twin.org/data-core';
|
|
2
|
-
import { Is, GeneralError } from '@twin.org/core';
|
|
2
|
+
import { Is, GeneralError, ObjectHelper } from '@twin.org/core';
|
|
3
3
|
import { FetchHelper, HttpMethod, MimeTypes } from '@twin.org/web';
|
|
4
4
|
import jsonLd from 'jsonld';
|
|
5
5
|
|
|
@@ -372,33 +372,7 @@ var anyOf$7 = [
|
|
|
372
372
|
type: "object",
|
|
373
373
|
properties: {
|
|
374
374
|
"@context": {
|
|
375
|
-
|
|
376
|
-
{
|
|
377
|
-
type: "null"
|
|
378
|
-
},
|
|
379
|
-
{
|
|
380
|
-
type: "string"
|
|
381
|
-
},
|
|
382
|
-
{
|
|
383
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
type: "array",
|
|
387
|
-
items: {
|
|
388
|
-
anyOf: [
|
|
389
|
-
{
|
|
390
|
-
type: "null"
|
|
391
|
-
},
|
|
392
|
-
{
|
|
393
|
-
type: "string"
|
|
394
|
-
},
|
|
395
|
-
{
|
|
396
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
397
|
-
}
|
|
398
|
-
]
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
]
|
|
375
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
402
376
|
},
|
|
403
377
|
"@graph": {
|
|
404
378
|
anyOf: [
|
|
@@ -560,33 +534,7 @@ var properties$4 = {
|
|
|
560
534
|
]
|
|
561
535
|
},
|
|
562
536
|
"@context": {
|
|
563
|
-
|
|
564
|
-
{
|
|
565
|
-
type: "null"
|
|
566
|
-
},
|
|
567
|
-
{
|
|
568
|
-
type: "string"
|
|
569
|
-
},
|
|
570
|
-
{
|
|
571
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
572
|
-
},
|
|
573
|
-
{
|
|
574
|
-
type: "array",
|
|
575
|
-
items: {
|
|
576
|
-
anyOf: [
|
|
577
|
-
{
|
|
578
|
-
type: "null"
|
|
579
|
-
},
|
|
580
|
-
{
|
|
581
|
-
type: "string"
|
|
582
|
-
},
|
|
583
|
-
{
|
|
584
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
585
|
-
}
|
|
586
|
-
]
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
]
|
|
537
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
590
538
|
}
|
|
591
539
|
};
|
|
592
540
|
var required$3 = [
|
|
@@ -782,33 +730,7 @@ var properties$3 = {
|
|
|
782
730
|
]
|
|
783
731
|
},
|
|
784
732
|
"@context": {
|
|
785
|
-
|
|
786
|
-
{
|
|
787
|
-
type: "null"
|
|
788
|
-
},
|
|
789
|
-
{
|
|
790
|
-
type: "string"
|
|
791
|
-
},
|
|
792
|
-
{
|
|
793
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
794
|
-
},
|
|
795
|
-
{
|
|
796
|
-
type: "array",
|
|
797
|
-
items: {
|
|
798
|
-
anyOf: [
|
|
799
|
-
{
|
|
800
|
-
type: "null"
|
|
801
|
-
},
|
|
802
|
-
{
|
|
803
|
-
type: "string"
|
|
804
|
-
},
|
|
805
|
-
{
|
|
806
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
807
|
-
}
|
|
808
|
-
]
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
]
|
|
733
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
812
734
|
},
|
|
813
735
|
"@direction": {
|
|
814
736
|
type: [
|
|
@@ -1060,33 +982,7 @@ var JsonLdListOrSetItemSchema = {
|
|
|
1060
982
|
var type$2 = "object";
|
|
1061
983
|
var properties$1 = {
|
|
1062
984
|
"@context": {
|
|
1063
|
-
|
|
1064
|
-
{
|
|
1065
|
-
type: "null"
|
|
1066
|
-
},
|
|
1067
|
-
{
|
|
1068
|
-
type: "string"
|
|
1069
|
-
},
|
|
1070
|
-
{
|
|
1071
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1072
|
-
},
|
|
1073
|
-
{
|
|
1074
|
-
type: "array",
|
|
1075
|
-
items: {
|
|
1076
|
-
anyOf: [
|
|
1077
|
-
{
|
|
1078
|
-
type: "null"
|
|
1079
|
-
},
|
|
1080
|
-
{
|
|
1081
|
-
type: "string"
|
|
1082
|
-
},
|
|
1083
|
-
{
|
|
1084
|
-
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinition"
|
|
1085
|
-
}
|
|
1086
|
-
]
|
|
1087
|
-
}
|
|
1088
|
-
}
|
|
1089
|
-
]
|
|
985
|
+
$ref: "https://schema.twindev.org/json-ld/JsonLdContextDefinitionRoot"
|
|
1090
986
|
},
|
|
1091
987
|
"@id": {
|
|
1092
988
|
anyOf: [
|
|
@@ -1529,16 +1425,39 @@ class JsonLdProcessor {
|
|
|
1529
1425
|
/**
|
|
1530
1426
|
* Compact a document according to a particular context.
|
|
1531
1427
|
* @param document The JSON-LD document to compact.
|
|
1532
|
-
* @param context The context to compact the document to.
|
|
1428
|
+
* @param context The context to compact the document to, if not provided will try and gather from the object.
|
|
1533
1429
|
* @returns The compacted JSON-LD document.
|
|
1534
1430
|
*/
|
|
1535
1431
|
static async compact(document, context) {
|
|
1536
1432
|
try {
|
|
1537
|
-
|
|
1433
|
+
// There is a cast here because the jsonld types are not correct.
|
|
1434
|
+
// A context definition can be an array or an object, but the types only allow an object.
|
|
1435
|
+
if (Is.empty(context)) {
|
|
1436
|
+
context = {};
|
|
1437
|
+
if (Is.array(document)) {
|
|
1438
|
+
for (const node of document) {
|
|
1439
|
+
context = JsonLdProcessor.gatherContexts(node, context);
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
else if (Is.array(document["@graph"])) {
|
|
1443
|
+
for (const node of document["@graph"]) {
|
|
1444
|
+
context = JsonLdProcessor.gatherContexts(node, context);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
else if (Is.object(document)) {
|
|
1448
|
+
context = JsonLdProcessor.gatherContexts(document, context);
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
const compacted = await jsonLd.compact(document, context, {
|
|
1538
1452
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1539
1453
|
});
|
|
1454
|
+
return compacted;
|
|
1540
1455
|
}
|
|
1541
1456
|
catch (err) {
|
|
1457
|
+
if (Is.object(err) &&
|
|
1458
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1459
|
+
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1460
|
+
}
|
|
1542
1461
|
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "compact", undefined, err);
|
|
1543
1462
|
}
|
|
1544
1463
|
}
|
|
@@ -1549,11 +1468,16 @@ class JsonLdProcessor {
|
|
|
1549
1468
|
*/
|
|
1550
1469
|
static async expand(compacted) {
|
|
1551
1470
|
try {
|
|
1552
|
-
|
|
1471
|
+
const expanded = await jsonLd.expand(compacted, {
|
|
1553
1472
|
documentLoader: JsonLdProcessor.DOCUMENT_LOADER
|
|
1554
1473
|
});
|
|
1474
|
+
return expanded;
|
|
1555
1475
|
}
|
|
1556
1476
|
catch (err) {
|
|
1477
|
+
if (Is.object(err) &&
|
|
1478
|
+
err.name === "jsonld.InvalidUrl") {
|
|
1479
|
+
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "invalidUrl", { url: err.details?.url }, err);
|
|
1480
|
+
}
|
|
1557
1481
|
throw new GeneralError(JsonLdProcessor._CLASS_NAME, "expand", undefined, err);
|
|
1558
1482
|
}
|
|
1559
1483
|
}
|
|
@@ -1567,6 +1491,103 @@ class JsonLdProcessor {
|
|
|
1567
1491
|
this._redirects.push({ from, to });
|
|
1568
1492
|
}
|
|
1569
1493
|
}
|
|
1494
|
+
/**
|
|
1495
|
+
* Extract a property from the JSON-LD.
|
|
1496
|
+
* @param nodeObject The JSON-LD node object to extract from.
|
|
1497
|
+
* @param propertyNames The possible names for the property.
|
|
1498
|
+
* @param deleteProperty Delete the property from the object, defaults to true.
|
|
1499
|
+
* @returns The properties if available.
|
|
1500
|
+
*/
|
|
1501
|
+
static extractProperty(nodeObject, propertyNames, deleteProperty = true) {
|
|
1502
|
+
let retVal;
|
|
1503
|
+
for (const prop of propertyNames) {
|
|
1504
|
+
retVal ??= nodeObject[prop];
|
|
1505
|
+
if (deleteProperty) {
|
|
1506
|
+
delete nodeObject[prop];
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
return retVal;
|
|
1510
|
+
}
|
|
1511
|
+
/**
|
|
1512
|
+
* Combine contexts.
|
|
1513
|
+
* @param context1 The first JSON-LD context to combine.
|
|
1514
|
+
* @param context2 The second JSON-LD context to combine.
|
|
1515
|
+
* @returns The combined context.
|
|
1516
|
+
*/
|
|
1517
|
+
static combineContexts(context1, context2) {
|
|
1518
|
+
const combinedContext = [];
|
|
1519
|
+
if (Is.string(context1)) {
|
|
1520
|
+
if (!combinedContext.includes(context1)) {
|
|
1521
|
+
combinedContext.push(context1);
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
else if (Is.array(context1)) {
|
|
1525
|
+
for (const context of context1) {
|
|
1526
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context));
|
|
1527
|
+
if (!hasMatch) {
|
|
1528
|
+
combinedContext.push(context);
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
else if (Is.object(context1)) {
|
|
1533
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context1));
|
|
1534
|
+
if (!hasMatch) {
|
|
1535
|
+
combinedContext.push(context1);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
if (Is.string(context2)) {
|
|
1539
|
+
if (!combinedContext.includes(context2)) {
|
|
1540
|
+
combinedContext.push(context2);
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
else if (Is.array(context2)) {
|
|
1544
|
+
for (const context of context2) {
|
|
1545
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context));
|
|
1546
|
+
if (!hasMatch) {
|
|
1547
|
+
combinedContext.push(context);
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
else if (Is.object(context2)) {
|
|
1552
|
+
const hasMatch = combinedContext.some(c => ObjectHelper.equal(c, context2));
|
|
1553
|
+
if (!hasMatch) {
|
|
1554
|
+
combinedContext.push(context2);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
if (combinedContext.length === 0) {
|
|
1558
|
+
return null;
|
|
1559
|
+
}
|
|
1560
|
+
if (combinedContext.length === 1) {
|
|
1561
|
+
return combinedContext[0];
|
|
1562
|
+
}
|
|
1563
|
+
return combinedContext;
|
|
1564
|
+
}
|
|
1565
|
+
/**
|
|
1566
|
+
* Gather all the contexts from the element and it's children.
|
|
1567
|
+
* @param element The element to gather the contexts from.
|
|
1568
|
+
* @param initial The initial context.
|
|
1569
|
+
* @returns The combined contexts.
|
|
1570
|
+
*/
|
|
1571
|
+
static gatherContexts(element, initial) {
|
|
1572
|
+
let combinedContexts = initial;
|
|
1573
|
+
if (!Is.empty(element["@context"])) {
|
|
1574
|
+
combinedContexts = JsonLdProcessor.combineContexts(initial, element["@context"]);
|
|
1575
|
+
}
|
|
1576
|
+
for (const prop of Object.keys(element)) {
|
|
1577
|
+
const value = element[prop];
|
|
1578
|
+
if (Is.object(value)) {
|
|
1579
|
+
combinedContexts = this.gatherContexts(value, combinedContexts);
|
|
1580
|
+
}
|
|
1581
|
+
else if (Is.array(value)) {
|
|
1582
|
+
for (const item of value) {
|
|
1583
|
+
if (Is.object(item)) {
|
|
1584
|
+
combinedContexts = this.gatherContexts(item, combinedContexts);
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
return combinedContexts;
|
|
1590
|
+
}
|
|
1570
1591
|
/**
|
|
1571
1592
|
* Document loader which uses a caching mechanism.
|
|
1572
1593
|
* @param url The document url to load.
|
|
@@ -140,6 +140,14 @@ export interface IJsonLdContextDefinition {
|
|
|
140
140
|
"@version"?: IJsonLdKeyword["@version"] | undefined;
|
|
141
141
|
"@vocab"?: IJsonLdKeyword["@vocab"] | undefined;
|
|
142
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* A context definition element is used to define the types of a context definition.
|
|
145
|
+
*/
|
|
146
|
+
export type IJsonLdContextDefinitionElement = null | string | IJsonLdContextDefinition;
|
|
147
|
+
/**
|
|
148
|
+
* A context definition root is used to define the root of a context definition.
|
|
149
|
+
*/
|
|
150
|
+
export type IJsonLdContextDefinitionRoot = IJsonLdContextDefinitionElement | IJsonLdContextDefinitionElement[];
|
|
143
151
|
/**
|
|
144
152
|
* An expanded term definition is used to describe the mapping between a term
|
|
145
153
|
* and its expanded identifier, as well as other properties of the value
|
|
@@ -171,7 +179,7 @@ export type IJsonLdExpandedTermDefinition = {
|
|
|
171
179
|
export type IJsonLdKeyword = {
|
|
172
180
|
"@base": string | null;
|
|
173
181
|
"@container": ("@list" | "@set" | IJsonLdContainerType) | ("@list" | "@set" | IJsonLdContainerType)[] | IJsonLdContainerTypeArray | null;
|
|
174
|
-
"@context":
|
|
182
|
+
"@context": IJsonLdContextDefinitionRoot;
|
|
175
183
|
"@direction": "ltr" | "rtl" | null;
|
|
176
184
|
"@graph": IJsonLdValueObject | IJsonLdNodeObject | (IJsonLdValueObject | IJsonLdNodeObject)[];
|
|
177
185
|
"@id": string | string[];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RemoteDocument, Url } from "jsonld/jsonld-spec";
|
|
2
|
-
import type {
|
|
2
|
+
import type { IJsonLdContextDefinitionRoot, IJsonLdDocument, IJsonLdNodeObject } from "../models/IJsonLdDocument";
|
|
3
3
|
/**
|
|
4
4
|
* JSON-LD Processor.
|
|
5
5
|
*/
|
|
@@ -15,10 +15,10 @@ export declare class JsonLdProcessor {
|
|
|
15
15
|
/**
|
|
16
16
|
* Compact a document according to a particular context.
|
|
17
17
|
* @param document The JSON-LD document to compact.
|
|
18
|
-
* @param context The context to compact the document to.
|
|
18
|
+
* @param context The context to compact the document to, if not provided will try and gather from the object.
|
|
19
19
|
* @returns The compacted JSON-LD document.
|
|
20
20
|
*/
|
|
21
|
-
static compact(document: IJsonLdDocument, context?:
|
|
21
|
+
static compact(document: IJsonLdDocument, context?: IJsonLdContextDefinitionRoot): Promise<IJsonLdDocument>;
|
|
22
22
|
/**
|
|
23
23
|
* Expand a document, removing its context.
|
|
24
24
|
* @param compacted The compacted JSON-LD document to expand.
|
|
@@ -31,4 +31,28 @@ export declare class JsonLdProcessor {
|
|
|
31
31
|
* @param to The URL to redirect to.
|
|
32
32
|
*/
|
|
33
33
|
static addRedirect(from: RegExp, to: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* Extract a property from the JSON-LD.
|
|
36
|
+
* @param nodeObject The JSON-LD node object to extract from.
|
|
37
|
+
* @param propertyNames The possible names for the property.
|
|
38
|
+
* @param deleteProperty Delete the property from the object, defaults to true.
|
|
39
|
+
* @returns The properties if available.
|
|
40
|
+
*/
|
|
41
|
+
static extractProperty<T>(nodeObject: IJsonLdNodeObject, propertyNames: string[], deleteProperty?: boolean): T | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Combine contexts.
|
|
44
|
+
* @param context1 The first JSON-LD context to combine.
|
|
45
|
+
* @param context2 The second JSON-LD context to combine.
|
|
46
|
+
* @returns The combined context.
|
|
47
|
+
*/
|
|
48
|
+
static combineContexts(context1: IJsonLdContextDefinitionRoot | undefined, context2: IJsonLdContextDefinitionRoot | undefined): IJsonLdContextDefinitionRoot | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Gather all the contexts from the element and it's children.
|
|
51
|
+
* @param element The element to gather the contexts from.
|
|
52
|
+
* @param initial The initial context.
|
|
53
|
+
* @returns The combined contexts.
|
|
54
|
+
*/
|
|
55
|
+
static gatherContexts(element: {
|
|
56
|
+
[id: string]: unknown;
|
|
57
|
+
}, initial?: IJsonLdContextDefinitionRoot): IJsonLdContextDefinitionRoot | undefined;
|
|
34
58
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -42,9 +42,9 @@ Compact a document according to a particular context.
|
|
|
42
42
|
|
|
43
43
|
The JSON-LD document to compact.
|
|
44
44
|
|
|
45
|
-
• **context?**: [`
|
|
45
|
+
• **context?**: [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
46
46
|
|
|
47
|
-
The context to compact the document to.
|
|
47
|
+
The context to compact the document to, if not provided will try and gather from the object.
|
|
48
48
|
|
|
49
49
|
#### Returns
|
|
50
50
|
|
|
@@ -93,3 +93,83 @@ The URL to redirect to.
|
|
|
93
93
|
#### Returns
|
|
94
94
|
|
|
95
95
|
`void`
|
|
96
|
+
|
|
97
|
+
***
|
|
98
|
+
|
|
99
|
+
### extractProperty()
|
|
100
|
+
|
|
101
|
+
> `static` **extractProperty**\<`T`\>(`nodeObject`, `propertyNames`, `deleteProperty`): `undefined` \| `T`
|
|
102
|
+
|
|
103
|
+
Extract a property from the JSON-LD.
|
|
104
|
+
|
|
105
|
+
#### Type Parameters
|
|
106
|
+
|
|
107
|
+
• **T**
|
|
108
|
+
|
|
109
|
+
#### Parameters
|
|
110
|
+
|
|
111
|
+
• **nodeObject**: [`IJsonLdNodeObject`](../interfaces/IJsonLdNodeObject.md)
|
|
112
|
+
|
|
113
|
+
The JSON-LD node object to extract from.
|
|
114
|
+
|
|
115
|
+
• **propertyNames**: `string`[]
|
|
116
|
+
|
|
117
|
+
The possible names for the property.
|
|
118
|
+
|
|
119
|
+
• **deleteProperty**: `boolean` = `true`
|
|
120
|
+
|
|
121
|
+
Delete the property from the object, defaults to true.
|
|
122
|
+
|
|
123
|
+
#### Returns
|
|
124
|
+
|
|
125
|
+
`undefined` \| `T`
|
|
126
|
+
|
|
127
|
+
The properties if available.
|
|
128
|
+
|
|
129
|
+
***
|
|
130
|
+
|
|
131
|
+
### combineContexts()
|
|
132
|
+
|
|
133
|
+
> `static` **combineContexts**(`context1`, `context2`): `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
134
|
+
|
|
135
|
+
Combine contexts.
|
|
136
|
+
|
|
137
|
+
#### Parameters
|
|
138
|
+
|
|
139
|
+
• **context1**: `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
140
|
+
|
|
141
|
+
The first JSON-LD context to combine.
|
|
142
|
+
|
|
143
|
+
• **context2**: `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
144
|
+
|
|
145
|
+
The second JSON-LD context to combine.
|
|
146
|
+
|
|
147
|
+
#### Returns
|
|
148
|
+
|
|
149
|
+
`undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
150
|
+
|
|
151
|
+
The combined context.
|
|
152
|
+
|
|
153
|
+
***
|
|
154
|
+
|
|
155
|
+
### gatherContexts()
|
|
156
|
+
|
|
157
|
+
> `static` **gatherContexts**(`element`, `initial`?): `undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
158
|
+
|
|
159
|
+
Gather all the contexts from the element and it's children.
|
|
160
|
+
|
|
161
|
+
#### Parameters
|
|
162
|
+
|
|
163
|
+
• **element**
|
|
164
|
+
|
|
165
|
+
The element to gather the contexts from.
|
|
166
|
+
|
|
167
|
+
• **initial?**: [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
168
|
+
|
|
169
|
+
The initial context.
|
|
170
|
+
|
|
171
|
+
#### Returns
|
|
172
|
+
|
|
173
|
+
`undefined` \| [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
174
|
+
|
|
175
|
+
The combined contexts.
|
package/docs/reference/index.md
CHANGED
|
@@ -26,6 +26,8 @@
|
|
|
26
26
|
- [IJsonLdValueObject](type-aliases/IJsonLdValueObject.md)
|
|
27
27
|
- [IJsonLdIndexMapItem](type-aliases/IJsonLdIndexMapItem.md)
|
|
28
28
|
- [IJsonLdIncludedBlock](type-aliases/IJsonLdIncludedBlock.md)
|
|
29
|
+
- [IJsonLdContextDefinitionElement](type-aliases/IJsonLdContextDefinitionElement.md)
|
|
30
|
+
- [IJsonLdContextDefinitionRoot](type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
29
31
|
- [IJsonLdExpandedTermDefinition](type-aliases/IJsonLdExpandedTermDefinition.md)
|
|
30
32
|
- [IJsonLdKeyword](type-aliases/IJsonLdKeyword.md)
|
|
31
33
|
- [IJsonLdListOrSetItem](type-aliases/IJsonLdListOrSetItem.md)
|
|
@@ -28,4 +28,4 @@ https://www.w3.org/TR/json-ld11/#graph-objects
|
|
|
28
28
|
|
|
29
29
|
### @context?
|
|
30
30
|
|
|
31
|
-
> `optional` **@context**:
|
|
31
|
+
> `optional` **@context**: [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
@@ -15,7 +15,7 @@ https://www.w3.org/TR/json-ld11/#node-objects
|
|
|
15
15
|
|
|
16
16
|
### @context?
|
|
17
17
|
|
|
18
|
-
> `optional` **@context**:
|
|
18
|
+
> `optional` **@context**: [`IJsonLdContextDefinitionRoot`](../type-aliases/IJsonLdContextDefinitionRoot.md)
|
|
19
19
|
|
|
20
20
|
***
|
|
21
21
|
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Type Alias: IJsonLdContextDefinitionRoot
|
|
2
|
+
|
|
3
|
+
> **IJsonLdContextDefinitionRoot**: [`IJsonLdContextDefinitionElement`](IJsonLdContextDefinitionElement.md) \| [`IJsonLdContextDefinitionElement`](IJsonLdContextDefinitionElement.md)[]
|
|
4
|
+
|
|
5
|
+
A context definition root is used to define the root of a context definition.
|
|
@@ -18,7 +18,7 @@ Not for export.
|
|
|
18
18
|
|
|
19
19
|
### @context
|
|
20
20
|
|
|
21
|
-
> **@context**:
|
|
21
|
+
> **@context**: [`IJsonLdContextDefinitionRoot`](IJsonLdContextDefinitionRoot.md)
|
|
22
22
|
|
|
23
23
|
### @direction
|
|
24
24
|
|
package/locales/en.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
2
|
+
"jsonLdProcessor": {
|
|
3
3
|
"compact": "The JSON-LD compaction failed",
|
|
4
|
-
"expand": "The JSON-LD expansion failed"
|
|
4
|
+
"expand": "The JSON-LD expansion failed",
|
|
5
|
+
"invalidUrl": "The JSON-LD processing failed to retrieve from the following url \"{url}\""
|
|
5
6
|
}
|
|
6
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/data-json-ld",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.5",
|
|
4
4
|
"description": "Models which define the structure of JSON LD",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,46 +13,16 @@
|
|
|
13
13
|
"engines": {
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
|
-
"scripts": {
|
|
17
|
-
"clean": "rimraf dist coverage docs/reference src/schemas",
|
|
18
|
-
"build:schema": "ts-to-schema ./ts-to-schema.json ./src/schemas",
|
|
19
|
-
"build:compile": "tspc",
|
|
20
|
-
"build": "npm run build:schema && npm run build:compile",
|
|
21
|
-
"test": "vitest --run --config ./vitest.config.ts --no-cache",
|
|
22
|
-
"coverage": "vitest --run --coverage --config ./vitest.config.ts --no-cache",
|
|
23
|
-
"bundle:esm": "rollup --config rollup.config.mjs --environment MODULE:esm",
|
|
24
|
-
"bundle:cjs": "rollup --config rollup.config.mjs --environment MODULE:cjs",
|
|
25
|
-
"bundle": "npm run bundle:esm && npm run bundle:cjs",
|
|
26
|
-
"docs:clean": "rimraf docs/reference",
|
|
27
|
-
"docs:generate": "typedoc",
|
|
28
|
-
"docs": "npm run docs:clean && npm run docs:generate",
|
|
29
|
-
"dist": "npm run clean && npm run build && npm run test && npm run bundle && npm run docs"
|
|
30
|
-
},
|
|
31
16
|
"dependencies": {
|
|
32
17
|
"@twin.org/core": "next",
|
|
33
|
-
"@twin.org/data-core": "0.0.1-next.
|
|
18
|
+
"@twin.org/data-core": "0.0.1-next.5",
|
|
34
19
|
"@twin.org/entity": "next",
|
|
35
20
|
"@twin.org/nameof": "next",
|
|
36
21
|
"@twin.org/web": "next",
|
|
37
|
-
"@types/jsonld": "1.5.15",
|
|
38
22
|
"@types/json-schema": "7.0.15",
|
|
23
|
+
"@types/jsonld": "1.5.15",
|
|
39
24
|
"jsonld": "8.3.2"
|
|
40
25
|
},
|
|
41
|
-
"devDependencies": {
|
|
42
|
-
"@twin.org/nameof-transformer": "next",
|
|
43
|
-
"@twin.org/ts-to-schema": "next",
|
|
44
|
-
"@rollup/plugin-json": "6.1.0",
|
|
45
|
-
"@vitest/coverage-v8": "2.1.1",
|
|
46
|
-
"copyfiles": "2.4.1",
|
|
47
|
-
"rimraf": "6.0.1",
|
|
48
|
-
"rollup": "4.22.0",
|
|
49
|
-
"rollup-plugin-typescript2": "0.36.0",
|
|
50
|
-
"ts-patch": "3.2.1",
|
|
51
|
-
"typedoc": "0.26.7",
|
|
52
|
-
"typedoc-plugin-markdown": "4.2.7",
|
|
53
|
-
"typescript": "5.6.2",
|
|
54
|
-
"vitest": "2.1.1"
|
|
55
|
-
},
|
|
56
26
|
"main": "./dist/cjs/index.cjs",
|
|
57
27
|
"module": "./dist/esm/index.mjs",
|
|
58
28
|
"types": "./dist/types/index.d.ts",
|