object.js
3.75 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
// Generated by CoffeeScript 1.6.3
var object, _common,
__hasProp = {}.hasOwnProperty;
_common = require('./_common');
module.exports = object = {
isBareObject: _common.isBareObject.bind(_common),
/*
if object is an instance of a class
*/
isInstance: function(what) {
return !this.isBareObject(what);
},
/*
Alias to _common.typeOf
*/
typeOf: _common.typeOf.bind(_common),
/*
Alias to _common.clone
*/
clone: _common.clone.bind(_common),
/*
Empties an object of its properties.
*/
empty: function(o) {
var prop;
for (prop in o) {
if (o.hasOwnProperty(prop)) {
delete o[prop];
}
}
return o;
},
/*
Empties an object. Doesn't check for hasOwnProperty, so it's a tiny
bit faster. Use it for plain objects.
*/
fastEmpty: function(o) {
var property;
for (property in o) {
delete o[property];
}
return o;
},
/*
Overrides values fomr `newValues` on `base`, as long as they
already exist in base.
*/
overrideOnto: function(base, newValues) {
var key, newVal, oldVal;
if (!this.isBareObject(newValues) || !this.isBareObject(base)) {
return base;
}
for (key in base) {
oldVal = base[key];
newVal = newValues[key];
if (newVal === void 0) {
continue;
}
if (typeof newVal !== 'object' || this.isInstance(newVal)) {
base[key] = this.clone(newVal);
} else {
if (typeof oldVal !== 'object' || this.isInstance(oldVal)) {
base[key] = this.clone(newVal);
} else {
this.overrideOnto(oldVal, newVal);
}
}
}
return base;
},
/*
Takes a clone of 'base' and runs #overrideOnto on it
*/
override: function(base, newValues) {
return this.overrideOnto(this.clone(base), newValues);
},
append: function(base, toAppend) {
return this.appendOnto(this.clone(base), toAppend);
},
appendOnto: function(base, toAppend) {
var key, newVal, oldVal;
if (!this.isBareObject(toAppend) || !this.isBareObject(base)) {
return base;
}
for (key in toAppend) {
if (!__hasProp.call(toAppend, key)) continue;
newVal = toAppend[key];
if (newVal === void 0) {
continue;
}
if (typeof newVal !== 'object' || this.isInstance(newVal)) {
base[key] = newVal;
} else {
oldVal = base[key];
if (typeof oldVal !== 'object' || this.isInstance(oldVal)) {
base[key] = this.clone(newVal);
} else {
this.appendOnto(oldVal, newVal);
}
}
}
return base;
},
groupProps: function(obj, groups) {
var def, defs, grouped, key, name, shouldAdd, val, _i, _len;
grouped = {};
for (name in groups) {
defs = groups[name];
grouped[name] = {};
}
grouped['rest'] = {};
top: //;
for (key in obj) {
val = obj[key];
shouldAdd = false;
for (name in groups) {
defs = groups[name];
if (!Array.isArray(defs)) {
defs = [defs];
}
for (_i = 0, _len = defs.length; _i < _len; _i++) {
def = defs[_i];
if (typeof def === 'string') {
if (key === def) {
shouldAdd = true;
}
} else if (def instanceof RegExp) {
if (def.test(key)) {
shouldAdd = true;
}
} else if (def instanceof Function) {
if (def(key)) {
shouldAdd = true;
}
} else {
throw Error('Group definitions must either\
be strings, regexes, or functions.');
}
if (shouldAdd) {
grouped[name][key] = val;
continue top;
}
}
}
grouped['rest'][key] = val;
}
return grouped;
}
};