utils.js
4.37 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
var mysql = require("mysql")
, connection = mysql.createConnection({})
, util = require("util")
, DataTypes = require("./data-types")
var Utils = module.exports = {
_: (function() {
var _ = require("underscore")
, _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) {
return Utils.TICK_CHAR + Utils.removeTicks(s) + Utils.TICK_CHAR
},
removeTicks: function(s) {
return s.replace(new RegExp(Utils.TICK_CHAR, 'g'), "")
},
escape: function(s) {
return connection.escape(s).replace(/\\"/g, '"')
},
format: function(arr) {
return connection.format.apply(connection, [arr.shift(), arr])
},
isHash: function(obj) {
return Utils._.isObject(obj) && !Array.isArray(obj);
},
toSqlDate: function(date) {
return [
[
date.getFullYear(),
((date.getMonth() < 9 ? '0' : '') + (date.getMonth()+1)),
((date.getDate() < 10 ? '0' : '') + date.getDate())
].join("-"),
date.toLocaleTimeString()
].join(" ")
},
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) ? new Date() : 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
}
},
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;
}
}
Utils.CustomEventEmitter = require("./emitters/custom-event-emitter")
Utils.QueryChainer = require("./query-chainer")
Utils.Lingo = require("lingo")