-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatchSticker.ts
49 lines (42 loc) · 1.14 KB
/
patchSticker.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
40
41
42
43
44
45
46
47
48
49
import type { Vec2 } from "wtc-math"
import type { StickerItem } from "../types"
type PatchStickerOptions = {
stickers: StickerItem[]
id: string
} & (
| {
prop: "scale" | "rotation"
value: number
}
| {
prop: "position"
value: Vec2
}
)
/**
* Returns a copy of the provided `stickers` array with the updated ("patched") sticker in place.
* @param {PatchStickerOptions}
* @returns {StickerItem[]}
*/
export function patchSticker({
stickers,
prop,
value,
id,
}: PatchStickerOptions): StickerItem[] {
if (!stickers || !(stickers instanceof Array) || stickers.length <= 0)
throw Error("`stickers` array is empty")
const patchedSticker = stickers.find((sticker) => sticker.id === id)
if (!id || !patchedSticker)
throw Error("`id` needs to be a valid `sticker` id")
const PROPS = ["position", "scale", "rotation"]
if (!PROPS.includes(prop))
throw Error(`Invalid 'prop'. 'prop' must be one of: ${PROPS.join(",")}`)
return stickers.map((item) => {
if (item === patchedSticker) {
if (prop === "position") item[prop] = value
else item[prop] = value
}
return item
})
}