Merge pull request #336 from realm/sk-list-splice

List splice method now accepts single argument and is documented
This commit is contained in:
Scott Kyle 2016-03-17 15:13:25 -07:00
commit d7d53c4d18
4 changed files with 32 additions and 4 deletions

View File

@ -14,6 +14,7 @@
* Fix for using Chrome debug mode from a device
* Automatically forward port 8082 for Android
* Fix broken iterator methods on Android
* Fix for List splice method not accepting a single argument
* Don't download or unpack core libraries unnecessarily

View File

@ -16,7 +16,6 @@
//
////////////////////////////////////////////////////////////////////////////
/**
* Instances of this class will be returned when accessing object properties whose type is `"list"`
* (see {@linkplain Realm~ObjectSchemaProperty ObjectSchemaProperty}).
@ -89,6 +88,20 @@ class List {
*/
push(...object) {}
/**
* Changes the contents of the list by removing objects and/or inserting new objects.
* @see {@linkcode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice Array.prototype.splice}
* @param {number} index - The start index. If greater than the length of the list,
* the start index will be set to the length instead. If negative, then the start index
* will be counted from the end of the list (e.g. `list.length - index`).
* @param {number} [count] - The number of objects to remove from the list. If not provided,
* then all objects from the start index through the end of the list will be removed.
* @param {...Realm.Object} [object] - Objects to insert into the list starting at `index`.
* @returns {Realm.Object[]} containing the objects that were removed from the list. The
* array is empty if no objects were removed.
*/
splice(index, count, ...object) {}
/**
* Add one or more objects to the _beginning_ of the list.
* @param {...Realm.Object} object - Each objects type must match

View File

@ -174,14 +174,20 @@ JSValueRef ListSplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb
List *list = RJSGetInternal<List *>(thisObject);
size_t size = list->size();
RJSValidateArgumentCountIsAtLeast(argumentCount, 2);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
long index = std::min<long>(RJSValidatedValueToNumber(ctx, arguments[0]), size);
if (index < 0) {
index = std::max<long>(size + index, 0);
}
long remove = std::max<long>(RJSValidatedValueToNumber(ctx, arguments[1]), 0);
remove = std::min<long>(remove, size - index);
long remove;
if (argumentCount < 2) {
remove = size - index;
}
else {
remove = std::max<long>(RJSValidatedValueToNumber(ctx, arguments[1]), 0);
remove = std::min<long>(remove, size - index);
}
std::vector<JSObjectRef> removedObjects(remove);
for (size_t i = 0; i < remove; i++) {

View File

@ -344,6 +344,14 @@ module.exports = BaseTest.extend({
TestCase.assertEqual(removed.length, 0);
TestCase.assertEqual(array.length, 1);
removed = array.splice(1);
TestCase.assertEqual(removed.length, 0);
TestCase.assertEqual(array.length, 1);
removed = array.splice(0);
TestCase.assertEqual(removed.length, 1);
TestCase.assertEqual(array.length, 0);
TestCase.assertThrows(function() {
array.splice('cat', 1);
});