-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (90 loc) · 3.16 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
/**
* Created by Kevin on 3/10/2015.
*/
var Promise = require('bluebird');
module.exports = exports = {
/**
* unSettle function takes the returned array from the bluebird settle method and removes the extra
* properties _bitField & _settledValue. Returns a promise of an array.
* @param array
* @returns {Array}
*/
unSettle : function(array){
var b = {},
c = [];
return new Promise(function(resolve){
resolve(processor(array));
});
function processor(array){
for(var i=0;i<array.length;i++){
b = {};
for(var prop in array[i]._settledValue){
b[prop] = array[i]._settledValue[prop]
}
c.push(b);
}
return c;
}
},
/**
* Extends an object with a defaults object, similar to underscore's _.defaults
* Used for abstracting parameter handling from API methods
* @param object Configuration object to extend
* @param defs Object with default values
* @returns {*|{}} Returns the combined object with default values overwritten with the config object.
*/
defaults : function defaults(object, defs) {
var key;
object = object || {};
defs = defs || {};
// Iterate over object non-prototype properties:
for (key in defs) {
if (defs.hasOwnProperty(key)) {
// Replace values with defaults only if undefined (allow empty/zero values):
if (object[key] == null) object[key] = defs[key];
}
}
return object;
}, /**
* Tests whether supplied parameter is a true object
* @param obj
* @returns {*|boolean}
*/
isObject: function isObject(obj) {
return obj && toString.call(obj) === '[object Object]';
}, /**
* Random Password generator. Returns new password in callback. If no callback given, promise returned.
* @param l {Number} Desired length of returned random password
* @param cb {Function} Optional
* @returns {*}
*/
randomPassword: function(l,cb){
function _randPass(len){
var opts = {},
ret = "";
opts.length = len || 5; //default length of 5 characters
opts.chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < opts.length; i++ )
ret += opts.chars.charAt(Math.floor(Math.random() * opts.length));
return ret;
}
if(typeof cb === 'function'){
var text = _randPass(l);
return cb(text);
}
return new Promise(function(resolve){
resolve(_randPass(l))
});
},
schemaTypes: {
/**
* shortDate - Mongoose getter. Usage: createdDate : { type: Date, required: true, default: Date.now, get: require('ksplitt').schemaTypes.shortDate }
* @param val
* @returns {string}
*/
shortDate: function (val) {
if (!val) return val;
return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
}
}
};