mattermost-mobile/app/mm-redux/utils/array_utils.ts
Shaz Amjad 64223efafe
MM-28474: Custom Sidebar Categories (#5460)
* Further cleanup and fixes

Tests clean-up

Tests fixed?

Plays nicely with threads

Tests fixed

Fixes ESR and show experimental flags

Failing test fixed

DM Fix

WIP: Bottom bar UX

Fixes for unreads

Failing test

Always show current channel

Create a channel in a category!

* Unreads on top

* Various fixes

* Improves category collapsing

* Passes correct ID through

* Tests cleanup

* Redo unreads and unread-button

* Reverts to just using ids

* More unreads back to using ids

* Uses appropriate selectors for pref updates

* Unreads sorted by recency

* Fixes test for recency

* Fixes re-rendering bug

* Code review updates, websocket event debounced
2021-09-20 14:11:57 -04:00

51 lines
1.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// insertWithoutDuplicates inserts an item into an array and returns the result. The provided array is not modified.
// If the array already contains the given item, that item is moved to the new location instead of adding a duplicate.
// If the array already had the given item at the given index, the origianl array is returned.
export function insertWithoutDuplicates<T>(array: T[], item: T, newIndex: number) {
const index = array.indexOf(item);
if (newIndex === index) {
// The item doesn't need to be moved since its location hasn't changed
return array;
}
const newArray = [...array];
// Remove the item from its old location if it already exists in the array
if (index !== -1) {
newArray.splice(index, 1);
}
// And re-add it in its new location
newArray.splice(newIndex, 0, item);
return newArray;
}
export function insertMultipleWithoutDuplicates<T>(array: T[], items: T[], newIndex: number) {
let newArray = [...array];
items.forEach((item) => {
newArray = removeItem(newArray, item);
});
// And re-add it in its new location
newArray.splice(newIndex, 0, ...items);
return newArray;
}
// removeItem removes an item from an array and returns the result. The provided array is not modified. If the array
// did not originally contain the given item, the original array is returned.
export function removeItem<T>(array: T[], item: T) {
const index = array.indexOf(item);
if (index === -1) {
return array;
}
const result = [...array];
result.splice(index, 1);
return result;
}