jsonldjs 0.1.0 → 0.1.1
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 +375 -1
- package/builder-utils.d.ts +53 -0
- package/builder-utils.js +272 -0
- package/builder.d.ts +48 -0
- package/builder.js +127 -0
- package/config.d.ts +133 -0
- package/config.js +306 -0
- package/esm/builder-utils.js +259 -0
- package/esm/builder.js +122 -0
- package/esm/config.js +301 -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 +158 -0
- package/types.js +9 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for JSON-LD builder
|
|
3
|
+
*
|
|
4
|
+
* Provides configuration merging, validation, and transformation utilities
|
|
5
|
+
* for the JSON-LD builder system.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Merge two configurations, with the second taking precedence
|
|
9
|
+
*/
|
|
10
|
+
export function mergeConfigs(base, override) {
|
|
11
|
+
return {
|
|
12
|
+
// Base graph: override takes precedence
|
|
13
|
+
baseGraph: override.baseGraph ?? base.baseGraph,
|
|
14
|
+
// Filters: merge filter options
|
|
15
|
+
filters: mergeFilterOptions(base.filters, override.filters),
|
|
16
|
+
// Subgraph roots: override takes precedence
|
|
17
|
+
subgraphRoots: override.subgraphRoots ?? base.subgraphRoots,
|
|
18
|
+
// Property filters: combine arrays
|
|
19
|
+
propertyFilters: combinePropertyFilters(base.propertyFilters, override.propertyFilters),
|
|
20
|
+
// Property filters by IDs: combine arrays
|
|
21
|
+
propertyFiltersByIds: combineArrays(base.propertyFiltersByIds, override.propertyFiltersByIds),
|
|
22
|
+
// Property filters by types: combine arrays
|
|
23
|
+
propertyFiltersByTypes: combineArrays(base.propertyFiltersByTypes, override.propertyFiltersByTypes),
|
|
24
|
+
// Additional entities: combine arrays
|
|
25
|
+
additionalEntities: combineArrays(base.additionalEntities, override.additionalEntities),
|
|
26
|
+
// Pipes: combine arrays
|
|
27
|
+
pipes: combineArrays(base.pipes, override.pipes),
|
|
28
|
+
// Populate config: override takes precedence
|
|
29
|
+
populateConfig: override.populateConfig ?? base.populateConfig,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Merge filter options, merging arrays and using override for single values
|
|
34
|
+
*/
|
|
35
|
+
function mergeFilterOptions(base, override) {
|
|
36
|
+
if (!base && !override)
|
|
37
|
+
return undefined;
|
|
38
|
+
if (!base)
|
|
39
|
+
return override;
|
|
40
|
+
if (!override)
|
|
41
|
+
return base;
|
|
42
|
+
return {
|
|
43
|
+
// Merge arrays instead of replacing
|
|
44
|
+
includeTypes: combineArrays(base.includeTypes, override.includeTypes),
|
|
45
|
+
excludeTypes: combineArrays(base.excludeTypes, override.excludeTypes),
|
|
46
|
+
includeIds: combineArrays(base.includeIds, override.includeIds),
|
|
47
|
+
excludeIds: combineArrays(base.excludeIds, override.excludeIds),
|
|
48
|
+
requiredProperties: combineArrays(base.requiredProperties, override.requiredProperties),
|
|
49
|
+
excludeEntitiesWithProperties: combineArrays(base.excludeEntitiesWithProperties, override.excludeEntitiesWithProperties),
|
|
50
|
+
// Single values - override takes precedence
|
|
51
|
+
customFilter: override.customFilter ?? base.customFilter,
|
|
52
|
+
maxEntities: override.maxEntities ?? base.maxEntities,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Combine property filter configurations
|
|
57
|
+
*/
|
|
58
|
+
function combinePropertyFilters(base, override) {
|
|
59
|
+
if (!base && !override)
|
|
60
|
+
return undefined;
|
|
61
|
+
if (!base)
|
|
62
|
+
return override;
|
|
63
|
+
if (!override)
|
|
64
|
+
return base;
|
|
65
|
+
return [...base, ...override];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Combine arrays, handling undefined values
|
|
69
|
+
*/
|
|
70
|
+
function combineArrays(base, override) {
|
|
71
|
+
if (!base && !override)
|
|
72
|
+
return undefined;
|
|
73
|
+
if (!base)
|
|
74
|
+
return override;
|
|
75
|
+
if (!override)
|
|
76
|
+
return base;
|
|
77
|
+
return [...base, ...override];
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Convert property filter by ID rules to PropertyFilterConfig
|
|
81
|
+
*/
|
|
82
|
+
export function convertPropertyFiltersByIds(rules) {
|
|
83
|
+
return rules.flatMap((rule) => rule.entityIds.map((entityId) => ({
|
|
84
|
+
selector: { '@id': entityId },
|
|
85
|
+
include: rule.include,
|
|
86
|
+
exclude: rule.exclude,
|
|
87
|
+
})));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Convert property filter by type rules to PropertyFilterConfig
|
|
91
|
+
*/
|
|
92
|
+
export function convertPropertyFiltersByTypes(rules) {
|
|
93
|
+
return rules.flatMap((rule) => rule.entityTypes.map((entityType) => ({
|
|
94
|
+
selector: { '@type': entityType },
|
|
95
|
+
include: rule.include,
|
|
96
|
+
exclude: rule.exclude,
|
|
97
|
+
})));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Build complete property filter configuration from all sources
|
|
101
|
+
*/
|
|
102
|
+
export function buildPropertyFilterConfig(config) {
|
|
103
|
+
const filters = [];
|
|
104
|
+
// Add base property filters
|
|
105
|
+
if (config.propertyFilters) {
|
|
106
|
+
filters.push(...config.propertyFilters);
|
|
107
|
+
}
|
|
108
|
+
// Add property filters by IDs
|
|
109
|
+
if (config.propertyFiltersByIds) {
|
|
110
|
+
filters.push(...convertPropertyFiltersByIds(config.propertyFiltersByIds));
|
|
111
|
+
}
|
|
112
|
+
// Add property filters by types
|
|
113
|
+
if (config.propertyFiltersByTypes) {
|
|
114
|
+
filters.push(...convertPropertyFiltersByTypes(config.propertyFiltersByTypes));
|
|
115
|
+
}
|
|
116
|
+
return filters.length > 0 ? filters : undefined;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Validate configuration for common issues
|
|
120
|
+
*/
|
|
121
|
+
export function validateConfig(config) {
|
|
122
|
+
const errors = [];
|
|
123
|
+
// Check for conflicting include/exclude types
|
|
124
|
+
if (config.filters?.includeTypes && config.filters?.excludeTypes) {
|
|
125
|
+
const overlap = config.filters.includeTypes.filter((type) => config.filters.excludeTypes.includes(type));
|
|
126
|
+
if (overlap.length > 0) {
|
|
127
|
+
errors.push(`Conflicting include/exclude types: ${overlap.join(', ')}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Check for conflicting include/exclude IDs
|
|
131
|
+
if (config.filters?.includeIds && config.filters?.excludeIds) {
|
|
132
|
+
const overlap = config.filters.includeIds.filter((id) => config.filters.excludeIds.includes(id));
|
|
133
|
+
if (overlap.length > 0) {
|
|
134
|
+
errors.push(`Conflicting include/exclude IDs: ${overlap.join(', ')}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Check for invalid maxEntities
|
|
138
|
+
if (config.filters?.maxEntities !== undefined && config.filters.maxEntities < 1) {
|
|
139
|
+
errors.push('maxEntities must be greater than 0');
|
|
140
|
+
}
|
|
141
|
+
return errors;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Apply populate configuration to entities in the graph
|
|
145
|
+
*/
|
|
146
|
+
export function applyPopulateConfig(graph, populateConfig) {
|
|
147
|
+
return graph.map((entity) => {
|
|
148
|
+
const entityId = entity['@id'];
|
|
149
|
+
const populateRules = populateConfig[entityId];
|
|
150
|
+
if (!populateRules || populateRules.length === 0) {
|
|
151
|
+
return entity;
|
|
152
|
+
}
|
|
153
|
+
// Create a copy of the entity to avoid mutation
|
|
154
|
+
const populatedEntity = { ...entity };
|
|
155
|
+
// Apply each populate rule
|
|
156
|
+
for (const rule of populateRules) {
|
|
157
|
+
populatedEntity[rule.property] = rule.entities;
|
|
158
|
+
}
|
|
159
|
+
return populatedEntity;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Validate runtime configuration for critical errors only
|
|
164
|
+
* This is more lenient than the general validateConfig to handle runtime overrides
|
|
165
|
+
*/
|
|
166
|
+
export function validateRuntimeConfig(config) {
|
|
167
|
+
const errors = [];
|
|
168
|
+
// Check for baseGraph requirement
|
|
169
|
+
if (!config.baseGraph) {
|
|
170
|
+
errors.push('baseGraph is required');
|
|
171
|
+
}
|
|
172
|
+
else if (!Array.isArray(config.baseGraph)) {
|
|
173
|
+
errors.push('baseGraph must be an array');
|
|
174
|
+
}
|
|
175
|
+
// Only check for critical errors that would break processing
|
|
176
|
+
if (config.filters?.maxEntities !== undefined && config.filters.maxEntities < 1) {
|
|
177
|
+
errors.push('maxEntities must be greater than 0');
|
|
178
|
+
}
|
|
179
|
+
return errors;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Type guard to check if an entity matches a type
|
|
183
|
+
*/
|
|
184
|
+
export function entityHasType(entity, type) {
|
|
185
|
+
const entityType = entity['@type'];
|
|
186
|
+
if (!entityType)
|
|
187
|
+
return false;
|
|
188
|
+
if (Array.isArray(entityType)) {
|
|
189
|
+
return entityType.includes(type);
|
|
190
|
+
}
|
|
191
|
+
return entityType === type;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Type guard to check if an entity has any of the specified types
|
|
195
|
+
*/
|
|
196
|
+
export function entityHasAnyType(entity, types) {
|
|
197
|
+
return types.some((type) => entityHasType(entity, type));
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get all types from an entity as an array
|
|
201
|
+
*/
|
|
202
|
+
export function getEntityTypes(entity) {
|
|
203
|
+
const entityType = entity['@type'];
|
|
204
|
+
if (!entityType)
|
|
205
|
+
return [];
|
|
206
|
+
return Array.isArray(entityType) ? entityType : [entityType];
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Apply filters to a JSON-LD graph (copied from head-manager/filters.ts for self-containment)
|
|
210
|
+
*/
|
|
211
|
+
export function filterJsonLdGraph(graph, options) {
|
|
212
|
+
let filtered = [...graph];
|
|
213
|
+
// Apply type inclusion filter
|
|
214
|
+
if (options.includeTypes && options.includeTypes.length > 0) {
|
|
215
|
+
filtered = filtered.filter((entity) => {
|
|
216
|
+
const type = entity['@type'];
|
|
217
|
+
if (!type)
|
|
218
|
+
return false;
|
|
219
|
+
const types = Array.isArray(type) ? type : [type];
|
|
220
|
+
return types.some((t) => options.includeTypes.includes(t));
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
// Apply type exclusion filter
|
|
224
|
+
if (options.excludeTypes && options.excludeTypes.length > 0) {
|
|
225
|
+
filtered = filtered.filter((entity) => {
|
|
226
|
+
const type = entity['@type'];
|
|
227
|
+
if (!type)
|
|
228
|
+
return true;
|
|
229
|
+
const types = Array.isArray(type) ? type : [type];
|
|
230
|
+
return !types.some((t) => options.excludeTypes.includes(t));
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
// Apply ID inclusion filter
|
|
234
|
+
if (options.includeIds && options.includeIds.length > 0) {
|
|
235
|
+
filtered = filtered.filter((entity) => entity['@id'] && options.includeIds.includes(entity['@id']));
|
|
236
|
+
}
|
|
237
|
+
// Apply ID exclusion filter
|
|
238
|
+
if (options.excludeIds && options.excludeIds.length > 0) {
|
|
239
|
+
filtered = filtered.filter((entity) => !entity['@id'] || !options.excludeIds.includes(entity['@id']));
|
|
240
|
+
}
|
|
241
|
+
// Apply required properties filter
|
|
242
|
+
if (options.requiredProperties && options.requiredProperties.length > 0) {
|
|
243
|
+
filtered = filtered.filter((entity) => options.requiredProperties.every((prop) => prop in entity));
|
|
244
|
+
}
|
|
245
|
+
// Apply excluded properties filter (handle both old and new property names)
|
|
246
|
+
const excludeProps = options.excludeEntitiesWithProperties || options.excludeProperties;
|
|
247
|
+
if (excludeProps && excludeProps.length > 0) {
|
|
248
|
+
filtered = filtered.filter((entity) => !excludeProps.some((prop) => prop in entity));
|
|
249
|
+
}
|
|
250
|
+
// Apply custom filter
|
|
251
|
+
if (options.customFilter) {
|
|
252
|
+
filtered = filtered.filter(options.customFilter);
|
|
253
|
+
}
|
|
254
|
+
// Apply max entities limit
|
|
255
|
+
if (options.maxEntities && options.maxEntities > 0) {
|
|
256
|
+
filtered = filtered.slice(0, options.maxEntities);
|
|
257
|
+
}
|
|
258
|
+
return filtered;
|
|
259
|
+
}
|
package/esm/builder.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
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, mergeConfigs, 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
|
+
* Apply a pre-built configuration (builder-specific method)
|
|
26
|
+
*/
|
|
27
|
+
applyConfig(config) {
|
|
28
|
+
const currentConfig = this.getConfig();
|
|
29
|
+
const mergedConfig = mergeConfigs(currentConfig, config);
|
|
30
|
+
return new JsonLdBuilder(mergedConfig);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the current graph state (processes the graph if not already done)
|
|
34
|
+
*/
|
|
35
|
+
getCurrentGraph() {
|
|
36
|
+
if (!this.currentGraph) {
|
|
37
|
+
this.currentGraph = this.processGraph();
|
|
38
|
+
}
|
|
39
|
+
return this.currentGraph;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Build the final JSON-LD output
|
|
43
|
+
* @param options - Build configuration options
|
|
44
|
+
* @param options.prettyPrint - Whether to format JSON with indentation (default: true)
|
|
45
|
+
* @param options.contextUrl - The JSON-LD context URL (default: 'https://schema.org')
|
|
46
|
+
* @param options.withScriptTag - Whether to wrap output in a script tag (default: false)
|
|
47
|
+
* @param options.scriptId - Optional ID attribute for the script tag
|
|
48
|
+
* @returns The JSON-LD string, optionally wrapped in a script tag
|
|
49
|
+
*/
|
|
50
|
+
build(options = {}) {
|
|
51
|
+
const { prettyPrint = true, contextUrl = 'https://schema.org', withScriptTag = false, scriptId, } = options;
|
|
52
|
+
const graph = this.getCurrentGraph();
|
|
53
|
+
// Create the JSON-LD object
|
|
54
|
+
const jsonLd = {
|
|
55
|
+
'@context': contextUrl,
|
|
56
|
+
'@graph': graph,
|
|
57
|
+
};
|
|
58
|
+
// Stringify with optional pretty printing
|
|
59
|
+
const jsonString = JSON.stringify(jsonLd, null, prettyPrint ? 2 : undefined);
|
|
60
|
+
// Wrap in script tag if requested
|
|
61
|
+
if (withScriptTag) {
|
|
62
|
+
const idAttribute = scriptId ? ` id="${scriptId}"` : '';
|
|
63
|
+
return `<script${idAttribute} type="application/ld+json">${jsonString}</script>`;
|
|
64
|
+
}
|
|
65
|
+
return jsonString;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Process the graph according to the current configuration
|
|
69
|
+
* This implements the critical filtering logic from the current manager
|
|
70
|
+
*/
|
|
71
|
+
processGraph() {
|
|
72
|
+
const config = this.getConfig();
|
|
73
|
+
if (!config.baseGraph) {
|
|
74
|
+
throw new Error('No base graph provided. Use baseGraph() or applyConfig() with a baseGraph.');
|
|
75
|
+
}
|
|
76
|
+
// Validate configuration - only check for critical errors that would break processing
|
|
77
|
+
const errors = validateRuntimeConfig(config);
|
|
78
|
+
if (errors.length > 0) {
|
|
79
|
+
throw new Error(`Configuration validation failed: ${errors.join(', ')}`);
|
|
80
|
+
}
|
|
81
|
+
let graph = [...config.baseGraph];
|
|
82
|
+
const propertyFilters = buildPropertyFilterConfig(config);
|
|
83
|
+
let propertyFiltersApplied = false;
|
|
84
|
+
// Layer 1: Subgraph extraction with property filtering if subgraphRoots are set
|
|
85
|
+
if (config.subgraphRoots && config.subgraphRoots.length > 0) {
|
|
86
|
+
// Extract subgraph with property filtering during traversal
|
|
87
|
+
graph = extractSubgraphs(graph, config.subgraphRoots, propertyFilters);
|
|
88
|
+
propertyFiltersApplied = true; // Property filters were applied during subgraph extraction
|
|
89
|
+
}
|
|
90
|
+
// Layer 2: Regular entity filtering with property filtering if not already applied
|
|
91
|
+
if (config.filters) {
|
|
92
|
+
// Apply property filters first if they haven't been applied yet
|
|
93
|
+
if (!propertyFiltersApplied && propertyFilters) {
|
|
94
|
+
graph = filterGraphProperties(graph, propertyFilters);
|
|
95
|
+
propertyFiltersApplied = true;
|
|
96
|
+
}
|
|
97
|
+
// Apply entity-level filters
|
|
98
|
+
graph = filterJsonLdGraph(graph, config.filters);
|
|
99
|
+
}
|
|
100
|
+
// Apply entity population if configured
|
|
101
|
+
if (config.populateConfig) {
|
|
102
|
+
graph = applyPopulateConfig(graph, config.populateConfig);
|
|
103
|
+
}
|
|
104
|
+
// Add additional entities if specified
|
|
105
|
+
if (config.additionalEntities && config.additionalEntities.length > 0) {
|
|
106
|
+
graph = [...graph, ...config.additionalEntities];
|
|
107
|
+
}
|
|
108
|
+
// Apply custom transformation pipes
|
|
109
|
+
if (config.pipes && config.pipes.length > 0) {
|
|
110
|
+
for (const pipe of config.pipes) {
|
|
111
|
+
graph = pipe(graph);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return graph;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Factory function to create a new builder
|
|
119
|
+
*/
|
|
120
|
+
export function createJsonLdBuilder() {
|
|
121
|
+
return new JsonLdBuilder();
|
|
122
|
+
}
|
package/esm/config.js
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Immutable configuration builder class
|
|
10
|
+
*/
|
|
11
|
+
export class JsonLdConfigBuilder {
|
|
12
|
+
constructor(initialConfig = {}) {
|
|
13
|
+
this.config = { ...initialConfig };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a new instance of this class (can be overridden by subclasses)
|
|
17
|
+
*/
|
|
18
|
+
createInstance(config) {
|
|
19
|
+
return new JsonLdConfigBuilder(config);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Set the base graph to process
|
|
23
|
+
*/
|
|
24
|
+
baseGraph(graph) {
|
|
25
|
+
return this.createInstance({
|
|
26
|
+
...this.config,
|
|
27
|
+
baseGraph: graph,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Include only entities with these IDs (merges with existing IDs)
|
|
32
|
+
*/
|
|
33
|
+
includeIds(ids) {
|
|
34
|
+
const existingIds = this.config.filters?.includeIds || [];
|
|
35
|
+
return this.createInstance({
|
|
36
|
+
...this.config,
|
|
37
|
+
filters: {
|
|
38
|
+
...this.config.filters,
|
|
39
|
+
includeIds: [...existingIds, ...ids],
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Exclude entities with these IDs (merges with existing IDs)
|
|
45
|
+
*/
|
|
46
|
+
excludeIds(ids) {
|
|
47
|
+
const existingIds = this.config.filters?.excludeIds || [];
|
|
48
|
+
return this.createInstance({
|
|
49
|
+
...this.config,
|
|
50
|
+
filters: {
|
|
51
|
+
...this.config.filters,
|
|
52
|
+
excludeIds: [...existingIds, ...ids],
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Include only these entity types (merges with existing types)
|
|
58
|
+
*/
|
|
59
|
+
includeTypes(types) {
|
|
60
|
+
const existingTypes = this.config.filters?.includeTypes || [];
|
|
61
|
+
return this.createInstance({
|
|
62
|
+
...this.config,
|
|
63
|
+
filters: {
|
|
64
|
+
...this.config.filters,
|
|
65
|
+
includeTypes: [...existingTypes, ...types],
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Exclude these entity types (merges with existing types)
|
|
71
|
+
*/
|
|
72
|
+
excludeTypes(types) {
|
|
73
|
+
const existingTypes = this.config.filters?.excludeTypes || [];
|
|
74
|
+
return this.createInstance({
|
|
75
|
+
...this.config,
|
|
76
|
+
filters: {
|
|
77
|
+
...this.config.filters,
|
|
78
|
+
excludeTypes: [...existingTypes, ...types],
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Apply custom filter function
|
|
84
|
+
*/
|
|
85
|
+
customFilter(fn) {
|
|
86
|
+
return this.createInstance({
|
|
87
|
+
...this.config,
|
|
88
|
+
filters: {
|
|
89
|
+
...this.config.filters,
|
|
90
|
+
customFilter: fn,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Limit maximum number of entities
|
|
96
|
+
*/
|
|
97
|
+
maxEntities(max) {
|
|
98
|
+
return this.createInstance({
|
|
99
|
+
...this.config,
|
|
100
|
+
filters: {
|
|
101
|
+
...this.config.filters,
|
|
102
|
+
maxEntities: max,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Include only entities with these properties (merges with existing properties)
|
|
108
|
+
* @param props - Array of property names that entities must have
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* config.requiredProperties(['name']).requiredProperties(['url'])
|
|
112
|
+
* // Result: entities must have both 'name' and 'url' properties
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
requiredProperties(props) {
|
|
116
|
+
const existingProps = this.config.filters?.requiredProperties || [];
|
|
117
|
+
return this.createInstance({
|
|
118
|
+
...this.config,
|
|
119
|
+
filters: {
|
|
120
|
+
...this.config.filters,
|
|
121
|
+
requiredProperties: [...existingProps, ...props],
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Exclude entities with these properties (merges with existing properties)
|
|
127
|
+
*/
|
|
128
|
+
excludeEntitiesWithProperties(props) {
|
|
129
|
+
const existingProps = this.config.filters?.excludeEntitiesWithProperties || [];
|
|
130
|
+
return this.createInstance({
|
|
131
|
+
...this.config,
|
|
132
|
+
filters: {
|
|
133
|
+
...this.config.filters,
|
|
134
|
+
excludeEntitiesWithProperties: [...existingProps, ...props],
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Extract subgraph starting from these root IDs
|
|
140
|
+
*/
|
|
141
|
+
subgraph(rootIds) {
|
|
142
|
+
return this.createInstance({
|
|
143
|
+
...this.config,
|
|
144
|
+
subgraphRoots: [...(this.config?.subgraphRoots || []), ...rootIds],
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Filter properties for specific entity IDs
|
|
149
|
+
*/
|
|
150
|
+
filterPropertiesByIds(entityIds, rule) {
|
|
151
|
+
const existingRules = this.config.propertyFiltersByIds || [];
|
|
152
|
+
const newRule = {
|
|
153
|
+
entityIds,
|
|
154
|
+
...rule,
|
|
155
|
+
};
|
|
156
|
+
return this.createInstance({
|
|
157
|
+
...this.config,
|
|
158
|
+
propertyFiltersByIds: [...existingRules, newRule],
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Filter properties for specific entity types
|
|
163
|
+
*/
|
|
164
|
+
filterPropertiesByTypes(entityTypes, rule) {
|
|
165
|
+
const existingRules = this.config.propertyFiltersByTypes || [];
|
|
166
|
+
const newRule = {
|
|
167
|
+
entityTypes,
|
|
168
|
+
...rule,
|
|
169
|
+
};
|
|
170
|
+
return this.createInstance({
|
|
171
|
+
...this.config,
|
|
172
|
+
propertyFiltersByTypes: [...existingRules, newRule],
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Add additional entities
|
|
177
|
+
*/
|
|
178
|
+
addEntities(entities) {
|
|
179
|
+
const existingEntities = this.config.additionalEntities || [];
|
|
180
|
+
return this.createInstance({
|
|
181
|
+
...this.config,
|
|
182
|
+
additionalEntities: [...existingEntities, ...entities],
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Add custom transformation pipe
|
|
187
|
+
*/
|
|
188
|
+
pipe(fn) {
|
|
189
|
+
const existingPipes = this.config.pipes || [];
|
|
190
|
+
return this.createInstance({
|
|
191
|
+
...this.config,
|
|
192
|
+
pipes: [...existingPipes, fn],
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Configure entity population
|
|
197
|
+
*/
|
|
198
|
+
populateEntities(entityIds, populateConfig) {
|
|
199
|
+
// Merge with existing populate config
|
|
200
|
+
const existingPopulateConfig = this.config.populateConfig || {};
|
|
201
|
+
const mergedPopulateConfig = { ...existingPopulateConfig };
|
|
202
|
+
// Add the new populate config for the specified entity IDs
|
|
203
|
+
for (const entityId of entityIds) {
|
|
204
|
+
if (populateConfig[entityId]) {
|
|
205
|
+
mergedPopulateConfig[entityId] = populateConfig[entityId];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return this.createInstance({
|
|
209
|
+
...this.config,
|
|
210
|
+
populateConfig: mergedPopulateConfig,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Clear both includeIds and excludeIds
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* config.includeIds(['a']).excludeIds(['b']).clearIds().includeIds(['c'])
|
|
218
|
+
* // Result: only includes 'c', previous IDs are cleared
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
clearIds() {
|
|
222
|
+
return this.createInstance({
|
|
223
|
+
...this.config,
|
|
224
|
+
filters: {
|
|
225
|
+
...this.config.filters,
|
|
226
|
+
includeIds: undefined,
|
|
227
|
+
excludeIds: undefined,
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Clear both includeTypes and excludeTypes
|
|
233
|
+
*/
|
|
234
|
+
clearTypes() {
|
|
235
|
+
return this.createInstance({
|
|
236
|
+
...this.config,
|
|
237
|
+
filters: {
|
|
238
|
+
...this.config.filters,
|
|
239
|
+
includeTypes: undefined,
|
|
240
|
+
excludeTypes: undefined,
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Clear both requiredProperties and excludeEntitiesWithProperties
|
|
246
|
+
*/
|
|
247
|
+
clearPropertyRequirements() {
|
|
248
|
+
return this.createInstance({
|
|
249
|
+
...this.config,
|
|
250
|
+
filters: {
|
|
251
|
+
...this.config.filters,
|
|
252
|
+
requiredProperties: undefined,
|
|
253
|
+
excludeEntitiesWithProperties: undefined,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Clear both propertyFiltersByIds and propertyFiltersByTypes
|
|
259
|
+
*/
|
|
260
|
+
clearPropertyFilters() {
|
|
261
|
+
return this.createInstance({
|
|
262
|
+
...this.config,
|
|
263
|
+
propertyFiltersByIds: undefined,
|
|
264
|
+
propertyFiltersByTypes: undefined,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Clear subgraphRoots
|
|
269
|
+
*/
|
|
270
|
+
clearSubgraph() {
|
|
271
|
+
return this.createInstance({
|
|
272
|
+
...this.config,
|
|
273
|
+
subgraphRoots: undefined,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Clear entire configuration except baseGraph
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* config.baseGraph(graph).includeIds(['a']).clearAll().includeTypes(['Person'])
|
|
281
|
+
* // Result: only baseGraph and includeTypes remain
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
clearAll() {
|
|
285
|
+
return this.createInstance({
|
|
286
|
+
baseGraph: this.config.baseGraph,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Get the current configuration
|
|
291
|
+
*/
|
|
292
|
+
getConfig() {
|
|
293
|
+
return { ...this.config };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Factory function to create a new configuration builder
|
|
298
|
+
*/
|
|
299
|
+
export function createJsonLdConfig() {
|
|
300
|
+
return new JsonLdConfigBuilder();
|
|
301
|
+
}
|