-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.naive.js
44 lines (36 loc) · 1.07 KB
/
index.naive.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
import { pipe, always, applySpec } from 'ramda';
const getValue = (o, sortKey) => sortKey ? o[sortKey] : o
const sort = (list, sortKey) => {
// SHOULD OPTIMIZE
return list.sort((a, b) => getValue(a, sortKey) - getValue(b, sortKey));
};
const findIndex = (list, sortKey) => value => {
// SHOULD OPTIMIZE
return list.findIndex(i => getValue(i, sortKey) === value);
}
const insert = (list, sortKey, item) => {
// SHOULD OPTIMIZE
return [...list, item];
}
const remove = (list, sortKey, value) => {
// SHOULD OPTIMIZE
return list.filter(i => getValue(i) !== value);
}
export const List = ({ sortKey, initial, initialOrder}) => {
const items = initialOrder ? initial : sort(initial, sortKey);
return {
items,
findIndex: findIndex(items, sortKey),
remove: value => List({
sortKey,
initial: remove(items, sortKey, value),
initialOrder: true
}),
insert: item => List({
sortKey,
initial: insert(items, sortKey, item),
// You should reverse this bool if your insertion preserve order
initialOrder: false
})
}
}