forked from nan-academy/tron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharr.js
66 lines (60 loc) · 1.42 KB
/
arr.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
const range = (start, end) => {
if (end === undefined) {
end = start
start = 0
}
const max = end - start
const ret = Array(max)
let i = -1
while (++i <= max) {
ret[i] = start + i
}
return ret
}
range.from = n => end => range(n, end)
const proto = Object.getOwnPropertyNames(Array.prototype)
.filter(method => typeof Array.prototype[method] === 'function')
.reduce((acc, method) => {
const fn = Array.prototype[method]
acc[method] = function() { return arr => fn.apply(arr, arguments) }
return acc
}, {})
const remove = (arr, elem) => {
if (!arr) return arr
const idx = arr.indexOf(elem)
if (idx < 0) return arr
arr.splice(idx, 1)
return arr
}
const shuffle = arr => {
let i = arr.length
let j, tmp
while (--i > 0) {
j = Math.floor(Math.random() * (i + 1))
tmp = arr[j]
arr[j] = arr[i]
arr[i] = tmp
}
return arr
}
const n = n => arr => arr[n]
const first = arr => arr[0]
const append = (arr, value) => arr ? (arr.push(value), arr) : [ value ]
const prepend = (arr, value) => arr ? (arr.unshift(value), arr) : [ value ]
const last = arr => arr[arr.length - 1]
const unique = arr => Array.from(new Set(arr))
const random = arr => arr[Math.floor(Math.random() * arr.length)]
const inRange = (arr, idx) => Math.min(Math.max(idx, 0), arr.length - 1)
export {
range,
remove,
shuffle,
proto,
first,
append,
prepend,
last,
unique,
random,
inRange,
}