utils.js
4.78 KB
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
var util = require("util")
, DataTypes = require("./data-types")
, SqlString = require("./sql-string")
var Utils = module.exports = {
_: (function() {
var _ = require("lodash")
, _s = require('underscore.string')
_.mixin(_s.exports())
_.mixin({
includes: _s.include,
camelizeIf: function(string, condition) {
var result = string
if (condition) {
result = _.camelize(string)
}
return result
},
underscoredIf: function(string, condition) {
var result = string
if (condition) {
result = _.underscored(string)
}
return result
}
})
return _
})(),
addEventEmitter: function(_class) {
util.inherits(_class, require('events').EventEmitter)
},
TICK_CHAR: '`',
addTicks: function(s, tickChar) {
tickChar = tickChar || Utils.TICK_CHAR
return tickChar + Utils.removeTicks(s, tickChar) + tickChar
},
removeTicks: function(s, tickChar) {
tickChar = tickChar || Utils.TICK_CHAR
return s.replace(new RegExp(tickChar, 'g'), "")
},
escape: function(s) {
return SqlString.escape(s, true, "local").replace(/\\"/g, '"')
},
format: function(arr, dialect) {
var timeZone = null;
return SqlString.format(arr.shift(), arr, timeZone, dialect)
},
isHash: function(obj) {
return Utils._.isObject(obj) && !Array.isArray(obj);
},
pad: function (s) {
return s < 10 ? '0' + s : s
},
toSqlDate: function(date) {
return date.getUTCFullYear() + '-' +
this.pad(date.getUTCMonth()+1) + '-' +
this.pad(date.getUTCDate()) + ' ' +
this.pad(date.getUTCHours()) + ':' +
this.pad(date.getUTCMinutes()) + ':' +
this.pad(date.getUTCSeconds())
},
argsArePrimaryKeys: function(args, primaryKeys) {
var result = (args.length == Object.keys(primaryKeys).length)
if (result) {
Utils._.each(args, function(arg) {
if (result) {
if (['number', 'string'].indexOf(typeof arg) !== -1) {
result = true
} else {
result = (arg instanceof Date)
}
}
})
}
return result
},
combineTableNames: function(tableName1, tableName2) {
return (tableName1.toLowerCase() < tableName2.toLowerCase()) ? (tableName1 + tableName2) : (tableName2 + tableName1)
},
singularize: function(s) {
return Utils.Lingo.en.isSingular(s) ? s : Utils.Lingo.en.singularize(s)
},
pluralize: function(s) {
return Utils.Lingo.en.isPlural(s) ? s : Utils.Lingo.en.pluralize(s)
},
removeCommentsFromFunctionString: function(s) {
s = s.replace(/\s*(\/\/.*)/g, '')
s = s.replace(/(\/\*[\n\r\s\S]*?\*\/)/mg, '')
return s
},
toDefaultValue: function(value) {
return (value === DataTypes.NOW) ? Utils.now() : value
},
setAttributes: function(hash, identifier, instance, prefix) {
prefix = prefix || ''
if (this.isHash(identifier)) {
this._.each(identifier, function(elem, key) {
hash[prefix + key] = Utils._.isString(instance) ? instance : Utils._.isObject(instance) ? instance[elem.key || elem] : null
})
} else {
hash[prefix + identifier] = Utils._.isString(instance) ? instance : Utils._.isObject(instance) ? instance.id : null
}
return hash
},
removeNullValuesFromHash: function(hash, omitNull) {
var result = hash
if (omitNull) {
var _hash = {}
Utils._.each(hash, function(val, key) {
if (key.match(/Id$/) || ((val !== null) && (val !== undefined))) {
_hash[key] = val;
}
})
result = _hash
}
return result
},
prependTableNameToHash: function(tableName, hash) {
if (tableName) {
var _hash = {}
for (var key in hash) {
if (key.indexOf('.') === -1) {
_hash[tableName + '.' + key] = hash[key]
} else {
_hash[key] = hash[key]
}
}
return _hash
} else {
return hash
}
},
firstValueOfHash: function(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return obj[key]
}
return null
},
inherit: function(subClass, superClass) {
if (superClass.constructor == Function) {
// Normal Inheritance
subClass.prototype = new superClass();
subClass.prototype.constructor = subClass;
subClass.prototype.parent = superClass.prototype;
} else {
// Pure Virtual Inheritance
subClass.prototype = superClass;
subClass.prototype.constructor = subClass;
subClass.prototype.parent = superClass;
}
return subClass;
},
now: function() {
var now = new Date()
now.setMilliseconds(0)
return now
}
}
Utils.CustomEventEmitter = require("./emitters/custom-event-emitter")
Utils.QueryChainer = require("./query-chainer")
Utils.Lingo = require("lingo")