theprogrammablemind_4wp 9.3.0-beta.53 → 9.3.0-beta.54

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.
Files changed (2) hide show
  1. package/package.json +2 -1
  2. package/src/project2.js +54 -0
package/package.json CHANGED
@@ -53,6 +53,7 @@
53
53
  "src/digraph_internal.js",
54
54
  "src/generators.js",
55
55
  "src/project.js",
56
+ "src/project2.js",
56
57
  "src/semantics.js"
57
58
  ],
58
59
  "author": "dev@thinktelligence.com",
@@ -69,6 +70,6 @@
69
70
  "sort-json": "^2.0.0",
70
71
  "uuid": "^8.3.2"
71
72
  },
72
- "version": "9.3.0-beta.53",
73
+ "version": "9.3.0-beta.54",
73
74
  "license": "UNLICENSED"
74
75
  }
@@ -0,0 +1,54 @@
1
+ function project(source, filters) {
2
+ if (['string', 'number'].includes(typeof source)) {
3
+ return source
4
+ }
5
+
6
+ if (Object.keys(source).length === 0 && filters.length === 0) {
7
+ return {};
8
+ }
9
+
10
+ // Find the applicable filter for the current context
11
+ const filter = filters.find(f => f.match({ context: source }));
12
+ if (!filter) {
13
+ if (Array.isArray(source)) {
14
+ return source.map((element) => project(element, filters))
15
+ }
16
+ return {};
17
+ }
18
+
19
+ // Get the properties to include from the apply function
20
+ let properties = filter.apply();
21
+
22
+ // update
23
+ const updatedProperties = []
24
+ for (const property of properties) {
25
+ if (property.properties) {
26
+ for (const moreProperty of source[property.properties] || []) {
27
+ updatedProperties.push(moreProperty)
28
+ }
29
+ } else {
30
+ updatedProperties.push(property)
31
+ }
32
+ }
33
+ properties = updatedProperties
34
+
35
+ // Build the result object
36
+ const result = {};
37
+ properties.forEach(prop => {
38
+ if (source.hasOwnProperty(prop)) {
39
+ // If the property is an object and not null, recursively project it
40
+ if (typeof source[prop] === 'object' && source[prop] !== null) {
41
+ result[prop] = project(source[prop], filters);
42
+ } else {
43
+ // Copy primitive or null properties directly
44
+ result[prop] = source[prop];
45
+ }
46
+ }
47
+ });
48
+
49
+ return result;
50
+ }
51
+
52
+ module.exports = {
53
+ project
54
+ }