-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
235 lines (212 loc) · 5.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* @author Gilles Coomans <[email protected]>
*
*
* deepjs completion :
*
* nodes.asyncUps = function(s, objects) { // load
return proto.getAll(objects)
.done(function(objects) {
return nodes.ups(s, objects);
});
};
nodes.asyncBottoms = function(s, objects) {
return proto.getAll(objects)
.done(function(objects) {
return nodes.bottoms(s, objects);
});
};
nodes.ups = function(s, objects) {
if (s._deep_query_node_)
objects.forEach(function(object) {
s.set(compiler.aup(object, s.value));
});
else if (s._deep_array_)
s.forEach(function(result) {
objects.forEach(function(object) {
result.set(compiler.aup(object, result.value));
});
});
else
objects.forEach(function(object) {
s = compiler.aup(object, s);
});
return s;
};
nodes.bottoms = function(s, objects) {
if (s._deep_query_node_)
objects.forEach(function(object) {
s.set(compiler.abottom(object, s.value));
});
else if (s._deep_array_)
s.forEach(function(result) {
objects.forEach(function(object) {
result.set(compiler.abottom(object, result.value));
});
});
else
objects.forEach(function(object) {
s = compiler.abottom(object, s);
});
return s;
};
nodes.map = function(node, transformer) { // transform node(s) value(s)
if (!node)
return transformer(node);
if (node._deep_query_node_) {
var r = transformer(node);
if (r && r.then)
return promise.when(r)
.done(node.set);
return node.set(r);
}
if (!node.forEach)
return tansformer(node);
return prom.all(node.map(transformer))
.done(function(res) {
var count = 0;
//if(node._deep_array_)
node.forEach(function(n) {
n.set(res[count++]);
});
});
};
*/
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(["require", "deep-utils/index", "deep-utils/lib/schema", "deep-utils/lib/string"], function(require, utils, schemaUtils) {
var nodes = {};
nodes.Node = function(key, ancestor) {
var path = null;
var schema = null;
if (ancestor) {
path = ancestor.path;
if (!ancestor.key)
path += key;
else
path += "/" + key;
if (ancestor.schema)
schema = schemaUtils.retrieveFullSchemaByPath(ancestor.schema, key, "/");
this.value = ancestor.value[key];
this.paths = ancestor.paths.concat(key);
this.depth = ancestor.depth + 1;
this.root = ancestor.root || ancestor;
}
//console.log("deep.utils.nodes.create : "+path+" : schema : ",schema)
this._deep_query_node_ = true;
this.path = path;
this.key = key;
this.ancestor = ancestor;
this.schema = schema;
};
nodes.Node.prototype = {
toString: function(full) {
var r = "[QueryNode : " + this.path + " : value : " + utils.stringify(this.value);
if (full) {
if (this.schema)
r += " - schema : " + utils.stringify(this.schema);
r += " - depth : " + this.depth;
}
r += "]";
return r;
},
set: function(value) {
this.value = value;
if (this.ancestor)
this.ancestor.value[this.key] = value;
return value;
},
clone: function() {
if (this.ancestor)
return new nodes.Node(this.key, this.ancestor);
return nodes.root(this.value, this.schema);
}
};
nodes.val = function(s) { // return value(s) from node(s)
if (s._deep_array_)
return s.map(function(e) {
return e.value;
});
if (s._deep_query_node_)
return s.value;
return s;
};
nodes.paths = function(nodes) { // return paths from node(s)
if (node._deep_query_node_)
return [node.path];
return node.map(function(e) {
if (e._deep_query_node_)
return e.schema;
return null;
});
};
nodes.schemas = function(node) { // return schemas from node(s)
if (node._deep_query_node_)
return [node.schema];
return node.map(function(e) {
if (e._deep_query_node_)
return e.schema;
return null;
});
};
nodes.each = function(node, callback) {
if (node.forEach)
node.forEach(callback);
// else if (node.value && node.value.forEach)
// node.value.forEach(callback);
else
callback(node);
return node;
};
nodes.map = function(node, callback) {
if (node.forEach)
return node.map(callback);
// else if (node.value && node.value.forEach)
// return node.value.map(callback);
if (node._deep_query_node_)
return callback(node);
return callback(node);
};
nodes.clone = function(node) {
if (node.forEach)
return node.slice();
else if (node._deep_query_node_)
return node.clone();
return utils.copy(node);
};
/**
* create a root DeepQuery node
*
* @static
* @method root
* @param {Object} obj
* @param {Object} schema
* @return {Object} a DeepQuery root node
*/
nodes.root = function(obj, schema) {
if (obj && obj._deep_undefined_)
obj = undefined;
var node = new nodes.Node();
node.value = obj;
node.path = "/";
node.paths = [];
node.key = null;
node.ancestor = null;
node.schema = schema;
node.depth = 0;
node.root = null;
return node;
};
/**
* create a Node object that hold info(path, value, ancestor, etc)
* @method create
* @param {String} key
* @param {Node} ancestor
* @return {Node} a child node
*/
nodes.create = function(key, ancestor) {
return new nodes.Node(key, ancestor);
};
return nodes;
});