This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
409 lines (397 loc) · 12.5 KB
/
functions.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
* Created by Kyriakos Barbounakis<k.barbounakis@gmail.com> on 2014-03-30.
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com
Anthi Oikonomou anthioikonomou@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of MOST Web Framework nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @ignore
*/
var types = require('./types'),
sprintf = require('sprintf'),
dataCommon = require('./data-common'),
moment = require('moment'),
_ = require('lodash'),
Q = require("q");
/**
* @exports most-data/functions
*/
var functions = { };
/**
* @class
* @param {DataContext=} context
* @param {DataModel=} model
* @param {*=} target
* @constructor
*/
function FunctionContext(context, model, target) {
/**
* @type {DataContext}
*/
this.context = context;
/**
* @type {DataModel}
*/
this.model = model;
if (_.isNil(context) && _.isObject(model)) {
//get current context from DataModel.context property
this.context = model.context;
}
/**
* @type {*}
*/
this.target = target;
}
FunctionContext.prototype.eval = function(expr, callback) {
callback = callback || function() {};
if (typeof expr !=='string') {
callback(null);
return;
}
var re = /(fn:)\s?(.*?)\s?\((.*?)\)/, expr1=expr;
if (expr.indexOf('fn:')!==0) {
expr1 = 'fn:' + expr1;
}
var match = re.exec(expr1);
if (match) {
var expr2eval;
//check parameters (match[3])
if (match[3].length==0) {
expr2eval = expr1.replace(/(fn:)\s?(.*?)\s?\((.*?)\)/, "(function() { return this.$2(); });");
}
else {
expr2eval = expr1.replace(/(fn:)\s?(.*?)\s?\((.*?)\)/, "(function() { return this.$2($3); });");
}
//evaluate expression
try {
var f = eval(expr2eval);
var value1 = f.call(this);
if (typeof value1 !== 'undefined' && value1 !== null && typeof value1.then === 'function') {
value1.then(function(result) {
return callback(null, result);
}).catch(function(err) {
callback(err);
});
}
else {
return callback(null, value1);
}
}
catch(err) {
callback(err);
}
}
else {
console.log(sprintf.sprintf('Cannot evaluate %s.', expr1));
callback(new Error('Cannot evaluate expression.'));
}
};
/**
* @returns {Promise|*}
*/
FunctionContext.prototype.now = function() {
var deferred = Q.defer();
process.nextTick(function() {
deferred.resolve(new Date());
});
return deferred.promise;
};
/**
* @returns {Promise|*}
*/
FunctionContext.prototype.today = function() {
var deferred = Q.defer();
process.nextTick(function() {
deferred.resolve((new Date()).getDate());
});
return deferred.promise;
};
/**
* @returns {Promise|*}
*/
FunctionContext.prototype.newid = function() {
var deferred = Q.defer();
this.model.context.db.selectIdentity(this.model.sourceAdapter, this.model.primaryKey, function(err, result) {
if (err) {
return deferred.reject(err);
}
deferred.resolve(result);
});
return deferred.promise;
};
var UUID_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
function newGuidInternal() {
var chars = UUID_CHARS, uuid = [], i;
// rfc4122, version 4 form
var r;
// rfc4122 requires these characters
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
// Fill in random data. At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random()*16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join('');
}
/**
* @returns {Promise|*}
*/
FunctionContext.prototype.newGuid = function() {
var deferred = Q.defer();
process.nextTick(function() {
try {
deferred.resolve(newGuidInternal());
}
catch(err) {
deferred.reject(err)
}
});
return deferred.promise;
};
/**
* Generates a random integer value between the given minimum and maximum value
* @param {number} min
* @param {number} max
* @returns {Promise|*}
*/
FunctionContext.prototype.int = function(min, max) {
var deferred = Q.defer();
process.nextTick(function() {
try {
return deferred.resolve(_.random(min, max));
}
catch (err) {
deferred.reject(err);
}
deferred.resolve((new Date()).getDate());
});
return deferred.promise;
};
/**
* Generates a random sequence of numeric characters
* @param {number} length - A integer which represents the length of the sequence
* @returns {Promise|*}
*/
FunctionContext.prototype.numbers = function(length) {
var deferred = Q.defer();
process.nextTick(function() {
try {
length = length || 8;
if (length<0) {
return deferred.reject(new Error("Number sequence length must be greater than zero."));
}
if (length>255) {
return deferred.reject(new Error("Number sequence length exceeds the maximum of 255 characters."));
}
var times = Math.ceil(length / 10);
var res = '';
_.times(times, function() {
res += _.random(1000000000, 9000000000)
});
return deferred.resolve(res.substr(0,length));
}
catch (err) {
deferred.reject(err);
}
});
return deferred.promise;
};
/**
* @param {number} length
* @returns {Promise|*}
*/
FunctionContext.prototype.chars = function(length) {
var deferred = Q.defer();
process.nextTick(function() {
try {
length = length || 8;
var chars = "abcdefghkmnopqursuvwxz2456789ABCDEFHJKLMNPQURSTUVWXYZ";
var str = "";
for(var i = 0; i < length; i++) {
str += chars.substr(_.random(0, chars.length-1),1);
}
deferred.resolve(str);
}
catch (err) {
return deferred.reject(err);
}
});
return deferred.promise;
};
/**
* @param {number} length
* @returns {Promise|*}
*/
FunctionContext.prototype.password = function(length) {
var deferred = Q.defer();
process.nextTick(function() {
try {
length = length || 8;
var chars = "abcdefghkmnopqursuvwxz2456789ABCDEFHJKLMNPQURSTUVWXYZ",
str = "";
for(var i = 0; i < length; i++) {
str += chars.substr(_.random(0, chars.length-1),1);
}
deferred.resolve('{clear}' + str);
}
catch (err) {
return deferred.reject(err);
}
});
return deferred.promise;
};
/**
* @returns {Promise|*}
*/
FunctionContext.prototype.user = function() {
var self = this, context = self.model.context, deferred = Q.defer();
var user = context.interactiveUser || context.user || { };
process.nextTick(function() {
if (typeof user.id !== 'undefined') {
return deferred.resolve(user.id);
}
var userModel = context.model('User'), parser, undefinedUser = null;
userModel.where('name').equal(user.name).silent().select('id').first(function(err, result) {
if (err) {
dataCommon.log(err);
//try to get undefined user
parser = types.parsers['parse' + userModel.field('id').type];
if (typeof parser === 'function')
undefinedUser = parser(null);
//set id for next calls
user.id = undefinedUser;
if (_.isNil(context.user)) {
context.user = user;
}
return deferred.resolve(undefinedUser);
}
else if (_.isNil(result)) {
//try to get undefined user
parser = types.parsers['parse' + userModel.field('id').type];
if (typeof parser === 'function')
undefinedUser = parser(null);
//set id for next calls
user.id = undefinedUser;
if (_.isNil(context.user)) {
context.user = user;
}
return deferred.resolve(undefinedUser);
}
else {
//set id for next calls
user.id = result.id;
return deferred.resolve(result.id);
}
});
});
return deferred.promise;
};
/**
* @returns {Promise|*}
*/
FunctionContext.prototype.me = function() {
return this.user();
};
functions.FunctionContext = FunctionContext;
/**
* Creates a new instance of FunctionContext class
* @param {DataContext|*=} context
* @param {DataModel|*=} model
* @param {*=} target
* @returns FunctionContext
*/
// eslint-disable-next-line no-unused-vars
functions.createContext = function(context, model, target) {
return new FunctionContext();
};
/**
* Gets the current date and time
* @param {FunctionContext} e The current function context
* @param {Function} callback The callback function to be called
*/
functions.now = function(e, callback) {
callback.call(this, null, new Date());
};
/**
* Gets the current date
* @param {FunctionContext} e
* @param {Function} callback
*/
functions.today = function(e, callback) {
var d = new Date();
callback.call(this, d.getDate());
};
/**
* Gets new identity key for a primary key column
* @param {FunctionContext} e
* @param {Function} callback
*/
functions.newid = function(e, callback)
{
e.model.context.db.selectIdentity(e.model.sourceAdapter, e.model.primaryKey, callback);
};
/**
* Gets the current user
* @param {FunctionContext} e The current function context
* @param {Function} callback The callback function to be called
*/
functions.user = function(e, callback) {
callback = callback || function() {};
var user = e.model.context.interactiveUser || e.model.context.user || { };
//ensure user name (or anonymous)
user.name = user.name || 'anonymous';
if (user['id']) {
return callback(null, user['id']);
}
var userModel = e.model.context.model('User');
userModel.where('name').equal(user.name).silent().select('id','name').first(function(err, result) {
if (err) {
console.log(err);
callback();
}
else {
//filter result to exclude anonymous user
var filtered = result.filter(function(x) { return x.name!=='anonymous'; }, result);
//if user was found
if (filtered.length>0) {
e.model.context.user.id = result[0].id;
callback(null, result[0].id);
}
//if anonymous was found
else if (result.length>0) {
callback(null, result[0].id);
}
else
callback();
}
});
};
module.exports = functions;