1 |
|
2 | (function() {
|
3 | var _, binary, cat, complement, compose, curry, deepEqual, dequeue, detach, difference, dupes, empty, enqueue, fifth, fill, findIndexOf, findLastIndexOf, first, flip, fourth, identity, includes, insert, intersection, join, last, nth, pair, partial, pluck, pop, push, random, range, ref, remove, rest, round, second, shift, shuffle, slice, some, sort, splice, ternary, third, unary, union, uniq, unique, uniqueBy, unshift,
|
4 | slice1 = [].slice,
|
5 | indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
6 |
|
7 | ref = require("fairmont-core"), curry = ref.curry, flip = ref.flip, compose = ref.compose, partial = ref.partial, _ = ref._, identity = ref.identity, unary = ref.unary, binary = ref.binary, ternary = ref.ternary;
|
8 |
|
9 | detach = require("./object").detach;
|
10 |
|
11 | deepEqual = require("./util").deepEqual;
|
12 |
|
13 | nth = curry(function(i, ax) {
|
14 | return ax[i - 1];
|
15 | });
|
16 |
|
17 | first = nth(1);
|
18 |
|
19 | second = nth(2);
|
20 |
|
21 | third = nth(3);
|
22 |
|
23 | fourth = nth(4);
|
24 |
|
25 | fifth = nth(5);
|
26 |
|
27 | last = function(arg) {
|
28 | var k, last, rest;
|
29 | rest = 2 <= arg.length ? slice1.call(arg, 0, k = arg.length - 1) : (k = 0, []), last = arg[k++];
|
30 | return last;
|
31 | };
|
32 |
|
33 | rest = function(arg) {
|
34 | var first, rest;
|
35 | first = arg[0], rest = 2 <= arg.length ? slice1.call(arg, 1) : [];
|
36 | return rest;
|
37 | };
|
38 |
|
39 | empty = function(x) {
|
40 | return x.length === 0;
|
41 | };
|
42 |
|
43 | includes = Array.prototype.includes ? curry(function(a, ax) {
|
44 | return ax.includes(a);
|
45 | }) : curry(function(a, ax) {
|
46 | return (ax.indexOf(a)) !== -1;
|
47 | });
|
48 |
|
49 | findIndexOf = curry(function(a, ax) {
|
50 | var i;
|
51 | if ((i = ax.indexOf(a)) !== -1) {
|
52 | return i;
|
53 | }
|
54 | });
|
55 |
|
56 | findLastIndexOf = curry(function(a, ax) {
|
57 | var i;
|
58 | if ((i = ax.lastIndexOf(a)) !== -1) {
|
59 | return i;
|
60 | }
|
61 | });
|
62 |
|
63 | some = curry(binary(detach(Array.prototype.some)));
|
64 |
|
65 | push = curry(function() {
|
66 | var a, ax;
|
67 | ax = arguments[0], a = 2 <= arguments.length ? slice1.call(arguments, 1) : [];
|
68 | ax.push.apply(ax, a);
|
69 | return ax;
|
70 | });
|
71 |
|
72 | pop = detach(Array.prototype.pop);
|
73 |
|
74 | shift = detach(Array.prototype.shift);
|
75 |
|
76 | unshift = detach(Array.prototype.unshift);
|
77 |
|
78 | enqueue = unshift;
|
79 |
|
80 | dequeue = pop;
|
81 |
|
82 | splice = curry(function(i, n, ax) {
|
83 | ax.splice(i, n);
|
84 | return ax;
|
85 | });
|
86 |
|
87 | insert = curry(binary(function(ax, a, i) {
|
88 | if (i == null) {
|
89 | i = 0;
|
90 | }
|
91 | ax.splice(i, 0, a);
|
92 | return ax;
|
93 | }));
|
94 |
|
95 | remove = curry(function(ax, a) {
|
96 | var i;
|
97 | if ((i = ax.indexOf(a)) !== -1) {
|
98 | ax.splice(i, 1);
|
99 | }
|
100 | return ax;
|
101 | });
|
102 |
|
103 | cat = detach(Array.prototype.concat);
|
104 |
|
105 | slice = curry(function(i, j, ax) {
|
106 | return ax.slice(i, j);
|
107 | });
|
108 |
|
109 | sort = curry(binary(detach(Array.prototype.sort)));
|
110 |
|
111 | join = curry(binary(detach(Array.prototype.join)));
|
112 |
|
113 | fill = curry(function(ax, a) {
|
114 | return ax.fill(a);
|
115 | });
|
116 |
|
117 | uniqueBy = curry(function(f, ax) {
|
118 | var a, b, bx, k, len;
|
119 | bx = [];
|
120 | for (k = 0, len = ax.length; k < len; k++) {
|
121 | a = ax[k];
|
122 | b = f(a);
|
123 | if (indexOf.call(bx, b) < 0) {
|
124 | bx.push(b);
|
125 | }
|
126 | }
|
127 | return bx;
|
128 | });
|
129 |
|
130 | unique = uniq = uniqueBy(identity);
|
131 |
|
132 | dupes = function(arg) {
|
133 | var a, ax, bx;
|
134 | a = arg[0], ax = 2 <= arg.length ? slice1.call(arg, 1) : [];
|
135 | if (empty(ax)) {
|
136 | return [];
|
137 | } else {
|
138 | bx = dupes(ax);
|
139 | if (indexOf.call(ax, a) >= 0 && !(indexOf.call(bx, a) >= 0)) {
|
140 | return [a].concat(slice1.call(bx));
|
141 | } else {
|
142 | return bx;
|
143 | }
|
144 | }
|
145 | };
|
146 |
|
147 | union = curry(compose(unique, cat));
|
148 |
|
149 | intersection = function() {
|
150 | var first, k, len, ref1, rest, results, x;
|
151 | first = arguments[0], rest = 2 <= arguments.length ? slice1.call(arguments, 1) : [];
|
152 | if (empty(rest)) {
|
153 | return first;
|
154 | } else {
|
155 | ref1 = intersection.apply(null, rest);
|
156 | results = [];
|
157 | for (k = 0, len = ref1.length; k < len; k++) {
|
158 | x = ref1[k];
|
159 | if (indexOf.call(first, x) >= 0) {
|
160 | results.push(x);
|
161 | }
|
162 | }
|
163 | return results;
|
164 | }
|
165 | };
|
166 |
|
167 | difference = curry(function(ax, bx) {
|
168 | var cx;
|
169 | cx = union(ax, bx);
|
170 | return cx.filter(function(c) {
|
171 | return (indexOf.call(ax, c) >= 0 && !(indexOf.call(bx, c) >= 0)) || (indexOf.call(bx, c) >= 0 && !(indexOf.call(ax, c) >= 0));
|
172 | });
|
173 | });
|
174 |
|
175 | complement = curry(function(ax, bx) {
|
176 | return ax.filter(function(c) {
|
177 | return !(indexOf.call(bx, c) >= 0);
|
178 | });
|
179 | });
|
180 |
|
181 | shuffle = function(ax) {
|
182 | var b, bx, i, j, k, len, ref1;
|
183 | bx = cat(ax);
|
184 | if (!(bx.length <= 1)) {
|
185 | for (i = k = 0, len = bx.length; k < len; i = ++k) {
|
186 | b = bx[i];
|
187 | j = i;
|
188 | while (j === i) {
|
189 | j = Math.floor(Math.random() * bx.length);
|
190 | }
|
191 | ref1 = [bx[j], bx[i]], bx[i] = ref1[0], bx[j] = ref1[1];
|
192 | }
|
193 | if (deepEqual(ax, bx)) {
|
194 | return shuffle(ax);
|
195 | } else {
|
196 | return bx;
|
197 | }
|
198 | } else {
|
199 | return bx;
|
200 | }
|
201 | };
|
202 |
|
203 | range = curry(function(start, finish) {
|
204 | var k, results;
|
205 | return (function() {
|
206 | results = [];
|
207 | for (var k = start; start <= finish ? k <= finish : k >= finish; start <= finish ? k++ : k--){ results.push(k); }
|
208 | return results;
|
209 | }).apply(this);
|
210 | });
|
211 |
|
212 | random = Math.random, round = Math.round;
|
213 |
|
214 | pluck = function(ax) {
|
215 | return ax[round(random() * (ax.length - 1))];
|
216 | };
|
217 |
|
218 | pair = curry(function(a, b) {
|
219 | return [a, b];
|
220 | });
|
221 |
|
222 | module.exports = {
|
223 | first: first,
|
224 | second: second,
|
225 | third: third,
|
226 | fourth: fourth,
|
227 | fifth: fifth,
|
228 | nth: nth,
|
229 | last: last,
|
230 | rest: rest,
|
231 | empty: empty,
|
232 | includes: includes,
|
233 | findIndexOf: findIndexOf,
|
234 | findLastIndexOf: findLastIndexOf,
|
235 | uniqueBy: uniqueBy,
|
236 | unique: unique,
|
237 | uniq: uniq,
|
238 | dupes: dupes,
|
239 | union: union,
|
240 | intersection: intersection,
|
241 | difference: difference,
|
242 | complement: complement,
|
243 | push: push,
|
244 | pop: pop,
|
245 | shift: shift,
|
246 | unshift: unshift,
|
247 | enqueue: enqueue,
|
248 | dequeue: dequeue,
|
249 | splice: splice,
|
250 | insert: insert,
|
251 | remove: remove,
|
252 | cat: cat,
|
253 | slice: slice,
|
254 | join: join,
|
255 | fill: fill,
|
256 | shuffle: shuffle,
|
257 | range: range,
|
258 | pluck: pluck,
|
259 | pair: pair
|
260 | };
|
261 |
|
262 | }).call(this);
|