-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (43 loc) · 1.58 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
var assert = require('assert');
var assertType = function(type, val, name) {
assert(typeof type === 'string', 'Type must be a string');
assert(typeof val === type, name.concat(' is no ').concat(type));
return true;
};
var assertObj = assertType.bind(null, 'object');
var assertFunc = assertType.bind(null, 'function');
//------------------------------------------------------------
var exports = module.exports = {};
//------------------------------------------------------------
exports.camelCase = function camelCase(snakeCase){
assert( typeof snakeCase === 'string', 'argument is not a string');
var tokens = snakeCase.split(/_+/).filter(function(token){
return (token.length >= 1);
});
if (tokens.length <= 1) {
return snakeCase;
}
var first = tokens.shift().toLowerCase();
var rest = tokens.map(function(token){
return token.charAt(0).toUpperCase().concat(
token.substring(1).toLowerCase()
);
}).join('');
return first.concat(rest);
};
//------------------------------------------------------------
exports.inject = function inject(pg) {
assertObj(pg.Query.prototype, 'Query.prototype');
var queryProto = pg.Query.prototype;
assertFunc(queryProto.handleRowDescription, 'Query.prototype.handleRowDesription');
var handleRowDesc = queryProto.handleRowDescription;
queryProto.handleRowDescription = function(msg) {
msg.fields.forEach(function(field){
field.name = exports.camelCase(field.name);
});
return handleRowDesc.call(this, msg);
};
return function restore() {
queryProto.handleRowDescription = handleRowDesc;
};
};