-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (165 loc) · 3.61 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* !
* attribute
* Attribute setter/getter component.
*
* @author Enrico Marino and Federico Spini
* @copyright 2012 Enrico Marino and Federico Spini
* @licence MIT
*/
/*
* Module dependencies.
*/
var Emitter = require('emitter');
/*
* Expose `Attribute`.
*/
module.exports = Attribute;
/*
* Mixin emitter.
*/
Emitter(Attribute.prototype);
/*
* Initialize a new `Attribute`.
*
* @api public
*/
function Attribute(obj) {
if (obj) return mixin(obj);
}
/*
* Mixin the Attribute properties.
*
* @param {Object} obj
* @return {Object}
* @api private
*/
function mixin(obj) {
var proto = Attribute.prototype;
var key;
for (key in proto) {
obj[key] = proto[key];
}
return obj;
}
/*
* get
* Get the attribute value.
*
* @param {String} key attribute name
* @return {Any}
* @api public
*/
Attribute.prototype.get = function(key) {
this._attributes = this._attributes || {};
return this._attributes[key];
};
/*
* set
* Set the attribute value.
*
* @param {String} key attribute name
* @param {*} value attribute value
* @param {Object} options options
* @param {Boolean} [options.silent]
* @return {Object} this, for chaining
* @api public
*/
Attribute.prototype.set = function(key, value, options) {
this._attributes = this._attributes || {};
if (key == null) {
return this;
}
if (typeof key === 'object') {
return this.set_all(key, value);
}
var attributes = this._attributes;
var silent = options && options.silent;
var previous = attributes[key];
var created = !(key in attributes);
var changed = created || previous !== value;
if (changed) {
attributes[key] = value;
}
if (changed && !silent) {
this.emit('change:' + key, this, value, previous);
this.emit('change', this);
}
return this;
};
/*
* set_all
* Set the attribute values.
*
* @param {Object} values attribute values
* @param {Object} options options
* @param {Boolean} [options.silent]
* @return {Object} this, for chaining
* @api public
*/
Attribute.prototype.set_all = function(values, options) {
this._attributes = this._attributes || {};
if (values == null) {
return this;
}
var silent = options && options.silent;
var attributes = this._attributes;
var key;
var value;
var previous;
var changed;
var created;
var emitted;
for (key in values) {
previous = attributes[key];
value = values[key];
created = !(key in attributes);
changed = created || previous !== value;
if (changed) {
attributes[key] = value;
}
if (changed && !silent) {
this.emit('change:' + key, this, value, previous);
emitted = true;
}
}
if (emitted) {
this.emit('change', this);
}
return this;
};
/*
* del
* Delete attribute `key`.
*
* @param {String} key key
* @param {Object} options options
* @param {Boolean} [options.silent]
* @return {Attribute} this, for chaining.
* @api public
*/
Attribute.prototype.del = function(key, options) {
this._attributes = this._attributes || {};
var attributes = this._attributes;
var silent = options && options.silent;
var value = attributes[key];
var present = value != null;
if (present) {
delete attributes[key];
}
if (present && !silent) {
this.emit('delete:' + key, this, value);
}
return this;
};
/*
* has
* Return `true` if attributes contains `key`, `false` otherwise.
*
* @param {String} key key
* @return {Boolean} `true` if attributes contains `key`, `false` otherwise.
* @api public
*/
Attribute.prototype.has = function(key) {
this._attributes = this._attributes || {};
return this._attributes[key] != null;
};