/
helpers.tsx
109 lines (93 loc) · 2.3 KB
/
helpers.tsx
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/** @module Context.Select.Helpers */
import { ISelect } from '.'
/**
* Add or remove selection to selected
* @param selected Selected
* @param selection Selection
* @returns Selected
*/
export const addOrRemoveSelection = (
selected: ISelect[],
selection: ISelect
): ISelect[] => {
let localSelected = [...selected]
// Index
const index = localSelected.findIndex((s) => s.uuid === selection.uuid)
// Add or remove
if (index === -1) localSelected.push(selection)
else
localSelected = [
...localSelected.slice(0, index),
...localSelected.slice(index + 1)
]
// Return
return localSelected
}
/**
* Add selections to selected
* @param selected Selected
* @param selections Selections
* @returns Selected
*/
export const addSelections = (
selected: ISelect[],
selections: ISelect[]
): ISelect[] => {
// Merge
const localSelected = [...selected, ...selections]
// Remove duplicate
return localSelected.reduce((accumulator: ISelect[], current: ISelect) => {
if (!accumulator.find((item) => item.uuid === current.uuid))
accumulator.push(current)
return accumulator
}, [])
}
/**
* Remove selections from selected
* @param selected Selected
* @param selections Selections
* @returns Selected
*/
export const removeSelections = (
selected: ISelect[],
selections: ISelect[]
): ISelect[] => {
let localSelected = [...selected]
selections.forEach((selection) => {
// Index
const index = localSelected.findIndex((s) => s.uuid === selection.uuid)
// Remove
if (index !== -1)
localSelected = [
...localSelected.slice(0, index),
...localSelected.slice(index + 1)
]
})
// Return
return localSelected
}
/**
* Swap selections to selected
* @param selected Selected
* @param selections Selections
* @returns Selected
*/
export const swapSelections = (
selected: ISelect[],
selections: ISelect[]
): ISelect[] => {
let localSelected = [...selected]
selections.forEach((selection) => {
// Index
const index = localSelected.findIndex((s) => s.uuid === selection.uuid)
// Remove
if (index === -1) localSelected.push(selection)
else
localSelected = [
...localSelected.slice(0, index),
...localSelected.slice(index + 1)
]
})
// Return
return localSelected
}