generated from stagas/ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presets-group-detail.ts
125 lines (106 loc) · 2.93 KB
/
presets-group-detail.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { filterMap } from 'everyday-utils'
import { AbstractDetail, BasePresets } from './'
export interface PresetsGroupData {
details: [string, AbstractDetail][]
sources?: Map<string, BasePresets>
}
export class PresetsGroupDetail extends AbstractDetail<PresetsGroupData> {
constructor(data: any) {
super(data)
}
toJSON(): any {
return {
details: this.data.details
}
}
collectData(sources: NonNullable<PresetsGroupData['sources']>) {
// @ts-ignore
return new this.constructor({
sources: new Map(sources),
details:
filterMap(
[...sources],
([id, presets]) =>
!!presets.selectedPresetId
&& !!presets.selectedPreset?.detail
&& [id, presets.selectedPreset.detail.copy()]
)
})
}
applyData(data: PresetsGroupData): PresetsGroupData['sources'] {
return !data.sources
? new Map()
: new Map(filterMap(data.details,
([id, presetDetailData]) => {
let presets =
data.sources!.get(id)!
if (
!presets.selectedPreset
|| !presets.selectedPreset
.detail.equals(presetDetailData)
) {
presets = presets.setDetailData(presetDetailData, false, false, true)
}
return [id, presets]
})
)
}
equals(other?: this | this['data']) {
if (other === this) {
// console.warn('EXACTLY THE SAME', other, this)
return true
}
const otherData =
other && ('data' in other ? other.data : other)
if (!otherData) {
// console.log('NOT OTHER DATA', this)
return false
}
const sameLength = this.data.details.length === otherData.details.length
if (!sameLength) {
// console.log('NOT SAME LENGTH', other, this)
return false
}
const sameDetails = this.data.details.every(
function everySameDetail(a) {
return otherData.details
.some(function areDetailsEqual(b) {
return a[0] === b[0] && a[1].equals(b[1])
})
})
if (!sameDetails) {
// console.log('NOT SAME DETAIL', other, this)
return false
}
return true
}
satisfies(other?: this | this['data']) {
if (other === this) return true
const otherData =
other && ('data' in other ? other.data : other)
if (!otherData) {
return false
}
const satisfied = this.data.details.every(
function satisfiesEvery(a) {
return otherData.details
.some(function satisfies(b) {
return a[1].satisfies(b[1])
})
})
if (!satisfied) {
return false
}
return true
}
merge(other: this | this['data']) {
const otherData = ('data' in other ? other.data : other)
return new PresetsGroupDetail({
sources: new Map(otherData.sources),
details: otherData.details.map(
([key, detail]) =>
[key, detail.copy()]
)
})
}
}