-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat.js
75 lines (60 loc) · 1.7 KB
/
concat.js
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
import { groupBy } from './group.js'
// Negative and positive indices might match the same index.
// In that case, the negative indices are inserted last.
// - This ensures -1 is always last, which might be expected by some users
// Also, if several different negative indices have been bounded to 0, they
// are sorted.
// '*' updates are always applied first, so other updates can override them.
export const concatUpdates = function (updates) {
if (updates.length === 1) {
return updates
}
const updatesA = Object.values(groupBy(updates, 'index')).map(concatGroup)
// eslint-disable-next-line fp/no-mutating-methods
return [...updatesA].sort(secondSortFunc)
}
const concatGroup = function (updates) {
if (updates.length === 1) {
return updates[0]
}
const [{ index, any }] = updates
// eslint-disable-next-line fp/no-mutating-methods
const updatesA = [...updates].sort(firstSortFunc)
const items = updatesA.flatMap(getItems)
return { index, any, items }
}
const getItems = function ({ items }) {
return items
}
// eslint-disable-next-line complexity
const firstSortFunc = function (updateA, updateB) {
if (updateA.negation < updateB.negation) {
return -1
}
if (updateA.negation > updateB.negation) {
return 1
}
if (updateA.fullIndex < updateB.fullIndex) {
return -1
}
if (updateA.fullIndex > updateB.fullIndex) {
return 1
}
return 0
}
// eslint-disable-next-line complexity
const secondSortFunc = function (updateA, updateB) {
if (updateA.any < updateB.any) {
return 1
}
if (updateA.any > updateB.any) {
return -1
}
if (updateA.index < updateB.index) {
return -1
}
if (updateA.index > updateB.index) {
return 1
}
return 0
}