diff --git a/examples/ReactExample/components/todo-list-item.js b/examples/ReactExample/components/todo-list-item.js index 155063cc..38a879ef 100644 --- a/examples/ReactExample/components/todo-list-item.js +++ b/examples/ReactExample/components/todo-list-item.js @@ -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() { diff --git a/lib/index.js b/lib/index.js index ed3bf513..bf0e617d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; diff --git a/tests/js/list-tests.js b/tests/js/list-tests.js index 2441486d..cf4eaeb3 100644 --- a/tests/js/list-tests.js +++ b/tests/js/list-tests.js @@ -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'); + }); + }, }); diff --git a/tests/js/query-tests.js b/tests/js/query-tests.js index 1d2c0d81..df0fb503 100644 --- a/tests/js/query-tests.js +++ b/tests/js/query-tests.js @@ -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") { diff --git a/tests/js/results-tests.js b/tests/js/results-tests.js index 571a5447..4157d664 100644 --- a/tests/js/results-tests.js +++ b/tests/js/results-tests.js @@ -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; }); }; diff --git a/tests/js/schemas.js b/tests/js/schemas.js index f2dc9478..707d21c5 100644 --- a/tests/js/schemas.js +++ b/tests/js/schemas.js @@ -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 = {