forked from SohumB/knex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
51 lines (42 loc) · 1.15 KB
/
helpers.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
// helpers.js
// -------
// Just some common functions needed in multiple places within the library.
var _ = require('lodash');
var helpers = {
// Pick off the attributes from only the current layer of the object.
skim: function(data) {
return _.map(data, function(obj) {
return _.pick(obj, _.keys(obj));
});
},
// Check if the first argument is an array, otherwise
// uses all arguments as an array.
normalizeArr: function() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i];
}
if (_.isArray(args[0])) {
return args[0];
}
return args;
},
// Used to signify deprecated functionality.
deprecate: function(msg) {
this.warn(msg);
},
// Used to warn about incorrect use, without error'ing
warn: function(msg) {
if (typeof console !== "undefined" && console !== null &&
typeof console.warn === "function") {
console.warn("Knex: " + msg);
}
},
// Sort the keys for the insert
sortObject: function(obj) {
return _.sortBy(_.pairs(obj), function(a) {
return a[0];
});
}
};
module.exports = helpers;