1 | // Ramda v0.23.0
|
2 | // https://github.com/ramda/ramda
|
3 | // (c) 2013-2016 Scott Sauyet, Michael Hurley, and David Chambers
|
4 | // Ramda may be freely distributed under the MIT license.
|
5 |
|
6 | ;(function() {
|
7 |
|
8 | ;
|
9 |
|
10 | /**
|
11 | * A special placeholder value used to specify "gaps" within curried functions,
|
12 | * allowing partial application of any combination of arguments, regardless of
|
13 | * their positions.
|
14 | *
|
15 | * If `g` is a curried ternary function and `_` is `R.__`, the following are
|
16 | * equivalent:
|
17 | *
|
18 | * - `g(1, 2, 3)`
|
19 | * - `g(_, 2, 3)(1)`
|
20 | * - `g(_, _, 3)(1)(2)`
|
21 | * - `g(_, _, 3)(1, 2)`
|
22 | * - `g(_, 2, _)(1, 3)`
|
23 | * - `g(_, 2)(1)(3)`
|
24 | * - `g(_, 2)(1, 3)`
|
25 | * - `g(_, 2)(_, 3)(1)`
|
26 | *
|
27 | * @constant
|
28 | * @memberOf R
|
29 | * @since v0.6.0
|
30 | * @category Function
|
31 | * @example
|
32 | *
|
33 | * var greet = R.replace('{name}', R.__, 'Hello, {name}!');
|
34 | * greet('Alice'); //=> 'Hello, Alice!'
|
35 | */
|
36 | var __ = { '@@functional/placeholder': true };
|
37 |
|
38 | var _aperture = function _aperture(n, list) {
|
39 | var idx = 0;
|
40 | var limit = list.length - (n - 1);
|
41 | var acc = new Array(limit >= 0 ? limit : 0);
|
42 | while (idx < limit) {
|
43 | acc[idx] = Array.prototype.slice.call(list, idx, idx + n);
|
44 | idx += 1;
|
45 | }
|
46 | return acc;
|
47 | };
|
48 |
|
49 | /* eslint-disable no-unused-vars */
|
50 | var _arity = function _arity(n, fn) {
|
51 | /* eslint-disable no-unused-vars */
|
52 | switch (n) {
|
53 | case 0:
|
54 | return function () {
|
55 | return fn.apply(this, arguments);
|
56 | };
|
57 | case 1:
|
58 | return function (a0) {
|
59 | return fn.apply(this, arguments);
|
60 | };
|
61 | case 2:
|
62 | return function (a0, a1) {
|
63 | return fn.apply(this, arguments);
|
64 | };
|
65 | case 3:
|
66 | return function (a0, a1, a2) {
|
67 | return fn.apply(this, arguments);
|
68 | };
|
69 | case 4:
|
70 | return function (a0, a1, a2, a3) {
|
71 | return fn.apply(this, arguments);
|
72 | };
|
73 | case 5:
|
74 | return function (a0, a1, a2, a3, a4) {
|
75 | return fn.apply(this, arguments);
|
76 | };
|
77 | case 6:
|
78 | return function (a0, a1, a2, a3, a4, a5) {
|
79 | return fn.apply(this, arguments);
|
80 | };
|
81 | case 7:
|
82 | return function (a0, a1, a2, a3, a4, a5, a6) {
|
83 | return fn.apply(this, arguments);
|
84 | };
|
85 | case 8:
|
86 | return function (a0, a1, a2, a3, a4, a5, a6, a7) {
|
87 | return fn.apply(this, arguments);
|
88 | };
|
89 | case 9:
|
90 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
|
91 | return fn.apply(this, arguments);
|
92 | };
|
93 | case 10:
|
94 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
|
95 | return fn.apply(this, arguments);
|
96 | };
|
97 | default:
|
98 | throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
|
99 | }
|
100 | };
|
101 |
|
102 | var _arrayFromIterator = function _arrayFromIterator(iter) {
|
103 | var list = [];
|
104 | var next;
|
105 | while (!(next = iter.next()).done) {
|
106 | list.push(next.value);
|
107 | }
|
108 | return list;
|
109 | };
|
110 |
|
111 | var _cloneRegExp = function _cloneRegExp(pattern) {
|
112 | return new RegExp(pattern.source, (pattern.global ? 'g' : '') + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '') + (pattern.sticky ? 'y' : '') + (pattern.unicode ? 'u' : ''));
|
113 | };
|
114 |
|
115 | var _complement = function _complement(f) {
|
116 | return function () {
|
117 | return !f.apply(this, arguments);
|
118 | };
|
119 | };
|
120 |
|
121 | /**
|
122 | * Private `concat` function to merge two array-like objects.
|
123 | *
|
124 | * @private
|
125 | * @param {Array|Arguments} [set1=[]] An array-like object.
|
126 | * @param {Array|Arguments} [set2=[]] An array-like object.
|
127 | * @return {Array} A new, merged array.
|
128 | * @example
|
129 | *
|
130 | * _concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
|
131 | */
|
132 | var _concat = function _concat(set1, set2) {
|
133 | set1 = set1 || [];
|
134 | set2 = set2 || [];
|
135 | var idx;
|
136 | var len1 = set1.length;
|
137 | var len2 = set2.length;
|
138 | var result = [];
|
139 | idx = 0;
|
140 | while (idx < len1) {
|
141 | result[result.length] = set1[idx];
|
142 | idx += 1;
|
143 | }
|
144 | idx = 0;
|
145 | while (idx < len2) {
|
146 | result[result.length] = set2[idx];
|
147 | idx += 1;
|
148 | }
|
149 | return result;
|
150 | };
|
151 |
|
152 | var _containsWith = function _containsWith(pred, x, list) {
|
153 | var idx = 0;
|
154 | var len = list.length;
|
155 | while (idx < len) {
|
156 | if (pred(x, list[idx])) {
|
157 | return true;
|
158 | }
|
159 | idx += 1;
|
160 | }
|
161 | return false;
|
162 | };
|
163 |
|
164 | var _dropLastWhile = function dropLastWhile(pred, list) {
|
165 | var idx = list.length - 1;
|
166 | while (idx >= 0 && pred(list[idx])) {
|
167 | idx -= 1;
|
168 | }
|
169 | return Array.prototype.slice.call(list, 0, idx + 1);
|
170 | };
|
171 |
|
172 | var _filter = function _filter(fn, list) {
|
173 | var idx = 0;
|
174 | var len = list.length;
|
175 | var result = [];
|
176 | while (idx < len) {
|
177 | if (fn(list[idx])) {
|
178 | result[result.length] = list[idx];
|
179 | }
|
180 | idx += 1;
|
181 | }
|
182 | return result;
|
183 | };
|
184 |
|
185 | var _forceReduced = function _forceReduced(x) {
|
186 | return {
|
187 | '@@transducer/value': x,
|
188 | '@@transducer/reduced': true
|
189 | };
|
190 | };
|
191 |
|
192 | // String(x => x) evaluates to "x => x", so the pattern may not match.
|
193 | var _functionName = function _functionName(f) {
|
194 | // String(x => x) evaluates to "x => x", so the pattern may not match.
|
195 | var match = String(f).match(/^function (\w*)/);
|
196 | return match == null ? '' : match[1];
|
197 | };
|
198 |
|
199 | var _has = function _has(prop, obj) {
|
200 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
201 | };
|
202 |
|
203 | var _identity = function _identity(x) {
|
204 | return x;
|
205 | };
|
206 |
|
207 | var _isArguments = function () {
|
208 | var toString = Object.prototype.toString;
|
209 | return toString.call(arguments) === '[object Arguments]' ? function _isArguments(x) {
|
210 | return toString.call(x) === '[object Arguments]';
|
211 | } : function _isArguments(x) {
|
212 | return _has('callee', x);
|
213 | };
|
214 | }();
|
215 |
|
216 | /**
|
217 | * Tests whether or not an object is an array.
|
218 | *
|
219 | * @private
|
220 | * @param {*} val The object to test.
|
221 | * @return {Boolean} `true` if `val` is an array, `false` otherwise.
|
222 | * @example
|
223 | *
|
224 | * _isArray([]); //=> true
|
225 | * _isArray(null); //=> false
|
226 | * _isArray({}); //=> false
|
227 | */
|
228 | var _isArray = Array.isArray || function _isArray(val) {
|
229 | return val != null && val.length >= 0 && Object.prototype.toString.call(val) === '[object Array]';
|
230 | };
|
231 |
|
232 | var _isFunction = function _isFunction(x) {
|
233 | return Object.prototype.toString.call(x) === '[object Function]';
|
234 | };
|
235 |
|
236 | /**
|
237 | * Determine if the passed argument is an integer.
|
238 | *
|
239 | * @private
|
240 | * @param {*} n
|
241 | * @category Type
|
242 | * @return {Boolean}
|
243 | */
|
244 | var _isInteger = Number.isInteger || function _isInteger(n) {
|
245 | return n << 0 === n;
|
246 | };
|
247 |
|
248 | var _isNumber = function _isNumber(x) {
|
249 | return Object.prototype.toString.call(x) === '[object Number]';
|
250 | };
|
251 |
|
252 | var _isObject = function _isObject(x) {
|
253 | return Object.prototype.toString.call(x) === '[object Object]';
|
254 | };
|
255 |
|
256 | var _isPlaceholder = function _isPlaceholder(a) {
|
257 | return a != null && typeof a === 'object' && a['@@functional/placeholder'] === true;
|
258 | };
|
259 |
|
260 | var _isRegExp = function _isRegExp(x) {
|
261 | return Object.prototype.toString.call(x) === '[object RegExp]';
|
262 | };
|
263 |
|
264 | var _isString = function _isString(x) {
|
265 | return Object.prototype.toString.call(x) === '[object String]';
|
266 | };
|
267 |
|
268 | var _isTransformer = function _isTransformer(obj) {
|
269 | return typeof obj['@@transducer/step'] === 'function';
|
270 | };
|
271 |
|
272 | var _map = function _map(fn, functor) {
|
273 | var idx = 0;
|
274 | var len = functor.length;
|
275 | var result = Array(len);
|
276 | while (idx < len) {
|
277 | result[idx] = fn(functor[idx]);
|
278 | idx += 1;
|
279 | }
|
280 | return result;
|
281 | };
|
282 |
|
283 | // Based on https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
284 | var _objectAssign = function _objectAssign(target) {
|
285 | if (target == null) {
|
286 | throw new TypeError('Cannot convert undefined or null to object');
|
287 | }
|
288 | var output = Object(target);
|
289 | var idx = 1;
|
290 | var length = arguments.length;
|
291 | while (idx < length) {
|
292 | var source = arguments[idx];
|
293 | if (source != null) {
|
294 | for (var nextKey in source) {
|
295 | if (_has(nextKey, source)) {
|
296 | output[nextKey] = source[nextKey];
|
297 | }
|
298 | }
|
299 | }
|
300 | idx += 1;
|
301 | }
|
302 | return output;
|
303 | };
|
304 |
|
305 | var _of = function _of(x) {
|
306 | return [x];
|
307 | };
|
308 |
|
309 | var _pipe = function _pipe(f, g) {
|
310 | return function () {
|
311 | return g.call(this, f.apply(this, arguments));
|
312 | };
|
313 | };
|
314 |
|
315 | var _pipeP = function _pipeP(f, g) {
|
316 | return function () {
|
317 | var ctx = this;
|
318 | return f.apply(ctx, arguments).then(function (x) {
|
319 | return g.call(ctx, x);
|
320 | });
|
321 | };
|
322 | };
|
323 |
|
324 | // \b matches word boundary; [\b] matches backspace
|
325 | var _quote = function _quote(s) {
|
326 | var escaped = s.replace(/\\/g, '\\\\').replace(/[\b]/g, '\\b') // \b matches word boundary; [\b] matches backspace
|
327 | .replace(/\f/g, '\\f').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t').replace(/\v/g, '\\v').replace(/\0/g, '\\0');
|
328 | return '"' + escaped.replace(/"/g, '\\"') + '"';
|
329 | };
|
330 |
|
331 | var _reduced = function _reduced(x) {
|
332 | return x && x['@@transducer/reduced'] ? x : {
|
333 | '@@transducer/value': x,
|
334 | '@@transducer/reduced': true
|
335 | };
|
336 | };
|
337 |
|
338 | /**
|
339 | * Polyfill from <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString>.
|
340 | */
|
341 | var _toISOString = function () {
|
342 | var pad = function pad(n) {
|
343 | return (n < 10 ? '0' : '') + n;
|
344 | };
|
345 | return typeof Date.prototype.toISOString === 'function' ? function _toISOString(d) {
|
346 | return d.toISOString();
|
347 | } : function _toISOString(d) {
|
348 | return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + '.' + (d.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z';
|
349 | };
|
350 | }();
|
351 |
|
352 | var _xfBase = {
|
353 | init: function () {
|
354 | return this.xf['@@transducer/init']();
|
355 | },
|
356 | result: function (result) {
|
357 | return this.xf['@@transducer/result'](result);
|
358 | }
|
359 | };
|
360 |
|
361 | var _xwrap = function () {
|
362 | function XWrap(fn) {
|
363 | this.f = fn;
|
364 | }
|
365 | XWrap.prototype['@@transducer/init'] = function () {
|
366 | throw new Error('init not implemented on XWrap');
|
367 | };
|
368 | XWrap.prototype['@@transducer/result'] = function (acc) {
|
369 | return acc;
|
370 | };
|
371 | XWrap.prototype['@@transducer/step'] = function (acc, x) {
|
372 | return this.f(acc, x);
|
373 | };
|
374 | return function _xwrap(fn) {
|
375 | return new XWrap(fn);
|
376 | };
|
377 | }();
|
378 |
|
379 | var _assign = typeof Object.assign === 'function' ? Object.assign : _objectAssign;
|
380 |
|
381 | /**
|
382 | * This checks whether a function has a [methodname] function. If it isn't an
|
383 | * array it will execute that function otherwise it will default to the ramda
|
384 | * implementation.
|
385 | *
|
386 | * @private
|
387 | * @param {Function} fn ramda implemtation
|
388 | * @param {String} methodname property to check for a custom implementation
|
389 | * @return {Object} Whatever the return value of the method is.
|
390 | */
|
391 | var _checkForMethod = function _checkForMethod(methodname, fn) {
|
392 | return function () {
|
393 | var length = arguments.length;
|
394 | if (length === 0) {
|
395 | return fn();
|
396 | }
|
397 | var obj = arguments[length - 1];
|
398 | return _isArray(obj) || typeof obj[methodname] !== 'function' ? fn.apply(this, arguments) : obj[methodname].apply(obj, Array.prototype.slice.call(arguments, 0, length - 1));
|
399 | };
|
400 | };
|
401 |
|
402 | /**
|
403 | * Optimized internal one-arity curry function.
|
404 | *
|
405 | * @private
|
406 | * @category Function
|
407 | * @param {Function} fn The function to curry.
|
408 | * @return {Function} The curried function.
|
409 | */
|
410 | var _curry1 = function _curry1(fn) {
|
411 | return function f1(a) {
|
412 | if (arguments.length === 0 || _isPlaceholder(a)) {
|
413 | return f1;
|
414 | } else {
|
415 | return fn.apply(this, arguments);
|
416 | }
|
417 | };
|
418 | };
|
419 |
|
420 | /**
|
421 | * Optimized internal two-arity curry function.
|
422 | *
|
423 | * @private
|
424 | * @category Function
|
425 | * @param {Function} fn The function to curry.
|
426 | * @return {Function} The curried function.
|
427 | */
|
428 | var _curry2 = function _curry2(fn) {
|
429 | return function f2(a, b) {
|
430 | switch (arguments.length) {
|
431 | case 0:
|
432 | return f2;
|
433 | case 1:
|
434 | return _isPlaceholder(a) ? f2 : _curry1(function (_b) {
|
435 | return fn(a, _b);
|
436 | });
|
437 | default:
|
438 | return _isPlaceholder(a) && _isPlaceholder(b) ? f2 : _isPlaceholder(a) ? _curry1(function (_a) {
|
439 | return fn(_a, b);
|
440 | }) : _isPlaceholder(b) ? _curry1(function (_b) {
|
441 | return fn(a, _b);
|
442 | }) : fn(a, b);
|
443 | }
|
444 | };
|
445 | };
|
446 |
|
447 | /**
|
448 | * Optimized internal three-arity curry function.
|
449 | *
|
450 | * @private
|
451 | * @category Function
|
452 | * @param {Function} fn The function to curry.
|
453 | * @return {Function} The curried function.
|
454 | */
|
455 | var _curry3 = function _curry3(fn) {
|
456 | return function f3(a, b, c) {
|
457 | switch (arguments.length) {
|
458 | case 0:
|
459 | return f3;
|
460 | case 1:
|
461 | return _isPlaceholder(a) ? f3 : _curry2(function (_b, _c) {
|
462 | return fn(a, _b, _c);
|
463 | });
|
464 | case 2:
|
465 | return _isPlaceholder(a) && _isPlaceholder(b) ? f3 : _isPlaceholder(a) ? _curry2(function (_a, _c) {
|
466 | return fn(_a, b, _c);
|
467 | }) : _isPlaceholder(b) ? _curry2(function (_b, _c) {
|
468 | return fn(a, _b, _c);
|
469 | }) : _curry1(function (_c) {
|
470 | return fn(a, b, _c);
|
471 | });
|
472 | default:
|
473 | return _isPlaceholder(a) && _isPlaceholder(b) && _isPlaceholder(c) ? f3 : _isPlaceholder(a) && _isPlaceholder(b) ? _curry2(function (_a, _b) {
|
474 | return fn(_a, _b, c);
|
475 | }) : _isPlaceholder(a) && _isPlaceholder(c) ? _curry2(function (_a, _c) {
|
476 | return fn(_a, b, _c);
|
477 | }) : _isPlaceholder(b) && _isPlaceholder(c) ? _curry2(function (_b, _c) {
|
478 | return fn(a, _b, _c);
|
479 | }) : _isPlaceholder(a) ? _curry1(function (_a) {
|
480 | return fn(_a, b, c);
|
481 | }) : _isPlaceholder(b) ? _curry1(function (_b) {
|
482 | return fn(a, _b, c);
|
483 | }) : _isPlaceholder(c) ? _curry1(function (_c) {
|
484 | return fn(a, b, _c);
|
485 | }) : fn(a, b, c);
|
486 | }
|
487 | };
|
488 | };
|
489 |
|
490 | /**
|
491 | * Internal curryN function.
|
492 | *
|
493 | * @private
|
494 | * @category Function
|
495 | * @param {Number} length The arity of the curried function.
|
496 | * @param {Array} received An array of arguments received thus far.
|
497 | * @param {Function} fn The function to curry.
|
498 | * @return {Function} The curried function.
|
499 | */
|
500 | var _curryN = function _curryN(length, received, fn) {
|
501 | return function () {
|
502 | var combined = [];
|
503 | var argsIdx = 0;
|
504 | var left = length;
|
505 | var combinedIdx = 0;
|
506 | while (combinedIdx < received.length || argsIdx < arguments.length) {
|
507 | var result;
|
508 | if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
|
509 | result = received[combinedIdx];
|
510 | } else {
|
511 | result = arguments[argsIdx];
|
512 | argsIdx += 1;
|
513 | }
|
514 | combined[combinedIdx] = result;
|
515 | if (!_isPlaceholder(result)) {
|
516 | left -= 1;
|
517 | }
|
518 | combinedIdx += 1;
|
519 | }
|
520 | return left <= 0 ? fn.apply(this, combined) : _arity(left, _curryN(length, combined, fn));
|
521 | };
|
522 | };
|
523 |
|
524 | /**
|
525 | * Returns a function that dispatches with different strategies based on the
|
526 | * object in list position (last argument). If it is an array, executes [fn].
|
527 | * Otherwise, if it has a function with one of the given method names, it will
|
528 | * execute that function (functor case). Otherwise, if it is a transformer,
|
529 | * uses transducer [xf] to return a new transformer (transducer case).
|
530 | * Otherwise, it will default to executing [fn].
|
531 | *
|
532 | * @private
|
533 | * @param {Array} methodNames properties to check for a custom implementation
|
534 | * @param {Function} xf transducer to initialize if object is transformer
|
535 | * @param {Function} fn default ramda implementation
|
536 | * @return {Function} A function that dispatches on object in list position
|
537 | */
|
538 | var _dispatchable = function _dispatchable(methodNames, xf, fn) {
|
539 | return function () {
|
540 | if (arguments.length === 0) {
|
541 | return fn();
|
542 | }
|
543 | var args = Array.prototype.slice.call(arguments, 0);
|
544 | var obj = args.pop();
|
545 | if (!_isArray(obj)) {
|
546 | var idx = 0;
|
547 | while (idx < methodNames.length) {
|
548 | if (typeof obj[methodNames[idx]] === 'function') {
|
549 | return obj[methodNames[idx]].apply(obj, args);
|
550 | }
|
551 | idx += 1;
|
552 | }
|
553 | if (_isTransformer(obj)) {
|
554 | var transducer = xf.apply(null, args);
|
555 | return transducer(obj);
|
556 | }
|
557 | }
|
558 | return fn.apply(this, arguments);
|
559 | };
|
560 | };
|
561 |
|
562 | var _xall = function () {
|
563 | function XAll(f, xf) {
|
564 | this.xf = xf;
|
565 | this.f = f;
|
566 | this.all = true;
|
567 | }
|
568 | XAll.prototype['@@transducer/init'] = _xfBase.init;
|
569 | XAll.prototype['@@transducer/result'] = function (result) {
|
570 | if (this.all) {
|
571 | result = this.xf['@@transducer/step'](result, true);
|
572 | }
|
573 | return this.xf['@@transducer/result'](result);
|
574 | };
|
575 | XAll.prototype['@@transducer/step'] = function (result, input) {
|
576 | if (!this.f(input)) {
|
577 | this.all = false;
|
578 | result = _reduced(this.xf['@@transducer/step'](result, false));
|
579 | }
|
580 | return result;
|
581 | };
|
582 | return _curry2(function _xall(f, xf) {
|
583 | return new XAll(f, xf);
|
584 | });
|
585 | }();
|
586 |
|
587 | var _xany = function () {
|
588 | function XAny(f, xf) {
|
589 | this.xf = xf;
|
590 | this.f = f;
|
591 | this.any = false;
|
592 | }
|
593 | XAny.prototype['@@transducer/init'] = _xfBase.init;
|
594 | XAny.prototype['@@transducer/result'] = function (result) {
|
595 | if (!this.any) {
|
596 | result = this.xf['@@transducer/step'](result, false);
|
597 | }
|
598 | return this.xf['@@transducer/result'](result);
|
599 | };
|
600 | XAny.prototype['@@transducer/step'] = function (result, input) {
|
601 | if (this.f(input)) {
|
602 | this.any = true;
|
603 | result = _reduced(this.xf['@@transducer/step'](result, true));
|
604 | }
|
605 | return result;
|
606 | };
|
607 | return _curry2(function _xany(f, xf) {
|
608 | return new XAny(f, xf);
|
609 | });
|
610 | }();
|
611 |
|
612 | var _xaperture = function () {
|
613 | function XAperture(n, xf) {
|
614 | this.xf = xf;
|
615 | this.pos = 0;
|
616 | this.full = false;
|
617 | this.acc = new Array(n);
|
618 | }
|
619 | XAperture.prototype['@@transducer/init'] = _xfBase.init;
|
620 | XAperture.prototype['@@transducer/result'] = function (result) {
|
621 | this.acc = null;
|
622 | return this.xf['@@transducer/result'](result);
|
623 | };
|
624 | XAperture.prototype['@@transducer/step'] = function (result, input) {
|
625 | this.store(input);
|
626 | return this.full ? this.xf['@@transducer/step'](result, this.getCopy()) : result;
|
627 | };
|
628 | XAperture.prototype.store = function (input) {
|
629 | this.acc[this.pos] = input;
|
630 | this.pos += 1;
|
631 | if (this.pos === this.acc.length) {
|
632 | this.pos = 0;
|
633 | this.full = true;
|
634 | }
|
635 | };
|
636 | XAperture.prototype.getCopy = function () {
|
637 | return _concat(Array.prototype.slice.call(this.acc, this.pos), Array.prototype.slice.call(this.acc, 0, this.pos));
|
638 | };
|
639 | return _curry2(function _xaperture(n, xf) {
|
640 | return new XAperture(n, xf);
|
641 | });
|
642 | }();
|
643 |
|
644 | var _xdrop = function () {
|
645 | function XDrop(n, xf) {
|
646 | this.xf = xf;
|
647 | this.n = n;
|
648 | }
|
649 | XDrop.prototype['@@transducer/init'] = _xfBase.init;
|
650 | XDrop.prototype['@@transducer/result'] = _xfBase.result;
|
651 | XDrop.prototype['@@transducer/step'] = function (result, input) {
|
652 | if (this.n > 0) {
|
653 | this.n -= 1;
|
654 | return result;
|
655 | }
|
656 | return this.xf['@@transducer/step'](result, input);
|
657 | };
|
658 | return _curry2(function _xdrop(n, xf) {
|
659 | return new XDrop(n, xf);
|
660 | });
|
661 | }();
|
662 |
|
663 | var _xdropLast = function () {
|
664 | function XDropLast(n, xf) {
|
665 | this.xf = xf;
|
666 | this.pos = 0;
|
667 | this.full = false;
|
668 | this.acc = new Array(n);
|
669 | }
|
670 | XDropLast.prototype['@@transducer/init'] = _xfBase.init;
|
671 | XDropLast.prototype['@@transducer/result'] = function (result) {
|
672 | this.acc = null;
|
673 | return this.xf['@@transducer/result'](result);
|
674 | };
|
675 | XDropLast.prototype['@@transducer/step'] = function (result, input) {
|
676 | if (this.full) {
|
677 | result = this.xf['@@transducer/step'](result, this.acc[this.pos]);
|
678 | }
|
679 | this.store(input);
|
680 | return result;
|
681 | };
|
682 | XDropLast.prototype.store = function (input) {
|
683 | this.acc[this.pos] = input;
|
684 | this.pos += 1;
|
685 | if (this.pos === this.acc.length) {
|
686 | this.pos = 0;
|
687 | this.full = true;
|
688 | }
|
689 | };
|
690 | return _curry2(function _xdropLast(n, xf) {
|
691 | return new XDropLast(n, xf);
|
692 | });
|
693 | }();
|
694 |
|
695 | var _xdropRepeatsWith = function () {
|
696 | function XDropRepeatsWith(pred, xf) {
|
697 | this.xf = xf;
|
698 | this.pred = pred;
|
699 | this.lastValue = undefined;
|
700 | this.seenFirstValue = false;
|
701 | }
|
702 | XDropRepeatsWith.prototype['@@transducer/init'] = _xfBase.init;
|
703 | XDropRepeatsWith.prototype['@@transducer/result'] = _xfBase.result;
|
704 | XDropRepeatsWith.prototype['@@transducer/step'] = function (result, input) {
|
705 | var sameAsLast = false;
|
706 | if (!this.seenFirstValue) {
|
707 | this.seenFirstValue = true;
|
708 | } else if (this.pred(this.lastValue, input)) {
|
709 | sameAsLast = true;
|
710 | }
|
711 | this.lastValue = input;
|
712 | return sameAsLast ? result : this.xf['@@transducer/step'](result, input);
|
713 | };
|
714 | return _curry2(function _xdropRepeatsWith(pred, xf) {
|
715 | return new XDropRepeatsWith(pred, xf);
|
716 | });
|
717 | }();
|
718 |
|
719 | var _xdropWhile = function () {
|
720 | function XDropWhile(f, xf) {
|
721 | this.xf = xf;
|
722 | this.f = f;
|
723 | }
|
724 | XDropWhile.prototype['@@transducer/init'] = _xfBase.init;
|
725 | XDropWhile.prototype['@@transducer/result'] = _xfBase.result;
|
726 | XDropWhile.prototype['@@transducer/step'] = function (result, input) {
|
727 | if (this.f) {
|
728 | if (this.f(input)) {
|
729 | return result;
|
730 | }
|
731 | this.f = null;
|
732 | }
|
733 | return this.xf['@@transducer/step'](result, input);
|
734 | };
|
735 | return _curry2(function _xdropWhile(f, xf) {
|
736 | return new XDropWhile(f, xf);
|
737 | });
|
738 | }();
|
739 |
|
740 | var _xfilter = function () {
|
741 | function XFilter(f, xf) {
|
742 | this.xf = xf;
|
743 | this.f = f;
|
744 | }
|
745 | XFilter.prototype['@@transducer/init'] = _xfBase.init;
|
746 | XFilter.prototype['@@transducer/result'] = _xfBase.result;
|
747 | XFilter.prototype['@@transducer/step'] = function (result, input) {
|
748 | return this.f(input) ? this.xf['@@transducer/step'](result, input) : result;
|
749 | };
|
750 | return _curry2(function _xfilter(f, xf) {
|
751 | return new XFilter(f, xf);
|
752 | });
|
753 | }();
|
754 |
|
755 | var _xfind = function () {
|
756 | function XFind(f, xf) {
|
757 | this.xf = xf;
|
758 | this.f = f;
|
759 | this.found = false;
|
760 | }
|
761 | XFind.prototype['@@transducer/init'] = _xfBase.init;
|
762 | XFind.prototype['@@transducer/result'] = function (result) {
|
763 | if (!this.found) {
|
764 | result = this.xf['@@transducer/step'](result, void 0);
|
765 | }
|
766 | return this.xf['@@transducer/result'](result);
|
767 | };
|
768 | XFind.prototype['@@transducer/step'] = function (result, input) {
|
769 | if (this.f(input)) {
|
770 | this.found = true;
|
771 | result = _reduced(this.xf['@@transducer/step'](result, input));
|
772 | }
|
773 | return result;
|
774 | };
|
775 | return _curry2(function _xfind(f, xf) {
|
776 | return new XFind(f, xf);
|
777 | });
|
778 | }();
|
779 |
|
780 | var _xfindIndex = function () {
|
781 | function XFindIndex(f, xf) {
|
782 | this.xf = xf;
|
783 | this.f = f;
|
784 | this.idx = -1;
|
785 | this.found = false;
|
786 | }
|
787 | XFindIndex.prototype['@@transducer/init'] = _xfBase.init;
|
788 | XFindIndex.prototype['@@transducer/result'] = function (result) {
|
789 | if (!this.found) {
|
790 | result = this.xf['@@transducer/step'](result, -1);
|
791 | }
|
792 | return this.xf['@@transducer/result'](result);
|
793 | };
|
794 | XFindIndex.prototype['@@transducer/step'] = function (result, input) {
|
795 | this.idx += 1;
|
796 | if (this.f(input)) {
|
797 | this.found = true;
|
798 | result = _reduced(this.xf['@@transducer/step'](result, this.idx));
|
799 | }
|
800 | return result;
|
801 | };
|
802 | return _curry2(function _xfindIndex(f, xf) {
|
803 | return new XFindIndex(f, xf);
|
804 | });
|
805 | }();
|
806 |
|
807 | var _xfindLast = function () {
|
808 | function XFindLast(f, xf) {
|
809 | this.xf = xf;
|
810 | this.f = f;
|
811 | }
|
812 | XFindLast.prototype['@@transducer/init'] = _xfBase.init;
|
813 | XFindLast.prototype['@@transducer/result'] = function (result) {
|
814 | return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.last));
|
815 | };
|
816 | XFindLast.prototype['@@transducer/step'] = function (result, input) {
|
817 | if (this.f(input)) {
|
818 | this.last = input;
|
819 | }
|
820 | return result;
|
821 | };
|
822 | return _curry2(function _xfindLast(f, xf) {
|
823 | return new XFindLast(f, xf);
|
824 | });
|
825 | }();
|
826 |
|
827 | var _xfindLastIndex = function () {
|
828 | function XFindLastIndex(f, xf) {
|
829 | this.xf = xf;
|
830 | this.f = f;
|
831 | this.idx = -1;
|
832 | this.lastIdx = -1;
|
833 | }
|
834 | XFindLastIndex.prototype['@@transducer/init'] = _xfBase.init;
|
835 | XFindLastIndex.prototype['@@transducer/result'] = function (result) {
|
836 | return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.lastIdx));
|
837 | };
|
838 | XFindLastIndex.prototype['@@transducer/step'] = function (result, input) {
|
839 | this.idx += 1;
|
840 | if (this.f(input)) {
|
841 | this.lastIdx = this.idx;
|
842 | }
|
843 | return result;
|
844 | };
|
845 | return _curry2(function _xfindLastIndex(f, xf) {
|
846 | return new XFindLastIndex(f, xf);
|
847 | });
|
848 | }();
|
849 |
|
850 | var _xmap = function () {
|
851 | function XMap(f, xf) {
|
852 | this.xf = xf;
|
853 | this.f = f;
|
854 | }
|
855 | XMap.prototype['@@transducer/init'] = _xfBase.init;
|
856 | XMap.prototype['@@transducer/result'] = _xfBase.result;
|
857 | XMap.prototype['@@transducer/step'] = function (result, input) {
|
858 | return this.xf['@@transducer/step'](result, this.f(input));
|
859 | };
|
860 | return _curry2(function _xmap(f, xf) {
|
861 | return new XMap(f, xf);
|
862 | });
|
863 | }();
|
864 |
|
865 | var _xreduceBy = function () {
|
866 | function XReduceBy(valueFn, valueAcc, keyFn, xf) {
|
867 | this.valueFn = valueFn;
|
868 | this.valueAcc = valueAcc;
|
869 | this.keyFn = keyFn;
|
870 | this.xf = xf;
|
871 | this.inputs = {};
|
872 | }
|
873 | XReduceBy.prototype['@@transducer/init'] = _xfBase.init;
|
874 | XReduceBy.prototype['@@transducer/result'] = function (result) {
|
875 | var key;
|
876 | for (key in this.inputs) {
|
877 | if (_has(key, this.inputs)) {
|
878 | result = this.xf['@@transducer/step'](result, this.inputs[key]);
|
879 | if (result['@@transducer/reduced']) {
|
880 | result = result['@@transducer/value'];
|
881 | break;
|
882 | }
|
883 | }
|
884 | }
|
885 | this.inputs = null;
|
886 | return this.xf['@@transducer/result'](result);
|
887 | };
|
888 | XReduceBy.prototype['@@transducer/step'] = function (result, input) {
|
889 | var key = this.keyFn(input);
|
890 | this.inputs[key] = this.inputs[key] || [
|
891 | key,
|
892 | this.valueAcc
|
893 | ];
|
894 | this.inputs[key][1] = this.valueFn(this.inputs[key][1], input);
|
895 | return result;
|
896 | };
|
897 | return _curryN(4, [], function _xreduceBy(valueFn, valueAcc, keyFn, xf) {
|
898 | return new XReduceBy(valueFn, valueAcc, keyFn, xf);
|
899 | });
|
900 | }();
|
901 |
|
902 | var _xtake = function () {
|
903 | function XTake(n, xf) {
|
904 | this.xf = xf;
|
905 | this.n = n;
|
906 | this.i = 0;
|
907 | }
|
908 | XTake.prototype['@@transducer/init'] = _xfBase.init;
|
909 | XTake.prototype['@@transducer/result'] = _xfBase.result;
|
910 | XTake.prototype['@@transducer/step'] = function (result, input) {
|
911 | this.i += 1;
|
912 | var ret = this.n === 0 ? result : this.xf['@@transducer/step'](result, input);
|
913 | return this.i >= this.n ? _reduced(ret) : ret;
|
914 | };
|
915 | return _curry2(function _xtake(n, xf) {
|
916 | return new XTake(n, xf);
|
917 | });
|
918 | }();
|
919 |
|
920 | var _xtakeWhile = function () {
|
921 | function XTakeWhile(f, xf) {
|
922 | this.xf = xf;
|
923 | this.f = f;
|
924 | }
|
925 | XTakeWhile.prototype['@@transducer/init'] = _xfBase.init;
|
926 | XTakeWhile.prototype['@@transducer/result'] = _xfBase.result;
|
927 | XTakeWhile.prototype['@@transducer/step'] = function (result, input) {
|
928 | return this.f(input) ? this.xf['@@transducer/step'](result, input) : _reduced(result);
|
929 | };
|
930 | return _curry2(function _xtakeWhile(f, xf) {
|
931 | return new XTakeWhile(f, xf);
|
932 | });
|
933 | }();
|
934 |
|
935 | /**
|
936 | * Adds two values.
|
937 | *
|
938 | * @func
|
939 | * @memberOf R
|
940 | * @since v0.1.0
|
941 | * @category Math
|
942 | * @sig Number -> Number -> Number
|
943 | * @param {Number} a
|
944 | * @param {Number} b
|
945 | * @return {Number}
|
946 | * @see R.subtract
|
947 | * @example
|
948 | *
|
949 | * R.add(2, 3); //=> 5
|
950 | * R.add(7)(10); //=> 17
|
951 | */
|
952 | var add = _curry2(function add(a, b) {
|
953 | return Number(a) + Number(b);
|
954 | });
|
955 |
|
956 | /**
|
957 | * Applies a function to the value at the given index of an array, returning a
|
958 | * new copy of the array with the element at the given index replaced with the
|
959 | * result of the function application.
|
960 | *
|
961 | * @func
|
962 | * @memberOf R
|
963 | * @since v0.14.0
|
964 | * @category List
|
965 | * @sig (a -> a) -> Number -> [a] -> [a]
|
966 | * @param {Function} fn The function to apply.
|
967 | * @param {Number} idx The index.
|
968 | * @param {Array|Arguments} list An array-like object whose value
|
969 | * at the supplied index will be replaced.
|
970 | * @return {Array} A copy of the supplied array-like object with
|
971 | * the element at index `idx` replaced with the value
|
972 | * returned by applying `fn` to the existing element.
|
973 | * @see R.update
|
974 | * @example
|
975 | *
|
976 | * R.adjust(R.add(10), 1, [1, 2, 3]); //=> [1, 12, 3]
|
977 | * R.adjust(R.add(10))(1)([1, 2, 3]); //=> [1, 12, 3]
|
978 | * @symb R.adjust(f, -1, [a, b]) = [a, f(b)]
|
979 | * @symb R.adjust(f, 0, [a, b]) = [f(a), b]
|
980 | */
|
981 | var adjust = _curry3(function adjust(fn, idx, list) {
|
982 | if (idx >= list.length || idx < -list.length) {
|
983 | return list;
|
984 | }
|
985 | var start = idx < 0 ? list.length : 0;
|
986 | var _idx = start + idx;
|
987 | var _list = _concat(list);
|
988 | _list[_idx] = fn(list[_idx]);
|
989 | return _list;
|
990 | });
|
991 |
|
992 | /**
|
993 | * Returns `true` if all elements of the list match the predicate, `false` if
|
994 | * there are any that don't.
|
995 | *
|
996 | * Dispatches to the `all` method of the second argument, if present.
|
997 | *
|
998 | * Acts as a transducer if a transformer is given in list position.
|
999 | *
|
1000 | * @func
|
1001 | * @memberOf R
|
1002 | * @since v0.1.0
|
1003 | * @category List
|
1004 | * @sig (a -> Boolean) -> [a] -> Boolean
|
1005 | * @param {Function} fn The predicate function.
|
1006 | * @param {Array} list The array to consider.
|
1007 | * @return {Boolean} `true` if the predicate is satisfied by every element, `false`
|
1008 | * otherwise.
|
1009 | * @see R.any, R.none, R.transduce
|
1010 | * @example
|
1011 | *
|
1012 | * var equals3 = R.equals(3);
|
1013 | * R.all(equals3)([3, 3, 3, 3]); //=> true
|
1014 | * R.all(equals3)([3, 3, 1, 3]); //=> false
|
1015 | */
|
1016 | var all = _curry2(_dispatchable(['all'], _xall, function all(fn, list) {
|
1017 | var idx = 0;
|
1018 | while (idx < list.length) {
|
1019 | if (!fn(list[idx])) {
|
1020 | return false;
|
1021 | }
|
1022 | idx += 1;
|
1023 | }
|
1024 | return true;
|
1025 | }));
|
1026 |
|
1027 | /**
|
1028 | * Returns a function that always returns the given value. Note that for
|
1029 | * non-primitives the value returned is a reference to the original value.
|
1030 | *
|
1031 | * This function is known as `const`, `constant`, or `K` (for K combinator) in
|
1032 | * other languages and libraries.
|
1033 | *
|
1034 | * @func
|
1035 | * @memberOf R
|
1036 | * @since v0.1.0
|
1037 | * @category Function
|
1038 | * @sig a -> (* -> a)
|
1039 | * @param {*} val The value to wrap in a function
|
1040 | * @return {Function} A Function :: * -> val.
|
1041 | * @example
|
1042 | *
|
1043 | * var t = R.always('Tee');
|
1044 | * t(); //=> 'Tee'
|
1045 | */
|
1046 | var always = _curry1(function always(val) {
|
1047 | return function () {
|
1048 | return val;
|
1049 | };
|
1050 | });
|
1051 |
|
1052 | /**
|
1053 | * Returns `true` if both arguments are `true`; `false` otherwise.
|
1054 | *
|
1055 | * @func
|
1056 | * @memberOf R
|
1057 | * @since v0.1.0
|
1058 | * @category Logic
|
1059 | * @sig a -> b -> a | b
|
1060 | * @param {Any} a
|
1061 | * @param {Any} b
|
1062 | * @return {Any} the first argument if it is falsy, otherwise the second argument.
|
1063 | * @see R.both
|
1064 | * @example
|
1065 | *
|
1066 | * R.and(true, true); //=> true
|
1067 | * R.and(true, false); //=> false
|
1068 | * R.and(false, true); //=> false
|
1069 | * R.and(false, false); //=> false
|
1070 | */
|
1071 | var and = _curry2(function and(a, b) {
|
1072 | return a && b;
|
1073 | });
|
1074 |
|
1075 | /**
|
1076 | * Returns `true` if at least one of elements of the list match the predicate,
|
1077 | * `false` otherwise.
|
1078 | *
|
1079 | * Dispatches to the `any` method of the second argument, if present.
|
1080 | *
|
1081 | * Acts as a transducer if a transformer is given in list position.
|
1082 | *
|
1083 | * @func
|
1084 | * @memberOf R
|
1085 | * @since v0.1.0
|
1086 | * @category List
|
1087 | * @sig (a -> Boolean) -> [a] -> Boolean
|
1088 | * @param {Function} fn The predicate function.
|
1089 | * @param {Array} list The array to consider.
|
1090 | * @return {Boolean} `true` if the predicate is satisfied by at least one element, `false`
|
1091 | * otherwise.
|
1092 | * @see R.all, R.none, R.transduce
|
1093 | * @example
|
1094 | *
|
1095 | * var lessThan0 = R.flip(R.lt)(0);
|
1096 | * var lessThan2 = R.flip(R.lt)(2);
|
1097 | * R.any(lessThan0)([1, 2]); //=> false
|
1098 | * R.any(lessThan2)([1, 2]); //=> true
|
1099 | */
|
1100 | var any = _curry2(_dispatchable(['any'], _xany, function any(fn, list) {
|
1101 | var idx = 0;
|
1102 | while (idx < list.length) {
|
1103 | if (fn(list[idx])) {
|
1104 | return true;
|
1105 | }
|
1106 | idx += 1;
|
1107 | }
|
1108 | return false;
|
1109 | }));
|
1110 |
|
1111 | /**
|
1112 | * Returns a new list, composed of n-tuples of consecutive elements If `n` is
|
1113 | * greater than the length of the list, an empty list is returned.
|
1114 | *
|
1115 | * Acts as a transducer if a transformer is given in list position.
|
1116 | *
|
1117 | * @func
|
1118 | * @memberOf R
|
1119 | * @since v0.12.0
|
1120 | * @category List
|
1121 | * @sig Number -> [a] -> [[a]]
|
1122 | * @param {Number} n The size of the tuples to create
|
1123 | * @param {Array} list The list to split into `n`-length tuples
|
1124 | * @return {Array} The resulting list of `n`-length tuples
|
1125 | * @see R.transduce
|
1126 | * @example
|
1127 | *
|
1128 | * R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
|
1129 | * R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
|
1130 | * R.aperture(7, [1, 2, 3, 4, 5]); //=> []
|
1131 | */
|
1132 | var aperture = _curry2(_dispatchable([], _xaperture, _aperture));
|
1133 |
|
1134 | /**
|
1135 | * Returns a new list containing the contents of the given list, followed by
|
1136 | * the given element.
|
1137 | *
|
1138 | * @func
|
1139 | * @memberOf R
|
1140 | * @since v0.1.0
|
1141 | * @category List
|
1142 | * @sig a -> [a] -> [a]
|
1143 | * @param {*} el The element to add to the end of the new list.
|
1144 | * @param {Array} list The list of elements to add a new item to.
|
1145 | * list.
|
1146 | * @return {Array} A new list containing the elements of the old list followed by `el`.
|
1147 | * @see R.prepend
|
1148 | * @example
|
1149 | *
|
1150 | * R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
|
1151 | * R.append('tests', []); //=> ['tests']
|
1152 | * R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
|
1153 | */
|
1154 | var append = _curry2(function append(el, list) {
|
1155 | return _concat(list, [el]);
|
1156 | });
|
1157 |
|
1158 | /**
|
1159 | * Applies function `fn` to the argument list `args`. This is useful for
|
1160 | * creating a fixed-arity function from a variadic function. `fn` should be a
|
1161 | * bound function if context is significant.
|
1162 | *
|
1163 | * @func
|
1164 | * @memberOf R
|
1165 | * @since v0.7.0
|
1166 | * @category Function
|
1167 | * @sig (*... -> a) -> [*] -> a
|
1168 | * @param {Function} fn The function which will be called with `args`
|
1169 | * @param {Array} args The arguments to call `fn` with
|
1170 | * @return {*} result The result, equivalent to `fn(...args)`
|
1171 | * @see R.call, R.unapply
|
1172 | * @example
|
1173 | *
|
1174 | * var nums = [1, 2, 3, -99, 42, 6, 7];
|
1175 | * R.apply(Math.max, nums); //=> 42
|
1176 | * @symb R.apply(f, [a, b, c]) = f(a, b, c)
|
1177 | */
|
1178 | var apply = _curry2(function apply(fn, args) {
|
1179 | return fn.apply(this, args);
|
1180 | });
|
1181 |
|
1182 | /**
|
1183 | * Makes an ascending comparator function out of a function that returns a value
|
1184 | * that can be compared with `<` and `>`.
|
1185 | *
|
1186 | * @func
|
1187 | * @memberOf R
|
1188 | * @since v0.23.0
|
1189 | * @category Function
|
1190 | * @sig Ord b => (a -> b) -> a -> a -> Number
|
1191 | * @param {Function} fn A function of arity one that returns a value that can be compared
|
1192 | * @param {*} a The first item to be compared.
|
1193 | * @param {*} b The second item to be compared.
|
1194 | * @return {Number} `-1` if fn(a) < fn(b), `1` if fn(b) < fn(a), otherwise `0`
|
1195 | * @example
|
1196 | *
|
1197 | * var byAge = R.ascend(R.prop('age'));
|
1198 | * var people = [
|
1199 | * // ...
|
1200 | * ];
|
1201 | * var peopleByYoungestFirst = R.sort(byAge, people);
|
1202 | */
|
1203 | var ascend = _curry3(function ascend(fn, a, b) {
|
1204 | var aa = fn(a);
|
1205 | var bb = fn(b);
|
1206 | return aa < bb ? -1 : aa > bb ? 1 : 0;
|
1207 | });
|
1208 |
|
1209 | /**
|
1210 | * Makes a shallow clone of an object, setting or overriding the specified
|
1211 | * property with the given value. Note that this copies and flattens prototype
|
1212 | * properties onto the new object as well. All non-primitive properties are
|
1213 | * copied by reference.
|
1214 | *
|
1215 | * @func
|
1216 | * @memberOf R
|
1217 | * @since v0.8.0
|
1218 | * @category Object
|
1219 | * @sig String -> a -> {k: v} -> {k: v}
|
1220 | * @param {String} prop The property name to set
|
1221 | * @param {*} val The new value
|
1222 | * @param {Object} obj The object to clone
|
1223 | * @return {Object} A new object equivalent to the original except for the changed property.
|
1224 | * @see R.dissoc
|
1225 | * @example
|
1226 | *
|
1227 | * R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
|
1228 | */
|
1229 | var assoc = _curry3(function assoc(prop, val, obj) {
|
1230 | var result = {};
|
1231 | for (var p in obj) {
|
1232 | result[p] = obj[p];
|
1233 | }
|
1234 | result[prop] = val;
|
1235 | return result;
|
1236 | });
|
1237 |
|
1238 | /**
|
1239 | * Makes a shallow clone of an object, setting or overriding the nodes required
|
1240 | * to create the given path, and placing the specific value at the tail end of
|
1241 | * that path. Note that this copies and flattens prototype properties onto the
|
1242 | * new object as well. All non-primitive properties are copied by reference.
|
1243 | *
|
1244 | * @func
|
1245 | * @memberOf R
|
1246 | * @since v0.8.0
|
1247 | * @category Object
|
1248 | * @typedefn Idx = String | Int
|
1249 | * @sig [Idx] -> a -> {a} -> {a}
|
1250 | * @param {Array} path the path to set
|
1251 | * @param {*} val The new value
|
1252 | * @param {Object} obj The object to clone
|
1253 | * @return {Object} A new object equivalent to the original except along the specified path.
|
1254 | * @see R.dissocPath
|
1255 | * @example
|
1256 | *
|
1257 | * R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}}
|
1258 | *
|
1259 | * // Any missing or non-object keys in path will be overridden
|
1260 | * R.assocPath(['a', 'b', 'c'], 42, {a: 5}); //=> {a: {b: {c: 42}}}
|
1261 | */
|
1262 | var assocPath = _curry3(function assocPath(path, val, obj) {
|
1263 | if (path.length === 0) {
|
1264 | return val;
|
1265 | }
|
1266 | var idx = path[0];
|
1267 | if (path.length > 1) {
|
1268 | var nextObj = _has(idx, obj) ? obj[idx] : _isInteger(path[1]) ? [] : {};
|
1269 | val = assocPath(Array.prototype.slice.call(path, 1), val, nextObj);
|
1270 | }
|
1271 | if (_isInteger(idx) && _isArray(obj)) {
|
1272 | var arr = [].concat(obj);
|
1273 | arr[idx] = val;
|
1274 | return arr;
|
1275 | } else {
|
1276 | return assoc(idx, val, obj);
|
1277 | }
|
1278 | });
|
1279 |
|
1280 | /**
|
1281 | * Creates a function that is bound to a context.
|
1282 | * Note: `R.bind` does not provide the additional argument-binding capabilities of
|
1283 | * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
|
1284 | *
|
1285 | * @func
|
1286 | * @memberOf R
|
1287 | * @since v0.6.0
|
1288 | * @category Function
|
1289 | * @category Object
|
1290 | * @sig (* -> *) -> {*} -> (* -> *)
|
1291 | * @param {Function} fn The function to bind to context
|
1292 | * @param {Object} thisObj The context to bind `fn` to
|
1293 | * @return {Function} A function that will execute in the context of `thisObj`.
|
1294 | * @see R.partial
|
1295 | * @example
|
1296 | *
|
1297 | * var log = R.bind(console.log, console);
|
1298 | * R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
|
1299 | * // logs {a: 2}
|
1300 | * @symb R.bind(f, o)(a, b) = f.call(o, a, b)
|
1301 | */
|
1302 | var bind = _curry2(function bind(fn, thisObj) {
|
1303 | return _arity(fn.length, function () {
|
1304 | return fn.apply(thisObj, arguments);
|
1305 | });
|
1306 | });
|
1307 |
|
1308 | /**
|
1309 | * Restricts a number to be within a range.
|
1310 | *
|
1311 | * Also works for other ordered types such as Strings and Dates.
|
1312 | *
|
1313 | * @func
|
1314 | * @memberOf R
|
1315 | * @since v0.20.0
|
1316 | * @category Relation
|
1317 | * @sig Ord a => a -> a -> a -> a
|
1318 | * @param {Number} minimum The lower limit of the clamp (inclusive)
|
1319 | * @param {Number} maximum The upper limit of the clamp (inclusive)
|
1320 | * @param {Number} value Value to be clamped
|
1321 | * @return {Number} Returns `minimum` when `val < minimum`, `maximum` when `val > maximum`, returns `val` otherwise
|
1322 | * @example
|
1323 | *
|
1324 | * R.clamp(1, 10, -5) // => 1
|
1325 | * R.clamp(1, 10, 15) // => 10
|
1326 | * R.clamp(1, 10, 4) // => 4
|
1327 | */
|
1328 | var clamp = _curry3(function clamp(min, max, value) {
|
1329 | if (min > max) {
|
1330 | throw new Error('min must not be greater than max in clamp(min, max, value)');
|
1331 | }
|
1332 | return value < min ? min : value > max ? max : value;
|
1333 | });
|
1334 |
|
1335 | /**
|
1336 | * Makes a comparator function out of a function that reports whether the first
|
1337 | * element is less than the second.
|
1338 | *
|
1339 | * @func
|
1340 | * @memberOf R
|
1341 | * @since v0.1.0
|
1342 | * @category Function
|
1343 | * @sig (a, b -> Boolean) -> (a, b -> Number)
|
1344 | * @param {Function} pred A predicate function of arity two which will return `true` if the first argument
|
1345 | * is less than the second, `false` otherwise
|
1346 | * @return {Function} A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`
|
1347 | * @example
|
1348 | *
|
1349 | * var byAge = R.comparator((a, b) => a.age < b.age);
|
1350 | * var people = [
|
1351 | * // ...
|
1352 | * ];
|
1353 | * var peopleByIncreasingAge = R.sort(byAge, people);
|
1354 | */
|
1355 | var comparator = _curry1(function comparator(pred) {
|
1356 | return function (a, b) {
|
1357 | return pred(a, b) ? -1 : pred(b, a) ? 1 : 0;
|
1358 | };
|
1359 | });
|
1360 |
|
1361 | /**
|
1362 | * Returns a curried equivalent of the provided function, with the specified
|
1363 | * arity. The curried function has two unusual capabilities. First, its
|
1364 | * arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
|
1365 | * following are equivalent:
|
1366 | *
|
1367 | * - `g(1)(2)(3)`
|
1368 | * - `g(1)(2, 3)`
|
1369 | * - `g(1, 2)(3)`
|
1370 | * - `g(1, 2, 3)`
|
1371 | *
|
1372 | * Secondly, the special placeholder value `R.__` may be used to specify
|
1373 | * "gaps", allowing partial application of any combination of arguments,
|
1374 | * regardless of their positions. If `g` is as above and `_` is `R.__`, the
|
1375 | * following are equivalent:
|
1376 | *
|
1377 | * - `g(1, 2, 3)`
|
1378 | * - `g(_, 2, 3)(1)`
|
1379 | * - `g(_, _, 3)(1)(2)`
|
1380 | * - `g(_, _, 3)(1, 2)`
|
1381 | * - `g(_, 2)(1)(3)`
|
1382 | * - `g(_, 2)(1, 3)`
|
1383 | * - `g(_, 2)(_, 3)(1)`
|
1384 | *
|
1385 | * @func
|
1386 | * @memberOf R
|
1387 | * @since v0.5.0
|
1388 | * @category Function
|
1389 | * @sig Number -> (* -> a) -> (* -> a)
|
1390 | * @param {Number} length The arity for the returned function.
|
1391 | * @param {Function} fn The function to curry.
|
1392 | * @return {Function} A new, curried function.
|
1393 | * @see R.curry
|
1394 | * @example
|
1395 | *
|
1396 | * var sumArgs = (...args) => R.sum(args);
|
1397 | *
|
1398 | * var curriedAddFourNumbers = R.curryN(4, sumArgs);
|
1399 | * var f = curriedAddFourNumbers(1, 2);
|
1400 | * var g = f(3);
|
1401 | * g(4); //=> 10
|
1402 | */
|
1403 | var curryN = _curry2(function curryN(length, fn) {
|
1404 | if (length === 1) {
|
1405 | return _curry1(fn);
|
1406 | }
|
1407 | return _arity(length, _curryN(length, [], fn));
|
1408 | });
|
1409 |
|
1410 | /**
|
1411 | * Decrements its argument.
|
1412 | *
|
1413 | * @func
|
1414 | * @memberOf R
|
1415 | * @since v0.9.0
|
1416 | * @category Math
|
1417 | * @sig Number -> Number
|
1418 | * @param {Number} n
|
1419 | * @return {Number} n - 1
|
1420 | * @see R.inc
|
1421 | * @example
|
1422 | *
|
1423 | * R.dec(42); //=> 41
|
1424 | */
|
1425 | var dec = add(-1);
|
1426 |
|
1427 | /**
|
1428 | * Returns the second argument if it is not `null`, `undefined` or `NaN`
|
1429 | * otherwise the first argument is returned.
|
1430 | *
|
1431 | * @func
|
1432 | * @memberOf R
|
1433 | * @since v0.10.0
|
1434 | * @category Logic
|
1435 | * @sig a -> b -> a | b
|
1436 | * @param {a} default The default value.
|
1437 | * @param {b} val `val` will be returned instead of `default` unless `val` is `null`, `undefined` or `NaN`.
|
1438 | * @return {*} The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value
|
1439 | * @example
|
1440 | *
|
1441 | * var defaultTo42 = R.defaultTo(42);
|
1442 | *
|
1443 | * defaultTo42(null); //=> 42
|
1444 | * defaultTo42(undefined); //=> 42
|
1445 | * defaultTo42('Ramda'); //=> 'Ramda'
|
1446 | * // parseInt('string') results in NaN
|
1447 | * defaultTo42(parseInt('string')); //=> 42
|
1448 | */
|
1449 | var defaultTo = _curry2(function defaultTo(d, v) {
|
1450 | return v == null || v !== v ? d : v;
|
1451 | });
|
1452 |
|
1453 | /**
|
1454 | * Makes a descending comparator function out of a function that returns a value
|
1455 | * that can be compared with `<` and `>`.
|
1456 | *
|
1457 | * @func
|
1458 | * @memberOf R
|
1459 | * @since v0.23.0
|
1460 | * @category Function
|
1461 | * @sig Ord b => (a -> b) -> a -> a -> Number
|
1462 | * @param {Function} fn A function of arity one that returns a value that can be compared
|
1463 | * @param {*} a The first item to be compared.
|
1464 | * @param {*} b The second item to be compared.
|
1465 | * @return {Number} `-1` if fn(a) > fn(b), `1` if fn(b) > fn(a), otherwise `0`
|
1466 | * @example
|
1467 | *
|
1468 | * var byAge = R.descend(R.prop('age'));
|
1469 | * var people = [
|
1470 | * // ...
|
1471 | * ];
|
1472 | * var peopleByOldestFirst = R.sort(byAge, people);
|
1473 | */
|
1474 | var descend = _curry3(function descend(fn, a, b) {
|
1475 | var aa = fn(a);
|
1476 | var bb = fn(b);
|
1477 | return aa > bb ? -1 : aa < bb ? 1 : 0;
|
1478 | });
|
1479 |
|
1480 | /**
|
1481 | * Finds the set (i.e. no duplicates) of all elements in the first list not
|
1482 | * contained in the second list. Duplication is determined according to the
|
1483 | * value returned by applying the supplied predicate to two list elements.
|
1484 | *
|
1485 | * @func
|
1486 | * @memberOf R
|
1487 | * @since v0.1.0
|
1488 | * @category Relation
|
1489 | * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
|
1490 | * @param {Function} pred A predicate used to test whether two items are equal.
|
1491 | * @param {Array} list1 The first list.
|
1492 | * @param {Array} list2 The second list.
|
1493 | * @return {Array} The elements in `list1` that are not in `list2`.
|
1494 | * @see R.difference, R.symmetricDifference, R.symmetricDifferenceWith
|
1495 | * @example
|
1496 | *
|
1497 | * var cmp = (x, y) => x.a === y.a;
|
1498 | * var l1 = [{a: 1}, {a: 2}, {a: 3}];
|
1499 | * var l2 = [{a: 3}, {a: 4}];
|
1500 | * R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
|
1501 | */
|
1502 | var differenceWith = _curry3(function differenceWith(pred, first, second) {
|
1503 | var out = [];
|
1504 | var idx = 0;
|
1505 | var firstLen = first.length;
|
1506 | while (idx < firstLen) {
|
1507 | if (!_containsWith(pred, first[idx], second) && !_containsWith(pred, first[idx], out)) {
|
1508 | out.push(first[idx]);
|
1509 | }
|
1510 | idx += 1;
|
1511 | }
|
1512 | return out;
|
1513 | });
|
1514 |
|
1515 | /**
|
1516 | * Returns a new object that does not contain a `prop` property.
|
1517 | *
|
1518 | * @func
|
1519 | * @memberOf R
|
1520 | * @since v0.10.0
|
1521 | * @category Object
|
1522 | * @sig String -> {k: v} -> {k: v}
|
1523 | * @param {String} prop The name of the property to dissociate
|
1524 | * @param {Object} obj The object to clone
|
1525 | * @return {Object} A new object equivalent to the original but without the specified property
|
1526 | * @see R.assoc
|
1527 | * @example
|
1528 | *
|
1529 | * R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3}
|
1530 | */
|
1531 | var dissoc = _curry2(function dissoc(prop, obj) {
|
1532 | var result = {};
|
1533 | for (var p in obj) {
|
1534 | result[p] = obj[p];
|
1535 | }
|
1536 | delete result[prop];
|
1537 | return result;
|
1538 | });
|
1539 |
|
1540 | /**
|
1541 | * Makes a shallow clone of an object, omitting the property at the given path.
|
1542 | * Note that this copies and flattens prototype properties onto the new object
|
1543 | * as well. All non-primitive properties are copied by reference.
|
1544 | *
|
1545 | * @func
|
1546 | * @memberOf R
|
1547 | * @since v0.11.0
|
1548 | * @category Object
|
1549 | * @sig [String] -> {k: v} -> {k: v}
|
1550 | * @param {Array} path The path to the value to omit
|
1551 | * @param {Object} obj The object to clone
|
1552 | * @return {Object} A new object without the property at path
|
1553 | * @see R.assocPath
|
1554 | * @example
|
1555 | *
|
1556 | * R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}}
|
1557 | */
|
1558 | var dissocPath = _curry2(function dissocPath(path, obj) {
|
1559 | switch (path.length) {
|
1560 | case 0:
|
1561 | return obj;
|
1562 | case 1:
|
1563 | return dissoc(path[0], obj);
|
1564 | default:
|
1565 | var head = path[0];
|
1566 | var tail = Array.prototype.slice.call(path, 1);
|
1567 | return obj[head] == null ? obj : assoc(head, dissocPath(tail, obj[head]), obj);
|
1568 | }
|
1569 | });
|
1570 |
|
1571 | /**
|
1572 | * Divides two numbers. Equivalent to `a / b`.
|
1573 | *
|
1574 | * @func
|
1575 | * @memberOf R
|
1576 | * @since v0.1.0
|
1577 | * @category Math
|
1578 | * @sig Number -> Number -> Number
|
1579 | * @param {Number} a The first value.
|
1580 | * @param {Number} b The second value.
|
1581 | * @return {Number} The result of `a / b`.
|
1582 | * @see R.multiply
|
1583 | * @example
|
1584 | *
|
1585 | * R.divide(71, 100); //=> 0.71
|
1586 | *
|
1587 | * var half = R.divide(R.__, 2);
|
1588 | * half(42); //=> 21
|
1589 | *
|
1590 | * var reciprocal = R.divide(1);
|
1591 | * reciprocal(4); //=> 0.25
|
1592 | */
|
1593 | var divide = _curry2(function divide(a, b) {
|
1594 | return a / b;
|
1595 | });
|
1596 |
|
1597 | /**
|
1598 | * Returns a new list excluding the leading elements of a given list which
|
1599 | * satisfy the supplied predicate function. It passes each value to the supplied
|
1600 | * predicate function, skipping elements while the predicate function returns
|
1601 | * `true`. The predicate function is applied to one argument: *(value)*.
|
1602 | *
|
1603 | * Dispatches to the `dropWhile` method of the second argument, if present.
|
1604 | *
|
1605 | * Acts as a transducer if a transformer is given in list position.
|
1606 | *
|
1607 | * @func
|
1608 | * @memberOf R
|
1609 | * @since v0.9.0
|
1610 | * @category List
|
1611 | * @sig (a -> Boolean) -> [a] -> [a]
|
1612 | * @param {Function} fn The function called per iteration.
|
1613 | * @param {Array} list The collection to iterate over.
|
1614 | * @return {Array} A new array.
|
1615 | * @see R.takeWhile, R.transduce, R.addIndex
|
1616 | * @example
|
1617 | *
|
1618 | * var lteTwo = x => x <= 2;
|
1619 | *
|
1620 | * R.dropWhile(lteTwo, [1, 2, 3, 4, 3, 2, 1]); //=> [3, 4, 3, 2, 1]
|
1621 | */
|
1622 | var dropWhile = _curry2(_dispatchable(['dropWhile'], _xdropWhile, function dropWhile(pred, list) {
|
1623 | var idx = 0;
|
1624 | var len = list.length;
|
1625 | while (idx < len && pred(list[idx])) {
|
1626 | idx += 1;
|
1627 | }
|
1628 | return Array.prototype.slice.call(list, idx);
|
1629 | }));
|
1630 |
|
1631 | /**
|
1632 | * Returns the empty value of its argument's type. Ramda defines the empty
|
1633 | * value of Array (`[]`), Object (`{}`), String (`''`), and Arguments. Other
|
1634 | * types are supported if they define `<Type>.empty` and/or
|
1635 | * `<Type>.prototype.empty`.
|
1636 | *
|
1637 | * Dispatches to the `empty` method of the first argument, if present.
|
1638 | *
|
1639 | * @func
|
1640 | * @memberOf R
|
1641 | * @since v0.3.0
|
1642 | * @category Function
|
1643 | * @sig a -> a
|
1644 | * @param {*} x
|
1645 | * @return {*}
|
1646 | * @example
|
1647 | *
|
1648 | * R.empty(Just(42)); //=> Nothing()
|
1649 | * R.empty([1, 2, 3]); //=> []
|
1650 | * R.empty('unicorns'); //=> ''
|
1651 | * R.empty({x: 1, y: 2}); //=> {}
|
1652 | */
|
1653 | // else
|
1654 | var empty = _curry1(function empty(x) {
|
1655 | return x != null && typeof x.empty === 'function' ? x.empty() : x != null && x.constructor != null && typeof x.constructor.empty === 'function' ? x.constructor.empty() : _isArray(x) ? [] : _isString(x) ? '' : _isObject(x) ? {} : _isArguments(x) ? function () {
|
1656 | return arguments;
|
1657 | }() : // else
|
1658 | void 0;
|
1659 | });
|
1660 |
|
1661 | /**
|
1662 | * Creates a new object by recursively evolving a shallow copy of `object`,
|
1663 | * according to the `transformation` functions. All non-primitive properties
|
1664 | * are copied by reference.
|
1665 | *
|
1666 | * A `transformation` function will not be invoked if its corresponding key
|
1667 | * does not exist in the evolved object.
|
1668 | *
|
1669 | * @func
|
1670 | * @memberOf R
|
1671 | * @since v0.9.0
|
1672 | * @category Object
|
1673 | * @sig {k: (v -> v)} -> {k: v} -> {k: v}
|
1674 | * @param {Object} transformations The object specifying transformation functions to apply
|
1675 | * to the object.
|
1676 | * @param {Object} object The object to be transformed.
|
1677 | * @return {Object} The transformed object.
|
1678 | * @example
|
1679 | *
|
1680 | * var tomato = {firstName: ' Tomato ', data: {elapsed: 100, remaining: 1400}, id:123};
|
1681 | * var transformations = {
|
1682 | * firstName: R.trim,
|
1683 | * lastName: R.trim, // Will not get invoked.
|
1684 | * data: {elapsed: R.add(1), remaining: R.add(-1)}
|
1685 | * };
|
1686 | * R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}
|
1687 | */
|
1688 | var evolve = _curry2(function evolve(transformations, object) {
|
1689 | var result = {};
|
1690 | var transformation, key, type;
|
1691 | for (key in object) {
|
1692 | transformation = transformations[key];
|
1693 | type = typeof transformation;
|
1694 | result[key] = type === 'function' ? transformation(object[key]) : transformation && type === 'object' ? evolve(transformation, object[key]) : object[key];
|
1695 | }
|
1696 | return result;
|
1697 | });
|
1698 |
|
1699 | /**
|
1700 | * Returns the first element of the list which matches the predicate, or
|
1701 | * `undefined` if no element matches.
|
1702 | *
|
1703 | * Dispatches to the `find` method of the second argument, if present.
|
1704 | *
|
1705 | * Acts as a transducer if a transformer is given in list position.
|
1706 | *
|
1707 | * @func
|
1708 | * @memberOf R
|
1709 | * @since v0.1.0
|
1710 | * @category List
|
1711 | * @sig (a -> Boolean) -> [a] -> a | undefined
|
1712 | * @param {Function} fn The predicate function used to determine if the element is the
|
1713 | * desired one.
|
1714 | * @param {Array} list The array to consider.
|
1715 | * @return {Object} The element found, or `undefined`.
|
1716 | * @see R.transduce
|
1717 | * @example
|
1718 | *
|
1719 | * var xs = [{a: 1}, {a: 2}, {a: 3}];
|
1720 | * R.find(R.propEq('a', 2))(xs); //=> {a: 2}
|
1721 | * R.find(R.propEq('a', 4))(xs); //=> undefined
|
1722 | */
|
1723 | var find = _curry2(_dispatchable(['find'], _xfind, function find(fn, list) {
|
1724 | var idx = 0;
|
1725 | var len = list.length;
|
1726 | while (idx < len) {
|
1727 | if (fn(list[idx])) {
|
1728 | return list[idx];
|
1729 | }
|
1730 | idx += 1;
|
1731 | }
|
1732 | }));
|
1733 |
|
1734 | /**
|
1735 | * Returns the index of the first element of the list which matches the
|
1736 | * predicate, or `-1` if no element matches.
|
1737 | *
|
1738 | * Acts as a transducer if a transformer is given in list position.
|
1739 | *
|
1740 | * @func
|
1741 | * @memberOf R
|
1742 | * @since v0.1.1
|
1743 | * @category List
|
1744 | * @sig (a -> Boolean) -> [a] -> Number
|
1745 | * @param {Function} fn The predicate function used to determine if the element is the
|
1746 | * desired one.
|
1747 | * @param {Array} list The array to consider.
|
1748 | * @return {Number} The index of the element found, or `-1`.
|
1749 | * @see R.transduce
|
1750 | * @example
|
1751 | *
|
1752 | * var xs = [{a: 1}, {a: 2}, {a: 3}];
|
1753 | * R.findIndex(R.propEq('a', 2))(xs); //=> 1
|
1754 | * R.findIndex(R.propEq('a', 4))(xs); //=> -1
|
1755 | */
|
1756 | var findIndex = _curry2(_dispatchable([], _xfindIndex, function findIndex(fn, list) {
|
1757 | var idx = 0;
|
1758 | var len = list.length;
|
1759 | while (idx < len) {
|
1760 | if (fn(list[idx])) {
|
1761 | return idx;
|
1762 | }
|
1763 | idx += 1;
|
1764 | }
|
1765 | return -1;
|
1766 | }));
|
1767 |
|
1768 | /**
|
1769 | * Returns the last element of the list which matches the predicate, or
|
1770 | * `undefined` if no element matches.
|
1771 | *
|
1772 | * Acts as a transducer if a transformer is given in list position.
|
1773 | *
|
1774 | * @func
|
1775 | * @memberOf R
|
1776 | * @since v0.1.1
|
1777 | * @category List
|
1778 | * @sig (a -> Boolean) -> [a] -> a | undefined
|
1779 | * @param {Function} fn The predicate function used to determine if the element is the
|
1780 | * desired one.
|
1781 | * @param {Array} list The array to consider.
|
1782 | * @return {Object} The element found, or `undefined`.
|
1783 | * @see R.transduce
|
1784 | * @example
|
1785 | *
|
1786 | * var xs = [{a: 1, b: 0}, {a:1, b: 1}];
|
1787 | * R.findLast(R.propEq('a', 1))(xs); //=> {a: 1, b: 1}
|
1788 | * R.findLast(R.propEq('a', 4))(xs); //=> undefined
|
1789 | */
|
1790 | var findLast = _curry2(_dispatchable([], _xfindLast, function findLast(fn, list) {
|
1791 | var idx = list.length - 1;
|
1792 | while (idx >= 0) {
|
1793 | if (fn(list[idx])) {
|
1794 | return list[idx];
|
1795 | }
|
1796 | idx -= 1;
|
1797 | }
|
1798 | }));
|
1799 |
|
1800 | /**
|
1801 | * Returns the index of the last element of the list which matches the
|
1802 | * predicate, or `-1` if no element matches.
|
1803 | *
|
1804 | * Acts as a transducer if a transformer is given in list position.
|
1805 | *
|
1806 | * @func
|
1807 | * @memberOf R
|
1808 | * @since v0.1.1
|
1809 | * @category List
|
1810 | * @sig (a -> Boolean) -> [a] -> Number
|
1811 | * @param {Function} fn The predicate function used to determine if the element is the
|
1812 | * desired one.
|
1813 | * @param {Array} list The array to consider.
|
1814 | * @return {Number} The index of the element found, or `-1`.
|
1815 | * @see R.transduce
|
1816 | * @example
|
1817 | *
|
1818 | * var xs = [{a: 1, b: 0}, {a:1, b: 1}];
|
1819 | * R.findLastIndex(R.propEq('a', 1))(xs); //=> 1
|
1820 | * R.findLastIndex(R.propEq('a', 4))(xs); //=> -1
|
1821 | */
|
1822 | var findLastIndex = _curry2(_dispatchable([], _xfindLastIndex, function findLastIndex(fn, list) {
|
1823 | var idx = list.length - 1;
|
1824 | while (idx >= 0) {
|
1825 | if (fn(list[idx])) {
|
1826 | return idx;
|
1827 | }
|
1828 | idx -= 1;
|
1829 | }
|
1830 | return -1;
|
1831 | }));
|
1832 |
|
1833 | /**
|
1834 | * Iterate over an input `list`, calling a provided function `fn` for each
|
1835 | * element in the list.
|
1836 | *
|
1837 | * `fn` receives one argument: *(value)*.
|
1838 | *
|
1839 | * Note: `R.forEach` does not skip deleted or unassigned indices (sparse
|
1840 | * arrays), unlike the native `Array.prototype.forEach` method. For more
|
1841 | * details on this behavior, see:
|
1842 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
|
1843 | *
|
1844 | * Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns
|
1845 | * the original array. In some libraries this function is named `each`.
|
1846 | *
|
1847 | * Dispatches to the `forEach` method of the second argument, if present.
|
1848 | *
|
1849 | * @func
|
1850 | * @memberOf R
|
1851 | * @since v0.1.1
|
1852 | * @category List
|
1853 | * @sig (a -> *) -> [a] -> [a]
|
1854 | * @param {Function} fn The function to invoke. Receives one argument, `value`.
|
1855 | * @param {Array} list The list to iterate over.
|
1856 | * @return {Array} The original list.
|
1857 | * @see R.addIndex
|
1858 | * @example
|
1859 | *
|
1860 | * var printXPlusFive = x => console.log(x + 5);
|
1861 | * R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
|
1862 | * // logs 6
|
1863 | * // logs 7
|
1864 | * // logs 8
|
1865 | * @symb R.forEach(f, [a, b, c]) = [a, b, c]
|
1866 | */
|
1867 | var forEach = _curry2(_checkForMethod('forEach', function forEach(fn, list) {
|
1868 | var len = list.length;
|
1869 | var idx = 0;
|
1870 | while (idx < len) {
|
1871 | fn(list[idx]);
|
1872 | idx += 1;
|
1873 | }
|
1874 | return list;
|
1875 | }));
|
1876 |
|
1877 | /**
|
1878 | * Creates a new object from a list key-value pairs. If a key appears in
|
1879 | * multiple pairs, the rightmost pair is included in the object.
|
1880 | *
|
1881 | * @func
|
1882 | * @memberOf R
|
1883 | * @since v0.3.0
|
1884 | * @category List
|
1885 | * @sig [[k,v]] -> {k: v}
|
1886 | * @param {Array} pairs An array of two-element arrays that will be the keys and values of the output object.
|
1887 | * @return {Object} The object made by pairing up `keys` and `values`.
|
1888 | * @see R.toPairs, R.pair
|
1889 | * @example
|
1890 | *
|
1891 | * R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}
|
1892 | */
|
1893 | var fromPairs = _curry1(function fromPairs(pairs) {
|
1894 | var result = {};
|
1895 | var idx = 0;
|
1896 | while (idx < pairs.length) {
|
1897 | result[pairs[idx][0]] = pairs[idx][1];
|
1898 | idx += 1;
|
1899 | }
|
1900 | return result;
|
1901 | });
|
1902 |
|
1903 | /**
|
1904 | * Takes a list and returns a list of lists where each sublist's elements are
|
1905 | * all "equal" according to the provided equality function.
|
1906 | *
|
1907 | * @func
|
1908 | * @memberOf R
|
1909 | * @since v0.21.0
|
1910 | * @category List
|
1911 | * @sig ((a, a) → Boolean) → [a] → [[a]]
|
1912 | * @param {Function} fn Function for determining whether two given (adjacent)
|
1913 | * elements should be in the same group
|
1914 | * @param {Array} list The array to group. Also accepts a string, which will be
|
1915 | * treated as a list of characters.
|
1916 | * @return {List} A list that contains sublists of equal elements,
|
1917 | * whose concatenations are equal to the original list.
|
1918 | * @example
|
1919 | *
|
1920 | * R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21])
|
1921 | * //=> [[0], [1, 1], [2], [3], [5], [8], [13], [21]]
|
1922 | *
|
1923 | * R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21])
|
1924 | * //=> [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
|
1925 | *
|
1926 | * R.groupWith(R.eqBy(isVowel), 'aestiou')
|
1927 | * //=> ['ae', 'st', 'iou']
|
1928 | */
|
1929 | var groupWith = _curry2(function (fn, list) {
|
1930 | var res = [];
|
1931 | var idx = 0;
|
1932 | var len = list.length;
|
1933 | while (idx < len) {
|
1934 | var nextidx = idx + 1;
|
1935 | while (nextidx < len && fn(list[idx], list[nextidx])) {
|
1936 | nextidx += 1;
|
1937 | }
|
1938 | res.push(list.slice(idx, nextidx));
|
1939 | idx = nextidx;
|
1940 | }
|
1941 | return res;
|
1942 | });
|
1943 |
|
1944 | /**
|
1945 | * Returns `true` if the first argument is greater than the second; `false`
|
1946 | * otherwise.
|
1947 | *
|
1948 | * @func
|
1949 | * @memberOf R
|
1950 | * @since v0.1.0
|
1951 | * @category Relation
|
1952 | * @sig Ord a => a -> a -> Boolean
|
1953 | * @param {*} a
|
1954 | * @param {*} b
|
1955 | * @return {Boolean}
|
1956 | * @see R.lt
|
1957 | * @example
|
1958 | *
|
1959 | * R.gt(2, 1); //=> true
|
1960 | * R.gt(2, 2); //=> false
|
1961 | * R.gt(2, 3); //=> false
|
1962 | * R.gt('a', 'z'); //=> false
|
1963 | * R.gt('z', 'a'); //=> true
|
1964 | */
|
1965 | var gt = _curry2(function gt(a, b) {
|
1966 | return a > b;
|
1967 | });
|
1968 |
|
1969 | /**
|
1970 | * Returns `true` if the first argument is greater than or equal to the second;
|
1971 | * `false` otherwise.
|
1972 | *
|
1973 | * @func
|
1974 | * @memberOf R
|
1975 | * @since v0.1.0
|
1976 | * @category Relation
|
1977 | * @sig Ord a => a -> a -> Boolean
|
1978 | * @param {Number} a
|
1979 | * @param {Number} b
|
1980 | * @return {Boolean}
|
1981 | * @see R.lte
|
1982 | * @example
|
1983 | *
|
1984 | * R.gte(2, 1); //=> true
|
1985 | * R.gte(2, 2); //=> true
|
1986 | * R.gte(2, 3); //=> false
|
1987 | * R.gte('a', 'z'); //=> false
|
1988 | * R.gte('z', 'a'); //=> true
|
1989 | */
|
1990 | var gte = _curry2(function gte(a, b) {
|
1991 | return a >= b;
|
1992 | });
|
1993 |
|
1994 | /**
|
1995 | * Returns whether or not an object has an own property with the specified name
|
1996 | *
|
1997 | * @func
|
1998 | * @memberOf R
|
1999 | * @since v0.7.0
|
2000 | * @category Object
|
2001 | * @sig s -> {s: x} -> Boolean
|
2002 | * @param {String} prop The name of the property to check for.
|
2003 | * @param {Object} obj The object to query.
|
2004 | * @return {Boolean} Whether the property exists.
|
2005 | * @example
|
2006 | *
|
2007 | * var hasName = R.has('name');
|
2008 | * hasName({name: 'alice'}); //=> true
|
2009 | * hasName({name: 'bob'}); //=> true
|
2010 | * hasName({}); //=> false
|
2011 | *
|
2012 | * var point = {x: 0, y: 0};
|
2013 | * var pointHas = R.has(R.__, point);
|
2014 | * pointHas('x'); //=> true
|
2015 | * pointHas('y'); //=> true
|
2016 | * pointHas('z'); //=> false
|
2017 | */
|
2018 | var has = _curry2(_has);
|
2019 |
|
2020 | /**
|
2021 | * Returns whether or not an object or its prototype chain has a property with
|
2022 | * the specified name
|
2023 | *
|
2024 | * @func
|
2025 | * @memberOf R
|
2026 | * @since v0.7.0
|
2027 | * @category Object
|
2028 | * @sig s -> {s: x} -> Boolean
|
2029 | * @param {String} prop The name of the property to check for.
|
2030 | * @param {Object} obj The object to query.
|
2031 | * @return {Boolean} Whether the property exists.
|
2032 | * @example
|
2033 | *
|
2034 | * function Rectangle(width, height) {
|
2035 | * this.width = width;
|
2036 | * this.height = height;
|
2037 | * }
|
2038 | * Rectangle.prototype.area = function() {
|
2039 | * return this.width * this.height;
|
2040 | * };
|
2041 | *
|
2042 | * var square = new Rectangle(2, 2);
|
2043 | * R.hasIn('width', square); //=> true
|
2044 | * R.hasIn('area', square); //=> true
|
2045 | */
|
2046 | var hasIn = _curry2(function hasIn(prop, obj) {
|
2047 | return prop in obj;
|
2048 | });
|
2049 |
|
2050 | /**
|
2051 | * Returns true if its arguments are identical, false otherwise. Values are
|
2052 | * identical if they reference the same memory. `NaN` is identical to `NaN`;
|
2053 | * `0` and `-0` are not identical.
|
2054 | *
|
2055 | * @func
|
2056 | * @memberOf R
|
2057 | * @since v0.15.0
|
2058 | * @category Relation
|
2059 | * @sig a -> a -> Boolean
|
2060 | * @param {*} a
|
2061 | * @param {*} b
|
2062 | * @return {Boolean}
|
2063 | * @example
|
2064 | *
|
2065 | * var o = {};
|
2066 | * R.identical(o, o); //=> true
|
2067 | * R.identical(1, 1); //=> true
|
2068 | * R.identical(1, '1'); //=> false
|
2069 | * R.identical([], []); //=> false
|
2070 | * R.identical(0, -0); //=> false
|
2071 | * R.identical(NaN, NaN); //=> true
|
2072 | */
|
2073 | // SameValue algorithm
|
2074 | // Steps 1-5, 7-10
|
2075 | // Steps 6.b-6.e: +0 != -0
|
2076 | // Step 6.a: NaN == NaN
|
2077 | var identical = _curry2(function identical(a, b) {
|
2078 | // SameValue algorithm
|
2079 | if (a === b) {
|
2080 | // Steps 1-5, 7-10
|
2081 | // Steps 6.b-6.e: +0 != -0
|
2082 | return a !== 0 || 1 / a === 1 / b;
|
2083 | } else {
|
2084 | // Step 6.a: NaN == NaN
|
2085 | return a !== a && b !== b;
|
2086 | }
|
2087 | });
|
2088 |
|
2089 | /**
|
2090 | * A function that does nothing but return the parameter supplied to it. Good
|
2091 | * as a default or placeholder function.
|
2092 | *
|
2093 | * @func
|
2094 | * @memberOf R
|
2095 | * @since v0.1.0
|
2096 | * @category Function
|
2097 | * @sig a -> a
|
2098 | * @param {*} x The value to return.
|
2099 | * @return {*} The input value, `x`.
|
2100 | * @example
|
2101 | *
|
2102 | * R.identity(1); //=> 1
|
2103 | *
|
2104 | * var obj = {};
|
2105 | * R.identity(obj) === obj; //=> true
|
2106 | * @symb R.identity(a) = a
|
2107 | */
|
2108 | var identity = _curry1(_identity);
|
2109 |
|
2110 | /**
|
2111 | * Creates a function that will process either the `onTrue` or the `onFalse`
|
2112 | * function depending upon the result of the `condition` predicate.
|
2113 | *
|
2114 | * @func
|
2115 | * @memberOf R
|
2116 | * @since v0.8.0
|
2117 | * @category Logic
|
2118 | * @sig (*... -> Boolean) -> (*... -> *) -> (*... -> *) -> (*... -> *)
|
2119 | * @param {Function} condition A predicate function
|
2120 | * @param {Function} onTrue A function to invoke when the `condition` evaluates to a truthy value.
|
2121 | * @param {Function} onFalse A function to invoke when the `condition` evaluates to a falsy value.
|
2122 | * @return {Function} A new unary function that will process either the `onTrue` or the `onFalse`
|
2123 | * function depending upon the result of the `condition` predicate.
|
2124 | * @see R.unless, R.when
|
2125 | * @example
|
2126 | *
|
2127 | * var incCount = R.ifElse(
|
2128 | * R.has('count'),
|
2129 | * R.over(R.lensProp('count'), R.inc),
|
2130 | * R.assoc('count', 1)
|
2131 | * );
|
2132 | * incCount({}); //=> { count: 1 }
|
2133 | * incCount({ count: 1 }); //=> { count: 2 }
|
2134 | */
|
2135 | var ifElse = _curry3(function ifElse(condition, onTrue, onFalse) {
|
2136 | return curryN(Math.max(condition.length, onTrue.length, onFalse.length), function _ifElse() {
|
2137 | return condition.apply(this, arguments) ? onTrue.apply(this, arguments) : onFalse.apply(this, arguments);
|
2138 | });
|
2139 | });
|
2140 |
|
2141 | /**
|
2142 | * Increments its argument.
|
2143 | *
|
2144 | * @func
|
2145 | * @memberOf R
|
2146 | * @since v0.9.0
|
2147 | * @category Math
|
2148 | * @sig Number -> Number
|
2149 | * @param {Number} n
|
2150 | * @return {Number} n + 1
|
2151 | * @see R.dec
|
2152 | * @example
|
2153 | *
|
2154 | * R.inc(42); //=> 43
|
2155 | */
|
2156 | var inc = add(1);
|
2157 |
|
2158 | /**
|
2159 | * Inserts the supplied element into the list, at index `index`. _Note that
|
2160 | * this is not destructive_: it returns a copy of the list with the changes.
|
2161 | * <small>No lists have been harmed in the application of this function.</small>
|
2162 | *
|
2163 | * @func
|
2164 | * @memberOf R
|
2165 | * @since v0.2.2
|
2166 | * @category List
|
2167 | * @sig Number -> a -> [a] -> [a]
|
2168 | * @param {Number} index The position to insert the element
|
2169 | * @param {*} elt The element to insert into the Array
|
2170 | * @param {Array} list The list to insert into
|
2171 | * @return {Array} A new Array with `elt` inserted at `index`.
|
2172 | * @example
|
2173 | *
|
2174 | * R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
|
2175 | */
|
2176 | var insert = _curry3(function insert(idx, elt, list) {
|
2177 | idx = idx < list.length && idx >= 0 ? idx : list.length;
|
2178 | var result = Array.prototype.slice.call(list, 0);
|
2179 | result.splice(idx, 0, elt);
|
2180 | return result;
|
2181 | });
|
2182 |
|
2183 | /**
|
2184 | * Inserts the sub-list into the list, at index `index`. _Note that this is not
|
2185 | * destructive_: it returns a copy of the list with the changes.
|
2186 | * <small>No lists have been harmed in the application of this function.</small>
|
2187 | *
|
2188 | * @func
|
2189 | * @memberOf R
|
2190 | * @since v0.9.0
|
2191 | * @category List
|
2192 | * @sig Number -> [a] -> [a] -> [a]
|
2193 | * @param {Number} index The position to insert the sub-list
|
2194 | * @param {Array} elts The sub-list to insert into the Array
|
2195 | * @param {Array} list The list to insert the sub-list into
|
2196 | * @return {Array} A new Array with `elts` inserted starting at `index`.
|
2197 | * @example
|
2198 | *
|
2199 | * R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4]
|
2200 | */
|
2201 | var insertAll = _curry3(function insertAll(idx, elts, list) {
|
2202 | idx = idx < list.length && idx >= 0 ? idx : list.length;
|
2203 | return [].concat(Array.prototype.slice.call(list, 0, idx), elts, Array.prototype.slice.call(list, idx));
|
2204 | });
|
2205 |
|
2206 | /**
|
2207 | * Creates a new list with the separator interposed between elements.
|
2208 | *
|
2209 | * Dispatches to the `intersperse` method of the second argument, if present.
|
2210 | *
|
2211 | * @func
|
2212 | * @memberOf R
|
2213 | * @since v0.14.0
|
2214 | * @category List
|
2215 | * @sig a -> [a] -> [a]
|
2216 | * @param {*} separator The element to add to the list.
|
2217 | * @param {Array} list The list to be interposed.
|
2218 | * @return {Array} The new list.
|
2219 | * @example
|
2220 | *
|
2221 | * R.intersperse('n', ['ba', 'a', 'a']); //=> ['ba', 'n', 'a', 'n', 'a']
|
2222 | */
|
2223 | var intersperse = _curry2(_checkForMethod('intersperse', function intersperse(separator, list) {
|
2224 | var out = [];
|
2225 | var idx = 0;
|
2226 | var length = list.length;
|
2227 | while (idx < length) {
|
2228 | if (idx === length - 1) {
|
2229 | out.push(list[idx]);
|
2230 | } else {
|
2231 | out.push(list[idx], separator);
|
2232 | }
|
2233 | idx += 1;
|
2234 | }
|
2235 | return out;
|
2236 | }));
|
2237 |
|
2238 | /**
|
2239 | * See if an object (`val`) is an instance of the supplied constructor. This
|
2240 | * function will check up the inheritance chain, if any.
|
2241 | *
|
2242 | * @func
|
2243 | * @memberOf R
|
2244 | * @since v0.3.0
|
2245 | * @category Type
|
2246 | * @sig (* -> {*}) -> a -> Boolean
|
2247 | * @param {Object} ctor A constructor
|
2248 | * @param {*} val The value to test
|
2249 | * @return {Boolean}
|
2250 | * @example
|
2251 | *
|
2252 | * R.is(Object, {}); //=> true
|
2253 | * R.is(Number, 1); //=> true
|
2254 | * R.is(Object, 1); //=> false
|
2255 | * R.is(String, 's'); //=> true
|
2256 | * R.is(String, new String('')); //=> true
|
2257 | * R.is(Object, new String('')); //=> true
|
2258 | * R.is(Object, 's'); //=> false
|
2259 | * R.is(Number, {}); //=> false
|
2260 | */
|
2261 | var is = _curry2(function is(Ctor, val) {
|
2262 | return val != null && val.constructor === Ctor || val instanceof Ctor;
|
2263 | });
|
2264 |
|
2265 | /**
|
2266 | * Tests whether or not an object is similar to an array.
|
2267 | *
|
2268 | * @func
|
2269 | * @memberOf R
|
2270 | * @since v0.5.0
|
2271 | * @category Type
|
2272 | * @category List
|
2273 | * @sig * -> Boolean
|
2274 | * @param {*} x The object to test.
|
2275 | * @return {Boolean} `true` if `x` has a numeric length property and extreme indices defined; `false` otherwise.
|
2276 | * @deprecated since v0.23.0
|
2277 | * @example
|
2278 | *
|
2279 | * R.isArrayLike([]); //=> true
|
2280 | * R.isArrayLike(true); //=> false
|
2281 | * R.isArrayLike({}); //=> false
|
2282 | * R.isArrayLike({length: 10}); //=> false
|
2283 | * R.isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
|
2284 | */
|
2285 | var isArrayLike = _curry1(function isArrayLike(x) {
|
2286 | if (_isArray(x)) {
|
2287 | return true;
|
2288 | }
|
2289 | if (!x) {
|
2290 | return false;
|
2291 | }
|
2292 | if (typeof x !== 'object') {
|
2293 | return false;
|
2294 | }
|
2295 | if (_isString(x)) {
|
2296 | return false;
|
2297 | }
|
2298 | if (x.nodeType === 1) {
|
2299 | return !!x.length;
|
2300 | }
|
2301 | if (x.length === 0) {
|
2302 | return true;
|
2303 | }
|
2304 | if (x.length > 0) {
|
2305 | return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1);
|
2306 | }
|
2307 | return false;
|
2308 | });
|
2309 |
|
2310 | /**
|
2311 | * Checks if the input value is `null` or `undefined`.
|
2312 | *
|
2313 | * @func
|
2314 | * @memberOf R
|
2315 | * @since v0.9.0
|
2316 | * @category Type
|
2317 | * @sig * -> Boolean
|
2318 | * @param {*} x The value to test.
|
2319 | * @return {Boolean} `true` if `x` is `undefined` or `null`, otherwise `false`.
|
2320 | * @example
|
2321 | *
|
2322 | * R.isNil(null); //=> true
|
2323 | * R.isNil(undefined); //=> true
|
2324 | * R.isNil(0); //=> false
|
2325 | * R.isNil([]); //=> false
|
2326 | */
|
2327 | var isNil = _curry1(function isNil(x) {
|
2328 | return x == null;
|
2329 | });
|
2330 |
|
2331 | /**
|
2332 | * Returns a list containing the names of all the enumerable own properties of
|
2333 | * the supplied object.
|
2334 | * Note that the order of the output array is not guaranteed to be consistent
|
2335 | * across different JS platforms.
|
2336 | *
|
2337 | * @func
|
2338 | * @memberOf R
|
2339 | * @since v0.1.0
|
2340 | * @category Object
|
2341 | * @sig {k: v} -> [k]
|
2342 | * @param {Object} obj The object to extract properties from
|
2343 | * @return {Array} An array of the object's own properties.
|
2344 | * @example
|
2345 | *
|
2346 | * R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
|
2347 | */
|
2348 | // cover IE < 9 keys issues
|
2349 | // Safari bug
|
2350 | var keys = function () {
|
2351 | // cover IE < 9 keys issues
|
2352 | var hasEnumBug = !{ toString: null }.propertyIsEnumerable('toString');
|
2353 | var nonEnumerableProps = [
|
2354 | 'constructor',
|
2355 | 'valueOf',
|
2356 | 'isPrototypeOf',
|
2357 | 'toString',
|
2358 | 'propertyIsEnumerable',
|
2359 | 'hasOwnProperty',
|
2360 | 'toLocaleString'
|
2361 | ];
|
2362 | // Safari bug
|
2363 | var hasArgsEnumBug = function () {
|
2364 | ;
|
2365 | return arguments.propertyIsEnumerable('length');
|
2366 | }();
|
2367 | var contains = function contains(list, item) {
|
2368 | var idx = 0;
|
2369 | while (idx < list.length) {
|
2370 | if (list[idx] === item) {
|
2371 | return true;
|
2372 | }
|
2373 | idx += 1;
|
2374 | }
|
2375 | return false;
|
2376 | };
|
2377 | return typeof Object.keys === 'function' && !hasArgsEnumBug ? _curry1(function keys(obj) {
|
2378 | return Object(obj) !== obj ? [] : Object.keys(obj);
|
2379 | }) : _curry1(function keys(obj) {
|
2380 | if (Object(obj) !== obj) {
|
2381 | return [];
|
2382 | }
|
2383 | var prop, nIdx;
|
2384 | var ks = [];
|
2385 | var checkArgsLength = hasArgsEnumBug && _isArguments(obj);
|
2386 | for (prop in obj) {
|
2387 | if (_has(prop, obj) && (!checkArgsLength || prop !== 'length')) {
|
2388 | ks[ks.length] = prop;
|
2389 | }
|
2390 | }
|
2391 | if (hasEnumBug) {
|
2392 | nIdx = nonEnumerableProps.length - 1;
|
2393 | while (nIdx >= 0) {
|
2394 | prop = nonEnumerableProps[nIdx];
|
2395 | if (_has(prop, obj) && !contains(ks, prop)) {
|
2396 | ks[ks.length] = prop;
|
2397 | }
|
2398 | nIdx -= 1;
|
2399 | }
|
2400 | }
|
2401 | return ks;
|
2402 | });
|
2403 | }();
|
2404 |
|
2405 | /**
|
2406 | * Returns a list containing the names of all the properties of the supplied
|
2407 | * object, including prototype properties.
|
2408 | * Note that the order of the output array is not guaranteed to be consistent
|
2409 | * across different JS platforms.
|
2410 | *
|
2411 | * @func
|
2412 | * @memberOf R
|
2413 | * @since v0.2.0
|
2414 | * @category Object
|
2415 | * @sig {k: v} -> [k]
|
2416 | * @param {Object} obj The object to extract properties from
|
2417 | * @return {Array} An array of the object's own and prototype properties.
|
2418 | * @example
|
2419 | *
|
2420 | * var F = function() { this.x = 'X'; };
|
2421 | * F.prototype.y = 'Y';
|
2422 | * var f = new F();
|
2423 | * R.keysIn(f); //=> ['x', 'y']
|
2424 | */
|
2425 | var keysIn = _curry1(function keysIn(obj) {
|
2426 | var prop;
|
2427 | var ks = [];
|
2428 | for (prop in obj) {
|
2429 | ks[ks.length] = prop;
|
2430 | }
|
2431 | return ks;
|
2432 | });
|
2433 |
|
2434 | /**
|
2435 | * Returns the number of elements in the array by returning `list.length`.
|
2436 | *
|
2437 | * @func
|
2438 | * @memberOf R
|
2439 | * @since v0.3.0
|
2440 | * @category List
|
2441 | * @sig [a] -> Number
|
2442 | * @param {Array} list The array to inspect.
|
2443 | * @return {Number} The length of the array.
|
2444 | * @example
|
2445 | *
|
2446 | * R.length([]); //=> 0
|
2447 | * R.length([1, 2, 3]); //=> 3
|
2448 | */
|
2449 | var length = _curry1(function length(list) {
|
2450 | return list != null && _isNumber(list.length) ? list.length : NaN;
|
2451 | });
|
2452 |
|
2453 | /**
|
2454 | * Returns `true` if the first argument is less than the second; `false`
|
2455 | * otherwise.
|
2456 | *
|
2457 | * @func
|
2458 | * @memberOf R
|
2459 | * @since v0.1.0
|
2460 | * @category Relation
|
2461 | * @sig Ord a => a -> a -> Boolean
|
2462 | * @param {*} a
|
2463 | * @param {*} b
|
2464 | * @return {Boolean}
|
2465 | * @see R.gt
|
2466 | * @example
|
2467 | *
|
2468 | * R.lt(2, 1); //=> false
|
2469 | * R.lt(2, 2); //=> false
|
2470 | * R.lt(2, 3); //=> true
|
2471 | * R.lt('a', 'z'); //=> true
|
2472 | * R.lt('z', 'a'); //=> false
|
2473 | */
|
2474 | var lt = _curry2(function lt(a, b) {
|
2475 | return a < b;
|
2476 | });
|
2477 |
|
2478 | /**
|
2479 | * Returns `true` if the first argument is less than or equal to the second;
|
2480 | * `false` otherwise.
|
2481 | *
|
2482 | * @func
|
2483 | * @memberOf R
|
2484 | * @since v0.1.0
|
2485 | * @category Relation
|
2486 | * @sig Ord a => a -> a -> Boolean
|
2487 | * @param {Number} a
|
2488 | * @param {Number} b
|
2489 | * @return {Boolean}
|
2490 | * @see R.gte
|
2491 | * @example
|
2492 | *
|
2493 | * R.lte(2, 1); //=> false
|
2494 | * R.lte(2, 2); //=> true
|
2495 | * R.lte(2, 3); //=> true
|
2496 | * R.lte('a', 'z'); //=> true
|
2497 | * R.lte('z', 'a'); //=> false
|
2498 | */
|
2499 | var lte = _curry2(function lte(a, b) {
|
2500 | return a <= b;
|
2501 | });
|
2502 |
|
2503 | /**
|
2504 | * The mapAccum function behaves like a combination of map and reduce; it
|
2505 | * applies a function to each element of a list, passing an accumulating
|
2506 | * parameter from left to right, and returning a final value of this
|
2507 | * accumulator together with the new list.
|
2508 | *
|
2509 | * The iterator function receives two arguments, *acc* and *value*, and should
|
2510 | * return a tuple *[acc, value]*.
|
2511 | *
|
2512 | * @func
|
2513 | * @memberOf R
|
2514 | * @since v0.10.0
|
2515 | * @category List
|
2516 | * @sig (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
|
2517 | * @param {Function} fn The function to be called on every element of the input `list`.
|
2518 | * @param {*} acc The accumulator value.
|
2519 | * @param {Array} list The list to iterate over.
|
2520 | * @return {*} The final, accumulated value.
|
2521 | * @see R.addIndex, R.mapAccumRight
|
2522 | * @example
|
2523 | *
|
2524 | * var digits = ['1', '2', '3', '4'];
|
2525 | * var appender = (a, b) => [a + b, a + b];
|
2526 | *
|
2527 | * R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
|
2528 | * @symb R.mapAccum(f, a, [b, c, d]) = [
|
2529 | * f(f(f(a, b)[0], c)[0], d)[0],
|
2530 | * [
|
2531 | * f(a, b)[1],
|
2532 | * f(f(a, b)[0], c)[1],
|
2533 | * f(f(f(a, b)[0], c)[0], d)[1]
|
2534 | * ]
|
2535 | * ]
|
2536 | */
|
2537 | var mapAccum = _curry3(function mapAccum(fn, acc, list) {
|
2538 | var idx = 0;
|
2539 | var len = list.length;
|
2540 | var result = [];
|
2541 | var tuple = [acc];
|
2542 | while (idx < len) {
|
2543 | tuple = fn(tuple[0], list[idx]);
|
2544 | result[idx] = tuple[1];
|
2545 | idx += 1;
|
2546 | }
|
2547 | return [
|
2548 | tuple[0],
|
2549 | result
|
2550 | ];
|
2551 | });
|
2552 |
|
2553 | /**
|
2554 | * The mapAccumRight function behaves like a combination of map and reduce; it
|
2555 | * applies a function to each element of a list, passing an accumulating
|
2556 | * parameter from right to left, and returning a final value of this
|
2557 | * accumulator together with the new list.
|
2558 | *
|
2559 | * Similar to `mapAccum`, except moves through the input list from the right to
|
2560 | * the left.
|
2561 | *
|
2562 | * The iterator function receives two arguments, *value* and *acc*, and should
|
2563 | * return a tuple *[value, acc]*.
|
2564 | *
|
2565 | * @func
|
2566 | * @memberOf R
|
2567 | * @since v0.10.0
|
2568 | * @category List
|
2569 | * @sig (x-> acc -> (y, acc)) -> acc -> [x] -> ([y], acc)
|
2570 | * @param {Function} fn The function to be called on every element of the input `list`.
|
2571 | * @param {*} acc The accumulator value.
|
2572 | * @param {Array} list The list to iterate over.
|
2573 | * @return {*} The final, accumulated value.
|
2574 | * @see R.addIndex, R.mapAccum
|
2575 | * @example
|
2576 | *
|
2577 | * var digits = ['1', '2', '3', '4'];
|
2578 | * var append = (a, b) => [a + b, a + b];
|
2579 | *
|
2580 | * R.mapAccumRight(append, 5, digits); //=> [['12345', '2345', '345', '45'], '12345']
|
2581 | * @symb R.mapAccumRight(f, a, [b, c, d]) = [
|
2582 | * [
|
2583 | * f(b, f(c, f(d, a)[0])[0])[1],
|
2584 | * f(c, f(d, a)[0])[1],
|
2585 | * f(d, a)[1],
|
2586 | * ]
|
2587 | * f(b, f(c, f(d, a)[0])[0])[0],
|
2588 | * ]
|
2589 | */
|
2590 | var mapAccumRight = _curry3(function mapAccumRight(fn, acc, list) {
|
2591 | var idx = list.length - 1;
|
2592 | var result = [];
|
2593 | var tuple = [acc];
|
2594 | while (idx >= 0) {
|
2595 | tuple = fn(list[idx], tuple[0]);
|
2596 | result[idx] = tuple[1];
|
2597 | idx -= 1;
|
2598 | }
|
2599 | return [
|
2600 | result,
|
2601 | tuple[0]
|
2602 | ];
|
2603 | });
|
2604 |
|
2605 | /**
|
2606 | * Tests a regular expression against a String. Note that this function will
|
2607 | * return an empty array when there are no matches. This differs from
|
2608 | * [`String.prototype.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
|
2609 | * which returns `null` when there are no matches.
|
2610 | *
|
2611 | * @func
|
2612 | * @memberOf R
|
2613 | * @since v0.1.0
|
2614 | * @category String
|
2615 | * @sig RegExp -> String -> [String | Undefined]
|
2616 | * @param {RegExp} rx A regular expression.
|
2617 | * @param {String} str The string to match against
|
2618 | * @return {Array} The list of matches or empty array.
|
2619 | * @see R.test
|
2620 | * @example
|
2621 | *
|
2622 | * R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na']
|
2623 | * R.match(/a/, 'b'); //=> []
|
2624 | * R.match(/a/, null); //=> TypeError: null does not have a method named "match"
|
2625 | */
|
2626 | var match = _curry2(function match(rx, str) {
|
2627 | return str.match(rx) || [];
|
2628 | });
|
2629 |
|
2630 | /**
|
2631 | * mathMod behaves like the modulo operator should mathematically, unlike the
|
2632 | * `%` operator (and by extension, R.modulo). So while "-17 % 5" is -2,
|
2633 | * mathMod(-17, 5) is 3. mathMod requires Integer arguments, and returns NaN
|
2634 | * when the modulus is zero or negative.
|
2635 | *
|
2636 | * @func
|
2637 | * @memberOf R
|
2638 | * @since v0.3.0
|
2639 | * @category Math
|
2640 | * @sig Number -> Number -> Number
|
2641 | * @param {Number} m The dividend.
|
2642 | * @param {Number} p the modulus.
|
2643 | * @return {Number} The result of `b mod a`.
|
2644 | * @example
|
2645 | *
|
2646 | * R.mathMod(-17, 5); //=> 3
|
2647 | * R.mathMod(17, 5); //=> 2
|
2648 | * R.mathMod(17, -5); //=> NaN
|
2649 | * R.mathMod(17, 0); //=> NaN
|
2650 | * R.mathMod(17.2, 5); //=> NaN
|
2651 | * R.mathMod(17, 5.3); //=> NaN
|
2652 | *
|
2653 | * var clock = R.mathMod(R.__, 12);
|
2654 | * clock(15); //=> 3
|
2655 | * clock(24); //=> 0
|
2656 | *
|
2657 | * var seventeenMod = R.mathMod(17);
|
2658 | * seventeenMod(3); //=> 2
|
2659 | * seventeenMod(4); //=> 1
|
2660 | * seventeenMod(10); //=> 7
|
2661 | */
|
2662 | var mathMod = _curry2(function mathMod(m, p) {
|
2663 | if (!_isInteger(m)) {
|
2664 | return NaN;
|
2665 | }
|
2666 | if (!_isInteger(p) || p < 1) {
|
2667 | return NaN;
|
2668 | }
|
2669 | return (m % p + p) % p;
|
2670 | });
|
2671 |
|
2672 | /**
|
2673 | * Returns the larger of its two arguments.
|
2674 | *
|
2675 | * @func
|
2676 | * @memberOf R
|
2677 | * @since v0.1.0
|
2678 | * @category Relation
|
2679 | * @sig Ord a => a -> a -> a
|
2680 | * @param {*} a
|
2681 | * @param {*} b
|
2682 | * @return {*}
|
2683 | * @see R.maxBy, R.min
|
2684 | * @example
|
2685 | *
|
2686 | * R.max(789, 123); //=> 789
|
2687 | * R.max('a', 'b'); //=> 'b'
|
2688 | */
|
2689 | var max = _curry2(function max(a, b) {
|
2690 | return b > a ? b : a;
|
2691 | });
|
2692 |
|
2693 | /**
|
2694 | * Takes a function and two values, and returns whichever value produces the
|
2695 | * larger result when passed to the provided function.
|
2696 | *
|
2697 | * @func
|
2698 | * @memberOf R
|
2699 | * @since v0.8.0
|
2700 | * @category Relation
|
2701 | * @sig Ord b => (a -> b) -> a -> a -> a
|
2702 | * @param {Function} f
|
2703 | * @param {*} a
|
2704 | * @param {*} b
|
2705 | * @return {*}
|
2706 | * @see R.max, R.minBy
|
2707 | * @example
|
2708 | *
|
2709 | * // square :: Number -> Number
|
2710 | * var square = n => n * n;
|
2711 | *
|
2712 | * R.maxBy(square, -3, 2); //=> -3
|
2713 | *
|
2714 | * R.reduce(R.maxBy(square), 0, [3, -5, 4, 1, -2]); //=> -5
|
2715 | * R.reduce(R.maxBy(square), 0, []); //=> 0
|
2716 | */
|
2717 | var maxBy = _curry3(function maxBy(f, a, b) {
|
2718 | return f(b) > f(a) ? b : a;
|
2719 | });
|
2720 |
|
2721 | /**
|
2722 | * Create a new object with the own properties of the first object merged with
|
2723 | * the own properties of the second object. If a key exists in both objects,
|
2724 | * the value from the second object will be used.
|
2725 | *
|
2726 | * @func
|
2727 | * @memberOf R
|
2728 | * @since v0.1.0
|
2729 | * @category Object
|
2730 | * @sig {k: v} -> {k: v} -> {k: v}
|
2731 | * @param {Object} l
|
2732 | * @param {Object} r
|
2733 | * @return {Object}
|
2734 | * @see R.mergeWith, R.mergeWithKey
|
2735 | * @example
|
2736 | *
|
2737 | * R.merge({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
|
2738 | * //=> { 'name': 'fred', 'age': 40 }
|
2739 | *
|
2740 | * var resetToDefault = R.merge(R.__, {x: 0});
|
2741 | * resetToDefault({x: 5, y: 2}); //=> {x: 0, y: 2}
|
2742 | * @symb R.merge({ x: 1, y: 2 }, { y: 5, z: 3 }) = { x: 1, y: 5, z: 3 }
|
2743 | */
|
2744 | var merge = _curry2(function merge(l, r) {
|
2745 | return _assign({}, l, r);
|
2746 | });
|
2747 |
|
2748 | /**
|
2749 | * Merges a list of objects together into one object.
|
2750 | *
|
2751 | * @func
|
2752 | * @memberOf R
|
2753 | * @since v0.10.0
|
2754 | * @category List
|
2755 | * @sig [{k: v}] -> {k: v}
|
2756 | * @param {Array} list An array of objects
|
2757 | * @return {Object} A merged object.
|
2758 | * @see R.reduce
|
2759 | * @example
|
2760 | *
|
2761 | * R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3}
|
2762 | * R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2}
|
2763 | * @symb R.mergeAll([{ x: 1 }, { y: 2 }, { z: 3 }]) = { x: 1, y: 2, z: 3 }
|
2764 | */
|
2765 | var mergeAll = _curry1(function mergeAll(list) {
|
2766 | return _assign.apply(null, [{}].concat(list));
|
2767 | });
|
2768 |
|
2769 | /**
|
2770 | * Creates a new object with the own properties of the two provided objects. If
|
2771 | * a key exists in both objects, the provided function is applied to the key
|
2772 | * and the values associated with the key in each object, with the result being
|
2773 | * used as the value associated with the key in the returned object. The key
|
2774 | * will be excluded from the returned object if the resulting value is
|
2775 | * `undefined`.
|
2776 | *
|
2777 | * @func
|
2778 | * @memberOf R
|
2779 | * @since v0.19.0
|
2780 | * @category Object
|
2781 | * @sig (String -> a -> a -> a) -> {a} -> {a} -> {a}
|
2782 | * @param {Function} fn
|
2783 | * @param {Object} l
|
2784 | * @param {Object} r
|
2785 | * @return {Object}
|
2786 | * @see R.merge, R.mergeWith
|
2787 | * @example
|
2788 | *
|
2789 | * let concatValues = (k, l, r) => k == 'values' ? R.concat(l, r) : r
|
2790 | * R.mergeWithKey(concatValues,
|
2791 | * { a: true, thing: 'foo', values: [10, 20] },
|
2792 | * { b: true, thing: 'bar', values: [15, 35] });
|
2793 | * //=> { a: true, b: true, thing: 'bar', values: [10, 20, 15, 35] }
|
2794 | * @symb R.mergeWithKey(f, { x: 1, y: 2 }, { y: 5, z: 3 }) = { x: 1, y: f('y', 2, 5), z: 3 }
|
2795 | */
|
2796 | var mergeWithKey = _curry3(function mergeWithKey(fn, l, r) {
|
2797 | var result = {};
|
2798 | var k;
|
2799 | for (k in l) {
|
2800 | if (_has(k, l)) {
|
2801 | result[k] = _has(k, r) ? fn(k, l[k], r[k]) : l[k];
|
2802 | }
|
2803 | }
|
2804 | for (k in r) {
|
2805 | if (_has(k, r) && !_has(k, result)) {
|
2806 | result[k] = r[k];
|
2807 | }
|
2808 | }
|
2809 | return result;
|
2810 | });
|
2811 |
|
2812 | /**
|
2813 | * Returns the smaller of its two arguments.
|
2814 | *
|
2815 | * @func
|
2816 | * @memberOf R
|
2817 | * @since v0.1.0
|
2818 | * @category Relation
|
2819 | * @sig Ord a => a -> a -> a
|
2820 | * @param {*} a
|
2821 | * @param {*} b
|
2822 | * @return {*}
|
2823 | * @see R.minBy, R.max
|
2824 | * @example
|
2825 | *
|
2826 | * R.min(789, 123); //=> 123
|
2827 | * R.min('a', 'b'); //=> 'a'
|
2828 | */
|
2829 | var min = _curry2(function min(a, b) {
|
2830 | return b < a ? b : a;
|
2831 | });
|
2832 |
|
2833 | /**
|
2834 | * Takes a function and two values, and returns whichever value produces the
|
2835 | * smaller result when passed to the provided function.
|
2836 | *
|
2837 | * @func
|
2838 | * @memberOf R
|
2839 | * @since v0.8.0
|
2840 | * @category Relation
|
2841 | * @sig Ord b => (a -> b) -> a -> a -> a
|
2842 | * @param {Function} f
|
2843 | * @param {*} a
|
2844 | * @param {*} b
|
2845 | * @return {*}
|
2846 | * @see R.min, R.maxBy
|
2847 | * @example
|
2848 | *
|
2849 | * // square :: Number -> Number
|
2850 | * var square = n => n * n;
|
2851 | *
|
2852 | * R.minBy(square, -3, 2); //=> 2
|
2853 | *
|
2854 | * R.reduce(R.minBy(square), Infinity, [3, -5, 4, 1, -2]); //=> 1
|
2855 | * R.reduce(R.minBy(square), Infinity, []); //=> Infinity
|
2856 | */
|
2857 | var minBy = _curry3(function minBy(f, a, b) {
|
2858 | return f(b) < f(a) ? b : a;
|
2859 | });
|
2860 |
|
2861 | /**
|
2862 | * Divides the first parameter by the second and returns the remainder. Note
|
2863 | * that this function preserves the JavaScript-style behavior for modulo. For
|
2864 | * mathematical modulo see `mathMod`.
|
2865 | *
|
2866 | * @func
|
2867 | * @memberOf R
|
2868 | * @since v0.1.1
|
2869 | * @category Math
|
2870 | * @sig Number -> Number -> Number
|
2871 | * @param {Number} a The value to the divide.
|
2872 | * @param {Number} b The pseudo-modulus
|
2873 | * @return {Number} The result of `b % a`.
|
2874 | * @see R.mathMod
|
2875 | * @example
|
2876 | *
|
2877 | * R.modulo(17, 3); //=> 2
|
2878 | * // JS behavior:
|
2879 | * R.modulo(-17, 3); //=> -2
|
2880 | * R.modulo(17, -3); //=> 2
|
2881 | *
|
2882 | * var isOdd = R.modulo(R.__, 2);
|
2883 | * isOdd(42); //=> 0
|
2884 | * isOdd(21); //=> 1
|
2885 | */
|
2886 | var modulo = _curry2(function modulo(a, b) {
|
2887 | return a % b;
|
2888 | });
|
2889 |
|
2890 | /**
|
2891 | * Multiplies two numbers. Equivalent to `a * b` but curried.
|
2892 | *
|
2893 | * @func
|
2894 | * @memberOf R
|
2895 | * @since v0.1.0
|
2896 | * @category Math
|
2897 | * @sig Number -> Number -> Number
|
2898 | * @param {Number} a The first value.
|
2899 | * @param {Number} b The second value.
|
2900 | * @return {Number} The result of `a * b`.
|
2901 | * @see R.divide
|
2902 | * @example
|
2903 | *
|
2904 | * var double = R.multiply(2);
|
2905 | * var triple = R.multiply(3);
|
2906 | * double(3); //=> 6
|
2907 | * triple(4); //=> 12
|
2908 | * R.multiply(2, 5); //=> 10
|
2909 | */
|
2910 | var multiply = _curry2(function multiply(a, b) {
|
2911 | return a * b;
|
2912 | });
|
2913 |
|
2914 | /**
|
2915 | * Wraps a function of any arity (including nullary) in a function that accepts
|
2916 | * exactly `n` parameters. Any extraneous parameters will not be passed to the
|
2917 | * supplied function.
|
2918 | *
|
2919 | * @func
|
2920 | * @memberOf R
|
2921 | * @since v0.1.0
|
2922 | * @category Function
|
2923 | * @sig Number -> (* -> a) -> (* -> a)
|
2924 | * @param {Number} n The desired arity of the new function.
|
2925 | * @param {Function} fn The function to wrap.
|
2926 | * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
|
2927 | * arity `n`.
|
2928 | * @example
|
2929 | *
|
2930 | * var takesTwoArgs = (a, b) => [a, b];
|
2931 | *
|
2932 | * takesTwoArgs.length; //=> 2
|
2933 | * takesTwoArgs(1, 2); //=> [1, 2]
|
2934 | *
|
2935 | * var takesOneArg = R.nAry(1, takesTwoArgs);
|
2936 | * takesOneArg.length; //=> 1
|
2937 | * // Only `n` arguments are passed to the wrapped function
|
2938 | * takesOneArg(1, 2); //=> [1, undefined]
|
2939 | * @symb R.nAry(0, f)(a, b) = f()
|
2940 | * @symb R.nAry(1, f)(a, b) = f(a)
|
2941 | * @symb R.nAry(2, f)(a, b) = f(a, b)
|
2942 | */
|
2943 | var nAry = _curry2(function nAry(n, fn) {
|
2944 | switch (n) {
|
2945 | case 0:
|
2946 | return function () {
|
2947 | return fn.call(this);
|
2948 | };
|
2949 | case 1:
|
2950 | return function (a0) {
|
2951 | return fn.call(this, a0);
|
2952 | };
|
2953 | case 2:
|
2954 | return function (a0, a1) {
|
2955 | return fn.call(this, a0, a1);
|
2956 | };
|
2957 | case 3:
|
2958 | return function (a0, a1, a2) {
|
2959 | return fn.call(this, a0, a1, a2);
|
2960 | };
|
2961 | case 4:
|
2962 | return function (a0, a1, a2, a3) {
|
2963 | return fn.call(this, a0, a1, a2, a3);
|
2964 | };
|
2965 | case 5:
|
2966 | return function (a0, a1, a2, a3, a4) {
|
2967 | return fn.call(this, a0, a1, a2, a3, a4);
|
2968 | };
|
2969 | case 6:
|
2970 | return function (a0, a1, a2, a3, a4, a5) {
|
2971 | return fn.call(this, a0, a1, a2, a3, a4, a5);
|
2972 | };
|
2973 | case 7:
|
2974 | return function (a0, a1, a2, a3, a4, a5, a6) {
|
2975 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6);
|
2976 | };
|
2977 | case 8:
|
2978 | return function (a0, a1, a2, a3, a4, a5, a6, a7) {
|
2979 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);
|
2980 | };
|
2981 | case 9:
|
2982 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
|
2983 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);
|
2984 | };
|
2985 | case 10:
|
2986 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
|
2987 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
|
2988 | };
|
2989 | default:
|
2990 | throw new Error('First argument to nAry must be a non-negative integer no greater than ten');
|
2991 | }
|
2992 | });
|
2993 |
|
2994 | /**
|
2995 | * Negates its argument.
|
2996 | *
|
2997 | * @func
|
2998 | * @memberOf R
|
2999 | * @since v0.9.0
|
3000 | * @category Math
|
3001 | * @sig Number -> Number
|
3002 | * @param {Number} n
|
3003 | * @return {Number}
|
3004 | * @example
|
3005 | *
|
3006 | * R.negate(42); //=> -42
|
3007 | */
|
3008 | var negate = _curry1(function negate(n) {
|
3009 | return -n;
|
3010 | });
|
3011 |
|
3012 | /**
|
3013 | * Returns `true` if no elements of the list match the predicate, `false`
|
3014 | * otherwise.
|
3015 | *
|
3016 | * Dispatches to the `any` method of the second argument, if present.
|
3017 | *
|
3018 | * @func
|
3019 | * @memberOf R
|
3020 | * @since v0.12.0
|
3021 | * @category List
|
3022 | * @sig (a -> Boolean) -> [a] -> Boolean
|
3023 | * @param {Function} fn The predicate function.
|
3024 | * @param {Array} list The array to consider.
|
3025 | * @return {Boolean} `true` if the predicate is not satisfied by every element, `false` otherwise.
|
3026 | * @see R.all, R.any
|
3027 | * @example
|
3028 | *
|
3029 | * var isEven = n => n % 2 === 0;
|
3030 | *
|
3031 | * R.none(isEven, [1, 3, 5, 7, 9, 11]); //=> true
|
3032 | * R.none(isEven, [1, 3, 5, 7, 8, 11]); //=> false
|
3033 | */
|
3034 | var none = _curry2(_complement(_dispatchable(['any'], _xany, any)));
|
3035 |
|
3036 | /**
|
3037 | * A function that returns the `!` of its argument. It will return `true` when
|
3038 | * passed false-y value, and `false` when passed a truth-y one.
|
3039 | *
|
3040 | * @func
|
3041 | * @memberOf R
|
3042 | * @since v0.1.0
|
3043 | * @category Logic
|
3044 | * @sig * -> Boolean
|
3045 | * @param {*} a any value
|
3046 | * @return {Boolean} the logical inverse of passed argument.
|
3047 | * @see R.complement
|
3048 | * @example
|
3049 | *
|
3050 | * R.not(true); //=> false
|
3051 | * R.not(false); //=> true
|
3052 | * R.not(0); //=> true
|
3053 | * R.not(1); //=> false
|
3054 | */
|
3055 | var not = _curry1(function not(a) {
|
3056 | return !a;
|
3057 | });
|
3058 |
|
3059 | /**
|
3060 | * Returns the nth element of the given list or string. If n is negative the
|
3061 | * element at index length + n is returned.
|
3062 | *
|
3063 | * @func
|
3064 | * @memberOf R
|
3065 | * @since v0.1.0
|
3066 | * @category List
|
3067 | * @sig Number -> [a] -> a | Undefined
|
3068 | * @sig Number -> String -> String
|
3069 | * @param {Number} offset
|
3070 | * @param {*} list
|
3071 | * @return {*}
|
3072 | * @example
|
3073 | *
|
3074 | * var list = ['foo', 'bar', 'baz', 'quux'];
|
3075 | * R.nth(1, list); //=> 'bar'
|
3076 | * R.nth(-1, list); //=> 'quux'
|
3077 | * R.nth(-99, list); //=> undefined
|
3078 | *
|
3079 | * R.nth(2, 'abc'); //=> 'c'
|
3080 | * R.nth(3, 'abc'); //=> ''
|
3081 | * @symb R.nth(-1, [a, b, c]) = c
|
3082 | * @symb R.nth(0, [a, b, c]) = a
|
3083 | * @symb R.nth(1, [a, b, c]) = b
|
3084 | */
|
3085 | var nth = _curry2(function nth(offset, list) {
|
3086 | var idx = offset < 0 ? list.length + offset : offset;
|
3087 | return _isString(list) ? list.charAt(idx) : list[idx];
|
3088 | });
|
3089 |
|
3090 | /**
|
3091 | * Returns a function which returns its nth argument.
|
3092 | *
|
3093 | * @func
|
3094 | * @memberOf R
|
3095 | * @since v0.9.0
|
3096 | * @category Function
|
3097 | * @sig Number -> *... -> *
|
3098 | * @param {Number} n
|
3099 | * @return {Function}
|
3100 | * @example
|
3101 | *
|
3102 | * R.nthArg(1)('a', 'b', 'c'); //=> 'b'
|
3103 | * R.nthArg(-1)('a', 'b', 'c'); //=> 'c'
|
3104 | * @symb R.nthArg(-1)(a, b, c) = c
|
3105 | * @symb R.nthArg(0)(a, b, c) = a
|
3106 | * @symb R.nthArg(1)(a, b, c) = b
|
3107 | */
|
3108 | var nthArg = _curry1(function nthArg(n) {
|
3109 | var arity = n < 0 ? 1 : n + 1;
|
3110 | return curryN(arity, function () {
|
3111 | return nth(n, arguments);
|
3112 | });
|
3113 | });
|
3114 |
|
3115 | /**
|
3116 | * Creates an object containing a single key:value pair.
|
3117 | *
|
3118 | * @func
|
3119 | * @memberOf R
|
3120 | * @since v0.18.0
|
3121 | * @category Object
|
3122 | * @sig String -> a -> {String:a}
|
3123 | * @param {String} key
|
3124 | * @param {*} val
|
3125 | * @return {Object}
|
3126 | * @see R.pair
|
3127 | * @example
|
3128 | *
|
3129 | * var matchPhrases = R.compose(
|
3130 | * R.objOf('must'),
|
3131 | * R.map(R.objOf('match_phrase'))
|
3132 | * );
|
3133 | * matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]}
|
3134 | */
|
3135 | var objOf = _curry2(function objOf(key, val) {
|
3136 | var obj = {};
|
3137 | obj[key] = val;
|
3138 | return obj;
|
3139 | });
|
3140 |
|
3141 | /**
|
3142 | * Returns a singleton array containing the value provided.
|
3143 | *
|
3144 | * Note this `of` is different from the ES6 `of`; See
|
3145 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
|
3146 | *
|
3147 | * @func
|
3148 | * @memberOf R
|
3149 | * @since v0.3.0
|
3150 | * @category Function
|
3151 | * @sig a -> [a]
|
3152 | * @param {*} x any value
|
3153 | * @return {Array} An array wrapping `x`.
|
3154 | * @example
|
3155 | *
|
3156 | * R.of(null); //=> [null]
|
3157 | * R.of([42]); //=> [[42]]
|
3158 | */
|
3159 | var of = _curry1(_of);
|
3160 |
|
3161 | /**
|
3162 | * Accepts a function `fn` and returns a function that guards invocation of
|
3163 | * `fn` such that `fn` can only ever be called once, no matter how many times
|
3164 | * the returned function is invoked. The first value calculated is returned in
|
3165 | * subsequent invocations.
|
3166 | *
|
3167 | * @func
|
3168 | * @memberOf R
|
3169 | * @since v0.1.0
|
3170 | * @category Function
|
3171 | * @sig (a... -> b) -> (a... -> b)
|
3172 | * @param {Function} fn The function to wrap in a call-only-once wrapper.
|
3173 | * @return {Function} The wrapped function.
|
3174 | * @example
|
3175 | *
|
3176 | * var addOneOnce = R.once(x => x + 1);
|
3177 | * addOneOnce(10); //=> 11
|
3178 | * addOneOnce(addOneOnce(50)); //=> 11
|
3179 | */
|
3180 | var once = _curry1(function once(fn) {
|
3181 | var called = false;
|
3182 | var result;
|
3183 | return _arity(fn.length, function () {
|
3184 | if (called) {
|
3185 | return result;
|
3186 | }
|
3187 | called = true;
|
3188 | result = fn.apply(this, arguments);
|
3189 | return result;
|
3190 | });
|
3191 | });
|
3192 |
|
3193 | /**
|
3194 | * Returns `true` if one or both of its arguments are `true`. Returns `false`
|
3195 | * if both arguments are `false`.
|
3196 | *
|
3197 | * @func
|
3198 | * @memberOf R
|
3199 | * @since v0.1.0
|
3200 | * @category Logic
|
3201 | * @sig a -> b -> a | b
|
3202 | * @param {Any} a
|
3203 | * @param {Any} b
|
3204 | * @return {Any} the first argument if truthy, otherwise the second argument.
|
3205 | * @see R.either
|
3206 | * @example
|
3207 | *
|
3208 | * R.or(true, true); //=> true
|
3209 | * R.or(true, false); //=> true
|
3210 | * R.or(false, true); //=> true
|
3211 | * R.or(false, false); //=> false
|
3212 | */
|
3213 | var or = _curry2(function or(a, b) {
|
3214 | return a || b;
|
3215 | });
|
3216 |
|
3217 | /**
|
3218 | * Returns the result of "setting" the portion of the given data structure
|
3219 | * focused by the given lens to the result of applying the given function to
|
3220 | * the focused value.
|
3221 | *
|
3222 | * @func
|
3223 | * @memberOf R
|
3224 | * @since v0.16.0
|
3225 | * @category Object
|
3226 | * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
|
3227 | * @sig Lens s a -> (a -> a) -> s -> s
|
3228 | * @param {Lens} lens
|
3229 | * @param {*} v
|
3230 | * @param {*} x
|
3231 | * @return {*}
|
3232 | * @see R.prop, R.lensIndex, R.lensProp
|
3233 | * @example
|
3234 | *
|
3235 | * var headLens = R.lensIndex(0);
|
3236 | *
|
3237 | * R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']); //=> ['FOO', 'bar', 'baz']
|
3238 | */
|
3239 | // `Identity` is a functor that holds a single value, where `map` simply
|
3240 | // transforms the held value with the provided function.
|
3241 | // The value returned by the getter function is first transformed with `f`,
|
3242 | // then set as the value of an `Identity`. This is then mapped over with the
|
3243 | // setter function of the lens.
|
3244 | var over = function () {
|
3245 | // `Identity` is a functor that holds a single value, where `map` simply
|
3246 | // transforms the held value with the provided function.
|
3247 | var Identity = function (x) {
|
3248 | return {
|
3249 | value: x,
|
3250 | map: function (f) {
|
3251 | return Identity(f(x));
|
3252 | }
|
3253 | };
|
3254 | };
|
3255 | return _curry3(function over(lens, f, x) {
|
3256 | // The value returned by the getter function is first transformed with `f`,
|
3257 | // then set as the value of an `Identity`. This is then mapped over with the
|
3258 | // setter function of the lens.
|
3259 | return lens(function (y) {
|
3260 | return Identity(f(y));
|
3261 | })(x).value;
|
3262 | });
|
3263 | }();
|
3264 |
|
3265 | /**
|
3266 | * Takes two arguments, `fst` and `snd`, and returns `[fst, snd]`.
|
3267 | *
|
3268 | * @func
|
3269 | * @memberOf R
|
3270 | * @since v0.18.0
|
3271 | * @category List
|
3272 | * @sig a -> b -> (a,b)
|
3273 | * @param {*} fst
|
3274 | * @param {*} snd
|
3275 | * @return {Array}
|
3276 | * @see R.objOf, R.of
|
3277 | * @example
|
3278 | *
|
3279 | * R.pair('foo', 'bar'); //=> ['foo', 'bar']
|
3280 | */
|
3281 | var pair = _curry2(function pair(fst, snd) {
|
3282 | return [
|
3283 | fst,
|
3284 | snd
|
3285 | ];
|
3286 | });
|
3287 |
|
3288 | /**
|
3289 | * Retrieve the value at a given path.
|
3290 | *
|
3291 | * @func
|
3292 | * @memberOf R
|
3293 | * @since v0.2.0
|
3294 | * @category Object
|
3295 | * @typedefn Idx = String | Int
|
3296 | * @sig [Idx] -> {a} -> a | Undefined
|
3297 | * @param {Array} path The path to use.
|
3298 | * @param {Object} obj The object to retrieve the nested property from.
|
3299 | * @return {*} The data at `path`.
|
3300 | * @see R.prop
|
3301 | * @example
|
3302 | *
|
3303 | * R.path(['a', 'b'], {a: {b: 2}}); //=> 2
|
3304 | * R.path(['a', 'b'], {c: {b: 2}}); //=> undefined
|
3305 | */
|
3306 | var path = _curry2(function path(paths, obj) {
|
3307 | var val = obj;
|
3308 | var idx = 0;
|
3309 | while (idx < paths.length) {
|
3310 | if (val == null) {
|
3311 | return;
|
3312 | }
|
3313 | val = val[paths[idx]];
|
3314 | idx += 1;
|
3315 | }
|
3316 | return val;
|
3317 | });
|
3318 |
|
3319 | /**
|
3320 | * If the given, non-null object has a value at the given path, returns the
|
3321 | * value at that path. Otherwise returns the provided default value.
|
3322 | *
|
3323 | * @func
|
3324 | * @memberOf R
|
3325 | * @since v0.18.0
|
3326 | * @category Object
|
3327 | * @typedefn Idx = String | Int
|
3328 | * @sig a -> [Idx] -> {a} -> a
|
3329 | * @param {*} d The default value.
|
3330 | * @param {Array} p The path to use.
|
3331 | * @param {Object} obj The object to retrieve the nested property from.
|
3332 | * @return {*} The data at `path` of the supplied object or the default value.
|
3333 | * @example
|
3334 | *
|
3335 | * R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2
|
3336 | * R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A"
|
3337 | */
|
3338 | var pathOr = _curry3(function pathOr(d, p, obj) {
|
3339 | return defaultTo(d, path(p, obj));
|
3340 | });
|
3341 |
|
3342 | /**
|
3343 | * Returns `true` if the specified object property at given path satisfies the
|
3344 | * given predicate; `false` otherwise.
|
3345 | *
|
3346 | * @func
|
3347 | * @memberOf R
|
3348 | * @since v0.19.0
|
3349 | * @category Logic
|
3350 | * @typedefn Idx = String | Int
|
3351 | * @sig (a -> Boolean) -> [Idx] -> {a} -> Boolean
|
3352 | * @param {Function} pred
|
3353 | * @param {Array} propPath
|
3354 | * @param {*} obj
|
3355 | * @return {Boolean}
|
3356 | * @see R.propSatisfies, R.path
|
3357 | * @example
|
3358 | *
|
3359 | * R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: 2}}); //=> true
|
3360 | */
|
3361 | var pathSatisfies = _curry3(function pathSatisfies(pred, propPath, obj) {
|
3362 | return propPath.length > 0 && pred(path(propPath, obj));
|
3363 | });
|
3364 |
|
3365 | /**
|
3366 | * Returns a partial copy of an object containing only the keys specified. If
|
3367 | * the key does not exist, the property is ignored.
|
3368 | *
|
3369 | * @func
|
3370 | * @memberOf R
|
3371 | * @since v0.1.0
|
3372 | * @category Object
|
3373 | * @sig [k] -> {k: v} -> {k: v}
|
3374 | * @param {Array} names an array of String property names to copy onto a new object
|
3375 | * @param {Object} obj The object to copy from
|
3376 | * @return {Object} A new object with only properties from `names` on it.
|
3377 | * @see R.omit, R.props
|
3378 | * @example
|
3379 | *
|
3380 | * R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
|
3381 | * R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}
|
3382 | */
|
3383 | var pick = _curry2(function pick(names, obj) {
|
3384 | var result = {};
|
3385 | var idx = 0;
|
3386 | while (idx < names.length) {
|
3387 | if (names[idx] in obj) {
|
3388 | result[names[idx]] = obj[names[idx]];
|
3389 | }
|
3390 | idx += 1;
|
3391 | }
|
3392 | return result;
|
3393 | });
|
3394 |
|
3395 | /**
|
3396 | * Similar to `pick` except that this one includes a `key: undefined` pair for
|
3397 | * properties that don't exist.
|
3398 | *
|
3399 | * @func
|
3400 | * @memberOf R
|
3401 | * @since v0.1.0
|
3402 | * @category Object
|
3403 | * @sig [k] -> {k: v} -> {k: v}
|
3404 | * @param {Array} names an array of String property names to copy onto a new object
|
3405 | * @param {Object} obj The object to copy from
|
3406 | * @return {Object} A new object with only properties from `names` on it.
|
3407 | * @see R.pick
|
3408 | * @example
|
3409 | *
|
3410 | * R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
|
3411 | * R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined}
|
3412 | */
|
3413 | var pickAll = _curry2(function pickAll(names, obj) {
|
3414 | var result = {};
|
3415 | var idx = 0;
|
3416 | var len = names.length;
|
3417 | while (idx < len) {
|
3418 | var name = names[idx];
|
3419 | result[name] = obj[name];
|
3420 | idx += 1;
|
3421 | }
|
3422 | return result;
|
3423 | });
|
3424 |
|
3425 | /**
|
3426 | * Returns a partial copy of an object containing only the keys that satisfy
|
3427 | * the supplied predicate.
|
3428 | *
|
3429 | * @func
|
3430 | * @memberOf R
|
3431 | * @since v0.8.0
|
3432 | * @category Object
|
3433 | * @sig (v, k -> Boolean) -> {k: v} -> {k: v}
|
3434 | * @param {Function} pred A predicate to determine whether or not a key
|
3435 | * should be included on the output object.
|
3436 | * @param {Object} obj The object to copy from
|
3437 | * @return {Object} A new object with only properties that satisfy `pred`
|
3438 | * on it.
|
3439 | * @see R.pick, R.filter
|
3440 | * @example
|
3441 | *
|
3442 | * var isUpperCase = (val, key) => key.toUpperCase() === key;
|
3443 | * R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4}
|
3444 | */
|
3445 | var pickBy = _curry2(function pickBy(test, obj) {
|
3446 | var result = {};
|
3447 | for (var prop in obj) {
|
3448 | if (test(obj[prop], prop, obj)) {
|
3449 | result[prop] = obj[prop];
|
3450 | }
|
3451 | }
|
3452 | return result;
|
3453 | });
|
3454 |
|
3455 | /**
|
3456 | * Returns a new list with the given element at the front, followed by the
|
3457 | * contents of the list.
|
3458 | *
|
3459 | * @func
|
3460 | * @memberOf R
|
3461 | * @since v0.1.0
|
3462 | * @category List
|
3463 | * @sig a -> [a] -> [a]
|
3464 | * @param {*} el The item to add to the head of the output list.
|
3465 | * @param {Array} list The array to add to the tail of the output list.
|
3466 | * @return {Array} A new array.
|
3467 | * @see R.append
|
3468 | * @example
|
3469 | *
|
3470 | * R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum']
|
3471 | */
|
3472 | var prepend = _curry2(function prepend(el, list) {
|
3473 | return _concat([el], list);
|
3474 | });
|
3475 |
|
3476 | /**
|
3477 | * Returns a function that when supplied an object returns the indicated
|
3478 | * property of that object, if it exists.
|
3479 | *
|
3480 | * @func
|
3481 | * @memberOf R
|
3482 | * @since v0.1.0
|
3483 | * @category Object
|
3484 | * @sig s -> {s: a} -> a | Undefined
|
3485 | * @param {String} p The property name
|
3486 | * @param {Object} obj The object to query
|
3487 | * @return {*} The value at `obj.p`.
|
3488 | * @see R.path
|
3489 | * @example
|
3490 | *
|
3491 | * R.prop('x', {x: 100}); //=> 100
|
3492 | * R.prop('x', {}); //=> undefined
|
3493 | */
|
3494 | var prop = _curry2(function prop(p, obj) {
|
3495 | return obj[p];
|
3496 | });
|
3497 |
|
3498 | /**
|
3499 | * Returns `true` if the specified object property is of the given type;
|
3500 | * `false` otherwise.
|
3501 | *
|
3502 | * @func
|
3503 | * @memberOf R
|
3504 | * @since v0.16.0
|
3505 | * @category Type
|
3506 | * @sig Type -> String -> Object -> Boolean
|
3507 | * @param {Function} type
|
3508 | * @param {String} name
|
3509 | * @param {*} obj
|
3510 | * @return {Boolean}
|
3511 | * @see R.is, R.propSatisfies
|
3512 | * @example
|
3513 | *
|
3514 | * R.propIs(Number, 'x', {x: 1, y: 2}); //=> true
|
3515 | * R.propIs(Number, 'x', {x: 'foo'}); //=> false
|
3516 | * R.propIs(Number, 'x', {}); //=> false
|
3517 | */
|
3518 | var propIs = _curry3(function propIs(type, name, obj) {
|
3519 | return is(type, obj[name]);
|
3520 | });
|
3521 |
|
3522 | /**
|
3523 | * If the given, non-null object has an own property with the specified name,
|
3524 | * returns the value of that property. Otherwise returns the provided default
|
3525 | * value.
|
3526 | *
|
3527 | * @func
|
3528 | * @memberOf R
|
3529 | * @since v0.6.0
|
3530 | * @category Object
|
3531 | * @sig a -> String -> Object -> a
|
3532 | * @param {*} val The default value.
|
3533 | * @param {String} p The name of the property to return.
|
3534 | * @param {Object} obj The object to query.
|
3535 | * @return {*} The value of given property of the supplied object or the default value.
|
3536 | * @example
|
3537 | *
|
3538 | * var alice = {
|
3539 | * name: 'ALICE',
|
3540 | * age: 101
|
3541 | * };
|
3542 | * var favorite = R.prop('favoriteLibrary');
|
3543 | * var favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary');
|
3544 | *
|
3545 | * favorite(alice); //=> undefined
|
3546 | * favoriteWithDefault(alice); //=> 'Ramda'
|
3547 | */
|
3548 | var propOr = _curry3(function propOr(val, p, obj) {
|
3549 | return obj != null && _has(p, obj) ? obj[p] : val;
|
3550 | });
|
3551 |
|
3552 | /**
|
3553 | * Returns `true` if the specified object property satisfies the given
|
3554 | * predicate; `false` otherwise.
|
3555 | *
|
3556 | * @func
|
3557 | * @memberOf R
|
3558 | * @since v0.16.0
|
3559 | * @category Logic
|
3560 | * @sig (a -> Boolean) -> String -> {String: a} -> Boolean
|
3561 | * @param {Function} pred
|
3562 | * @param {String} name
|
3563 | * @param {*} obj
|
3564 | * @return {Boolean}
|
3565 | * @see R.propEq, R.propIs
|
3566 | * @example
|
3567 | *
|
3568 | * R.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true
|
3569 | */
|
3570 | var propSatisfies = _curry3(function propSatisfies(pred, name, obj) {
|
3571 | return pred(obj[name]);
|
3572 | });
|
3573 |
|
3574 | /**
|
3575 | * Acts as multiple `prop`: array of keys in, array of values out. Preserves
|
3576 | * order.
|
3577 | *
|
3578 | * @func
|
3579 | * @memberOf R
|
3580 | * @since v0.1.0
|
3581 | * @category Object
|
3582 | * @sig [k] -> {k: v} -> [v]
|
3583 | * @param {Array} ps The property names to fetch
|
3584 | * @param {Object} obj The object to query
|
3585 | * @return {Array} The corresponding values or partially applied function.
|
3586 | * @example
|
3587 | *
|
3588 | * R.props(['x', 'y'], {x: 1, y: 2}); //=> [1, 2]
|
3589 | * R.props(['c', 'a', 'b'], {b: 2, a: 1}); //=> [undefined, 1, 2]
|
3590 | *
|
3591 | * var fullName = R.compose(R.join(' '), R.props(['first', 'last']));
|
3592 | * fullName({last: 'Bullet-Tooth', age: 33, first: 'Tony'}); //=> 'Tony Bullet-Tooth'
|
3593 | */
|
3594 | var props = _curry2(function props(ps, obj) {
|
3595 | var len = ps.length;
|
3596 | var out = [];
|
3597 | var idx = 0;
|
3598 | while (idx < len) {
|
3599 | out[idx] = obj[ps[idx]];
|
3600 | idx += 1;
|
3601 | }
|
3602 | return out;
|
3603 | });
|
3604 |
|
3605 | /**
|
3606 | * Returns a list of numbers from `from` (inclusive) to `to` (exclusive).
|
3607 | *
|
3608 | * @func
|
3609 | * @memberOf R
|
3610 | * @since v0.1.0
|
3611 | * @category List
|
3612 | * @sig Number -> Number -> [Number]
|
3613 | * @param {Number} from The first number in the list.
|
3614 | * @param {Number} to One more than the last number in the list.
|
3615 | * @return {Array} The list of numbers in tthe set `[a, b)`.
|
3616 | * @example
|
3617 | *
|
3618 | * R.range(1, 5); //=> [1, 2, 3, 4]
|
3619 | * R.range(50, 53); //=> [50, 51, 52]
|
3620 | */
|
3621 | var range = _curry2(function range(from, to) {
|
3622 | if (!(_isNumber(from) && _isNumber(to))) {
|
3623 | throw new TypeError('Both arguments to range must be numbers');
|
3624 | }
|
3625 | var result = [];
|
3626 | var n = from;
|
3627 | while (n < to) {
|
3628 | result.push(n);
|
3629 | n += 1;
|
3630 | }
|
3631 | return result;
|
3632 | });
|
3633 |
|
3634 | /**
|
3635 | * Returns a single item by iterating through the list, successively calling
|
3636 | * the iterator function and passing it an accumulator value and the current
|
3637 | * value from the array, and then passing the result to the next call.
|
3638 | *
|
3639 | * Similar to `reduce`, except moves through the input list from the right to
|
3640 | * the left.
|
3641 | *
|
3642 | * The iterator function receives two values: *(value, acc)*, while the arguments'
|
3643 | * order of `reduce`'s iterator function is *(acc, value)*.
|
3644 | *
|
3645 | * Note: `R.reduceRight` does not skip deleted or unassigned indices (sparse
|
3646 | * arrays), unlike the native `Array.prototype.reduce` method. For more details
|
3647 | * on this behavior, see:
|
3648 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
|
3649 | *
|
3650 | * @func
|
3651 | * @memberOf R
|
3652 | * @since v0.1.0
|
3653 | * @category List
|
3654 | * @sig (a, b -> b) -> b -> [a] -> b
|
3655 | * @param {Function} fn The iterator function. Receives two values, the current element from the array
|
3656 | * and the accumulator.
|
3657 | * @param {*} acc The accumulator value.
|
3658 | * @param {Array} list The list to iterate over.
|
3659 | * @return {*} The final, accumulated value.
|
3660 | * @see R.reduce, R.addIndex
|
3661 | * @example
|
3662 | *
|
3663 | * R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2
|
3664 | * - -2
|
3665 | * / \ / \
|
3666 | * 1 - 1 3
|
3667 | * / \ / \
|
3668 | * 2 - ==> 2 -1
|
3669 | * / \ / \
|
3670 | * 3 - 3 4
|
3671 | * / \ / \
|
3672 | * 4 0 4 0
|
3673 | *
|
3674 | * @symb R.reduceRight(f, a, [b, c, d]) = f(b, f(c, f(d, a)))
|
3675 | */
|
3676 | var reduceRight = _curry3(function reduceRight(fn, acc, list) {
|
3677 | var idx = list.length - 1;
|
3678 | while (idx >= 0) {
|
3679 | acc = fn(list[idx], acc);
|
3680 | idx -= 1;
|
3681 | }
|
3682 | return acc;
|
3683 | });
|
3684 |
|
3685 | /**
|
3686 | * Returns a value wrapped to indicate that it is the final value of the reduce
|
3687 | * and transduce functions. The returned value should be considered a black
|
3688 | * box: the internal structure is not guaranteed to be stable.
|
3689 | *
|
3690 | * Note: this optimization is unavailable to functions not explicitly listed
|
3691 | * above. For instance, it is not currently supported by reduceRight.
|
3692 | *
|
3693 | * @func
|
3694 | * @memberOf R
|
3695 | * @since v0.15.0
|
3696 | * @category List
|
3697 | * @sig a -> *
|
3698 | * @param {*} x The final value of the reduce.
|
3699 | * @return {*} The wrapped value.
|
3700 | * @see R.reduce, R.transduce
|
3701 | * @example
|
3702 | *
|
3703 | * R.reduce(
|
3704 | * R.pipe(R.add, R.when(R.gte(R.__, 10), R.reduced)),
|
3705 | * 0,
|
3706 | * [1, 2, 3, 4, 5]) // 10
|
3707 | */
|
3708 | var reduced = _curry1(_reduced);
|
3709 |
|
3710 | /**
|
3711 | * Removes the sub-list of `list` starting at index `start` and containing
|
3712 | * `count` elements. _Note that this is not destructive_: it returns a copy of
|
3713 | * the list with the changes.
|
3714 | * <small>No lists have been harmed in the application of this function.</small>
|
3715 | *
|
3716 | * @func
|
3717 | * @memberOf R
|
3718 | * @since v0.2.2
|
3719 | * @category List
|
3720 | * @sig Number -> Number -> [a] -> [a]
|
3721 | * @param {Number} start The position to start removing elements
|
3722 | * @param {Number} count The number of elements to remove
|
3723 | * @param {Array} list The list to remove from
|
3724 | * @return {Array} A new Array with `count` elements from `start` removed.
|
3725 | * @example
|
3726 | *
|
3727 | * R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]
|
3728 | */
|
3729 | var remove = _curry3(function remove(start, count, list) {
|
3730 | var result = Array.prototype.slice.call(list, 0);
|
3731 | result.splice(start, count);
|
3732 | return result;
|
3733 | });
|
3734 |
|
3735 | /**
|
3736 | * Replace a substring or regex match in a string with a replacement.
|
3737 | *
|
3738 | * @func
|
3739 | * @memberOf R
|
3740 | * @since v0.7.0
|
3741 | * @category String
|
3742 | * @sig RegExp|String -> String -> String -> String
|
3743 | * @param {RegExp|String} pattern A regular expression or a substring to match.
|
3744 | * @param {String} replacement The string to replace the matches with.
|
3745 | * @param {String} str The String to do the search and replacement in.
|
3746 | * @return {String} The result.
|
3747 | * @example
|
3748 | *
|
3749 | * R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
|
3750 | * R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
|
3751 | *
|
3752 | * // Use the "g" (global) flag to replace all occurrences:
|
3753 | * R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar'
|
3754 | */
|
3755 | var replace = _curry3(function replace(regex, replacement, str) {
|
3756 | return str.replace(regex, replacement);
|
3757 | });
|
3758 |
|
3759 | /**
|
3760 | * Returns a new list or string with the elements or characters in reverse
|
3761 | * order.
|
3762 | *
|
3763 | * @func
|
3764 | * @memberOf R
|
3765 | * @since v0.1.0
|
3766 | * @category List
|
3767 | * @sig [a] -> [a]
|
3768 | * @sig String -> String
|
3769 | * @param {Array|String} list
|
3770 | * @return {Array|String}
|
3771 | * @example
|
3772 | *
|
3773 | * R.reverse([1, 2, 3]); //=> [3, 2, 1]
|
3774 | * R.reverse([1, 2]); //=> [2, 1]
|
3775 | * R.reverse([1]); //=> [1]
|
3776 | * R.reverse([]); //=> []
|
3777 | *
|
3778 | * R.reverse('abc'); //=> 'cba'
|
3779 | * R.reverse('ab'); //=> 'ba'
|
3780 | * R.reverse('a'); //=> 'a'
|
3781 | * R.reverse(''); //=> ''
|
3782 | */
|
3783 | var reverse = _curry1(function reverse(list) {
|
3784 | return _isString(list) ? list.split('').reverse().join('') : Array.prototype.slice.call(list, 0).reverse();
|
3785 | });
|
3786 |
|
3787 | /**
|
3788 | * Scan is similar to reduce, but returns a list of successively reduced values
|
3789 | * from the left
|
3790 | *
|
3791 | * @func
|
3792 | * @memberOf R
|
3793 | * @since v0.10.0
|
3794 | * @category List
|
3795 | * @sig (a,b -> a) -> a -> [b] -> [a]
|
3796 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
3797 | * current element from the array
|
3798 | * @param {*} acc The accumulator value.
|
3799 | * @param {Array} list The list to iterate over.
|
3800 | * @return {Array} A list of all intermediately reduced values.
|
3801 | * @example
|
3802 | *
|
3803 | * var numbers = [1, 2, 3, 4];
|
3804 | * var factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]
|
3805 | * @symb R.scan(f, a, [b, c]) = [a, f(a, b), f(f(a, b), c)]
|
3806 | */
|
3807 | var scan = _curry3(function scan(fn, acc, list) {
|
3808 | var idx = 0;
|
3809 | var len = list.length;
|
3810 | var result = [acc];
|
3811 | while (idx < len) {
|
3812 | acc = fn(acc, list[idx]);
|
3813 | result[idx + 1] = acc;
|
3814 | idx += 1;
|
3815 | }
|
3816 | return result;
|
3817 | });
|
3818 |
|
3819 | /**
|
3820 | * Returns the result of "setting" the portion of the given data structure
|
3821 | * focused by the given lens to the given value.
|
3822 | *
|
3823 | * @func
|
3824 | * @memberOf R
|
3825 | * @since v0.16.0
|
3826 | * @category Object
|
3827 | * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
|
3828 | * @sig Lens s a -> a -> s -> s
|
3829 | * @param {Lens} lens
|
3830 | * @param {*} v
|
3831 | * @param {*} x
|
3832 | * @return {*}
|
3833 | * @see R.prop, R.lensIndex, R.lensProp
|
3834 | * @example
|
3835 | *
|
3836 | * var xLens = R.lensProp('x');
|
3837 | *
|
3838 | * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
|
3839 | * R.set(xLens, 8, {x: 1, y: 2}); //=> {x: 8, y: 2}
|
3840 | */
|
3841 | var set = _curry3(function set(lens, v, x) {
|
3842 | return over(lens, always(v), x);
|
3843 | });
|
3844 |
|
3845 | /**
|
3846 | * Returns the elements of the given list or string (or object with a `slice`
|
3847 | * method) from `fromIndex` (inclusive) to `toIndex` (exclusive).
|
3848 | *
|
3849 | * Dispatches to the `slice` method of the third argument, if present.
|
3850 | *
|
3851 | * @func
|
3852 | * @memberOf R
|
3853 | * @since v0.1.4
|
3854 | * @category List
|
3855 | * @sig Number -> Number -> [a] -> [a]
|
3856 | * @sig Number -> Number -> String -> String
|
3857 | * @param {Number} fromIndex The start index (inclusive).
|
3858 | * @param {Number} toIndex The end index (exclusive).
|
3859 | * @param {*} list
|
3860 | * @return {*}
|
3861 | * @example
|
3862 | *
|
3863 | * R.slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
|
3864 | * R.slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd']
|
3865 | * R.slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c']
|
3866 | * R.slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c']
|
3867 | * R.slice(0, 3, 'ramda'); //=> 'ram'
|
3868 | */
|
3869 | var slice = _curry3(_checkForMethod('slice', function slice(fromIndex, toIndex, list) {
|
3870 | return Array.prototype.slice.call(list, fromIndex, toIndex);
|
3871 | }));
|
3872 |
|
3873 | /**
|
3874 | * Returns a copy of the list, sorted according to the comparator function,
|
3875 | * which should accept two values at a time and return a negative number if the
|
3876 | * first value is smaller, a positive number if it's larger, and zero if they
|
3877 | * are equal. Please note that this is a **copy** of the list. It does not
|
3878 | * modify the original.
|
3879 | *
|
3880 | * @func
|
3881 | * @memberOf R
|
3882 | * @since v0.1.0
|
3883 | * @category List
|
3884 | * @sig (a,a -> Number) -> [a] -> [a]
|
3885 | * @param {Function} comparator A sorting function :: a -> b -> Int
|
3886 | * @param {Array} list The list to sort
|
3887 | * @return {Array} a new array with its elements sorted by the comparator function.
|
3888 | * @example
|
3889 | *
|
3890 | * var diff = function(a, b) { return a - b; };
|
3891 | * R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
|
3892 | */
|
3893 | var sort = _curry2(function sort(comparator, list) {
|
3894 | return Array.prototype.slice.call(list, 0).sort(comparator);
|
3895 | });
|
3896 |
|
3897 | /**
|
3898 | * Sorts the list according to the supplied function.
|
3899 | *
|
3900 | * @func
|
3901 | * @memberOf R
|
3902 | * @since v0.1.0
|
3903 | * @category Relation
|
3904 | * @sig Ord b => (a -> b) -> [a] -> [a]
|
3905 | * @param {Function} fn
|
3906 | * @param {Array} list The list to sort.
|
3907 | * @return {Array} A new list sorted by the keys generated by `fn`.
|
3908 | * @example
|
3909 | *
|
3910 | * var sortByFirstItem = R.sortBy(R.prop(0));
|
3911 | * var sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
|
3912 | * var pairs = [[-1, 1], [-2, 2], [-3, 3]];
|
3913 | * sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
|
3914 | * var alice = {
|
3915 | * name: 'ALICE',
|
3916 | * age: 101
|
3917 | * };
|
3918 | * var bob = {
|
3919 | * name: 'Bob',
|
3920 | * age: -10
|
3921 | * };
|
3922 | * var clara = {
|
3923 | * name: 'clara',
|
3924 | * age: 314.159
|
3925 | * };
|
3926 | * var people = [clara, bob, alice];
|
3927 | * sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
|
3928 | */
|
3929 | var sortBy = _curry2(function sortBy(fn, list) {
|
3930 | return Array.prototype.slice.call(list, 0).sort(function (a, b) {
|
3931 | var aa = fn(a);
|
3932 | var bb = fn(b);
|
3933 | return aa < bb ? -1 : aa > bb ? 1 : 0;
|
3934 | });
|
3935 | });
|
3936 |
|
3937 | /**
|
3938 | * Sorts a list according to a list of comparators.
|
3939 | *
|
3940 | * @func
|
3941 | * @memberOf R
|
3942 | * @since v0.23.0
|
3943 | * @category Relation
|
3944 | * @sig [a -> a -> Number] -> [a] -> [a]
|
3945 | * @param {Array} functions A list of comparator functions.
|
3946 | * @param {Array} list The list to sort.
|
3947 | * @return {Array} A new list sorted according to the comarator functions.
|
3948 | * @example
|
3949 | *
|
3950 | * var alice = {
|
3951 | * name: 'alice',
|
3952 | * age: 40
|
3953 | * };
|
3954 | * var bob = {
|
3955 | * name: 'bob',
|
3956 | * age: 30
|
3957 | * };
|
3958 | * var clara = {
|
3959 | * name: 'clara',
|
3960 | * age: 40
|
3961 | * };
|
3962 | * var people = [clara, bob, alice];
|
3963 | * var ageNameSort = R.sortWith([
|
3964 | * R.descend(R.prop('age')),
|
3965 | * R.ascend(R.prop('name'))
|
3966 | * ]);
|
3967 | * ageNameSort(people); //=> [alice, clara, bob]
|
3968 | */
|
3969 | var sortWith = _curry2(function sortWith(fns, list) {
|
3970 | return Array.prototype.slice.call(list, 0).sort(function (a, b) {
|
3971 | var result = 0;
|
3972 | var i = 0;
|
3973 | while (result === 0 && i < fns.length) {
|
3974 | result = fns[i](a, b);
|
3975 | i += 1;
|
3976 | }
|
3977 | return result;
|
3978 | });
|
3979 | });
|
3980 |
|
3981 | /**
|
3982 | * Splits a given list or string at a given index.
|
3983 | *
|
3984 | * @func
|
3985 | * @memberOf R
|
3986 | * @since v0.19.0
|
3987 | * @category List
|
3988 | * @sig Number -> [a] -> [[a], [a]]
|
3989 | * @sig Number -> String -> [String, String]
|
3990 | * @param {Number} index The index where the array/string is split.
|
3991 | * @param {Array|String} array The array/string to be split.
|
3992 | * @return {Array}
|
3993 | * @example
|
3994 | *
|
3995 | * R.splitAt(1, [1, 2, 3]); //=> [[1], [2, 3]]
|
3996 | * R.splitAt(5, 'hello world'); //=> ['hello', ' world']
|
3997 | * R.splitAt(-1, 'foobar'); //=> ['fooba', 'r']
|
3998 | */
|
3999 | var splitAt = _curry2(function splitAt(index, array) {
|
4000 | return [
|
4001 | slice(0, index, array),
|
4002 | slice(index, length(array), array)
|
4003 | ];
|
4004 | });
|
4005 |
|
4006 | /**
|
4007 | * Splits a collection into slices of the specified length.
|
4008 | *
|
4009 | * @func
|
4010 | * @memberOf R
|
4011 | * @since v0.16.0
|
4012 | * @category List
|
4013 | * @sig Number -> [a] -> [[a]]
|
4014 | * @sig Number -> String -> [String]
|
4015 | * @param {Number} n
|
4016 | * @param {Array} list
|
4017 | * @return {Array}
|
4018 | * @example
|
4019 | *
|
4020 | * R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]); //=> [[1, 2, 3], [4, 5, 6], [7]]
|
4021 | * R.splitEvery(3, 'foobarbaz'); //=> ['foo', 'bar', 'baz']
|
4022 | */
|
4023 | var splitEvery = _curry2(function splitEvery(n, list) {
|
4024 | if (n <= 0) {
|
4025 | throw new Error('First argument to splitEvery must be a positive integer');
|
4026 | }
|
4027 | var result = [];
|
4028 | var idx = 0;
|
4029 | while (idx < list.length) {
|
4030 | result.push(slice(idx, idx += n, list));
|
4031 | }
|
4032 | return result;
|
4033 | });
|
4034 |
|
4035 | /**
|
4036 | * Takes a list and a predicate and returns a pair of lists with the following properties:
|
4037 | *
|
4038 | * - the result of concatenating the two output lists is equivalent to the input list;
|
4039 | * - none of the elements of the first output list satisfies the predicate; and
|
4040 | * - if the second output list is non-empty, its first element satisfies the predicate.
|
4041 | *
|
4042 | * @func
|
4043 | * @memberOf R
|
4044 | * @since v0.19.0
|
4045 | * @category List
|
4046 | * @sig (a -> Boolean) -> [a] -> [[a], [a]]
|
4047 | * @param {Function} pred The predicate that determines where the array is split.
|
4048 | * @param {Array} list The array to be split.
|
4049 | * @return {Array}
|
4050 | * @example
|
4051 | *
|
4052 | * R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]); //=> [[1], [2, 3, 1, 2, 3]]
|
4053 | */
|
4054 | var splitWhen = _curry2(function splitWhen(pred, list) {
|
4055 | var idx = 0;
|
4056 | var len = list.length;
|
4057 | var prefix = [];
|
4058 | while (idx < len && !pred(list[idx])) {
|
4059 | prefix.push(list[idx]);
|
4060 | idx += 1;
|
4061 | }
|
4062 | return [
|
4063 | prefix,
|
4064 | Array.prototype.slice.call(list, idx)
|
4065 | ];
|
4066 | });
|
4067 |
|
4068 | /**
|
4069 | * Subtracts its second argument from its first argument.
|
4070 | *
|
4071 | * @func
|
4072 | * @memberOf R
|
4073 | * @since v0.1.0
|
4074 | * @category Math
|
4075 | * @sig Number -> Number -> Number
|
4076 | * @param {Number} a The first value.
|
4077 | * @param {Number} b The second value.
|
4078 | * @return {Number} The result of `a - b`.
|
4079 | * @see R.add
|
4080 | * @example
|
4081 | *
|
4082 | * R.subtract(10, 8); //=> 2
|
4083 | *
|
4084 | * var minus5 = R.subtract(R.__, 5);
|
4085 | * minus5(17); //=> 12
|
4086 | *
|
4087 | * var complementaryAngle = R.subtract(90);
|
4088 | * complementaryAngle(30); //=> 60
|
4089 | * complementaryAngle(72); //=> 18
|
4090 | */
|
4091 | var subtract = _curry2(function subtract(a, b) {
|
4092 | return Number(a) - Number(b);
|
4093 | });
|
4094 |
|
4095 | /**
|
4096 | * Returns all but the first element of the given list or string (or object
|
4097 | * with a `tail` method).
|
4098 | *
|
4099 | * Dispatches to the `slice` method of the first argument, if present.
|
4100 | *
|
4101 | * @func
|
4102 | * @memberOf R
|
4103 | * @since v0.1.0
|
4104 | * @category List
|
4105 | * @sig [a] -> [a]
|
4106 | * @sig String -> String
|
4107 | * @param {*} list
|
4108 | * @return {*}
|
4109 | * @see R.head, R.init, R.last
|
4110 | * @example
|
4111 | *
|
4112 | * R.tail([1, 2, 3]); //=> [2, 3]
|
4113 | * R.tail([1, 2]); //=> [2]
|
4114 | * R.tail([1]); //=> []
|
4115 | * R.tail([]); //=> []
|
4116 | *
|
4117 | * R.tail('abc'); //=> 'bc'
|
4118 | * R.tail('ab'); //=> 'b'
|
4119 | * R.tail('a'); //=> ''
|
4120 | * R.tail(''); //=> ''
|
4121 | */
|
4122 | var tail = _curry1(_checkForMethod('tail', slice(1, Infinity)));
|
4123 |
|
4124 | /**
|
4125 | * Returns the first `n` elements of the given list, string, or
|
4126 | * transducer/transformer (or object with a `take` method).
|
4127 | *
|
4128 | * Dispatches to the `take` method of the second argument, if present.
|
4129 | *
|
4130 | * @func
|
4131 | * @memberOf R
|
4132 | * @since v0.1.0
|
4133 | * @category List
|
4134 | * @sig Number -> [a] -> [a]
|
4135 | * @sig Number -> String -> String
|
4136 | * @param {Number} n
|
4137 | * @param {*} list
|
4138 | * @return {*}
|
4139 | * @see R.drop
|
4140 | * @example
|
4141 | *
|
4142 | * R.take(1, ['foo', 'bar', 'baz']); //=> ['foo']
|
4143 | * R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
|
4144 | * R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
|
4145 | * R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
|
4146 | * R.take(3, 'ramda'); //=> 'ram'
|
4147 | *
|
4148 | * var personnel = [
|
4149 | * 'Dave Brubeck',
|
4150 | * 'Paul Desmond',
|
4151 | * 'Eugene Wright',
|
4152 | * 'Joe Morello',
|
4153 | * 'Gerry Mulligan',
|
4154 | * 'Bob Bates',
|
4155 | * 'Joe Dodge',
|
4156 | * 'Ron Crotty'
|
4157 | * ];
|
4158 | *
|
4159 | * var takeFive = R.take(5);
|
4160 | * takeFive(personnel);
|
4161 | * //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']
|
4162 | * @symb R.take(-1, [a, b]) = [a, b]
|
4163 | * @symb R.take(0, [a, b]) = []
|
4164 | * @symb R.take(1, [a, b]) = [a]
|
4165 | * @symb R.take(2, [a, b]) = [a, b]
|
4166 | */
|
4167 | var take = _curry2(_dispatchable(['take'], _xtake, function take(n, xs) {
|
4168 | return slice(0, n < 0 ? Infinity : n, xs);
|
4169 | }));
|
4170 |
|
4171 | /**
|
4172 | * Returns a new list containing the last `n` elements of a given list, passing
|
4173 | * each value to the supplied predicate function, and terminating when the
|
4174 | * predicate function returns `false`. Excludes the element that caused the
|
4175 | * predicate function to fail. The predicate function is passed one argument:
|
4176 | * *(value)*.
|
4177 | *
|
4178 | * @func
|
4179 | * @memberOf R
|
4180 | * @since v0.16.0
|
4181 | * @category List
|
4182 | * @sig (a -> Boolean) -> [a] -> [a]
|
4183 | * @param {Function} fn The function called per iteration.
|
4184 | * @param {Array} list The collection to iterate over.
|
4185 | * @return {Array} A new array.
|
4186 | * @see R.dropLastWhile, R.addIndex
|
4187 | * @example
|
4188 | *
|
4189 | * var isNotOne = x => x !== 1;
|
4190 | *
|
4191 | * R.takeLastWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4]
|
4192 | */
|
4193 | var takeLastWhile = _curry2(function takeLastWhile(fn, list) {
|
4194 | var idx = list.length - 1;
|
4195 | while (idx >= 0 && fn(list[idx])) {
|
4196 | idx -= 1;
|
4197 | }
|
4198 | return Array.prototype.slice.call(list, idx + 1);
|
4199 | });
|
4200 |
|
4201 | /**
|
4202 | * Returns a new list containing the first `n` elements of a given list,
|
4203 | * passing each value to the supplied predicate function, and terminating when
|
4204 | * the predicate function returns `false`. Excludes the element that caused the
|
4205 | * predicate function to fail. The predicate function is passed one argument:
|
4206 | * *(value)*.
|
4207 | *
|
4208 | * Dispatches to the `takeWhile` method of the second argument, if present.
|
4209 | *
|
4210 | * Acts as a transducer if a transformer is given in list position.
|
4211 | *
|
4212 | * @func
|
4213 | * @memberOf R
|
4214 | * @since v0.1.0
|
4215 | * @category List
|
4216 | * @sig (a -> Boolean) -> [a] -> [a]
|
4217 | * @param {Function} fn The function called per iteration.
|
4218 | * @param {Array} list The collection to iterate over.
|
4219 | * @return {Array} A new array.
|
4220 | * @see R.dropWhile, R.transduce, R.addIndex
|
4221 | * @example
|
4222 | *
|
4223 | * var isNotFour = x => x !== 4;
|
4224 | *
|
4225 | * R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
|
4226 | */
|
4227 | var takeWhile = _curry2(_dispatchable(['takeWhile'], _xtakeWhile, function takeWhile(fn, list) {
|
4228 | var idx = 0;
|
4229 | var len = list.length;
|
4230 | while (idx < len && fn(list[idx])) {
|
4231 | idx += 1;
|
4232 | }
|
4233 | return Array.prototype.slice.call(list, 0, idx);
|
4234 | }));
|
4235 |
|
4236 | /**
|
4237 | * Runs the given function with the supplied object, then returns the object.
|
4238 | *
|
4239 | * @func
|
4240 | * @memberOf R
|
4241 | * @since v0.1.0
|
4242 | * @category Function
|
4243 | * @sig (a -> *) -> a -> a
|
4244 | * @param {Function} fn The function to call with `x`. The return value of `fn` will be thrown away.
|
4245 | * @param {*} x
|
4246 | * @return {*} `x`.
|
4247 | * @example
|
4248 | *
|
4249 | * var sayX = x => console.log('x is ' + x);
|
4250 | * R.tap(sayX, 100); //=> 100
|
4251 | * // logs 'x is 100'
|
4252 | * @symb R.tap(f, a) = a
|
4253 | */
|
4254 | var tap = _curry2(function tap(fn, x) {
|
4255 | fn(x);
|
4256 | return x;
|
4257 | });
|
4258 |
|
4259 | /**
|
4260 | * Calls an input function `n` times, returning an array containing the results
|
4261 | * of those function calls.
|
4262 | *
|
4263 | * `fn` is passed one argument: The current value of `n`, which begins at `0`
|
4264 | * and is gradually incremented to `n - 1`.
|
4265 | *
|
4266 | * @func
|
4267 | * @memberOf R
|
4268 | * @since v0.2.3
|
4269 | * @category List
|
4270 | * @sig (Number -> a) -> Number -> [a]
|
4271 | * @param {Function} fn The function to invoke. Passed one argument, the current value of `n`.
|
4272 | * @param {Number} n A value between `0` and `n - 1`. Increments after each function call.
|
4273 | * @return {Array} An array containing the return values of all calls to `fn`.
|
4274 | * @example
|
4275 | *
|
4276 | * R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]
|
4277 | * @symb R.times(f, 0) = []
|
4278 | * @symb R.times(f, 1) = [f(0)]
|
4279 | * @symb R.times(f, 2) = [f(0), f(1)]
|
4280 | */
|
4281 | var times = _curry2(function times(fn, n) {
|
4282 | var len = Number(n);
|
4283 | var idx = 0;
|
4284 | var list;
|
4285 | if (len < 0 || isNaN(len)) {
|
4286 | throw new RangeError('n must be a non-negative number');
|
4287 | }
|
4288 | list = new Array(len);
|
4289 | while (idx < len) {
|
4290 | list[idx] = fn(idx);
|
4291 | idx += 1;
|
4292 | }
|
4293 | return list;
|
4294 | });
|
4295 |
|
4296 | /**
|
4297 | * Converts an object into an array of key, value arrays. Only the object's
|
4298 | * own properties are used.
|
4299 | * Note that the order of the output array is not guaranteed to be consistent
|
4300 | * across different JS platforms.
|
4301 | *
|
4302 | * @func
|
4303 | * @memberOf R
|
4304 | * @since v0.4.0
|
4305 | * @category Object
|
4306 | * @sig {String: *} -> [[String,*]]
|
4307 | * @param {Object} obj The object to extract from
|
4308 | * @return {Array} An array of key, value arrays from the object's own properties.
|
4309 | * @see R.fromPairs
|
4310 | * @example
|
4311 | *
|
4312 | * R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]]
|
4313 | */
|
4314 | var toPairs = _curry1(function toPairs(obj) {
|
4315 | var pairs = [];
|
4316 | for (var prop in obj) {
|
4317 | if (_has(prop, obj)) {
|
4318 | pairs[pairs.length] = [
|
4319 | prop,
|
4320 | obj[prop]
|
4321 | ];
|
4322 | }
|
4323 | }
|
4324 | return pairs;
|
4325 | });
|
4326 |
|
4327 | /**
|
4328 | * Converts an object into an array of key, value arrays. The object's own
|
4329 | * properties and prototype properties are used. Note that the order of the
|
4330 | * output array is not guaranteed to be consistent across different JS
|
4331 | * platforms.
|
4332 | *
|
4333 | * @func
|
4334 | * @memberOf R
|
4335 | * @since v0.4.0
|
4336 | * @category Object
|
4337 | * @sig {String: *} -> [[String,*]]
|
4338 | * @param {Object} obj The object to extract from
|
4339 | * @return {Array} An array of key, value arrays from the object's own
|
4340 | * and prototype properties.
|
4341 | * @example
|
4342 | *
|
4343 | * var F = function() { this.x = 'X'; };
|
4344 | * F.prototype.y = 'Y';
|
4345 | * var f = new F();
|
4346 | * R.toPairsIn(f); //=> [['x','X'], ['y','Y']]
|
4347 | */
|
4348 | var toPairsIn = _curry1(function toPairsIn(obj) {
|
4349 | var pairs = [];
|
4350 | for (var prop in obj) {
|
4351 | pairs[pairs.length] = [
|
4352 | prop,
|
4353 | obj[prop]
|
4354 | ];
|
4355 | }
|
4356 | return pairs;
|
4357 | });
|
4358 |
|
4359 | /**
|
4360 | * Transposes the rows and columns of a 2D list.
|
4361 | * When passed a list of `n` lists of length `x`,
|
4362 | * returns a list of `x` lists of length `n`.
|
4363 | *
|
4364 | *
|
4365 | * @func
|
4366 | * @memberOf R
|
4367 | * @since v0.19.0
|
4368 | * @category List
|
4369 | * @sig [[a]] -> [[a]]
|
4370 | * @param {Array} list A 2D list
|
4371 | * @return {Array} A 2D list
|
4372 | * @example
|
4373 | *
|
4374 | * R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']]
|
4375 | * R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']]
|
4376 | *
|
4377 | * If some of the rows are shorter than the following rows, their elements are skipped:
|
4378 | *
|
4379 | * R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]]
|
4380 | * @symb R.transpose([[a], [b], [c]]) = [a, b, c]
|
4381 | * @symb R.transpose([[a, b], [c, d]]) = [[a, c], [b, d]]
|
4382 | * @symb R.transpose([[a, b], [c]]) = [[a, c], [b]]
|
4383 | */
|
4384 | var transpose = _curry1(function transpose(outerlist) {
|
4385 | var i = 0;
|
4386 | var result = [];
|
4387 | while (i < outerlist.length) {
|
4388 | var innerlist = outerlist[i];
|
4389 | var j = 0;
|
4390 | while (j < innerlist.length) {
|
4391 | if (typeof result[j] === 'undefined') {
|
4392 | result[j] = [];
|
4393 | }
|
4394 | result[j].push(innerlist[j]);
|
4395 | j += 1;
|
4396 | }
|
4397 | i += 1;
|
4398 | }
|
4399 | return result;
|
4400 | });
|
4401 |
|
4402 | /**
|
4403 | * Removes (strips) whitespace from both ends of the string.
|
4404 | *
|
4405 | * @func
|
4406 | * @memberOf R
|
4407 | * @since v0.6.0
|
4408 | * @category String
|
4409 | * @sig String -> String
|
4410 | * @param {String} str The string to trim.
|
4411 | * @return {String} Trimmed version of `str`.
|
4412 | * @example
|
4413 | *
|
4414 | * R.trim(' xyz '); //=> 'xyz'
|
4415 | * R.map(R.trim, R.split(',', 'x, y, z')); //=> ['x', 'y', 'z']
|
4416 | */
|
4417 | var trim = function () {
|
4418 | var ws = '\t\n\x0B\f\r \xA0\u1680\u180E\u2000\u2001\u2002\u2003' + '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' + '\u2029\uFEFF';
|
4419 | var zeroWidth = '\u200B';
|
4420 | var hasProtoTrim = typeof String.prototype.trim === 'function';
|
4421 | if (!hasProtoTrim || (ws.trim() || !zeroWidth.trim())) {
|
4422 | return _curry1(function trim(str) {
|
4423 | var beginRx = new RegExp('^[' + ws + '][' + ws + ']*');
|
4424 | var endRx = new RegExp('[' + ws + '][' + ws + ']*$');
|
4425 | return str.replace(beginRx, '').replace(endRx, '');
|
4426 | });
|
4427 | } else {
|
4428 | return _curry1(function trim(str) {
|
4429 | return str.trim();
|
4430 | });
|
4431 | }
|
4432 | }();
|
4433 |
|
4434 | /**
|
4435 | * `tryCatch` takes two functions, a `tryer` and a `catcher`. The returned
|
4436 | * function evaluates the `tryer`; if it does not throw, it simply returns the
|
4437 | * result. If the `tryer` *does* throw, the returned function evaluates the
|
4438 | * `catcher` function and returns its result. Note that for effective
|
4439 | * composition with this function, both the `tryer` and `catcher` functions
|
4440 | * must return the same type of results.
|
4441 | *
|
4442 | * @func
|
4443 | * @memberOf R
|
4444 | * @since v0.20.0
|
4445 | * @category Function
|
4446 | * @sig (...x -> a) -> ((e, ...x) -> a) -> (...x -> a)
|
4447 | * @param {Function} tryer The function that may throw.
|
4448 | * @param {Function} catcher The function that will be evaluated if `tryer` throws.
|
4449 | * @return {Function} A new function that will catch exceptions and send then to the catcher.
|
4450 | * @example
|
4451 | *
|
4452 | * R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
|
4453 | * R.tryCatch(R.prop('x'), R.F)(null); //=> false
|
4454 | */
|
4455 | var tryCatch = _curry2(function _tryCatch(tryer, catcher) {
|
4456 | return _arity(tryer.length, function () {
|
4457 | try {
|
4458 | return tryer.apply(this, arguments);
|
4459 | } catch (e) {
|
4460 | return catcher.apply(this, _concat([e], arguments));
|
4461 | }
|
4462 | });
|
4463 | });
|
4464 |
|
4465 | /**
|
4466 | * Gives a single-word string description of the (native) type of a value,
|
4467 | * returning such answers as 'Object', 'Number', 'Array', or 'Null'. Does not
|
4468 | * attempt to distinguish user Object types any further, reporting them all as
|
4469 | * 'Object'.
|
4470 | *
|
4471 | * @func
|
4472 | * @memberOf R
|
4473 | * @since v0.8.0
|
4474 | * @category Type
|
4475 | * @sig (* -> {*}) -> String
|
4476 | * @param {*} val The value to test
|
4477 | * @return {String}
|
4478 | * @example
|
4479 | *
|
4480 | * R.type({}); //=> "Object"
|
4481 | * R.type(1); //=> "Number"
|
4482 | * R.type(false); //=> "Boolean"
|
4483 | * R.type('s'); //=> "String"
|
4484 | * R.type(null); //=> "Null"
|
4485 | * R.type([]); //=> "Array"
|
4486 | * R.type(/[A-z]/); //=> "RegExp"
|
4487 | */
|
4488 | var type = _curry1(function type(val) {
|
4489 | return val === null ? 'Null' : val === undefined ? 'Undefined' : Object.prototype.toString.call(val).slice(8, -1);
|
4490 | });
|
4491 |
|
4492 | /**
|
4493 | * Takes a function `fn`, which takes a single array argument, and returns a
|
4494 | * function which:
|
4495 | *
|
4496 | * - takes any number of positional arguments;
|
4497 | * - passes these arguments to `fn` as an array; and
|
4498 | * - returns the result.
|
4499 | *
|
4500 | * In other words, R.unapply derives a variadic function from a function which
|
4501 | * takes an array. R.unapply is the inverse of R.apply.
|
4502 | *
|
4503 | * @func
|
4504 | * @memberOf R
|
4505 | * @since v0.8.0
|
4506 | * @category Function
|
4507 | * @sig ([*...] -> a) -> (*... -> a)
|
4508 | * @param {Function} fn
|
4509 | * @return {Function}
|
4510 | * @see R.apply
|
4511 | * @example
|
4512 | *
|
4513 | * R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]'
|
4514 | * @symb R.unapply(f)(a, b) = f([a, b])
|
4515 | */
|
4516 | var unapply = _curry1(function unapply(fn) {
|
4517 | return function () {
|
4518 | return fn(Array.prototype.slice.call(arguments, 0));
|
4519 | };
|
4520 | });
|
4521 |
|
4522 | /**
|
4523 | * Wraps a function of any arity (including nullary) in a function that accepts
|
4524 | * exactly 1 parameter. Any extraneous parameters will not be passed to the
|
4525 | * supplied function.
|
4526 | *
|
4527 | * @func
|
4528 | * @memberOf R
|
4529 | * @since v0.2.0
|
4530 | * @category Function
|
4531 | * @sig (* -> b) -> (a -> b)
|
4532 | * @param {Function} fn The function to wrap.
|
4533 | * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
|
4534 | * arity 1.
|
4535 | * @example
|
4536 | *
|
4537 | * var takesTwoArgs = function(a, b) {
|
4538 | * return [a, b];
|
4539 | * };
|
4540 | * takesTwoArgs.length; //=> 2
|
4541 | * takesTwoArgs(1, 2); //=> [1, 2]
|
4542 | *
|
4543 | * var takesOneArg = R.unary(takesTwoArgs);
|
4544 | * takesOneArg.length; //=> 1
|
4545 | * // Only 1 argument is passed to the wrapped function
|
4546 | * takesOneArg(1, 2); //=> [1, undefined]
|
4547 | * @symb R.unary(f)(a, b, c) = f(a)
|
4548 | */
|
4549 | var unary = _curry1(function unary(fn) {
|
4550 | return nAry(1, fn);
|
4551 | });
|
4552 |
|
4553 | /**
|
4554 | * Returns a function of arity `n` from a (manually) curried function.
|
4555 | *
|
4556 | * @func
|
4557 | * @memberOf R
|
4558 | * @since v0.14.0
|
4559 | * @category Function
|
4560 | * @sig Number -> (a -> b) -> (a -> c)
|
4561 | * @param {Number} length The arity for the returned function.
|
4562 | * @param {Function} fn The function to uncurry.
|
4563 | * @return {Function} A new function.
|
4564 | * @see R.curry
|
4565 | * @example
|
4566 | *
|
4567 | * var addFour = a => b => c => d => a + b + c + d;
|
4568 | *
|
4569 | * var uncurriedAddFour = R.uncurryN(4, addFour);
|
4570 | * uncurriedAddFour(1, 2, 3, 4); //=> 10
|
4571 | */
|
4572 | var uncurryN = _curry2(function uncurryN(depth, fn) {
|
4573 | return curryN(depth, function () {
|
4574 | var currentDepth = 1;
|
4575 | var value = fn;
|
4576 | var idx = 0;
|
4577 | var endIdx;
|
4578 | while (currentDepth <= depth && typeof value === 'function') {
|
4579 | endIdx = currentDepth === depth ? arguments.length : idx + value.length;
|
4580 | value = value.apply(this, Array.prototype.slice.call(arguments, idx, endIdx));
|
4581 | currentDepth += 1;
|
4582 | idx = endIdx;
|
4583 | }
|
4584 | return value;
|
4585 | });
|
4586 | });
|
4587 |
|
4588 | /**
|
4589 | * Builds a list from a seed value. Accepts an iterator function, which returns
|
4590 | * either false to stop iteration or an array of length 2 containing the value
|
4591 | * to add to the resulting list and the seed to be used in the next call to the
|
4592 | * iterator function.
|
4593 | *
|
4594 | * The iterator function receives one argument: *(seed)*.
|
4595 | *
|
4596 | * @func
|
4597 | * @memberOf R
|
4598 | * @since v0.10.0
|
4599 | * @category List
|
4600 | * @sig (a -> [b]) -> * -> [b]
|
4601 | * @param {Function} fn The iterator function. receives one argument, `seed`, and returns
|
4602 | * either false to quit iteration or an array of length two to proceed. The element
|
4603 | * at index 0 of this array will be added to the resulting array, and the element
|
4604 | * at index 1 will be passed to the next call to `fn`.
|
4605 | * @param {*} seed The seed value.
|
4606 | * @return {Array} The final list.
|
4607 | * @example
|
4608 | *
|
4609 | * var f = n => n > 50 ? false : [-n, n + 10];
|
4610 | * R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]
|
4611 | * @symb R.unfold(f, x) = [f(x)[0], f(f(x)[1])[0], f(f(f(x)[1])[1])[0], ...]
|
4612 | */
|
4613 | var unfold = _curry2(function unfold(fn, seed) {
|
4614 | var pair = fn(seed);
|
4615 | var result = [];
|
4616 | while (pair && pair.length) {
|
4617 | result[result.length] = pair[0];
|
4618 | pair = fn(pair[1]);
|
4619 | }
|
4620 | return result;
|
4621 | });
|
4622 |
|
4623 | /**
|
4624 | * Returns a new list containing only one copy of each element in the original
|
4625 | * list, based upon the value returned by applying the supplied predicate to
|
4626 | * two list elements. Prefers the first item if two items compare equal based
|
4627 | * on the predicate.
|
4628 | *
|
4629 | * @func
|
4630 | * @memberOf R
|
4631 | * @since v0.2.0
|
4632 | * @category List
|
4633 | * @sig (a, a -> Boolean) -> [a] -> [a]
|
4634 | * @param {Function} pred A predicate used to test whether two items are equal.
|
4635 | * @param {Array} list The array to consider.
|
4636 | * @return {Array} The list of unique items.
|
4637 | * @example
|
4638 | *
|
4639 | * var strEq = R.eqBy(String);
|
4640 | * R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
|
4641 | * R.uniqWith(strEq)([{}, {}]); //=> [{}]
|
4642 | * R.uniqWith(strEq)([1, '1', 1]); //=> [1]
|
4643 | * R.uniqWith(strEq)(['1', 1, 1]); //=> ['1']
|
4644 | */
|
4645 | var uniqWith = _curry2(function uniqWith(pred, list) {
|
4646 | var idx = 0;
|
4647 | var len = list.length;
|
4648 | var result = [];
|
4649 | var item;
|
4650 | while (idx < len) {
|
4651 | item = list[idx];
|
4652 | if (!_containsWith(pred, item, result)) {
|
4653 | result[result.length] = item;
|
4654 | }
|
4655 | idx += 1;
|
4656 | }
|
4657 | return result;
|
4658 | });
|
4659 |
|
4660 | /**
|
4661 | * Tests the final argument by passing it to the given predicate function. If
|
4662 | * the predicate is not satisfied, the function will return the result of
|
4663 | * calling the `whenFalseFn` function with the same argument. If the predicate
|
4664 | * is satisfied, the argument is returned as is.
|
4665 | *
|
4666 | * @func
|
4667 | * @memberOf R
|
4668 | * @since v0.18.0
|
4669 | * @category Logic
|
4670 | * @sig (a -> Boolean) -> (a -> a) -> a -> a
|
4671 | * @param {Function} pred A predicate function
|
4672 | * @param {Function} whenFalseFn A function to invoke when the `pred` evaluates
|
4673 | * to a falsy value.
|
4674 | * @param {*} x An object to test with the `pred` function and
|
4675 | * pass to `whenFalseFn` if necessary.
|
4676 | * @return {*} Either `x` or the result of applying `x` to `whenFalseFn`.
|
4677 | * @see R.ifElse, R.when
|
4678 | * @example
|
4679 | *
|
4680 | * // coerceArray :: (a|[a]) -> [a]
|
4681 | * var coerceArray = R.unless(R.isArrayLike, R.of);
|
4682 | * coerceArray([1, 2, 3]); //=> [1, 2, 3]
|
4683 | * coerceArray(1); //=> [1]
|
4684 | */
|
4685 | var unless = _curry3(function unless(pred, whenFalseFn, x) {
|
4686 | return pred(x) ? x : whenFalseFn(x);
|
4687 | });
|
4688 |
|
4689 | /**
|
4690 | * Takes a predicate, a transformation function, and an initial value,
|
4691 | * and returns a value of the same type as the initial value.
|
4692 | * It does so by applying the transformation until the predicate is satisfied,
|
4693 | * at which point it returns the satisfactory value.
|
4694 | *
|
4695 | * @func
|
4696 | * @memberOf R
|
4697 | * @since v0.20.0
|
4698 | * @category Logic
|
4699 | * @sig (a -> Boolean) -> (a -> a) -> a -> a
|
4700 | * @param {Function} pred A predicate function
|
4701 | * @param {Function} fn The iterator function
|
4702 | * @param {*} init Initial value
|
4703 | * @return {*} Final value that satisfies predicate
|
4704 | * @example
|
4705 | *
|
4706 | * R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128
|
4707 | */
|
4708 | var until = _curry3(function until(pred, fn, init) {
|
4709 | var val = init;
|
4710 | while (!pred(val)) {
|
4711 | val = fn(val);
|
4712 | }
|
4713 | return val;
|
4714 | });
|
4715 |
|
4716 | /**
|
4717 | * Returns a new copy of the array with the element at the provided index
|
4718 | * replaced with the given value.
|
4719 | *
|
4720 | * @func
|
4721 | * @memberOf R
|
4722 | * @since v0.14.0
|
4723 | * @category List
|
4724 | * @sig Number -> a -> [a] -> [a]
|
4725 | * @param {Number} idx The index to update.
|
4726 | * @param {*} x The value to exist at the given index of the returned array.
|
4727 | * @param {Array|Arguments} list The source array-like object to be updated.
|
4728 | * @return {Array} A copy of `list` with the value at index `idx` replaced with `x`.
|
4729 | * @see R.adjust
|
4730 | * @example
|
4731 | *
|
4732 | * R.update(1, 11, [0, 1, 2]); //=> [0, 11, 2]
|
4733 | * R.update(1)(11)([0, 1, 2]); //=> [0, 11, 2]
|
4734 | * @symb R.update(-1, a, [b, c]) = [b, a]
|
4735 | * @symb R.update(0, a, [b, c]) = [a, c]
|
4736 | * @symb R.update(1, a, [b, c]) = [b, a]
|
4737 | */
|
4738 | var update = _curry3(function update(idx, x, list) {
|
4739 | return adjust(always(x), idx, list);
|
4740 | });
|
4741 |
|
4742 | /**
|
4743 | * Accepts a function `fn` and a list of transformer functions and returns a
|
4744 | * new curried function. When the new function is invoked, it calls the
|
4745 | * function `fn` with parameters consisting of the result of calling each
|
4746 | * supplied handler on successive arguments to the new function.
|
4747 | *
|
4748 | * If more arguments are passed to the returned function than transformer
|
4749 | * functions, those arguments are passed directly to `fn` as additional
|
4750 | * parameters. If you expect additional arguments that don't need to be
|
4751 | * transformed, although you can ignore them, it's best to pass an identity
|
4752 | * function so that the new function reports the correct arity.
|
4753 | *
|
4754 | * @func
|
4755 | * @memberOf R
|
4756 | * @since v0.1.0
|
4757 | * @category Function
|
4758 | * @sig (x1 -> x2 -> ... -> z) -> [(a -> x1), (b -> x2), ...] -> (a -> b -> ... -> z)
|
4759 | * @param {Function} fn The function to wrap.
|
4760 | * @param {Array} transformers A list of transformer functions
|
4761 | * @return {Function} The wrapped function.
|
4762 | * @see R.converge
|
4763 | * @example
|
4764 | *
|
4765 | * R.useWith(Math.pow, [R.identity, R.identity])(3, 4); //=> 81
|
4766 | * R.useWith(Math.pow, [R.identity, R.identity])(3)(4); //=> 81
|
4767 | * R.useWith(Math.pow, [R.dec, R.inc])(3, 4); //=> 32
|
4768 | * R.useWith(Math.pow, [R.dec, R.inc])(3)(4); //=> 32
|
4769 | * @symb R.useWith(f, [g, h])(a, b) = f(g(a), h(b))
|
4770 | */
|
4771 | var useWith = _curry2(function useWith(fn, transformers) {
|
4772 | return curryN(transformers.length, function () {
|
4773 | var args = [];
|
4774 | var idx = 0;
|
4775 | while (idx < transformers.length) {
|
4776 | args.push(transformers[idx].call(this, arguments[idx]));
|
4777 | idx += 1;
|
4778 | }
|
4779 | return fn.apply(this, args.concat(Array.prototype.slice.call(arguments, transformers.length)));
|
4780 | });
|
4781 | });
|
4782 |
|
4783 | /**
|
4784 | * Returns a list of all the enumerable own properties of the supplied object.
|
4785 | * Note that the order of the output array is not guaranteed across different
|
4786 | * JS platforms.
|
4787 | *
|
4788 | * @func
|
4789 | * @memberOf R
|
4790 | * @since v0.1.0
|
4791 | * @category Object
|
4792 | * @sig {k: v} -> [v]
|
4793 | * @param {Object} obj The object to extract values from
|
4794 | * @return {Array} An array of the values of the object's own properties.
|
4795 | * @example
|
4796 | *
|
4797 | * R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3]
|
4798 | */
|
4799 | var values = _curry1(function values(obj) {
|
4800 | var props = keys(obj);
|
4801 | var len = props.length;
|
4802 | var vals = [];
|
4803 | var idx = 0;
|
4804 | while (idx < len) {
|
4805 | vals[idx] = obj[props[idx]];
|
4806 | idx += 1;
|
4807 | }
|
4808 | return vals;
|
4809 | });
|
4810 |
|
4811 | /**
|
4812 | * Returns a list of all the properties, including prototype properties, of the
|
4813 | * supplied object.
|
4814 | * Note that the order of the output array is not guaranteed to be consistent
|
4815 | * across different JS platforms.
|
4816 | *
|
4817 | * @func
|
4818 | * @memberOf R
|
4819 | * @since v0.2.0
|
4820 | * @category Object
|
4821 | * @sig {k: v} -> [v]
|
4822 | * @param {Object} obj The object to extract values from
|
4823 | * @return {Array} An array of the values of the object's own and prototype properties.
|
4824 | * @example
|
4825 | *
|
4826 | * var F = function() { this.x = 'X'; };
|
4827 | * F.prototype.y = 'Y';
|
4828 | * var f = new F();
|
4829 | * R.valuesIn(f); //=> ['X', 'Y']
|
4830 | */
|
4831 | var valuesIn = _curry1(function valuesIn(obj) {
|
4832 | var prop;
|
4833 | var vs = [];
|
4834 | for (prop in obj) {
|
4835 | vs[vs.length] = obj[prop];
|
4836 | }
|
4837 | return vs;
|
4838 | });
|
4839 |
|
4840 | /**
|
4841 | * Returns a "view" of the given data structure, determined by the given lens.
|
4842 | * The lens's focus determines which portion of the data structure is visible.
|
4843 | *
|
4844 | * @func
|
4845 | * @memberOf R
|
4846 | * @since v0.16.0
|
4847 | * @category Object
|
4848 | * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
|
4849 | * @sig Lens s a -> s -> a
|
4850 | * @param {Lens} lens
|
4851 | * @param {*} x
|
4852 | * @return {*}
|
4853 | * @see R.prop, R.lensIndex, R.lensProp
|
4854 | * @example
|
4855 | *
|
4856 | * var xLens = R.lensProp('x');
|
4857 | *
|
4858 | * R.view(xLens, {x: 1, y: 2}); //=> 1
|
4859 | * R.view(xLens, {x: 4, y: 2}); //=> 4
|
4860 | */
|
4861 | // `Const` is a functor that effectively ignores the function given to `map`.
|
4862 | // Using `Const` effectively ignores the setter function of the `lens`,
|
4863 | // leaving the value returned by the getter function unmodified.
|
4864 | var view = function () {
|
4865 | // `Const` is a functor that effectively ignores the function given to `map`.
|
4866 | var Const = function (x) {
|
4867 | return {
|
4868 | value: x,
|
4869 | map: function () {
|
4870 | return this;
|
4871 | }
|
4872 | };
|
4873 | };
|
4874 | return _curry2(function view(lens, x) {
|
4875 | // Using `Const` effectively ignores the setter function of the `lens`,
|
4876 | // leaving the value returned by the getter function unmodified.
|
4877 | return lens(Const)(x).value;
|
4878 | });
|
4879 | }();
|
4880 |
|
4881 | /**
|
4882 | * Tests the final argument by passing it to the given predicate function. If
|
4883 | * the predicate is satisfied, the function will return the result of calling
|
4884 | * the `whenTrueFn` function with the same argument. If the predicate is not
|
4885 | * satisfied, the argument is returned as is.
|
4886 | *
|
4887 | * @func
|
4888 | * @memberOf R
|
4889 | * @since v0.18.0
|
4890 | * @category Logic
|
4891 | * @sig (a -> Boolean) -> (a -> a) -> a -> a
|
4892 | * @param {Function} pred A predicate function
|
4893 | * @param {Function} whenTrueFn A function to invoke when the `condition`
|
4894 | * evaluates to a truthy value.
|
4895 | * @param {*} x An object to test with the `pred` function and
|
4896 | * pass to `whenTrueFn` if necessary.
|
4897 | * @return {*} Either `x` or the result of applying `x` to `whenTrueFn`.
|
4898 | * @see R.ifElse, R.unless
|
4899 | * @example
|
4900 | *
|
4901 | * // truncate :: String -> String
|
4902 | * var truncate = R.when(
|
4903 | * R.propSatisfies(R.gt(R.__, 10), 'length'),
|
4904 | * R.pipe(R.take(10), R.append('…'), R.join(''))
|
4905 | * );
|
4906 | * truncate('12345'); //=> '12345'
|
4907 | * truncate('0123456789ABC'); //=> '0123456789…'
|
4908 | */
|
4909 | var when = _curry3(function when(pred, whenTrueFn, x) {
|
4910 | return pred(x) ? whenTrueFn(x) : x;
|
4911 | });
|
4912 |
|
4913 | /**
|
4914 | * Takes a spec object and a test object; returns true if the test satisfies
|
4915 | * the spec. Each of the spec's own properties must be a predicate function.
|
4916 | * Each predicate is applied to the value of the corresponding property of the
|
4917 | * test object. `where` returns true if all the predicates return true, false
|
4918 | * otherwise.
|
4919 | *
|
4920 | * `where` is well suited to declaratively expressing constraints for other
|
4921 | * functions such as `filter` and `find`.
|
4922 | *
|
4923 | * @func
|
4924 | * @memberOf R
|
4925 | * @since v0.1.1
|
4926 | * @category Object
|
4927 | * @sig {String: (* -> Boolean)} -> {String: *} -> Boolean
|
4928 | * @param {Object} spec
|
4929 | * @param {Object} testObj
|
4930 | * @return {Boolean}
|
4931 | * @example
|
4932 | *
|
4933 | * // pred :: Object -> Boolean
|
4934 | * var pred = R.where({
|
4935 | * a: R.equals('foo'),
|
4936 | * b: R.complement(R.equals('bar')),
|
4937 | * x: R.gt(__, 10),
|
4938 | * y: R.lt(__, 20)
|
4939 | * });
|
4940 | *
|
4941 | * pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
|
4942 | * pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
|
4943 | * pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
|
4944 | * pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
|
4945 | * pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false
|
4946 | */
|
4947 | var where = _curry2(function where(spec, testObj) {
|
4948 | for (var prop in spec) {
|
4949 | if (_has(prop, spec) && !spec[prop](testObj[prop])) {
|
4950 | return false;
|
4951 | }
|
4952 | }
|
4953 | return true;
|
4954 | });
|
4955 |
|
4956 | /**
|
4957 | * Creates a new list out of the two supplied by creating each possible pair
|
4958 | * from the lists.
|
4959 | *
|
4960 | * @func
|
4961 | * @memberOf R
|
4962 | * @since v0.1.0
|
4963 | * @category List
|
4964 | * @sig [a] -> [b] -> [[a,b]]
|
4965 | * @param {Array} as The first list.
|
4966 | * @param {Array} bs The second list.
|
4967 | * @return {Array} The list made by combining each possible pair from
|
4968 | * `as` and `bs` into pairs (`[a, b]`).
|
4969 | * @example
|
4970 | *
|
4971 | * R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
4972 | * @symb R.xprod([a, b], [c, d]) = [[a, c], [a, d], [b, c], [b, d]]
|
4973 | */
|
4974 | // = xprodWith(prepend); (takes about 3 times as long...)
|
4975 | var xprod = _curry2(function xprod(a, b) {
|
4976 | // = xprodWith(prepend); (takes about 3 times as long...)
|
4977 | var idx = 0;
|
4978 | var ilen = a.length;
|
4979 | var j;
|
4980 | var jlen = b.length;
|
4981 | var result = [];
|
4982 | while (idx < ilen) {
|
4983 | j = 0;
|
4984 | while (j < jlen) {
|
4985 | result[result.length] = [
|
4986 | a[idx],
|
4987 | b[j]
|
4988 | ];
|
4989 | j += 1;
|
4990 | }
|
4991 | idx += 1;
|
4992 | }
|
4993 | return result;
|
4994 | });
|
4995 |
|
4996 | /**
|
4997 | * Creates a new list out of the two supplied by pairing up equally-positioned
|
4998 | * items from both lists. The returned list is truncated to the length of the
|
4999 | * shorter of the two input lists.
|
5000 | * Note: `zip` is equivalent to `zipWith(function(a, b) { return [a, b] })`.
|
5001 | *
|
5002 | * @func
|
5003 | * @memberOf R
|
5004 | * @since v0.1.0
|
5005 | * @category List
|
5006 | * @sig [a] -> [b] -> [[a,b]]
|
5007 | * @param {Array} list1 The first array to consider.
|
5008 | * @param {Array} list2 The second array to consider.
|
5009 | * @return {Array} The list made by pairing up same-indexed elements of `list1` and `list2`.
|
5010 | * @example
|
5011 | *
|
5012 | * R.zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
|
5013 | * @symb R.zip([a, b, c], [d, e, f]) = [[a, d], [b, e], [c, f]]
|
5014 | */
|
5015 | var zip = _curry2(function zip(a, b) {
|
5016 | var rv = [];
|
5017 | var idx = 0;
|
5018 | var len = Math.min(a.length, b.length);
|
5019 | while (idx < len) {
|
5020 | rv[idx] = [
|
5021 | a[idx],
|
5022 | b[idx]
|
5023 | ];
|
5024 | idx += 1;
|
5025 | }
|
5026 | return rv;
|
5027 | });
|
5028 |
|
5029 | /**
|
5030 | * Creates a new object out of a list of keys and a list of values.
|
5031 | * Key/value pairing is truncated to the length of the shorter of the two lists.
|
5032 | * Note: `zipObj` is equivalent to `pipe(zipWith(pair), fromPairs)`.
|
5033 | *
|
5034 | * @func
|
5035 | * @memberOf R
|
5036 | * @since v0.3.0
|
5037 | * @category List
|
5038 | * @sig [String] -> [*] -> {String: *}
|
5039 | * @param {Array} keys The array that will be properties on the output object.
|
5040 | * @param {Array} values The list of values on the output object.
|
5041 | * @return {Object} The object made by pairing up same-indexed elements of `keys` and `values`.
|
5042 | * @example
|
5043 | *
|
5044 | * R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3}
|
5045 | */
|
5046 | var zipObj = _curry2(function zipObj(keys, values) {
|
5047 | var idx = 0;
|
5048 | var len = Math.min(keys.length, values.length);
|
5049 | var out = {};
|
5050 | while (idx < len) {
|
5051 | out[keys[idx]] = values[idx];
|
5052 | idx += 1;
|
5053 | }
|
5054 | return out;
|
5055 | });
|
5056 |
|
5057 | /**
|
5058 | * Creates a new list out of the two supplied by applying the function to each
|
5059 | * equally-positioned pair in the lists. The returned list is truncated to the
|
5060 | * length of the shorter of the two input lists.
|
5061 | *
|
5062 | * @function
|
5063 | * @memberOf R
|
5064 | * @since v0.1.0
|
5065 | * @category List
|
5066 | * @sig (a,b -> c) -> [a] -> [b] -> [c]
|
5067 | * @param {Function} fn The function used to combine the two elements into one value.
|
5068 | * @param {Array} list1 The first array to consider.
|
5069 | * @param {Array} list2 The second array to consider.
|
5070 | * @return {Array} The list made by combining same-indexed elements of `list1` and `list2`
|
5071 | * using `fn`.
|
5072 | * @example
|
5073 | *
|
5074 | * var f = (x, y) => {
|
5075 | * // ...
|
5076 | * };
|
5077 | * R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
|
5078 | * //=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
|
5079 | * @symb R.zipWith(fn, [a, b, c], [d, e, f]) = [fn(a, d), fn(b, e), fn(c, f)]
|
5080 | */
|
5081 | var zipWith = _curry3(function zipWith(fn, a, b) {
|
5082 | var rv = [];
|
5083 | var idx = 0;
|
5084 | var len = Math.min(a.length, b.length);
|
5085 | while (idx < len) {
|
5086 | rv[idx] = fn(a[idx], b[idx]);
|
5087 | idx += 1;
|
5088 | }
|
5089 | return rv;
|
5090 | });
|
5091 |
|
5092 | /**
|
5093 | * A function that always returns `false`. Any passed in parameters are ignored.
|
5094 | *
|
5095 | * @func
|
5096 | * @memberOf R
|
5097 | * @since v0.9.0
|
5098 | * @category Function
|
5099 | * @sig * -> Boolean
|
5100 | * @param {*}
|
5101 | * @return {Boolean}
|
5102 | * @see R.always, R.T
|
5103 | * @example
|
5104 | *
|
5105 | * R.F(); //=> false
|
5106 | */
|
5107 | var F = always(false);
|
5108 |
|
5109 | /**
|
5110 | * A function that always returns `true`. Any passed in parameters are ignored.
|
5111 | *
|
5112 | * @func
|
5113 | * @memberOf R
|
5114 | * @since v0.9.0
|
5115 | * @category Function
|
5116 | * @sig * -> Boolean
|
5117 | * @param {*}
|
5118 | * @return {Boolean}
|
5119 | * @see R.always, R.F
|
5120 | * @example
|
5121 | *
|
5122 | * R.T(); //=> true
|
5123 | */
|
5124 | var T = always(true);
|
5125 |
|
5126 | /**
|
5127 | * Copies an object.
|
5128 | *
|
5129 | * @private
|
5130 | * @param {*} value The value to be copied
|
5131 | * @param {Array} refFrom Array containing the source references
|
5132 | * @param {Array} refTo Array containing the copied source references
|
5133 | * @param {Boolean} deep Whether or not to perform deep cloning.
|
5134 | * @return {*} The copied value.
|
5135 | */
|
5136 | var _clone = function _clone(value, refFrom, refTo, deep) {
|
5137 | var copy = function copy(copiedValue) {
|
5138 | var len = refFrom.length;
|
5139 | var idx = 0;
|
5140 | while (idx < len) {
|
5141 | if (value === refFrom[idx]) {
|
5142 | return refTo[idx];
|
5143 | }
|
5144 | idx += 1;
|
5145 | }
|
5146 | refFrom[idx + 1] = value;
|
5147 | refTo[idx + 1] = copiedValue;
|
5148 | for (var key in value) {
|
5149 | copiedValue[key] = deep ? _clone(value[key], refFrom, refTo, true) : value[key];
|
5150 | }
|
5151 | return copiedValue;
|
5152 | };
|
5153 | switch (type(value)) {
|
5154 | case 'Object':
|
5155 | return copy({});
|
5156 | case 'Array':
|
5157 | return copy([]);
|
5158 | case 'Date':
|
5159 | return new Date(value.valueOf());
|
5160 | case 'RegExp':
|
5161 | return _cloneRegExp(value);
|
5162 | default:
|
5163 | return value;
|
5164 | }
|
5165 | };
|
5166 |
|
5167 | var _createPartialApplicator = function _createPartialApplicator(concat) {
|
5168 | return _curry2(function (fn, args) {
|
5169 | return _arity(Math.max(0, fn.length - args.length), function () {
|
5170 | return fn.apply(this, concat(args, arguments));
|
5171 | });
|
5172 | });
|
5173 | };
|
5174 |
|
5175 | var _dropLast = function dropLast(n, xs) {
|
5176 | return take(n < xs.length ? xs.length - n : 0, xs);
|
5177 | };
|
5178 |
|
5179 | // Values of other types are only equal if identical.
|
5180 | var _equals = function _equals(a, b, stackA, stackB) {
|
5181 | if (identical(a, b)) {
|
5182 | return true;
|
5183 | }
|
5184 | if (type(a) !== type(b)) {
|
5185 | return false;
|
5186 | }
|
5187 | if (a == null || b == null) {
|
5188 | return false;
|
5189 | }
|
5190 | if (typeof a.equals === 'function' || typeof b.equals === 'function') {
|
5191 | return typeof a.equals === 'function' && a.equals(b) && typeof b.equals === 'function' && b.equals(a);
|
5192 | }
|
5193 | switch (type(a)) {
|
5194 | case 'Arguments':
|
5195 | case 'Array':
|
5196 | case 'Object':
|
5197 | if (typeof a.constructor === 'function' && _functionName(a.constructor) === 'Promise') {
|
5198 | return a === b;
|
5199 | }
|
5200 | break;
|
5201 | case 'Boolean':
|
5202 | case 'Number':
|
5203 | case 'String':
|
5204 | if (!(typeof a === typeof b && identical(a.valueOf(), b.valueOf()))) {
|
5205 | return false;
|
5206 | }
|
5207 | break;
|
5208 | case 'Date':
|
5209 | if (!identical(a.valueOf(), b.valueOf())) {
|
5210 | return false;
|
5211 | }
|
5212 | break;
|
5213 | case 'Error':
|
5214 | return a.name === b.name && a.message === b.message;
|
5215 | case 'RegExp':
|
5216 | if (!(a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline && a.sticky === b.sticky && a.unicode === b.unicode)) {
|
5217 | return false;
|
5218 | }
|
5219 | break;
|
5220 | case 'Map':
|
5221 | case 'Set':
|
5222 | if (!_equals(_arrayFromIterator(a.entries()), _arrayFromIterator(b.entries()), stackA, stackB)) {
|
5223 | return false;
|
5224 | }
|
5225 | break;
|
5226 | case 'Int8Array':
|
5227 | case 'Uint8Array':
|
5228 | case 'Uint8ClampedArray':
|
5229 | case 'Int16Array':
|
5230 | case 'Uint16Array':
|
5231 | case 'Int32Array':
|
5232 | case 'Uint32Array':
|
5233 | case 'Float32Array':
|
5234 | case 'Float64Array':
|
5235 | break;
|
5236 | case 'ArrayBuffer':
|
5237 | break;
|
5238 | default:
|
5239 | // Values of other types are only equal if identical.
|
5240 | return false;
|
5241 | }
|
5242 | var keysA = keys(a);
|
5243 | if (keysA.length !== keys(b).length) {
|
5244 | return false;
|
5245 | }
|
5246 | var idx = stackA.length - 1;
|
5247 | while (idx >= 0) {
|
5248 | if (stackA[idx] === a) {
|
5249 | return stackB[idx] === b;
|
5250 | }
|
5251 | idx -= 1;
|
5252 | }
|
5253 | stackA.push(a);
|
5254 | stackB.push(b);
|
5255 | idx = keysA.length - 1;
|
5256 | while (idx >= 0) {
|
5257 | var key = keysA[idx];
|
5258 | if (!(_has(key, b) && _equals(b[key], a[key], stackA, stackB))) {
|
5259 | return false;
|
5260 | }
|
5261 | idx -= 1;
|
5262 | }
|
5263 | stackA.pop();
|
5264 | stackB.pop();
|
5265 | return true;
|
5266 | };
|
5267 |
|
5268 | /**
|
5269 | * `_makeFlat` is a helper function that returns a one-level or fully recursive
|
5270 | * function based on the flag passed in.
|
5271 | *
|
5272 | * @private
|
5273 | */
|
5274 | var _makeFlat = function _makeFlat(recursive) {
|
5275 | return function flatt(list) {
|
5276 | var value, jlen, j;
|
5277 | var result = [];
|
5278 | var idx = 0;
|
5279 | var ilen = list.length;
|
5280 | while (idx < ilen) {
|
5281 | if (isArrayLike(list[idx])) {
|
5282 | value = recursive ? flatt(list[idx]) : list[idx];
|
5283 | j = 0;
|
5284 | jlen = value.length;
|
5285 | while (j < jlen) {
|
5286 | result[result.length] = value[j];
|
5287 | j += 1;
|
5288 | }
|
5289 | } else {
|
5290 | result[result.length] = list[idx];
|
5291 | }
|
5292 | idx += 1;
|
5293 | }
|
5294 | return result;
|
5295 | };
|
5296 | };
|
5297 |
|
5298 | var _reduce = function () {
|
5299 | function _arrayReduce(xf, acc, list) {
|
5300 | var idx = 0;
|
5301 | var len = list.length;
|
5302 | while (idx < len) {
|
5303 | acc = xf['@@transducer/step'](acc, list[idx]);
|
5304 | if (acc && acc['@@transducer/reduced']) {
|
5305 | acc = acc['@@transducer/value'];
|
5306 | break;
|
5307 | }
|
5308 | idx += 1;
|
5309 | }
|
5310 | return xf['@@transducer/result'](acc);
|
5311 | }
|
5312 | function _iterableReduce(xf, acc, iter) {
|
5313 | var step = iter.next();
|
5314 | while (!step.done) {
|
5315 | acc = xf['@@transducer/step'](acc, step.value);
|
5316 | if (acc && acc['@@transducer/reduced']) {
|
5317 | acc = acc['@@transducer/value'];
|
5318 | break;
|
5319 | }
|
5320 | step = iter.next();
|
5321 | }
|
5322 | return xf['@@transducer/result'](acc);
|
5323 | }
|
5324 | function _methodReduce(xf, acc, obj) {
|
5325 | return xf['@@transducer/result'](obj.reduce(bind(xf['@@transducer/step'], xf), acc));
|
5326 | }
|
5327 | var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
|
5328 | return function _reduce(fn, acc, list) {
|
5329 | if (typeof fn === 'function') {
|
5330 | fn = _xwrap(fn);
|
5331 | }
|
5332 | if (isArrayLike(list)) {
|
5333 | return _arrayReduce(fn, acc, list);
|
5334 | }
|
5335 | if (typeof list.reduce === 'function') {
|
5336 | return _methodReduce(fn, acc, list);
|
5337 | }
|
5338 | if (list[symIterator] != null) {
|
5339 | return _iterableReduce(fn, acc, list[symIterator]());
|
5340 | }
|
5341 | if (typeof list.next === 'function') {
|
5342 | return _iterableReduce(fn, acc, list);
|
5343 | }
|
5344 | throw new TypeError('reduce: list must be array or iterable');
|
5345 | };
|
5346 | }();
|
5347 |
|
5348 | var _stepCat = function () {
|
5349 | var _stepCatArray = {
|
5350 | '@@transducer/init': Array,
|
5351 | '@@transducer/step': function (xs, x) {
|
5352 | xs.push(x);
|
5353 | return xs;
|
5354 | },
|
5355 | '@@transducer/result': _identity
|
5356 | };
|
5357 | var _stepCatString = {
|
5358 | '@@transducer/init': String,
|
5359 | '@@transducer/step': function (a, b) {
|
5360 | return a + b;
|
5361 | },
|
5362 | '@@transducer/result': _identity
|
5363 | };
|
5364 | var _stepCatObject = {
|
5365 | '@@transducer/init': Object,
|
5366 | '@@transducer/step': function (result, input) {
|
5367 | return _assign(result, isArrayLike(input) ? objOf(input[0], input[1]) : input);
|
5368 | },
|
5369 | '@@transducer/result': _identity
|
5370 | };
|
5371 | return function _stepCat(obj) {
|
5372 | if (_isTransformer(obj)) {
|
5373 | return obj;
|
5374 | }
|
5375 | if (isArrayLike(obj)) {
|
5376 | return _stepCatArray;
|
5377 | }
|
5378 | if (typeof obj === 'string') {
|
5379 | return _stepCatString;
|
5380 | }
|
5381 | if (typeof obj === 'object') {
|
5382 | return _stepCatObject;
|
5383 | }
|
5384 | throw new Error('Cannot create transformer for ' + obj);
|
5385 | };
|
5386 | }();
|
5387 |
|
5388 | var _xdropLastWhile = function () {
|
5389 | function XDropLastWhile(fn, xf) {
|
5390 | this.f = fn;
|
5391 | this.retained = [];
|
5392 | this.xf = xf;
|
5393 | }
|
5394 | XDropLastWhile.prototype['@@transducer/init'] = _xfBase.init;
|
5395 | XDropLastWhile.prototype['@@transducer/result'] = function (result) {
|
5396 | this.retained = null;
|
5397 | return this.xf['@@transducer/result'](result);
|
5398 | };
|
5399 | XDropLastWhile.prototype['@@transducer/step'] = function (result, input) {
|
5400 | return this.f(input) ? this.retain(result, input) : this.flush(result, input);
|
5401 | };
|
5402 | XDropLastWhile.prototype.flush = function (result, input) {
|
5403 | result = _reduce(this.xf['@@transducer/step'], result, this.retained);
|
5404 | this.retained = [];
|
5405 | return this.xf['@@transducer/step'](result, input);
|
5406 | };
|
5407 | XDropLastWhile.prototype.retain = function (result, input) {
|
5408 | this.retained.push(input);
|
5409 | return result;
|
5410 | };
|
5411 | return _curry2(function _xdropLastWhile(fn, xf) {
|
5412 | return new XDropLastWhile(fn, xf);
|
5413 | });
|
5414 | }();
|
5415 |
|
5416 | /**
|
5417 | * Creates a new list iteration function from an existing one by adding two new
|
5418 | * parameters to its callback function: the current index, and the entire list.
|
5419 | *
|
5420 | * This would turn, for instance, Ramda's simple `map` function into one that
|
5421 | * more closely resembles `Array.prototype.map`. Note that this will only work
|
5422 | * for functions in which the iteration callback function is the first
|
5423 | * parameter, and where the list is the last parameter. (This latter might be
|
5424 | * unimportant if the list parameter is not used.)
|
5425 | *
|
5426 | * @func
|
5427 | * @memberOf R
|
5428 | * @since v0.15.0
|
5429 | * @category Function
|
5430 | * @category List
|
5431 | * @sig ((a ... -> b) ... -> [a] -> *) -> (a ..., Int, [a] -> b) ... -> [a] -> *)
|
5432 | * @param {Function} fn A list iteration function that does not pass index or list to its callback
|
5433 | * @return {Function} An altered list iteration function that passes (item, index, list) to its callback
|
5434 | * @example
|
5435 | *
|
5436 | * var mapIndexed = R.addIndex(R.map);
|
5437 | * mapIndexed((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r']);
|
5438 | * //=> ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
|
5439 | */
|
5440 | var addIndex = _curry1(function addIndex(fn) {
|
5441 | return curryN(fn.length, function () {
|
5442 | var idx = 0;
|
5443 | var origFn = arguments[0];
|
5444 | var list = arguments[arguments.length - 1];
|
5445 | var args = Array.prototype.slice.call(arguments, 0);
|
5446 | args[0] = function () {
|
5447 | var result = origFn.apply(this, _concat(arguments, [
|
5448 | idx,
|
5449 | list
|
5450 | ]));
|
5451 | idx += 1;
|
5452 | return result;
|
5453 | };
|
5454 | return fn.apply(this, args);
|
5455 | });
|
5456 | });
|
5457 |
|
5458 | /**
|
5459 | * Wraps a function of any arity (including nullary) in a function that accepts
|
5460 | * exactly 2 parameters. Any extraneous parameters will not be passed to the
|
5461 | * supplied function.
|
5462 | *
|
5463 | * @func
|
5464 | * @memberOf R
|
5465 | * @since v0.2.0
|
5466 | * @category Function
|
5467 | * @sig (* -> c) -> (a, b -> c)
|
5468 | * @param {Function} fn The function to wrap.
|
5469 | * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
|
5470 | * arity 2.
|
5471 | * @example
|
5472 | *
|
5473 | * var takesThreeArgs = function(a, b, c) {
|
5474 | * return [a, b, c];
|
5475 | * };
|
5476 | * takesThreeArgs.length; //=> 3
|
5477 | * takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
|
5478 | *
|
5479 | * var takesTwoArgs = R.binary(takesThreeArgs);
|
5480 | * takesTwoArgs.length; //=> 2
|
5481 | * // Only 2 arguments are passed to the wrapped function
|
5482 | * takesTwoArgs(1, 2, 3); //=> [1, 2, undefined]
|
5483 | * @symb R.binary(f)(a, b, c) = f(a, b)
|
5484 | */
|
5485 | var binary = _curry1(function binary(fn) {
|
5486 | return nAry(2, fn);
|
5487 | });
|
5488 |
|
5489 | /**
|
5490 | * Creates a deep copy of the value which may contain (nested) `Array`s and
|
5491 | * `Object`s, `Number`s, `String`s, `Boolean`s and `Date`s. `Function`s are
|
5492 | * assigned by reference rather than copied
|
5493 | *
|
5494 | * Dispatches to a `clone` method if present.
|
5495 | *
|
5496 | * @func
|
5497 | * @memberOf R
|
5498 | * @since v0.1.0
|
5499 | * @category Object
|
5500 | * @sig {*} -> {*}
|
5501 | * @param {*} value The object or array to clone
|
5502 | * @return {*} A deeply cloned copy of `val`
|
5503 | * @example
|
5504 | *
|
5505 | * var objects = [{}, {}, {}];
|
5506 | * var objectsClone = R.clone(objects);
|
5507 | * objects === objectsClone; //=> false
|
5508 | * objects[0] === objectsClone[0]; //=> false
|
5509 | */
|
5510 | var clone = _curry1(function clone(value) {
|
5511 | return value != null && typeof value.clone === 'function' ? value.clone() : _clone(value, [], [], true);
|
5512 | });
|
5513 |
|
5514 | /**
|
5515 | * Returns a curried equivalent of the provided function. The curried function
|
5516 | * has two unusual capabilities. First, its arguments needn't be provided one
|
5517 | * at a time. If `f` is a ternary function and `g` is `R.curry(f)`, the
|
5518 | * following are equivalent:
|
5519 | *
|
5520 | * - `g(1)(2)(3)`
|
5521 | * - `g(1)(2, 3)`
|
5522 | * - `g(1, 2)(3)`
|
5523 | * - `g(1, 2, 3)`
|
5524 | *
|
5525 | * Secondly, the special placeholder value `R.__` may be used to specify
|
5526 | * "gaps", allowing partial application of any combination of arguments,
|
5527 | * regardless of their positions. If `g` is as above and `_` is `R.__`, the
|
5528 | * following are equivalent:
|
5529 | *
|
5530 | * - `g(1, 2, 3)`
|
5531 | * - `g(_, 2, 3)(1)`
|
5532 | * - `g(_, _, 3)(1)(2)`
|
5533 | * - `g(_, _, 3)(1, 2)`
|
5534 | * - `g(_, 2)(1)(3)`
|
5535 | * - `g(_, 2)(1, 3)`
|
5536 | * - `g(_, 2)(_, 3)(1)`
|
5537 | *
|
5538 | * @func
|
5539 | * @memberOf R
|
5540 | * @since v0.1.0
|
5541 | * @category Function
|
5542 | * @sig (* -> a) -> (* -> a)
|
5543 | * @param {Function} fn The function to curry.
|
5544 | * @return {Function} A new, curried function.
|
5545 | * @see R.curryN
|
5546 | * @example
|
5547 | *
|
5548 | * var addFourNumbers = (a, b, c, d) => a + b + c + d;
|
5549 | *
|
5550 | * var curriedAddFourNumbers = R.curry(addFourNumbers);
|
5551 | * var f = curriedAddFourNumbers(1, 2);
|
5552 | * var g = f(3);
|
5553 | * g(4); //=> 10
|
5554 | */
|
5555 | var curry = _curry1(function curry(fn) {
|
5556 | return curryN(fn.length, fn);
|
5557 | });
|
5558 |
|
5559 | /**
|
5560 | * Returns all but the first `n` elements of the given list, string, or
|
5561 | * transducer/transformer (or object with a `drop` method).
|
5562 | *
|
5563 | * Dispatches to the `drop` method of the second argument, if present.
|
5564 | *
|
5565 | * @func
|
5566 | * @memberOf R
|
5567 | * @since v0.1.0
|
5568 | * @category List
|
5569 | * @sig Number -> [a] -> [a]
|
5570 | * @sig Number -> String -> String
|
5571 | * @param {Number} n
|
5572 | * @param {[a]} list
|
5573 | * @return {[a]} A copy of list without the first `n` elements
|
5574 | * @see R.take, R.transduce, R.dropLast, R.dropWhile
|
5575 | * @example
|
5576 | *
|
5577 | * R.drop(1, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
|
5578 | * R.drop(2, ['foo', 'bar', 'baz']); //=> ['baz']
|
5579 | * R.drop(3, ['foo', 'bar', 'baz']); //=> []
|
5580 | * R.drop(4, ['foo', 'bar', 'baz']); //=> []
|
5581 | * R.drop(3, 'ramda'); //=> 'da'
|
5582 | */
|
5583 | var drop = _curry2(_dispatchable(['drop'], _xdrop, function drop(n, xs) {
|
5584 | return slice(Math.max(0, n), Infinity, xs);
|
5585 | }));
|
5586 |
|
5587 | /**
|
5588 | * Returns a list containing all but the last `n` elements of the given `list`.
|
5589 | *
|
5590 | * @func
|
5591 | * @memberOf R
|
5592 | * @since v0.16.0
|
5593 | * @category List
|
5594 | * @sig Number -> [a] -> [a]
|
5595 | * @sig Number -> String -> String
|
5596 | * @param {Number} n The number of elements of `list` to skip.
|
5597 | * @param {Array} list The list of elements to consider.
|
5598 | * @return {Array} A copy of the list with only the first `list.length - n` elements
|
5599 | * @see R.takeLast, R.drop, R.dropWhile, R.dropLastWhile
|
5600 | * @example
|
5601 | *
|
5602 | * R.dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
|
5603 | * R.dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo']
|
5604 | * R.dropLast(3, ['foo', 'bar', 'baz']); //=> []
|
5605 | * R.dropLast(4, ['foo', 'bar', 'baz']); //=> []
|
5606 | * R.dropLast(3, 'ramda'); //=> 'ra'
|
5607 | */
|
5608 | var dropLast = _curry2(_dispatchable([], _xdropLast, _dropLast));
|
5609 |
|
5610 | /**
|
5611 | * Returns a new list excluding all the tailing elements of a given list which
|
5612 | * satisfy the supplied predicate function. It passes each value from the right
|
5613 | * to the supplied predicate function, skipping elements until the predicate
|
5614 | * function returns a `falsy` value. The predicate function is applied to one argument:
|
5615 | * *(value)*.
|
5616 | *
|
5617 | * @func
|
5618 | * @memberOf R
|
5619 | * @since v0.16.0
|
5620 | * @category List
|
5621 | * @sig (a -> Boolean) -> [a] -> [a]
|
5622 | * @param {Function} predicate The function to be called on each element
|
5623 | * @param {Array} list The collection to iterate over.
|
5624 | * @return {Array} A new array without any trailing elements that return `falsy` values from the `predicate`.
|
5625 | * @see R.takeLastWhile, R.addIndex, R.drop, R.dropWhile
|
5626 | * @example
|
5627 | *
|
5628 | * var lteThree = x => x <= 3;
|
5629 | *
|
5630 | * R.dropLastWhile(lteThree, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3, 4]
|
5631 | */
|
5632 | var dropLastWhile = _curry2(_dispatchable([], _xdropLastWhile, _dropLastWhile));
|
5633 |
|
5634 | /**
|
5635 | * Returns `true` if its arguments are equivalent, `false` otherwise. Handles
|
5636 | * cyclical data structures.
|
5637 | *
|
5638 | * Dispatches symmetrically to the `equals` methods of both arguments, if
|
5639 | * present.
|
5640 | *
|
5641 | * @func
|
5642 | * @memberOf R
|
5643 | * @since v0.15.0
|
5644 | * @category Relation
|
5645 | * @sig a -> b -> Boolean
|
5646 | * @param {*} a
|
5647 | * @param {*} b
|
5648 | * @return {Boolean}
|
5649 | * @example
|
5650 | *
|
5651 | * R.equals(1, 1); //=> true
|
5652 | * R.equals(1, '1'); //=> false
|
5653 | * R.equals([1, 2, 3], [1, 2, 3]); //=> true
|
5654 | *
|
5655 | * var a = {}; a.v = a;
|
5656 | * var b = {}; b.v = b;
|
5657 | * R.equals(a, b); //=> true
|
5658 | */
|
5659 | var equals = _curry2(function equals(a, b) {
|
5660 | return _equals(a, b, [], []);
|
5661 | });
|
5662 |
|
5663 | /**
|
5664 | * Takes a predicate and a "filterable", and returns a new filterable of the
|
5665 | * same type containing the members of the given filterable which satisfy the
|
5666 | * given predicate.
|
5667 | *
|
5668 | * Dispatches to the `filter` method of the second argument, if present.
|
5669 | *
|
5670 | * Acts as a transducer if a transformer is given in list position.
|
5671 | *
|
5672 | * @func
|
5673 | * @memberOf R
|
5674 | * @since v0.1.0
|
5675 | * @category List
|
5676 | * @sig Filterable f => (a -> Boolean) -> f a -> f a
|
5677 | * @param {Function} pred
|
5678 | * @param {Array} filterable
|
5679 | * @return {Array}
|
5680 | * @see R.reject, R.transduce, R.addIndex
|
5681 | * @example
|
5682 | *
|
5683 | * var isEven = n => n % 2 === 0;
|
5684 | *
|
5685 | * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
|
5686 | *
|
5687 | * R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
|
5688 | */
|
5689 | // else
|
5690 | var filter = _curry2(_dispatchable(['filter'], _xfilter, function (pred, filterable) {
|
5691 | return _isObject(filterable) ? _reduce(function (acc, key) {
|
5692 | if (pred(filterable[key])) {
|
5693 | acc[key] = filterable[key];
|
5694 | }
|
5695 | return acc;
|
5696 | }, {}, keys(filterable)) : // else
|
5697 | _filter(pred, filterable);
|
5698 | }));
|
5699 |
|
5700 | /**
|
5701 | * Returns a new list by pulling every item out of it (and all its sub-arrays)
|
5702 | * and putting them in a new array, depth-first.
|
5703 | *
|
5704 | * @func
|
5705 | * @memberOf R
|
5706 | * @since v0.1.0
|
5707 | * @category List
|
5708 | * @sig [a] -> [b]
|
5709 | * @param {Array} list The array to consider.
|
5710 | * @return {Array} The flattened list.
|
5711 | * @see R.unnest
|
5712 | * @example
|
5713 | *
|
5714 | * R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
|
5715 | * //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
5716 | */
|
5717 | var flatten = _curry1(_makeFlat(true));
|
5718 |
|
5719 | /**
|
5720 | * Returns a new function much like the supplied one, except that the first two
|
5721 | * arguments' order is reversed.
|
5722 | *
|
5723 | * @func
|
5724 | * @memberOf R
|
5725 | * @since v0.1.0
|
5726 | * @category Function
|
5727 | * @sig (a -> b -> c -> ... -> z) -> (b -> a -> c -> ... -> z)
|
5728 | * @param {Function} fn The function to invoke with its first two parameters reversed.
|
5729 | * @return {*} The result of invoking `fn` with its first two parameters' order reversed.
|
5730 | * @example
|
5731 | *
|
5732 | * var mergeThree = (a, b, c) => [].concat(a, b, c);
|
5733 | *
|
5734 | * mergeThree(1, 2, 3); //=> [1, 2, 3]
|
5735 | *
|
5736 | * R.flip(mergeThree)(1, 2, 3); //=> [2, 1, 3]
|
5737 | * @symb R.flip(f)(a, b, c) = f(b, a, c)
|
5738 | */
|
5739 | var flip = _curry1(function flip(fn) {
|
5740 | return curry(function (a, b) {
|
5741 | var args = Array.prototype.slice.call(arguments, 0);
|
5742 | args[0] = b;
|
5743 | args[1] = a;
|
5744 | return fn.apply(this, args);
|
5745 | });
|
5746 | });
|
5747 |
|
5748 | /**
|
5749 | * Iterate over an input `object`, calling a provided function `fn` for each
|
5750 | * key and value in the object.
|
5751 | *
|
5752 | * `fn` receives three argument: *(value, key, obj)*.
|
5753 | *
|
5754 | * @func
|
5755 | * @memberOf R
|
5756 | * @since v0.23.0
|
5757 | * @category Object
|
5758 | * @sig ((a, String, StrMap a) -> Any) -> StrMap a -> StrMap a
|
5759 | * @param {Function} fn The function to invoke. Receives three argument, `value`, `key`, `obj`.
|
5760 | * @param {Object} obj The object to iterate over.
|
5761 | * @return {Object} The original object.
|
5762 | * @example
|
5763 | *
|
5764 | * var printKeyConcatValue = (value, key) => console.log(key + ':' + value);
|
5765 | * R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2}
|
5766 | * // logs x:1
|
5767 | * // logs y:2
|
5768 | * @symb R.forEachObjIndexed(f, {x: a, y: b}) = {x: a, y: b}
|
5769 | */
|
5770 | var forEachObjIndexed = _curry2(function forEachObjIndexed(fn, obj) {
|
5771 | var keyList = keys(obj);
|
5772 | var idx = 0;
|
5773 | while (idx < keyList.length) {
|
5774 | var key = keyList[idx];
|
5775 | fn(obj[key], key, obj);
|
5776 | idx += 1;
|
5777 | }
|
5778 | return obj;
|
5779 | });
|
5780 |
|
5781 | /**
|
5782 | * Returns the first element of the given list or string. In some libraries
|
5783 | * this function is named `first`.
|
5784 | *
|
5785 | * @func
|
5786 | * @memberOf R
|
5787 | * @since v0.1.0
|
5788 | * @category List
|
5789 | * @sig [a] -> a | Undefined
|
5790 | * @sig String -> String
|
5791 | * @param {Array|String} list
|
5792 | * @return {*}
|
5793 | * @see R.tail, R.init, R.last
|
5794 | * @example
|
5795 | *
|
5796 | * R.head(['fi', 'fo', 'fum']); //=> 'fi'
|
5797 | * R.head([]); //=> undefined
|
5798 | *
|
5799 | * R.head('abc'); //=> 'a'
|
5800 | * R.head(''); //=> ''
|
5801 | */
|
5802 | var head = nth(0);
|
5803 |
|
5804 | /**
|
5805 | * Returns all but the last element of the given list or string.
|
5806 | *
|
5807 | * @func
|
5808 | * @memberOf R
|
5809 | * @since v0.9.0
|
5810 | * @category List
|
5811 | * @sig [a] -> [a]
|
5812 | * @sig String -> String
|
5813 | * @param {*} list
|
5814 | * @return {*}
|
5815 | * @see R.last, R.head, R.tail
|
5816 | * @example
|
5817 | *
|
5818 | * R.init([1, 2, 3]); //=> [1, 2]
|
5819 | * R.init([1, 2]); //=> [1]
|
5820 | * R.init([1]); //=> []
|
5821 | * R.init([]); //=> []
|
5822 | *
|
5823 | * R.init('abc'); //=> 'ab'
|
5824 | * R.init('ab'); //=> 'a'
|
5825 | * R.init('a'); //=> ''
|
5826 | * R.init(''); //=> ''
|
5827 | */
|
5828 | var init = slice(0, -1);
|
5829 |
|
5830 | /**
|
5831 | * Combines two lists into a set (i.e. no duplicates) composed of those
|
5832 | * elements common to both lists. Duplication is determined according to the
|
5833 | * value returned by applying the supplied predicate to two list elements.
|
5834 | *
|
5835 | * @func
|
5836 | * @memberOf R
|
5837 | * @since v0.1.0
|
5838 | * @category Relation
|
5839 | * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
|
5840 | * @param {Function} pred A predicate function that determines whether
|
5841 | * the two supplied elements are equal.
|
5842 | * @param {Array} list1 One list of items to compare
|
5843 | * @param {Array} list2 A second list of items to compare
|
5844 | * @return {Array} A new list containing those elements common to both lists.
|
5845 | * @see R.intersection
|
5846 | * @example
|
5847 | *
|
5848 | * var buffaloSpringfield = [
|
5849 | * {id: 824, name: 'Richie Furay'},
|
5850 | * {id: 956, name: 'Dewey Martin'},
|
5851 | * {id: 313, name: 'Bruce Palmer'},
|
5852 | * {id: 456, name: 'Stephen Stills'},
|
5853 | * {id: 177, name: 'Neil Young'}
|
5854 | * ];
|
5855 | * var csny = [
|
5856 | * {id: 204, name: 'David Crosby'},
|
5857 | * {id: 456, name: 'Stephen Stills'},
|
5858 | * {id: 539, name: 'Graham Nash'},
|
5859 | * {id: 177, name: 'Neil Young'}
|
5860 | * ];
|
5861 | *
|
5862 | * R.intersectionWith(R.eqBy(R.prop('id')), buffaloSpringfield, csny);
|
5863 | * //=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}]
|
5864 | */
|
5865 | var intersectionWith = _curry3(function intersectionWith(pred, list1, list2) {
|
5866 | var lookupList, filteredList;
|
5867 | if (list1.length > list2.length) {
|
5868 | lookupList = list1;
|
5869 | filteredList = list2;
|
5870 | } else {
|
5871 | lookupList = list2;
|
5872 | filteredList = list1;
|
5873 | }
|
5874 | var results = [];
|
5875 | var idx = 0;
|
5876 | while (idx < filteredList.length) {
|
5877 | if (_containsWith(pred, filteredList[idx], lookupList)) {
|
5878 | results[results.length] = filteredList[idx];
|
5879 | }
|
5880 | idx += 1;
|
5881 | }
|
5882 | return uniqWith(pred, results);
|
5883 | });
|
5884 |
|
5885 | /**
|
5886 | * Transforms the items of the list with the transducer and appends the
|
5887 | * transformed items to the accumulator using an appropriate iterator function
|
5888 | * based on the accumulator type.
|
5889 | *
|
5890 | * The accumulator can be an array, string, object or a transformer. Iterated
|
5891 | * items will be appended to arrays and concatenated to strings. Objects will
|
5892 | * be merged directly or 2-item arrays will be merged as key, value pairs.
|
5893 | *
|
5894 | * The accumulator can also be a transformer object that provides a 2-arity
|
5895 | * reducing iterator function, step, 0-arity initial value function, init, and
|
5896 | * 1-arity result extraction function result. The step function is used as the
|
5897 | * iterator function in reduce. The result function is used to convert the
|
5898 | * final accumulator into the return type and in most cases is R.identity. The
|
5899 | * init function is used to provide the initial accumulator.
|
5900 | *
|
5901 | * The iteration is performed with R.reduce after initializing the transducer.
|
5902 | *
|
5903 | * @func
|
5904 | * @memberOf R
|
5905 | * @since v0.12.0
|
5906 | * @category List
|
5907 | * @sig a -> (b -> b) -> [c] -> a
|
5908 | * @param {*} acc The initial accumulator value.
|
5909 | * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
|
5910 | * @param {Array} list The list to iterate over.
|
5911 | * @return {*} The final, accumulated value.
|
5912 | * @example
|
5913 | *
|
5914 | * var numbers = [1, 2, 3, 4];
|
5915 | * var transducer = R.compose(R.map(R.add(1)), R.take(2));
|
5916 | *
|
5917 | * R.into([], transducer, numbers); //=> [2, 3]
|
5918 | *
|
5919 | * var intoArray = R.into([]);
|
5920 | * intoArray(transducer, numbers); //=> [2, 3]
|
5921 | */
|
5922 | var into = _curry3(function into(acc, xf, list) {
|
5923 | return _isTransformer(acc) ? _reduce(xf(acc), acc['@@transducer/init'](), list) : _reduce(xf(_stepCat(acc)), _clone(acc, [], [], false), list);
|
5924 | });
|
5925 |
|
5926 | /**
|
5927 | * Same as R.invertObj, however this accounts for objects with duplicate values
|
5928 | * by putting the values into an array.
|
5929 | *
|
5930 | * @func
|
5931 | * @memberOf R
|
5932 | * @since v0.9.0
|
5933 | * @category Object
|
5934 | * @sig {s: x} -> {x: [ s, ... ]}
|
5935 | * @param {Object} obj The object or array to invert
|
5936 | * @return {Object} out A new object with keys
|
5937 | * in an array.
|
5938 | * @example
|
5939 | *
|
5940 | * var raceResultsByFirstName = {
|
5941 | * first: 'alice',
|
5942 | * second: 'jake',
|
5943 | * third: 'alice',
|
5944 | * };
|
5945 | * R.invert(raceResultsByFirstName);
|
5946 | * //=> { 'alice': ['first', 'third'], 'jake':['second'] }
|
5947 | */
|
5948 | var invert = _curry1(function invert(obj) {
|
5949 | var props = keys(obj);
|
5950 | var len = props.length;
|
5951 | var idx = 0;
|
5952 | var out = {};
|
5953 | while (idx < len) {
|
5954 | var key = props[idx];
|
5955 | var val = obj[key];
|
5956 | var list = _has(val, out) ? out[val] : out[val] = [];
|
5957 | list[list.length] = key;
|
5958 | idx += 1;
|
5959 | }
|
5960 | return out;
|
5961 | });
|
5962 |
|
5963 | /**
|
5964 | * Returns a new object with the keys of the given object as values, and the
|
5965 | * values of the given object, which are coerced to strings, as keys. Note
|
5966 | * that the last key found is preferred when handling the same value.
|
5967 | *
|
5968 | * @func
|
5969 | * @memberOf R
|
5970 | * @since v0.9.0
|
5971 | * @category Object
|
5972 | * @sig {s: x} -> {x: s}
|
5973 | * @param {Object} obj The object or array to invert
|
5974 | * @return {Object} out A new object
|
5975 | * @example
|
5976 | *
|
5977 | * var raceResults = {
|
5978 | * first: 'alice',
|
5979 | * second: 'jake'
|
5980 | * };
|
5981 | * R.invertObj(raceResults);
|
5982 | * //=> { 'alice': 'first', 'jake':'second' }
|
5983 | *
|
5984 | * // Alternatively:
|
5985 | * var raceResults = ['alice', 'jake'];
|
5986 | * R.invertObj(raceResults);
|
5987 | * //=> { 'alice': '0', 'jake':'1' }
|
5988 | */
|
5989 | var invertObj = _curry1(function invertObj(obj) {
|
5990 | var props = keys(obj);
|
5991 | var len = props.length;
|
5992 | var idx = 0;
|
5993 | var out = {};
|
5994 | while (idx < len) {
|
5995 | var key = props[idx];
|
5996 | out[obj[key]] = key;
|
5997 | idx += 1;
|
5998 | }
|
5999 | return out;
|
6000 | });
|
6001 |
|
6002 | /**
|
6003 | * Returns `true` if the given value is its type's empty value; `false`
|
6004 | * otherwise.
|
6005 | *
|
6006 | * @func
|
6007 | * @memberOf R
|
6008 | * @since v0.1.0
|
6009 | * @category Logic
|
6010 | * @sig a -> Boolean
|
6011 | * @param {*} x
|
6012 | * @return {Boolean}
|
6013 | * @see R.empty
|
6014 | * @example
|
6015 | *
|
6016 | * R.isEmpty([1, 2, 3]); //=> false
|
6017 | * R.isEmpty([]); //=> true
|
6018 | * R.isEmpty(''); //=> true
|
6019 | * R.isEmpty(null); //=> false
|
6020 | * R.isEmpty({}); //=> true
|
6021 | * R.isEmpty({length: 0}); //=> false
|
6022 | */
|
6023 | var isEmpty = _curry1(function isEmpty(x) {
|
6024 | return x != null && equals(x, empty(x));
|
6025 | });
|
6026 |
|
6027 | /**
|
6028 | * Returns the last element of the given list or string.
|
6029 | *
|
6030 | * @func
|
6031 | * @memberOf R
|
6032 | * @since v0.1.4
|
6033 | * @category List
|
6034 | * @sig [a] -> a | Undefined
|
6035 | * @sig String -> String
|
6036 | * @param {*} list
|
6037 | * @return {*}
|
6038 | * @see R.init, R.head, R.tail
|
6039 | * @example
|
6040 | *
|
6041 | * R.last(['fi', 'fo', 'fum']); //=> 'fum'
|
6042 | * R.last([]); //=> undefined
|
6043 | *
|
6044 | * R.last('abc'); //=> 'c'
|
6045 | * R.last(''); //=> ''
|
6046 | */
|
6047 | var last = nth(-1);
|
6048 |
|
6049 | /**
|
6050 | * Returns the position of the last occurrence of an item in an array, or -1 if
|
6051 | * the item is not included in the array. `R.equals` is used to determine
|
6052 | * equality.
|
6053 | *
|
6054 | * @func
|
6055 | * @memberOf R
|
6056 | * @since v0.1.0
|
6057 | * @category List
|
6058 | * @sig a -> [a] -> Number
|
6059 | * @param {*} target The item to find.
|
6060 | * @param {Array} xs The array to search in.
|
6061 | * @return {Number} the index of the target, or -1 if the target is not found.
|
6062 | * @see R.indexOf
|
6063 | * @example
|
6064 | *
|
6065 | * R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6
|
6066 | * R.lastIndexOf(10, [1,2,3,4]); //=> -1
|
6067 | */
|
6068 | var lastIndexOf = _curry2(function lastIndexOf(target, xs) {
|
6069 | if (typeof xs.lastIndexOf === 'function' && !_isArray(xs)) {
|
6070 | return xs.lastIndexOf(target);
|
6071 | } else {
|
6072 | var idx = xs.length - 1;
|
6073 | while (idx >= 0) {
|
6074 | if (equals(xs[idx], target)) {
|
6075 | return idx;
|
6076 | }
|
6077 | idx -= 1;
|
6078 | }
|
6079 | return -1;
|
6080 | }
|
6081 | });
|
6082 |
|
6083 | /**
|
6084 | * Takes a function and
|
6085 | * a [functor](https://github.com/fantasyland/fantasy-land#functor),
|
6086 | * applies the function to each of the functor's values, and returns
|
6087 | * a functor of the same shape.
|
6088 | *
|
6089 | * Ramda provides suitable `map` implementations for `Array` and `Object`,
|
6090 | * so this function may be applied to `[1, 2, 3]` or `{x: 1, y: 2, z: 3}`.
|
6091 | *
|
6092 | * Dispatches to the `map` method of the second argument, if present.
|
6093 | *
|
6094 | * Acts as a transducer if a transformer is given in list position.
|
6095 | *
|
6096 | * Also treats functions as functors and will compose them together.
|
6097 | *
|
6098 | * @func
|
6099 | * @memberOf R
|
6100 | * @since v0.1.0
|
6101 | * @category List
|
6102 | * @sig Functor f => (a -> b) -> f a -> f b
|
6103 | * @param {Function} fn The function to be called on every element of the input `list`.
|
6104 | * @param {Array} list The list to be iterated over.
|
6105 | * @return {Array} The new list.
|
6106 | * @see R.transduce, R.addIndex
|
6107 | * @example
|
6108 | *
|
6109 | * var double = x => x * 2;
|
6110 | *
|
6111 | * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
|
6112 | *
|
6113 | * R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
|
6114 | * @symb R.map(f, [a, b]) = [f(a), f(b)]
|
6115 | * @symb R.map(f, { x: a, y: b }) = { x: f(a), y: f(b) }
|
6116 | * @symb R.map(f, functor_o) = functor_o.map(f)
|
6117 | */
|
6118 | var map = _curry2(_dispatchable(['map'], _xmap, function map(fn, functor) {
|
6119 | switch (Object.prototype.toString.call(functor)) {
|
6120 | case '[object Function]':
|
6121 | return curryN(functor.length, function () {
|
6122 | return fn.call(this, functor.apply(this, arguments));
|
6123 | });
|
6124 | case '[object Object]':
|
6125 | return _reduce(function (acc, key) {
|
6126 | acc[key] = fn(functor[key]);
|
6127 | return acc;
|
6128 | }, {}, keys(functor));
|
6129 | default:
|
6130 | return _map(fn, functor);
|
6131 | }
|
6132 | }));
|
6133 |
|
6134 | /**
|
6135 | * An Object-specific version of `map`. The function is applied to three
|
6136 | * arguments: *(value, key, obj)*. If only the value is significant, use
|
6137 | * `map` instead.
|
6138 | *
|
6139 | * @func
|
6140 | * @memberOf R
|
6141 | * @since v0.9.0
|
6142 | * @category Object
|
6143 | * @sig ((*, String, Object) -> *) -> Object -> Object
|
6144 | * @param {Function} fn
|
6145 | * @param {Object} obj
|
6146 | * @return {Object}
|
6147 | * @see R.map
|
6148 | * @example
|
6149 | *
|
6150 | * var values = { x: 1, y: 2, z: 3 };
|
6151 | * var prependKeyAndDouble = (num, key, obj) => key + (num * 2);
|
6152 | *
|
6153 | * R.mapObjIndexed(prependKeyAndDouble, values); //=> { x: 'x2', y: 'y4', z: 'z6' }
|
6154 | */
|
6155 | var mapObjIndexed = _curry2(function mapObjIndexed(fn, obj) {
|
6156 | return _reduce(function (acc, key) {
|
6157 | acc[key] = fn(obj[key], key, obj);
|
6158 | return acc;
|
6159 | }, {}, keys(obj));
|
6160 | });
|
6161 |
|
6162 | /**
|
6163 | * Creates a new object with the own properties of the two provided objects. If
|
6164 | * a key exists in both objects, the provided function is applied to the values
|
6165 | * associated with the key in each object, with the result being used as the
|
6166 | * value associated with the key in the returned object. The key will be
|
6167 | * excluded from the returned object if the resulting value is `undefined`.
|
6168 | *
|
6169 | * @func
|
6170 | * @memberOf R
|
6171 | * @since v0.19.0
|
6172 | * @category Object
|
6173 | * @sig (a -> a -> a) -> {a} -> {a} -> {a}
|
6174 | * @param {Function} fn
|
6175 | * @param {Object} l
|
6176 | * @param {Object} r
|
6177 | * @return {Object}
|
6178 | * @see R.merge, R.mergeWithKey
|
6179 | * @example
|
6180 | *
|
6181 | * R.mergeWith(R.concat,
|
6182 | * { a: true, values: [10, 20] },
|
6183 | * { b: true, values: [15, 35] });
|
6184 | * //=> { a: true, b: true, values: [10, 20, 15, 35] }
|
6185 | */
|
6186 | var mergeWith = _curry3(function mergeWith(fn, l, r) {
|
6187 | return mergeWithKey(function (_, _l, _r) {
|
6188 | return fn(_l, _r);
|
6189 | }, l, r);
|
6190 | });
|
6191 |
|
6192 | /**
|
6193 | * Takes a function `f` and a list of arguments, and returns a function `g`.
|
6194 | * When applied, `g` returns the result of applying `f` to the arguments
|
6195 | * provided initially followed by the arguments provided to `g`.
|
6196 | *
|
6197 | * @func
|
6198 | * @memberOf R
|
6199 | * @since v0.10.0
|
6200 | * @category Function
|
6201 | * @sig ((a, b, c, ..., n) -> x) -> [a, b, c, ...] -> ((d, e, f, ..., n) -> x)
|
6202 | * @param {Function} f
|
6203 | * @param {Array} args
|
6204 | * @return {Function}
|
6205 | * @see R.partialRight
|
6206 | * @example
|
6207 | *
|
6208 | * var multiply2 = (a, b) => a * b;
|
6209 | * var double = R.partial(multiply2, [2]);
|
6210 | * double(2); //=> 4
|
6211 | *
|
6212 | * var greet = (salutation, title, firstName, lastName) =>
|
6213 | * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
|
6214 | *
|
6215 | * var sayHello = R.partial(greet, ['Hello']);
|
6216 | * var sayHelloToMs = R.partial(sayHello, ['Ms.']);
|
6217 | * sayHelloToMs('Jane', 'Jones'); //=> 'Hello, Ms. Jane Jones!'
|
6218 | * @symb R.partial(f, [a, b])(c, d) = f(a, b, c, d)
|
6219 | */
|
6220 | var partial = _createPartialApplicator(_concat);
|
6221 |
|
6222 | /**
|
6223 | * Takes a function `f` and a list of arguments, and returns a function `g`.
|
6224 | * When applied, `g` returns the result of applying `f` to the arguments
|
6225 | * provided to `g` followed by the arguments provided initially.
|
6226 | *
|
6227 | * @func
|
6228 | * @memberOf R
|
6229 | * @since v0.10.0
|
6230 | * @category Function
|
6231 | * @sig ((a, b, c, ..., n) -> x) -> [d, e, f, ..., n] -> ((a, b, c, ...) -> x)
|
6232 | * @param {Function} f
|
6233 | * @param {Array} args
|
6234 | * @return {Function}
|
6235 | * @see R.partial
|
6236 | * @example
|
6237 | *
|
6238 | * var greet = (salutation, title, firstName, lastName) =>
|
6239 | * salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
|
6240 | *
|
6241 | * var greetMsJaneJones = R.partialRight(greet, ['Ms.', 'Jane', 'Jones']);
|
6242 | *
|
6243 | * greetMsJaneJones('Hello'); //=> 'Hello, Ms. Jane Jones!'
|
6244 | * @symb R.partialRight(f, [a, b])(c, d) = f(c, d, a, b)
|
6245 | */
|
6246 | var partialRight = _createPartialApplicator(flip(_concat));
|
6247 |
|
6248 | /**
|
6249 | * Determines whether a nested path on an object has a specific value, in
|
6250 | * `R.equals` terms. Most likely used to filter a list.
|
6251 | *
|
6252 | * @func
|
6253 | * @memberOf R
|
6254 | * @since v0.7.0
|
6255 | * @category Relation
|
6256 | * @typedefn Idx = String | Int
|
6257 | * @sig [Idx] -> a -> {a} -> Boolean
|
6258 | * @param {Array} path The path of the nested property to use
|
6259 | * @param {*} val The value to compare the nested property with
|
6260 | * @param {Object} obj The object to check the nested property in
|
6261 | * @return {Boolean} `true` if the value equals the nested object property,
|
6262 | * `false` otherwise.
|
6263 | * @example
|
6264 | *
|
6265 | * var user1 = { address: { zipCode: 90210 } };
|
6266 | * var user2 = { address: { zipCode: 55555 } };
|
6267 | * var user3 = { name: 'Bob' };
|
6268 | * var users = [ user1, user2, user3 ];
|
6269 | * var isFamous = R.pathEq(['address', 'zipCode'], 90210);
|
6270 | * R.filter(isFamous, users); //=> [ user1 ]
|
6271 | */
|
6272 | var pathEq = _curry3(function pathEq(_path, val, obj) {
|
6273 | return equals(path(_path, obj), val);
|
6274 | });
|
6275 |
|
6276 | /**
|
6277 | * Returns a new list by plucking the same named property off all objects in
|
6278 | * the list supplied.
|
6279 | *
|
6280 | * @func
|
6281 | * @memberOf R
|
6282 | * @since v0.1.0
|
6283 | * @category List
|
6284 | * @sig k -> [{k: v}] -> [v]
|
6285 | * @param {Number|String} key The key name to pluck off of each object.
|
6286 | * @param {Array} list The array to consider.
|
6287 | * @return {Array} The list of values for the given key.
|
6288 | * @see R.props
|
6289 | * @example
|
6290 | *
|
6291 | * R.pluck('a')([{a: 1}, {a: 2}]); //=> [1, 2]
|
6292 | * R.pluck(0)([[1, 2], [3, 4]]); //=> [1, 3]
|
6293 | * @symb R.pluck('x', [{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}]) = [1, 3, 5]
|
6294 | * @symb R.pluck(0, [[1, 2], [3, 4], [5, 6]]) = [1, 3, 5]
|
6295 | */
|
6296 | var pluck = _curry2(function pluck(p, list) {
|
6297 | return map(prop(p), list);
|
6298 | });
|
6299 |
|
6300 | /**
|
6301 | * Reasonable analog to SQL `select` statement.
|
6302 | *
|
6303 | * @func
|
6304 | * @memberOf R
|
6305 | * @since v0.1.0
|
6306 | * @category Object
|
6307 | * @category Relation
|
6308 | * @sig [k] -> [{k: v}] -> [{k: v}]
|
6309 | * @param {Array} props The property names to project
|
6310 | * @param {Array} objs The objects to query
|
6311 | * @return {Array} An array of objects with just the `props` properties.
|
6312 | * @example
|
6313 | *
|
6314 | * var abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2};
|
6315 | * var fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7};
|
6316 | * var kids = [abby, fred];
|
6317 | * R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
|
6318 | */
|
6319 | // passing `identity` gives correct arity
|
6320 | var project = useWith(_map, [
|
6321 | pickAll,
|
6322 | identity
|
6323 | ]);
|
6324 |
|
6325 | /**
|
6326 | * Returns `true` if the specified object property is equal, in `R.equals`
|
6327 | * terms, to the given value; `false` otherwise.
|
6328 | *
|
6329 | * @func
|
6330 | * @memberOf R
|
6331 | * @since v0.1.0
|
6332 | * @category Relation
|
6333 | * @sig String -> a -> Object -> Boolean
|
6334 | * @param {String} name
|
6335 | * @param {*} val
|
6336 | * @param {*} obj
|
6337 | * @return {Boolean}
|
6338 | * @see R.equals, R.propSatisfies
|
6339 | * @example
|
6340 | *
|
6341 | * var abby = {name: 'Abby', age: 7, hair: 'blond'};
|
6342 | * var fred = {name: 'Fred', age: 12, hair: 'brown'};
|
6343 | * var rusty = {name: 'Rusty', age: 10, hair: 'brown'};
|
6344 | * var alois = {name: 'Alois', age: 15, disposition: 'surly'};
|
6345 | * var kids = [abby, fred, rusty, alois];
|
6346 | * var hasBrownHair = R.propEq('hair', 'brown');
|
6347 | * R.filter(hasBrownHair, kids); //=> [fred, rusty]
|
6348 | */
|
6349 | var propEq = _curry3(function propEq(name, val, obj) {
|
6350 | return equals(val, obj[name]);
|
6351 | });
|
6352 |
|
6353 | /**
|
6354 | * Returns a single item by iterating through the list, successively calling
|
6355 | * the iterator function and passing it an accumulator value and the current
|
6356 | * value from the array, and then passing the result to the next call.
|
6357 | *
|
6358 | * The iterator function receives two values: *(acc, value)*. It may use
|
6359 | * `R.reduced` to shortcut the iteration.
|
6360 | *
|
6361 | * The arguments' order of `reduceRight`'s iterator function is *(value, acc)*.
|
6362 | *
|
6363 | * Note: `R.reduce` does not skip deleted or unassigned indices (sparse
|
6364 | * arrays), unlike the native `Array.prototype.reduce` method. For more details
|
6365 | * on this behavior, see:
|
6366 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
|
6367 | *
|
6368 | * Dispatches to the `reduce` method of the third argument, if present.
|
6369 | *
|
6370 | * @func
|
6371 | * @memberOf R
|
6372 | * @since v0.1.0
|
6373 | * @category List
|
6374 | * @sig ((a, b) -> a) -> a -> [b] -> a
|
6375 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
6376 | * current element from the array.
|
6377 | * @param {*} acc The accumulator value.
|
6378 | * @param {Array} list The list to iterate over.
|
6379 | * @return {*} The final, accumulated value.
|
6380 | * @see R.reduced, R.addIndex, R.reduceRight
|
6381 | * @example
|
6382 | *
|
6383 | * R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
|
6384 | * - -10
|
6385 | * / \ / \
|
6386 | * - 4 -6 4
|
6387 | * / \ / \
|
6388 | * - 3 ==> -3 3
|
6389 | * / \ / \
|
6390 | * - 2 -1 2
|
6391 | * / \ / \
|
6392 | * 0 1 0 1
|
6393 | *
|
6394 | * @symb R.reduce(f, a, [b, c, d]) = f(f(f(a, b), c), d)
|
6395 | */
|
6396 | var reduce = _curry3(_reduce);
|
6397 |
|
6398 | /**
|
6399 | * Groups the elements of the list according to the result of calling
|
6400 | * the String-returning function `keyFn` on each element and reduces the elements
|
6401 | * of each group to a single value via the reducer function `valueFn`.
|
6402 | *
|
6403 | * This function is basically a more general `groupBy` function.
|
6404 | *
|
6405 | * Acts as a transducer if a transformer is given in list position.
|
6406 | *
|
6407 | * @func
|
6408 | * @memberOf R
|
6409 | * @since v0.20.0
|
6410 | * @category List
|
6411 | * @sig ((a, b) -> a) -> a -> (b -> String) -> [b] -> {String: a}
|
6412 | * @param {Function} valueFn The function that reduces the elements of each group to a single
|
6413 | * value. Receives two values, accumulator for a particular group and the current element.
|
6414 | * @param {*} acc The (initial) accumulator value for each group.
|
6415 | * @param {Function} keyFn The function that maps the list's element into a key.
|
6416 | * @param {Array} list The array to group.
|
6417 | * @return {Object} An object with the output of `keyFn` for keys, mapped to the output of
|
6418 | * `valueFn` for elements which produced that key when passed to `keyFn`.
|
6419 | * @see R.groupBy, R.reduce
|
6420 | * @example
|
6421 | *
|
6422 | * var reduceToNamesBy = R.reduceBy((acc, student) => acc.concat(student.name), []);
|
6423 | * var namesByGrade = reduceToNamesBy(function(student) {
|
6424 | * var score = student.score;
|
6425 | * return score < 65 ? 'F' :
|
6426 | * score < 70 ? 'D' :
|
6427 | * score < 80 ? 'C' :
|
6428 | * score < 90 ? 'B' : 'A';
|
6429 | * });
|
6430 | * var students = [{name: 'Lucy', score: 92},
|
6431 | * {name: 'Drew', score: 85},
|
6432 | * // ...
|
6433 | * {name: 'Bart', score: 62}];
|
6434 | * namesByGrade(students);
|
6435 | * // {
|
6436 | * // 'A': ['Lucy'],
|
6437 | * // 'B': ['Drew']
|
6438 | * // // ...,
|
6439 | * // 'F': ['Bart']
|
6440 | * // }
|
6441 | */
|
6442 | var reduceBy = _curryN(4, [], _dispatchable([], _xreduceBy, function reduceBy(valueFn, valueAcc, keyFn, list) {
|
6443 | return _reduce(function (acc, elt) {
|
6444 | var key = keyFn(elt);
|
6445 | acc[key] = valueFn(_has(key, acc) ? acc[key] : valueAcc, elt);
|
6446 | return acc;
|
6447 | }, {}, list);
|
6448 | }));
|
6449 |
|
6450 | /**
|
6451 | * Like `reduce`, `reduceWhile` returns a single item by iterating through
|
6452 | * the list, successively calling the iterator function. `reduceWhile` also
|
6453 | * takes a predicate that is evaluated before each step. If the predicate returns
|
6454 | * `false`, it "short-circuits" the iteration and returns the current value
|
6455 | * of the accumulator.
|
6456 | *
|
6457 | * @func
|
6458 | * @memberOf R
|
6459 | * @since v0.22.0
|
6460 | * @category List
|
6461 | * @sig ((a, b) -> Boolean) -> ((a, b) -> a) -> a -> [b] -> a
|
6462 | * @param {Function} pred The predicate. It is passed the accumulator and the
|
6463 | * current element.
|
6464 | * @param {Function} fn The iterator function. Receives two values, the
|
6465 | * accumulator and the current element.
|
6466 | * @param {*} a The accumulator value.
|
6467 | * @param {Array} list The list to iterate over.
|
6468 | * @return {*} The final, accumulated value.
|
6469 | * @see R.reduce, R.reduced
|
6470 | * @example
|
6471 | *
|
6472 | * var isOdd = (acc, x) => x % 2 === 1;
|
6473 | * var xs = [1, 3, 5, 60, 777, 800];
|
6474 | * R.reduceWhile(isOdd, R.add, 0, xs); //=> 9
|
6475 | *
|
6476 | * var ys = [2, 4, 6]
|
6477 | * R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
|
6478 | */
|
6479 | var reduceWhile = _curryN(4, [], function _reduceWhile(pred, fn, a, list) {
|
6480 | return _reduce(function (acc, x) {
|
6481 | return pred(acc, x) ? fn(acc, x) : _reduced(acc);
|
6482 | }, a, list);
|
6483 | });
|
6484 |
|
6485 | /**
|
6486 | * The complement of `filter`.
|
6487 | *
|
6488 | * Acts as a transducer if a transformer is given in list position.
|
6489 | *
|
6490 | * @func
|
6491 | * @memberOf R
|
6492 | * @since v0.1.0
|
6493 | * @category List
|
6494 | * @sig Filterable f => (a -> Boolean) -> f a -> f a
|
6495 | * @param {Function} pred
|
6496 | * @param {Array} filterable
|
6497 | * @return {Array}
|
6498 | * @see R.filter, R.transduce, R.addIndex
|
6499 | * @example
|
6500 | *
|
6501 | * var isOdd = (n) => n % 2 === 1;
|
6502 | *
|
6503 | * R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
|
6504 | *
|
6505 | * R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
|
6506 | */
|
6507 | var reject = _curry2(function reject(pred, filterable) {
|
6508 | return filter(_complement(pred), filterable);
|
6509 | });
|
6510 |
|
6511 | /**
|
6512 | * Returns a fixed list of size `n` containing a specified identical value.
|
6513 | *
|
6514 | * @func
|
6515 | * @memberOf R
|
6516 | * @since v0.1.1
|
6517 | * @category List
|
6518 | * @sig a -> n -> [a]
|
6519 | * @param {*} value The value to repeat.
|
6520 | * @param {Number} n The desired size of the output list.
|
6521 | * @return {Array} A new array containing `n` `value`s.
|
6522 | * @example
|
6523 | *
|
6524 | * R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
|
6525 | *
|
6526 | * var obj = {};
|
6527 | * var repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}]
|
6528 | * repeatedObjs[0] === repeatedObjs[1]; //=> true
|
6529 | * @symb R.repeat(a, 0) = []
|
6530 | * @symb R.repeat(a, 1) = [a]
|
6531 | * @symb R.repeat(a, 2) = [a, a]
|
6532 | */
|
6533 | var repeat = _curry2(function repeat(value, n) {
|
6534 | return times(always(value), n);
|
6535 | });
|
6536 |
|
6537 | /**
|
6538 | * Adds together all the elements of a list.
|
6539 | *
|
6540 | * @func
|
6541 | * @memberOf R
|
6542 | * @since v0.1.0
|
6543 | * @category Math
|
6544 | * @sig [Number] -> Number
|
6545 | * @param {Array} list An array of numbers
|
6546 | * @return {Number} The sum of all the numbers in the list.
|
6547 | * @see R.reduce
|
6548 | * @example
|
6549 | *
|
6550 | * R.sum([2,4,6,8,100,1]); //=> 121
|
6551 | */
|
6552 | var sum = reduce(add, 0);
|
6553 |
|
6554 | /**
|
6555 | * Returns a new list containing the last `n` elements of the given list.
|
6556 | * If `n > list.length`, returns a list of `list.length` elements.
|
6557 | *
|
6558 | * @func
|
6559 | * @memberOf R
|
6560 | * @since v0.16.0
|
6561 | * @category List
|
6562 | * @sig Number -> [a] -> [a]
|
6563 | * @sig Number -> String -> String
|
6564 | * @param {Number} n The number of elements to return.
|
6565 | * @param {Array} xs The collection to consider.
|
6566 | * @return {Array}
|
6567 | * @see R.dropLast
|
6568 | * @example
|
6569 | *
|
6570 | * R.takeLast(1, ['foo', 'bar', 'baz']); //=> ['baz']
|
6571 | * R.takeLast(2, ['foo', 'bar', 'baz']); //=> ['bar', 'baz']
|
6572 | * R.takeLast(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
|
6573 | * R.takeLast(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
|
6574 | * R.takeLast(3, 'ramda'); //=> 'mda'
|
6575 | */
|
6576 | var takeLast = _curry2(function takeLast(n, xs) {
|
6577 | return drop(n >= 0 ? xs.length - n : 0, xs);
|
6578 | });
|
6579 |
|
6580 | /**
|
6581 | * Initializes a transducer using supplied iterator function. Returns a single
|
6582 | * item by iterating through the list, successively calling the transformed
|
6583 | * iterator function and passing it an accumulator value and the current value
|
6584 | * from the array, and then passing the result to the next call.
|
6585 | *
|
6586 | * The iterator function receives two values: *(acc, value)*. It will be
|
6587 | * wrapped as a transformer to initialize the transducer. A transformer can be
|
6588 | * passed directly in place of an iterator function. In both cases, iteration
|
6589 | * may be stopped early with the `R.reduced` function.
|
6590 | *
|
6591 | * A transducer is a function that accepts a transformer and returns a
|
6592 | * transformer and can be composed directly.
|
6593 | *
|
6594 | * A transformer is an an object that provides a 2-arity reducing iterator
|
6595 | * function, step, 0-arity initial value function, init, and 1-arity result
|
6596 | * extraction function, result. The step function is used as the iterator
|
6597 | * function in reduce. The result function is used to convert the final
|
6598 | * accumulator into the return type and in most cases is R.identity. The init
|
6599 | * function can be used to provide an initial accumulator, but is ignored by
|
6600 | * transduce.
|
6601 | *
|
6602 | * The iteration is performed with R.reduce after initializing the transducer.
|
6603 | *
|
6604 | * @func
|
6605 | * @memberOf R
|
6606 | * @since v0.12.0
|
6607 | * @category List
|
6608 | * @sig (c -> c) -> (a,b -> a) -> a -> [b] -> a
|
6609 | * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
|
6610 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
6611 | * current element from the array. Wrapped as transformer, if necessary, and used to
|
6612 | * initialize the transducer
|
6613 | * @param {*} acc The initial accumulator value.
|
6614 | * @param {Array} list The list to iterate over.
|
6615 | * @return {*} The final, accumulated value.
|
6616 | * @see R.reduce, R.reduced, R.into
|
6617 | * @example
|
6618 | *
|
6619 | * var numbers = [1, 2, 3, 4];
|
6620 | * var transducer = R.compose(R.map(R.add(1)), R.take(2));
|
6621 | *
|
6622 | * R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3]
|
6623 | */
|
6624 | var transduce = curryN(4, function transduce(xf, fn, acc, list) {
|
6625 | return _reduce(xf(typeof fn === 'function' ? _xwrap(fn) : fn), acc, list);
|
6626 | });
|
6627 |
|
6628 | /**
|
6629 | * Combines two lists into a set (i.e. no duplicates) composed of the elements
|
6630 | * of each list. Duplication is determined according to the value returned by
|
6631 | * applying the supplied predicate to two list elements.
|
6632 | *
|
6633 | * @func
|
6634 | * @memberOf R
|
6635 | * @since v0.1.0
|
6636 | * @category Relation
|
6637 | * @sig (a -> a -> Boolean) -> [*] -> [*] -> [*]
|
6638 | * @param {Function} pred A predicate used to test whether two items are equal.
|
6639 | * @param {Array} list1 The first list.
|
6640 | * @param {Array} list2 The second list.
|
6641 | * @return {Array} The first and second lists concatenated, with
|
6642 | * duplicates removed.
|
6643 | * @see R.union
|
6644 | * @example
|
6645 | *
|
6646 | * var l1 = [{a: 1}, {a: 2}];
|
6647 | * var l2 = [{a: 1}, {a: 4}];
|
6648 | * R.unionWith(R.eqBy(R.prop('a')), l1, l2); //=> [{a: 1}, {a: 2}, {a: 4}]
|
6649 | */
|
6650 | var unionWith = _curry3(function unionWith(pred, list1, list2) {
|
6651 | return uniqWith(pred, _concat(list1, list2));
|
6652 | });
|
6653 |
|
6654 | /**
|
6655 | * Takes a spec object and a test object; returns true if the test satisfies
|
6656 | * the spec, false otherwise. An object satisfies the spec if, for each of the
|
6657 | * spec's own properties, accessing that property of the object gives the same
|
6658 | * value (in `R.equals` terms) as accessing that property of the spec.
|
6659 | *
|
6660 | * `whereEq` is a specialization of [`where`](#where).
|
6661 | *
|
6662 | * @func
|
6663 | * @memberOf R
|
6664 | * @since v0.14.0
|
6665 | * @category Object
|
6666 | * @sig {String: *} -> {String: *} -> Boolean
|
6667 | * @param {Object} spec
|
6668 | * @param {Object} testObj
|
6669 | * @return {Boolean}
|
6670 | * @see R.where
|
6671 | * @example
|
6672 | *
|
6673 | * // pred :: Object -> Boolean
|
6674 | * var pred = R.whereEq({a: 1, b: 2});
|
6675 | *
|
6676 | * pred({a: 1}); //=> false
|
6677 | * pred({a: 1, b: 2}); //=> true
|
6678 | * pred({a: 1, b: 2, c: 3}); //=> true
|
6679 | * pred({a: 1, b: 1}); //=> false
|
6680 | */
|
6681 | var whereEq = _curry2(function whereEq(spec, testObj) {
|
6682 | return where(map(equals, spec), testObj);
|
6683 | });
|
6684 |
|
6685 | var _flatCat = function () {
|
6686 | var preservingReduced = function (xf) {
|
6687 | return {
|
6688 | '@@transducer/init': _xfBase.init,
|
6689 | '@@transducer/result': function (result) {
|
6690 | return xf['@@transducer/result'](result);
|
6691 | },
|
6692 | '@@transducer/step': function (result, input) {
|
6693 | var ret = xf['@@transducer/step'](result, input);
|
6694 | return ret['@@transducer/reduced'] ? _forceReduced(ret) : ret;
|
6695 | }
|
6696 | };
|
6697 | };
|
6698 | return function _xcat(xf) {
|
6699 | var rxf = preservingReduced(xf);
|
6700 | return {
|
6701 | '@@transducer/init': _xfBase.init,
|
6702 | '@@transducer/result': function (result) {
|
6703 | return rxf['@@transducer/result'](result);
|
6704 | },
|
6705 | '@@transducer/step': function (result, input) {
|
6706 | return !isArrayLike(input) ? _reduce(rxf, result, [input]) : _reduce(rxf, result, input);
|
6707 | }
|
6708 | };
|
6709 | };
|
6710 | }();
|
6711 |
|
6712 | // Array.prototype.indexOf doesn't exist below IE9
|
6713 | // manually crawl the list to distinguish between +0 and -0
|
6714 | // NaN
|
6715 | // non-zero numbers can utilise Set
|
6716 | // all these types can utilise Set
|
6717 | // null can utilise Set
|
6718 | // anything else not covered above, defer to R.equals
|
6719 | var _indexOf = function _indexOf(list, a, idx) {
|
6720 | var inf, item;
|
6721 | // Array.prototype.indexOf doesn't exist below IE9
|
6722 | if (typeof list.indexOf === 'function') {
|
6723 | switch (typeof a) {
|
6724 | case 'number':
|
6725 | if (a === 0) {
|
6726 | // manually crawl the list to distinguish between +0 and -0
|
6727 | inf = 1 / a;
|
6728 | while (idx < list.length) {
|
6729 | item = list[idx];
|
6730 | if (item === 0 && 1 / item === inf) {
|
6731 | return idx;
|
6732 | }
|
6733 | idx += 1;
|
6734 | }
|
6735 | return -1;
|
6736 | } else if (a !== a) {
|
6737 | // NaN
|
6738 | while (idx < list.length) {
|
6739 | item = list[idx];
|
6740 | if (typeof item === 'number' && item !== item) {
|
6741 | return idx;
|
6742 | }
|
6743 | idx += 1;
|
6744 | }
|
6745 | return -1;
|
6746 | }
|
6747 | // non-zero numbers can utilise Set
|
6748 | return list.indexOf(a, idx);
|
6749 | // all these types can utilise Set
|
6750 | case 'string':
|
6751 | case 'boolean':
|
6752 | case 'function':
|
6753 | case 'undefined':
|
6754 | return list.indexOf(a, idx);
|
6755 | case 'object':
|
6756 | if (a === null) {
|
6757 | // null can utilise Set
|
6758 | return list.indexOf(a, idx);
|
6759 | }
|
6760 | }
|
6761 | }
|
6762 | // anything else not covered above, defer to R.equals
|
6763 | while (idx < list.length) {
|
6764 | if (equals(list[idx], a)) {
|
6765 | return idx;
|
6766 | }
|
6767 | idx += 1;
|
6768 | }
|
6769 | return -1;
|
6770 | };
|
6771 |
|
6772 | var _xchain = _curry2(function _xchain(f, xf) {
|
6773 | return map(f, _flatCat(xf));
|
6774 | });
|
6775 |
|
6776 | /**
|
6777 | * Takes a list of predicates and returns a predicate that returns true for a
|
6778 | * given list of arguments if every one of the provided predicates is satisfied
|
6779 | * by those arguments.
|
6780 | *
|
6781 | * The function returned is a curried function whose arity matches that of the
|
6782 | * highest-arity predicate.
|
6783 | *
|
6784 | * @func
|
6785 | * @memberOf R
|
6786 | * @since v0.9.0
|
6787 | * @category Logic
|
6788 | * @sig [(*... -> Boolean)] -> (*... -> Boolean)
|
6789 | * @param {Array} predicates An array of predicates to check
|
6790 | * @return {Function} The combined predicate
|
6791 | * @see R.anyPass
|
6792 | * @example
|
6793 | *
|
6794 | * var isQueen = R.propEq('rank', 'Q');
|
6795 | * var isSpade = R.propEq('suit', '♠︎');
|
6796 | * var isQueenOfSpades = R.allPass([isQueen, isSpade]);
|
6797 | *
|
6798 | * isQueenOfSpades({rank: 'Q', suit: '♣︎'}); //=> false
|
6799 | * isQueenOfSpades({rank: 'Q', suit: '♠︎'}); //=> true
|
6800 | */
|
6801 | var allPass = _curry1(function allPass(preds) {
|
6802 | return curryN(reduce(max, 0, pluck('length', preds)), function () {
|
6803 | var idx = 0;
|
6804 | var len = preds.length;
|
6805 | while (idx < len) {
|
6806 | if (!preds[idx].apply(this, arguments)) {
|
6807 | return false;
|
6808 | }
|
6809 | idx += 1;
|
6810 | }
|
6811 | return true;
|
6812 | });
|
6813 | });
|
6814 |
|
6815 | /**
|
6816 | * Takes a list of predicates and returns a predicate that returns true for a
|
6817 | * given list of arguments if at least one of the provided predicates is
|
6818 | * satisfied by those arguments.
|
6819 | *
|
6820 | * The function returned is a curried function whose arity matches that of the
|
6821 | * highest-arity predicate.
|
6822 | *
|
6823 | * @func
|
6824 | * @memberOf R
|
6825 | * @since v0.9.0
|
6826 | * @category Logic
|
6827 | * @sig [(*... -> Boolean)] -> (*... -> Boolean)
|
6828 | * @param {Array} predicates An array of predicates to check
|
6829 | * @return {Function} The combined predicate
|
6830 | * @see R.allPass
|
6831 | * @example
|
6832 | *
|
6833 | * var isClub = R.propEq('suit', '♣');
|
6834 | * var isSpade = R.propEq('suit', '♠');
|
6835 | * var isBlackCard = R.anyPass([isClub, isSpade]);
|
6836 | *
|
6837 | * isBlackCard({rank: '10', suit: '♣'}); //=> true
|
6838 | * isBlackCard({rank: 'Q', suit: '♠'}); //=> true
|
6839 | * isBlackCard({rank: 'Q', suit: '♦'}); //=> false
|
6840 | */
|
6841 | var anyPass = _curry1(function anyPass(preds) {
|
6842 | return curryN(reduce(max, 0, pluck('length', preds)), function () {
|
6843 | var idx = 0;
|
6844 | var len = preds.length;
|
6845 | while (idx < len) {
|
6846 | if (preds[idx].apply(this, arguments)) {
|
6847 | return true;
|
6848 | }
|
6849 | idx += 1;
|
6850 | }
|
6851 | return false;
|
6852 | });
|
6853 | });
|
6854 |
|
6855 | /**
|
6856 | * ap applies a list of functions to a list of values.
|
6857 | *
|
6858 | * Dispatches to the `ap` method of the second argument, if present. Also
|
6859 | * treats curried functions as applicatives.
|
6860 | *
|
6861 | * @func
|
6862 | * @memberOf R
|
6863 | * @since v0.3.0
|
6864 | * @category Function
|
6865 | * @sig [a -> b] -> [a] -> [b]
|
6866 | * @sig Apply f => f (a -> b) -> f a -> f b
|
6867 | * @param {Array} fns An array of functions
|
6868 | * @param {Array} vs An array of values
|
6869 | * @return {Array} An array of results of applying each of `fns` to all of `vs` in turn.
|
6870 | * @example
|
6871 | *
|
6872 | * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
|
6873 | * R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
|
6874 | * @symb R.ap([f, g], [a, b]) = [f(a), f(b), g(a), g(b)]
|
6875 | */
|
6876 | // else
|
6877 | var ap = _curry2(function ap(applicative, fn) {
|
6878 | return typeof applicative.ap === 'function' ? applicative.ap(fn) : typeof applicative === 'function' ? function (x) {
|
6879 | return applicative(x)(fn(x));
|
6880 | } : // else
|
6881 | _reduce(function (acc, f) {
|
6882 | return _concat(acc, map(f, fn));
|
6883 | }, [], applicative);
|
6884 | });
|
6885 |
|
6886 | /**
|
6887 | * Given a spec object recursively mapping properties to functions, creates a
|
6888 | * function producing an object of the same structure, by mapping each property
|
6889 | * to the result of calling its associated function with the supplied arguments.
|
6890 | *
|
6891 | * @func
|
6892 | * @memberOf R
|
6893 | * @since v0.20.0
|
6894 | * @category Function
|
6895 | * @sig {k: ((a, b, ..., m) -> v)} -> ((a, b, ..., m) -> {k: v})
|
6896 | * @param {Object} spec an object recursively mapping properties to functions for
|
6897 | * producing the values for these properties.
|
6898 | * @return {Function} A function that returns an object of the same structure
|
6899 | * as `spec', with each property set to the value returned by calling its
|
6900 | * associated function with the supplied arguments.
|
6901 | * @see R.converge, R.juxt
|
6902 | * @example
|
6903 | *
|
6904 | * var getMetrics = R.applySpec({
|
6905 | * sum: R.add,
|
6906 | * nested: { mul: R.multiply }
|
6907 | * });
|
6908 | * getMetrics(2, 4); // => { sum: 6, nested: { mul: 8 } }
|
6909 | * @symb R.applySpec({ x: f, y: { z: g } })(a, b) = { x: f(a, b), y: { z: g(a, b) } }
|
6910 | */
|
6911 | var applySpec = _curry1(function applySpec(spec) {
|
6912 | spec = map(function (v) {
|
6913 | return typeof v == 'function' ? v : applySpec(v);
|
6914 | }, spec);
|
6915 | return curryN(reduce(max, 0, pluck('length', values(spec))), function () {
|
6916 | var args = arguments;
|
6917 | return map(function (f) {
|
6918 | return apply(f, args);
|
6919 | }, spec);
|
6920 | });
|
6921 | });
|
6922 |
|
6923 | /**
|
6924 | * Returns the result of calling its first argument with the remaining
|
6925 | * arguments. This is occasionally useful as a converging function for
|
6926 | * `R.converge`: the left branch can produce a function while the right branch
|
6927 | * produces a value to be passed to that function as an argument.
|
6928 | *
|
6929 | * @func
|
6930 | * @memberOf R
|
6931 | * @since v0.9.0
|
6932 | * @category Function
|
6933 | * @sig (*... -> a),*... -> a
|
6934 | * @param {Function} fn The function to apply to the remaining arguments.
|
6935 | * @param {...*} args Any number of positional arguments.
|
6936 | * @return {*}
|
6937 | * @see R.apply
|
6938 | * @example
|
6939 | *
|
6940 | * R.call(R.add, 1, 2); //=> 3
|
6941 | *
|
6942 | * var indentN = R.pipe(R.times(R.always(' ')),
|
6943 | * R.join(''),
|
6944 | * R.replace(/^(?!$)/gm));
|
6945 | *
|
6946 | * var format = R.converge(R.call, [
|
6947 | * R.pipe(R.prop('indent'), indentN),
|
6948 | * R.prop('value')
|
6949 | * ]);
|
6950 | *
|
6951 | * format({indent: 2, value: 'foo\nbar\nbaz\n'}); //=> ' foo\n bar\n baz\n'
|
6952 | * @symb R.call(f, a, b) = f(a, b)
|
6953 | */
|
6954 | var call = curry(function call(fn) {
|
6955 | return fn.apply(this, Array.prototype.slice.call(arguments, 1));
|
6956 | });
|
6957 |
|
6958 | /**
|
6959 | * `chain` maps a function over a list and concatenates the results. `chain`
|
6960 | * is also known as `flatMap` in some libraries
|
6961 | *
|
6962 | * Dispatches to the `chain` method of the second argument, if present,
|
6963 | * according to the [FantasyLand Chain spec](https://github.com/fantasyland/fantasy-land#chain).
|
6964 | *
|
6965 | * @func
|
6966 | * @memberOf R
|
6967 | * @since v0.3.0
|
6968 | * @category List
|
6969 | * @sig Chain m => (a -> m b) -> m a -> m b
|
6970 | * @param {Function} fn The function to map with
|
6971 | * @param {Array} list The list to map over
|
6972 | * @return {Array} The result of flat-mapping `list` with `fn`
|
6973 | * @example
|
6974 | *
|
6975 | * var duplicate = n => [n, n];
|
6976 | * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
|
6977 | *
|
6978 | * R.chain(R.append, R.head)([1, 2, 3]); //=> [1, 2, 3, 1]
|
6979 | */
|
6980 | var chain = _curry2(_dispatchable(['chain'], _xchain, function chain(fn, monad) {
|
6981 | if (typeof monad === 'function') {
|
6982 | return function (x) {
|
6983 | return fn(monad(x))(x);
|
6984 | };
|
6985 | }
|
6986 | return _makeFlat(false)(map(fn, monad));
|
6987 | }));
|
6988 |
|
6989 | /**
|
6990 | * Returns a function, `fn`, which encapsulates `if/else, if/else, ...` logic.
|
6991 | * `R.cond` takes a list of [predicate, transformer] pairs. All of the arguments
|
6992 | * to `fn` are applied to each of the predicates in turn until one returns a
|
6993 | * "truthy" value, at which point `fn` returns the result of applying its
|
6994 | * arguments to the corresponding transformer. If none of the predicates
|
6995 | * matches, `fn` returns undefined.
|
6996 | *
|
6997 | * @func
|
6998 | * @memberOf R
|
6999 | * @since v0.6.0
|
7000 | * @category Logic
|
7001 | * @sig [[(*... -> Boolean),(*... -> *)]] -> (*... -> *)
|
7002 | * @param {Array} pairs A list of [predicate, transformer]
|
7003 | * @return {Function}
|
7004 | * @example
|
7005 | *
|
7006 | * var fn = R.cond([
|
7007 | * [R.equals(0), R.always('water freezes at 0°C')],
|
7008 | * [R.equals(100), R.always('water boils at 100°C')],
|
7009 | * [R.T, temp => 'nothing special happens at ' + temp + '°C']
|
7010 | * ]);
|
7011 | * fn(0); //=> 'water freezes at 0°C'
|
7012 | * fn(50); //=> 'nothing special happens at 50°C'
|
7013 | * fn(100); //=> 'water boils at 100°C'
|
7014 | */
|
7015 | var cond = _curry1(function cond(pairs) {
|
7016 | var arity = reduce(max, 0, map(function (pair) {
|
7017 | return pair[0].length;
|
7018 | }, pairs));
|
7019 | return _arity(arity, function () {
|
7020 | var idx = 0;
|
7021 | while (idx < pairs.length) {
|
7022 | if (pairs[idx][0].apply(this, arguments)) {
|
7023 | return pairs[idx][1].apply(this, arguments);
|
7024 | }
|
7025 | idx += 1;
|
7026 | }
|
7027 | });
|
7028 | });
|
7029 |
|
7030 | /**
|
7031 | * Wraps a constructor function inside a curried function that can be called
|
7032 | * with the same arguments and returns the same type. The arity of the function
|
7033 | * returned is specified to allow using variadic constructor functions.
|
7034 | *
|
7035 | * @func
|
7036 | * @memberOf R
|
7037 | * @since v0.4.0
|
7038 | * @category Function
|
7039 | * @sig Number -> (* -> {*}) -> (* -> {*})
|
7040 | * @param {Number} n The arity of the constructor function.
|
7041 | * @param {Function} Fn The constructor function to wrap.
|
7042 | * @return {Function} A wrapped, curried constructor function.
|
7043 | * @example
|
7044 | *
|
7045 | * // Variadic Constructor function
|
7046 | * function Salad() {
|
7047 | * this.ingredients = arguments;
|
7048 | * };
|
7049 | * Salad.prototype.recipe = function() {
|
7050 | * var instructions = R.map((ingredient) => (
|
7051 | * 'Add a whollop of ' + ingredient, this.ingredients)
|
7052 | * )
|
7053 | * return R.join('\n', instructions)
|
7054 | * }
|
7055 | *
|
7056 | * var ThreeLayerSalad = R.constructN(3, Salad)
|
7057 | *
|
7058 | * // Notice we no longer need the 'new' keyword, and the constructor is curried for 3 arguments.
|
7059 | * var salad = ThreeLayerSalad('Mayonnaise')('Potato Chips')('Ketchup')
|
7060 | * console.log(salad.recipe());
|
7061 | * // Add a whollop of Mayonnaise
|
7062 | * // Add a whollop of Potato Chips
|
7063 | * // Add a whollop of Potato Ketchup
|
7064 | */
|
7065 | var constructN = _curry2(function constructN(n, Fn) {
|
7066 | if (n > 10) {
|
7067 | throw new Error('Constructor with greater than ten arguments');
|
7068 | }
|
7069 | if (n === 0) {
|
7070 | return function () {
|
7071 | return new Fn();
|
7072 | };
|
7073 | }
|
7074 | return curry(nAry(n, function ($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
|
7075 | switch (arguments.length) {
|
7076 | case 1:
|
7077 | return new Fn($0);
|
7078 | case 2:
|
7079 | return new Fn($0, $1);
|
7080 | case 3:
|
7081 | return new Fn($0, $1, $2);
|
7082 | case 4:
|
7083 | return new Fn($0, $1, $2, $3);
|
7084 | case 5:
|
7085 | return new Fn($0, $1, $2, $3, $4);
|
7086 | case 6:
|
7087 | return new Fn($0, $1, $2, $3, $4, $5);
|
7088 | case 7:
|
7089 | return new Fn($0, $1, $2, $3, $4, $5, $6);
|
7090 | case 8:
|
7091 | return new Fn($0, $1, $2, $3, $4, $5, $6, $7);
|
7092 | case 9:
|
7093 | return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8);
|
7094 | case 10:
|
7095 | return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
|
7096 | }
|
7097 | }));
|
7098 | });
|
7099 |
|
7100 | /**
|
7101 | * Accepts a converging function and a list of branching functions and returns
|
7102 | * a new function. When invoked, this new function is applied to some
|
7103 | * arguments, each branching function is applied to those same arguments. The
|
7104 | * results of each branching function are passed as arguments to the converging
|
7105 | * function to produce the return value.
|
7106 | *
|
7107 | * @func
|
7108 | * @memberOf R
|
7109 | * @since v0.4.2
|
7110 | * @category Function
|
7111 | * @sig (x1 -> x2 -> ... -> z) -> [(a -> b -> ... -> x1), (a -> b -> ... -> x2), ...] -> (a -> b -> ... -> z)
|
7112 | * @param {Function} after A function. `after` will be invoked with the return values of
|
7113 | * `fn1` and `fn2` as its arguments.
|
7114 | * @param {Array} functions A list of functions.
|
7115 | * @return {Function} A new function.
|
7116 | * @see R.useWith
|
7117 | * @example
|
7118 | *
|
7119 | * var average = R.converge(R.divide, [R.sum, R.length])
|
7120 | * average([1, 2, 3, 4, 5, 6, 7]) //=> 4
|
7121 | *
|
7122 | * var strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
|
7123 | * strangeConcat("Yodel") //=> "YODELyodel"
|
7124 | *
|
7125 | * @symb R.converge(f, [g, h])(a, b) = f(g(a, b), h(a, b))
|
7126 | */
|
7127 | var converge = _curry2(function converge(after, fns) {
|
7128 | return curryN(reduce(max, 0, pluck('length', fns)), function () {
|
7129 | var args = arguments;
|
7130 | var context = this;
|
7131 | return after.apply(context, _map(function (fn) {
|
7132 | return fn.apply(context, args);
|
7133 | }, fns));
|
7134 | });
|
7135 | });
|
7136 |
|
7137 | /**
|
7138 | * Counts the elements of a list according to how many match each value of a
|
7139 | * key generated by the supplied function. Returns an object mapping the keys
|
7140 | * produced by `fn` to the number of occurrences in the list. Note that all
|
7141 | * keys are coerced to strings because of how JavaScript objects work.
|
7142 | *
|
7143 | * Acts as a transducer if a transformer is given in list position.
|
7144 | *
|
7145 | * @func
|
7146 | * @memberOf R
|
7147 | * @since v0.1.0
|
7148 | * @category Relation
|
7149 | * @sig (a -> String) -> [a] -> {*}
|
7150 | * @param {Function} fn The function used to map values to keys.
|
7151 | * @param {Array} list The list to count elements from.
|
7152 | * @return {Object} An object mapping keys to number of occurrences in the list.
|
7153 | * @example
|
7154 | *
|
7155 | * var numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
|
7156 | * R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
|
7157 | *
|
7158 | * var letters = ['a', 'b', 'A', 'a', 'B', 'c'];
|
7159 | * R.countBy(R.toLower)(letters); //=> {'a': 3, 'b': 2, 'c': 1}
|
7160 | */
|
7161 | var countBy = reduceBy(function (acc, elem) {
|
7162 | return acc + 1;
|
7163 | }, 0);
|
7164 |
|
7165 | /**
|
7166 | * Returns a new list without any consecutively repeating elements. Equality is
|
7167 | * determined by applying the supplied predicate to each pair of consecutive elements. The
|
7168 | * first element in a series of equal elements will be preserved.
|
7169 | *
|
7170 | * Acts as a transducer if a transformer is given in list position.
|
7171 | *
|
7172 | * @func
|
7173 | * @memberOf R
|
7174 | * @since v0.14.0
|
7175 | * @category List
|
7176 | * @sig (a, a -> Boolean) -> [a] -> [a]
|
7177 | * @param {Function} pred A predicate used to test whether two items are equal.
|
7178 | * @param {Array} list The array to consider.
|
7179 | * @return {Array} `list` without repeating elements.
|
7180 | * @see R.transduce
|
7181 | * @example
|
7182 | *
|
7183 | * var l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
|
7184 | * R.dropRepeatsWith(R.eqBy(Math.abs), l); //=> [1, 3, 4, -5, 3]
|
7185 | */
|
7186 | var dropRepeatsWith = _curry2(_dispatchable([], _xdropRepeatsWith, function dropRepeatsWith(pred, list) {
|
7187 | var result = [];
|
7188 | var idx = 1;
|
7189 | var len = list.length;
|
7190 | if (len !== 0) {
|
7191 | result[0] = list[0];
|
7192 | while (idx < len) {
|
7193 | if (!pred(last(result), list[idx])) {
|
7194 | result[result.length] = list[idx];
|
7195 | }
|
7196 | idx += 1;
|
7197 | }
|
7198 | }
|
7199 | return result;
|
7200 | }));
|
7201 |
|
7202 | /**
|
7203 | * Takes a function and two values in its domain and returns `true` if the
|
7204 | * values map to the same value in the codomain; `false` otherwise.
|
7205 | *
|
7206 | * @func
|
7207 | * @memberOf R
|
7208 | * @since v0.18.0
|
7209 | * @category Relation
|
7210 | * @sig (a -> b) -> a -> a -> Boolean
|
7211 | * @param {Function} f
|
7212 | * @param {*} x
|
7213 | * @param {*} y
|
7214 | * @return {Boolean}
|
7215 | * @example
|
7216 | *
|
7217 | * R.eqBy(Math.abs, 5, -5); //=> true
|
7218 | */
|
7219 | var eqBy = _curry3(function eqBy(f, x, y) {
|
7220 | return equals(f(x), f(y));
|
7221 | });
|
7222 |
|
7223 | /**
|
7224 | * Reports whether two objects have the same value, in `R.equals` terms, for
|
7225 | * the specified property. Useful as a curried predicate.
|
7226 | *
|
7227 | * @func
|
7228 | * @memberOf R
|
7229 | * @since v0.1.0
|
7230 | * @category Object
|
7231 | * @sig k -> {k: v} -> {k: v} -> Boolean
|
7232 | * @param {String} prop The name of the property to compare
|
7233 | * @param {Object} obj1
|
7234 | * @param {Object} obj2
|
7235 | * @return {Boolean}
|
7236 | *
|
7237 | * @example
|
7238 | *
|
7239 | * var o1 = { a: 1, b: 2, c: 3, d: 4 };
|
7240 | * var o2 = { a: 10, b: 20, c: 3, d: 40 };
|
7241 | * R.eqProps('a', o1, o2); //=> false
|
7242 | * R.eqProps('c', o1, o2); //=> true
|
7243 | */
|
7244 | var eqProps = _curry3(function eqProps(prop, obj1, obj2) {
|
7245 | return equals(obj1[prop], obj2[prop]);
|
7246 | });
|
7247 |
|
7248 | /**
|
7249 | * Splits a list into sub-lists stored in an object, based on the result of
|
7250 | * calling a String-returning function on each element, and grouping the
|
7251 | * results according to values returned.
|
7252 | *
|
7253 | * Dispatches to the `groupBy` method of the second argument, if present.
|
7254 | *
|
7255 | * Acts as a transducer if a transformer is given in list position.
|
7256 | *
|
7257 | * @func
|
7258 | * @memberOf R
|
7259 | * @since v0.1.0
|
7260 | * @category List
|
7261 | * @sig (a -> String) -> [a] -> {String: [a]}
|
7262 | * @param {Function} fn Function :: a -> String
|
7263 | * @param {Array} list The array to group
|
7264 | * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
|
7265 | * that produced that key when passed to `fn`.
|
7266 | * @see R.transduce
|
7267 | * @example
|
7268 | *
|
7269 | * var byGrade = R.groupBy(function(student) {
|
7270 | * var score = student.score;
|
7271 | * return score < 65 ? 'F' :
|
7272 | * score < 70 ? 'D' :
|
7273 | * score < 80 ? 'C' :
|
7274 | * score < 90 ? 'B' : 'A';
|
7275 | * });
|
7276 | * var students = [{name: 'Abby', score: 84},
|
7277 | * {name: 'Eddy', score: 58},
|
7278 | * // ...
|
7279 | * {name: 'Jack', score: 69}];
|
7280 | * byGrade(students);
|
7281 | * // {
|
7282 | * // 'A': [{name: 'Dianne', score: 99}],
|
7283 | * // 'B': [{name: 'Abby', score: 84}]
|
7284 | * // // ...,
|
7285 | * // 'F': [{name: 'Eddy', score: 58}]
|
7286 | * // }
|
7287 | */
|
7288 | var groupBy = _curry2(_checkForMethod('groupBy', reduceBy(function (acc, item) {
|
7289 | if (acc == null) {
|
7290 | acc = [];
|
7291 | }
|
7292 | acc.push(item);
|
7293 | return acc;
|
7294 | }, null)));
|
7295 |
|
7296 | /**
|
7297 | * Given a function that generates a key, turns a list of objects into an
|
7298 | * object indexing the objects by the given key. Note that if multiple
|
7299 | * objects generate the same value for the indexing key only the last value
|
7300 | * will be included in the generated object.
|
7301 | *
|
7302 | * Acts as a transducer if a transformer is given in list position.
|
7303 | *
|
7304 | * @func
|
7305 | * @memberOf R
|
7306 | * @since v0.19.0
|
7307 | * @category List
|
7308 | * @sig (a -> String) -> [{k: v}] -> {k: {k: v}}
|
7309 | * @param {Function} fn Function :: a -> String
|
7310 | * @param {Array} array The array of objects to index
|
7311 | * @return {Object} An object indexing each array element by the given property.
|
7312 | * @example
|
7313 | *
|
7314 | * var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
|
7315 | * R.indexBy(R.prop('id'), list);
|
7316 | * //=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
|
7317 | */
|
7318 | var indexBy = reduceBy(function (acc, elem) {
|
7319 | return elem;
|
7320 | }, null);
|
7321 |
|
7322 | /**
|
7323 | * Returns the position of the first occurrence of an item in an array, or -1
|
7324 | * if the item is not included in the array. `R.equals` is used to determine
|
7325 | * equality.
|
7326 | *
|
7327 | * @func
|
7328 | * @memberOf R
|
7329 | * @since v0.1.0
|
7330 | * @category List
|
7331 | * @sig a -> [a] -> Number
|
7332 | * @param {*} target The item to find.
|
7333 | * @param {Array} xs The array to search in.
|
7334 | * @return {Number} the index of the target, or -1 if the target is not found.
|
7335 | * @see R.lastIndexOf
|
7336 | * @example
|
7337 | *
|
7338 | * R.indexOf(3, [1,2,3,4]); //=> 2
|
7339 | * R.indexOf(10, [1,2,3,4]); //=> -1
|
7340 | */
|
7341 | var indexOf = _curry2(function indexOf(target, xs) {
|
7342 | return typeof xs.indexOf === 'function' && !_isArray(xs) ? xs.indexOf(target) : _indexOf(xs, target, 0);
|
7343 | });
|
7344 |
|
7345 | /**
|
7346 | * juxt applies a list of functions to a list of values.
|
7347 | *
|
7348 | * @func
|
7349 | * @memberOf R
|
7350 | * @since v0.19.0
|
7351 | * @category Function
|
7352 | * @sig [(a, b, ..., m) -> n] -> ((a, b, ..., m) -> [n])
|
7353 | * @param {Array} fns An array of functions
|
7354 | * @return {Function} A function that returns a list of values after applying each of the original `fns` to its parameters.
|
7355 | * @see R.applySpec
|
7356 | * @example
|
7357 | *
|
7358 | * var getRange = R.juxt([Math.min, Math.max]);
|
7359 | * getRange(3, 4, 9, -3); //=> [-3, 9]
|
7360 | * @symb R.juxt([f, g, h])(a, b) = [f(a, b), g(a, b), h(a, b)]
|
7361 | */
|
7362 | var juxt = _curry1(function juxt(fns) {
|
7363 | return converge(function () {
|
7364 | return Array.prototype.slice.call(arguments, 0);
|
7365 | }, fns);
|
7366 | });
|
7367 |
|
7368 | /**
|
7369 | * Returns a lens for the given getter and setter functions. The getter "gets"
|
7370 | * the value of the focus; the setter "sets" the value of the focus. The setter
|
7371 | * should not mutate the data structure.
|
7372 | *
|
7373 | * @func
|
7374 | * @memberOf R
|
7375 | * @since v0.8.0
|
7376 | * @category Object
|
7377 | * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
|
7378 | * @sig (s -> a) -> ((a, s) -> s) -> Lens s a
|
7379 | * @param {Function} getter
|
7380 | * @param {Function} setter
|
7381 | * @return {Lens}
|
7382 | * @see R.view, R.set, R.over, R.lensIndex, R.lensProp
|
7383 | * @example
|
7384 | *
|
7385 | * var xLens = R.lens(R.prop('x'), R.assoc('x'));
|
7386 | *
|
7387 | * R.view(xLens, {x: 1, y: 2}); //=> 1
|
7388 | * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
|
7389 | * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
|
7390 | */
|
7391 | var lens = _curry2(function lens(getter, setter) {
|
7392 | return function (toFunctorFn) {
|
7393 | return function (target) {
|
7394 | return map(function (focus) {
|
7395 | return setter(focus, target);
|
7396 | }, toFunctorFn(getter(target)));
|
7397 | };
|
7398 | };
|
7399 | });
|
7400 |
|
7401 | /**
|
7402 | * Returns a lens whose focus is the specified index.
|
7403 | *
|
7404 | * @func
|
7405 | * @memberOf R
|
7406 | * @since v0.14.0
|
7407 | * @category Object
|
7408 | * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
|
7409 | * @sig Number -> Lens s a
|
7410 | * @param {Number} n
|
7411 | * @return {Lens}
|
7412 | * @see R.view, R.set, R.over
|
7413 | * @example
|
7414 | *
|
7415 | * var headLens = R.lensIndex(0);
|
7416 | *
|
7417 | * R.view(headLens, ['a', 'b', 'c']); //=> 'a'
|
7418 | * R.set(headLens, 'x', ['a', 'b', 'c']); //=> ['x', 'b', 'c']
|
7419 | * R.over(headLens, R.toUpper, ['a', 'b', 'c']); //=> ['A', 'b', 'c']
|
7420 | */
|
7421 | var lensIndex = _curry1(function lensIndex(n) {
|
7422 | return lens(nth(n), update(n));
|
7423 | });
|
7424 |
|
7425 | /**
|
7426 | * Returns a lens whose focus is the specified path.
|
7427 | *
|
7428 | * @func
|
7429 | * @memberOf R
|
7430 | * @since v0.19.0
|
7431 | * @category Object
|
7432 | * @typedefn Idx = String | Int
|
7433 | * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
|
7434 | * @sig [Idx] -> Lens s a
|
7435 | * @param {Array} path The path to use.
|
7436 | * @return {Lens}
|
7437 | * @see R.view, R.set, R.over
|
7438 | * @example
|
7439 | *
|
7440 | * var xHeadYLens = R.lensPath(['x', 0, 'y']);
|
7441 | *
|
7442 | * R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
|
7443 | * //=> 2
|
7444 | * R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
|
7445 | * //=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]}
|
7446 | * R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]});
|
7447 | * //=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]}
|
7448 | */
|
7449 | var lensPath = _curry1(function lensPath(p) {
|
7450 | return lens(path(p), assocPath(p));
|
7451 | });
|
7452 |
|
7453 | /**
|
7454 | * Returns a lens whose focus is the specified property.
|
7455 | *
|
7456 | * @func
|
7457 | * @memberOf R
|
7458 | * @since v0.14.0
|
7459 | * @category Object
|
7460 | * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
|
7461 | * @sig String -> Lens s a
|
7462 | * @param {String} k
|
7463 | * @return {Lens}
|
7464 | * @see R.view, R.set, R.over
|
7465 | * @example
|
7466 | *
|
7467 | * var xLens = R.lensProp('x');
|
7468 | *
|
7469 | * R.view(xLens, {x: 1, y: 2}); //=> 1
|
7470 | * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
|
7471 | * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
|
7472 | */
|
7473 | var lensProp = _curry1(function lensProp(k) {
|
7474 | return lens(prop(k), assoc(k));
|
7475 | });
|
7476 |
|
7477 | /**
|
7478 | * "lifts" a function to be the specified arity, so that it may "map over" that
|
7479 | * many lists, Functions or other objects that satisfy the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
|
7480 | *
|
7481 | * @func
|
7482 | * @memberOf R
|
7483 | * @since v0.7.0
|
7484 | * @category Function
|
7485 | * @sig Number -> (*... -> *) -> ([*]... -> [*])
|
7486 | * @param {Function} fn The function to lift into higher context
|
7487 | * @return {Function} The lifted function.
|
7488 | * @see R.lift, R.ap
|
7489 | * @example
|
7490 | *
|
7491 | * var madd3 = R.liftN(3, (...args) => R.sum(args));
|
7492 | * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
|
7493 | */
|
7494 | var liftN = _curry2(function liftN(arity, fn) {
|
7495 | var lifted = curryN(arity, fn);
|
7496 | return curryN(arity, function () {
|
7497 | return _reduce(ap, map(lifted, arguments[0]), Array.prototype.slice.call(arguments, 1));
|
7498 | });
|
7499 | });
|
7500 |
|
7501 | /**
|
7502 | * Returns the mean of the given list of numbers.
|
7503 | *
|
7504 | * @func
|
7505 | * @memberOf R
|
7506 | * @since v0.14.0
|
7507 | * @category Math
|
7508 | * @sig [Number] -> Number
|
7509 | * @param {Array} list
|
7510 | * @return {Number}
|
7511 | * @example
|
7512 | *
|
7513 | * R.mean([2, 7, 9]); //=> 6
|
7514 | * R.mean([]); //=> NaN
|
7515 | */
|
7516 | var mean = _curry1(function mean(list) {
|
7517 | return sum(list) / list.length;
|
7518 | });
|
7519 |
|
7520 | /**
|
7521 | * Returns the median of the given list of numbers.
|
7522 | *
|
7523 | * @func
|
7524 | * @memberOf R
|
7525 | * @since v0.14.0
|
7526 | * @category Math
|
7527 | * @sig [Number] -> Number
|
7528 | * @param {Array} list
|
7529 | * @return {Number}
|
7530 | * @example
|
7531 | *
|
7532 | * R.median([2, 9, 7]); //=> 7
|
7533 | * R.median([7, 2, 10, 9]); //=> 8
|
7534 | * R.median([]); //=> NaN
|
7535 | */
|
7536 | var median = _curry1(function median(list) {
|
7537 | var len = list.length;
|
7538 | if (len === 0) {
|
7539 | return NaN;
|
7540 | }
|
7541 | var width = 2 - len % 2;
|
7542 | var idx = (len - width) / 2;
|
7543 | return mean(Array.prototype.slice.call(list, 0).sort(function (a, b) {
|
7544 | return a < b ? -1 : a > b ? 1 : 0;
|
7545 | }).slice(idx, idx + width));
|
7546 | });
|
7547 |
|
7548 | /**
|
7549 | * Takes a predicate and a list or other "filterable" object and returns the
|
7550 | * pair of filterable objects of the same type of elements which do and do not
|
7551 | * satisfy, the predicate, respectively.
|
7552 | *
|
7553 | * @func
|
7554 | * @memberOf R
|
7555 | * @since v0.1.4
|
7556 | * @category List
|
7557 | * @sig Filterable f => (a -> Boolean) -> f a -> [f a, f a]
|
7558 | * @param {Function} pred A predicate to determine which side the element belongs to.
|
7559 | * @param {Array} filterable the list (or other filterable) to partition.
|
7560 | * @return {Array} An array, containing first the subset of elements that satisfy the
|
7561 | * predicate, and second the subset of elements that do not satisfy.
|
7562 | * @see R.filter, R.reject
|
7563 | * @example
|
7564 | *
|
7565 | * R.partition(R.contains('s'), ['sss', 'ttt', 'foo', 'bars']);
|
7566 | * // => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
|
7567 | *
|
7568 | * R.partition(R.contains('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
|
7569 | * // => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]
|
7570 | */
|
7571 | var partition = juxt([
|
7572 | filter,
|
7573 | reject
|
7574 | ]);
|
7575 |
|
7576 | /**
|
7577 | * Performs left-to-right function composition. The leftmost function may have
|
7578 | * any arity; the remaining functions must be unary.
|
7579 | *
|
7580 | * In some libraries this function is named `sequence`.
|
7581 | *
|
7582 | * **Note:** The result of pipe is not automatically curried.
|
7583 | *
|
7584 | * @func
|
7585 | * @memberOf R
|
7586 | * @since v0.1.0
|
7587 | * @category Function
|
7588 | * @sig (((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)) -> ((a, b, ..., n) -> z)
|
7589 | * @param {...Function} functions
|
7590 | * @return {Function}
|
7591 | * @see R.compose
|
7592 | * @example
|
7593 | *
|
7594 | * var f = R.pipe(Math.pow, R.negate, R.inc);
|
7595 | *
|
7596 | * f(3, 4); // -(3^4) + 1
|
7597 | * @symb R.pipe(f, g, h)(a, b) = h(g(f(a, b)))
|
7598 | */
|
7599 | var pipe = function pipe() {
|
7600 | if (arguments.length === 0) {
|
7601 | throw new Error('pipe requires at least one argument');
|
7602 | }
|
7603 | return _arity(arguments[0].length, reduce(_pipe, arguments[0], tail(arguments)));
|
7604 | };
|
7605 |
|
7606 | /**
|
7607 | * Performs left-to-right composition of one or more Promise-returning
|
7608 | * functions. The leftmost function may have any arity; the remaining functions
|
7609 | * must be unary.
|
7610 | *
|
7611 | * @func
|
7612 | * @memberOf R
|
7613 | * @since v0.10.0
|
7614 | * @category Function
|
7615 | * @sig ((a -> Promise b), (b -> Promise c), ..., (y -> Promise z)) -> (a -> Promise z)
|
7616 | * @param {...Function} functions
|
7617 | * @return {Function}
|
7618 | * @see R.composeP
|
7619 | * @example
|
7620 | *
|
7621 | * // followersForUser :: String -> Promise [User]
|
7622 | * var followersForUser = R.pipeP(db.getUserById, db.getFollowers);
|
7623 | */
|
7624 | var pipeP = function pipeP() {
|
7625 | if (arguments.length === 0) {
|
7626 | throw new Error('pipeP requires at least one argument');
|
7627 | }
|
7628 | return _arity(arguments[0].length, reduce(_pipeP, arguments[0], tail(arguments)));
|
7629 | };
|
7630 |
|
7631 | /**
|
7632 | * Multiplies together all the elements of a list.
|
7633 | *
|
7634 | * @func
|
7635 | * @memberOf R
|
7636 | * @since v0.1.0
|
7637 | * @category Math
|
7638 | * @sig [Number] -> Number
|
7639 | * @param {Array} list An array of numbers
|
7640 | * @return {Number} The product of all the numbers in the list.
|
7641 | * @see R.reduce
|
7642 | * @example
|
7643 | *
|
7644 | * R.product([2,4,6,8,100,1]); //=> 38400
|
7645 | */
|
7646 | var product = reduce(multiply, 1);
|
7647 |
|
7648 | /**
|
7649 | * Transforms a [Traversable](https://github.com/fantasyland/fantasy-land#traversable)
|
7650 | * of [Applicative](https://github.com/fantasyland/fantasy-land#applicative) into an
|
7651 | * Applicative of Traversable.
|
7652 | *
|
7653 | * Dispatches to the `sequence` method of the second argument, if present.
|
7654 | *
|
7655 | * @func
|
7656 | * @memberOf R
|
7657 | * @since v0.19.0
|
7658 | * @category List
|
7659 | * @sig (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
|
7660 | * @param {Function} of
|
7661 | * @param {*} traversable
|
7662 | * @return {*}
|
7663 | * @see R.traverse
|
7664 | * @example
|
7665 | *
|
7666 | * R.sequence(Maybe.of, [Just(1), Just(2), Just(3)]); //=> Just([1, 2, 3])
|
7667 | * R.sequence(Maybe.of, [Just(1), Just(2), Nothing()]); //=> Nothing()
|
7668 | *
|
7669 | * R.sequence(R.of, Just([1, 2, 3])); //=> [Just(1), Just(2), Just(3)]
|
7670 | * R.sequence(R.of, Nothing()); //=> [Nothing()]
|
7671 | */
|
7672 | var sequence = _curry2(function sequence(of, traversable) {
|
7673 | return typeof traversable.sequence === 'function' ? traversable.sequence(of) : reduceRight(function (x, acc) {
|
7674 | return ap(map(prepend, x), acc);
|
7675 | }, of([]), traversable);
|
7676 | });
|
7677 |
|
7678 | /**
|
7679 | * Maps an [Applicative](https://github.com/fantasyland/fantasy-land#applicative)-returning
|
7680 | * function over a [Traversable](https://github.com/fantasyland/fantasy-land#traversable),
|
7681 | * then uses [`sequence`](#sequence) to transform the resulting Traversable of Applicative
|
7682 | * into an Applicative of Traversable.
|
7683 | *
|
7684 | * Dispatches to the `sequence` method of the third argument, if present.
|
7685 | *
|
7686 | * @func
|
7687 | * @memberOf R
|
7688 | * @since v0.19.0
|
7689 | * @category List
|
7690 | * @sig (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b)
|
7691 | * @param {Function} of
|
7692 | * @param {Function} f
|
7693 | * @param {*} traversable
|
7694 | * @return {*}
|
7695 | * @see R.sequence
|
7696 | * @example
|
7697 | *
|
7698 | * // Returns `Nothing` if the given divisor is `0`
|
7699 | * safeDiv = n => d => d === 0 ? Nothing() : Just(n / d)
|
7700 | *
|
7701 | * R.traverse(Maybe.of, safeDiv(10), [2, 4, 5]); //=> Just([5, 2.5, 2])
|
7702 | * R.traverse(Maybe.of, safeDiv(10), [2, 0, 5]); //=> Nothing
|
7703 | */
|
7704 | var traverse = _curry3(function traverse(of, f, traversable) {
|
7705 | return sequence(of, map(f, traversable));
|
7706 | });
|
7707 |
|
7708 | /**
|
7709 | * Shorthand for `R.chain(R.identity)`, which removes one level of nesting from
|
7710 | * any [Chain](https://github.com/fantasyland/fantasy-land#chain).
|
7711 | *
|
7712 | * @func
|
7713 | * @memberOf R
|
7714 | * @since v0.3.0
|
7715 | * @category List
|
7716 | * @sig Chain c => c (c a) -> c a
|
7717 | * @param {*} list
|
7718 | * @return {*}
|
7719 | * @see R.flatten, R.chain
|
7720 | * @example
|
7721 | *
|
7722 | * R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]]
|
7723 | * R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6]
|
7724 | */
|
7725 | var unnest = chain(_identity);
|
7726 |
|
7727 | var _contains = function _contains(a, list) {
|
7728 | return _indexOf(list, a, 0) >= 0;
|
7729 | };
|
7730 |
|
7731 | // mapPairs :: (Object, [String]) -> [String]
|
7732 | var _toString = function _toString(x, seen) {
|
7733 | var recur = function recur(y) {
|
7734 | var xs = seen.concat([x]);
|
7735 | return _contains(y, xs) ? '<Circular>' : _toString(y, xs);
|
7736 | };
|
7737 | // mapPairs :: (Object, [String]) -> [String]
|
7738 | var mapPairs = function (obj, keys) {
|
7739 | return _map(function (k) {
|
7740 | return _quote(k) + ': ' + recur(obj[k]);
|
7741 | }, keys.slice().sort());
|
7742 | };
|
7743 | switch (Object.prototype.toString.call(x)) {
|
7744 | case '[object Arguments]':
|
7745 | return '(function() { return arguments; }(' + _map(recur, x).join(', ') + '))';
|
7746 | case '[object Array]':
|
7747 | return '[' + _map(recur, x).concat(mapPairs(x, reject(function (k) {
|
7748 | return /^\d+$/.test(k);
|
7749 | }, keys(x)))).join(', ') + ']';
|
7750 | case '[object Boolean]':
|
7751 | return typeof x === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString();
|
7752 | case '[object Date]':
|
7753 | return 'new Date(' + (isNaN(x.valueOf()) ? recur(NaN) : _quote(_toISOString(x))) + ')';
|
7754 | case '[object Null]':
|
7755 | return 'null';
|
7756 | case '[object Number]':
|
7757 | return typeof x === 'object' ? 'new Number(' + recur(x.valueOf()) + ')' : 1 / x === -Infinity ? '-0' : x.toString(10);
|
7758 | case '[object String]':
|
7759 | return typeof x === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : _quote(x);
|
7760 | case '[object Undefined]':
|
7761 | return 'undefined';
|
7762 | default:
|
7763 | if (typeof x.toString === 'function') {
|
7764 | var repr = x.toString();
|
7765 | if (repr !== '[object Object]') {
|
7766 | return repr;
|
7767 | }
|
7768 | }
|
7769 | return '{' + mapPairs(x, keys(x)).join(', ') + '}';
|
7770 | }
|
7771 | };
|
7772 |
|
7773 | /**
|
7774 | * Performs right-to-left function composition. The rightmost function may have
|
7775 | * any arity; the remaining functions must be unary.
|
7776 | *
|
7777 | * **Note:** The result of compose is not automatically curried.
|
7778 | *
|
7779 | * @func
|
7780 | * @memberOf R
|
7781 | * @since v0.1.0
|
7782 | * @category Function
|
7783 | * @sig ((y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)) -> ((a, b, ..., n) -> z)
|
7784 | * @param {...Function} ...functions The functions to compose
|
7785 | * @return {Function}
|
7786 | * @see R.pipe
|
7787 | * @example
|
7788 | *
|
7789 | * var classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
|
7790 | * var yellGreeting = R.compose(R.toUpper, classyGreeting);
|
7791 | * yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
|
7792 | *
|
7793 | * R.compose(Math.abs, R.add(1), R.multiply(2))(-4) //=> 7
|
7794 | *
|
7795 | * @symb R.compose(f, g, h)(a, b) = f(g(h(a, b)))
|
7796 | */
|
7797 | var compose = function compose() {
|
7798 | if (arguments.length === 0) {
|
7799 | throw new Error('compose requires at least one argument');
|
7800 | }
|
7801 | return pipe.apply(this, reverse(arguments));
|
7802 | };
|
7803 |
|
7804 | /**
|
7805 | * Returns the right-to-left Kleisli composition of the provided functions,
|
7806 | * each of which must return a value of a type supported by [`chain`](#chain).
|
7807 | *
|
7808 | * `R.composeK(h, g, f)` is equivalent to `R.compose(R.chain(h), R.chain(g), R.chain(f))`.
|
7809 | *
|
7810 | * @func
|
7811 | * @memberOf R
|
7812 | * @since v0.16.0
|
7813 | * @category Function
|
7814 | * @sig Chain m => ((y -> m z), (x -> m y), ..., (a -> m b)) -> (a -> m z)
|
7815 | * @param {...Function} ...functions The functions to compose
|
7816 | * @return {Function}
|
7817 | * @see R.pipeK
|
7818 | * @example
|
7819 | *
|
7820 | * // get :: String -> Object -> Maybe *
|
7821 | * var get = R.curry((propName, obj) => Maybe(obj[propName]))
|
7822 | *
|
7823 | * // getStateCode :: Maybe String -> Maybe String
|
7824 | * var getStateCode = R.composeK(
|
7825 | * R.compose(Maybe.of, R.toUpper),
|
7826 | * get('state'),
|
7827 | * get('address'),
|
7828 | * get('user'),
|
7829 | * );
|
7830 | * getStateCode({"user":{"address":{"state":"ny"}}}); //=> Maybe.Just("NY")
|
7831 | * getStateCode({}); //=> Maybe.Nothing()
|
7832 | * @symb R.composeK(f, g, h)(a) = R.chain(f, R.chain(g, h(a)))
|
7833 | */
|
7834 | var composeK = function composeK() {
|
7835 | if (arguments.length === 0) {
|
7836 | throw new Error('composeK requires at least one argument');
|
7837 | }
|
7838 | var init = Array.prototype.slice.call(arguments);
|
7839 | var last = init.pop();
|
7840 | return compose(compose.apply(this, map(chain, init)), last);
|
7841 | };
|
7842 |
|
7843 | /**
|
7844 | * Performs right-to-left composition of one or more Promise-returning
|
7845 | * functions. The rightmost function may have any arity; the remaining
|
7846 | * functions must be unary.
|
7847 | *
|
7848 | * @func
|
7849 | * @memberOf R
|
7850 | * @since v0.10.0
|
7851 | * @category Function
|
7852 | * @sig ((y -> Promise z), (x -> Promise y), ..., (a -> Promise b)) -> (a -> Promise z)
|
7853 | * @param {...Function} functions The functions to compose
|
7854 | * @return {Function}
|
7855 | * @see R.pipeP
|
7856 | * @example
|
7857 | *
|
7858 | * var db = {
|
7859 | * users: {
|
7860 | * JOE: {
|
7861 | * name: 'Joe',
|
7862 | * followers: ['STEVE', 'SUZY']
|
7863 | * }
|
7864 | * }
|
7865 | * }
|
7866 | *
|
7867 | * // We'll pretend to do a db lookup which returns a promise
|
7868 | * var lookupUser = (userId) => Promise.resolve(db.users[userId])
|
7869 | * var lookupFollowers = (user) => Promise.resolve(user.followers)
|
7870 | * lookupUser('JOE').then(lookupFollowers)
|
7871 | *
|
7872 | * // followersForUser :: String -> Promise [UserId]
|
7873 | * var followersForUser = R.composeP(lookupFollowers, lookupUser);
|
7874 | * followersForUser('JOE').then(followers => console.log('Followers:', followers))
|
7875 | * // Followers: ["STEVE","SUZY"]
|
7876 | */
|
7877 | var composeP = function composeP() {
|
7878 | if (arguments.length === 0) {
|
7879 | throw new Error('composeP requires at least one argument');
|
7880 | }
|
7881 | return pipeP.apply(this, reverse(arguments));
|
7882 | };
|
7883 |
|
7884 | /**
|
7885 | * Wraps a constructor function inside a curried function that can be called
|
7886 | * with the same arguments and returns the same type.
|
7887 | *
|
7888 | * @func
|
7889 | * @memberOf R
|
7890 | * @since v0.1.0
|
7891 | * @category Function
|
7892 | * @sig (* -> {*}) -> (* -> {*})
|
7893 | * @param {Function} fn The constructor function to wrap.
|
7894 | * @return {Function} A wrapped, curried constructor function.
|
7895 | * @example
|
7896 | *
|
7897 | * // Constructor function
|
7898 | * function Animal(kind) {
|
7899 | * this.kind = kind;
|
7900 | * };
|
7901 | * Animal.prototype.sighting = function() {
|
7902 | * return "It's a " + this.kind + "!";
|
7903 | * }
|
7904 | *
|
7905 | * var AnimalConstructor = R.construct(Animal)
|
7906 | *
|
7907 | * // Notice we no longer need the 'new' keyword:
|
7908 | * AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}};
|
7909 | *
|
7910 | * var animalTypes = ["Lion", "Tiger", "Bear"];
|
7911 | * var animalSighting = R.invoker(0, 'sighting');
|
7912 | * var sightNewAnimal = R.compose(animalSighting, AnimalConstructor);
|
7913 | * R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"]
|
7914 | */
|
7915 | var construct = _curry1(function construct(Fn) {
|
7916 | return constructN(Fn.length, Fn);
|
7917 | });
|
7918 |
|
7919 | /**
|
7920 | * Returns `true` if the specified value is equal, in `R.equals` terms, to at
|
7921 | * least one element of the given list; `false` otherwise.
|
7922 | *
|
7923 | * @func
|
7924 | * @memberOf R
|
7925 | * @since v0.1.0
|
7926 | * @category List
|
7927 | * @sig a -> [a] -> Boolean
|
7928 | * @param {Object} a The item to compare against.
|
7929 | * @param {Array} list The array to consider.
|
7930 | * @return {Boolean} `true` if an equivalent item is in the list, `false` otherwise.
|
7931 | * @see R.any
|
7932 | * @example
|
7933 | *
|
7934 | * R.contains(3, [1, 2, 3]); //=> true
|
7935 | * R.contains(4, [1, 2, 3]); //=> false
|
7936 | * R.contains({ name: 'Fred' }, [{ name: 'Fred' }]); //=> true
|
7937 | * R.contains([42], [[42]]); //=> true
|
7938 | */
|
7939 | var contains = _curry2(_contains);
|
7940 |
|
7941 | /**
|
7942 | * Finds the set (i.e. no duplicates) of all elements in the first list not
|
7943 | * contained in the second list. Objects and Arrays are compared are compared
|
7944 | * in terms of value equality, not reference equality.
|
7945 | *
|
7946 | * @func
|
7947 | * @memberOf R
|
7948 | * @since v0.1.0
|
7949 | * @category Relation
|
7950 | * @sig [*] -> [*] -> [*]
|
7951 | * @param {Array} list1 The first list.
|
7952 | * @param {Array} list2 The second list.
|
7953 | * @return {Array} The elements in `list1` that are not in `list2`.
|
7954 | * @see R.differenceWith, R.symmetricDifference, R.symmetricDifferenceWith
|
7955 | * @example
|
7956 | *
|
7957 | * R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
|
7958 | * R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
|
7959 | * R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]
|
7960 | */
|
7961 | var difference = _curry2(function difference(first, second) {
|
7962 | var out = [];
|
7963 | var idx = 0;
|
7964 | var firstLen = first.length;
|
7965 | while (idx < firstLen) {
|
7966 | if (!_contains(first[idx], second) && !_contains(first[idx], out)) {
|
7967 | out[out.length] = first[idx];
|
7968 | }
|
7969 | idx += 1;
|
7970 | }
|
7971 | return out;
|
7972 | });
|
7973 |
|
7974 | /**
|
7975 | * Returns a new list without any consecutively repeating elements. `R.equals`
|
7976 | * is used to determine equality.
|
7977 | *
|
7978 | * Acts as a transducer if a transformer is given in list position.
|
7979 | *
|
7980 | * @func
|
7981 | * @memberOf R
|
7982 | * @since v0.14.0
|
7983 | * @category List
|
7984 | * @sig [a] -> [a]
|
7985 | * @param {Array} list The array to consider.
|
7986 | * @return {Array} `list` without repeating elements.
|
7987 | * @see R.transduce
|
7988 | * @example
|
7989 | *
|
7990 | * R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2]
|
7991 | */
|
7992 | var dropRepeats = _curry1(_dispatchable([], _xdropRepeatsWith(equals), dropRepeatsWith(equals)));
|
7993 |
|
7994 | /**
|
7995 | * "lifts" a function of arity > 1 so that it may "map over" a list, Function or other
|
7996 | * object that satisfies the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
|
7997 | *
|
7998 | * @func
|
7999 | * @memberOf R
|
8000 | * @since v0.7.0
|
8001 | * @category Function
|
8002 | * @sig (*... -> *) -> ([*]... -> [*])
|
8003 | * @param {Function} fn The function to lift into higher context
|
8004 | * @return {Function} The lifted function.
|
8005 | * @see R.liftN
|
8006 | * @example
|
8007 | *
|
8008 | * var madd3 = R.lift((a, b, c) => a + b + c);
|
8009 | *
|
8010 | * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
|
8011 | *
|
8012 | * var madd5 = R.lift((a, b, c, d, e) => a + b + c + d + e);
|
8013 | *
|
8014 | * madd5([1,2], [3], [4, 5], [6], [7, 8]); //=> [21, 22, 22, 23, 22, 23, 23, 24]
|
8015 | */
|
8016 | var lift = _curry1(function lift(fn) {
|
8017 | return liftN(fn.length, fn);
|
8018 | });
|
8019 |
|
8020 | /**
|
8021 | * Returns a partial copy of an object omitting the keys specified.
|
8022 | *
|
8023 | * @func
|
8024 | * @memberOf R
|
8025 | * @since v0.1.0
|
8026 | * @category Object
|
8027 | * @sig [String] -> {String: *} -> {String: *}
|
8028 | * @param {Array} names an array of String property names to omit from the new object
|
8029 | * @param {Object} obj The object to copy from
|
8030 | * @return {Object} A new object with properties from `names` not on it.
|
8031 | * @see R.pick
|
8032 | * @example
|
8033 | *
|
8034 | * R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}
|
8035 | */
|
8036 | var omit = _curry2(function omit(names, obj) {
|
8037 | var result = {};
|
8038 | for (var prop in obj) {
|
8039 | if (!_contains(prop, names)) {
|
8040 | result[prop] = obj[prop];
|
8041 | }
|
8042 | }
|
8043 | return result;
|
8044 | });
|
8045 |
|
8046 | /**
|
8047 | * Returns the left-to-right Kleisli composition of the provided functions,
|
8048 | * each of which must return a value of a type supported by [`chain`](#chain).
|
8049 | *
|
8050 | * `R.pipeK(f, g, h)` is equivalent to `R.pipe(R.chain(f), R.chain(g), R.chain(h))`.
|
8051 | *
|
8052 | * @func
|
8053 | * @memberOf R
|
8054 | * @since v0.16.0
|
8055 | * @category Function
|
8056 | * @sig Chain m => ((a -> m b), (b -> m c), ..., (y -> m z)) -> (a -> m z)
|
8057 | * @param {...Function}
|
8058 | * @return {Function}
|
8059 | * @see R.composeK
|
8060 | * @example
|
8061 | *
|
8062 | * // parseJson :: String -> Maybe *
|
8063 | * // get :: String -> Object -> Maybe *
|
8064 | *
|
8065 | * // getStateCode :: Maybe String -> Maybe String
|
8066 | * var getStateCode = R.pipeK(
|
8067 | * parseJson,
|
8068 | * get('user'),
|
8069 | * get('address'),
|
8070 | * get('state'),
|
8071 | * R.compose(Maybe.of, R.toUpper)
|
8072 | * );
|
8073 | *
|
8074 | * getStateCode('{"user":{"address":{"state":"ny"}}}');
|
8075 | * //=> Just('NY')
|
8076 | * getStateCode('[Invalid JSON]');
|
8077 | * //=> Nothing()
|
8078 | * @symb R.pipeK(f, g, h)(a) = R.chain(h, R.chain(g, f(a)))
|
8079 | */
|
8080 | var pipeK = function pipeK() {
|
8081 | if (arguments.length === 0) {
|
8082 | throw new Error('pipeK requires at least one argument');
|
8083 | }
|
8084 | return composeK.apply(this, reverse(arguments));
|
8085 | };
|
8086 |
|
8087 | /**
|
8088 | * Returns the string representation of the given value. `eval`'ing the output
|
8089 | * should result in a value equivalent to the input value. Many of the built-in
|
8090 | * `toString` methods do not satisfy this requirement.
|
8091 | *
|
8092 | * If the given value is an `[object Object]` with a `toString` method other
|
8093 | * than `Object.prototype.toString`, this method is invoked with no arguments
|
8094 | * to produce the return value. This means user-defined constructor functions
|
8095 | * can provide a suitable `toString` method. For example:
|
8096 | *
|
8097 | * function Point(x, y) {
|
8098 | * this.x = x;
|
8099 | * this.y = y;
|
8100 | * }
|
8101 | *
|
8102 | * Point.prototype.toString = function() {
|
8103 | * return 'new Point(' + this.x + ', ' + this.y + ')';
|
8104 | * };
|
8105 | *
|
8106 | * R.toString(new Point(1, 2)); //=> 'new Point(1, 2)'
|
8107 | *
|
8108 | * @func
|
8109 | * @memberOf R
|
8110 | * @since v0.14.0
|
8111 | * @category String
|
8112 | * @sig * -> String
|
8113 | * @param {*} val
|
8114 | * @return {String}
|
8115 | * @example
|
8116 | *
|
8117 | * R.toString(42); //=> '42'
|
8118 | * R.toString('abc'); //=> '"abc"'
|
8119 | * R.toString([1, 2, 3]); //=> '[1, 2, 3]'
|
8120 | * R.toString({foo: 1, bar: 2, baz: 3}); //=> '{"bar": 2, "baz": 3, "foo": 1}'
|
8121 | * R.toString(new Date('2001-02-03T04:05:06Z')); //=> 'new Date("2001-02-03T04:05:06.000Z")'
|
8122 | */
|
8123 | var toString = _curry1(function toString(val) {
|
8124 | return _toString(val, []);
|
8125 | });
|
8126 |
|
8127 | /**
|
8128 | * Returns a new list without values in the first argument.
|
8129 | * `R.equals` is used to determine equality.
|
8130 | *
|
8131 | * Acts as a transducer if a transformer is given in list position.
|
8132 | *
|
8133 | * @func
|
8134 | * @memberOf R
|
8135 | * @since v0.19.0
|
8136 | * @category List
|
8137 | * @sig [a] -> [a] -> [a]
|
8138 | * @param {Array} list1 The values to be removed from `list2`.
|
8139 | * @param {Array} list2 The array to remove values from.
|
8140 | * @return {Array} The new array without values in `list1`.
|
8141 | * @see R.transduce
|
8142 | * @example
|
8143 | *
|
8144 | * R.without([1, 2], [1, 2, 1, 3, 4]); //=> [3, 4]
|
8145 | */
|
8146 | var without = _curry2(function (xs, list) {
|
8147 | return reject(flip(_contains)(xs), list);
|
8148 | });
|
8149 |
|
8150 | // A simple Set type that honours R.equals semantics
|
8151 | /* globals Set */
|
8152 | // until we figure out why jsdoc chokes on this
|
8153 | // @param item The item to add to the Set
|
8154 | // @returns {boolean} true if the item did not exist prior, otherwise false
|
8155 | //
|
8156 | //
|
8157 | // @param item The item to check for existence in the Set
|
8158 | // @returns {boolean} true if the item exists in the Set, otherwise false
|
8159 | //
|
8160 | //
|
8161 | // Combines the logic for checking whether an item is a member of the set and
|
8162 | // for adding a new item to the set.
|
8163 | //
|
8164 | // @param item The item to check or add to the Set instance.
|
8165 | // @param shouldAdd If true, the item will be added to the set if it doesn't
|
8166 | // already exist.
|
8167 | // @param set The set instance to check or add to.
|
8168 | // @return {boolean} true if the item already existed, otherwise false.
|
8169 | //
|
8170 | // distinguish between +0 and -0
|
8171 | // these types can all utilise the native Set
|
8172 | // set._items['boolean'] holds a two element array
|
8173 | // representing [ falseExists, trueExists ]
|
8174 | // compare functions for reference equality
|
8175 | /* falls through */
|
8176 | // reduce the search size of heterogeneous sets by creating buckets
|
8177 | // for each type.
|
8178 | // scan through all previously applied items
|
8179 | var _Set = function () {
|
8180 | function _Set() {
|
8181 | /* globals Set */
|
8182 | this._nativeSet = typeof Set === 'function' ? new Set() : null;
|
8183 | this._items = {};
|
8184 | }
|
8185 | // until we figure out why jsdoc chokes on this
|
8186 | // @param item The item to add to the Set
|
8187 | // @returns {boolean} true if the item did not exist prior, otherwise false
|
8188 | //
|
8189 | _Set.prototype.add = function (item) {
|
8190 | return !hasOrAdd(item, true, this);
|
8191 | };
|
8192 | //
|
8193 | // @param item The item to check for existence in the Set
|
8194 | // @returns {boolean} true if the item exists in the Set, otherwise false
|
8195 | //
|
8196 | _Set.prototype.has = function (item) {
|
8197 | return hasOrAdd(item, false, this);
|
8198 | };
|
8199 | //
|
8200 | // Combines the logic for checking whether an item is a member of the set and
|
8201 | // for adding a new item to the set.
|
8202 | //
|
8203 | // @param item The item to check or add to the Set instance.
|
8204 | // @param shouldAdd If true, the item will be added to the set if it doesn't
|
8205 | // already exist.
|
8206 | // @param set The set instance to check or add to.
|
8207 | // @return {boolean} true if the item already existed, otherwise false.
|
8208 | //
|
8209 | function hasOrAdd(item, shouldAdd, set) {
|
8210 | var type = typeof item;
|
8211 | var prevSize, newSize;
|
8212 | switch (type) {
|
8213 | case 'string':
|
8214 | case 'number':
|
8215 | // distinguish between +0 and -0
|
8216 | if (item === 0 && 1 / item === -Infinity) {
|
8217 | if (set._items['-0']) {
|
8218 | return true;
|
8219 | } else {
|
8220 | if (shouldAdd) {
|
8221 | set._items['-0'] = true;
|
8222 | }
|
8223 | return false;
|
8224 | }
|
8225 | }
|
8226 | // these types can all utilise the native Set
|
8227 | if (set._nativeSet !== null) {
|
8228 | if (shouldAdd) {
|
8229 | prevSize = set._nativeSet.size;
|
8230 | set._nativeSet.add(item);
|
8231 | newSize = set._nativeSet.size;
|
8232 | return newSize === prevSize;
|
8233 | } else {
|
8234 | return set._nativeSet.has(item);
|
8235 | }
|
8236 | } else {
|
8237 | if (!(type in set._items)) {
|
8238 | if (shouldAdd) {
|
8239 | set._items[type] = {};
|
8240 | set._items[type][item] = true;
|
8241 | }
|
8242 | return false;
|
8243 | } else if (item in set._items[type]) {
|
8244 | return true;
|
8245 | } else {
|
8246 | if (shouldAdd) {
|
8247 | set._items[type][item] = true;
|
8248 | }
|
8249 | return false;
|
8250 | }
|
8251 | }
|
8252 | case 'boolean':
|
8253 | // set._items['boolean'] holds a two element array
|
8254 | // representing [ falseExists, trueExists ]
|
8255 | if (type in set._items) {
|
8256 | var bIdx = item ? 1 : 0;
|
8257 | if (set._items[type][bIdx]) {
|
8258 | return true;
|
8259 | } else {
|
8260 | if (shouldAdd) {
|
8261 | set._items[type][bIdx] = true;
|
8262 | }
|
8263 | return false;
|
8264 | }
|
8265 | } else {
|
8266 | if (shouldAdd) {
|
8267 | set._items[type] = item ? [
|
8268 | false,
|
8269 | true
|
8270 | ] : [
|
8271 | true,
|
8272 | false
|
8273 | ];
|
8274 | }
|
8275 | return false;
|
8276 | }
|
8277 | case 'function':
|
8278 | // compare functions for reference equality
|
8279 | if (set._nativeSet !== null) {
|
8280 | if (shouldAdd) {
|
8281 | prevSize = set._nativeSet.size;
|
8282 | set._nativeSet.add(item);
|
8283 | newSize = set._nativeSet.size;
|
8284 | return newSize === prevSize;
|
8285 | } else {
|
8286 | return set._nativeSet.has(item);
|
8287 | }
|
8288 | } else {
|
8289 | if (!(type in set._items)) {
|
8290 | if (shouldAdd) {
|
8291 | set._items[type] = [item];
|
8292 | }
|
8293 | return false;
|
8294 | }
|
8295 | if (!_contains(item, set._items[type])) {
|
8296 | if (shouldAdd) {
|
8297 | set._items[type].push(item);
|
8298 | }
|
8299 | return false;
|
8300 | }
|
8301 | return true;
|
8302 | }
|
8303 | case 'undefined':
|
8304 | if (set._items[type]) {
|
8305 | return true;
|
8306 | } else {
|
8307 | if (shouldAdd) {
|
8308 | set._items[type] = true;
|
8309 | }
|
8310 | return false;
|
8311 | }
|
8312 | case 'object':
|
8313 | if (item === null) {
|
8314 | if (!set._items['null']) {
|
8315 | if (shouldAdd) {
|
8316 | set._items['null'] = true;
|
8317 | }
|
8318 | return false;
|
8319 | }
|
8320 | return true;
|
8321 | }
|
8322 | /* falls through */
|
8323 | default:
|
8324 | // reduce the search size of heterogeneous sets by creating buckets
|
8325 | // for each type.
|
8326 | type = Object.prototype.toString.call(item);
|
8327 | if (!(type in set._items)) {
|
8328 | if (shouldAdd) {
|
8329 | set._items[type] = [item];
|
8330 | }
|
8331 | return false;
|
8332 | }
|
8333 | // scan through all previously applied items
|
8334 | if (!_contains(item, set._items[type])) {
|
8335 | if (shouldAdd) {
|
8336 | set._items[type].push(item);
|
8337 | }
|
8338 | return false;
|
8339 | }
|
8340 | return true;
|
8341 | }
|
8342 | }
|
8343 | return _Set;
|
8344 | }();
|
8345 |
|
8346 | /**
|
8347 | * A function which calls the two provided functions and returns the `&&`
|
8348 | * of the results.
|
8349 | * It returns the result of the first function if it is false-y and the result
|
8350 | * of the second function otherwise. Note that this is short-circuited,
|
8351 | * meaning that the second function will not be invoked if the first returns a
|
8352 | * false-y value.
|
8353 | *
|
8354 | * In addition to functions, `R.both` also accepts any fantasy-land compatible
|
8355 | * applicative functor.
|
8356 | *
|
8357 | * @func
|
8358 | * @memberOf R
|
8359 | * @since v0.12.0
|
8360 | * @category Logic
|
8361 | * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
|
8362 | * @param {Function} f A predicate
|
8363 | * @param {Function} g Another predicate
|
8364 | * @return {Function} a function that applies its arguments to `f` and `g` and `&&`s their outputs together.
|
8365 | * @see R.and
|
8366 | * @example
|
8367 | *
|
8368 | * var gt10 = R.gt(R.__, 10)
|
8369 | * var lt20 = R.lt(R.__, 20)
|
8370 | * var f = R.both(gt10, lt20);
|
8371 | * f(15); //=> true
|
8372 | * f(30); //=> false
|
8373 | */
|
8374 | var both = _curry2(function both(f, g) {
|
8375 | return _isFunction(f) ? function _both() {
|
8376 | return f.apply(this, arguments) && g.apply(this, arguments);
|
8377 | } : lift(and)(f, g);
|
8378 | });
|
8379 |
|
8380 | /**
|
8381 | * Takes a function `f` and returns a function `g` such that if called with the same arguments
|
8382 | * when `f` returns a "truthy" value, `g` returns `false` and when `f` returns a "falsy" value `g` returns `true`.
|
8383 | *
|
8384 | * `R.complement` may be applied to any functor
|
8385 | *
|
8386 | * @func
|
8387 | * @memberOf R
|
8388 | * @since v0.12.0
|
8389 | * @category Logic
|
8390 | * @sig (*... -> *) -> (*... -> Boolean)
|
8391 | * @param {Function} f
|
8392 | * @return {Function}
|
8393 | * @see R.not
|
8394 | * @example
|
8395 | *
|
8396 | * var isNotNil = R.complement(R.isNil);
|
8397 | * isNil(null); //=> true
|
8398 | * isNotNil(null); //=> false
|
8399 | * isNil(7); //=> false
|
8400 | * isNotNil(7); //=> true
|
8401 | */
|
8402 | var complement = lift(not);
|
8403 |
|
8404 | /**
|
8405 | * Returns the result of concatenating the given lists or strings.
|
8406 | *
|
8407 | * Note: `R.concat` expects both arguments to be of the same type,
|
8408 | * unlike the native `Array.prototype.concat` method. It will throw
|
8409 | * an error if you `concat` an Array with a non-Array value.
|
8410 | *
|
8411 | * Dispatches to the `concat` method of the first argument, if present.
|
8412 | *
|
8413 | * @func
|
8414 | * @memberOf R
|
8415 | * @since v0.1.0
|
8416 | * @category List
|
8417 | * @sig [a] -> [a] -> [a]
|
8418 | * @sig String -> String -> String
|
8419 | * @param {Array|String} firstList The first list
|
8420 | * @param {Array|String} secondList The second list
|
8421 | * @return {Array|String} A list consisting of the elements of `firstList` followed by the elements of
|
8422 | * `secondList`.
|
8423 | *
|
8424 | * @example
|
8425 | *
|
8426 | * R.concat('ABC', 'DEF'); // 'ABCDEF'
|
8427 | * R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
|
8428 | * R.concat([], []); //=> []
|
8429 | */
|
8430 | var concat = _curry2(function concat(a, b) {
|
8431 | if (a == null || !_isFunction(a.concat)) {
|
8432 | throw new TypeError(toString(a) + ' does not have a method named "concat"');
|
8433 | }
|
8434 | if (_isArray(a) && !_isArray(b)) {
|
8435 | throw new TypeError(toString(b) + ' is not an array');
|
8436 | }
|
8437 | return a.concat(b);
|
8438 | });
|
8439 |
|
8440 | /**
|
8441 | * A function wrapping calls to the two functions in an `||` operation,
|
8442 | * returning the result of the first function if it is truth-y and the result
|
8443 | * of the second function otherwise. Note that this is short-circuited,
|
8444 | * meaning that the second function will not be invoked if the first returns a
|
8445 | * truth-y value.
|
8446 | *
|
8447 | * In addition to functions, `R.either` also accepts any fantasy-land compatible
|
8448 | * applicative functor.
|
8449 | *
|
8450 | * @func
|
8451 | * @memberOf R
|
8452 | * @since v0.12.0
|
8453 | * @category Logic
|
8454 | * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
|
8455 | * @param {Function} f a predicate
|
8456 | * @param {Function} g another predicate
|
8457 | * @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
|
8458 | * @see R.or
|
8459 | * @example
|
8460 | *
|
8461 | * var gt10 = x => x > 10;
|
8462 | * var even = x => x % 2 === 0;
|
8463 | * var f = R.either(gt10, even);
|
8464 | * f(101); //=> true
|
8465 | * f(8); //=> true
|
8466 | */
|
8467 | var either = _curry2(function either(f, g) {
|
8468 | return _isFunction(f) ? function _either() {
|
8469 | return f.apply(this, arguments) || g.apply(this, arguments);
|
8470 | } : lift(or)(f, g);
|
8471 | });
|
8472 |
|
8473 | /**
|
8474 | * Turns a named method with a specified arity into a function that can be
|
8475 | * called directly supplied with arguments and a target object.
|
8476 | *
|
8477 | * The returned function is curried and accepts `arity + 1` parameters where
|
8478 | * the final parameter is the target object.
|
8479 | *
|
8480 | * @func
|
8481 | * @memberOf R
|
8482 | * @since v0.1.0
|
8483 | * @category Function
|
8484 | * @sig Number -> String -> (a -> b -> ... -> n -> Object -> *)
|
8485 | * @param {Number} arity Number of arguments the returned function should take
|
8486 | * before the target object.
|
8487 | * @param {String} method Name of the method to call.
|
8488 | * @return {Function} A new curried function.
|
8489 | * @example
|
8490 | *
|
8491 | * var sliceFrom = R.invoker(1, 'slice');
|
8492 | * sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
|
8493 | * var sliceFrom6 = R.invoker(2, 'slice')(6);
|
8494 | * sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
|
8495 | * @symb R.invoker(0, 'method')(o) = o['method']()
|
8496 | * @symb R.invoker(1, 'method')(a, o) = o['method'](a)
|
8497 | * @symb R.invoker(2, 'method')(a, b, o) = o['method'](a, b)
|
8498 | */
|
8499 | var invoker = _curry2(function invoker(arity, method) {
|
8500 | return curryN(arity + 1, function () {
|
8501 | var target = arguments[arity];
|
8502 | if (target != null && _isFunction(target[method])) {
|
8503 | return target[method].apply(target, Array.prototype.slice.call(arguments, 0, arity));
|
8504 | }
|
8505 | throw new TypeError(toString(target) + ' does not have a method named "' + method + '"');
|
8506 | });
|
8507 | });
|
8508 |
|
8509 | /**
|
8510 | * Returns a string made by inserting the `separator` between each element and
|
8511 | * concatenating all the elements into a single string.
|
8512 | *
|
8513 | * @func
|
8514 | * @memberOf R
|
8515 | * @since v0.1.0
|
8516 | * @category List
|
8517 | * @sig String -> [a] -> String
|
8518 | * @param {Number|String} separator The string used to separate the elements.
|
8519 | * @param {Array} xs The elements to join into a string.
|
8520 | * @return {String} str The string made by concatenating `xs` with `separator`.
|
8521 | * @see R.split
|
8522 | * @example
|
8523 | *
|
8524 | * var spacer = R.join(' ');
|
8525 | * spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
|
8526 | * R.join('|', [1, 2, 3]); //=> '1|2|3'
|
8527 | */
|
8528 | var join = invoker(1, 'join');
|
8529 |
|
8530 | /**
|
8531 | * Creates a new function that, when invoked, caches the result of calling `fn`
|
8532 | * for a given argument set and returns the result. Subsequent calls to the
|
8533 | * memoized `fn` with the same argument set will not result in an additional
|
8534 | * call to `fn`; instead, the cached result for that set of arguments will be
|
8535 | * returned.
|
8536 | *
|
8537 | * @func
|
8538 | * @memberOf R
|
8539 | * @since v0.1.0
|
8540 | * @category Function
|
8541 | * @sig (*... -> a) -> (*... -> a)
|
8542 | * @param {Function} fn The function to memoize.
|
8543 | * @return {Function} Memoized version of `fn`.
|
8544 | * @example
|
8545 | *
|
8546 | * var count = 0;
|
8547 | * var factorial = R.memoize(n => {
|
8548 | * count += 1;
|
8549 | * return R.product(R.range(1, n + 1));
|
8550 | * });
|
8551 | * factorial(5); //=> 120
|
8552 | * factorial(5); //=> 120
|
8553 | * factorial(5); //=> 120
|
8554 | * count; //=> 1
|
8555 | */
|
8556 | var memoize = _curry1(function memoize(fn) {
|
8557 | var cache = {};
|
8558 | return _arity(fn.length, function () {
|
8559 | var key = toString(arguments);
|
8560 | if (!_has(key, cache)) {
|
8561 | cache[key] = fn.apply(this, arguments);
|
8562 | }
|
8563 | return cache[key];
|
8564 | });
|
8565 | });
|
8566 |
|
8567 | /**
|
8568 | * Splits a string into an array of strings based on the given
|
8569 | * separator.
|
8570 | *
|
8571 | * @func
|
8572 | * @memberOf R
|
8573 | * @since v0.1.0
|
8574 | * @category String
|
8575 | * @sig (String | RegExp) -> String -> [String]
|
8576 | * @param {String|RegExp} sep The pattern.
|
8577 | * @param {String} str The string to separate into an array.
|
8578 | * @return {Array} The array of strings from `str` separated by `str`.
|
8579 | * @see R.join
|
8580 | * @example
|
8581 | *
|
8582 | * var pathComponents = R.split('/');
|
8583 | * R.tail(pathComponents('/usr/local/bin/node')); //=> ['usr', 'local', 'bin', 'node']
|
8584 | *
|
8585 | * R.split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd']
|
8586 | */
|
8587 | var split = invoker(1, 'split');
|
8588 |
|
8589 | /**
|
8590 | * Finds the set (i.e. no duplicates) of all elements contained in the first or
|
8591 | * second list, but not both.
|
8592 | *
|
8593 | * @func
|
8594 | * @memberOf R
|
8595 | * @since v0.19.0
|
8596 | * @category Relation
|
8597 | * @sig [*] -> [*] -> [*]
|
8598 | * @param {Array} list1 The first list.
|
8599 | * @param {Array} list2 The second list.
|
8600 | * @return {Array} The elements in `list1` or `list2`, but not both.
|
8601 | * @see R.symmetricDifferenceWith, R.difference, R.differenceWith
|
8602 | * @example
|
8603 | *
|
8604 | * R.symmetricDifference([1,2,3,4], [7,6,5,4,3]); //=> [1,2,7,6,5]
|
8605 | * R.symmetricDifference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5,1,2]
|
8606 | */
|
8607 | var symmetricDifference = _curry2(function symmetricDifference(list1, list2) {
|
8608 | return concat(difference(list1, list2), difference(list2, list1));
|
8609 | });
|
8610 |
|
8611 | /**
|
8612 | * Finds the set (i.e. no duplicates) of all elements contained in the first or
|
8613 | * second list, but not both. Duplication is determined according to the value
|
8614 | * returned by applying the supplied predicate to two list elements.
|
8615 | *
|
8616 | * @func
|
8617 | * @memberOf R
|
8618 | * @since v0.19.0
|
8619 | * @category Relation
|
8620 | * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
|
8621 | * @param {Function} pred A predicate used to test whether two items are equal.
|
8622 | * @param {Array} list1 The first list.
|
8623 | * @param {Array} list2 The second list.
|
8624 | * @return {Array} The elements in `list1` or `list2`, but not both.
|
8625 | * @see R.symmetricDifference, R.difference, R.differenceWith
|
8626 | * @example
|
8627 | *
|
8628 | * var eqA = R.eqBy(R.prop('a'));
|
8629 | * var l1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
|
8630 | * var l2 = [{a: 3}, {a: 4}, {a: 5}, {a: 6}];
|
8631 | * R.symmetricDifferenceWith(eqA, l1, l2); //=> [{a: 1}, {a: 2}, {a: 5}, {a: 6}]
|
8632 | */
|
8633 | var symmetricDifferenceWith = _curry3(function symmetricDifferenceWith(pred, list1, list2) {
|
8634 | return concat(differenceWith(pred, list1, list2), differenceWith(pred, list2, list1));
|
8635 | });
|
8636 |
|
8637 | /**
|
8638 | * Determines whether a given string matches a given regular expression.
|
8639 | *
|
8640 | * @func
|
8641 | * @memberOf R
|
8642 | * @since v0.12.0
|
8643 | * @category String
|
8644 | * @sig RegExp -> String -> Boolean
|
8645 | * @param {RegExp} pattern
|
8646 | * @param {String} str
|
8647 | * @return {Boolean}
|
8648 | * @see R.match
|
8649 | * @example
|
8650 | *
|
8651 | * R.test(/^x/, 'xyz'); //=> true
|
8652 | * R.test(/^y/, 'xyz'); //=> false
|
8653 | */
|
8654 | var test = _curry2(function test(pattern, str) {
|
8655 | if (!_isRegExp(pattern)) {
|
8656 | throw new TypeError('\u2018test\u2019 requires a value of type RegExp as its first argument; received ' + toString(pattern));
|
8657 | }
|
8658 | return _cloneRegExp(pattern).test(str);
|
8659 | });
|
8660 |
|
8661 | /**
|
8662 | * The lower case version of a string.
|
8663 | *
|
8664 | * @func
|
8665 | * @memberOf R
|
8666 | * @since v0.9.0
|
8667 | * @category String
|
8668 | * @sig String -> String
|
8669 | * @param {String} str The string to lower case.
|
8670 | * @return {String} The lower case version of `str`.
|
8671 | * @see R.toUpper
|
8672 | * @example
|
8673 | *
|
8674 | * R.toLower('XYZ'); //=> 'xyz'
|
8675 | */
|
8676 | var toLower = invoker(0, 'toLowerCase');
|
8677 |
|
8678 | /**
|
8679 | * The upper case version of a string.
|
8680 | *
|
8681 | * @func
|
8682 | * @memberOf R
|
8683 | * @since v0.9.0
|
8684 | * @category String
|
8685 | * @sig String -> String
|
8686 | * @param {String} str The string to upper case.
|
8687 | * @return {String} The upper case version of `str`.
|
8688 | * @see R.toLower
|
8689 | * @example
|
8690 | *
|
8691 | * R.toUpper('abc'); //=> 'ABC'
|
8692 | */
|
8693 | var toUpper = invoker(0, 'toUpperCase');
|
8694 |
|
8695 | /**
|
8696 | * Returns a new list containing only one copy of each element in the original
|
8697 | * list, based upon the value returned by applying the supplied function to
|
8698 | * each list element. Prefers the first item if the supplied function produces
|
8699 | * the same value on two items. `R.equals` is used for comparison.
|
8700 | *
|
8701 | * @func
|
8702 | * @memberOf R
|
8703 | * @since v0.16.0
|
8704 | * @category List
|
8705 | * @sig (a -> b) -> [a] -> [a]
|
8706 | * @param {Function} fn A function used to produce a value to use during comparisons.
|
8707 | * @param {Array} list The array to consider.
|
8708 | * @return {Array} The list of unique items.
|
8709 | * @example
|
8710 | *
|
8711 | * R.uniqBy(Math.abs, [-1, -5, 2, 10, 1, 2]); //=> [-1, -5, 2, 10]
|
8712 | */
|
8713 | var uniqBy = _curry2(function uniqBy(fn, list) {
|
8714 | var set = new _Set();
|
8715 | var result = [];
|
8716 | var idx = 0;
|
8717 | var appliedItem, item;
|
8718 | while (idx < list.length) {
|
8719 | item = list[idx];
|
8720 | appliedItem = fn(item);
|
8721 | if (set.add(appliedItem)) {
|
8722 | result.push(item);
|
8723 | }
|
8724 | idx += 1;
|
8725 | }
|
8726 | return result;
|
8727 | });
|
8728 |
|
8729 | /**
|
8730 | * Returns a new list containing only one copy of each element in the original
|
8731 | * list. `R.equals` is used to determine equality.
|
8732 | *
|
8733 | * @func
|
8734 | * @memberOf R
|
8735 | * @since v0.1.0
|
8736 | * @category List
|
8737 | * @sig [a] -> [a]
|
8738 | * @param {Array} list The array to consider.
|
8739 | * @return {Array} The list of unique items.
|
8740 | * @example
|
8741 | *
|
8742 | * R.uniq([1, 1, 2, 1]); //=> [1, 2]
|
8743 | * R.uniq([1, '1']); //=> [1, '1']
|
8744 | * R.uniq([[42], [42]]); //=> [[42]]
|
8745 | */
|
8746 | var uniq = uniqBy(identity);
|
8747 |
|
8748 | /**
|
8749 | * Combines two lists into a set (i.e. no duplicates) composed of those
|
8750 | * elements common to both lists.
|
8751 | *
|
8752 | * @func
|
8753 | * @memberOf R
|
8754 | * @since v0.1.0
|
8755 | * @category Relation
|
8756 | * @sig [*] -> [*] -> [*]
|
8757 | * @param {Array} list1 The first list.
|
8758 | * @param {Array} list2 The second list.
|
8759 | * @return {Array} The list of elements found in both `list1` and `list2`.
|
8760 | * @see R.intersectionWith
|
8761 | * @example
|
8762 | *
|
8763 | * R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
|
8764 | */
|
8765 | var intersection = _curry2(function intersection(list1, list2) {
|
8766 | var lookupList, filteredList;
|
8767 | if (list1.length > list2.length) {
|
8768 | lookupList = list1;
|
8769 | filteredList = list2;
|
8770 | } else {
|
8771 | lookupList = list2;
|
8772 | filteredList = list1;
|
8773 | }
|
8774 | return uniq(_filter(flip(_contains)(lookupList), filteredList));
|
8775 | });
|
8776 |
|
8777 | /**
|
8778 | * Combines two lists into a set (i.e. no duplicates) composed of the elements
|
8779 | * of each list.
|
8780 | *
|
8781 | * @func
|
8782 | * @memberOf R
|
8783 | * @since v0.1.0
|
8784 | * @category Relation
|
8785 | * @sig [*] -> [*] -> [*]
|
8786 | * @param {Array} as The first list.
|
8787 | * @param {Array} bs The second list.
|
8788 | * @return {Array} The first and second lists concatenated, with
|
8789 | * duplicates removed.
|
8790 | * @example
|
8791 | *
|
8792 | * R.union([1, 2, 3], [2, 3, 4]); //=> [1, 2, 3, 4]
|
8793 | */
|
8794 | var union = _curry2(compose(uniq, _concat));
|
8795 |
|
8796 | var R = {
|
8797 | F: F,
|
8798 | T: T,
|
8799 | __: __,
|
8800 | add: add,
|
8801 | addIndex: addIndex,
|
8802 | adjust: adjust,
|
8803 | all: all,
|
8804 | allPass: allPass,
|
8805 | always: always,
|
8806 | and: and,
|
8807 | any: any,
|
8808 | anyPass: anyPass,
|
8809 | ap: ap,
|
8810 | aperture: aperture,
|
8811 | append: append,
|
8812 | apply: apply,
|
8813 | applySpec: applySpec,
|
8814 | ascend: ascend,
|
8815 | assoc: assoc,
|
8816 | assocPath: assocPath,
|
8817 | binary: binary,
|
8818 | bind: bind,
|
8819 | both: both,
|
8820 | call: call,
|
8821 | chain: chain,
|
8822 | clamp: clamp,
|
8823 | clone: clone,
|
8824 | comparator: comparator,
|
8825 | complement: complement,
|
8826 | compose: compose,
|
8827 | composeK: composeK,
|
8828 | composeP: composeP,
|
8829 | concat: concat,
|
8830 | cond: cond,
|
8831 | construct: construct,
|
8832 | constructN: constructN,
|
8833 | contains: contains,
|
8834 | converge: converge,
|
8835 | countBy: countBy,
|
8836 | curry: curry,
|
8837 | curryN: curryN,
|
8838 | dec: dec,
|
8839 | defaultTo: defaultTo,
|
8840 | descend: descend,
|
8841 | difference: difference,
|
8842 | differenceWith: differenceWith,
|
8843 | dissoc: dissoc,
|
8844 | dissocPath: dissocPath,
|
8845 | divide: divide,
|
8846 | drop: drop,
|
8847 | dropLast: dropLast,
|
8848 | dropLastWhile: dropLastWhile,
|
8849 | dropRepeats: dropRepeats,
|
8850 | dropRepeatsWith: dropRepeatsWith,
|
8851 | dropWhile: dropWhile,
|
8852 | either: either,
|
8853 | empty: empty,
|
8854 | eqBy: eqBy,
|
8855 | eqProps: eqProps,
|
8856 | equals: equals,
|
8857 | evolve: evolve,
|
8858 | filter: filter,
|
8859 | find: find,
|
8860 | findIndex: findIndex,
|
8861 | findLast: findLast,
|
8862 | findLastIndex: findLastIndex,
|
8863 | flatten: flatten,
|
8864 | flip: flip,
|
8865 | forEach: forEach,
|
8866 | forEachObjIndexed: forEachObjIndexed,
|
8867 | fromPairs: fromPairs,
|
8868 | groupBy: groupBy,
|
8869 | groupWith: groupWith,
|
8870 | gt: gt,
|
8871 | gte: gte,
|
8872 | has: has,
|
8873 | hasIn: hasIn,
|
8874 | head: head,
|
8875 | identical: identical,
|
8876 | identity: identity,
|
8877 | ifElse: ifElse,
|
8878 | inc: inc,
|
8879 | indexBy: indexBy,
|
8880 | indexOf: indexOf,
|
8881 | init: init,
|
8882 | insert: insert,
|
8883 | insertAll: insertAll,
|
8884 | intersection: intersection,
|
8885 | intersectionWith: intersectionWith,
|
8886 | intersperse: intersperse,
|
8887 | into: into,
|
8888 | invert: invert,
|
8889 | invertObj: invertObj,
|
8890 | invoker: invoker,
|
8891 | is: is,
|
8892 | isArrayLike: isArrayLike,
|
8893 | isEmpty: isEmpty,
|
8894 | isNil: isNil,
|
8895 | join: join,
|
8896 | juxt: juxt,
|
8897 | keys: keys,
|
8898 | keysIn: keysIn,
|
8899 | last: last,
|
8900 | lastIndexOf: lastIndexOf,
|
8901 | length: length,
|
8902 | lens: lens,
|
8903 | lensIndex: lensIndex,
|
8904 | lensPath: lensPath,
|
8905 | lensProp: lensProp,
|
8906 | lift: lift,
|
8907 | liftN: liftN,
|
8908 | lt: lt,
|
8909 | lte: lte,
|
8910 | map: map,
|
8911 | mapAccum: mapAccum,
|
8912 | mapAccumRight: mapAccumRight,
|
8913 | mapObjIndexed: mapObjIndexed,
|
8914 | match: match,
|
8915 | mathMod: mathMod,
|
8916 | max: max,
|
8917 | maxBy: maxBy,
|
8918 | mean: mean,
|
8919 | median: median,
|
8920 | memoize: memoize,
|
8921 | merge: merge,
|
8922 | mergeAll: mergeAll,
|
8923 | mergeWith: mergeWith,
|
8924 | mergeWithKey: mergeWithKey,
|
8925 | min: min,
|
8926 | minBy: minBy,
|
8927 | modulo: modulo,
|
8928 | multiply: multiply,
|
8929 | nAry: nAry,
|
8930 | negate: negate,
|
8931 | none: none,
|
8932 | not: not,
|
8933 | nth: nth,
|
8934 | nthArg: nthArg,
|
8935 | objOf: objOf,
|
8936 | of: of,
|
8937 | omit: omit,
|
8938 | once: once,
|
8939 | or: or,
|
8940 | over: over,
|
8941 | pair: pair,
|
8942 | partial: partial,
|
8943 | partialRight: partialRight,
|
8944 | partition: partition,
|
8945 | path: path,
|
8946 | pathEq: pathEq,
|
8947 | pathOr: pathOr,
|
8948 | pathSatisfies: pathSatisfies,
|
8949 | pick: pick,
|
8950 | pickAll: pickAll,
|
8951 | pickBy: pickBy,
|
8952 | pipe: pipe,
|
8953 | pipeK: pipeK,
|
8954 | pipeP: pipeP,
|
8955 | pluck: pluck,
|
8956 | prepend: prepend,
|
8957 | product: product,
|
8958 | project: project,
|
8959 | prop: prop,
|
8960 | propEq: propEq,
|
8961 | propIs: propIs,
|
8962 | propOr: propOr,
|
8963 | propSatisfies: propSatisfies,
|
8964 | props: props,
|
8965 | range: range,
|
8966 | reduce: reduce,
|
8967 | reduceBy: reduceBy,
|
8968 | reduceRight: reduceRight,
|
8969 | reduceWhile: reduceWhile,
|
8970 | reduced: reduced,
|
8971 | reject: reject,
|
8972 | remove: remove,
|
8973 | repeat: repeat,
|
8974 | replace: replace,
|
8975 | reverse: reverse,
|
8976 | scan: scan,
|
8977 | sequence: sequence,
|
8978 | set: set,
|
8979 | slice: slice,
|
8980 | sort: sort,
|
8981 | sortBy: sortBy,
|
8982 | sortWith: sortWith,
|
8983 | split: split,
|
8984 | splitAt: splitAt,
|
8985 | splitEvery: splitEvery,
|
8986 | splitWhen: splitWhen,
|
8987 | subtract: subtract,
|
8988 | sum: sum,
|
8989 | symmetricDifference: symmetricDifference,
|
8990 | symmetricDifferenceWith: symmetricDifferenceWith,
|
8991 | tail: tail,
|
8992 | take: take,
|
8993 | takeLast: takeLast,
|
8994 | takeLastWhile: takeLastWhile,
|
8995 | takeWhile: takeWhile,
|
8996 | tap: tap,
|
8997 | test: test,
|
8998 | times: times,
|
8999 | toLower: toLower,
|
9000 | toPairs: toPairs,
|
9001 | toPairsIn: toPairsIn,
|
9002 | toString: toString,
|
9003 | toUpper: toUpper,
|
9004 | transduce: transduce,
|
9005 | transpose: transpose,
|
9006 | traverse: traverse,
|
9007 | trim: trim,
|
9008 | tryCatch: tryCatch,
|
9009 | type: type,
|
9010 | unapply: unapply,
|
9011 | unary: unary,
|
9012 | uncurryN: uncurryN,
|
9013 | unfold: unfold,
|
9014 | union: union,
|
9015 | unionWith: unionWith,
|
9016 | uniq: uniq,
|
9017 | uniqBy: uniqBy,
|
9018 | uniqWith: uniqWith,
|
9019 | unless: unless,
|
9020 | unnest: unnest,
|
9021 | until: until,
|
9022 | update: update,
|
9023 | useWith: useWith,
|
9024 | values: values,
|
9025 | valuesIn: valuesIn,
|
9026 | view: view,
|
9027 | when: when,
|
9028 | where: where,
|
9029 | whereEq: whereEq,
|
9030 | without: without,
|
9031 | xprod: xprod,
|
9032 | zip: zip,
|
9033 | zipObj: zipObj,
|
9034 | zipWith: zipWith
|
9035 | };
|
9036 | /* eslint-env amd */
|
9037 |
|
9038 | /* TEST_ENTRY_POINT */
|
9039 |
|
9040 | if (typeof exports === 'object') {
|
9041 | module.exports = R;
|
9042 | } else if (typeof define === 'function' && define.amd) {
|
9043 | define(function() { return R; });
|
9044 | } else {
|
9045 | this.R = R;
|
9046 | }
|
9047 |
|
9048 | }.call(this));
|