jsonldjs 0.1.1 → 0.2.0
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/LICENSE +1 -1
- package/README.md +35 -5
- package/builder-utils.d.ts +4 -0
- package/builder-utils.js +11 -14
- package/builder.d.ts +0 -4
- package/builder.js +4 -11
- package/config.d.ts +11 -1
- package/config.js +47 -8
- package/esm/builder-utils.js +11 -15
- package/esm/builder.js +5 -12
- package/esm/config.js +47 -8
- package/package.json +12 -11
- package/types.d.ts +12 -8
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2025
|
|
3
|
+
Copyright (c) 2025 Interweb, Inc. <developers@interweb.co>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -63,7 +63,7 @@ const blogConfig = globalConfig.includeTypes(['Article']);
|
|
|
63
63
|
|
|
64
64
|
// Use configurations
|
|
65
65
|
const result = createJsonLdBuilder()
|
|
66
|
-
.
|
|
66
|
+
.mergeConfig(homeConfig.getConfig())
|
|
67
67
|
.excludeIds(['runtime:override']) // Runtime overrides
|
|
68
68
|
.build({ prettyPrint: true });
|
|
69
69
|
```
|
|
@@ -122,7 +122,7 @@ const config = createJsonLdConfig()
|
|
|
122
122
|
Creates a new builder that extends the configuration builder with graph processing capabilities.
|
|
123
123
|
|
|
124
124
|
```typescript
|
|
125
|
-
const builder = createJsonLdBuilder().baseGraph(graph).
|
|
125
|
+
const builder = createJsonLdBuilder().baseGraph(graph).mergeConfig(config);
|
|
126
126
|
```
|
|
127
127
|
|
|
128
128
|
### Configuration Methods
|
|
@@ -149,6 +149,37 @@ All methods are inherited by the builder from the configuration builder:
|
|
|
149
149
|
- `.clearSubgraph()` - Clear subgraphRoots
|
|
150
150
|
- `.clearAll()` - Clear entire configuration (except baseGraph)
|
|
151
151
|
|
|
152
|
+
#### Configuration Merging
|
|
153
|
+
|
|
154
|
+
- `.mergeConfig(config: JsonLdConfig)` - Merge with another complete configuration
|
|
155
|
+
- `.mergeFilters(filters: JsonLdFilterOptions)` - Merge only the filters part of another configuration
|
|
156
|
+
|
|
157
|
+
**Available in both config builder and main builder** - These methods work the same way in both classes.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// Config builder usage
|
|
161
|
+
const baseConfig = createJsonLdConfig().includeTypes(['Person']);
|
|
162
|
+
const otherConfig = createJsonLdConfig()
|
|
163
|
+
.includeTypes(['Organization'])
|
|
164
|
+
.excludeIds(['test'])
|
|
165
|
+
.getConfig();
|
|
166
|
+
const merged = baseConfig.mergeConfig(otherConfig);
|
|
167
|
+
// Result: includeTypes: ['Person', 'Organization'], excludeIds: ['test']
|
|
168
|
+
|
|
169
|
+
// Main builder usage (processes graph immediately)
|
|
170
|
+
const result = createJsonLdBuilder()
|
|
171
|
+
.baseGraph(graph)
|
|
172
|
+
.includeTypes(['Person'])
|
|
173
|
+
.mergeConfig(otherConfig)
|
|
174
|
+
.build({ prettyPrint: true });
|
|
175
|
+
|
|
176
|
+
// Merge only filters
|
|
177
|
+
const baseConfig = createJsonLdConfig().includeTypes(['Person']).addEntities([entity]);
|
|
178
|
+
const otherFilters = { includeTypes: ['Organization'], maxEntities: 10 };
|
|
179
|
+
const merged = baseConfig.mergeFilters(otherFilters);
|
|
180
|
+
// Result: includeTypes: ['Person', 'Organization'], maxEntities: 10, additionalEntities preserved
|
|
181
|
+
```
|
|
182
|
+
|
|
152
183
|
#### Property Filtering
|
|
153
184
|
|
|
154
185
|
- `.filterPropertiesByIds(entityIds, rule)` - Filter properties for specific entity IDs
|
|
@@ -175,7 +206,6 @@ All methods are inherited by the builder from the configuration builder:
|
|
|
175
206
|
|
|
176
207
|
#### Builder-Only Methods
|
|
177
208
|
|
|
178
|
-
- `.applyConfig(config: JsonLdConfig)` - Apply a pre-built configuration
|
|
179
209
|
- `.getCurrentGraph()` - Get the current graph state
|
|
180
210
|
- `.build(options?: BuildOptions)` - Build the final JSON-LD output
|
|
181
211
|
|
|
@@ -358,8 +388,8 @@ const result = createJsonLdBuilder()
|
|
|
358
388
|
1. Clone the repository:
|
|
359
389
|
|
|
360
390
|
```bash
|
|
361
|
-
git clone https://github.com/hyperweb-io/
|
|
362
|
-
cd jsonld-tools
|
|
391
|
+
git clone https://github.com/hyperweb-io/dev-utils.git
|
|
392
|
+
cd dev-utils/packages/jsonld-tools
|
|
363
393
|
```
|
|
364
394
|
|
|
365
395
|
2. Install dependencies:
|
package/builder-utils.d.ts
CHANGED
|
@@ -10,6 +10,10 @@ import type { JsonLdConfig, JsonLdFilterOptions, PopulateConfig, PropertyFilterB
|
|
|
10
10
|
* Merge two configurations, with the second taking precedence
|
|
11
11
|
*/
|
|
12
12
|
export declare function mergeConfigs(base: JsonLdConfig, override: JsonLdConfig): JsonLdConfig;
|
|
13
|
+
/**
|
|
14
|
+
* Merge filter options, merging arrays and using override for single values
|
|
15
|
+
*/
|
|
16
|
+
export declare function mergeFilterOptions(base?: JsonLdFilterOptions, override?: JsonLdFilterOptions): JsonLdFilterOptions | undefined;
|
|
13
17
|
/**
|
|
14
18
|
* Convert property filter by ID rules to PropertyFilterConfig
|
|
15
19
|
*/
|
package/builder-utils.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.mergeConfigs = mergeConfigs;
|
|
10
|
+
exports.mergeFilterOptions = mergeFilterOptions;
|
|
10
11
|
exports.convertPropertyFiltersByIds = convertPropertyFiltersByIds;
|
|
11
12
|
exports.convertPropertyFiltersByTypes = convertPropertyFiltersByTypes;
|
|
12
13
|
exports.buildPropertyFilterConfig = buildPropertyFilterConfig;
|
|
@@ -26,14 +27,6 @@ function mergeConfigs(base, override) {
|
|
|
26
27
|
baseGraph: override.baseGraph ?? base.baseGraph,
|
|
27
28
|
// Filters: merge filter options
|
|
28
29
|
filters: mergeFilterOptions(base.filters, override.filters),
|
|
29
|
-
// Subgraph roots: override takes precedence
|
|
30
|
-
subgraphRoots: override.subgraphRoots ?? base.subgraphRoots,
|
|
31
|
-
// Property filters: combine arrays
|
|
32
|
-
propertyFilters: combinePropertyFilters(base.propertyFilters, override.propertyFilters),
|
|
33
|
-
// Property filters by IDs: combine arrays
|
|
34
|
-
propertyFiltersByIds: combineArrays(base.propertyFiltersByIds, override.propertyFiltersByIds),
|
|
35
|
-
// Property filters by types: combine arrays
|
|
36
|
-
propertyFiltersByTypes: combineArrays(base.propertyFiltersByTypes, override.propertyFiltersByTypes),
|
|
37
30
|
// Additional entities: combine arrays
|
|
38
31
|
additionalEntities: combineArrays(base.additionalEntities, override.additionalEntities),
|
|
39
32
|
// Pipes: combine arrays
|
|
@@ -60,6 +53,10 @@ function mergeFilterOptions(base, override) {
|
|
|
60
53
|
excludeIds: combineArrays(base.excludeIds, override.excludeIds),
|
|
61
54
|
requiredProperties: combineArrays(base.requiredProperties, override.requiredProperties),
|
|
62
55
|
excludeEntitiesWithProperties: combineArrays(base.excludeEntitiesWithProperties, override.excludeEntitiesWithProperties),
|
|
56
|
+
subgraphRoots: combineArrays(base.subgraphRoots, override.subgraphRoots),
|
|
57
|
+
propertyFilters: combinePropertyFilters(base.propertyFilters, override.propertyFilters),
|
|
58
|
+
propertyFiltersByIds: combineArrays(base.propertyFiltersByIds, override.propertyFiltersByIds),
|
|
59
|
+
propertyFiltersByTypes: combineArrays(base.propertyFiltersByTypes, override.propertyFiltersByTypes),
|
|
63
60
|
// Single values - override takes precedence
|
|
64
61
|
customFilter: override.customFilter ?? base.customFilter,
|
|
65
62
|
maxEntities: override.maxEntities ?? base.maxEntities,
|
|
@@ -115,16 +112,16 @@ function convertPropertyFiltersByTypes(rules) {
|
|
|
115
112
|
function buildPropertyFilterConfig(config) {
|
|
116
113
|
const filters = [];
|
|
117
114
|
// Add base property filters
|
|
118
|
-
if (config.propertyFilters) {
|
|
119
|
-
filters.push(...config.propertyFilters);
|
|
115
|
+
if (config.filters?.propertyFilters) {
|
|
116
|
+
filters.push(...config.filters.propertyFilters);
|
|
120
117
|
}
|
|
121
118
|
// Add property filters by IDs
|
|
122
|
-
if (config.propertyFiltersByIds) {
|
|
123
|
-
filters.push(...convertPropertyFiltersByIds(config.propertyFiltersByIds));
|
|
119
|
+
if (config.filters?.propertyFiltersByIds) {
|
|
120
|
+
filters.push(...convertPropertyFiltersByIds(config.filters.propertyFiltersByIds));
|
|
124
121
|
}
|
|
125
122
|
// Add property filters by types
|
|
126
|
-
if (config.propertyFiltersByTypes) {
|
|
127
|
-
filters.push(...convertPropertyFiltersByTypes(config.propertyFiltersByTypes));
|
|
123
|
+
if (config.filters?.propertyFiltersByTypes) {
|
|
124
|
+
filters.push(...convertPropertyFiltersByTypes(config.filters.propertyFiltersByTypes));
|
|
128
125
|
}
|
|
129
126
|
return filters.length > 0 ? filters : undefined;
|
|
130
127
|
}
|
package/builder.d.ts
CHANGED
|
@@ -18,10 +18,6 @@ export declare class JsonLdBuilder extends JsonLdConfigBuilder {
|
|
|
18
18
|
* Override the config builder's method creation to return JsonLdBuilder instances
|
|
19
19
|
*/
|
|
20
20
|
protected createInstance(config: JsonLdConfig): JsonLdBuilder;
|
|
21
|
-
/**
|
|
22
|
-
* Apply a pre-built configuration (builder-specific method)
|
|
23
|
-
*/
|
|
24
|
-
applyConfig(config: JsonLdConfig): JsonLdBuilder;
|
|
25
21
|
/**
|
|
26
22
|
* Get the current graph state (processes the graph if not already done)
|
|
27
23
|
*/
|
package/builder.js
CHANGED
|
@@ -16,6 +16,7 @@ const builder_utils_1 = require("./builder-utils");
|
|
|
16
16
|
* Mutable builder class that extends config builder with automatic method delegation
|
|
17
17
|
*/
|
|
18
18
|
class JsonLdBuilder extends config_1.JsonLdConfigBuilder {
|
|
19
|
+
currentGraph;
|
|
19
20
|
constructor(initialConfig = {}) {
|
|
20
21
|
super(initialConfig);
|
|
21
22
|
}
|
|
@@ -25,14 +26,6 @@ class JsonLdBuilder extends config_1.JsonLdConfigBuilder {
|
|
|
25
26
|
createInstance(config) {
|
|
26
27
|
return new JsonLdBuilder(config);
|
|
27
28
|
}
|
|
28
|
-
/**
|
|
29
|
-
* Apply a pre-built configuration (builder-specific method)
|
|
30
|
-
*/
|
|
31
|
-
applyConfig(config) {
|
|
32
|
-
const currentConfig = this.getConfig();
|
|
33
|
-
const mergedConfig = (0, builder_utils_1.mergeConfigs)(currentConfig, config);
|
|
34
|
-
return new JsonLdBuilder(mergedConfig);
|
|
35
|
-
}
|
|
36
29
|
/**
|
|
37
30
|
* Get the current graph state (processes the graph if not already done)
|
|
38
31
|
*/
|
|
@@ -75,7 +68,7 @@ class JsonLdBuilder extends config_1.JsonLdConfigBuilder {
|
|
|
75
68
|
processGraph() {
|
|
76
69
|
const config = this.getConfig();
|
|
77
70
|
if (!config.baseGraph) {
|
|
78
|
-
throw new Error('No base graph provided. Use baseGraph() or
|
|
71
|
+
throw new Error('No base graph provided. Use baseGraph() or mergeConfig() with a baseGraph.');
|
|
79
72
|
}
|
|
80
73
|
// Validate configuration - only check for critical errors that would break processing
|
|
81
74
|
const errors = (0, builder_utils_1.validateRuntimeConfig)(config);
|
|
@@ -86,9 +79,9 @@ class JsonLdBuilder extends config_1.JsonLdConfigBuilder {
|
|
|
86
79
|
const propertyFilters = (0, builder_utils_1.buildPropertyFilterConfig)(config);
|
|
87
80
|
let propertyFiltersApplied = false;
|
|
88
81
|
// Layer 1: Subgraph extraction with property filtering if subgraphRoots are set
|
|
89
|
-
if (config.subgraphRoots && config.subgraphRoots.length > 0) {
|
|
82
|
+
if (config.filters?.subgraphRoots && config.filters.subgraphRoots.length > 0) {
|
|
90
83
|
// Extract subgraph with property filtering during traversal
|
|
91
|
-
graph = (0, jsonld_utils_1.extractSubgraphs)(graph, config.subgraphRoots, propertyFilters);
|
|
84
|
+
graph = (0, jsonld_utils_1.extractSubgraphs)(graph, config.filters.subgraphRoots, propertyFilters);
|
|
92
85
|
propertyFiltersApplied = true; // Property filters were applied during subgraph extraction
|
|
93
86
|
}
|
|
94
87
|
// Layer 2: Regular entity filtering with property filtering if not already applied
|
package/config.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* performing any graph processing. Each method returns a new
|
|
6
6
|
* configuration object for safe reuse and extension.
|
|
7
7
|
*/
|
|
8
|
-
import type { IJsonLdConfigBuilder, JsonLdConfig, JsonLdEntity, JsonLdFilter, JsonLdGraph, PipeFunction, PopulateConfig } from './types';
|
|
8
|
+
import type { IJsonLdConfigBuilder, JsonLdConfig, JsonLdEntity, JsonLdFilter, JsonLdFilterOptions, JsonLdGraph, PipeFunction, PopulateConfig } from './types';
|
|
9
9
|
/**
|
|
10
10
|
* Immutable configuration builder class
|
|
11
11
|
*/
|
|
@@ -122,6 +122,16 @@ export declare class JsonLdConfigBuilder implements IJsonLdConfigBuilder {
|
|
|
122
122
|
* ```
|
|
123
123
|
*/
|
|
124
124
|
clearAll(): this;
|
|
125
|
+
/**
|
|
126
|
+
* Merge with another configuration
|
|
127
|
+
* @param config - Configuration to merge with current configuration
|
|
128
|
+
*/
|
|
129
|
+
mergeConfig(config: JsonLdConfig): this;
|
|
130
|
+
/**
|
|
131
|
+
* Merge only the filters part of another configuration
|
|
132
|
+
* @param filters - Filter options to merge with current filters
|
|
133
|
+
*/
|
|
134
|
+
mergeFilters(filters: JsonLdFilterOptions): this;
|
|
125
135
|
/**
|
|
126
136
|
* Get the current configuration
|
|
127
137
|
*/
|
package/config.js
CHANGED
|
@@ -9,10 +9,12 @@
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.JsonLdConfigBuilder = void 0;
|
|
11
11
|
exports.createJsonLdConfig = createJsonLdConfig;
|
|
12
|
+
const builder_utils_1 = require("./builder-utils");
|
|
12
13
|
/**
|
|
13
14
|
* Immutable configuration builder class
|
|
14
15
|
*/
|
|
15
16
|
class JsonLdConfigBuilder {
|
|
17
|
+
config;
|
|
16
18
|
constructor(initialConfig = {}) {
|
|
17
19
|
this.config = { ...initialConfig };
|
|
18
20
|
}
|
|
@@ -143,37 +145,47 @@ class JsonLdConfigBuilder {
|
|
|
143
145
|
* Extract subgraph starting from these root IDs
|
|
144
146
|
*/
|
|
145
147
|
subgraph(rootIds) {
|
|
148
|
+
const existingRoots = this.config.filters?.subgraphRoots || [];
|
|
146
149
|
return this.createInstance({
|
|
147
150
|
...this.config,
|
|
148
|
-
|
|
151
|
+
filters: {
|
|
152
|
+
...this.config.filters,
|
|
153
|
+
subgraphRoots: [...existingRoots, ...rootIds],
|
|
154
|
+
},
|
|
149
155
|
});
|
|
150
156
|
}
|
|
151
157
|
/**
|
|
152
158
|
* Filter properties for specific entity IDs
|
|
153
159
|
*/
|
|
154
160
|
filterPropertiesByIds(entityIds, rule) {
|
|
155
|
-
const existingRules = this.config.propertyFiltersByIds || [];
|
|
161
|
+
const existingRules = this.config.filters?.propertyFiltersByIds || [];
|
|
156
162
|
const newRule = {
|
|
157
163
|
entityIds,
|
|
158
164
|
...rule,
|
|
159
165
|
};
|
|
160
166
|
return this.createInstance({
|
|
161
167
|
...this.config,
|
|
162
|
-
|
|
168
|
+
filters: {
|
|
169
|
+
...this.config.filters,
|
|
170
|
+
propertyFiltersByIds: [...existingRules, newRule],
|
|
171
|
+
},
|
|
163
172
|
});
|
|
164
173
|
}
|
|
165
174
|
/**
|
|
166
175
|
* Filter properties for specific entity types
|
|
167
176
|
*/
|
|
168
177
|
filterPropertiesByTypes(entityTypes, rule) {
|
|
169
|
-
const existingRules = this.config.propertyFiltersByTypes || [];
|
|
178
|
+
const existingRules = this.config.filters?.propertyFiltersByTypes || [];
|
|
170
179
|
const newRule = {
|
|
171
180
|
entityTypes,
|
|
172
181
|
...rule,
|
|
173
182
|
};
|
|
174
183
|
return this.createInstance({
|
|
175
184
|
...this.config,
|
|
176
|
-
|
|
185
|
+
filters: {
|
|
186
|
+
...this.config.filters,
|
|
187
|
+
propertyFiltersByTypes: [...existingRules, newRule],
|
|
188
|
+
},
|
|
177
189
|
});
|
|
178
190
|
}
|
|
179
191
|
/**
|
|
@@ -264,8 +276,11 @@ class JsonLdConfigBuilder {
|
|
|
264
276
|
clearPropertyFilters() {
|
|
265
277
|
return this.createInstance({
|
|
266
278
|
...this.config,
|
|
267
|
-
|
|
268
|
-
|
|
279
|
+
filters: {
|
|
280
|
+
...this.config.filters,
|
|
281
|
+
propertyFiltersByIds: undefined,
|
|
282
|
+
propertyFiltersByTypes: undefined,
|
|
283
|
+
},
|
|
269
284
|
});
|
|
270
285
|
}
|
|
271
286
|
/**
|
|
@@ -274,7 +289,10 @@ class JsonLdConfigBuilder {
|
|
|
274
289
|
clearSubgraph() {
|
|
275
290
|
return this.createInstance({
|
|
276
291
|
...this.config,
|
|
277
|
-
|
|
292
|
+
filters: {
|
|
293
|
+
...this.config.filters,
|
|
294
|
+
subgraphRoots: undefined,
|
|
295
|
+
},
|
|
278
296
|
});
|
|
279
297
|
}
|
|
280
298
|
/**
|
|
@@ -290,6 +308,27 @@ class JsonLdConfigBuilder {
|
|
|
290
308
|
baseGraph: this.config.baseGraph,
|
|
291
309
|
});
|
|
292
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Merge with another configuration
|
|
313
|
+
* @param config - Configuration to merge with current configuration
|
|
314
|
+
*/
|
|
315
|
+
mergeConfig(config) {
|
|
316
|
+
const currentConfig = this.getConfig();
|
|
317
|
+
const mergedConfig = (0, builder_utils_1.mergeConfigs)(currentConfig, config);
|
|
318
|
+
return this.createInstance(mergedConfig);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Merge only the filters part of another configuration
|
|
322
|
+
* @param filters - Filter options to merge with current filters
|
|
323
|
+
*/
|
|
324
|
+
mergeFilters(filters) {
|
|
325
|
+
const currentFilters = this.config.filters;
|
|
326
|
+
const mergedFilters = (0, builder_utils_1.mergeFilterOptions)(currentFilters, filters);
|
|
327
|
+
return this.createInstance({
|
|
328
|
+
...this.config,
|
|
329
|
+
filters: mergedFilters,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
293
332
|
/**
|
|
294
333
|
* Get the current configuration
|
|
295
334
|
*/
|
package/esm/builder-utils.js
CHANGED
|
@@ -13,14 +13,6 @@ export function mergeConfigs(base, override) {
|
|
|
13
13
|
baseGraph: override.baseGraph ?? base.baseGraph,
|
|
14
14
|
// Filters: merge filter options
|
|
15
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
16
|
// Additional entities: combine arrays
|
|
25
17
|
additionalEntities: combineArrays(base.additionalEntities, override.additionalEntities),
|
|
26
18
|
// Pipes: combine arrays
|
|
@@ -32,7 +24,7 @@ export function mergeConfigs(base, override) {
|
|
|
32
24
|
/**
|
|
33
25
|
* Merge filter options, merging arrays and using override for single values
|
|
34
26
|
*/
|
|
35
|
-
function mergeFilterOptions(base, override) {
|
|
27
|
+
export function mergeFilterOptions(base, override) {
|
|
36
28
|
if (!base && !override)
|
|
37
29
|
return undefined;
|
|
38
30
|
if (!base)
|
|
@@ -47,6 +39,10 @@ function mergeFilterOptions(base, override) {
|
|
|
47
39
|
excludeIds: combineArrays(base.excludeIds, override.excludeIds),
|
|
48
40
|
requiredProperties: combineArrays(base.requiredProperties, override.requiredProperties),
|
|
49
41
|
excludeEntitiesWithProperties: combineArrays(base.excludeEntitiesWithProperties, override.excludeEntitiesWithProperties),
|
|
42
|
+
subgraphRoots: combineArrays(base.subgraphRoots, override.subgraphRoots),
|
|
43
|
+
propertyFilters: combinePropertyFilters(base.propertyFilters, override.propertyFilters),
|
|
44
|
+
propertyFiltersByIds: combineArrays(base.propertyFiltersByIds, override.propertyFiltersByIds),
|
|
45
|
+
propertyFiltersByTypes: combineArrays(base.propertyFiltersByTypes, override.propertyFiltersByTypes),
|
|
50
46
|
// Single values - override takes precedence
|
|
51
47
|
customFilter: override.customFilter ?? base.customFilter,
|
|
52
48
|
maxEntities: override.maxEntities ?? base.maxEntities,
|
|
@@ -102,16 +98,16 @@ export function convertPropertyFiltersByTypes(rules) {
|
|
|
102
98
|
export function buildPropertyFilterConfig(config) {
|
|
103
99
|
const filters = [];
|
|
104
100
|
// Add base property filters
|
|
105
|
-
if (config.propertyFilters) {
|
|
106
|
-
filters.push(...config.propertyFilters);
|
|
101
|
+
if (config.filters?.propertyFilters) {
|
|
102
|
+
filters.push(...config.filters.propertyFilters);
|
|
107
103
|
}
|
|
108
104
|
// Add property filters by IDs
|
|
109
|
-
if (config.propertyFiltersByIds) {
|
|
110
|
-
filters.push(...convertPropertyFiltersByIds(config.propertyFiltersByIds));
|
|
105
|
+
if (config.filters?.propertyFiltersByIds) {
|
|
106
|
+
filters.push(...convertPropertyFiltersByIds(config.filters.propertyFiltersByIds));
|
|
111
107
|
}
|
|
112
108
|
// Add property filters by types
|
|
113
|
-
if (config.propertyFiltersByTypes) {
|
|
114
|
-
filters.push(...convertPropertyFiltersByTypes(config.propertyFiltersByTypes));
|
|
109
|
+
if (config.filters?.propertyFiltersByTypes) {
|
|
110
|
+
filters.push(...convertPropertyFiltersByTypes(config.filters.propertyFiltersByTypes));
|
|
115
111
|
}
|
|
116
112
|
return filters.length > 0 ? filters : undefined;
|
|
117
113
|
}
|
package/esm/builder.js
CHANGED
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { JsonLdConfigBuilder } from './config';
|
|
9
9
|
import { extractSubgraphs, filterGraphProperties } from './jsonld-utils';
|
|
10
|
-
import { applyPopulateConfig, buildPropertyFilterConfig, filterJsonLdGraph,
|
|
10
|
+
import { applyPopulateConfig, buildPropertyFilterConfig, filterJsonLdGraph, validateRuntimeConfig, } from './builder-utils';
|
|
11
11
|
/**
|
|
12
12
|
* Mutable builder class that extends config builder with automatic method delegation
|
|
13
13
|
*/
|
|
14
14
|
export class JsonLdBuilder extends JsonLdConfigBuilder {
|
|
15
|
+
currentGraph;
|
|
15
16
|
constructor(initialConfig = {}) {
|
|
16
17
|
super(initialConfig);
|
|
17
18
|
}
|
|
@@ -21,14 +22,6 @@ export class JsonLdBuilder extends JsonLdConfigBuilder {
|
|
|
21
22
|
createInstance(config) {
|
|
22
23
|
return new JsonLdBuilder(config);
|
|
23
24
|
}
|
|
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
25
|
/**
|
|
33
26
|
* Get the current graph state (processes the graph if not already done)
|
|
34
27
|
*/
|
|
@@ -71,7 +64,7 @@ export class JsonLdBuilder extends JsonLdConfigBuilder {
|
|
|
71
64
|
processGraph() {
|
|
72
65
|
const config = this.getConfig();
|
|
73
66
|
if (!config.baseGraph) {
|
|
74
|
-
throw new Error('No base graph provided. Use baseGraph() or
|
|
67
|
+
throw new Error('No base graph provided. Use baseGraph() or mergeConfig() with a baseGraph.');
|
|
75
68
|
}
|
|
76
69
|
// Validate configuration - only check for critical errors that would break processing
|
|
77
70
|
const errors = validateRuntimeConfig(config);
|
|
@@ -82,9 +75,9 @@ export class JsonLdBuilder extends JsonLdConfigBuilder {
|
|
|
82
75
|
const propertyFilters = buildPropertyFilterConfig(config);
|
|
83
76
|
let propertyFiltersApplied = false;
|
|
84
77
|
// Layer 1: Subgraph extraction with property filtering if subgraphRoots are set
|
|
85
|
-
if (config.subgraphRoots && config.subgraphRoots.length > 0) {
|
|
78
|
+
if (config.filters?.subgraphRoots && config.filters.subgraphRoots.length > 0) {
|
|
86
79
|
// Extract subgraph with property filtering during traversal
|
|
87
|
-
graph = extractSubgraphs(graph, config.subgraphRoots, propertyFilters);
|
|
80
|
+
graph = extractSubgraphs(graph, config.filters.subgraphRoots, propertyFilters);
|
|
88
81
|
propertyFiltersApplied = true; // Property filters were applied during subgraph extraction
|
|
89
82
|
}
|
|
90
83
|
// Layer 2: Regular entity filtering with property filtering if not already applied
|
package/esm/config.js
CHANGED
|
@@ -5,10 +5,12 @@
|
|
|
5
5
|
* performing any graph processing. Each method returns a new
|
|
6
6
|
* configuration object for safe reuse and extension.
|
|
7
7
|
*/
|
|
8
|
+
import { mergeConfigs, mergeFilterOptions } from './builder-utils';
|
|
8
9
|
/**
|
|
9
10
|
* Immutable configuration builder class
|
|
10
11
|
*/
|
|
11
12
|
export class JsonLdConfigBuilder {
|
|
13
|
+
config;
|
|
12
14
|
constructor(initialConfig = {}) {
|
|
13
15
|
this.config = { ...initialConfig };
|
|
14
16
|
}
|
|
@@ -139,37 +141,47 @@ export class JsonLdConfigBuilder {
|
|
|
139
141
|
* Extract subgraph starting from these root IDs
|
|
140
142
|
*/
|
|
141
143
|
subgraph(rootIds) {
|
|
144
|
+
const existingRoots = this.config.filters?.subgraphRoots || [];
|
|
142
145
|
return this.createInstance({
|
|
143
146
|
...this.config,
|
|
144
|
-
|
|
147
|
+
filters: {
|
|
148
|
+
...this.config.filters,
|
|
149
|
+
subgraphRoots: [...existingRoots, ...rootIds],
|
|
150
|
+
},
|
|
145
151
|
});
|
|
146
152
|
}
|
|
147
153
|
/**
|
|
148
154
|
* Filter properties for specific entity IDs
|
|
149
155
|
*/
|
|
150
156
|
filterPropertiesByIds(entityIds, rule) {
|
|
151
|
-
const existingRules = this.config.propertyFiltersByIds || [];
|
|
157
|
+
const existingRules = this.config.filters?.propertyFiltersByIds || [];
|
|
152
158
|
const newRule = {
|
|
153
159
|
entityIds,
|
|
154
160
|
...rule,
|
|
155
161
|
};
|
|
156
162
|
return this.createInstance({
|
|
157
163
|
...this.config,
|
|
158
|
-
|
|
164
|
+
filters: {
|
|
165
|
+
...this.config.filters,
|
|
166
|
+
propertyFiltersByIds: [...existingRules, newRule],
|
|
167
|
+
},
|
|
159
168
|
});
|
|
160
169
|
}
|
|
161
170
|
/**
|
|
162
171
|
* Filter properties for specific entity types
|
|
163
172
|
*/
|
|
164
173
|
filterPropertiesByTypes(entityTypes, rule) {
|
|
165
|
-
const existingRules = this.config.propertyFiltersByTypes || [];
|
|
174
|
+
const existingRules = this.config.filters?.propertyFiltersByTypes || [];
|
|
166
175
|
const newRule = {
|
|
167
176
|
entityTypes,
|
|
168
177
|
...rule,
|
|
169
178
|
};
|
|
170
179
|
return this.createInstance({
|
|
171
180
|
...this.config,
|
|
172
|
-
|
|
181
|
+
filters: {
|
|
182
|
+
...this.config.filters,
|
|
183
|
+
propertyFiltersByTypes: [...existingRules, newRule],
|
|
184
|
+
},
|
|
173
185
|
});
|
|
174
186
|
}
|
|
175
187
|
/**
|
|
@@ -260,8 +272,11 @@ export class JsonLdConfigBuilder {
|
|
|
260
272
|
clearPropertyFilters() {
|
|
261
273
|
return this.createInstance({
|
|
262
274
|
...this.config,
|
|
263
|
-
|
|
264
|
-
|
|
275
|
+
filters: {
|
|
276
|
+
...this.config.filters,
|
|
277
|
+
propertyFiltersByIds: undefined,
|
|
278
|
+
propertyFiltersByTypes: undefined,
|
|
279
|
+
},
|
|
265
280
|
});
|
|
266
281
|
}
|
|
267
282
|
/**
|
|
@@ -270,7 +285,10 @@ export class JsonLdConfigBuilder {
|
|
|
270
285
|
clearSubgraph() {
|
|
271
286
|
return this.createInstance({
|
|
272
287
|
...this.config,
|
|
273
|
-
|
|
288
|
+
filters: {
|
|
289
|
+
...this.config.filters,
|
|
290
|
+
subgraphRoots: undefined,
|
|
291
|
+
},
|
|
274
292
|
});
|
|
275
293
|
}
|
|
276
294
|
/**
|
|
@@ -286,6 +304,27 @@ export class JsonLdConfigBuilder {
|
|
|
286
304
|
baseGraph: this.config.baseGraph,
|
|
287
305
|
});
|
|
288
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* Merge with another configuration
|
|
309
|
+
* @param config - Configuration to merge with current configuration
|
|
310
|
+
*/
|
|
311
|
+
mergeConfig(config) {
|
|
312
|
+
const currentConfig = this.getConfig();
|
|
313
|
+
const mergedConfig = mergeConfigs(currentConfig, config);
|
|
314
|
+
return this.createInstance(mergedConfig);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Merge only the filters part of another configuration
|
|
318
|
+
* @param filters - Filter options to merge with current filters
|
|
319
|
+
*/
|
|
320
|
+
mergeFilters(filters) {
|
|
321
|
+
const currentFilters = this.config.filters;
|
|
322
|
+
const mergedFilters = mergeFilterOptions(currentFilters, filters);
|
|
323
|
+
return this.createInstance({
|
|
324
|
+
...this.config,
|
|
325
|
+
filters: mergedFilters,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
289
328
|
/**
|
|
290
329
|
* Get the current configuration
|
|
291
330
|
*/
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsonldjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "JSON-LD Tooling",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
|
-
"homepage": "https://github.com/hyperweb-io/
|
|
7
|
-
"license": "
|
|
6
|
+
"homepage": "https://github.com/hyperweb-io/dev-utils",
|
|
7
|
+
"license": "MIT",
|
|
8
8
|
"main": "index.js",
|
|
9
9
|
"module": "esm/index.js",
|
|
10
10
|
"types": "index.d.ts",
|
|
@@ -14,21 +14,22 @@
|
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": "https://github.com/hyperweb-io/
|
|
17
|
+
"url": "https://github.com/hyperweb-io/dev-utils"
|
|
18
18
|
},
|
|
19
19
|
"keywords": [
|
|
20
20
|
"jsonld-tools"
|
|
21
21
|
],
|
|
22
|
-
"bugs": {
|
|
23
|
-
"url": "https://github.com/hyperweb-io/jsonld-tools/issues"
|
|
24
|
-
},
|
|
25
22
|
"scripts": {
|
|
26
|
-
"copy": "
|
|
23
|
+
"copy": "cpy ../../LICENSE README.md package.json dist --flat",
|
|
27
24
|
"clean": "rimraf dist",
|
|
25
|
+
"prepublishOnly": "pnpm run build",
|
|
28
26
|
"build": "pnpm run clean && tsc && tsc -p tsconfig.esm.json && pnpm run copy",
|
|
29
|
-
"build:dev": "pnpm run clean && tsc --declarationMap && tsc -p tsconfig.esm.json && pnpm run copy",
|
|
30
27
|
"test": "jest",
|
|
31
28
|
"test:watch": "jest --watch",
|
|
32
29
|
"test:debug": "node --inspect node_modules/.bin/jest --runInBand"
|
|
33
|
-
}
|
|
34
|
-
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/hyperweb-io/dev-utils/issues"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "361c37cf04737fd8d5917bac54834558e62b2e15"
|
|
35
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -31,6 +31,14 @@ export interface JsonLdFilterOptions {
|
|
|
31
31
|
requiredProperties?: string[];
|
|
32
32
|
/** Exclude entities with specific properties (renamed from excludeProperties) */
|
|
33
33
|
excludeEntitiesWithProperties?: string[];
|
|
34
|
+
/** Root IDs for subgraph extraction */
|
|
35
|
+
subgraphRoots?: string[];
|
|
36
|
+
/** Property-level filtering configuration */
|
|
37
|
+
propertyFilters?: PropertyFilterConfig;
|
|
38
|
+
/** Property filtering rules by entity IDs */
|
|
39
|
+
propertyFiltersByIds?: PropertyFilterByIdRule[];
|
|
40
|
+
/** Property filtering rules by entity types */
|
|
41
|
+
propertyFiltersByTypes?: PropertyFilterByTypeRule[];
|
|
34
42
|
}
|
|
35
43
|
/**
|
|
36
44
|
* Configuration for property filtering by entity IDs
|
|
@@ -69,14 +77,6 @@ export interface JsonLdConfig {
|
|
|
69
77
|
baseGraph?: JsonLdGraph;
|
|
70
78
|
/** Entity filtering options */
|
|
71
79
|
filters?: JsonLdFilterOptions;
|
|
72
|
-
/** Root IDs for subgraph extraction */
|
|
73
|
-
subgraphRoots?: string[];
|
|
74
|
-
/** Property-level filtering configuration */
|
|
75
|
-
propertyFilters?: PropertyFilterConfig;
|
|
76
|
-
/** Property filtering rules by entity IDs */
|
|
77
|
-
propertyFiltersByIds?: PropertyFilterByIdRule[];
|
|
78
|
-
/** Property filtering rules by entity types */
|
|
79
|
-
propertyFiltersByTypes?: PropertyFilterByTypeRule[];
|
|
80
80
|
/** Additional entities to include */
|
|
81
81
|
additionalEntities?: JsonLdEntity[];
|
|
82
82
|
/** Custom transformation functions */
|
|
@@ -153,6 +153,10 @@ export interface IJsonLdConfigBuilder {
|
|
|
153
153
|
clearSubgraph(): this;
|
|
154
154
|
/** Clear entire configuration except baseGraph */
|
|
155
155
|
clearAll(): this;
|
|
156
|
+
/** Merge with another configuration */
|
|
157
|
+
mergeConfig(config: JsonLdConfig): this;
|
|
158
|
+
/** Merge only the filters part of another configuration */
|
|
159
|
+
mergeFilters(filters: JsonLdFilterOptions): this;
|
|
156
160
|
/** Get the current configuration */
|
|
157
161
|
getConfig(): JsonLdConfig;
|
|
158
162
|
}
|