diff --git a/CHANGELOG.md b/CHANGELOG.md index 273f4c29..e507ec05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/list.js b/docs/list.js index 79ee73cc..e3b5b873 100644 --- a/docs/list.js +++ b/docs/list.js @@ -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 object’s type must match diff --git a/src/js_list.cpp b/src/js_list.cpp index 4a8ca1b0..7acbdadf 100644 --- a/src/js_list.cpp +++ b/src/js_list.cpp @@ -174,14 +174,20 @@ JSValueRef ListSplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb List *list = RJSGetInternal(thisObject); size_t size = list->size(); - RJSValidateArgumentCountIsAtLeast(argumentCount, 2); + RJSValidateArgumentCountIsAtLeast(argumentCount, 1); long index = std::min(RJSValidatedValueToNumber(ctx, arguments[0]), size); if (index < 0) { index = std::max(size + index, 0); } - long remove = std::max(RJSValidatedValueToNumber(ctx, arguments[1]), 0); - remove = std::min(remove, size - index); + long remove; + if (argumentCount < 2) { + remove = size - index; + } + else { + remove = std::max(RJSValidatedValueToNumber(ctx, arguments[1]), 0); + remove = std::min(remove, size - index); + } std::vector removedObjects(remove); for (size_t i = 0; i < remove; i++) { diff --git a/tests/js/list-tests.js b/tests/js/list-tests.js index 34d79028..0a93c913 100644 --- a/tests/js/list-tests.js +++ b/tests/js/list-tests.js @@ -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); });