Support many Array methods on List and Results

Resolves #18
This commit is contained in:
Scott Kyle 2016-02-28 21:40:27 -08:00
parent 236d0a5eed
commit 4ea2e729b4
6 changed files with 93 additions and 8 deletions

View File

@ -40,7 +40,7 @@ export default class TodoListItem extends React.Component {
get done() {
let items = this.props.item.items;
return items.length > 0 && Array.prototype.every.call(items, (item) => item.done);
return items.length > 0 && items.every((item) => item.done);
}
get text() {

View File

@ -18,12 +18,44 @@
'use strict';
var realmConstructor;
if (typeof Realm != 'undefined') {
// The global Realm constructor should be available on device (using JavaScriptCore).
module.exports = Realm; // eslint-disable-line no-undef
} else if (navigator.userAgent) {
realmConstructor = Realm; // eslint-disable-line no-undef
} else if (typeof navigator != 'undefined' && navigator.userAgent) { // eslint-disable-line no-undef
// The userAgent will be defined when running in a browser (such as Chrome debugging mode).
module.exports = require('./browser').default; // (exported as ES6 module)
realmConstructor = require('./browser').default; // (exported as ES6 module)
} else {
throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!');
}
var arrayPrototype = Array.prototype;
var arrayMethods = {};
[
'join',
'slice',
'forEach',
'every',
'some',
'find',
'findIndex',
'map',
'reduce',
'reduceRight',
'entries',
'keys',
'values',
].forEach(function(methodName) {
var method = arrayPrototype[methodName];
if (method) {
arrayMethods[methodName] = {value: method};
}
});
// Add the specified Array methods to the prototype of List and Results.
Object.defineProperties(realmConstructor.List.prototype, arrayMethods);
Object.defineProperties(realmConstructor.Results.prototype, arrayMethods);
module.exports = realmConstructor;

View File

@ -511,7 +511,7 @@ module.exports = BaseTest.extend({
});
var names = function(results, prop) {
return Array.prototype.map.call(results, function(object) {
return results.map(function(object) {
return object.name;
});
};
@ -522,4 +522,54 @@ module.exports = BaseTest.extend({
objects = list.sorted(['age', 'name']);
TestCase.assertArraysEqual(names(objects), ['Ari', 'Tim', 'Alex', 'Bjarne']);
},
testArrayMethods: function() {
var realm = new Realm({schema: [schemas.PersonObject, schemas.PersonList]});
var object;
realm.write(function() {
object = realm.create('PersonList', {list: [
{name: 'Ari', age: 10},
{name: 'Tim', age: 11},
{name: 'Bjarne', age: 12},
]});
});
[
object.list,
realm.objects('PersonObject'),
].forEach(function(list) {
TestCase.assertEqual(list.slice().length, 3);
TestCase.assertEqual(list.slice(-1).length, 1);
TestCase.assertEqual(list.slice(-1)[0].age, 12);
TestCase.assertEqual(list.slice(1, 3).length, 2);
TestCase.assertEqual(list.slice(1, 3)[1].age, 12);
TestCase.assertEqual(list.join(' '), 'Ari Tim Bjarne');
var count = 0;
list.forEach(function(p, i) {
TestCase.assertEqual(p.name, list[i].name);
count++;
});
TestCase.assertEqual(count, list.length);
TestCase.assertArraysEqual(list.map(function(p) {return p.age}), [10, 11, 12]);
TestCase.assertTrue(list.some(function(p) {return p.age > 10}));
TestCase.assertTrue(list.every(function(p) {return p.age > 0}));
var person = list.find(function(p) {return p.name == 'Tim'});
TestCase.assertEqual(person.name, 'Tim');
var index = list.findIndex(function(p) {return p.name == 'Tim'});
TestCase.assertEqual(index, 1);
TestCase.assertEqual(list.reduce(function(n, p) {return n + p.age}, 0), 33);
TestCase.assertEqual(list.reduceRight(function(n, p) {return n + p.age}, 0), 33);
TestCase.assertEqual(list.entries().next().value[1].name, 'Ari');
TestCase.assertEqual(list.keys().next().value, 0);
TestCase.assertEqual(list.values().next().value.name, 'Ari');
});
},
});

View File

@ -97,8 +97,8 @@ function runQuerySuite(suite) {
throw "Primary key required for object comparison";
}
TestCase.assertArraysEqual(test[1], Array.prototype.map.call(results, function(el) {
return el[primary]
TestCase.assertArraysEqual(test[1], results.map(function(el) {
return el[primary];
}));
}
else if (test[0] == "QueryThrows") {

View File

@ -180,7 +180,7 @@ module.exports = BaseTest.extend({
});
var primaries = function(results, prop) {
return Array.prototype.map.call(results, function(object) {
return results.map(function(object) {
return object.primaryCol;
});
};

View File

@ -39,6 +39,9 @@ PersonObject.schema = {
PersonObject.prototype.description = function() {
return this.name + ' ' + this.age;
};
PersonObject.prototype.toString = function() {
return this.name;
};
exports.PersonObject = PersonObject;
exports.PersonList = {