forked from documentcloud/underscore-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderscore.object.selectors.js
108 lines (80 loc) · 3.01 KB
/
underscore.object.selectors.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
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
// Underscore-contrib (underscore.object.selectors.js 0.0.1)
// (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors
// Underscore-contrib may be freely distributed under the MIT license.
(function(root) {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `global` on the server.
var _ = root._ || require('underscore');
// Helpers
// -------
// Create quick reference variables for speed access to core prototypes.
var concat = Array.prototype.concat;
var ArrayProto = Array.prototype;
var slice = ArrayProto.slice;
// Mixing in the object selectors
// ------------------------------
_.mixin({
// Returns a function that will attempt to look up a named field
// in any object that it's given.
accessor: function(field) {
return function(obj) {
return (obj && obj[field]);
};
},
// Given an object, returns a function that will attempt to look up a field
// that it's given.
dictionary: function (obj) {
return function(field) {
return (obj && field && obj[field]);
};
},
// Like `_.pick` except that it takes an array of keys to pick.
selectKeys: function (obj, ks) {
return _.pick.apply(null, concat.call([obj], ks));
},
// Returns the key/value pair for a given property in an object, undefined if not found.
kv: function(obj, key) {
if (_.has(obj, key)) {
return [key, obj[key]];
}
return void 0;
},
// Gets the value at any depth in a nested object based on the
// path described by the keys given. Keys may be given as an array
// or as a dot-separated string.
getPath: function getPath (obj, ks) {
if (typeof ks == "string") ks = ks.split(".");
// If we have reached an undefined property
// then stop executing and return undefined
if (obj === undefined) return void 0;
// If the path array has no more elements, we've reached
// the intended property and return its value
if (ks.length === 0) return obj;
// If we still have elements in the path array and the current
// value is null, stop executing and return undefined
if (obj === null) return void 0;
return getPath(obj[_.first(ks)], _.rest(ks));
},
// Returns a boolean indicating whether there is a property
// at the path described by the keys given
hasPath: function hasPath (obj, ks) {
if (typeof ks == "string") ks = ks.split(".");
var numKeys = ks.length;
if (obj == null && numKeys > 0) return false;
if (!(ks[0] in obj)) return false;
if (numKeys === 1) return true;
return hasPath(obj[_.first(ks)], _.rest(ks));
},
pickWhen: function(obj, pred) {
var copy = {};
_.each(obj, function(value, key) {
if (pred(obj[key])) copy[key] = obj[key];
});
return copy;
},
omitWhen: function(obj, pred) {
return _.pickWhen(obj, function(e) { return !pred(e); });
}
});
})(this);