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/esm/builder.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-LD Builder with fluent interface
|
|
3
|
+
*
|
|
4
|
+
* Provides a mutable builder that applies configurations and processes graphs.
|
|
5
|
+
* Supports runtime overrides and maintains the complex filtering logic from
|
|
6
|
+
* the current head-manager system.
|
|
7
|
+
*/
|
|
8
|
+
import { JsonLdConfigBuilder } from './config';
|
|
9
|
+
import { extractSubgraphs, filterGraphProperties } from './jsonld-utils';
|
|
10
|
+
import { applyPopulateConfig, buildPropertyFilterConfig, filterJsonLdGraph, validateRuntimeConfig, } from './builder-utils';
|
|
11
|
+
/**
|
|
12
|
+
* Mutable builder class that extends config builder with automatic method delegation
|
|
13
|
+
*/
|
|
14
|
+
export class JsonLdBuilder extends JsonLdConfigBuilder {
|
|
15
|
+
constructor(initialConfig = {}) {
|
|
16
|
+
super(initialConfig);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Override the config builder's method creation to return JsonLdBuilder instances
|
|
20
|
+
*/
|
|
21
|
+
createInstance(config) {
|
|
22
|
+
return new JsonLdBuilder(config);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get the current graph state (processes the graph if not already done)
|
|
26
|
+
*/
|
|
27
|
+
getCurrentGraph() {
|
|
28
|
+
if (!this.currentGraph) {
|
|
29
|
+
this.currentGraph = this.processGraph();
|
|
30
|
+
}
|
|
31
|
+
return this.currentGraph;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Build the final JSON-LD output
|
|
35
|
+
* @param options - Build configuration options
|
|
36
|
+
* @param options.prettyPrint - Whether to format JSON with indentation (default: true)
|
|
37
|
+
* @param options.contextUrl - The JSON-LD context URL (default: 'https://schema.org')
|
|
38
|
+
* @param options.withScriptTag - Whether to wrap output in a script tag (default: false)
|
|
39
|
+
* @param options.scriptId - Optional ID attribute for the script tag
|
|
40
|
+
* @returns The JSON-LD string, optionally wrapped in a script tag
|
|
41
|
+
*/
|
|
42
|
+
build(options = {}) {
|
|
43
|
+
const { prettyPrint = true, contextUrl = 'https://schema.org', withScriptTag = false, scriptId, } = options;
|
|
44
|
+
const graph = this.getCurrentGraph();
|
|
45
|
+
// Create the JSON-LD object
|
|
46
|
+
const jsonLd = {
|
|
47
|
+
'@context': contextUrl,
|
|
48
|
+
'@graph': graph,
|
|
49
|
+
};
|
|
50
|
+
// Stringify with optional pretty printing
|
|
51
|
+
const jsonString = JSON.stringify(jsonLd, null, prettyPrint ? 2 : undefined);
|
|
52
|
+
// Wrap in script tag if requested
|
|
53
|
+
if (withScriptTag) {
|
|
54
|
+
const idAttribute = scriptId ? ` id="${scriptId}"` : '';
|
|
55
|
+
return `<script${idAttribute} type="application/ld+json">${jsonString}</script>`;
|
|
56
|
+
}
|
|
57
|
+
return jsonString;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Process the graph according to the current configuration
|
|
61
|
+
* This implements the critical filtering logic from the current manager
|
|
62
|
+
*/
|
|
63
|
+
processGraph() {
|
|
64
|
+
const config = this.getConfig();
|
|
65
|
+
if (!config.baseGraph) {
|
|
66
|
+
throw new Error('No base graph provided. Use baseGraph() or mergeConfig() with a baseGraph.');
|
|
67
|
+
}
|
|
68
|
+
// Validate configuration - only check for critical errors that would break processing
|
|
69
|
+
const errors = validateRuntimeConfig(config);
|
|
70
|
+
if (errors.length > 0) {
|
|
71
|
+
throw new Error(`Configuration validation failed: ${errors.join(', ')}`);
|
|
72
|
+
}
|
|
73
|
+
let graph = [...config.baseGraph];
|
|
74
|
+
const propertyFilters = buildPropertyFilterConfig(config);
|
|
75
|
+
let propertyFiltersApplied = false;
|
|
76
|
+
// Layer 1: Subgraph extraction with property filtering if subgraphRoots are set
|
|
77
|
+
if (config.filters?.subgraphRoots && config.filters.subgraphRoots.length > 0) {
|
|
78
|
+
// Extract subgraph with property filtering during traversal
|
|
79
|
+
graph = extractSubgraphs(graph, config.filters.subgraphRoots, propertyFilters);
|
|
80
|
+
propertyFiltersApplied = true; // Property filters were applied during subgraph extraction
|
|
81
|
+
}
|
|
82
|
+
// Layer 2: Regular entity filtering with property filtering if not already applied
|
|
83
|
+
if (config.filters) {
|
|
84
|
+
// Apply property filters first if they haven't been applied yet
|
|
85
|
+
if (!propertyFiltersApplied && propertyFilters) {
|
|
86
|
+
graph = filterGraphProperties(graph, propertyFilters);
|
|
87
|
+
propertyFiltersApplied = true;
|
|
88
|
+
}
|
|
89
|
+
// Apply entity-level filters
|
|
90
|
+
graph = filterJsonLdGraph(graph, config.filters);
|
|
91
|
+
}
|
|
92
|
+
// Apply entity population if configured
|
|
93
|
+
if (config.populateConfig) {
|
|
94
|
+
graph = applyPopulateConfig(graph, config.populateConfig);
|
|
95
|
+
}
|
|
96
|
+
// Add additional entities if specified
|
|
97
|
+
if (config.additionalEntities && config.additionalEntities.length > 0) {
|
|
98
|
+
graph = [...graph, ...config.additionalEntities];
|
|
99
|
+
}
|
|
100
|
+
// Apply custom transformation pipes
|
|
101
|
+
if (config.pipes && config.pipes.length > 0) {
|
|
102
|
+
for (const pipe of config.pipes) {
|
|
103
|
+
graph = pipe(graph);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return graph;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Factory function to create a new builder
|
|
111
|
+
*/
|
|
112
|
+
export function createJsonLdBuilder() {
|
|
113
|
+
return new JsonLdBuilder();
|
|
114
|
+
}
|
package/esm/config.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Immutable configuration builder for JSON-LD processing
|
|
3
|
+
*
|
|
4
|
+
* Provides a fluent interface for building configurations without
|
|
5
|
+
* performing any graph processing. Each method returns a new
|
|
6
|
+
* configuration object for safe reuse and extension.
|
|
7
|
+
*/
|
|
8
|
+
import { mergeConfigs, mergeFilterOptions } from './builder-utils';
|
|
9
|
+
/**
|
|
10
|
+
* Immutable configuration builder class
|
|
11
|
+
*/
|
|
12
|
+
export class JsonLdConfigBuilder {
|
|
13
|
+
constructor(initialConfig = {}) {
|
|
14
|
+
this.config = { ...initialConfig };
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a new instance of this class (can be overridden by subclasses)
|
|
18
|
+
*/
|
|
19
|
+
createInstance(config) {
|
|
20
|
+
return new JsonLdConfigBuilder(config);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Set the base graph to process
|
|
24
|
+
*/
|
|
25
|
+
baseGraph(graph) {
|
|
26
|
+
return this.createInstance({
|
|
27
|
+
...this.config,
|
|
28
|
+
baseGraph: graph,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Include only entities with these IDs (merges with existing IDs)
|
|
33
|
+
*/
|
|
34
|
+
includeIds(ids) {
|
|
35
|
+
const existingIds = this.config.filters?.includeIds || [];
|
|
36
|
+
return this.createInstance({
|
|
37
|
+
...this.config,
|
|
38
|
+
filters: {
|
|
39
|
+
...this.config.filters,
|
|
40
|
+
includeIds: [...existingIds, ...ids],
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Exclude entities with these IDs (merges with existing IDs)
|
|
46
|
+
*/
|
|
47
|
+
excludeIds(ids) {
|
|
48
|
+
const existingIds = this.config.filters?.excludeIds || [];
|
|
49
|
+
return this.createInstance({
|
|
50
|
+
...this.config,
|
|
51
|
+
filters: {
|
|
52
|
+
...this.config.filters,
|
|
53
|
+
excludeIds: [...existingIds, ...ids],
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Include only these entity types (merges with existing types)
|
|
59
|
+
*/
|
|
60
|
+
includeTypes(types) {
|
|
61
|
+
const existingTypes = this.config.filters?.includeTypes || [];
|
|
62
|
+
return this.createInstance({
|
|
63
|
+
...this.config,
|
|
64
|
+
filters: {
|
|
65
|
+
...this.config.filters,
|
|
66
|
+
includeTypes: [...existingTypes, ...types],
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Exclude these entity types (merges with existing types)
|
|
72
|
+
*/
|
|
73
|
+
excludeTypes(types) {
|
|
74
|
+
const existingTypes = this.config.filters?.excludeTypes || [];
|
|
75
|
+
return this.createInstance({
|
|
76
|
+
...this.config,
|
|
77
|
+
filters: {
|
|
78
|
+
...this.config.filters,
|
|
79
|
+
excludeTypes: [...existingTypes, ...types],
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Apply custom filter function
|
|
85
|
+
*/
|
|
86
|
+
customFilter(fn) {
|
|
87
|
+
return this.createInstance({
|
|
88
|
+
...this.config,
|
|
89
|
+
filters: {
|
|
90
|
+
...this.config.filters,
|
|
91
|
+
customFilter: fn,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Limit maximum number of entities
|
|
97
|
+
*/
|
|
98
|
+
maxEntities(max) {
|
|
99
|
+
return this.createInstance({
|
|
100
|
+
...this.config,
|
|
101
|
+
filters: {
|
|
102
|
+
...this.config.filters,
|
|
103
|
+
maxEntities: max,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Include only entities with these properties (merges with existing properties)
|
|
109
|
+
* @param props - Array of property names that entities must have
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* config.requiredProperties(['name']).requiredProperties(['url'])
|
|
113
|
+
* // Result: entities must have both 'name' and 'url' properties
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
requiredProperties(props) {
|
|
117
|
+
const existingProps = this.config.filters?.requiredProperties || [];
|
|
118
|
+
return this.createInstance({
|
|
119
|
+
...this.config,
|
|
120
|
+
filters: {
|
|
121
|
+
...this.config.filters,
|
|
122
|
+
requiredProperties: [...existingProps, ...props],
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Exclude entities with these properties (merges with existing properties)
|
|
128
|
+
*/
|
|
129
|
+
excludeEntitiesWithProperties(props) {
|
|
130
|
+
const existingProps = this.config.filters?.excludeEntitiesWithProperties || [];
|
|
131
|
+
return this.createInstance({
|
|
132
|
+
...this.config,
|
|
133
|
+
filters: {
|
|
134
|
+
...this.config.filters,
|
|
135
|
+
excludeEntitiesWithProperties: [...existingProps, ...props],
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Extract subgraph starting from these root IDs
|
|
141
|
+
*/
|
|
142
|
+
subgraph(rootIds) {
|
|
143
|
+
const existingRoots = this.config.filters?.subgraphRoots || [];
|
|
144
|
+
return this.createInstance({
|
|
145
|
+
...this.config,
|
|
146
|
+
filters: {
|
|
147
|
+
...this.config.filters,
|
|
148
|
+
subgraphRoots: [...existingRoots, ...rootIds],
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Filter properties for specific entity IDs
|
|
154
|
+
*/
|
|
155
|
+
filterPropertiesByIds(entityIds, rule) {
|
|
156
|
+
const existingRules = this.config.filters?.propertyFiltersByIds || [];
|
|
157
|
+
const newRule = {
|
|
158
|
+
entityIds,
|
|
159
|
+
...rule,
|
|
160
|
+
};
|
|
161
|
+
return this.createInstance({
|
|
162
|
+
...this.config,
|
|
163
|
+
filters: {
|
|
164
|
+
...this.config.filters,
|
|
165
|
+
propertyFiltersByIds: [...existingRules, newRule],
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Filter properties for specific entity types
|
|
171
|
+
*/
|
|
172
|
+
filterPropertiesByTypes(entityTypes, rule) {
|
|
173
|
+
const existingRules = this.config.filters?.propertyFiltersByTypes || [];
|
|
174
|
+
const newRule = {
|
|
175
|
+
entityTypes,
|
|
176
|
+
...rule,
|
|
177
|
+
};
|
|
178
|
+
return this.createInstance({
|
|
179
|
+
...this.config,
|
|
180
|
+
filters: {
|
|
181
|
+
...this.config.filters,
|
|
182
|
+
propertyFiltersByTypes: [...existingRules, newRule],
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Add additional entities
|
|
188
|
+
*/
|
|
189
|
+
addEntities(entities) {
|
|
190
|
+
const existingEntities = this.config.additionalEntities || [];
|
|
191
|
+
return this.createInstance({
|
|
192
|
+
...this.config,
|
|
193
|
+
additionalEntities: [...existingEntities, ...entities],
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Add custom transformation pipe
|
|
198
|
+
*/
|
|
199
|
+
pipe(fn) {
|
|
200
|
+
const existingPipes = this.config.pipes || [];
|
|
201
|
+
return this.createInstance({
|
|
202
|
+
...this.config,
|
|
203
|
+
pipes: [...existingPipes, fn],
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Configure entity population
|
|
208
|
+
*/
|
|
209
|
+
populateEntities(entityIds, populateConfig) {
|
|
210
|
+
// Merge with existing populate config
|
|
211
|
+
const existingPopulateConfig = this.config.populateConfig || {};
|
|
212
|
+
const mergedPopulateConfig = { ...existingPopulateConfig };
|
|
213
|
+
// Add the new populate config for the specified entity IDs
|
|
214
|
+
for (const entityId of entityIds) {
|
|
215
|
+
if (populateConfig[entityId]) {
|
|
216
|
+
mergedPopulateConfig[entityId] = populateConfig[entityId];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return this.createInstance({
|
|
220
|
+
...this.config,
|
|
221
|
+
populateConfig: mergedPopulateConfig,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Clear both includeIds and excludeIds
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* config.includeIds(['a']).excludeIds(['b']).clearIds().includeIds(['c'])
|
|
229
|
+
* // Result: only includes 'c', previous IDs are cleared
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
clearIds() {
|
|
233
|
+
return this.createInstance({
|
|
234
|
+
...this.config,
|
|
235
|
+
filters: {
|
|
236
|
+
...this.config.filters,
|
|
237
|
+
includeIds: undefined,
|
|
238
|
+
excludeIds: undefined,
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Clear both includeTypes and excludeTypes
|
|
244
|
+
*/
|
|
245
|
+
clearTypes() {
|
|
246
|
+
return this.createInstance({
|
|
247
|
+
...this.config,
|
|
248
|
+
filters: {
|
|
249
|
+
...this.config.filters,
|
|
250
|
+
includeTypes: undefined,
|
|
251
|
+
excludeTypes: undefined,
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Clear both requiredProperties and excludeEntitiesWithProperties
|
|
257
|
+
*/
|
|
258
|
+
clearPropertyRequirements() {
|
|
259
|
+
return this.createInstance({
|
|
260
|
+
...this.config,
|
|
261
|
+
filters: {
|
|
262
|
+
...this.config.filters,
|
|
263
|
+
requiredProperties: undefined,
|
|
264
|
+
excludeEntitiesWithProperties: undefined,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Clear both propertyFiltersByIds and propertyFiltersByTypes
|
|
270
|
+
*/
|
|
271
|
+
clearPropertyFilters() {
|
|
272
|
+
return this.createInstance({
|
|
273
|
+
...this.config,
|
|
274
|
+
filters: {
|
|
275
|
+
...this.config.filters,
|
|
276
|
+
propertyFiltersByIds: undefined,
|
|
277
|
+
propertyFiltersByTypes: undefined,
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Clear subgraphRoots
|
|
283
|
+
*/
|
|
284
|
+
clearSubgraph() {
|
|
285
|
+
return this.createInstance({
|
|
286
|
+
...this.config,
|
|
287
|
+
filters: {
|
|
288
|
+
...this.config.filters,
|
|
289
|
+
subgraphRoots: undefined,
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Clear entire configuration except baseGraph
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* config.baseGraph(graph).includeIds(['a']).clearAll().includeTypes(['Person'])
|
|
298
|
+
* // Result: only baseGraph and includeTypes remain
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
clearAll() {
|
|
302
|
+
return this.createInstance({
|
|
303
|
+
baseGraph: this.config.baseGraph,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Merge with another configuration
|
|
308
|
+
* @param config - Configuration to merge with current configuration
|
|
309
|
+
*/
|
|
310
|
+
mergeConfig(config) {
|
|
311
|
+
const currentConfig = this.getConfig();
|
|
312
|
+
const mergedConfig = mergeConfigs(currentConfig, config);
|
|
313
|
+
return this.createInstance(mergedConfig);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Merge only the filters part of another configuration
|
|
317
|
+
* @param filters - Filter options to merge with current filters
|
|
318
|
+
*/
|
|
319
|
+
mergeFilters(filters) {
|
|
320
|
+
const currentFilters = this.config.filters;
|
|
321
|
+
const mergedFilters = mergeFilterOptions(currentFilters, filters);
|
|
322
|
+
return this.createInstance({
|
|
323
|
+
...this.config,
|
|
324
|
+
filters: mergedFilters,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Get the current configuration
|
|
329
|
+
*/
|
|
330
|
+
getConfig() {
|
|
331
|
+
return { ...this.config };
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Factory function to create a new configuration builder
|
|
336
|
+
*/
|
|
337
|
+
export function createJsonLdConfig() {
|
|
338
|
+
return new JsonLdConfigBuilder();
|
|
339
|
+
}
|