-
Notifications
You must be signed in to change notification settings - Fork 8
/
json-mapper.js
250 lines (219 loc) · 5.77 KB
/
json-mapper.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
module.exports.debugLog = function(){};
/**
* take the path and try solve this, if it is impossible return undefined
*
* @example
* var obj = {
* user:{
* name:'Jon',
* speed:100
* },
* place:{
* distance: '100km'
* }
* }
*
* var name = getValByPath('user.name',obj);
* var distance = getValByPath('place.distance',obj);
* @param {string|string[]} path Path to search
* @param {Object} obj object for search
* @returns {*}
*/
var getValByPath = module.exports.getValByPath = function (path,obj){
var trg = obj,
arr = typeof path == 'string'?path.split('.'):path,
l = arr.length -1,
i=-1;
while(i++<l){
if (arr[i] !== '$root'){
if (typeof trg === "undefined" || trg === null){
module.exports.debugLog('path ' + path.toString() + ' has unavailable chunk' + arr[i]);
return void 0;
}
trg = trg[arr[i]];
}
}
return trg;
};
/**
* take some functions and call is one by one
* @example
* var chain = ch(function(a){return a + 1},function(b){return b / 2});
* chain(3); // return 2 because (3 + 1) /2 = 2
* @returns {Function}
*/
var ch = module.exports.ch = function(){
var chain = [].slice.call(arguments);
chain = chain.map(makeCb);
return function(input){
return chain.reduce(function(tail,cb){
return cb(tail)
},input);
}
};
/**
*
* @param {object} input object to transform
* @param {object} schema schema for transform
* @returns {object}
*/
var applySchema = module.exports.applySchema = function(input,schema){
var output = {};
for (var par in schema ){
if (schema.hasOwnProperty(par)){
var val = schema[par](input);
if ( typeof val !== "undefined" ){
output[par] = val;
}
}
}
return output;
};
/**
* generate factory for transform object
* @param schema
* @returns {Function}
*/
var makeConverter = module.exports.makeConverter = function(schema){
var schemaConverter = {};
if (Object.prototype.toString.call(schema) !== "[object Object]" && !(schema instanceof Array) ){
throw new Error("schema must be Object or Array");
}
// generate callbacks for the scheme
for (var par in schema ){
if (schema.hasOwnProperty(par)){
schemaConverter[par] = makeCb(schema[par]);
}
}
return function(input){
return applySchema(input,schemaConverter)
}
};
/**
* generate a converter to map a converter to an array
* @param convert
* @return {Function}
*/
module.exports.makeMapConverter = function(convert){
return function(input){
var result = [];
input.forEach(function(obj){
result.push(convert(obj));
});
return result;
};
};
/**
* make getVal factory
* @param pth
* @returns {Function}
*/
var getVal = module.exports.getVal = function(pth){
var pt = pth.split('.');
return function(input){
return getValByPath(pt,input);
}
};
/**
* make factory to make map with fn
* @param {Function|String} fn
* @returns {Function}
*/
module.exports.map = function(fn){
var cb = makeCb(fn);
return function(input){
if (input instanceof Array){
return input.map(cb);
}
return void 0;
}
};
/**
* make factory to transform
* @type {makeCb}
*/
var makeCb = module.exports.makeCb = function(fn){
if (typeof fn === "string"){
return getVal(fn);
} else if (typeof fn === "function"){
return fn;
} else if (fn instanceof Array){
return ch.apply(null,fn);
} else if ((typeof fn == "object") && (fn !== null)){
return makeConverter(fn);
} else {
return function(){};
}
};
var filterUndefined = function(converter){
return function(input){
if (typeof input !== 'undefined'){
return converter(input);
}
}
};
module.exports.helpers = {
/**
* Template factory replace {key} to value from input[key]
* @param {string} tpl
* @returns {Function}
*/
template:function(tpl,strong){
// parce template
var finder = /\{([^\}]+\})/g,
paths = tpl.match(finder);
if ((paths === null) || (paths.length == 0)){
paths = [];
}
//make getters to placeholder
paths = paths.map(function(pth){
return {
pth : pth,
cb : makeCb(pth.slice(1,pth.length-1))
}
});
return function(input){
return paths.reduce(function(tail,itm){
var val = itm.cb(input);
if (typeof tail !== "undefined" && !(strong && typeof val === "undefined")){
return tail.replace( itm.pth, val );
} else {
return void 0; //undefined
}
},tpl);
}
},
templateStrong:function(tpl){
return module.exports.helpers.template(tpl,true);
},
def:function(val){
return function(){
return val;
}
},
valOrDef:function(def){
return function(input){
return input || def;
}
},
dict:function(dictionary){
return function(key){
return dictionary[key]
}
},
filterUndefined:filterUndefined,
toBoolean:filterUndefined(Boolean),
toNumber:filterUndefined(Number),
toString:function(input){
if (input === null || typeof input === 'undefined'){
return '';
}
return String(input)
},
toUndefined:function(input){
if (isNaN(input) || input === null || typeof input === 'undefined'){
return void 0;
}
return input;
}
};