1 | {curry, flip, compose, partial, _, identity,
|
2 | unary, binary, ternary} = require "fairmont-core"
|
3 |
|
4 | {detach} = require "./object"
|
5 | {deepEqual} = require "./util"
|
6 |
|
7 | nth = curry (i, ax) -> ax[i - 1]
|
8 | first = nth 1
|
9 | second = nth 2
|
10 | third = nth 3
|
11 | fourth = nth 4
|
12 | fifth = nth 5
|
13 | last = ([rest..., last]) -> last
|
14 | rest = ([first, rest...]) -> rest
|
15 |
|
16 |
|
17 | empty = (x) -> x.length == 0
|
18 |
|
19 | includes = if Array::includes
|
20 | curry (a, ax) -> ax.includes a
|
21 | else
|
22 | curry (a, ax) -> (ax.indexOf a ) != -1
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | findIndexOf = curry (a, ax) -> if (i = ax.indexOf a) != -1 then i
|
30 | findLastIndexOf = curry (a, ax) -> if (i = ax.lastIndexOf a) != -1 then i
|
31 |
|
32 |
|
33 | some = curry binary detach Array::some
|
34 |
|
35 |
|
36 | push = curry (ax, a...) ->
|
37 | ax.push a...
|
38 | ax
|
39 | pop = detach Array::pop
|
40 | shift = detach Array::shift
|
41 | unshift = detach Array::unshift
|
42 | enqueue = unshift
|
43 | dequeue = pop
|
44 |
|
45 |
|
46 |
|
47 | splice = curry (i, n, ax) ->
|
48 | ax.splice i, n
|
49 | ax
|
50 |
|
51 | insert = curry ternary (ax, a, i) ->
|
52 | ax.splice i, 0, a
|
53 | ax
|
54 |
|
55 | remove = curry (ax, a) ->
|
56 | (ax.splice i, 1) if (i = ax.indexOf( a )) != -1
|
57 | ax
|
58 |
|
59 | cat = detach Array::concat
|
60 |
|
61 | slice = curry (i, j, ax) -> ax[i...j]
|
62 |
|
63 | sort = curry binary detach Array::sort
|
64 | join = curry binary detach Array::join
|
65 | fill = curry (ax, a) -> ax.fill a
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | uniqueBy = curry (f, ax) ->
|
72 | bx = []
|
73 | for a in ax
|
74 | b = f a
|
75 | (bx.push b) unless b in bx
|
76 | bx
|
77 |
|
78 | unique = uniq = uniqueBy identity
|
79 |
|
80 | dupes = ([a, ax...]) ->
|
81 | if empty ax
|
82 | []
|
83 | else
|
84 | bx = dupes ax
|
85 | if a in ax && !(a in bx) then [a, bx...] else bx
|
86 |
|
87 | union = curry compose unique, cat
|
88 |
|
89 | intersection = (first, rest...) ->
|
90 | if empty rest
|
91 | first
|
92 | else
|
93 | x for x in (intersection rest...) when x in first
|
94 |
|
95 | difference = curry (ax, bx) ->
|
96 | cx = union ax, bx
|
97 | cx.filter (c) ->
|
98 | (c in ax && !(c in bx)) ||
|
99 | (c in bx && !(c in ax))
|
100 |
|
101 | complement = curry (ax, bx) -> ax.filter (c) -> !(c in bx)
|
102 |
|
103 |
|
104 |
|
105 | shuffle = (ax) ->
|
106 | bx = cat ax
|
107 | i = bx.length
|
108 | unless i <= 1
|
109 | while --i > 0
|
110 |
|
111 |
|
112 |
|
113 | j = Math.floor Math.random() * (i + 1)
|
114 | [bx[i], bx[j]] = [bx[j], bx[i]]
|
115 | if deepEqual ax, bx then shuffle ax else bx
|
116 | else
|
117 | bx
|
118 |
|
119 | range = curry (start, finish) -> [start..finish]
|
120 |
|
121 | {random, round} = Math
|
122 | pluck = (ax) -> ax[(round random() * (ax.length - 1))]
|
123 |
|
124 | pair = curry (a, b) -> [a, b]
|
125 |
|
126 | module.exports = {
|
127 | first, second, third, fourth, fifth, nth, last, rest,
|
128 | empty, includes, findIndexOf, findLastIndexOf,
|
129 | uniqueBy, unique, uniq, dupes,
|
130 | union, intersection, difference, complement,
|
131 | push, pop, shift, unshift, enqueue, dequeue,
|
132 | splice, insert, remove, cat,
|
133 | slice, join, fill,
|
134 | shuffle, range, pluck, pair}
|