-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
146 lines (122 loc) · 3.08 KB
/
utils.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* date-fns re-implementation
*/
const idFormat = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
});
export function toISODate(date) {
const parts = idFormat.formatToParts(date);
return `${parts.find((part) => part.type === 'year').value}-${
parts.find((part) => part.type === 'month').value
}-${parts.find((part) => part.type === 'day').value}`;
}
export function addDays(days, date) {
return new Date(date.valueOf() + 24 * 60 * 60 * 1000 * days);
}
export function startOfDay(date) {
const day = new Date(date);
day.setHours(0, 0, 0, 0);
return day;
}
export function isSameDay(a, b) {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
);
}
export function diffDays(from, to) {
const diffTime = from - to;
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
/**
* Custom helpers
*/
export function uid() {
return 'id' + (performance.now().toString(36) + Math.random().toString(36)).replace(/\./g, '');
}
export function fence(min, max, value) {
return Math.min(max, Math.max(min, value));
}
export function moveIndex(from, to, items) {
const updated = [...items];
if (to >= updated.length) {
var k = to - updated.length + 1;
while (k--) {
updated.push(undefined);
}
}
updated.splice(to, 0, updated.splice(from, 1)[0]);
return updated;
}
/**
* lodash/fp re-implementations
*/
export function flow(...fns) {
return function (data) {
return fns.reduce((acc, fn) => fn(acc), data);
};
}
export function map(fn) {
return function (data) {
return data.map(fn);
};
}
export function filter(fn) {
return function (data) {
return data.filter(fn);
};
}
export function reduce(fn, initial) {
return function (data) {
return data.reduce(fn, initial);
};
}
export function update(path, fn) {
const pathArray = typeof path === 'string' ? path.split('.') : path;
return function (data) {
return pathArray.length === 0
? fn(data)
: { ...data, [pathArray[0]]: update(pathArray.slice(1), fn)(data[pathArray[0]]) };
};
}
export function set(path, value) {
return update(path, () => value);
}
export function pull(...value) {
return filter((item) => !value.includes(item));
}
/**
* Some custom helpers based on lodash
*/
export function append(path, item) {
return update(path, (items) => [...items, item]);
}
export function insert(newItem, item, isAfter) {
return reduce(
(acc, current) =>
acc.concat(current === item ? (isAfter ? [current, newItem] : [newItem, current]) : current),
[],
);
}
export function insertId(toNewItem, itemId, isAfter) {
return reduce(
(acc, current) =>
acc.concat(
current.id === itemId
? isAfter
? [current, toNewItem(current)]
: [toNewItem(current), current]
: current,
),
[],
);
}
export function pullId(...value) {
return filter((item) => !value.includes(item.id));
}
export function mapOnlyId(id, fn) {
return map((item) => (item.id === id ? fn(item) : item));
}