utils.js
2.65 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
var client = new (require("mysql").Client)()
, util = require("util")
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("`", "")
},
escape: function(s) {
return client.escape(s)
},
format: function(arr) {
var query = arr[0]
, replacements = Utils._.compact(arr.map(function(obj) { return obj != query ? obj : null}))
return client.format.apply(client, [query, replacements])
},
isHash: function(obj) {
return (typeof obj == 'object') && !obj.hasOwnProperty('length')
},
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 == Utils._.keys(primaryKeys).length)
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)
},
merge: function(a, b){
for(var key in b) {
a[key] = b[key]
}
return a
},
removeCommentsFromFunctionString: function(s) {
s = s.replace(/\s*(\/\/.*)/g, '')
s = s.replace(/(\/\*[\n\r\s\S]*?\*\/)/mg, '')
return s
}
}
Utils.CustomEventEmitter = require("./emitters/custom-event-emitter")
Utils.QueryChainer = require("./query-chainer")
Utils.Lingo = require("lingo")