-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteSticker.ts
39 lines (34 loc) · 943 Bytes
/
deleteSticker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import type { StickerItem } from "../types"
interface DeleteStickerOptions {
stickers: StickerItem[]
id: string
}
/**
* Returns a copy of the provided `stickers` array without the selected sticker.
* @param {DeleteStickerOptions}
* @returns {StickerItem[]}
*/
export function deleteSticker({
stickers,
id,
}: DeleteStickerOptions): StickerItem[] {
if (!stickers || !(stickers instanceof Array) || stickers.length <= 0)
throw Error("`stickers` array is empty")
const [filteredStickers, deletedSticker] = stickers.reduce<
[StickerItem[], StickerItem | null]
>(
(acc, s) => {
if (s.id !== id) acc[0].push(s)
else acc[1] = s
return acc
},
[[], null]
)
if (!id || !deletedSticker)
throw Error("`id` needs to be a valid `sticker` id")
return filteredStickers.map((item) => {
// fix the order
if (item.order > deletedSticker.order) item.order -= 1
return item
})
}