-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
collection.js
218 lines (184 loc) · 4.78 KB
/
collection.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict';
var Base = require('base');
var debug = require('debug')('base:templates:collection');
var plugin = require('./plugins');
var utils = require('./utils');
/**
* Expose `Collection`
*/
module.exports = exports = Collection;
/**
* Create an instance of `Collection` with the given `options`.
*
* ```js
* var collection = new Collection();
* collection.addItem('foo', {content: 'bar'});
* ```
* @param {Object} `options`
* @api public
*/
function Collection(options) {
if (!(this instanceof Collection)) {
return new Collection(options);
}
Base.call(this, {}, options);
this.is('Collection');
this.items = {};
this.use(utils.option());
this.use(utils.plugin());
this.init(options || {});
}
/**
* Inherit `Base`
*/
Base.extend(Collection);
/**
* Mixin static methods
*/
plugin.is(Collection);
/**
* Initialize `Collection` defaults
*/
Collection.prototype.init = function(opts) {
debug('initializing', __filename);
// add constructors to the instance
this.define('Item', opts.Item || Collection.Item);
this.define('View', opts.View || Collection.View);
this.use(plugin.renameKey());
this.use(plugin.item('item', 'Item', {emit: false}));
// if an instance of `List` or `Collection` is passed, load it now
if (Array.isArray(opts) || opts.isList) {
this.options = opts.options;
this.addList(opts.items);
} else if (opts.isCollection) {
this.options = opts.options;
this.addItems(opts.items);
} else {
this.options = opts;
}
};
/**
* Add an item to the collection.
*
* ```js
* collection.addItem('foo', {content: 'bar'});
* ```
* @emits `item` With the created `item` and `collection` instance as arguments.
* @param {String|Object} `key` Item name or object
* @param {Object} `val` Item object, when `key` is a string.
* @developer The `item` method is decorated onto the collection using the `item` plugin
* @return {Object} returns the `item` instance.
* @api public
*/
Collection.prototype.addItem = function(key, val) {
debug('adding item "%s"');
var item = this.item(key, val);
if (typeof item.use === 'function') {
this.run(item);
}
this.emit('item', item, this);
this.items[item.key] = item;
return item;
};
/**
* Identical to `.addItem`, except the collection instance is returned instead of
* the item, to allow chaining.
*
* ```js
* collection.setItem('foo', {content: 'bar'});
* ```
* @emits `item` With the created `item` and `collection` instance as arguments.
* @param {String|Object} `key` Item name or object
* @param {Object} `val` Item object, when `key` is a string.
* @return {Object} returns the `collection` instance.
* @api public
*/
Collection.prototype.setItem = function(/*key, value*/) {
this.addItem.apply(this, arguments);
return this;
};
/**
* Get an item from `collection.items`.
*
* ```js
* collection.getItem('a.html');
* ```
* @param {String} `key` Key of the item to get.
* @return {Object}
* @api public
*/
Collection.prototype.getItem = function(key) {
return this.items[key] || this.items[this.renameKey(key)];
};
/**
* Remove an item from `collection.items`.
*
* ```js
* items.deleteItem('abc');
* ```
* @param {String} `key`
* @return {Object} Returns the instance for chaining
* @api public
*/
Collection.prototype.deleteItem = function(item) {
if (typeof item === 'string') {
item = this.getItem(item);
}
delete this.items[item.key];
return this;
};
/**
* Load multiple items onto the collection.
*
* ```js
* collection.addItems({
* 'a.html': {content: '...'},
* 'b.html': {content: '...'},
* 'c.html': {content: '...'}
* });
* ```
* @param {Object|Array} `items`
* @return {Object} returns the instance for chaining
* @api public
*/
Collection.prototype.addItems = function(items) {
if (Array.isArray(items)) {
return this.addList.apply(this, arguments);
}
this.visit('addItem', items);
return this;
};
/**
* Load an array of items onto the collection.
*
* ```js
* collection.addList([
* {path: 'a.html', content: '...'},
* {path: 'b.html', content: '...'},
* {path: 'c.html', content: '...'}
* ]);
* ```
* @param {Array} `items` or an instance of `List`
* @param {Function} `fn` Optional sync callback function that is called on each item.
* @return {Object} returns the Collection instance for chaining
* @api public
*/
Collection.prototype.addList = function(list, fn) {
if (!Array.isArray(list)) {
throw new TypeError('expected list to be an array.');
}
if (typeof fn !== 'function') {
fn = utils.identity;
}
var len = list.length;
var idx = -1;
while (++idx < len) {
this.addItem(fn(list[idx]));
}
return this;
};
/**
* Expose static properties
*/
utils.define(Collection, 'Item', require('vinyl-item'));
utils.define(Collection, 'View', require('vinyl-view'));