add List.filtered

This commit is contained in:
Ari Lazier 2016-02-17 20:24:08 -08:00
parent d838d61de4
commit 6d02614eaa
2 changed files with 59 additions and 0 deletions

View File

@ -8,6 +8,9 @@
#include "js_util.hpp"
#include "object_accessor.hpp"
#include "parser.hpp"
#include "query_builder.hpp"
#include <assert.h>
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
@ -200,6 +203,34 @@ JSValueRef ListStaticResults(JSContextRef ctx, JSObjectRef function, JSObjectRef
return NULL;
}
JSValueRef ListFiltered(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
try {
List *list = RJSGetInternal<List *>(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
std::string queryString = RJSValidatedStringForValue(ctx, arguments[0], "predicate");
std::vector<JSValueRef> args;
for (size_t i = 1; i < argumentCount; i++) {
args.push_back(arguments[i]);
}
parser::Predicate predicate = parser::parse(queryString);
query_builder::ArgumentConverter<JSValueRef, JSContextRef> arguments(ctx, args);
Query query = list->get_query();
query_builder::apply_predicate(query, predicate, arguments, *sharedRealm->config().schema, list->get_object_schema().name);
return RJSResultsCreate(ctx, sharedRealm, list->get_object_schema(), query);
}
catch (std::exception &exp) {
if (jsException) {
*jsException = RJSMakeError(ctx, exp);
}
}
return NULL;
}
JSObjectRef RJSListCreate(JSContextRef ctx, realm::List &list) {
return RJSWrapObject<List *>(ctx, RJSListClass(), new List(list));
}
@ -210,6 +241,7 @@ static const JSStaticFunction RJSListFuncs[] = {
{"shift", ListShift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"unshift", ListUnshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"splice", ListSplice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"filtered", ListFiltered, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"snapshot", ListStaticResults, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL},
};

View File

@ -443,4 +443,31 @@ module.exports = BaseTest.extend({
TestCase.assertEqual(arrayCopy[0], null);
});
},
testListFiltered: function() {
var personListSchema = {
name: 'PersonList',
properties: {
list: {type: 'list', objectType: 'PersonObject'}
}
};
var realm = new Realm({schema: [schemas.PersonObject, personListSchema]});
var listObject;
realm.write(function() {
listObject = realm.create('PersonList', {list: [
{name: 'Ari', age: 10},
{name: 'Tim', age: 11},
{name: 'Bjarne', age: 12},
{name: 'Alex', age: 12, married: true}
]});
realm.create('PersonObject', {name: 'NotInList', age: 10});
});
var list = listObject.list;
TestCase.assertEqual(list.filtered("truepredicate").length, 4);
TestCase.assertEqual(list.filtered('age = 11')[0].name, 'Tim');
TestCase.assertEqual(list.filtered('age = 12').length, 2);
TestCase.assertEqual(list.filtered('age > 10 && age < 13').length, 3);
TestCase.assertEqual(list.filtered('age > 10').filtered('age < 13').length, 3);
},
});