Files
react-native-devtools-frontend/front_end/platform/array-utilities.js
T
Simon Zünd e6c698951f [utilities] Port Array#remove to a ArrayUtilities.removeElement helper
This CL is part of a cleanup effort to get rid of Array#remove. The
code for the helper and the unit test is duplicated for now. Once
all use-sites of Array#remove are cleaned up, we can remove
Array#remove itself (plus the unit test).

R=jacktfranklin@chromium.org

Bug: chromium:1050549
Change-Id: Icef15af1a85d15e2d833a5ac2ba95e3591bb256a
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2094124
Commit-Queue: Simon Zünd <szuend@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
2020-03-09 13:39:37 +00:00

29 lines
779 B
JavaScript

// Copyright (c) 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @param {!Array<T>} array
* @param {!T} element
* @param {boolean=} firstOnly Only remove the first occurrence of `element` from the array.
* @return {boolean} True, if any element got removed.
* @template T
*/
export const removeElement = (array, element, firstOnly) => {
let index = array.indexOf(element);
if (index === -1) {
return false;
}
if (firstOnly) {
array.splice(index, 1);
return true;
}
for (let i = index + 1, n = array.length; i < n; ++i) {
if (array[i] !== element) {
array[index++] = array[i];
}
}
array.length = index;
return true;
};