-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocker.js
275 lines (255 loc) · 11.7 KB
/
mocker.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// dataSource mocker v0.4.1
// (c) 2012-2013 caicanliang, [email protected]
// freely distributed under the MIT license.
// Mocking dataSource by javascript, providing save, remove, query and sort
// create eg.
// var fruitsDataSource = Mocker.mock({
// template: { say: 'hello' }, // or an array: [ {..}, {..} ]
// amount: 100, // it's necessary if template isn't an array
// idAttribute: 'id', // or json full format: { key: 'id', prefix: 'fruit_' }
// randomAttributes: [{
// key: 'type',
// values: [ 'apple', 'banana', 'orange', 'grapes' ]
// },{
// key: 'taste',
// values: [ 'awesome', 'good', 'shit smells' ]
// },{
// key: 'time',
// values: { min: 1325347200000, max: 1356883200000 }
// }],
// onCreate: function( item ) { // item on create callback
// item.say = 'hi';
// },
// onComplete: function( items ) { // items on complete callback
// // you can add some statistics
// },
// shuffle: true, // to shuffle items order
// delay: 1000 // simulate request & response delay
// });
// or with AMD.
// require( [ 'mocker' ], function( Mocker ) { } );
// query eg.
// fruitsDataSource({
// params: {
// query: 'type gte {a}, type lte {z}, time gt {0}', // or json format: [ { key: 'type', operator: 'like', value: 'apple' } ]
// sort: 'type asc, time desc', // or json format: [ { key: 'time', order: 'desc' } ]
// skip: 0,
// limit: 100,
// count: true
// },
// success: function( data ) {
// console.log( 'data count: ' + data.count );
// console.log( 'data list: ' );
// console.log( data.list );
// },
// error: function() {
// console.error( 'shit happens!' );
// }
// });
// save eg.
// fruitsDataSource({
// params: {
// save: { // or string format: "{ 'type': 'orange', 'taste': 'bad' }"
// type: 'apple',
// taste: 'awesome',
// time: new Date().getTime()
// }
// },
// success: function( data ) {
// console.log( 'data id: ' + data.id );
// }
// });
// remove eg.
// fruitsDataSource({
// params: {
// remove: 'taste eq {shit smells}' // or json format: [ { key: 'taste', operator: 'nq', value: 'awesome' } ]
// },
// success: function( count ) {
// console.log( 'data count of remove: ' + count );
// }
// });
(function( root, factory ) {
if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define( [ 'underscore' ], factory );
} else {
// Browser globals
root.Mocker = factory( root._ );
}
})( this, function( _ ) {
var generateItems = function( configs ) {
var items = _.isArray( configs.template ) ? configs.template : _.times( configs.amount, function() {
return deepClone( configs.template );
});
_.each( items, function( item, index ) {
if ( configs.idAttribute ) {
var id = index + 1;
item[ configs.idAttribute.key || configs.idAttribute ] = configs.idAttribute.prefix ? configs.idAttribute.prefix + id : id;
}
_.each( configs.randomAttributes, function( randomAttribute ) {
if ( _.isArray( randomAttribute.values ) ) {
item[ randomAttribute.key ] = randomAttribute.values[ random( 0, randomAttribute.values.length - 1 ) ];
} else if ( _.isObject( randomAttribute.values ) ) {
item[ randomAttribute.key ] = random( randomAttribute.values.min, randomAttribute.values.max );
}
});
_.isFunction( configs.onCreate ) && configs.onCreate( item );
});
configs.shuffle && ( items = _.shuffle( items ) );
_.isFunction( configs.onComplete ) && configs.onComplete( items );
return items;
};
var saveItem = function( items, item, configs ) {
if ( configs.idAttribute && item[ configs.idAttribute.key ] != null ) {
// update
return _.extend( _.find( items, function( current ) {
return current[ configs.idAttribute.key ] === item[ configs.idAttribute.key ];
}), item );
} else {
// insert
configs.idAttribute && ( item[ configs.idAttribute.key ] = _.uniqueId( configs.idAttribute.prefix ) );
items.unshift( deepClone( item ) );
return item;
}
};
var queryItems = function( items, queries, onMatch ) {
var result = _.filter( items, function( item ) {
return _.every( queries, function( query ){
switch ( query.operator ) {
case 'lt' : return ( item[ query.key ] < query.value ) ? true : false;
case 'lte': return ( item[ query.key ] <= query.value ) ? true : false;
case 'eq' : return ( item[ query.key ] == query.value ) ? true : false;
case 'ne' : return ( item[ query.key ] != query.value ) ? true : false;
case 'gt' : return ( item[ query.key ] > query.value ) ? true : false;
case 'gte': return ( item[ query.key ] >= query.value ) ? true : false;
case 'like' : return ( item[ query.key ].indexOf( query.value ) > -1 ) ? true : false;
default: console && console.error && console.error( 'unsupport operator: ' + query.operator ); return false;
}
});
});
_.isFunction( onMatch ) && _.each( result, onMatch );
return deepClone( result );
};
var removeItems = function( items, query ) {
return queryItems( items, query, function( item, index, _items ) {
delete items[ index ];
}).length;
};
var sortItems = function( items, sort ) {
// add next reference for chaining
for ( var i = 0, length = sort.length; i < length - 1 ; i++ ) {
sort[ i ].next = sort[ i + 1 ];
}
if ( sort.length ) return ( function doSort( currentArray, currentOrder ) {
// separate by group
var groups = _.groupBy( currentArray, function( a ) {
return a[ currentOrder.key ];
});
// sort elements of groups by recursion
if ( currentOrder.next ) {
var self = arguments.callee;
_.each( groups, function( group, key ) {
groups[ key ] = self( group, currentOrder.next );
});
}
// sort the keys of groups
var keys = _.keys( groups ).sort( function( a, b ) {
( /^[-+]?[0-9]+(\.[0-9]+)?$/.test( a + b) ) && ( a = parseFloat( a ), b = parseFloat( b ) );
var direction = ( currentOrder.order === 'asc' ) ? 1 : -1;
var result = ( a > b ) ? 1 : -1;
return direction * result;
});
// link up the groups
var sortedArray = [];
_.each( keys, function( key ) {
sortedArray = sortedArray.concat( groups[ key ] );
});
return sortedArray;
})( items, sort[ 0 ] );
};
var parseQuery = function( query ) {
return ( query == null || ( query = query.replace( /^\s+|\s+$/g, '' ), query === '' ) ) ? [] :
_.map( query.replace( /\s*\}$/g, '' ).split( /\s*\}\s*\,\s*/ ), function( current ) {
var divide = current.lastIndexOf('{');
var front = current.substring( 0, divide ).replace( /\s+$/, '' ).split( /\s+/ );
var behind = current.substring( divide + 1 ).replace( /^\s+/, '' );
return {
key: front[ 0 ],
operator: front[ 1 ],
value: behind
};
});
};
var parseSort = function( sort ) {
return ( sort == null || ( sort = sort.replace( /^\s+|\s+$/g, '' ), sort === '' ) ) ? [] :
_.map( sort.split( /\s*,\s*/ ), function( current ) {
current = current.split( /\s+/ );
return {
key: current[ 0 ],
order: current[ 1 ]
};
});
};
// underscore has bug, use it instead
var random = function( min, max ) {
if ( min == null && max == null ) {
max = 1;
}
min = +min || 0;
if ( max == null ) {
max = min;
min = 0;
}
return min + Math.floor( Math.random() * ( ( +max || 0 ) - min + 1) );
};
var deepClone = function( obj ) {
if ( _.isArray( obj ) ) {
var out = [];
var i = obj.length;
while( i-- ) {
out.unshift( arguments.callee( obj[ i ] ) );
}
return out;
}
if ( _.isObject( obj ) ) {
var out = {};
for ( var p in obj ) {
out[ p ] = arguments.callee( obj[ p ] );
}
return out;
}
return obj;
};
return {
mock: function( cfgs ) {
var configs = _.extend( { template: {}, delay: 1000 }, cfgs );
var items = generateItems( configs );
return function dataSource( args ) {
setTimeout( function() {
var result;
if ( args.params && args.params.save ) {
var item = _.isObject( args.params.save ) ? args.params.save : eval( '(' + args.params.save + ')' );
result = saveItem( items, item, configs );
} else if ( args.params && args.params.remove ) {
var query = _.isArray( args.params.remove ) ? args.params.remove : parseQuery( args.params.remove );
result = removeItems( items, query );
} else {
var params = _.extend( { skip: 0, limit: 1000000 }, args.params );
var query = _.isArray( params.query ) ? params.query : parseQuery( params.query );
var matchedItems = queryItems( items, query );
if ( params.sort ) {
var sort = _.isArray( params.sort ) ? params.sort : parseSort( params.sort );
matchedItems = sortItems( matchedItems, sort );
}
result = {
count: ( params.count === true ) ? matchedItems.length : null,
list: matchedItems.slice( params.skip, params.skip + Math.min( params.limit, matchedItems.length - params.skip ) )
};
}
_.isFunction( args.success ) && args.success( result );
_.isFunction( args.complete ) && args.complete();
}, configs.delay );
};
}
};
});