Skip to content

Commit 4a47e52

Browse files
committed
Initial commit
0 parents  commit 4a47e52

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
components
2+
build

History.md

Whitespace-only changes.

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
build: components index.js
3+
@component build --dev
4+
5+
components: component.json
6+
@component install --dev
7+
8+
clean:
9+
rm -fr build components template.js
10+
11+
.PHONY: clean

Readme.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# collection
3+
4+
Enumerable data model collections
5+
6+
## Installation
7+
8+
$ component install component/collection
9+
10+
## API
11+
12+
### Collection([models])
13+
14+
Initialize a new collection with the given `models`.
15+
16+
### Collection#length()
17+
18+
Return the collection length.
19+
20+
### Collection#push(model:Object)
21+
22+
Add `model` to the collection and return the index.
23+
24+
## License
25+
26+
MIT

component.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "collection",
3+
"repo": "component/collection",
4+
"description": "Enumerable data model collections",
5+
"version": "0.0.1",
6+
"keywords": ["enumerable", "data", "model", "db"],
7+
"dependencies": {
8+
"component/enumerable": "*"
9+
},
10+
"development": {},
11+
"license": "MIT",
12+
"scripts": [
13+
"index.js"
14+
]
15+
}

index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var Enumerable = require('enumerable');
7+
8+
/**
9+
* Expose `Collection`.
10+
*/
11+
12+
module.exports = Collection;
13+
14+
/**
15+
* Initialize a new collection with the given `models`.
16+
*
17+
* @param {Array} models
18+
* @api public
19+
*/
20+
21+
function Collection(models) {
22+
this.models = models || [];
23+
}
24+
25+
/**
26+
* Mixin enumerable.
27+
*/
28+
29+
Enumerable(Collection.prototype);
30+
31+
/**
32+
* Iterator implementation.
33+
*/
34+
35+
Collection.prototype.__iterate__ = function(){
36+
var self = this;
37+
return {
38+
length: function(){ return self.length() },
39+
get: function(i){ return self.models[i] }
40+
}
41+
};
42+
43+
/**
44+
* Return the collection length.
45+
*
46+
* @return {Number}
47+
* @api public
48+
*/
49+
50+
Collection.prototype.length = function(){
51+
return this.models.length;
52+
};
53+
54+
/**
55+
* Add `model` to the collection and return the index.
56+
*
57+
* @param {Object} model
58+
* @return {Number}
59+
* @api public
60+
*/
61+
62+
Collection.prototype.push = function(model){
63+
return this.models.push(model);
64+
};

0 commit comments

Comments
 (0)