jsonldjs 0.1.0 → 0.1.2
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 +405 -1
- package/builder-utils.d.ts +57 -0
- package/builder-utils.js +269 -0
- package/builder.d.ts +44 -0
- package/builder.js +119 -0
- package/config.d.ts +143 -0
- package/config.js +344 -0
- package/esm/builder-utils.js +255 -0
- package/esm/builder.js +114 -0
- package/esm/config.js +339 -0
- package/esm/index.js +4 -390
- package/esm/jsonld-utils.js +483 -0
- package/esm/types.js +8 -0
- package/index.d.ts +5 -119
- package/index.js +24 -402
- package/jsonld-utils.d.ts +142 -0
- package/jsonld-utils.js +500 -0
- package/package.json +1 -1
- package/types.d.ts +162 -0
- package/types.js +9 -0
package/index.js
CHANGED
|
@@ -1,404 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Custom JSON-LD graph utilities
|
|
4
|
-
*
|
|
5
|
-
* Simple, focused functions for working with JSON-LD graph arrays.
|
|
6
|
-
* No external dependencies, no context expansion - just pure graph traversal.
|
|
7
|
-
*/
|
|
8
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.findEntity =
|
|
10
|
-
|
|
11
|
-
exports.
|
|
12
|
-
exports.
|
|
13
|
-
|
|
14
|
-
exports.
|
|
15
|
-
exports.
|
|
16
|
-
|
|
17
|
-
exports.
|
|
18
|
-
exports.
|
|
19
|
-
exports.
|
|
20
|
-
exports.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
*
|
|
34
|
-
* @param graph - Array of JSON-LD entities
|
|
35
|
-
* @param ids - Array of @ids to search for
|
|
36
|
-
* @returns Array of found entities (may be fewer than requested if some not found)
|
|
37
|
-
*/
|
|
38
|
-
function findEntities(graph, ids) {
|
|
39
|
-
const idSet = new Set(ids);
|
|
40
|
-
return graph.filter(entity => idSet.has(entity['@id']));
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Find entities by @type
|
|
44
|
-
*
|
|
45
|
-
* @param graph - Array of JSON-LD entities
|
|
46
|
-
* @param type - The @type to search for
|
|
47
|
-
* @returns Array of entities matching the type
|
|
48
|
-
*/
|
|
49
|
-
function findEntitiesByType(graph, type) {
|
|
50
|
-
return graph.filter(entity => {
|
|
51
|
-
const entityType = entity['@type'];
|
|
52
|
-
if (Array.isArray(entityType)) {
|
|
53
|
-
return entityType.includes(type);
|
|
54
|
-
}
|
|
55
|
-
return entityType === type;
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Extract all @id references from an entity's properties
|
|
60
|
-
*
|
|
61
|
-
* @param entity - JSON-LD entity to scan
|
|
62
|
-
* @returns Set of @id strings found in the entity
|
|
63
|
-
*/
|
|
64
|
-
function extractReferences(entity) {
|
|
65
|
-
const references = new Set();
|
|
66
|
-
function scanValue(value) {
|
|
67
|
-
if (value === null || value === undefined) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (typeof value === 'string') {
|
|
71
|
-
// Check if it looks like an @id reference using regex pattern
|
|
72
|
-
// Pattern: ^(?:[a-zA-Z][a-zA-Z0-9+.-]*:)?[^\s]+$
|
|
73
|
-
// This matches:
|
|
74
|
-
// - URIs with scheme: scheme:path (e.g., mailto:user@example.com)
|
|
75
|
-
// - Compact URIs: prefix:suffix (e.g., person:danlynch, org:hyperweb)
|
|
76
|
-
// - Must not contain spaces (filters out titles/descriptions with colons)
|
|
77
|
-
const idPattern = /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:)[^\s]+$/;
|
|
78
|
-
// Additional checks:
|
|
79
|
-
// - Must match the pattern
|
|
80
|
-
// - Must contain a colon (to be a reference)
|
|
81
|
-
// - Exclude http/https URLs (handled separately)
|
|
82
|
-
if (idPattern.test(value) && value.includes(':') && !value.startsWith('http')) {
|
|
83
|
-
references.add(value);
|
|
84
|
-
}
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (Array.isArray(value)) {
|
|
88
|
-
value.forEach(scanValue);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (typeof value === 'object') {
|
|
92
|
-
// If it has an @id, add it
|
|
93
|
-
if (value['@id']) {
|
|
94
|
-
references.add(value['@id']);
|
|
95
|
-
}
|
|
96
|
-
// Recursively scan all properties
|
|
97
|
-
Object.values(value).forEach(scanValue);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
// Scan all properties except @id and @type
|
|
102
|
-
Object.entries(entity).forEach(([key, value]) => {
|
|
103
|
-
if (key !== '@id' && key !== '@type') {
|
|
104
|
-
scanValue(value);
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
return references;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Extract a subgraph containing an entity and all its referenced entities
|
|
111
|
-
*
|
|
112
|
-
* Recursively finds all @id references within the entity and includes
|
|
113
|
-
* those entities in the result. Returns a filtered array of the original
|
|
114
|
-
* graph elements - does not modify the entities themselves.
|
|
115
|
-
*
|
|
116
|
-
* @param graph - Array of JSON-LD entities
|
|
117
|
-
* @param id - The root @id to start from
|
|
118
|
-
* @returns Array containing the root entity and all recursively referenced entities
|
|
119
|
-
*/
|
|
120
|
-
function extractSubgraph(graph, id) {
|
|
121
|
-
const result = new Map();
|
|
122
|
-
const visited = new Set();
|
|
123
|
-
const toProcess = new Set([id]);
|
|
124
|
-
while (toProcess.size > 0) {
|
|
125
|
-
const currentId = toProcess.values().next().value;
|
|
126
|
-
toProcess.delete(currentId);
|
|
127
|
-
if (visited.has(currentId)) {
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
visited.add(currentId);
|
|
131
|
-
const entity = findEntity(graph, currentId);
|
|
132
|
-
if (!entity) {
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
result.set(currentId, entity);
|
|
136
|
-
// Find all references in this entity
|
|
137
|
-
const references = extractReferences(entity);
|
|
138
|
-
// Add unvisited references to process queue
|
|
139
|
-
references.forEach(refId => {
|
|
140
|
-
if (!visited.has(refId)) {
|
|
141
|
-
toProcess.add(refId);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
return Array.from(result.values());
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Extract subgraphs for multiple root entities
|
|
149
|
-
*
|
|
150
|
-
* @param graph - Array of JSON-LD entities
|
|
151
|
-
* @param ids - Array of root @ids to start from
|
|
152
|
-
* @returns Array containing all entities and their recursively referenced entities
|
|
153
|
-
*/
|
|
154
|
-
function extractSubgraphs(graph, ids) {
|
|
155
|
-
const allEntities = new Map();
|
|
156
|
-
ids.forEach(id => {
|
|
157
|
-
const subgraph = extractSubgraph(graph, id);
|
|
158
|
-
subgraph.forEach(entity => {
|
|
159
|
-
allEntities.set(entity['@id'], entity);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
return Array.from(allEntities.values());
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Find entities that reference a specific @id
|
|
166
|
-
*
|
|
167
|
-
* @param graph - Array of JSON-LD entities
|
|
168
|
-
* @param targetId - The @id to find references to
|
|
169
|
-
* @returns Array of entities that reference the target @id
|
|
170
|
-
*/
|
|
171
|
-
function findReferencingEntities(graph, targetId) {
|
|
172
|
-
return graph.filter(entity => {
|
|
173
|
-
const references = extractReferences(entity);
|
|
174
|
-
return references.has(targetId);
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Extract a subgraph with limited depth traversal
|
|
179
|
-
*
|
|
180
|
-
* Unlike extractSubgraph which recursively follows all references,
|
|
181
|
-
* this function only traverses to a specified depth from the root entity.
|
|
182
|
-
*
|
|
183
|
-
* @param graph - Array of JSON-LD entities
|
|
184
|
-
* @param id - The root @id to start from
|
|
185
|
-
* @param maxDepth - Maximum depth to traverse (1 = direct references only)
|
|
186
|
-
* @returns Array containing entities up to the specified depth
|
|
187
|
-
*/
|
|
188
|
-
function extractSubgraphWithDepth(graph, id, maxDepth) {
|
|
189
|
-
if (maxDepth < 1) {
|
|
190
|
-
return [];
|
|
191
|
-
}
|
|
192
|
-
const result = new Map();
|
|
193
|
-
const visited = new Set();
|
|
194
|
-
const toProcess = [{ id, depth: 0 }];
|
|
195
|
-
while (toProcess.length > 0) {
|
|
196
|
-
const current = toProcess.shift();
|
|
197
|
-
if (visited.has(current.id) || current.depth > maxDepth) {
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
visited.add(current.id);
|
|
201
|
-
const entity = findEntity(graph, current.id);
|
|
202
|
-
if (!entity) {
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
result.set(current.id, entity);
|
|
206
|
-
// Only process references if we haven't reached max depth
|
|
207
|
-
if (current.depth < maxDepth) {
|
|
208
|
-
const references = extractReferences(entity);
|
|
209
|
-
references.forEach(refId => {
|
|
210
|
-
if (!visited.has(refId)) {
|
|
211
|
-
toProcess.push({ id: refId, depth: current.depth + 1 });
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return Array.from(result.values());
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Find all @id references that don't have corresponding entities in the graph
|
|
220
|
-
* @param graph - The JSON-LD graph to analyze
|
|
221
|
-
* @returns Array of missing @id references
|
|
222
|
-
*/
|
|
223
|
-
function findMissingReferences(graph) {
|
|
224
|
-
// First, collect all entity @ids that exist in the graph
|
|
225
|
-
const existingIds = new Set();
|
|
226
|
-
for (const entity of graph) {
|
|
227
|
-
if (entity['@id']) {
|
|
228
|
-
existingIds.add(entity['@id']);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
// Then, collect all referenced @ids
|
|
232
|
-
const allReferences = new Set();
|
|
233
|
-
for (const entity of graph) {
|
|
234
|
-
const references = extractReferences(entity);
|
|
235
|
-
for (const ref of references) {
|
|
236
|
-
allReferences.add(ref);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
// Find references that don't exist
|
|
240
|
-
const missing = [];
|
|
241
|
-
for (const ref of allReferences) {
|
|
242
|
-
if (!existingIds.has(ref)) {
|
|
243
|
-
missing.push(ref);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return missing.sort(); // Sort for consistent output
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Find entities that contain nested objects (potential normalization candidates)
|
|
250
|
-
* @param graph - The JSON-LD graph to analyze
|
|
251
|
-
* @returns Array of objects with parent @id and details about nested entities
|
|
252
|
-
*/
|
|
253
|
-
function findNestedEntities(graph) {
|
|
254
|
-
const results = [];
|
|
255
|
-
function isNestedEntity(value) {
|
|
256
|
-
// Check if it's an object (not null, not array)
|
|
257
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
258
|
-
return false;
|
|
259
|
-
}
|
|
260
|
-
// Skip if it's just an @id reference (only has @id property)
|
|
261
|
-
if (Object.keys(value).length === 1 && value['@id']) {
|
|
262
|
-
return false;
|
|
263
|
-
}
|
|
264
|
-
// It's a nested entity if it has properties beyond just @id
|
|
265
|
-
return true;
|
|
266
|
-
}
|
|
267
|
-
function scanEntity(entity, parentId, basePath = '') {
|
|
268
|
-
for (const [key, value] of Object.entries(entity)) {
|
|
269
|
-
// Skip special JSON-LD properties
|
|
270
|
-
if (key.startsWith('@'))
|
|
271
|
-
continue;
|
|
272
|
-
const propertyPath = basePath ? `${basePath}.${key}` : key;
|
|
273
|
-
if (isNestedEntity(value)) {
|
|
274
|
-
results.push({
|
|
275
|
-
parentId: parentId,
|
|
276
|
-
property: propertyPath,
|
|
277
|
-
nestedEntity: value,
|
|
278
|
-
hasId: !!value['@id']
|
|
279
|
-
});
|
|
280
|
-
// Recursively scan the nested entity
|
|
281
|
-
scanEntity(value, parentId, propertyPath);
|
|
282
|
-
}
|
|
283
|
-
else if (Array.isArray(value)) {
|
|
284
|
-
// Check arrays for nested entities
|
|
285
|
-
value.forEach((item, index) => {
|
|
286
|
-
if (isNestedEntity(item)) {
|
|
287
|
-
const arrayPath = `${propertyPath}[${index}]`;
|
|
288
|
-
results.push({
|
|
289
|
-
parentId: parentId,
|
|
290
|
-
property: arrayPath,
|
|
291
|
-
nestedEntity: item,
|
|
292
|
-
hasId: !!item['@id']
|
|
293
|
-
});
|
|
294
|
-
// Recursively scan the nested entity in array
|
|
295
|
-
scanEntity(item, parentId, arrayPath);
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
for (const entity of graph) {
|
|
302
|
-
if (!entity['@id'])
|
|
303
|
-
continue;
|
|
304
|
-
scanEntity(entity, entity['@id']);
|
|
305
|
-
}
|
|
306
|
-
return results;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Find all entities that exist in the graph but are never referenced by other entities
|
|
310
|
-
* @param graph - The JSON-LD graph to analyze
|
|
311
|
-
* @returns Array of orphaned @id values
|
|
312
|
-
*/
|
|
313
|
-
function findOrphans(graph) {
|
|
314
|
-
// First, collect all entity @ids that exist in the graph
|
|
315
|
-
const allIds = new Set();
|
|
316
|
-
for (const entity of graph) {
|
|
317
|
-
if (entity['@id']) {
|
|
318
|
-
allIds.add(entity['@id']);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
// Then, collect all referenced @ids
|
|
322
|
-
const referencedIds = new Set();
|
|
323
|
-
for (const entity of graph) {
|
|
324
|
-
const references = extractReferences(entity);
|
|
325
|
-
for (const ref of references) {
|
|
326
|
-
referencedIds.add(ref);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
// Find entities that exist but are never referenced
|
|
330
|
-
const orphans = [];
|
|
331
|
-
for (const id of allIds) {
|
|
332
|
-
if (!referencedIds.has(id)) {
|
|
333
|
-
orphans.push(id);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
return orphans.sort(); // Sort for consistent output
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Inline all @id references within entities, replacing them with their full entity data
|
|
340
|
-
*
|
|
341
|
-
* This function takes an array of entities and recursively replaces all @id references
|
|
342
|
-
* with the actual entity data. If a specific root ID is provided, it returns only
|
|
343
|
-
* that entity with all its references inlined. Otherwise, it returns all entities
|
|
344
|
-
* with references inlined.
|
|
345
|
-
*
|
|
346
|
-
* @param entities - Array of JSON-LD entities
|
|
347
|
-
* @param rootId - Optional root entity @id to return (if not provided, returns all entities)
|
|
348
|
-
* @returns Either a single entity with inlined references or array of all entities with inlined references
|
|
349
|
-
*/
|
|
350
|
-
function inlineReferences(entities, rootId) {
|
|
351
|
-
// Create a lookup map for all entities
|
|
352
|
-
const entityMap = new Map();
|
|
353
|
-
entities.forEach(entity => {
|
|
354
|
-
if (entity['@id']) {
|
|
355
|
-
entityMap.set(entity['@id'], entity);
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
// Function to recursively inline references with circular reference protection
|
|
359
|
-
function inlineValue(value, visited = new Set()) {
|
|
360
|
-
if (value === null || value === undefined) {
|
|
361
|
-
return value;
|
|
362
|
-
}
|
|
363
|
-
if (Array.isArray(value)) {
|
|
364
|
-
return value.map(v => inlineValue(v, visited));
|
|
365
|
-
}
|
|
366
|
-
if (typeof value === 'object') {
|
|
367
|
-
// If it's an @id reference (object with only @id property), replace with the full entity
|
|
368
|
-
if (value['@id'] && Object.keys(value).length === 1) {
|
|
369
|
-
const id = value['@id'];
|
|
370
|
-
// Check for circular reference
|
|
371
|
-
if (visited.has(id)) {
|
|
372
|
-
// Return just the @id reference to break the cycle
|
|
373
|
-
return { '@id': id };
|
|
374
|
-
}
|
|
375
|
-
const referencedEntity = entityMap.get(id);
|
|
376
|
-
if (referencedEntity) {
|
|
377
|
-
// Add to visited set before processing
|
|
378
|
-
const newVisited = new Set(visited);
|
|
379
|
-
newVisited.add(id);
|
|
380
|
-
// Create a copy and inline its references
|
|
381
|
-
const copy = { ...referencedEntity };
|
|
382
|
-
return inlineValue(copy, newVisited);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
// Otherwise, recursively process all properties
|
|
386
|
-
const result = {};
|
|
387
|
-
for (const [key, val] of Object.entries(value)) {
|
|
388
|
-
result[key] = inlineValue(val, visited);
|
|
389
|
-
}
|
|
390
|
-
return result;
|
|
391
|
-
}
|
|
392
|
-
return value;
|
|
393
|
-
}
|
|
394
|
-
// If a root ID is specified, return only that entity with inlined references
|
|
395
|
-
if (rootId) {
|
|
396
|
-
const mainEntity = entities.find(e => e['@id'] === rootId);
|
|
397
|
-
if (!mainEntity) {
|
|
398
|
-
throw new Error(`Entity with @id "${rootId}" not found`);
|
|
399
|
-
}
|
|
400
|
-
return inlineValue(mainEntity);
|
|
401
|
-
}
|
|
402
|
-
// Otherwise, inline references in all entities
|
|
403
|
-
return entities.map(entity => inlineValue(entity));
|
|
404
|
-
}
|
|
3
|
+
exports.filterJsonLdGraph = exports.inlineReferences = exports.findReferencingEntities = exports.findOrphans = exports.findNestedEntities = exports.findMissingReferences = exports.findEntity = exports.findEntitiesByType = exports.findEntities = exports.filterGraphProperties = exports.filterEntityProperties = exports.extractSubgraphs = exports.extractSubgraphWithDepth = exports.extractSubgraph = exports.extractReferences = exports.JsonLdBuilder = exports.createJsonLdBuilder = exports.JsonLdConfigBuilder = exports.createJsonLdConfig = void 0;
|
|
4
|
+
var config_1 = require("./config");
|
|
5
|
+
Object.defineProperty(exports, "createJsonLdConfig", { enumerable: true, get: function () { return config_1.createJsonLdConfig; } });
|
|
6
|
+
Object.defineProperty(exports, "JsonLdConfigBuilder", { enumerable: true, get: function () { return config_1.JsonLdConfigBuilder; } });
|
|
7
|
+
var builder_1 = require("./builder");
|
|
8
|
+
Object.defineProperty(exports, "createJsonLdBuilder", { enumerable: true, get: function () { return builder_1.createJsonLdBuilder; } });
|
|
9
|
+
Object.defineProperty(exports, "JsonLdBuilder", { enumerable: true, get: function () { return builder_1.JsonLdBuilder; } });
|
|
10
|
+
var jsonld_utils_1 = require("./jsonld-utils");
|
|
11
|
+
Object.defineProperty(exports, "extractReferences", { enumerable: true, get: function () { return jsonld_utils_1.extractReferences; } });
|
|
12
|
+
Object.defineProperty(exports, "extractSubgraph", { enumerable: true, get: function () { return jsonld_utils_1.extractSubgraph; } });
|
|
13
|
+
Object.defineProperty(exports, "extractSubgraphWithDepth", { enumerable: true, get: function () { return jsonld_utils_1.extractSubgraphWithDepth; } });
|
|
14
|
+
Object.defineProperty(exports, "extractSubgraphs", { enumerable: true, get: function () { return jsonld_utils_1.extractSubgraphs; } });
|
|
15
|
+
Object.defineProperty(exports, "filterEntityProperties", { enumerable: true, get: function () { return jsonld_utils_1.filterEntityProperties; } });
|
|
16
|
+
Object.defineProperty(exports, "filterGraphProperties", { enumerable: true, get: function () { return jsonld_utils_1.filterGraphProperties; } });
|
|
17
|
+
Object.defineProperty(exports, "findEntities", { enumerable: true, get: function () { return jsonld_utils_1.findEntities; } });
|
|
18
|
+
Object.defineProperty(exports, "findEntitiesByType", { enumerable: true, get: function () { return jsonld_utils_1.findEntitiesByType; } });
|
|
19
|
+
Object.defineProperty(exports, "findEntity", { enumerable: true, get: function () { return jsonld_utils_1.findEntity; } });
|
|
20
|
+
Object.defineProperty(exports, "findMissingReferences", { enumerable: true, get: function () { return jsonld_utils_1.findMissingReferences; } });
|
|
21
|
+
Object.defineProperty(exports, "findNestedEntities", { enumerable: true, get: function () { return jsonld_utils_1.findNestedEntities; } });
|
|
22
|
+
Object.defineProperty(exports, "findOrphans", { enumerable: true, get: function () { return jsonld_utils_1.findOrphans; } });
|
|
23
|
+
Object.defineProperty(exports, "findReferencingEntities", { enumerable: true, get: function () { return jsonld_utils_1.findReferencingEntities; } });
|
|
24
|
+
Object.defineProperty(exports, "inlineReferences", { enumerable: true, get: function () { return jsonld_utils_1.inlineReferences; } });
|
|
25
|
+
var builder_utils_1 = require("./builder-utils");
|
|
26
|
+
Object.defineProperty(exports, "filterJsonLdGraph", { enumerable: true, get: function () { return builder_utils_1.filterJsonLdGraph; } });
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom JSON-LD graph utilities
|
|
3
|
+
*
|
|
4
|
+
* Simple, focused functions for working with JSON-LD graph arrays.
|
|
5
|
+
* No external dependencies, no context expansion - just pure graph traversal.
|
|
6
|
+
*/
|
|
7
|
+
export type JsonLdEntity = {
|
|
8
|
+
'@id': string;
|
|
9
|
+
'@type'?: string | string[];
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
};
|
|
12
|
+
export type JsonLdGraph = JsonLdEntity[];
|
|
13
|
+
export type PropertyFilterSelector = '*' | Record<string, any>;
|
|
14
|
+
export interface PropertyFilterRule {
|
|
15
|
+
selector: PropertyFilterSelector;
|
|
16
|
+
include?: string[];
|
|
17
|
+
exclude?: string[];
|
|
18
|
+
}
|
|
19
|
+
export type PropertyFilterConfig = PropertyFilterRule[];
|
|
20
|
+
/**
|
|
21
|
+
* Check if an entity matches a property filter selector
|
|
22
|
+
*/
|
|
23
|
+
export declare function matchesSelector(entity: JsonLdEntity, selector: PropertyFilterSelector): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Find a single entity in the graph by @id
|
|
26
|
+
*
|
|
27
|
+
* @param graph - Array of JSON-LD entities
|
|
28
|
+
* @param id - The @id to search for
|
|
29
|
+
* @returns The entity if found, undefined otherwise
|
|
30
|
+
*/
|
|
31
|
+
export declare function findEntity(graph: JsonLdGraph, id: string): JsonLdEntity | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Find multiple entities in the graph by @id
|
|
34
|
+
*
|
|
35
|
+
* @param graph - Array of JSON-LD entities
|
|
36
|
+
* @param ids - Array of @ids to search for
|
|
37
|
+
* @returns Array of found entities (may be fewer than requested if some not found)
|
|
38
|
+
*/
|
|
39
|
+
export declare function findEntities(graph: JsonLdGraph, ids: string[]): JsonLdEntity[];
|
|
40
|
+
/**
|
|
41
|
+
* Find entities by @type
|
|
42
|
+
*
|
|
43
|
+
* @param graph - Array of JSON-LD entities
|
|
44
|
+
* @param type - The @type to search for
|
|
45
|
+
* @returns Array of entities matching the type
|
|
46
|
+
*/
|
|
47
|
+
export declare function findEntitiesByType(graph: JsonLdGraph, type: string): JsonLdEntity[];
|
|
48
|
+
/**
|
|
49
|
+
* Extract all @id references from an entity's properties
|
|
50
|
+
*
|
|
51
|
+
* @param entity - JSON-LD entity to scan
|
|
52
|
+
* @returns Set of @id strings found in the entity
|
|
53
|
+
*/
|
|
54
|
+
export declare function extractReferences(entity: JsonLdEntity): Set<string>;
|
|
55
|
+
/**
|
|
56
|
+
* Extract a subgraph containing an entity and all its referenced entities
|
|
57
|
+
*
|
|
58
|
+
* Recursively finds all @id references within the entity and includes
|
|
59
|
+
* those entities in the result. Returns a filtered array of the original
|
|
60
|
+
* graph elements - does not modify the entities themselves.
|
|
61
|
+
*
|
|
62
|
+
* @param graph - Array of JSON-LD entities
|
|
63
|
+
* @param id - The root @id to start from
|
|
64
|
+
* @param propertyFilters - Optional property filtering configuration to apply before reference traversal
|
|
65
|
+
* @returns Array containing the root entity and all recursively referenced entities
|
|
66
|
+
*/
|
|
67
|
+
export declare function extractSubgraph(graph: JsonLdGraph, id: string, propertyFilters?: PropertyFilterConfig): JsonLdEntity[];
|
|
68
|
+
/**
|
|
69
|
+
* Extract subgraphs for multiple root entities
|
|
70
|
+
*
|
|
71
|
+
* @param graph - Array of JSON-LD entities
|
|
72
|
+
* @param ids - Array of root @ids to start from
|
|
73
|
+
* @param propertyFilters - Optional property filtering configuration to apply before reference traversal
|
|
74
|
+
* @returns Array containing all entities and their recursively referenced entities
|
|
75
|
+
*/
|
|
76
|
+
export declare function extractSubgraphs(graph: JsonLdGraph, ids: string[], propertyFilters?: PropertyFilterConfig): JsonLdEntity[];
|
|
77
|
+
/**
|
|
78
|
+
* Find entities that reference a specific @id
|
|
79
|
+
*
|
|
80
|
+
* @param graph - Array of JSON-LD entities
|
|
81
|
+
* @param targetId - The @id to find references to
|
|
82
|
+
* @param propertyFilters - Optional property filtering to apply before checking references
|
|
83
|
+
* @returns Array of entities that reference the target @id
|
|
84
|
+
*/
|
|
85
|
+
export declare function findReferencingEntities(graph: JsonLdGraph, targetId: string, propertyFilters?: PropertyFilterConfig): JsonLdEntity[];
|
|
86
|
+
/**
|
|
87
|
+
* Extract a subgraph with limited depth traversal
|
|
88
|
+
*
|
|
89
|
+
* Unlike extractSubgraph which recursively follows all references,
|
|
90
|
+
* this function only traverses to a specified depth from the root entity.
|
|
91
|
+
*
|
|
92
|
+
* @param graph - Array of JSON-LD entities
|
|
93
|
+
* @param id - The root @id to start from
|
|
94
|
+
* @param maxDepth - Maximum depth to traverse (1 = direct references only)
|
|
95
|
+
* @param propertyFilters - Optional property filtering configuration to apply before reference traversal
|
|
96
|
+
* @returns Array containing entities up to the specified depth
|
|
97
|
+
*/
|
|
98
|
+
export declare function extractSubgraphWithDepth(graph: JsonLdGraph, id: string, maxDepth: number, propertyFilters?: PropertyFilterConfig): JsonLdEntity[];
|
|
99
|
+
/**
|
|
100
|
+
* Find all @id references that don't have corresponding entities in the graph
|
|
101
|
+
* @param graph - The JSON-LD graph to analyze
|
|
102
|
+
* @returns Array of missing @id references
|
|
103
|
+
*/
|
|
104
|
+
export declare function findMissingReferences(graph: JsonLdGraph): string[];
|
|
105
|
+
/**
|
|
106
|
+
* Find entities that contain nested objects (potential normalization candidates)
|
|
107
|
+
* @param graph - The JSON-LD graph to analyze
|
|
108
|
+
* @returns Array of objects with parent @id and details about nested entities
|
|
109
|
+
*/
|
|
110
|
+
export declare function findNestedEntities(graph: JsonLdGraph): Array<{
|
|
111
|
+
parentId: string;
|
|
112
|
+
property: string;
|
|
113
|
+
nestedEntity: any;
|
|
114
|
+
hasId: boolean;
|
|
115
|
+
}>;
|
|
116
|
+
/**
|
|
117
|
+
* Find all entities that exist in the graph but are never referenced by other entities
|
|
118
|
+
* @param graph - The JSON-LD graph to analyze
|
|
119
|
+
* @returns Array of orphaned @id values
|
|
120
|
+
*/
|
|
121
|
+
export declare function findOrphans(graph: JsonLdGraph): string[];
|
|
122
|
+
/**
|
|
123
|
+
* Inline all @id references within entities, replacing them with their full entity data
|
|
124
|
+
*
|
|
125
|
+
* This function takes an array of entities and recursively replaces all @id references
|
|
126
|
+
* with the actual entity data. If a specific root ID is provided, it returns only
|
|
127
|
+
* that entity with all its references inlined. Otherwise, it returns all entities
|
|
128
|
+
* with references inlined.
|
|
129
|
+
*
|
|
130
|
+
* @param entities - Array of JSON-LD entities
|
|
131
|
+
* @param rootId - Optional root entity @id to return (if not provided, returns all entities)
|
|
132
|
+
* @returns Either a single entity with inlined references or array of all entities with inlined references
|
|
133
|
+
*/
|
|
134
|
+
export declare function inlineReferences(entities: JsonLdGraph, rootId?: string): JsonLdEntity | JsonLdGraph;
|
|
135
|
+
/**
|
|
136
|
+
* Apply property filtering to a single entity based on matching rules
|
|
137
|
+
*/
|
|
138
|
+
export declare function filterEntityProperties(entity: JsonLdEntity, config: PropertyFilterConfig): JsonLdEntity;
|
|
139
|
+
/**
|
|
140
|
+
* Apply property filtering to all entities in a graph
|
|
141
|
+
*/
|
|
142
|
+
export declare function filterGraphProperties(graph: JsonLdGraph, config: PropertyFilterConfig): JsonLdGraph;
|