Nh/is empty (#1820)

* adding isEmpty method to Result and List
This commit is contained in:
Nabil Hachicha 2018-05-29 12:24:54 +01:00 committed by GitHub
parent 58671dd59e
commit 9b9d7e6471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 1 deletions

View File

@ -6,6 +6,7 @@ x.x.x Release notes (xxxx-xx-xx)
* None.
### Enhancements
* Added `isEmpty()` method on `Realm.Results` and `Realm.List`.
* Added schema change listener to `Realm.addListener()` (#1825).
### Bug fixes

View File

@ -63,6 +63,13 @@ class Collection {
*/
isValid() {}
/**
* Checks if this collection is empty.
* @returns {boolean} indicating if the collection is empty or not.
* @since 2.7.0
*/
isEmpty() {}
/**
* Returns new _Results_ that represent this collection being filtered by the provided query.
*

5
lib/index.d.ts vendored
View File

@ -150,6 +150,11 @@ declare namespace Realm {
*/
isValid(): boolean;
/**
* @returns boolean
*/
isEmpty(): boolean;
min(property?: string): number | Date | null;
max(property?: string): number | Date | null;
sum(property?: string): number | null;

View File

@ -74,6 +74,7 @@ struct ListClass : ClassDefinition<T, realm::js::List<T>, CollectionClass<T>> {
static void filtered(ContextType, ObjectType, Arguments, ReturnValue &);
static void sorted(ContextType, ObjectType, Arguments, ReturnValue &);
static void is_valid(ContextType, ObjectType, Arguments, ReturnValue &);
static void is_empty(ContextType, ObjectType, Arguments, ReturnValue &);
static void index_of(ContextType, ObjectType, Arguments, ReturnValue &);
// observable
@ -93,6 +94,7 @@ struct ListClass : ClassDefinition<T, realm::js::List<T>, CollectionClass<T>> {
{"filtered", wrap<filtered>},
{"sorted", wrap<sorted>},
{"isValid", wrap<is_valid>},
{"isEmpty", wrap<is_empty>},
{"indexOf", wrap<index_of>},
{"min", wrap<compute_aggregate_on_collection<ListClass<T>, AggregateFunc::Min>>},
{"max", wrap<compute_aggregate_on_collection<ListClass<T>, AggregateFunc::Max>>},
@ -272,6 +274,11 @@ void ListClass<T>::is_valid(ContextType ctx, ObjectType this_object, Arguments a
return_value.set(get_internal<T, ListClass<T>>(this_object)->is_valid());
}
template<typename T>
void ListClass<T>::is_empty(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
return_value.set(get_internal<T, ListClass<T>>(this_object)->size() == 0);
}
template<typename T>
void ListClass<T>::index_of(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
auto fn = [&](auto&& row) {

View File

@ -87,6 +87,7 @@ struct ResultsClass : ClassDefinition<T, realm::js::Results<T>, CollectionClass<
static void filtered(ContextType, ObjectType, Arguments, ReturnValue &);
static void sorted(ContextType, ObjectType, Arguments, ReturnValue &);
static void is_valid(ContextType, ObjectType, Arguments, ReturnValue &);
static void is_empty(ContextType, ObjectType, Arguments, ReturnValue &);
#if REALM_ENABLE_SYNC
static void subscribe(ContextType, ObjectType, Arguments, ReturnValue &);
#endif
@ -115,6 +116,7 @@ struct ResultsClass : ClassDefinition<T, realm::js::Results<T>, CollectionClass<
{"filtered", wrap<filtered>},
{"sorted", wrap<sorted>},
{"isValid", wrap<is_valid>},
{"isEmpty", wrap<is_empty>},
#if REALM_ENABLE_SYNC
{"subscribe", wrap<subscribe>},
#endif
@ -283,6 +285,11 @@ void ResultsClass<T>::is_valid(ContextType ctx, ObjectType this_object, Argument
return_value.set(get_internal<T, ResultsClass<T>>(this_object)->is_valid());
}
template<typename T>
void ResultsClass<T>::is_empty(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
return_value.set(get_internal<T, ResultsClass<T>>(this_object)->size() == 0);
}
#if REALM_ENABLE_SYNC
template<typename T>
void ResultsClass<T>::subscribe(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {

View File

@ -1002,6 +1002,28 @@ module.exports = {
TestCase.assertThrowsContaining(() => list.length, 'invalidated');
},
testIsEmpty: function () {
const realm = new Realm({ schema: [schemas.PersonObject, schemas.PersonList] });
let object;
realm.write(() => {
object = realm.create('PersonList', {
list: [
]
});
});
TestCase.assertTrue(object.list.isEmpty());
realm.write(() => {
object.list = [
{ name: 'Bob', age: 42 },
{ name: 'Alice', age: 42 }
]
});
TestCase.assertFalse(object.list.isEmpty());
realm.close();
},
testListAggregateFunctions: function() {
const NullableBasicTypesList = {
name: 'NullableBasicTypesList',
@ -1301,5 +1323,5 @@ module.exports = {
TestCase.assertEqual(objects[1].list1[0], "Foo");
TestCase.assertEqual(objects[1].list2.length, 1);
TestCase.assertEqual(objects[1].list2[0], "Bar");
}
},
};

View File

@ -334,6 +334,17 @@ module.exports = {
});
},
testIsEmpty: function () {
let realm = new Realm({ schema: [schemas.TestObject] });
let result = realm.objects('TestObject');
TestCase.assertTrue(result.isEmpty());
realm.write(function () {
realm.create('TestObject', { doubleCol: 42 });
});
TestCase.assertFalse(result.isEmpty());
realm.close();
},
testResultsDeletedObjects: function() {
var realm = new Realm({schema: [schemas.TestObject]});