@twin.org/data-json-ld 0.0.3-next.16 → 0.0.3-next.17
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/README.md +1 -1
- package/docs/changelog.md +15 -1
- package/docs/examples.md +190 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# TWIN JSON LD Data
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This package provides JSON-LD data models and helper utilities for working with linked data documents in a predictable and reusable way. It aligns package-level data structures with the [JSON-LD](https://json-ld.org/) ecosystem, making it easier to represent interoperable semantic data across services.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
package/docs/changelog.md
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.0.3-next.17](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.16...data-json-ld-v0.0.3-next.17) (2026-03-12)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **data-json-ld:** Synchronize repo versions
|
|
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.16 to 0.0.3-next.17
|
|
2
16
|
|
|
3
17
|
## [0.0.3-next.16](https://github.com/twinfoundation/data/compare/data-json-ld-v0.0.3-next.15...data-json-ld-v0.0.3-next.16) (2026-03-06)
|
|
4
18
|
|
package/docs/examples.md
CHANGED
|
@@ -1 +1,190 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Data Json Ld Examples
|
|
2
|
+
|
|
3
|
+
These examples walk through context management, document processing, validation, and property access patterns for JSON-LD documents.
|
|
4
|
+
|
|
5
|
+
## JsonLdProcessor
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { JsonLdProcessor } from '@twin.org/data-json-ld';
|
|
9
|
+
|
|
10
|
+
JsonLdProcessor.setCacheLimit(900000);
|
|
11
|
+
console.log('Cache limit:', JsonLdProcessor.getCacheLimit());
|
|
12
|
+
|
|
13
|
+
JsonLdProcessor.setRedirects([
|
|
14
|
+
{
|
|
15
|
+
from: /^https:\/\/raw\.githubusercontent\.com\//,
|
|
16
|
+
to: 'https://cdn.example.org/github-content'
|
|
17
|
+
}
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
JsonLdProcessor.addRedirect(/^https:\/\/schema\.org\//, 'https://schema.org/');
|
|
21
|
+
console.log('Has redirects:', JsonLdProcessor.getRedirects().length > 0);
|
|
22
|
+
|
|
23
|
+
const loader = JsonLdProcessor.getDocumentLoader();
|
|
24
|
+
console.log('Loader type:', typeof loader);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { JsonLdProcessor, type IJsonLdNodeObject } from '@twin.org/data-json-ld';
|
|
29
|
+
|
|
30
|
+
const compactedDoc = {
|
|
31
|
+
'@context': {
|
|
32
|
+
ex: 'https://example.org/vocab/',
|
|
33
|
+
name: 'ex:name'
|
|
34
|
+
},
|
|
35
|
+
'@id': 'urn:example:person:1',
|
|
36
|
+
name: 'Ada'
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const expanded = await JsonLdProcessor.expand(compactedDoc);
|
|
40
|
+
const compactedAgain = await JsonLdProcessor.compact(expanded[0], compactedDoc['@context']);
|
|
41
|
+
|
|
42
|
+
console.log('Compacted id:', compactedAgain['@id']);
|
|
43
|
+
|
|
44
|
+
const canonical = await JsonLdProcessor.canonize(expanded[0] as IJsonLdNodeObject);
|
|
45
|
+
console.log('Canonical includes id:', canonical.includes('urn:example:person:1'));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { JsonLdProcessor, type IJsonLdNodeObject } from '@twin.org/data-json-ld';
|
|
50
|
+
|
|
51
|
+
const contextA = 'https://example.org/context/base';
|
|
52
|
+
const contextB = {
|
|
53
|
+
ex: 'https://example.org/vocab/',
|
|
54
|
+
name: 'ex:name'
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const combined = JsonLdProcessor.combineContexts(contextA, contextB);
|
|
58
|
+
|
|
59
|
+
const documentWithNestedContexts: IJsonLdNodeObject = {
|
|
60
|
+
'@context': contextA,
|
|
61
|
+
'@graph': [
|
|
62
|
+
{
|
|
63
|
+
'@context': contextB,
|
|
64
|
+
'@id': 'urn:example:item:1',
|
|
65
|
+
'@type': 'ex:Person',
|
|
66
|
+
'ex:name': 'Ada'
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const gathered = JsonLdProcessor.gatherContexts(documentWithNestedContexts);
|
|
72
|
+
const filtered = JsonLdProcessor.removeContexts(gathered, [contextB]);
|
|
73
|
+
|
|
74
|
+
console.log('Combined is array:', Array.isArray(combined));
|
|
75
|
+
console.log('Filtered context equals contextA:', filtered === contextA);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { JsonLdProcessor } from '@twin.org/data-json-ld';
|
|
80
|
+
|
|
81
|
+
await JsonLdProcessor.documentCacheAdd('https://example.org/context', {
|
|
82
|
+
'@context': {
|
|
83
|
+
ex: 'https://example.org/vocab/'
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
await JsonLdProcessor.documentCacheRemove('https://example.org/context');
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## JsonLdHelper
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { type IValidationFailure } from '@twin.org/core';
|
|
94
|
+
import { ValidationMode } from '@twin.org/data-core';
|
|
95
|
+
import { JsonLdHelper, type IJsonLdNodeObject } from '@twin.org/data-json-ld';
|
|
96
|
+
|
|
97
|
+
const compactedDocument = {
|
|
98
|
+
'@context': {
|
|
99
|
+
ex: 'https://example.org/vocab/',
|
|
100
|
+
id: '@id',
|
|
101
|
+
type: '@type',
|
|
102
|
+
name: 'ex:name'
|
|
103
|
+
},
|
|
104
|
+
id: 'urn:example:person:1',
|
|
105
|
+
type: 'ex:Person',
|
|
106
|
+
name: 'Ada'
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const failures: IValidationFailure[] = [];
|
|
110
|
+
await JsonLdHelper.validate(compactedDocument, failures, {
|
|
111
|
+
validationMode: ValidationMode.Either,
|
|
112
|
+
failOnMissingType: false
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const expanded = await JsonLdHelper.expand(compactedDocument);
|
|
116
|
+
const hasPersonType = await JsonLdHelper.isType(expanded, ['https://example.org/vocab/Person']);
|
|
117
|
+
const types = await JsonLdHelper.getType(expanded);
|
|
118
|
+
const id = await JsonLdHelper.getId(expanded);
|
|
119
|
+
|
|
120
|
+
console.log('Failure count:', failures.length);
|
|
121
|
+
console.log('Has person type:', hasPersonType);
|
|
122
|
+
console.log('Type list includes person:', types.includes('https://example.org/vocab/Person'));
|
|
123
|
+
console.log('Document id:', id);
|
|
124
|
+
|
|
125
|
+
const nodeObject = JsonLdHelper.toNodeObject(compactedDocument);
|
|
126
|
+
const structuredObject = JsonLdHelper.toStructuredObject<{ name: string }>(
|
|
127
|
+
nodeObject as IJsonLdNodeObject
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
console.log('Structured name:', structuredObject.name);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { JsonLdHelper } from '@twin.org/data-json-ld';
|
|
135
|
+
|
|
136
|
+
const expandedDocument = [
|
|
137
|
+
{
|
|
138
|
+
'@id': 'urn:example:article:1',
|
|
139
|
+
'https://schema.org/name': [
|
|
140
|
+
{ '@value': 'Hello', '@language': 'en' },
|
|
141
|
+
{ '@value': 'Hallo', '@language': 'de' }
|
|
142
|
+
],
|
|
143
|
+
'https://schema.org/wordCount': [{ '@value': 240 }]
|
|
144
|
+
}
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
const title = await JsonLdHelper.getPropertyValue(
|
|
148
|
+
expandedDocument,
|
|
149
|
+
'https://schema.org/name',
|
|
150
|
+
'en'
|
|
151
|
+
);
|
|
152
|
+
const [nameValues, wordCountValues] = await JsonLdHelper.getPropertyValues(expandedDocument, [
|
|
153
|
+
'https://schema.org/name',
|
|
154
|
+
'https://schema.org/wordCount'
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
console.log('English title:', title?.[0]);
|
|
158
|
+
console.log('Name value count:', nameValues?.length);
|
|
159
|
+
console.log('Word count value:', wordCountValues?.[0]);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { JsonLdHelper } from '@twin.org/data-json-ld';
|
|
164
|
+
|
|
165
|
+
const node = {
|
|
166
|
+
'@id': 'urn:example:item:1',
|
|
167
|
+
name: 'Ada',
|
|
168
|
+
role: 'Engineer'
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const prefixed = JsonLdHelper.prefixProperties(node, 'ex', ['name', 'role']);
|
|
172
|
+
const stripped = JsonLdHelper.stripPrefixProperties(prefixed, 'ex');
|
|
173
|
+
|
|
174
|
+
console.log('Prefixed name:', prefixed['ex:name']);
|
|
175
|
+
console.log('Stripped name:', stripped.name);
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## JsonLdDataTypes
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { DataTypeHelper } from '@twin.org/data-core';
|
|
182
|
+
import { JsonLdContexts, JsonLdDataTypes, JsonLdTypes } from '@twin.org/data-json-ld';
|
|
183
|
+
|
|
184
|
+
JsonLdDataTypes.registerTypes();
|
|
185
|
+
|
|
186
|
+
const schema = await DataTypeHelper.getSchemaForType(
|
|
187
|
+
`${JsonLdContexts.Namespace}${JsonLdTypes.Document}`
|
|
188
|
+
);
|
|
189
|
+
console.log('Registered schema type:', schema?.type);
|
|
190
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/data-json-ld",
|
|
3
|
-
"version": "0.0.3-next.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.3-next.17",
|
|
4
|
+
"description": "JSON-LD data models and helpers for working with linked data documents.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/twinfoundation/data.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.17",
|
|
19
19
|
"@twin.org/entity": "next",
|
|
20
20
|
"@twin.org/nameof": "next",
|
|
21
21
|
"@twin.org/web": "next",
|