Skip to content

Commit

Permalink
Amends to list + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Jan 10, 2014
1 parent 21c114f commit 60e5d07
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 28 deletions.
67 changes: 39 additions & 28 deletions lib/list.js
@@ -1,3 +1,4 @@
var util = require('util');

module.exports = List;

Expand Down Expand Up @@ -30,13 +31,19 @@ function List(data, type, parent) {
value: 1
});

data = list.items = data || [];
var Item = list.ItemType = ListItem;

if (typeof type === 'object' && type.constructor.name === 'Array') {
list.ItemType = type[0] || ListItem;
ListItem = type[0] || ListItem;
}

data = list.items = data || [];
var Item = ListItem;
Object.defineProperty(list, 'ItemType', {
writable: true,
enumerable: false,
configurable: true,
value: ListItem
});

if ('string' === typeof data) {
try {
list.items = data = JSON.parse(data);
Expand Down Expand Up @@ -70,6 +77,10 @@ function List(data, type, parent) {

}

List.prototype.inspect = function() {
return util.inspect(this.items);
};

var _;
try {
var underscore = 'underscore';
Expand Down Expand Up @@ -156,15 +167,14 @@ if (_) {
});

List.prototype.find = function(pattern, field) {
if (field) {
var res;
this.items.forEach(function(o) {
if (o[field] == pattern) res = o;
});
return res;
} else {
return this.items[this.items.indexOf(pattern)];
if (!field) {
field = 'id';
}
var res;
this.items.forEach(function(o) {
if (o[field] == pattern) res = o;
});
return res;
};

List.prototype.removeAt = function(index) {
Expand Down Expand Up @@ -210,20 +220,20 @@ List.prototype.remove = function(obj) {
}
};

List.prototype.sort = function(cb) {
return this.items.sort(cb);
};

List.prototype.some = function(cb) {
return this.items.some(cb);
};

List.prototype.map = function(cb) {
if (typeof cb === 'function') return this.items.map(cb);
if (typeof cb === 'string') return this.items.map(function(el) {
if (typeof el[cb] === 'function') return el[cb]();
if (el.hasOwnProperty(cb)) return el[cb];
});
if (typeof cb === 'function') {
return this.items.map(cb);
}
if (typeof cb === 'string') {
return this.items.map(function(el) {
if (typeof el[cb] === 'function') {
return el[cb]();
}
if (el.hasOwnProperty(cb)) {
return el[cb];
}
});
}
};

function ListItem(data, parent) {
Expand All @@ -248,8 +258,9 @@ function ListItem(data, parent) {
}
}

this.save = function(c) {
parent.parent.save(c);
};
}

ListItem.prototype.save = function save() {
this.parent.parent.save(c);
};

63 changes: 63 additions & 0 deletions test/list.test.js
@@ -0,0 +1,63 @@
// This test written in mocha+should.js
var should = require('./init.js');

var db, Page;

describe('list', function() {

before(function() {
db = getSchema();
Page = db.define('Page', function(m) {
m.property('widgets', []);
});
});

it('should be exported to json just as "items"', function() {
var p = new Page({widgets: ['hello']});
JSON.stringify(p).should.equal(
'{"widgets":[{"id":"hello"}]}'
);
});

it('should push and remove object', function() {
var p = new Page({widgets: []});
p.widgets.push(7);
JSON.stringify(p.widgets).should.equal('[{"id":7}]');
p.widgets.remove(7);
JSON.stringify(p.widgets).should.equal('[]');
});

describe('#map', function() {

it('should collect field', function() {
var p = new Page({widgets: [{foo: 'bar'}, {foo: 'baz'}]});
p.widgets.map('foo').should.eql(['bar', 'baz']);
});

it('should work as usual js array map', function() {
var p = new Page({widgets: [{foo: 'bar'}, {foo: 'baz'}]});
p.widgets.map(function(x) {
return x.id;
}).should.eql([1, 2]);
});

});

describe('#find', function() {

it('should find object', function() {
var p = new Page({widgets: ['foo', 'bar', 'baz']});
JSON.stringify(
p.widgets.find('foo')
).should.eql('{"id":"foo"}');
});

it('should find object by property', function() {
var p = new Page({widgets: [{foo: 'bar'}, {foo: 'baz'}]});
JSON.stringify(
p.widgets.find('bar', 'foo')
).should.eql('{"foo":"bar","id":1}');
});

});
});

0 comments on commit 60e5d07

Please sign in to comment.