@live-change/dao 0.9.85 → 0.9.86
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/lib/collectPointers.js +79 -66
- package/package.json +2 -2
package/lib/collectPointers.js
CHANGED
|
@@ -16,9 +16,10 @@ function flatMap(source, fun) {
|
|
|
16
16
|
return out
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
function getPropertyValues(source, property) {
|
|
19
|
+
function getPropertyValues(source, property) {
|
|
20
20
|
if(Array.isArray(source)) {
|
|
21
|
-
|
|
21
|
+
const values = flatMap(markMany(source), v => getPropertyValues(v, property))
|
|
22
|
+
return values
|
|
22
23
|
} else {
|
|
23
24
|
if(source === undefined) return []
|
|
24
25
|
if(source === null) return []
|
|
@@ -52,7 +53,10 @@ function cross(lists) {
|
|
|
52
53
|
}
|
|
53
54
|
let out = new Array(count)
|
|
54
55
|
out.many = many
|
|
55
|
-
if(count > 1 && !many)
|
|
56
|
+
if(count > 1 && !many) {
|
|
57
|
+
console.log("ERROR WHEN CROSSING", lists.map(l => JSON.stringify(l, null, 2)+' many:'+(!!l.many)).join('\n'))
|
|
58
|
+
throw new Error("more than one result for non array fields")
|
|
59
|
+
}
|
|
56
60
|
for(let i = 0; i < count; i++) {
|
|
57
61
|
let res = new Array(lists.length)
|
|
58
62
|
let a = i
|
|
@@ -67,75 +71,84 @@ function cross(lists) {
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
function collect(source, schema, getSource) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return cross(partValues)
|
|
80
|
-
} else {
|
|
81
|
-
if(schema.source) {
|
|
82
|
-
const sourcePointers = collect(source, schema.source, getSource)
|
|
83
|
-
return flatMap( sourcePointers, ptr => {
|
|
84
|
-
const source = getSource(ptr)
|
|
85
|
-
const results = collect(source, schema.schema, getSource)
|
|
86
|
-
return results
|
|
87
|
-
})
|
|
88
|
-
} else if(schema.nonEmpty) {
|
|
89
|
-
const collected = collect(source, schema.nonEmpty, getSource)
|
|
90
|
-
const result = collected.filter(x=>!!x)
|
|
91
|
-
result.many = collected.many
|
|
92
|
-
return result
|
|
93
|
-
} else if(schema.identity) {
|
|
94
|
-
if(typeof source == 'undefined' || source === null) return []
|
|
95
|
-
return Array.isArray(source) ? markMany(source) : [source]
|
|
96
|
-
} else if(schema.array) {
|
|
97
|
-
return [ collect(source, schema.array, getSource) ]
|
|
98
|
-
} else if(schema.property) {
|
|
99
|
-
if(typeof source == 'undefined' || source === null) return []
|
|
100
|
-
if(Array.isArray(schema.property)) {
|
|
101
|
-
let values = getNestedPropertyValues(source, schema.property)
|
|
102
|
-
return values
|
|
103
|
-
} else {
|
|
104
|
-
let values = getPropertyValues(source, schema.property)
|
|
105
|
-
return values
|
|
74
|
+
try {
|
|
75
|
+
if(typeof schema == 'string') {
|
|
76
|
+
return [schema]
|
|
77
|
+
} else if(typeof schema != 'object') {
|
|
78
|
+
return [schema]
|
|
79
|
+
} else if(Array.isArray(schema)) {
|
|
80
|
+
let partValues = new Array(schema.length)
|
|
81
|
+
for(let i = 0; i < schema.length; i++) {
|
|
82
|
+
partValues[i] = collect(source, schema[i], getSource)
|
|
106
83
|
}
|
|
107
|
-
|
|
108
|
-
const values = collect(source, schema.value, getSource)
|
|
109
|
-
return flatMap(values, v => {
|
|
110
|
-
const found = schema.switch[v]
|
|
111
|
-
if(found) return collect(source, found, getSource)
|
|
112
|
-
if(schema.default) return collect(source, schema.default, getSource)
|
|
113
|
-
return []
|
|
114
|
-
})
|
|
115
|
-
} else if(schema.static) {
|
|
116
|
-
return [schema.static]
|
|
84
|
+
return cross(partValues)
|
|
117
85
|
} else {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
86
|
+
if(schema.source) {
|
|
87
|
+
const sourcePointers = collect(source, schema.source, getSource)
|
|
88
|
+
return flatMap( sourcePointers, ptr => {
|
|
89
|
+
const source = getSource(ptr)
|
|
90
|
+
const results = collect(source, schema.schema, getSource)
|
|
91
|
+
return results
|
|
92
|
+
})
|
|
93
|
+
} else if(schema.nonEmpty) {
|
|
94
|
+
const collected = collect(source, schema.nonEmpty, getSource)
|
|
95
|
+
const result = collected.filter(x=>!!x)
|
|
96
|
+
result.many = collected.many
|
|
97
|
+
return result
|
|
98
|
+
} else if(schema.identity) {
|
|
99
|
+
if(typeof source == 'undefined' || source === null) return []
|
|
100
|
+
return Array.isArray(source) ? markMany(source) : [source]
|
|
101
|
+
} else if(schema.array) {
|
|
102
|
+
return [ collect(source, schema.array, getSource) ]
|
|
103
|
+
} else if(schema.property) {
|
|
104
|
+
if(typeof source == 'undefined' || source === null) return []
|
|
105
|
+
if(Array.isArray(schema.property)) {
|
|
106
|
+
let values = getNestedPropertyValues(source, schema.property)
|
|
107
|
+
return values
|
|
108
|
+
} else {
|
|
109
|
+
let values = getPropertyValues(source, schema.property)
|
|
110
|
+
return values
|
|
111
|
+
}
|
|
112
|
+
} else if(schema.switch) {
|
|
113
|
+
const values = collect(source, schema.value, getSource)
|
|
114
|
+
return flatMap(values, v => {
|
|
115
|
+
const found = schema.switch[v]
|
|
116
|
+
if(found) return collect(source, found, getSource)
|
|
117
|
+
if(schema.default) return collect(source, schema.default, getSource)
|
|
118
|
+
return []
|
|
119
|
+
})
|
|
120
|
+
} else if(schema.static) {
|
|
121
|
+
return [schema.static]
|
|
122
|
+
} else if(schema.or) {
|
|
123
|
+
const resultsFromOr = schema.or.map(orSchema => collect(source, orSchema, getSource))
|
|
124
|
+
return resultsFromOr.find(r => r.length > 0) || []
|
|
125
|
+
} else {
|
|
126
|
+
let objectSchema = schema.object ? schema.object : schema
|
|
127
|
+
let propValues = []
|
|
128
|
+
let propId = 0
|
|
131
129
|
for(let key in objectSchema) {
|
|
132
|
-
|
|
130
|
+
let values = collect(source, objectSchema[key], getSource)
|
|
131
|
+
propValues[propId] = values.filter(Boolean)
|
|
132
|
+
propValues[propId].many = values.many
|
|
133
|
+
propId++
|
|
134
|
+
}
|
|
135
|
+
let crossed = cross(propValues)
|
|
136
|
+
let results = new Array(crossed.length)
|
|
137
|
+
for(let i = 0; i < crossed.length; i++) {
|
|
138
|
+
let result = {}
|
|
139
|
+
let j = 0
|
|
140
|
+
for(let key in objectSchema) {
|
|
141
|
+
result[key] = crossed[i][j++]
|
|
142
|
+
}
|
|
143
|
+
results[i] = result
|
|
133
144
|
}
|
|
134
|
-
results
|
|
145
|
+
results.many = crossed.many
|
|
146
|
+
return results
|
|
135
147
|
}
|
|
136
|
-
results.many = crossed.many
|
|
137
|
-
return results
|
|
138
148
|
}
|
|
149
|
+
} catch(e) {
|
|
150
|
+
console.log("ERROR WHEN COLLECTING", source, JSON.stringify(schema, null, 2), e)
|
|
151
|
+
throw e
|
|
139
152
|
}
|
|
140
153
|
}
|
|
141
154
|
|
package/package.json
CHANGED