1 | /**
|
2 | * k2 - Functional javascript utils
|
3 | * @version v0.8.0
|
4 | */
|
5 | (function webpackUniversalModuleDefinition(root, factory) {
|
6 | if(typeof exports === 'object' && typeof module === 'object')
|
7 | module.exports = factory(require("_"));
|
8 | else if(typeof define === 'function' && define.amd)
|
9 | define(["_"], factory);
|
10 | else if(typeof exports === 'object')
|
11 | exports["k2"] = factory(require("_"));
|
12 | else
|
13 | root["k2"] = factory(root["_"]);
|
14 | })(this, function(__WEBPACK_EXTERNAL_MODULE_8__) {
|
15 | return /******/ (function(modules) { // webpackBootstrap
|
16 | /******/ // The module cache
|
17 | /******/ var installedModules = {};
|
18 |
|
19 | /******/ // The require function
|
20 | /******/ function __webpack_require__(moduleId) {
|
21 |
|
22 | /******/ // Check if module is in cache
|
23 | /******/ if(installedModules[moduleId])
|
24 | /******/ return installedModules[moduleId].exports;
|
25 |
|
26 | /******/ // Create a new module (and put it into the cache)
|
27 | /******/ var module = installedModules[moduleId] = {
|
28 | /******/ exports: {},
|
29 | /******/ id: moduleId,
|
30 | /******/ loaded: false
|
31 | /******/ };
|
32 |
|
33 | /******/ // Execute the module function
|
34 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
35 |
|
36 | /******/ // Flag the module as loaded
|
37 | /******/ module.loaded = true;
|
38 |
|
39 | /******/ // Return the exports of the module
|
40 | /******/ return module.exports;
|
41 | /******/ }
|
42 |
|
43 |
|
44 | /******/ // expose the modules object (__webpack_modules__)
|
45 | /******/ __webpack_require__.m = modules;
|
46 |
|
47 | /******/ // expose the module cache
|
48 | /******/ __webpack_require__.c = installedModules;
|
49 |
|
50 | /******/ // __webpack_public_path__
|
51 | /******/ __webpack_require__.p = "";
|
52 |
|
53 | /******/ // Load entry module and return exports
|
54 | /******/ return __webpack_require__(0);
|
55 | /******/ })
|
56 | /************************************************************************/
|
57 | /******/ ([
|
58 | /* 0 */
|
59 | /***/ function(module, exports, __webpack_require__) {
|
60 |
|
61 | ;
|
62 |
|
63 | var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
|
64 |
|
65 | var findPartialMatches = _interopRequire(__webpack_require__(1));
|
66 |
|
67 | var rankPartialMatches = _interopRequire(__webpack_require__(2));
|
68 |
|
69 | var cleanText = _interopRequire(__webpack_require__(3));
|
70 |
|
71 | var objectLens = _interopRequire(__webpack_require__(4));
|
72 |
|
73 | var guessDateFormat = _interopRequire(__webpack_require__(5));
|
74 |
|
75 | var onlyTrue = _interopRequire(__webpack_require__(6));
|
76 |
|
77 | var presentProperties = _interopRequire(__webpack_require__(7));
|
78 |
|
79 | module.exports = {
|
80 | findPartialMatches: findPartialMatches,
|
81 | rankPartialMatches: rankPartialMatches,
|
82 | cleanEnteredText: cleanText,
|
83 | objectLens: objectLens,
|
84 | guessDateFormat: guessDateFormat,
|
85 | onlyTrue: onlyTrue,
|
86 | presentProperties: presentProperties
|
87 | };
|
88 |
|
89 | /***/ },
|
90 | /* 1 */
|
91 | /***/ function(module, exports, __webpack_require__) {
|
92 |
|
93 | ;
|
94 |
|
95 | __webpack_require__(9);
|
96 | var check = __webpack_require__(10);
|
97 | var _ = __webpack_require__(8);
|
98 |
|
99 | function findPartialMatchesSingleProperty(property, items, queryText) {
|
100 | la(check.unemptyString(property), "need property name", property);
|
101 | la(check.array(items), "expected list of items", items);
|
102 | la(check.string(queryText), "expected query string", queryText);
|
103 | if (!queryText) {
|
104 | return [];
|
105 | }
|
106 |
|
107 | var text = queryText.toLowerCase();
|
108 | function hasQueryText(item) {
|
109 | return item[property].toLowerCase().indexOf(text) !== -1;
|
110 | }
|
111 |
|
112 | return items.filter(hasQueryText);
|
113 | }
|
114 |
|
115 | function findPartialMatchesMultipleProperties(properties, items, queryText) {
|
116 | if (check.string(properties)) {
|
117 | return findPartialMatchesSingleProperty(properties, items, queryText);
|
118 | }
|
119 | la(check.arrayOfStrings(properties), "need properties", properties);
|
120 | la(check.array(items), "expected list of items", items);
|
121 | la(check.string(queryText), "expected query string", queryText);
|
122 | if (!queryText) {
|
123 | return [];
|
124 | }
|
125 |
|
126 | var text = queryText.toLowerCase();
|
127 | function hasQueryText(item) {
|
128 | return properties.some(function (property) {
|
129 | var value = item[property];
|
130 | if (!value) {
|
131 | return false;
|
132 | }
|
133 | return value.toLowerCase().indexOf(text) !== -1;
|
134 | });
|
135 | }
|
136 |
|
137 | return items.filter(hasQueryText);
|
138 | }
|
139 |
|
140 | module.exports = findPartialMatchesMultipleProperties;
|
141 |
|
142 | /***/ },
|
143 | /* 2 */
|
144 | /***/ function(module, exports, __webpack_require__) {
|
145 |
|
146 | ;
|
147 |
|
148 | __webpack_require__(9);
|
149 | var check = __webpack_require__(10);
|
150 | var _ = __webpack_require__(8);
|
151 |
|
152 | // given objects that match query text, rank them, with better matches first
|
153 | function rankPartialMatchesSingleProperty(property, matches, queryText) {
|
154 | la(check.unemptyString(property), "need property name", property);
|
155 | la(check.array(matches), "expected list of matches", matches);
|
156 | la(check.string(queryText), "expected query string", queryText);
|
157 | if (!matches.length || !queryText) {
|
158 | return [];
|
159 | }
|
160 | var text = queryText.toLowerCase();
|
161 |
|
162 | // ranks items, whereby a lower number is a better rank, assumes item
|
163 | // matches the query string.
|
164 | function rankMatch(item) {
|
165 | var NOT_FOUND_RANK = 1000000;
|
166 | var matchText = item[property].toLowerCase();
|
167 | if (matchText === text) {
|
168 | return -1;
|
169 | }
|
170 | var matchStartsAt = matchText.indexOf(text);
|
171 | if (matchStartsAt === -1) {
|
172 | return NOT_FOUND_RANK;
|
173 | }
|
174 | return matchStartsAt;
|
175 | }
|
176 |
|
177 | return _.sortBy(matches, rankMatch);
|
178 | }
|
179 |
|
180 | function rankPartialMatchesMultipleProperties(properties, matches, queryText) {
|
181 | if (check.string(properties)) {
|
182 | return rankPartialMatchesSingleProperty(properties, matches, queryText);
|
183 | }
|
184 | la(check.arrayOfStrings(properties), "need properties", properties);
|
185 | la(check.array(matches), "expected list of matches", matches);
|
186 | la(check.string(queryText), "expected query string", queryText);
|
187 | if (!matches.length || !queryText) {
|
188 | return [];
|
189 | }
|
190 | var text = queryText.toLowerCase();
|
191 |
|
192 | // ranks items, whereby a lower number is a better rank, assumes item
|
193 | // matches the query string.
|
194 | function rankMatch(property, item) {
|
195 | var NOT_FOUND_RANK = 1000000;
|
196 | var matchText = item[property];
|
197 | if (!matchText) {
|
198 | return NOT_FOUND_RANK;
|
199 | }
|
200 | matchText = matchText.toLowerCase();
|
201 | if (matchText === text) {
|
202 | return -1;
|
203 | }
|
204 | var matchStartsAt = matchText.indexOf(text);
|
205 | if (matchStartsAt === -1) {
|
206 | return NOT_FOUND_RANK;
|
207 | }
|
208 | return matchStartsAt;
|
209 | }
|
210 |
|
211 | // best match over multiple properties is the smallest match
|
212 | function rankMatches(item) {
|
213 | var ranks = properties.map(function (property) {
|
214 | return rankMatch(property, item);
|
215 | });
|
216 |
|
217 | return _.min(ranks);
|
218 | }
|
219 |
|
220 | return _.sortBy(matches, rankMatches);
|
221 | }
|
222 |
|
223 | module.exports = rankPartialMatchesMultipleProperties;
|
224 |
|
225 | /***/ },
|
226 | /* 3 */
|
227 | /***/ function(module, exports, __webpack_require__) {
|
228 |
|
229 |
|
230 |
|
231 | /**
|
232 | Removes HTML entities added by TextArea. Used in ticker search
|
233 | @method cleanEnteredSearchText */
|
234 | ;
|
235 |
|
236 | module.exports = cleanEnteredSearchText;
|
237 | __webpack_require__(9);
|
238 | var check = __webpack_require__(10);
|
239 | var _ = __webpack_require__(8);
|
240 | function cleanEnteredSearchText(str) {
|
241 | la(check.string(str), "expected string to clean", str);
|
242 | str = str.toLowerCase();
|
243 | str = str.replace(" ", " ");
|
244 | str = str.trim();
|
245 | str = _.unescape(str);
|
246 | return str;
|
247 | }
|
248 |
|
249 | /***/ },
|
250 | /* 4 */
|
251 | /***/ function(module, exports, __webpack_require__) {
|
252 |
|
253 | ;
|
254 |
|
255 | var R = __webpack_require__(11);
|
256 |
|
257 | /**
|
258 | Makes a lens for immutable object updates on the given key.
|
259 | @method objectLens */
|
260 | function objectLens(key) {
|
261 | return R.lens(R.prop(key), function (val, obj) {
|
262 | var child = Object.create(obj);
|
263 | child.toJSON = function () {
|
264 | var base;
|
265 | if (obj.toJSON) {
|
266 | base = obj.toJSON();
|
267 | } else {
|
268 | base = R.pick(Object.keys(obj), obj);
|
269 | }
|
270 | base[key] = val;
|
271 | return base;
|
272 | };
|
273 | child[key] = val;
|
274 | return child;
|
275 | });
|
276 | }
|
277 |
|
278 | module.exports = objectLens;
|
279 |
|
280 | /***/ },
|
281 | /* 5 */
|
282 | /***/ function(module, exports, __webpack_require__) {
|
283 |
|
284 | ;
|
285 |
|
286 | var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
|
287 |
|
288 | __webpack_require__(9);
|
289 | var check = __webpack_require__(10);
|
290 |
|
291 | var xor = _interopRequire(__webpack_require__(6));
|
292 |
|
293 | var isYear = function (x) {
|
294 | return check.number(x) && x > 0;
|
295 | };
|
296 |
|
297 | var isMonth = function (x) {
|
298 | return check.number(x) && x > 0 && x < 13;
|
299 | };
|
300 |
|
301 | var isDay = function (x) {
|
302 | return check.number(x) && x > 0 && x < 32;
|
303 | };
|
304 |
|
305 | var validYearMonthDay = function (y, m, d) {
|
306 | la(check.number(y) && check.number(m) && check.number(d), "invalid year or month or day", y, m, d);
|
307 | return isYear(y) && isMonth(m) && isDay(d);
|
308 | };
|
309 |
|
310 | var isFormat = function (regex, str) {
|
311 | la(check.instance(regex, RegExp), "expected regular expression", regex);
|
312 | la(check.string(str), "expected string", str);
|
313 | return regex.test(str);
|
314 | };
|
315 |
|
316 | var validIndices = check.schema.bind(null, {
|
317 | year: check.number,
|
318 | month: check.number,
|
319 | day: check.number
|
320 | });
|
321 |
|
322 | var parseString = function (regex, indices, str) {
|
323 | la(check.instance(regex, RegExp));
|
324 | la(check.object(indices) && validIndices(indices), "missing indices", indices);
|
325 | la(check.string(str), "missing date string", str);
|
326 | var initial = check.unemptyString(str) && isFormat(regex, str);
|
327 | if (!initial) {
|
328 | return;
|
329 | }
|
330 | var matches = regex.exec(str);
|
331 | return {
|
332 | year: parseInt(matches[indices.year]),
|
333 | month: parseInt(matches[indices.month]),
|
334 | day: parseInt(matches[indices.day])
|
335 | };
|
336 | };
|
337 |
|
338 | var parseIfPossible = function (regex, indices, str) {
|
339 | var date = parseString(regex, indices, str);
|
340 | if (date) {
|
341 | la(validIndices(date), "missing date fields", date);
|
342 | return validYearMonthDay(date.year, date.month, date.day);
|
343 | }
|
344 | };
|
345 |
|
346 | var isYYYYMMDD = parseIfPossible.bind(null, /^(\d\d\d\d)[\-|\/](\d\d)\-(\d\d)$/, {
|
347 | year: 1,
|
348 | month: 2,
|
349 | day: 3
|
350 | });
|
351 |
|
352 | var isYYYYDDMM = parseIfPossible.bind(null, /^(\d\d\d\d)\-(\d\d)\-(\d\d)$/, {
|
353 | year: 1,
|
354 | month: 3,
|
355 | day: 2
|
356 | });
|
357 |
|
358 | var isDDMMYYYY = parseIfPossible.bind(null, /^(\d\d)[-|\/](\d\d?)[-|\/](\d\d\d\d)$/, {
|
359 | year: 3,
|
360 | month: 2,
|
361 | day: 1
|
362 | });
|
363 |
|
364 | var isMMDDYYYY = parseIfPossible.bind(null, /^(\d\d)[-|\/](\d\d?)[-|\/](\d\d\d\d)$/, {
|
365 | day: 2,
|
366 | month: 1,
|
367 | year: 3 });
|
368 |
|
369 | function lift(fn) {
|
370 | return function lifted(arr) {
|
371 | return Array.isArray(arr) ? arr.every(fn) : fn(arr);
|
372 | };
|
373 | }
|
374 |
|
375 | var formats = {
|
376 | "YYYY-MM-DD": lift(isYYYYMMDD),
|
377 | "YYYY-DD-MM": lift(isYYYYDDMM),
|
378 | "DD-MM-YYYY": lift(isDDMMYYYY),
|
379 | "MM-DD-YYYY": lift(isMMDDYYYY)
|
380 | };
|
381 |
|
382 | function findMatchedFormats(strings) {
|
383 | var matchedFormats = [];
|
384 | Object.keys(formats).forEach(function (format) {
|
385 | var formatCheck = formats[format];
|
386 | la(check.fn(formatCheck), "expected check function", format, formatCheck);
|
387 | if (formatCheck(strings)) {
|
388 | matchedFormats.push(format);
|
389 | }
|
390 | });
|
391 | return matchedFormats;
|
392 | }
|
393 |
|
394 | function guessDateFormat(strings) {
|
395 | if (!arguments.length) {
|
396 | return;
|
397 | }
|
398 |
|
399 | var matchedFormats = findMatchedFormats(strings);
|
400 | la(check.array(matchedFormats), "expected array result", matchedFormats, strings);
|
401 |
|
402 | if (matchedFormats.length !== 1) {
|
403 | // no matches or ambiguous dates
|
404 | return;
|
405 | }
|
406 |
|
407 | return matchedFormats[0];
|
408 | }
|
409 |
|
410 | module.exports = guessDateFormat;
|
411 |
|
412 | /***/ },
|
413 | /* 6 */
|
414 | /***/ function(module, exports, __webpack_require__) {
|
415 |
|
416 | /*
|
417 | Returns true only if for the given list of predicates,
|
418 | only single one is true, and the rest are false.
|
419 |
|
420 | onlyTrue(true, false, false); // true
|
421 | onlyTrue(false, false, false); // false
|
422 | onlyTrue(false, true, true); // false
|
423 | onlyTrue(false, false, true); // true
|
424 | */
|
425 | ;
|
426 |
|
427 | function onlyTrue() {
|
428 | var predicates = Array.prototype.slice.call(arguments, 0);
|
429 | if (!predicates.length) {
|
430 | return false;
|
431 | }
|
432 | var count = 0,
|
433 | k;
|
434 | for (k = 0; k < predicates.length; k += 1) {
|
435 | if (predicates[k]) {
|
436 | count += 1;
|
437 | if (count > 1) {
|
438 | return false;
|
439 | }
|
440 | }
|
441 | }
|
442 |
|
443 | return count === 1;
|
444 | }
|
445 |
|
446 | module.exports = onlyTrue;
|
447 |
|
448 | /***/ },
|
449 | /* 7 */
|
450 | /***/ function(module, exports, __webpack_require__) {
|
451 |
|
452 | ;
|
453 |
|
454 | __webpack_require__(9);
|
455 | var check = __webpack_require__(10);
|
456 | var _ = __webpack_require__(8);
|
457 | la(check.fn(_.has), "missing lodash.has method, version upgrade?", _.VERSION);
|
458 |
|
459 | function presentProperties(testProperties, list) {
|
460 | la(check.arrayOf(check.unemptyString, testProperties), "missing test properties", testProperties);
|
461 | la(check.array(list), "missing list of objects", list);
|
462 |
|
463 | return testProperties.filter(function (key) {
|
464 | return list.every(function (object) {
|
465 | return _.has(object, key);
|
466 | });
|
467 | });
|
468 | }
|
469 |
|
470 | module.exports = _.curry(presentProperties);
|
471 |
|
472 | /***/ },
|
473 | /* 8 */
|
474 | /***/ function(module, exports, __webpack_require__) {
|
475 |
|
476 | module.exports = __WEBPACK_EXTERNAL_MODULE_8__;
|
477 |
|
478 | /***/ },
|
479 | /* 9 */
|
480 | /***/ function(module, exports, __webpack_require__) {
|
481 |
|
482 | /* WEBPACK VAR INJECTION */(function(global) {(function initLazyAss() {
|
483 |
|
484 | function isArrayLike(a) {
|
485 | return a && typeof a.length === 'number';
|
486 | }
|
487 |
|
488 | function formMessage(args) {
|
489 | var msg = args.reduce(function (total, arg, k) {
|
490 | if (k) {
|
491 | total += ' ';
|
492 | }
|
493 | if (typeof arg === 'string') {
|
494 | return total + arg;
|
495 | }
|
496 | if (typeof arg === 'function') {
|
497 | var fnResult;
|
498 | try {
|
499 | fnResult = arg();
|
500 | } catch (err) {
|
501 | // ignore the error
|
502 | fnResult = '[function ' + arg.name + ' threw error!]';
|
503 | }
|
504 | return total + fnResult;
|
505 | }
|
506 | if (Array.isArray(arg)) {
|
507 | return total + JSON.stringify(arg);
|
508 | }
|
509 | if (isArrayLike(arg)) {
|
510 | return total + JSON.stringify(Array.prototype.slice.call(arg));
|
511 | }
|
512 | if (arg instanceof Error) {
|
513 | return total + arg.name + ' ' + arg.message;
|
514 | }
|
515 | var argString;
|
516 | try {
|
517 | argString = JSON.stringify(arg, null, 2);
|
518 | } catch (err) {
|
519 | argString = '[cannot stringify arg ' + k + ']';
|
520 | }
|
521 | return total + argString;
|
522 | }, '');
|
523 | return msg;
|
524 | }
|
525 |
|
526 | function lazyAssLogic(condition) {
|
527 | var fn = typeof condition === 'function' ? condition : null;
|
528 |
|
529 | if (fn) {
|
530 | condition = fn();
|
531 | }
|
532 | if (!condition) {
|
533 | var args = [].slice.call(arguments, 1);
|
534 | if (fn) {
|
535 | args.unshift(fn.toString());
|
536 | }
|
537 | return new Error(formMessage(args));
|
538 | }
|
539 | }
|
540 |
|
541 | var lazyAss = function lazyAss() {
|
542 | var err = lazyAssLogic.apply(null, arguments);
|
543 | if (err) {
|
544 | throw err;
|
545 | }
|
546 | };
|
547 |
|
548 | var lazyAssync = function lazyAssync() {
|
549 | var err = lazyAssLogic.apply(null, arguments);
|
550 | if (err) {
|
551 | setTimeout(function () {
|
552 | throw err;
|
553 | }, 0);
|
554 | }
|
555 | };
|
556 |
|
557 | function register(value, name) {
|
558 | if (typeof window === 'object') {
|
559 | /* global window */
|
560 | window[name] = value;
|
561 | } else if (typeof global === 'object') {
|
562 | global[name] = value;
|
563 | } else {
|
564 | throw new Error('Do not know how to register ' + name);
|
565 | }
|
566 | }
|
567 |
|
568 | register(lazyAss, 'lazyAss');
|
569 | register(lazyAss, 'la');
|
570 | register(lazyAssync, 'lazyAssync');
|
571 | register(lazyAssync, 'lac');
|
572 |
|
573 | }());
|
574 |
|
575 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))
|
576 |
|
577 | /***/ },
|
578 | /* 10 */
|
579 | /***/ function(module, exports, __webpack_require__) {
|
580 |
|
581 | /* WEBPACK VAR INJECTION */(function(global) {(function checkMoreTypes(check) {
|
582 | ;
|
583 |
|
584 | /**
|
585 | Custom assertions and predicates for https://github.com/philbooth/check-types.js
|
586 | Created by Kensho https://github.com/kensho
|
587 | Copyright @ 2014 Kensho https://www.kensho.com/
|
588 | License: MIT
|
589 |
|
590 | @module check
|
591 | */
|
592 |
|
593 | if (!check) {
|
594 | if (false) {
|
595 | throw new Error('Cannot find check-types library, has it been loaded?');
|
596 | }
|
597 | check = __webpack_require__(12);
|
598 | }
|
599 |
|
600 | /**
|
601 | Checks if argument is defined or not
|
602 |
|
603 | This method now is part of the check-types.js
|
604 | @method defined
|
605 | */
|
606 | function defined(value) {
|
607 | return typeof value !== 'undefined';
|
608 | }
|
609 |
|
610 | /**
|
611 | same as ===
|
612 |
|
613 | @method same
|
614 | */
|
615 | function same(a, b) {
|
616 | return a === b;
|
617 | }
|
618 |
|
619 | /**
|
620 | Returns true if the index is valid for give string / array
|
621 |
|
622 | @method index
|
623 | */
|
624 | function index(list, k) {
|
625 | return defined(list) &&
|
626 | has(list, 'length') &&
|
627 | k >= 0 &&
|
628 | k < list.length;
|
629 | }
|
630 |
|
631 | /**
|
632 | Returns true if both objects are the same type and have same length property
|
633 |
|
634 | @method sameLength
|
635 | */
|
636 | function sameLength(a, b) {
|
637 | return typeof a === typeof b &&
|
638 | a && b &&
|
639 | a.length === b.length;
|
640 | }
|
641 |
|
642 | /**
|
643 | Returns true if all items in an array are the same reference
|
644 |
|
645 | @method allSame
|
646 | */
|
647 | function allSame(arr) {
|
648 | if (!check.array(arr)) {
|
649 | return false;
|
650 | }
|
651 | if (!arr.length) {
|
652 | return true;
|
653 | }
|
654 | var first = arr[0];
|
655 | return arr.every(function (item) {
|
656 | return item === first;
|
657 | });
|
658 | }
|
659 |
|
660 | /**
|
661 | Returns true if given item is in the array
|
662 |
|
663 | @method oneOf
|
664 | */
|
665 | function oneOf(arr, x) {
|
666 | check.verify.array(arr, 'expected an array');
|
667 | return arr.indexOf(x) !== -1;
|
668 | }
|
669 |
|
670 | /**
|
671 | Returns true for urls of the format `git@....git`
|
672 |
|
673 | @method git
|
674 | */
|
675 | function git(url) {
|
676 | return check.unemptyString(url) &&
|
677 | /^git@/.test(url);
|
678 | }
|
679 |
|
680 | /**
|
681 | Checks if given value is 0 or 1
|
682 |
|
683 | @method bit
|
684 | */
|
685 | function bit(value) {
|
686 | return value === 0 || value === 1;
|
687 | }
|
688 |
|
689 | /**
|
690 | Checks if given value is true of false
|
691 |
|
692 | @method bool
|
693 | */
|
694 | function bool(value) {
|
695 | return typeof value === 'boolean';
|
696 | }
|
697 |
|
698 | /**
|
699 | Checks if given object has a property
|
700 | @method has
|
701 | */
|
702 | function has(o, property) {
|
703 | return Boolean(o && property &&
|
704 | typeof property === 'string' &&
|
705 | typeof o[property] !== 'undefined');
|
706 | }
|
707 |
|
708 | /**
|
709 | Checks if given string is already in lower case
|
710 | @method lowerCase
|
711 | */
|
712 | function lowerCase(str) {
|
713 | return check.string(str) &&
|
714 | str.toLowerCase() === str;
|
715 | }
|
716 |
|
717 | /**
|
718 | Returns true if the argument is an array with at least one value
|
719 | @method unemptyArray
|
720 | */
|
721 | function unemptyArray(a) {
|
722 | return check.array(a) && a.length > 0;
|
723 | }
|
724 |
|
725 | /**
|
726 | Returns true if each item in the array passes the predicate
|
727 | @method arrayOf
|
728 | @param rule Predicate function
|
729 | @param a Array to check
|
730 | */
|
731 | function arrayOf(rule, a) {
|
732 | return check.array(a) && a.every(rule);
|
733 | }
|
734 |
|
735 | /**
|
736 | Returns items from array that do not passes the predicate
|
737 | @method badItems
|
738 | @param rule Predicate function
|
739 | @param a Array with items
|
740 | */
|
741 | function badItems(rule, a) {
|
742 | check.verify.array(a, 'expected array to find bad items');
|
743 | return a.filter(notModifier(rule));
|
744 | }
|
745 |
|
746 | /**
|
747 | Returns true if given array only has strings
|
748 | @method arrayOfStrings
|
749 | @param a Array to check
|
750 | @param checkLowerCase Checks if all strings are lowercase
|
751 | */
|
752 | function arrayOfStrings(a, checkLowerCase) {
|
753 | var v = check.array(a) && a.every(check.string);
|
754 | if (v && check.bool(checkLowerCase) && checkLowerCase) {
|
755 | return a.every(check.lowerCase);
|
756 | }
|
757 | return v;
|
758 | }
|
759 |
|
760 | /**
|
761 | Returns true if given argument is array of arrays of strings
|
762 | @method arrayOfArraysOfStrings
|
763 | @param a Array to check
|
764 | @param checkLowerCase Checks if all strings are lowercase
|
765 | */
|
766 | function arrayOfArraysOfStrings(a, checkLowerCase) {
|
767 | return check.array(a) && a.every(function (arr) {
|
768 | return check.arrayOfStrings(arr, checkLowerCase);
|
769 | });
|
770 | }
|
771 |
|
772 | /**
|
773 | Checks if object passes all rules in predicates.
|
774 |
|
775 | check.all({ foo: 'foo' }, { foo: check.string }, 'wrong object');
|
776 |
|
777 | This is a composition of check.every(check.map ...) calls
|
778 | https://github.com/philbooth/check-types.js#batch-operations
|
779 |
|
780 | @method all
|
781 | @param {object} object object to check
|
782 | @param {object} predicates rules to check. Usually one per property.
|
783 | @public
|
784 | @returns true or false
|
785 | */
|
786 | function all(obj, predicates) {
|
787 | check.verify.object(obj, 'missing object to check');
|
788 | check.verify.object(predicates, 'missing predicates object');
|
789 | Object.keys(predicates).forEach(function (property) {
|
790 | check.verify.fn(predicates[property], 'not a predicate function for ' + property);
|
791 | });
|
792 | return check.every(check.map(obj, predicates));
|
793 | }
|
794 |
|
795 | /**
|
796 | Checks given object against predicates object
|
797 | @method schema
|
798 | */
|
799 | function schema(predicates, obj) {
|
800 | return all(obj, predicates);
|
801 | }
|
802 |
|
803 | /** Checks if given function raises an error
|
804 |
|
805 | @method raises
|
806 | */
|
807 | function raises(fn, errorValidator) {
|
808 | check.verify.fn(fn, 'expected function that raises');
|
809 | try {
|
810 | fn();
|
811 | } catch (err) {
|
812 | if (typeof errorValidator === 'undefined') {
|
813 | return true;
|
814 | }
|
815 | if (typeof errorValidator === 'function') {
|
816 | return errorValidator(err);
|
817 | }
|
818 | return false;
|
819 | }
|
820 | // error has not been raised
|
821 | return false;
|
822 | }
|
823 |
|
824 | /**
|
825 | Returns true if given value is ''
|
826 | @method emptyString
|
827 | */
|
828 | function emptyString(a) {
|
829 | return a === '';
|
830 | }
|
831 |
|
832 | /**
|
833 | Returns true if given value is [], {} or ''
|
834 | @method empty
|
835 | */
|
836 | function empty(a) {
|
837 | var hasLength = typeof a === 'string' ||
|
838 | Array.isArray(a);
|
839 | if (hasLength) {
|
840 | return !a.length;
|
841 | }
|
842 | if (a instanceof Object) {
|
843 | return !Object.keys(a).length;
|
844 | }
|
845 | return false;
|
846 | }
|
847 |
|
848 | /**
|
849 | Returns true if given value has .length and it is not zero, or has properties
|
850 | @method unempty
|
851 | */
|
852 | function unempty(a) {
|
853 | var hasLength = typeof a === 'string' ||
|
854 | Array.isArray(a);
|
855 | if (hasLength) {
|
856 | return a.length;
|
857 | }
|
858 | if (a instanceof Object) {
|
859 | return Object.keys(a).length;
|
860 | }
|
861 | return true;
|
862 | }
|
863 |
|
864 | /**
|
865 | Returns true if 0 <= value <= 1
|
866 | @method unit
|
867 | */
|
868 | function unit(value) {
|
869 | return check.number(value) &&
|
870 | value >= 0.0 && value <= 1.0;
|
871 | }
|
872 |
|
873 | var rgb = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
|
874 | /**
|
875 | Returns true if value is hex RGB between '#000000' and '#FFFFFF'
|
876 | @method hexRgb
|
877 | */
|
878 | function hexRgb(value) {
|
879 | return check.string(value) &&
|
880 | rgb.test(value);
|
881 | }
|
882 |
|
883 | // typical git SHA commit id is 40 digit hex string, like
|
884 | // 3b819803cdf2225ca1338beb17e0c506fdeedefc
|
885 | var shaReg = /^[0-9a-f]{40}$/;
|
886 |
|
887 | /**
|
888 | Returns true if the given string is 40 digit SHA commit id
|
889 | @method commitId
|
890 | */
|
891 | function commitId(id) {
|
892 | return check.string(id) &&
|
893 | id.length === 40 &&
|
894 | shaReg.test(id);
|
895 | }
|
896 |
|
897 | // when using git log --oneline short ids are displayed, first 7 characters
|
898 | var shortShaReg = /^[0-9a-f]{7}$/;
|
899 |
|
900 | /**
|
901 | Returns true if the given string is short 7 character SHA id part
|
902 | @method shortCommitId
|
903 | */
|
904 | function shortCommitId(id) {
|
905 | return check.string(id) &&
|
906 | id.length === 7 &&
|
907 | shortShaReg.test(id);
|
908 | }
|
909 |
|
910 | //
|
911 | // helper methods
|
912 | //
|
913 |
|
914 | if (!check.defend) {
|
915 | var checkPredicates = function checksPredicates(fn, predicates, args) {
|
916 | check.verify.fn(fn, 'expected a function');
|
917 | check.verify.array(predicates, 'expected list of predicates');
|
918 | check.verify.defined(args, 'missing args');
|
919 |
|
920 | var k = 0, // iterates over predicates
|
921 | j = 0, // iterates over arguments
|
922 | n = predicates.length;
|
923 |
|
924 | for (k = 0; k < n; k += 1) {
|
925 | var predicate = predicates[k];
|
926 | if (!check.fn(predicate)) {
|
927 | continue;
|
928 | }
|
929 |
|
930 | if (!predicate.call(null, args[j])) {
|
931 | var msg = 'Argument ' + (j + 1) + ': ' + args[j] + ' does not pass predicate';
|
932 | if (check.unemptyString(predicates[k + 1])) {
|
933 | msg += ': ' + predicates[k + 1];
|
934 | }
|
935 | throw new Error(msg);
|
936 | }
|
937 |
|
938 | j += 1;
|
939 | }
|
940 | return fn.apply(null, args);
|
941 | };
|
942 |
|
943 | check.defend = function defend(fn) {
|
944 | var predicates = Array.prototype.slice.call(arguments, 1);
|
945 | return function () {
|
946 | return checkPredicates(fn, predicates, arguments);
|
947 | };
|
948 | };
|
949 | }
|
950 |
|
951 | /**
|
952 | * Public modifier `not`.
|
953 | *
|
954 | * Negates `predicate`.
|
955 | * copied from check-types.js
|
956 | */
|
957 | function notModifier(predicate) {
|
958 | return function () {
|
959 | return !predicate.apply(null, arguments);
|
960 | };
|
961 | }
|
962 |
|
963 | if (!check.mixin) {
|
964 | /** Adds new predicate to all objects
|
965 | @method mixin */
|
966 | check.mixin = function mixin(fn, name) {
|
967 | check.verify.fn(fn, 'expected predicate function');
|
968 | if (!check.unemptyString(name)) {
|
969 | name = fn.name;
|
970 | }
|
971 | check.verify.unemptyString(name, 'predicate function missing name\n' + fn.toString());
|
972 |
|
973 | function registerPredicate(obj, name, fn) {
|
974 | check.verify.object(obj, 'missing object');
|
975 | check.verify.unemptyString(name, 'missing name');
|
976 | check.verify.fn(fn, 'missing function');
|
977 |
|
978 | if (!obj[name]) {
|
979 | obj[name] = fn;
|
980 | }
|
981 | }
|
982 |
|
983 | /**
|
984 | * Public modifier `maybe`.
|
985 | *
|
986 | * Returns `true` if `predicate` is `null` or `undefined`,
|
987 | * otherwise propagates the return value from `predicate`.
|
988 | * copied from check-types.js
|
989 | */
|
990 | function maybeModifier(predicate) {
|
991 | return function () {
|
992 | if (!check.defined(arguments[0]) || check.nulled(arguments[0])) {
|
993 | return true;
|
994 | }
|
995 | return predicate.apply(null, arguments);
|
996 | };
|
997 | }
|
998 |
|
999 | /**
|
1000 | * Public modifier `verify`.
|
1001 | *
|
1002 | * Throws if `predicate` returns `false`.
|
1003 | * copied from check-types.js
|
1004 | */
|
1005 | function verifyModifier(predicate, defaultMessage) {
|
1006 | return function () {
|
1007 | var message;
|
1008 | if (predicate.apply(null, arguments) === false) {
|
1009 | message = arguments[arguments.length - 1];
|
1010 | throw new Error(check.unemptyString(message) ? message : defaultMessage);
|
1011 | }
|
1012 | };
|
1013 | }
|
1014 |
|
1015 | registerPredicate(check, name, fn);
|
1016 | registerPredicate(check.maybe, name, maybeModifier(fn));
|
1017 | registerPredicate(check.not, name, notModifier(fn));
|
1018 | registerPredicate(check.verify, name, verifyModifier(fn, name + ' failed'));
|
1019 | };
|
1020 | }
|
1021 |
|
1022 | if (!check.then) {
|
1023 | /**
|
1024 | Executes given function only if condition is truthy.
|
1025 | @method then
|
1026 | */
|
1027 | check.then = function then(condition, fn) {
|
1028 | return function () {
|
1029 | var ok = typeof condition === 'function' ?
|
1030 | condition.apply(null, arguments) : condition;
|
1031 | if (ok) {
|
1032 | return fn.apply(null, arguments);
|
1033 | }
|
1034 | };
|
1035 | };
|
1036 | }
|
1037 |
|
1038 | var promiseSchema = {
|
1039 | then: check.fn
|
1040 | };
|
1041 |
|
1042 | // work around reserved keywords checks
|
1043 | promiseSchema['catch'] = check.fn;
|
1044 | promiseSchema['finally'] = check.fn;
|
1045 |
|
1046 | var hasPromiseApi = schema.bind(null, promiseSchema);
|
1047 |
|
1048 | /**
|
1049 | Returns true if argument implements promise api (.then, .catch, .finally)
|
1050 | @method promise
|
1051 | */
|
1052 | function isPromise(p) {
|
1053 | return typeof p === 'object' &&
|
1054 | hasPromiseApi(p);
|
1055 | }
|
1056 |
|
1057 | // new predicates to be added to check object. Use object to preserve names
|
1058 | var predicates = {
|
1059 | defined: defined,
|
1060 | same: same,
|
1061 | allSame: allSame,
|
1062 | bit: bit,
|
1063 | bool: bool,
|
1064 | has: has,
|
1065 | lowerCase: lowerCase,
|
1066 | unemptyArray: unemptyArray,
|
1067 | arrayOfStrings: arrayOfStrings,
|
1068 | arrayOfArraysOfStrings: arrayOfArraysOfStrings,
|
1069 | all: all,
|
1070 | schema: schema,
|
1071 | raises: raises,
|
1072 | empty: empty,
|
1073 | emptyString: emptyString,
|
1074 | unempty: unempty,
|
1075 | unit: unit,
|
1076 | hexRgb: hexRgb,
|
1077 | sameLength: sameLength,
|
1078 | commitId: commitId,
|
1079 | shortCommitId: shortCommitId,
|
1080 | index: index,
|
1081 | git: git,
|
1082 | arrayOf: arrayOf,
|
1083 | badItems: badItems,
|
1084 | oneOf: oneOf,
|
1085 | promise: isPromise
|
1086 | };
|
1087 |
|
1088 | Object.keys(predicates).forEach(function (name) {
|
1089 | check.mixin(predicates[name], name);
|
1090 | });
|
1091 |
|
1092 | if (true) {
|
1093 | module.exports = check;
|
1094 | }
|
1095 | }(typeof window === 'object' ? window.check : global.check));
|
1096 |
|
1097 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))
|
1098 |
|
1099 | /***/ },
|
1100 | /* 11 */
|
1101 | /***/ function(module, exports, __webpack_require__) {
|
1102 |
|
1103 | // Ramda v0.14.0
|
1104 | // https://github.com/ramda/ramda
|
1105 | // (c) 2013-2015 Scott Sauyet, Michael Hurley, and David Chambers
|
1106 | // Ramda may be freely distributed under the MIT license.
|
1107 |
|
1108 | ;(function() {
|
1109 |
|
1110 | ;
|
1111 |
|
1112 | /**
|
1113 | * A special placeholder value used to specify "gaps" within curried functions,
|
1114 | * allowing partial application of any combination of arguments,
|
1115 | * regardless of their positions.
|
1116 | *
|
1117 | * If `g` is a curried ternary function and `_` is `R.__`, the following are equivalent:
|
1118 | *
|
1119 | * - `g(1, 2, 3)`
|
1120 | * - `g(_, 2, 3)(1)`
|
1121 | * - `g(_, _, 3)(1)(2)`
|
1122 | * - `g(_, _, 3)(1, 2)`
|
1123 | * - `g(_, 2, _)(1, 3)`
|
1124 | * - `g(_, 2)(1)(3)`
|
1125 | * - `g(_, 2)(1, 3)`
|
1126 | * - `g(_, 2)(_, 3)(1)`
|
1127 | *
|
1128 | * @constant
|
1129 | * @memberOf R
|
1130 | * @category Function
|
1131 | * @example
|
1132 | *
|
1133 | * var greet = R.replace('{name}', R.__, 'Hello, {name}!');
|
1134 | * greet('Alice'); //=> 'Hello, Alice!'
|
1135 | */
|
1136 | var __ = { ramda: 'placeholder' };
|
1137 |
|
1138 | var _add = function _add(a, b) {
|
1139 | return a + b;
|
1140 | };
|
1141 |
|
1142 | var _all = function _all(fn, list) {
|
1143 | var idx = -1;
|
1144 | while (++idx < list.length) {
|
1145 | if (!fn(list[idx])) {
|
1146 | return false;
|
1147 | }
|
1148 | }
|
1149 | return true;
|
1150 | };
|
1151 |
|
1152 | var _any = function _any(fn, list) {
|
1153 | var idx = -1;
|
1154 | while (++idx < list.length) {
|
1155 | if (fn(list[idx])) {
|
1156 | return true;
|
1157 | }
|
1158 | }
|
1159 | return false;
|
1160 | };
|
1161 |
|
1162 | var _assoc = function _assoc(prop, val, obj) {
|
1163 | var result = {};
|
1164 | for (var p in obj) {
|
1165 | result[p] = obj[p];
|
1166 | }
|
1167 | result[prop] = val;
|
1168 | return result;
|
1169 | };
|
1170 |
|
1171 | var _cloneRegExp = function _cloneRegExp(pattern) {
|
1172 | return new RegExp(pattern.source, (pattern.global ? 'g' : '') + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '') + (pattern.sticky ? 'y' : '') + (pattern.unicode ? 'u' : ''));
|
1173 | };
|
1174 |
|
1175 | var _complement = function _complement(f) {
|
1176 | return function () {
|
1177 | return !f.apply(this, arguments);
|
1178 | };
|
1179 | };
|
1180 |
|
1181 | /**
|
1182 | * Basic, right-associative composition function. Accepts two functions and returns the
|
1183 | * composite function; this composite function represents the operation `var h = f(g(x))`,
|
1184 | * where `f` is the first argument, `g` is the second argument, and `x` is whatever
|
1185 | * argument(s) are passed to `h`.
|
1186 | *
|
1187 | * This function's main use is to build the more general `compose` function, which accepts
|
1188 | * any number of functions.
|
1189 | *
|
1190 | * @private
|
1191 | * @category Function
|
1192 | * @param {Function} f A function.
|
1193 | * @param {Function} g A function.
|
1194 | * @return {Function} A new function that is the equivalent of `f(g(x))`.
|
1195 | * @example
|
1196 | *
|
1197 | * var double = function(x) { return x * 2; };
|
1198 | * var square = function(x) { return x * x; };
|
1199 | * var squareThenDouble = _compose(double, square);
|
1200 | *
|
1201 | * squareThenDouble(5); //≅ double(square(5)) => 50
|
1202 | */
|
1203 | var _compose = function _compose(f, g) {
|
1204 | return function () {
|
1205 | return f.call(this, g.apply(this, arguments));
|
1206 | };
|
1207 | };
|
1208 |
|
1209 | /**
|
1210 | * Private `concat` function to merge two array-like objects.
|
1211 | *
|
1212 | * @private
|
1213 | * @param {Array|Arguments} [set1=[]] An array-like object.
|
1214 | * @param {Array|Arguments} [set2=[]] An array-like object.
|
1215 | * @return {Array} A new, merged array.
|
1216 | * @example
|
1217 | *
|
1218 | * _concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
|
1219 | */
|
1220 | var _concat = function _concat(set1, set2) {
|
1221 | set1 = set1 || [];
|
1222 | set2 = set2 || [];
|
1223 | var idx;
|
1224 | var len1 = set1.length;
|
1225 | var len2 = set2.length;
|
1226 | var result = [];
|
1227 | idx = -1;
|
1228 | while (++idx < len1) {
|
1229 | result[result.length] = set1[idx];
|
1230 | }
|
1231 | idx = -1;
|
1232 | while (++idx < len2) {
|
1233 | result[result.length] = set2[idx];
|
1234 | }
|
1235 | return result;
|
1236 | };
|
1237 |
|
1238 | var _containsWith = function _containsWith(pred, x, list) {
|
1239 | var idx = -1, len = list.length;
|
1240 | while (++idx < len) {
|
1241 | if (pred(x, list[idx])) {
|
1242 | return true;
|
1243 | }
|
1244 | }
|
1245 | return false;
|
1246 | };
|
1247 |
|
1248 | var _createMapEntry = function _createMapEntry(key, val) {
|
1249 | var obj = {};
|
1250 | obj[key] = val;
|
1251 | return obj;
|
1252 | };
|
1253 |
|
1254 | /**
|
1255 | * Create a function which takes a comparator function and a list
|
1256 | * and determines the winning value by a compatator. Used internally
|
1257 | * by `R.maxBy` and `R.minBy`
|
1258 | *
|
1259 | * @private
|
1260 | * @param {Function} compatator a function to compare two items
|
1261 | * @category Math
|
1262 | * @return {Function}
|
1263 | */
|
1264 | var _createMaxMinBy = function _createMaxMinBy(comparator) {
|
1265 | return function (valueComputer, list) {
|
1266 | if (!(list && list.length > 0)) {
|
1267 | return;
|
1268 | }
|
1269 | var idx = 0;
|
1270 | var winner = list[idx];
|
1271 | var computedWinner = valueComputer(winner);
|
1272 | var computedCurrent;
|
1273 | while (++idx < list.length) {
|
1274 | computedCurrent = valueComputer(list[idx]);
|
1275 | if (comparator(computedCurrent, computedWinner)) {
|
1276 | computedWinner = computedCurrent;
|
1277 | winner = list[idx];
|
1278 | }
|
1279 | }
|
1280 | return winner;
|
1281 | };
|
1282 | };
|
1283 |
|
1284 | /**
|
1285 | * Optimized internal two-arity curry function.
|
1286 | *
|
1287 | * @private
|
1288 | * @category Function
|
1289 | * @param {Function} fn The function to curry.
|
1290 | * @return {Function} The curried function.
|
1291 | */
|
1292 | var _curry1 = function _curry1(fn) {
|
1293 | return function f1(a) {
|
1294 | if (arguments.length === 0) {
|
1295 | return f1;
|
1296 | } else if (a === __) {
|
1297 | return f1;
|
1298 | } else {
|
1299 | return fn(a);
|
1300 | }
|
1301 | };
|
1302 | };
|
1303 |
|
1304 | /**
|
1305 | * Optimized internal two-arity curry function.
|
1306 | *
|
1307 | * @private
|
1308 | * @category Function
|
1309 | * @param {Function} fn The function to curry.
|
1310 | * @return {Function} The curried function.
|
1311 | */
|
1312 | var _curry2 = function _curry2(fn) {
|
1313 | return function f2(a, b) {
|
1314 | var n = arguments.length;
|
1315 | if (n === 0) {
|
1316 | return f2;
|
1317 | } else if (n === 1 && a === __) {
|
1318 | return f2;
|
1319 | } else if (n === 1) {
|
1320 | return _curry1(function (b) {
|
1321 | return fn(a, b);
|
1322 | });
|
1323 | } else if (n === 2 && a === __ && b === __) {
|
1324 | return f2;
|
1325 | } else if (n === 2 && a === __) {
|
1326 | return _curry1(function (a) {
|
1327 | return fn(a, b);
|
1328 | });
|
1329 | } else if (n === 2 && b === __) {
|
1330 | return _curry1(function (b) {
|
1331 | return fn(a, b);
|
1332 | });
|
1333 | } else {
|
1334 | return fn(a, b);
|
1335 | }
|
1336 | };
|
1337 | };
|
1338 |
|
1339 | /**
|
1340 | * Optimized internal three-arity curry function.
|
1341 | *
|
1342 | * @private
|
1343 | * @category Function
|
1344 | * @param {Function} fn The function to curry.
|
1345 | * @return {Function} The curried function.
|
1346 | */
|
1347 | var _curry3 = function _curry3(fn) {
|
1348 | return function f3(a, b, c) {
|
1349 | var n = arguments.length;
|
1350 | if (n === 0) {
|
1351 | return f3;
|
1352 | } else if (n === 1 && a === __) {
|
1353 | return f3;
|
1354 | } else if (n === 1) {
|
1355 | return _curry2(function (b, c) {
|
1356 | return fn(a, b, c);
|
1357 | });
|
1358 | } else if (n === 2 && a === __ && b === __) {
|
1359 | return f3;
|
1360 | } else if (n === 2 && a === __) {
|
1361 | return _curry2(function (a, c) {
|
1362 | return fn(a, b, c);
|
1363 | });
|
1364 | } else if (n === 2 && b === __) {
|
1365 | return _curry2(function (b, c) {
|
1366 | return fn(a, b, c);
|
1367 | });
|
1368 | } else if (n === 2) {
|
1369 | return _curry1(function (c) {
|
1370 | return fn(a, b, c);
|
1371 | });
|
1372 | } else if (n === 3 && a === __ && b === __ && c === __) {
|
1373 | return f3;
|
1374 | } else if (n === 3 && a === __ && b === __) {
|
1375 | return _curry2(function (a, b) {
|
1376 | return fn(a, b, c);
|
1377 | });
|
1378 | } else if (n === 3 && a === __ && c === __) {
|
1379 | return _curry2(function (a, c) {
|
1380 | return fn(a, b, c);
|
1381 | });
|
1382 | } else if (n === 3 && b === __ && c === __) {
|
1383 | return _curry2(function (b, c) {
|
1384 | return fn(a, b, c);
|
1385 | });
|
1386 | } else if (n === 3 && a === __) {
|
1387 | return _curry1(function (a) {
|
1388 | return fn(a, b, c);
|
1389 | });
|
1390 | } else if (n === 3 && b === __) {
|
1391 | return _curry1(function (b) {
|
1392 | return fn(a, b, c);
|
1393 | });
|
1394 | } else if (n === 3 && c === __) {
|
1395 | return _curry1(function (c) {
|
1396 | return fn(a, b, c);
|
1397 | });
|
1398 | } else {
|
1399 | return fn(a, b, c);
|
1400 | }
|
1401 | };
|
1402 | };
|
1403 |
|
1404 | var _dissoc = function _dissoc(prop, obj) {
|
1405 | var result = {};
|
1406 | for (var p in obj) {
|
1407 | if (p !== prop) {
|
1408 | result[p] = obj[p];
|
1409 | }
|
1410 | }
|
1411 | return result;
|
1412 | };
|
1413 |
|
1414 | var _eq = function _eq(a, b) {
|
1415 | if (a === 0) {
|
1416 | return 1 / a === 1 / b;
|
1417 | } else {
|
1418 | return a === b || a !== a && b !== b;
|
1419 | }
|
1420 | };
|
1421 |
|
1422 | var _filter = function _filter(fn, list) {
|
1423 | var idx = -1, len = list.length, result = [];
|
1424 | while (++idx < len) {
|
1425 | if (fn(list[idx])) {
|
1426 | result[result.length] = list[idx];
|
1427 | }
|
1428 | }
|
1429 | return result;
|
1430 | };
|
1431 |
|
1432 | var _filterIndexed = function _filterIndexed(fn, list) {
|
1433 | var idx = -1, len = list.length, result = [];
|
1434 | while (++idx < len) {
|
1435 | if (fn(list[idx], idx, list)) {
|
1436 | result[result.length] = list[idx];
|
1437 | }
|
1438 | }
|
1439 | return result;
|
1440 | };
|
1441 |
|
1442 | // i can't bear not to return *something*
|
1443 | var _forEach = function _forEach(fn, list) {
|
1444 | var idx = -1, len = list.length;
|
1445 | while (++idx < len) {
|
1446 | fn(list[idx]);
|
1447 | }
|
1448 | // i can't bear not to return *something*
|
1449 | return list;
|
1450 | };
|
1451 |
|
1452 | /**
|
1453 | * @private
|
1454 | * @param {Function} fn The strategy for extracting function names from an object
|
1455 | * @return {Function} A function that takes an object and returns an array of function names.
|
1456 | */
|
1457 | var _functionsWith = function _functionsWith(fn) {
|
1458 | return function (obj) {
|
1459 | return _filter(function (key) {
|
1460 | return typeof obj[key] === 'function';
|
1461 | }, fn(obj));
|
1462 | };
|
1463 | };
|
1464 |
|
1465 | var _gt = function _gt(a, b) {
|
1466 | return a > b;
|
1467 | };
|
1468 |
|
1469 | var _has = function _has(prop, obj) {
|
1470 | return Object.prototype.hasOwnProperty.call(obj, prop);
|
1471 | };
|
1472 |
|
1473 | var _identity = function _identity(x) {
|
1474 | return x;
|
1475 | };
|
1476 |
|
1477 | var _indexOf = function _indexOf(list, item, from) {
|
1478 | var idx = 0, len = list.length;
|
1479 | if (typeof from == 'number') {
|
1480 | idx = from < 0 ? Math.max(0, len + from) : from;
|
1481 | }
|
1482 | while (idx < len) {
|
1483 | if (_eq(list[idx], item)) {
|
1484 | return idx;
|
1485 | }
|
1486 | ++idx;
|
1487 | }
|
1488 | return -1;
|
1489 | };
|
1490 |
|
1491 | /**
|
1492 | * Tests whether or not an object is an array.
|
1493 | *
|
1494 | * @private
|
1495 | * @param {*} val The object to test.
|
1496 | * @return {Boolean} `true` if `val` is an array, `false` otherwise.
|
1497 | * @example
|
1498 | *
|
1499 | * _isArray([]); //=> true
|
1500 | * _isArray(null); //=> false
|
1501 | * _isArray({}); //=> false
|
1502 | */
|
1503 | var _isArray = Array.isArray || function _isArray(val) {
|
1504 | return val != null && val.length >= 0 && Object.prototype.toString.call(val) === '[object Array]';
|
1505 | };
|
1506 |
|
1507 | /**
|
1508 | * Determine if the passed argument is an integer.
|
1509 | *
|
1510 | * @private
|
1511 | * @param {*} n
|
1512 | * @category Type
|
1513 | * @return {Boolean}
|
1514 | */
|
1515 | var _isInteger = Number.isInteger || function _isInteger(n) {
|
1516 | return n << 0 === n;
|
1517 | };
|
1518 |
|
1519 | /**
|
1520 | * Tests if a value is a thenable (promise).
|
1521 | */
|
1522 | var _isThenable = function _isThenable(value) {
|
1523 | return value != null && value === Object(value) && typeof value.then === 'function';
|
1524 | };
|
1525 |
|
1526 | var _isTransformer = function _isTransformer(obj) {
|
1527 | return typeof obj['@@transducer/step'] === 'function';
|
1528 | };
|
1529 |
|
1530 | var _lastIndexOf = function _lastIndexOf(list, item, from) {
|
1531 | var idx = list.length;
|
1532 | if (typeof from == 'number') {
|
1533 | idx = from < 0 ? idx + from + 1 : Math.min(idx, from + 1);
|
1534 | }
|
1535 | while (--idx >= 0) {
|
1536 | if (_eq(list[idx], item)) {
|
1537 | return idx;
|
1538 | }
|
1539 | }
|
1540 | return -1;
|
1541 | };
|
1542 |
|
1543 | var _lt = function _lt(a, b) {
|
1544 | return a < b;
|
1545 | };
|
1546 |
|
1547 | var _map = function _map(fn, list) {
|
1548 | var idx = -1, len = list.length, result = [];
|
1549 | while (++idx < len) {
|
1550 | result[idx] = fn(list[idx]);
|
1551 | }
|
1552 | return result;
|
1553 | };
|
1554 |
|
1555 | var _multiply = function _multiply(a, b) {
|
1556 | return a * b;
|
1557 | };
|
1558 |
|
1559 | var _nth = function _nth(n, list) {
|
1560 | return n < 0 ? list[list.length + n] : list[n];
|
1561 | };
|
1562 |
|
1563 | /**
|
1564 | * internal path function
|
1565 | * Takes an array, paths, indicating the deep set of keys
|
1566 | * to find.
|
1567 | *
|
1568 | * @private
|
1569 | * @memberOf R
|
1570 | * @category Object
|
1571 | * @param {Array} paths An array of strings to map to object properties
|
1572 | * @param {Object} obj The object to find the path in
|
1573 | * @return {Array} The value at the end of the path or `undefined`.
|
1574 | * @example
|
1575 | *
|
1576 | * _path(['a', 'b'], {a: {b: 2}}); //=> 2
|
1577 | */
|
1578 | var _path = function _path(paths, obj) {
|
1579 | if (obj == null) {
|
1580 | return;
|
1581 | } else {
|
1582 | var val = obj;
|
1583 | for (var idx = 0, len = paths.length; idx < len && val != null; idx += 1) {
|
1584 | val = val[paths[idx]];
|
1585 | }
|
1586 | return val;
|
1587 | }
|
1588 | };
|
1589 |
|
1590 | var _prepend = function _prepend(el, list) {
|
1591 | return _concat([el], list);
|
1592 | };
|
1593 |
|
1594 | var _quote = function _quote(s) {
|
1595 | return '"' + s.replace(/"/g, '\\"') + '"';
|
1596 | };
|
1597 |
|
1598 | var _reduced = function (x) {
|
1599 | return x && x['@@transducer/reduced'] ? x : {
|
1600 | '@@transducer/value': x,
|
1601 | '@@transducer/reduced': true
|
1602 | };
|
1603 | };
|
1604 |
|
1605 | /**
|
1606 | * An optimized, private array `slice` implementation.
|
1607 | *
|
1608 | * @private
|
1609 | * @param {Arguments|Array} args The array or arguments object to consider.
|
1610 | * @param {Number} [from=0] The array index to slice from, inclusive.
|
1611 | * @param {Number} [to=args.length] The array index to slice to, exclusive.
|
1612 | * @return {Array} A new, sliced array.
|
1613 | * @example
|
1614 | *
|
1615 | * _slice([1, 2, 3, 4, 5], 1, 3); //=> [2, 3]
|
1616 | *
|
1617 | * var firstThreeArgs = function(a, b, c, d) {
|
1618 | * return _slice(arguments, 0, 3);
|
1619 | * };
|
1620 | * firstThreeArgs(1, 2, 3, 4); //=> [1, 2, 3]
|
1621 | */
|
1622 | var _slice = function _slice(args, from, to) {
|
1623 | switch (arguments.length) {
|
1624 | case 1:
|
1625 | return _slice(args, 0, args.length);
|
1626 | case 2:
|
1627 | return _slice(args, from, args.length);
|
1628 | default:
|
1629 | var list = [];
|
1630 | var idx = -1;
|
1631 | var len = Math.max(0, Math.min(args.length, to) - from);
|
1632 | while (++idx < len) {
|
1633 | list[idx] = args[from + idx];
|
1634 | }
|
1635 | return list;
|
1636 | }
|
1637 | };
|
1638 |
|
1639 | /**
|
1640 | * Polyfill from <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString>.
|
1641 | */
|
1642 | var _toISOString = function () {
|
1643 | var pad = function pad(n) {
|
1644 | return (n < 10 ? '0' : '') + n;
|
1645 | };
|
1646 | return typeof Date.prototype.toISOString === 'function' ? function _toISOString(d) {
|
1647 | return d.toISOString();
|
1648 | } : function _toISOString(d) {
|
1649 | 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';
|
1650 | };
|
1651 | }();
|
1652 |
|
1653 | var _xdropRepeatsWith = function () {
|
1654 | function XDropRepeatsWith(pred, xf) {
|
1655 | this.xf = xf;
|
1656 | this.pred = pred;
|
1657 | this.lastValue = undefined;
|
1658 | this.seenFirstValue = false;
|
1659 | }
|
1660 | XDropRepeatsWith.prototype['@@transducer/init'] = function () {
|
1661 | return this.xf['@@transducer/init']();
|
1662 | };
|
1663 | XDropRepeatsWith.prototype['@@transducer/result'] = function (result) {
|
1664 | return this.xf['@@transducer/result'](result);
|
1665 | };
|
1666 | XDropRepeatsWith.prototype['@@transducer/step'] = function (result, input) {
|
1667 | var sameAsLast = false;
|
1668 | if (!this.seenFirstValue) {
|
1669 | this.seenFirstValue = true;
|
1670 | } else if (this.pred(this.lastValue, input)) {
|
1671 | sameAsLast = true;
|
1672 | }
|
1673 | this.lastValue = input;
|
1674 | return sameAsLast ? result : this.xf['@@transducer/step'](result, input);
|
1675 | };
|
1676 | return _curry2(function _xdropRepeatsWith(pred, xf) {
|
1677 | return new XDropRepeatsWith(pred, xf);
|
1678 | });
|
1679 | }();
|
1680 |
|
1681 | var _xfBase = {
|
1682 | init: function () {
|
1683 | return this.xf['@@transducer/init']();
|
1684 | },
|
1685 | result: function (result) {
|
1686 | return this.xf['@@transducer/result'](result);
|
1687 | }
|
1688 | };
|
1689 |
|
1690 | var _xfilter = function () {
|
1691 | function XFilter(f, xf) {
|
1692 | this.xf = xf;
|
1693 | this.f = f;
|
1694 | }
|
1695 | XFilter.prototype['@@transducer/init'] = _xfBase.init;
|
1696 | XFilter.prototype['@@transducer/result'] = _xfBase.result;
|
1697 | XFilter.prototype['@@transducer/step'] = function (result, input) {
|
1698 | return this.f(input) ? this.xf['@@transducer/step'](result, input) : result;
|
1699 | };
|
1700 | return _curry2(function _xfilter(f, xf) {
|
1701 | return new XFilter(f, xf);
|
1702 | });
|
1703 | }();
|
1704 |
|
1705 | var _xfind = function () {
|
1706 | function XFind(f, xf) {
|
1707 | this.xf = xf;
|
1708 | this.f = f;
|
1709 | this.found = false;
|
1710 | }
|
1711 | XFind.prototype['@@transducer/init'] = _xfBase.init;
|
1712 | XFind.prototype['@@transducer/result'] = function (result) {
|
1713 | if (!this.found) {
|
1714 | result = this.xf['@@transducer/step'](result, void 0);
|
1715 | }
|
1716 | return this.xf['@@transducer/result'](result);
|
1717 | };
|
1718 | XFind.prototype['@@transducer/step'] = function (result, input) {
|
1719 | if (this.f(input)) {
|
1720 | this.found = true;
|
1721 | result = _reduced(this.xf['@@transducer/step'](result, input));
|
1722 | }
|
1723 | return result;
|
1724 | };
|
1725 | return _curry2(function _xfind(f, xf) {
|
1726 | return new XFind(f, xf);
|
1727 | });
|
1728 | }();
|
1729 |
|
1730 | var _xfindIndex = function () {
|
1731 | function XFindIndex(f, xf) {
|
1732 | this.xf = xf;
|
1733 | this.f = f;
|
1734 | this.idx = -1;
|
1735 | this.found = false;
|
1736 | }
|
1737 | XFindIndex.prototype['@@transducer/init'] = _xfBase.init;
|
1738 | XFindIndex.prototype['@@transducer/result'] = function (result) {
|
1739 | if (!this.found) {
|
1740 | result = this.xf['@@transducer/step'](result, -1);
|
1741 | }
|
1742 | return this.xf['@@transducer/result'](result);
|
1743 | };
|
1744 | XFindIndex.prototype['@@transducer/step'] = function (result, input) {
|
1745 | this.idx += 1;
|
1746 | if (this.f(input)) {
|
1747 | this.found = true;
|
1748 | result = _reduced(this.xf['@@transducer/step'](result, this.idx));
|
1749 | }
|
1750 | return result;
|
1751 | };
|
1752 | return _curry2(function _xfindIndex(f, xf) {
|
1753 | return new XFindIndex(f, xf);
|
1754 | });
|
1755 | }();
|
1756 |
|
1757 | var _xfindLast = function () {
|
1758 | function XFindLast(f, xf) {
|
1759 | this.xf = xf;
|
1760 | this.f = f;
|
1761 | }
|
1762 | XFindLast.prototype['@@transducer/init'] = _xfBase.init;
|
1763 | XFindLast.prototype['@@transducer/result'] = function (result) {
|
1764 | return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.last));
|
1765 | };
|
1766 | XFindLast.prototype['@@transducer/step'] = function (result, input) {
|
1767 | if (this.f(input)) {
|
1768 | this.last = input;
|
1769 | }
|
1770 | return result;
|
1771 | };
|
1772 | return _curry2(function _xfindLast(f, xf) {
|
1773 | return new XFindLast(f, xf);
|
1774 | });
|
1775 | }();
|
1776 |
|
1777 | var _xfindLastIndex = function () {
|
1778 | function XFindLastIndex(f, xf) {
|
1779 | this.xf = xf;
|
1780 | this.f = f;
|
1781 | this.idx = -1;
|
1782 | this.lastIdx = -1;
|
1783 | }
|
1784 | XFindLastIndex.prototype['@@transducer/init'] = _xfBase.init;
|
1785 | XFindLastIndex.prototype['@@transducer/result'] = function (result) {
|
1786 | return this.xf['@@transducer/result'](this.xf['@@transducer/step'](result, this.lastIdx));
|
1787 | };
|
1788 | XFindLastIndex.prototype['@@transducer/step'] = function (result, input) {
|
1789 | this.idx += 1;
|
1790 | if (this.f(input)) {
|
1791 | this.lastIdx = this.idx;
|
1792 | }
|
1793 | return result;
|
1794 | };
|
1795 | return _curry2(function _xfindLastIndex(f, xf) {
|
1796 | return new XFindLastIndex(f, xf);
|
1797 | });
|
1798 | }();
|
1799 |
|
1800 | var _xmap = function () {
|
1801 | function XMap(f, xf) {
|
1802 | this.xf = xf;
|
1803 | this.f = f;
|
1804 | }
|
1805 | XMap.prototype['@@transducer/init'] = _xfBase.init;
|
1806 | XMap.prototype['@@transducer/result'] = _xfBase.result;
|
1807 | XMap.prototype['@@transducer/step'] = function (result, input) {
|
1808 | return this.xf['@@transducer/step'](result, this.f(input));
|
1809 | };
|
1810 | return _curry2(function _xmap(f, xf) {
|
1811 | return new XMap(f, xf);
|
1812 | });
|
1813 | }();
|
1814 |
|
1815 | var _xtake = function () {
|
1816 | function XTake(n, xf) {
|
1817 | this.xf = xf;
|
1818 | this.n = n;
|
1819 | }
|
1820 | XTake.prototype['@@transducer/init'] = _xfBase.init;
|
1821 | XTake.prototype['@@transducer/result'] = _xfBase.result;
|
1822 | XTake.prototype['@@transducer/step'] = function (result, input) {
|
1823 | this.n -= 1;
|
1824 | return this.n === 0 ? _reduced(this.xf['@@transducer/step'](result, input)) : this.xf['@@transducer/step'](result, input);
|
1825 | };
|
1826 | return _curry2(function _xtake(n, xf) {
|
1827 | return new XTake(n, xf);
|
1828 | });
|
1829 | }();
|
1830 |
|
1831 | var _xtakeWhile = function () {
|
1832 | function XTakeWhile(f, xf) {
|
1833 | this.xf = xf;
|
1834 | this.f = f;
|
1835 | }
|
1836 | XTakeWhile.prototype['@@transducer/init'] = _xfBase.init;
|
1837 | XTakeWhile.prototype['@@transducer/result'] = _xfBase.result;
|
1838 | XTakeWhile.prototype['@@transducer/step'] = function (result, input) {
|
1839 | return this.f(input) ? this.xf['@@transducer/step'](result, input) : _reduced(result);
|
1840 | };
|
1841 | return _curry2(function _xtakeWhile(f, xf) {
|
1842 | return new XTakeWhile(f, xf);
|
1843 | });
|
1844 | }();
|
1845 |
|
1846 | var _xwrap = function () {
|
1847 | function XWrap(fn) {
|
1848 | this.f = fn;
|
1849 | }
|
1850 | XWrap.prototype['@@transducer/init'] = function () {
|
1851 | throw new Error('init not implemented on XWrap');
|
1852 | };
|
1853 | XWrap.prototype['@@transducer/result'] = function (acc) {
|
1854 | return acc;
|
1855 | };
|
1856 | XWrap.prototype['@@transducer/step'] = function (acc, x) {
|
1857 | return this.f(acc, x);
|
1858 | };
|
1859 | return function _xwrap(fn) {
|
1860 | return new XWrap(fn);
|
1861 | };
|
1862 | }();
|
1863 |
|
1864 | /**
|
1865 | * Adds two numbers (or strings). Equivalent to `a + b` but curried.
|
1866 | *
|
1867 | * @func
|
1868 | * @memberOf R
|
1869 | * @category Math
|
1870 | * @sig Number -> Number -> Number
|
1871 | * @sig String -> String -> String
|
1872 | * @param {Number|String} a The first value.
|
1873 | * @param {Number|String} b The second value.
|
1874 | * @return {Number|String} The result of `a + b`.
|
1875 | * @example
|
1876 | *
|
1877 | * R.add(2, 3); //=> 5
|
1878 | * R.add(7)(10); //=> 17
|
1879 | */
|
1880 | var add = _curry2(_add);
|
1881 |
|
1882 | /**
|
1883 | * Applies a function to the value at the given index of an array,
|
1884 | * returning a new copy of the array with the element at the given
|
1885 | * index replaced with the result of the function application.
|
1886 | *
|
1887 | * @func
|
1888 | * @memberOf R
|
1889 | * @category List
|
1890 | * @sig (a -> a) -> Number -> [a] -> [a]
|
1891 | * @param {Function} fn The function to apply.
|
1892 | * @param {Number} idx The index.
|
1893 | * @param {Array|Arguments} list An array-like object whose value
|
1894 | * at the supplied index will be replaced.
|
1895 | * @return {Array} A copy of the supplied array-like object with
|
1896 | * the element at index `idx` replaced with the value
|
1897 | * returned by applying `fn` to the existing element.
|
1898 | * @example
|
1899 | *
|
1900 | * R.adjust(R.add(10), 1, [0, 1, 2]); //=> [0, 11, 2]
|
1901 | * R.adjust(R.add(10))(1)([0, 1, 2]); //=> [0, 11, 2]
|
1902 | */
|
1903 | var adjust = _curry3(function (fn, idx, list) {
|
1904 | if (idx >= list.length || idx < -list.length) {
|
1905 | return list;
|
1906 | }
|
1907 | var start = idx < 0 ? list.length : 0;
|
1908 | var _idx = start + idx;
|
1909 | var _list = _concat(list);
|
1910 | _list[_idx] = fn(list[_idx]);
|
1911 | return _list;
|
1912 | });
|
1913 |
|
1914 | /**
|
1915 | * Returns a function that always returns the given value. Note that for non-primitives the value
|
1916 | * returned is a reference to the original value.
|
1917 | *
|
1918 | * @func
|
1919 | * @memberOf R
|
1920 | * @category Function
|
1921 | * @sig a -> (* -> a)
|
1922 | * @param {*} val The value to wrap in a function
|
1923 | * @return {Function} A Function :: * -> val.
|
1924 | * @example
|
1925 | *
|
1926 | * var t = R.always('Tee');
|
1927 | * t(); //=> 'Tee'
|
1928 | */
|
1929 | var always = _curry1(function always(val) {
|
1930 | return function () {
|
1931 | return val;
|
1932 | };
|
1933 | });
|
1934 |
|
1935 | /**
|
1936 | * Returns a new list, composed of n-tuples of consecutive elements
|
1937 | * If `n` is greater than the length of the list, an empty list is returned.
|
1938 | *
|
1939 | * @func
|
1940 | * @memberOf R
|
1941 | * @category List
|
1942 | * @sig Number -> [a] -> [[a]]
|
1943 | * @param {Number} n The size of the tuples to create
|
1944 | * @param {Array} list The list to split into `n`-tuples
|
1945 | * @return {Array} The new list.
|
1946 | * @example
|
1947 | *
|
1948 | * R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]]
|
1949 | * R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
|
1950 | * R.aperture(7, [1, 2, 3, 4, 5]); //=> []
|
1951 | */
|
1952 | var aperture = _curry2(function aperture(n, list) {
|
1953 | var idx = -1;
|
1954 | var limit = list.length - (n - 1);
|
1955 | var acc = new Array(limit >= 0 ? limit : 0);
|
1956 | while (++idx < limit) {
|
1957 | acc[idx] = _slice(list, idx, idx + n);
|
1958 | }
|
1959 | return acc;
|
1960 | });
|
1961 |
|
1962 | /**
|
1963 | * Applies function `fn` to the argument list `args`. This is useful for
|
1964 | * creating a fixed-arity function from a variadic function. `fn` should
|
1965 | * be a bound function if context is significant.
|
1966 | *
|
1967 | * @func
|
1968 | * @memberOf R
|
1969 | * @category Function
|
1970 | * @sig (*... -> a) -> [*] -> a
|
1971 | * @param {Function} fn
|
1972 | * @param {Array} args
|
1973 | * @return {*}
|
1974 | * @example
|
1975 | *
|
1976 | * var nums = [1, 2, 3, -99, 42, 6, 7];
|
1977 | * R.apply(Math.max, nums); //=> 42
|
1978 | */
|
1979 | var apply = _curry2(function apply(fn, args) {
|
1980 | return fn.apply(this, args);
|
1981 | });
|
1982 |
|
1983 | /**
|
1984 | * Wraps a function of any arity (including nullary) in a function that accepts exactly `n`
|
1985 | * parameters. Unlike `nAry`, which passes only `n` arguments to the wrapped function,
|
1986 | * functions produced by `arity` will pass all provided arguments to the wrapped function.
|
1987 | *
|
1988 | * @func
|
1989 | * @memberOf R
|
1990 | * @sig (Number, (* -> *)) -> (* -> *)
|
1991 | * @category Function
|
1992 | * @param {Number} n The desired arity of the returned function.
|
1993 | * @param {Function} fn The function to wrap.
|
1994 | * @return {Function} A new function wrapping `fn`. The new function is
|
1995 | * guaranteed to be of arity `n`.
|
1996 | * @example
|
1997 | *
|
1998 | * var takesTwoArgs = function(a, b) {
|
1999 | * return [a, b];
|
2000 | * };
|
2001 | * takesTwoArgs.length; //=> 2
|
2002 | * takesTwoArgs(1, 2); //=> [1, 2]
|
2003 | *
|
2004 | * var takesOneArg = R.arity(1, takesTwoArgs);
|
2005 | * takesOneArg.length; //=> 1
|
2006 | * // All arguments are passed through to the wrapped function
|
2007 | * takesOneArg(1, 2); //=> [1, 2]
|
2008 | */
|
2009 | var arity = _curry2(function (n, fn) {
|
2010 | switch (n) {
|
2011 | case 0:
|
2012 | return function () {
|
2013 | return fn.apply(this, arguments);
|
2014 | };
|
2015 | case 1:
|
2016 | return function (a0) {
|
2017 | void a0;
|
2018 | return fn.apply(this, arguments);
|
2019 | };
|
2020 | case 2:
|
2021 | return function (a0, a1) {
|
2022 | void a1;
|
2023 | return fn.apply(this, arguments);
|
2024 | };
|
2025 | case 3:
|
2026 | return function (a0, a1, a2) {
|
2027 | void a2;
|
2028 | return fn.apply(this, arguments);
|
2029 | };
|
2030 | case 4:
|
2031 | return function (a0, a1, a2, a3) {
|
2032 | void a3;
|
2033 | return fn.apply(this, arguments);
|
2034 | };
|
2035 | case 5:
|
2036 | return function (a0, a1, a2, a3, a4) {
|
2037 | void a4;
|
2038 | return fn.apply(this, arguments);
|
2039 | };
|
2040 | case 6:
|
2041 | return function (a0, a1, a2, a3, a4, a5) {
|
2042 | void a5;
|
2043 | return fn.apply(this, arguments);
|
2044 | };
|
2045 | case 7:
|
2046 | return function (a0, a1, a2, a3, a4, a5, a6) {
|
2047 | void a6;
|
2048 | return fn.apply(this, arguments);
|
2049 | };
|
2050 | case 8:
|
2051 | return function (a0, a1, a2, a3, a4, a5, a6, a7) {
|
2052 | void a7;
|
2053 | return fn.apply(this, arguments);
|
2054 | };
|
2055 | case 9:
|
2056 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
|
2057 | void a8;
|
2058 | return fn.apply(this, arguments);
|
2059 | };
|
2060 | case 10:
|
2061 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
|
2062 | void a9;
|
2063 | return fn.apply(this, arguments);
|
2064 | };
|
2065 | default:
|
2066 | throw new Error('First argument to arity must be a non-negative integer no greater than ten');
|
2067 | }
|
2068 | });
|
2069 |
|
2070 | /**
|
2071 | * Makes a shallow clone of an object, setting or overriding the specified
|
2072 | * property with the given value. Note that this copies and flattens
|
2073 | * prototype properties onto the new object as well. All non-primitive
|
2074 | * properties are copied by reference.
|
2075 | *
|
2076 | * @func
|
2077 | * @memberOf R
|
2078 | * @category Object
|
2079 | * @sig String -> a -> {k: v} -> {k: v}
|
2080 | * @param {String} prop the property name to set
|
2081 | * @param {*} val the new value
|
2082 | * @param {Object} obj the object to clone
|
2083 | * @return {Object} a new object similar to the original except for the specified property.
|
2084 | * @example
|
2085 | *
|
2086 | * R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}
|
2087 | */
|
2088 | var assoc = _curry3(_assoc);
|
2089 |
|
2090 | /**
|
2091 | * Creates a function that is bound to a context.
|
2092 | * Note: `R.bind` does not provide the additional argument-binding capabilities of
|
2093 | * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
|
2094 | *
|
2095 | * @func
|
2096 | * @memberOf R
|
2097 | * @category Function
|
2098 | * @category Object
|
2099 | * @see R.partial
|
2100 | * @sig (* -> *) -> {*} -> (* -> *)
|
2101 | * @param {Function} fn The function to bind to context
|
2102 | * @param {Object} thisObj The context to bind `fn` to
|
2103 | * @return {Function} A function that will execute in the context of `thisObj`.
|
2104 | */
|
2105 | var bind = _curry2(function bind(fn, thisObj) {
|
2106 | return arity(fn.length, function () {
|
2107 | return fn.apply(thisObj, arguments);
|
2108 | });
|
2109 | });
|
2110 |
|
2111 | /**
|
2112 | * A function wrapping calls to the two functions in an `&&` operation, returning the result of the first
|
2113 | * function if it is false-y and the result of the second function otherwise. Note that this is
|
2114 | * short-circuited, meaning that the second function will not be invoked if the first returns a false-y
|
2115 | * value.
|
2116 | *
|
2117 | * @func
|
2118 | * @memberOf R
|
2119 | * @category Logic
|
2120 | * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
|
2121 | * @param {Function} f a predicate
|
2122 | * @param {Function} g another predicate
|
2123 | * @return {Function} a function that applies its arguments to `f` and `g` and `&&`s their outputs together.
|
2124 | * @example
|
2125 | *
|
2126 | * var gt10 = function(x) { return x > 10; };
|
2127 | * var even = function(x) { return x % 2 === 0 };
|
2128 | * var f = R.both(gt10, even);
|
2129 | * f(100); //=> true
|
2130 | * f(101); //=> false
|
2131 | */
|
2132 | var both = _curry2(function both(f, g) {
|
2133 | return function _both() {
|
2134 | return f.apply(this, arguments) && g.apply(this, arguments);
|
2135 | };
|
2136 | });
|
2137 |
|
2138 | /**
|
2139 | * Makes a comparator function out of a function that reports whether the first element is less than the second.
|
2140 | *
|
2141 | * @func
|
2142 | * @memberOf R
|
2143 | * @category Function
|
2144 | * @sig (a, b -> Boolean) -> (a, b -> Number)
|
2145 | * @param {Function} pred A predicate function of arity two.
|
2146 | * @return {Function} A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`.
|
2147 | * @example
|
2148 | *
|
2149 | * var cmp = R.comparator(function(a, b) {
|
2150 | * return a.age < b.age;
|
2151 | * });
|
2152 | * var people = [
|
2153 | * // ...
|
2154 | * ];
|
2155 | * R.sort(cmp, people);
|
2156 | */
|
2157 | var comparator = _curry1(function comparator(pred) {
|
2158 | return function (a, b) {
|
2159 | return pred(a, b) ? -1 : pred(b, a) ? 1 : 0;
|
2160 | };
|
2161 | });
|
2162 |
|
2163 | /**
|
2164 | * Takes a function `f` and returns a function `g` such that:
|
2165 | *
|
2166 | * - applying `g` to zero or more arguments will give __true__ if applying
|
2167 | * the same arguments to `f` gives a logical __false__ value; and
|
2168 | *
|
2169 | * - applying `g` to zero or more arguments will give __false__ if applying
|
2170 | * the same arguments to `f` gives a logical __true__ value.
|
2171 | *
|
2172 | * @func
|
2173 | * @memberOf R
|
2174 | * @category Logic
|
2175 | * @sig (*... -> *) -> (*... -> Boolean)
|
2176 | * @param {Function} f
|
2177 | * @return {Function}
|
2178 | * @example
|
2179 | *
|
2180 | * var isEven = function(n) { return n % 2 === 0; };
|
2181 | * var isOdd = R.complement(isEven);
|
2182 | * isOdd(21); //=> true
|
2183 | * isOdd(42); //=> false
|
2184 | */
|
2185 | var complement = _curry1(_complement);
|
2186 |
|
2187 | /**
|
2188 | * Returns a function, `fn`, which encapsulates if/else-if/else logic.
|
2189 | * Each argument to `R.cond` is a [predicate, transform] pair. All of
|
2190 | * the arguments to `fn` are applied to each of the predicates in turn
|
2191 | * until one returns a "truthy" value, at which point `fn` returns the
|
2192 | * result of applying its arguments to the corresponding transformer.
|
2193 | * If none of the predicates matches, `fn` returns undefined.
|
2194 | *
|
2195 | * @func
|
2196 | * @memberOf R
|
2197 | * @category Logic
|
2198 | * @sig [(*... -> Boolean),(*... -> *)]... -> (*... -> *)
|
2199 | * @param {...Function} functions
|
2200 | * @return {Function}
|
2201 | * @example
|
2202 | *
|
2203 | * var fn = R.cond(
|
2204 | * [R.eq(0), R.always('water freezes at 0°C')],
|
2205 | * [R.eq(100), R.always('water boils at 100°C')],
|
2206 | * [R.T, function(temp) { return 'nothing special happens at ' + temp + '°C'; }]
|
2207 | * );
|
2208 | * fn(0); //=> 'water freezes at 0°C'
|
2209 | * fn(50); //=> 'nothing special happens at 50°C'
|
2210 | * fn(100); //=> 'water boils at 100°C'
|
2211 | */
|
2212 | var cond = function cond() {
|
2213 | var pairs = arguments;
|
2214 | return function () {
|
2215 | var idx = -1;
|
2216 | while (++idx < pairs.length) {
|
2217 | if (pairs[idx][0].apply(this, arguments)) {
|
2218 | return pairs[idx][1].apply(this, arguments);
|
2219 | }
|
2220 | }
|
2221 | };
|
2222 | };
|
2223 |
|
2224 | /**
|
2225 | * Returns `true` if the `x` is found in the `list`, using `pred` as an
|
2226 | * equality predicate for `x`.
|
2227 | *
|
2228 | * @func
|
2229 | * @memberOf R
|
2230 | * @category List
|
2231 | * @sig (a, a -> Boolean) -> a -> [a] -> Boolean
|
2232 | * @param {Function} pred A predicate used to test whether two items are equal.
|
2233 | * @param {*} x The item to find
|
2234 | * @param {Array} list The list to iterate over
|
2235 | * @return {Boolean} `true` if `x` is in `list`, else `false`.
|
2236 | * @example
|
2237 | *
|
2238 | * var xs = [{x: 12}, {x: 11}, {x: 10}];
|
2239 | * R.containsWith(function(a, b) { return a.x === b.x; }, {x: 10}, xs); //=> true
|
2240 | * R.containsWith(function(a, b) { return a.x === b.x; }, {x: 1}, xs); //=> false
|
2241 | */
|
2242 | var containsWith = _curry3(_containsWith);
|
2243 |
|
2244 | /**
|
2245 | * Counts the elements of a list according to how many match each value
|
2246 | * of a key generated by the supplied function. Returns an object
|
2247 | * mapping the keys produced by `fn` to the number of occurrences in
|
2248 | * the list. Note that all keys are coerced to strings because of how
|
2249 | * JavaScript objects work.
|
2250 | *
|
2251 | * @func
|
2252 | * @memberOf R
|
2253 | * @category Relation
|
2254 | * @sig (a -> String) -> [a] -> {*}
|
2255 | * @param {Function} fn The function used to map values to keys.
|
2256 | * @param {Array} list The list to count elements from.
|
2257 | * @return {Object} An object mapping keys to number of occurrences in the list.
|
2258 | * @example
|
2259 | *
|
2260 | * var numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
|
2261 | * var letters = R.split('', 'abcABCaaaBBc');
|
2262 | * R.countBy(Math.floor)(numbers); //=> {'1': 3, '2': 2, '3': 1}
|
2263 | * R.countBy(R.toLower)(letters); //=> {'a': 5, 'b': 4, 'c': 3}
|
2264 | */
|
2265 | var countBy = _curry2(function countBy(fn, list) {
|
2266 | var counts = {};
|
2267 | var len = list.length;
|
2268 | var idx = -1;
|
2269 | while (++idx < len) {
|
2270 | var key = fn(list[idx]);
|
2271 | counts[key] = (_has(key, counts) ? counts[key] : 0) + 1;
|
2272 | }
|
2273 | return counts;
|
2274 | });
|
2275 |
|
2276 | /**
|
2277 | * Creates an object containing a single key:value pair.
|
2278 | *
|
2279 | * @func
|
2280 | * @memberOf R
|
2281 | * @category Object
|
2282 | * @sig String -> a -> {String:a}
|
2283 | * @param {String} key
|
2284 | * @param {*} val
|
2285 | * @return {Object}
|
2286 | * @example
|
2287 | *
|
2288 | * var matchPhrases = R.compose(
|
2289 | * R.createMapEntry('must'),
|
2290 | * R.map(R.createMapEntry('match_phrase'))
|
2291 | * );
|
2292 | * matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]}
|
2293 | */
|
2294 | var createMapEntry = _curry2(_createMapEntry);
|
2295 |
|
2296 | /**
|
2297 | * Returns a curried equivalent of the provided function, with the
|
2298 | * specified arity. The curried function has two unusual capabilities.
|
2299 | * First, its arguments needn't be provided one at a time. If `g` is
|
2300 | * `R.curryN(3, f)`, the following are equivalent:
|
2301 | *
|
2302 | * - `g(1)(2)(3)`
|
2303 | * - `g(1)(2, 3)`
|
2304 | * - `g(1, 2)(3)`
|
2305 | * - `g(1, 2, 3)`
|
2306 | *
|
2307 | * Secondly, the special placeholder value `R.__` may be used to specify
|
2308 | * "gaps", allowing partial application of any combination of arguments,
|
2309 | * regardless of their positions. If `g` is as above and `_` is `R.__`,
|
2310 | * the following are equivalent:
|
2311 | *
|
2312 | * - `g(1, 2, 3)`
|
2313 | * - `g(_, 2, 3)(1)`
|
2314 | * - `g(_, _, 3)(1)(2)`
|
2315 | * - `g(_, _, 3)(1, 2)`
|
2316 | * - `g(_, 2)(1)(3)`
|
2317 | * - `g(_, 2)(1, 3)`
|
2318 | * - `g(_, 2)(_, 3)(1)`
|
2319 | *
|
2320 | * @func
|
2321 | * @memberOf R
|
2322 | * @category Function
|
2323 | * @sig Number -> (* -> a) -> (* -> a)
|
2324 | * @param {Number} length The arity for the returned function.
|
2325 | * @param {Function} fn The function to curry.
|
2326 | * @return {Function} A new, curried function.
|
2327 | * @see R.curry
|
2328 | * @example
|
2329 | *
|
2330 | * var addFourNumbers = function() {
|
2331 | * return R.sum([].slice.call(arguments, 0, 4));
|
2332 | * };
|
2333 | *
|
2334 | * var curriedAddFourNumbers = R.curryN(4, addFourNumbers);
|
2335 | * var f = curriedAddFourNumbers(1, 2);
|
2336 | * var g = f(3);
|
2337 | * g(4); //=> 10
|
2338 | */
|
2339 | var curryN = _curry2(function curryN(length, fn) {
|
2340 | return arity(length, function () {
|
2341 | var n = arguments.length;
|
2342 | var shortfall = length - n;
|
2343 | var idx = n;
|
2344 | while (--idx >= 0) {
|
2345 | if (arguments[idx] === __) {
|
2346 | shortfall += 1;
|
2347 | }
|
2348 | }
|
2349 | if (shortfall <= 0) {
|
2350 | return fn.apply(this, arguments);
|
2351 | } else {
|
2352 | var initialArgs = _slice(arguments);
|
2353 | return curryN(shortfall, function () {
|
2354 | var currentArgs = _slice(arguments);
|
2355 | var combinedArgs = [];
|
2356 | var idx = -1;
|
2357 | while (++idx < n) {
|
2358 | var val = initialArgs[idx];
|
2359 | combinedArgs[idx] = val === __ ? currentArgs.shift() : val;
|
2360 | }
|
2361 | return fn.apply(this, combinedArgs.concat(currentArgs));
|
2362 | });
|
2363 | }
|
2364 | });
|
2365 | });
|
2366 |
|
2367 | /**
|
2368 | * Decrements its argument.
|
2369 | *
|
2370 | * @func
|
2371 | * @memberOf R
|
2372 | * @category Math
|
2373 | * @sig Number -> Number
|
2374 | * @param {Number} n
|
2375 | * @return {Number}
|
2376 | * @example
|
2377 | *
|
2378 | * R.dec(42); //=> 41
|
2379 | */
|
2380 | var dec = add(-1);
|
2381 |
|
2382 | /**
|
2383 | * Returns the second argument if it is not null or undefined. If it is null
|
2384 | * or undefined, the first (default) argument is returned.
|
2385 | *
|
2386 | * @func
|
2387 | * @memberOf R
|
2388 | * @category Logic
|
2389 | * @sig a -> b -> a | b
|
2390 | * @param {a} val The default value.
|
2391 | * @param {b} val The value to return if it is not null or undefined
|
2392 | * @return {*} The the second value or the default value
|
2393 | * @example
|
2394 | *
|
2395 | * var defaultTo42 = defaultTo(42);
|
2396 | *
|
2397 | * defaultTo42(null); //=> 42
|
2398 | * defaultTo42(undefined); //=> 42
|
2399 | * defaultTo42('Ramda'); //=> 'Ramda'
|
2400 | */
|
2401 | var defaultTo = _curry2(function defaultTo(d, v) {
|
2402 | return v == null ? d : v;
|
2403 | });
|
2404 |
|
2405 | /**
|
2406 | * Finds the set (i.e. no duplicates) of all elements in the first list not contained in the second list.
|
2407 | * Duplication is determined according to the value returned by applying the supplied predicate to two list
|
2408 | * elements.
|
2409 | *
|
2410 | * @func
|
2411 | * @memberOf R
|
2412 | * @category Relation
|
2413 | * @sig (a,a -> Boolean) -> [a] -> [a] -> [a]
|
2414 | * @param {Function} pred A predicate used to test whether two items are equal.
|
2415 | * @param {Array} list1 The first list.
|
2416 | * @param {Array} list2 The second list.
|
2417 | * @see R.difference
|
2418 | * @return {Array} The elements in `list1` that are not in `list2`.
|
2419 | * @example
|
2420 | *
|
2421 | * function cmp(x, y) { return x.a === y.a; }
|
2422 | * var l1 = [{a: 1}, {a: 2}, {a: 3}];
|
2423 | * var l2 = [{a: 3}, {a: 4}];
|
2424 | * R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
|
2425 | */
|
2426 | var differenceWith = _curry3(function differenceWith(pred, first, second) {
|
2427 | var out = [];
|
2428 | var idx = -1;
|
2429 | var firstLen = first.length;
|
2430 | var containsPred = containsWith(pred);
|
2431 | while (++idx < firstLen) {
|
2432 | if (!containsPred(first[idx], second) && !containsPred(first[idx], out)) {
|
2433 | out[out.length] = first[idx];
|
2434 | }
|
2435 | }
|
2436 | return out;
|
2437 | });
|
2438 |
|
2439 | /**
|
2440 | * Returns a new object that does not contain a `prop` property.
|
2441 | *
|
2442 | * @func
|
2443 | * @memberOf R
|
2444 | * @category Object
|
2445 | * @sig String -> {k: v} -> {k: v}
|
2446 | * @param {String} prop the name of the property to dissociate
|
2447 | * @param {Object} obj the object to clone
|
2448 | * @return {Object} a new object similar to the original but without the specified property
|
2449 | * @example
|
2450 | *
|
2451 | * R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3}
|
2452 | */
|
2453 | var dissoc = _curry2(_dissoc);
|
2454 |
|
2455 | /**
|
2456 | * Divides two numbers. Equivalent to `a / b`.
|
2457 | *
|
2458 | * @func
|
2459 | * @memberOf R
|
2460 | * @category Math
|
2461 | * @sig Number -> Number -> Number
|
2462 | * @param {Number} a The first value.
|
2463 | * @param {Number} b The second value.
|
2464 | * @return {Number} The result of `a / b`.
|
2465 | * @example
|
2466 | *
|
2467 | * R.divide(71, 100); //=> 0.71
|
2468 | *
|
2469 | * var half = R.divide(R.__, 2);
|
2470 | * half(42); //=> 21
|
2471 | *
|
2472 | * var reciprocal = R.divide(1);
|
2473 | * reciprocal(4); //=> 0.25
|
2474 | */
|
2475 | var divide = _curry2(function divide(a, b) {
|
2476 | return a / b;
|
2477 | });
|
2478 |
|
2479 | /**
|
2480 | * A function wrapping calls to the two functions in an `||` operation, returning the result of the first
|
2481 | * function if it is truth-y and the result of the second function otherwise. Note that this is
|
2482 | * short-circuited, meaning that the second function will not be invoked if the first returns a truth-y
|
2483 | * value.
|
2484 | *
|
2485 | * @func
|
2486 | * @memberOf R
|
2487 | * @category Logic
|
2488 | * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
|
2489 | * @param {Function} f a predicate
|
2490 | * @param {Function} g another predicate
|
2491 | * @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
|
2492 | * @example
|
2493 | *
|
2494 | * var gt10 = function(x) { return x > 10; };
|
2495 | * var even = function(x) { return x % 2 === 0 };
|
2496 | * var f = R.either(gt10, even);
|
2497 | * f(101); //=> true
|
2498 | * f(8); //=> true
|
2499 | */
|
2500 | var either = _curry2(function either(f, g) {
|
2501 | return function _either() {
|
2502 | return f.apply(this, arguments) || g.apply(this, arguments);
|
2503 | };
|
2504 | });
|
2505 |
|
2506 | /**
|
2507 | * Tests if two items are equal. Equality is strict here, meaning reference equality for objects and
|
2508 | * non-coercing equality for primitives.
|
2509 | *
|
2510 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
2511 | * are not considered equal.
|
2512 | *
|
2513 | * @func
|
2514 | * @memberOf R
|
2515 | * @category Relation
|
2516 | * @sig a -> a -> Boolean
|
2517 | * @param {*} a
|
2518 | * @param {*} b
|
2519 | * @return {Boolean}
|
2520 | * @example
|
2521 | *
|
2522 | * var o = {};
|
2523 | * R.eq(o, o); //=> true
|
2524 | * R.eq(o, {}); //=> false
|
2525 | * R.eq(1, 1); //=> true
|
2526 | * R.eq(1, '1'); //=> false
|
2527 | * R.eq(0, -0); //=> false
|
2528 | * R.eq(NaN, NaN); //=> true
|
2529 | */
|
2530 | var eq = _curry2(_eq);
|
2531 |
|
2532 | /**
|
2533 | * Reports whether two objects have the same value for the specified property. Useful as a curried predicate.
|
2534 | *
|
2535 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
2536 | * are not considered equal.
|
2537 | *
|
2538 | * @func
|
2539 | * @memberOf R
|
2540 | * @category Object
|
2541 | * @sig k -> {k: v} -> {k: v} -> Boolean
|
2542 | * @param {String} prop The name of the property to compare
|
2543 | * @param {Object} obj1
|
2544 | * @param {Object} obj2
|
2545 | * @return {Boolean}
|
2546 | *
|
2547 | * @example
|
2548 | *
|
2549 | * var o1 = { a: 1, b: 2, c: 3, d: 4 };
|
2550 | * var o2 = { a: 10, b: 20, c: 3, d: 40 };
|
2551 | * R.eqProps('a', o1, o2); //=> false
|
2552 | * R.eqProps('c', o1, o2); //=> true
|
2553 | */
|
2554 | var eqProps = _curry3(function eqProps(prop, obj1, obj2) {
|
2555 | return _eq(obj1[prop], obj2[prop]);
|
2556 | });
|
2557 |
|
2558 | /**
|
2559 | * Like `filter`, but passes additional parameters to the predicate function. The predicate
|
2560 | * function is passed three arguments: *(value, index, list)*.
|
2561 | *
|
2562 | * @func
|
2563 | * @memberOf R
|
2564 | * @category List
|
2565 | * @sig (a, i, [a] -> Boolean) -> [a] -> [a]
|
2566 | * @param {Function} fn The function called per iteration.
|
2567 | * @param {Array} list The collection to iterate over.
|
2568 | * @return {Array} The new filtered array.
|
2569 | * @example
|
2570 | *
|
2571 | * var lastTwo = function(val, idx, list) {
|
2572 | * return list.length - idx <= 2;
|
2573 | * };
|
2574 | * R.filterIndexed(lastTwo, [8, 6, 7, 5, 3, 0, 9]); //=> [0, 9]
|
2575 | */
|
2576 | var filterIndexed = _curry2(_filterIndexed);
|
2577 |
|
2578 | /**
|
2579 | * Iterate over an input `list`, calling a provided function `fn` for each element in the
|
2580 | * list.
|
2581 | *
|
2582 | * `fn` receives one argument: *(value)*.
|
2583 | *
|
2584 | * Note: `R.forEach` does not skip deleted or unassigned indices (sparse arrays), unlike
|
2585 | * the native `Array.prototype.forEach` method. For more details on this behavior, see:
|
2586 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
|
2587 | *
|
2588 | * Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns the original
|
2589 | * array. In some libraries this function is named `each`.
|
2590 | *
|
2591 | * @func
|
2592 | * @memberOf R
|
2593 | * @category List
|
2594 | * @sig (a -> *) -> [a] -> [a]
|
2595 | * @param {Function} fn The function to invoke. Receives one argument, `value`.
|
2596 | * @param {Array} list The list to iterate over.
|
2597 | * @return {Array} The original list.
|
2598 | * @example
|
2599 | *
|
2600 | * var printXPlusFive = function(x) { console.log(x + 5); };
|
2601 | * R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
|
2602 | * //-> 6
|
2603 | * //-> 7
|
2604 | * //-> 8
|
2605 | */
|
2606 | var forEach = _curry2(_forEach);
|
2607 |
|
2608 | /**
|
2609 | * Like `forEach`, but but passes additional parameters to the predicate function.
|
2610 | *
|
2611 | * `fn` receives three arguments: *(value, index, list)*.
|
2612 | *
|
2613 | * Note: `R.forEachIndexed` does not skip deleted or unassigned indices (sparse arrays),
|
2614 | * unlike the native `Array.prototype.forEach` method. For more details on this behavior,
|
2615 | * see:
|
2616 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
|
2617 | *
|
2618 | * Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns the original
|
2619 | * array. In some libraries this function is named `each`.
|
2620 | *
|
2621 | * @func
|
2622 | * @memberOf R
|
2623 | * @category List
|
2624 | * @sig (a, i, [a] -> ) -> [a] -> [a]
|
2625 | * @param {Function} fn The function to invoke. Receives three arguments:
|
2626 | * (`value`, `index`, `list`).
|
2627 | * @param {Array} list The list to iterate over.
|
2628 | * @return {Array} The original list.
|
2629 | * @example
|
2630 | *
|
2631 | * // Note that having access to the original `list` allows for
|
2632 | * // mutation. While you *can* do this, it's very un-functional behavior:
|
2633 | * var plusFive = function(num, idx, list) { list[idx] = num + 5 };
|
2634 | * R.forEachIndexed(plusFive, [1, 2, 3]); //=> [6, 7, 8]
|
2635 | */
|
2636 | // i can't bear not to return *something*
|
2637 | var forEachIndexed = _curry2(function forEachIndexed(fn, list) {
|
2638 | var idx = -1, len = list.length;
|
2639 | while (++idx < len) {
|
2640 | fn(list[idx], idx, list);
|
2641 | }
|
2642 | // i can't bear not to return *something*
|
2643 | return list;
|
2644 | });
|
2645 |
|
2646 | /**
|
2647 | * Creates a new object out of a list key-value pairs.
|
2648 | *
|
2649 | * @func
|
2650 | * @memberOf R
|
2651 | * @category List
|
2652 | * @sig [[k,v]] -> {k: v}
|
2653 | * @param {Array} pairs An array of two-element arrays that will be the keys and values of the output object.
|
2654 | * @return {Object} The object made by pairing up `keys` and `values`.
|
2655 | * @example
|
2656 | *
|
2657 | * R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3}
|
2658 | */
|
2659 | var fromPairs = _curry1(function fromPairs(pairs) {
|
2660 | var idx = -1, len = pairs.length, out = {};
|
2661 | while (++idx < len) {
|
2662 | if (_isArray(pairs[idx]) && pairs[idx].length) {
|
2663 | out[pairs[idx][0]] = pairs[idx][1];
|
2664 | }
|
2665 | }
|
2666 | return out;
|
2667 | });
|
2668 |
|
2669 | /**
|
2670 | * Returns true if the first parameter is greater than the second.
|
2671 | *
|
2672 | * @func
|
2673 | * @memberOf R
|
2674 | * @category Math
|
2675 | * @sig Number -> Number -> Boolean
|
2676 | * @param {Number} a
|
2677 | * @param {Number} b
|
2678 | * @return {Boolean} a > b
|
2679 | * @example
|
2680 | *
|
2681 | * R.gt(2, 6); //=> false
|
2682 | * R.gt(2, 0); //=> true
|
2683 | * R.gt(2, 2); //=> false
|
2684 | * R.gt(R.__, 2)(10); //=> true
|
2685 | * R.gt(2)(10); //=> false
|
2686 | */
|
2687 | var gt = _curry2(_gt);
|
2688 |
|
2689 | /**
|
2690 | * Returns true if the first parameter is greater than or equal to the second.
|
2691 | *
|
2692 | * @func
|
2693 | * @memberOf R
|
2694 | * @category Math
|
2695 | * @sig Number -> Number -> Boolean
|
2696 | * @param {Number} a
|
2697 | * @param {Number} b
|
2698 | * @return {Boolean} a >= b
|
2699 | * @example
|
2700 | *
|
2701 | * R.gte(2, 6); //=> false
|
2702 | * R.gte(2, 0); //=> true
|
2703 | * R.gte(2, 2); //=> true
|
2704 | * R.gte(R.__, 6)(2); //=> false
|
2705 | * R.gte(2)(0); //=> true
|
2706 | */
|
2707 | var gte = _curry2(function gte(a, b) {
|
2708 | return a >= b;
|
2709 | });
|
2710 |
|
2711 | /**
|
2712 | * Returns whether or not an object has an own property with
|
2713 | * the specified name
|
2714 | *
|
2715 | * @func
|
2716 | * @memberOf R
|
2717 | * @category Object
|
2718 | * @sig s -> {s: x} -> Boolean
|
2719 | * @param {String} prop The name of the property to check for.
|
2720 | * @param {Object} obj The object to query.
|
2721 | * @return {Boolean} Whether the property exists.
|
2722 | * @example
|
2723 | *
|
2724 | * var hasName = R.has('name');
|
2725 | * hasName({name: 'alice'}); //=> true
|
2726 | * hasName({name: 'bob'}); //=> true
|
2727 | * hasName({}); //=> false
|
2728 | *
|
2729 | * var point = {x: 0, y: 0};
|
2730 | * var pointHas = R.has(R.__, point);
|
2731 | * pointHas('x'); //=> true
|
2732 | * pointHas('y'); //=> true
|
2733 | * pointHas('z'); //=> false
|
2734 | */
|
2735 | var has = _curry2(_has);
|
2736 |
|
2737 | /**
|
2738 | * Returns whether or not an object or its prototype chain has
|
2739 | * a property with the specified name
|
2740 | *
|
2741 | * @func
|
2742 | * @memberOf R
|
2743 | * @category Object
|
2744 | * @sig s -> {s: x} -> Boolean
|
2745 | * @param {String} prop The name of the property to check for.
|
2746 | * @param {Object} obj The object to query.
|
2747 | * @return {Boolean} Whether the property exists.
|
2748 | * @example
|
2749 | *
|
2750 | * function Rectangle(width, height) {
|
2751 | * this.width = width;
|
2752 | * this.height = height;
|
2753 | * }
|
2754 | * Rectangle.prototype.area = function() {
|
2755 | * return this.width * this.height;
|
2756 | * };
|
2757 | *
|
2758 | * var square = new Rectangle(2, 2);
|
2759 | * R.hasIn('width', square); //=> true
|
2760 | * R.hasIn('area', square); //=> true
|
2761 | */
|
2762 | var hasIn = _curry2(function (prop, obj) {
|
2763 | return prop in obj;
|
2764 | });
|
2765 |
|
2766 | /**
|
2767 | * A function that does nothing but return the parameter supplied to it. Good as a default
|
2768 | * or placeholder function.
|
2769 | *
|
2770 | * @func
|
2771 | * @memberOf R
|
2772 | * @category Function
|
2773 | * @sig a -> a
|
2774 | * @param {*} x The value to return.
|
2775 | * @return {*} The input value, `x`.
|
2776 | * @example
|
2777 | *
|
2778 | * R.identity(1); //=> 1
|
2779 | *
|
2780 | * var obj = {};
|
2781 | * R.identity(obj) === obj; //=> true
|
2782 | */
|
2783 | var identity = _curry1(_identity);
|
2784 |
|
2785 | /**
|
2786 | * Creates a function that will process either the `onTrue` or the `onFalse` function depending
|
2787 | * upon the result of the `condition` predicate.
|
2788 | *
|
2789 | * @func
|
2790 | * @memberOf R
|
2791 | * @category Logic
|
2792 | * @sig (*... -> Boolean) -> (*... -> *) -> (*... -> *) -> (*... -> *)
|
2793 | * @param {Function} condition A predicate function
|
2794 | * @param {Function} onTrue A function to invoke when the `condition` evaluates to a truthy value.
|
2795 | * @param {Function} onFalse A function to invoke when the `condition` evaluates to a falsy value.
|
2796 | * @return {Function} A new unary function that will process either the `onTrue` or the `onFalse`
|
2797 | * function depending upon the result of the `condition` predicate.
|
2798 | * @example
|
2799 | *
|
2800 | * // Flatten all arrays in the list but leave other values alone.
|
2801 | * var flattenArrays = R.map(R.ifElse(Array.isArray, R.flatten, R.identity));
|
2802 | *
|
2803 | * flattenArrays([[0], [[10], [8]], 1234, {}]); //=> [[0], [10, 8], 1234, {}]
|
2804 | * flattenArrays([[[10], 123], [8, [10]], "hello"]); //=> [[10, 123], [8, 10], "hello"]
|
2805 | */
|
2806 | var ifElse = _curry3(function ifElse(condition, onTrue, onFalse) {
|
2807 | return curryN(Math.max(condition.length, onTrue.length, onFalse.length), function _ifElse() {
|
2808 | return condition.apply(this, arguments) ? onTrue.apply(this, arguments) : onFalse.apply(this, arguments);
|
2809 | });
|
2810 | });
|
2811 |
|
2812 | /**
|
2813 | * Increments its argument.
|
2814 | *
|
2815 | * @func
|
2816 | * @memberOf R
|
2817 | * @category Math
|
2818 | * @sig Number -> Number
|
2819 | * @param {Number} n
|
2820 | * @return {Number}
|
2821 | * @example
|
2822 | *
|
2823 | * R.inc(42); //=> 43
|
2824 | */
|
2825 | var inc = add(1);
|
2826 |
|
2827 | /**
|
2828 | * Returns the position of the first occurrence of an item in an array,
|
2829 | * or -1 if the item is not included in the array.
|
2830 | *
|
2831 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
2832 | * are not considered equal.
|
2833 | *
|
2834 | * @func
|
2835 | * @memberOf R
|
2836 | * @category List
|
2837 | * @sig a -> [a] -> Number
|
2838 | * @param {*} target The item to find.
|
2839 | * @param {Array} list The array to search in.
|
2840 | * @return {Number} the index of the target, or -1 if the target is not found.
|
2841 | *
|
2842 | * @example
|
2843 | *
|
2844 | * R.indexOf(3, [1,2,3,4]); //=> 2
|
2845 | * R.indexOf(10, [1,2,3,4]); //=> -1
|
2846 | */
|
2847 | var indexOf = _curry2(function indexOf(target, list) {
|
2848 | return _indexOf(list, target);
|
2849 | });
|
2850 |
|
2851 | /**
|
2852 | * Inserts the sub-list into the list, at index `index`. _Note that this
|
2853 | * is not destructive_: it returns a copy of the list with the changes.
|
2854 | * <small>No lists have been harmed in the application of this function.</small>
|
2855 | *
|
2856 | * @func
|
2857 | * @memberOf R
|
2858 | * @category List
|
2859 | * @sig Number -> [a] -> [a] -> [a]
|
2860 | * @param {Number} index The position to insert the sub-list
|
2861 | * @param {Array} elts The sub-list to insert into the Array
|
2862 | * @param {Array} list The list to insert the sub-list into
|
2863 | * @return {Array} A new Array with `elts` inserted starting at `index`.
|
2864 | * @example
|
2865 | *
|
2866 | * R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4]
|
2867 | */
|
2868 | var insertAll = _curry3(function insertAll(idx, elts, list) {
|
2869 | idx = idx < list.length && idx >= 0 ? idx : list.length;
|
2870 | return _concat(_concat(_slice(list, 0, idx), elts), _slice(list, idx));
|
2871 | });
|
2872 |
|
2873 | /**
|
2874 | * See if an object (`val`) is an instance of the supplied constructor.
|
2875 | * This function will check up the inheritance chain, if any.
|
2876 | *
|
2877 | * @func
|
2878 | * @memberOf R
|
2879 | * @category Type
|
2880 | * @sig (* -> {*}) -> a -> Boolean
|
2881 | * @param {Object} ctor A constructor
|
2882 | * @param {*} val The value to test
|
2883 | * @return {Boolean}
|
2884 | * @example
|
2885 | *
|
2886 | * R.is(Object, {}); //=> true
|
2887 | * R.is(Number, 1); //=> true
|
2888 | * R.is(Object, 1); //=> false
|
2889 | * R.is(String, 's'); //=> true
|
2890 | * R.is(String, new String('')); //=> true
|
2891 | * R.is(Object, new String('')); //=> true
|
2892 | * R.is(Object, 's'); //=> false
|
2893 | * R.is(Number, {}); //=> false
|
2894 | */
|
2895 | var is = _curry2(function is(Ctor, val) {
|
2896 | return val != null && val.constructor === Ctor || val instanceof Ctor;
|
2897 | });
|
2898 |
|
2899 | /**
|
2900 | * Tests whether or not an object is similar to an array.
|
2901 | *
|
2902 | * @func
|
2903 | * @memberOf R
|
2904 | * @category Type
|
2905 | * @category List
|
2906 | * @sig * -> Boolean
|
2907 | * @param {*} x The object to test.
|
2908 | * @return {Boolean} `true` if `x` has a numeric length property and extreme indices defined; `false` otherwise.
|
2909 | * @example
|
2910 | *
|
2911 | * R.isArrayLike([]); //=> true
|
2912 | * R.isArrayLike(true); //=> false
|
2913 | * R.isArrayLike({}); //=> false
|
2914 | * R.isArrayLike({length: 10}); //=> false
|
2915 | * R.isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
|
2916 | */
|
2917 | var isArrayLike = _curry1(function isArrayLike(x) {
|
2918 | if (_isArray(x)) {
|
2919 | return true;
|
2920 | }
|
2921 | if (!x) {
|
2922 | return false;
|
2923 | }
|
2924 | if (typeof x !== 'object') {
|
2925 | return false;
|
2926 | }
|
2927 | if (x instanceof String) {
|
2928 | return false;
|
2929 | }
|
2930 | if (x.nodeType === 1) {
|
2931 | return !!x.length;
|
2932 | }
|
2933 | if (x.length === 0) {
|
2934 | return true;
|
2935 | }
|
2936 | if (x.length > 0) {
|
2937 | return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1);
|
2938 | }
|
2939 | return false;
|
2940 | });
|
2941 |
|
2942 | /**
|
2943 | * Reports whether the list has zero elements.
|
2944 | *
|
2945 | * @func
|
2946 | * @memberOf R
|
2947 | * @category Logic
|
2948 | * @sig [a] -> Boolean
|
2949 | * @param {Array} list
|
2950 | * @return {Boolean}
|
2951 | * @example
|
2952 | *
|
2953 | * R.isEmpty([1, 2, 3]); //=> false
|
2954 | * R.isEmpty([]); //=> true
|
2955 | * R.isEmpty(''); //=> true
|
2956 | * R.isEmpty(null); //=> false
|
2957 | */
|
2958 | var isEmpty = _curry1(function isEmpty(list) {
|
2959 | return Object(list).length === 0;
|
2960 | });
|
2961 |
|
2962 | /**
|
2963 | * Returns `true` if the input value is `NaN`.
|
2964 | *
|
2965 | * Equivalent to ES6's [`Number.isNaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN).
|
2966 | *
|
2967 | * @deprecated since v0.14.0
|
2968 | * @func
|
2969 | * @memberOf R
|
2970 | * @category Math
|
2971 | * @sig * -> Boolean
|
2972 | * @param {*} x
|
2973 | * @return {Boolean}
|
2974 | * @example
|
2975 | *
|
2976 | * R.isNaN(NaN); //=> true
|
2977 | * R.isNaN(undefined); //=> false
|
2978 | * R.isNaN({}); //=> false
|
2979 | */
|
2980 | var isNaN = _curry1(function isNaN(x) {
|
2981 | return typeof x === 'number' && x !== x;
|
2982 | });
|
2983 |
|
2984 | /**
|
2985 | * Checks if the input value is `null` or `undefined`.
|
2986 | *
|
2987 | * @func
|
2988 | * @memberOf R
|
2989 | * @category Type
|
2990 | * @sig * -> Boolean
|
2991 | * @param {*} x The value to test.
|
2992 | * @return {Boolean} `true` if `x` is `undefined` or `null`, otherwise `false`.
|
2993 | * @example
|
2994 | *
|
2995 | * R.isNil(null); //=> true
|
2996 | * R.isNil(undefined); //=> true
|
2997 | * R.isNil(0); //=> false
|
2998 | * R.isNil([]); //=> false
|
2999 | */
|
3000 | var isNil = _curry1(function isNil(x) {
|
3001 | return x == null;
|
3002 | });
|
3003 |
|
3004 | /**
|
3005 | * Returns `true` if all elements are unique, otherwise `false`.
|
3006 | *
|
3007 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
3008 | * are not considered equal.
|
3009 | *
|
3010 | * @func
|
3011 | * @memberOf R
|
3012 | * @category List
|
3013 | * @sig [a] -> Boolean
|
3014 | * @param {Array} list The array to consider.
|
3015 | * @return {Boolean} `true` if all elements are unique, else `false`.
|
3016 | * @example
|
3017 | *
|
3018 | * R.isSet(['1', 1]); //=> true
|
3019 | * R.isSet([1, 1]); //=> false
|
3020 | * R.isSet([{}, {}]); //=> true
|
3021 | */
|
3022 | var isSet = _curry1(function isSet(list) {
|
3023 | var len = list.length;
|
3024 | var idx = -1;
|
3025 | while (++idx < len) {
|
3026 | if (_indexOf(list, list[idx], idx + 1) >= 0) {
|
3027 | return false;
|
3028 | }
|
3029 | }
|
3030 | return true;
|
3031 | });
|
3032 |
|
3033 | /**
|
3034 | * Returns a list containing the names of all the
|
3035 | * properties of the supplied object, including prototype properties.
|
3036 | * Note that the order of the output array is not guaranteed to be
|
3037 | * consistent across different JS platforms.
|
3038 | *
|
3039 | * @func
|
3040 | * @memberOf R
|
3041 | * @category Object
|
3042 | * @sig {k: v} -> [k]
|
3043 | * @param {Object} obj The object to extract properties from
|
3044 | * @return {Array} An array of the object's own and prototype properties.
|
3045 | * @example
|
3046 | *
|
3047 | * var F = function() { this.x = 'X'; };
|
3048 | * F.prototype.y = 'Y';
|
3049 | * var f = new F();
|
3050 | * R.keysIn(f); //=> ['x', 'y']
|
3051 | */
|
3052 | var keysIn = _curry1(function keysIn(obj) {
|
3053 | var prop, ks = [];
|
3054 | for (prop in obj) {
|
3055 | ks[ks.length] = prop;
|
3056 | }
|
3057 | return ks;
|
3058 | });
|
3059 |
|
3060 | /**
|
3061 | * Returns the position of the last occurrence of an item in
|
3062 | * an array, or -1 if the item is not included in the array.
|
3063 | *
|
3064 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
3065 | * are not considered equal.
|
3066 | *
|
3067 | * @func
|
3068 | * @memberOf R
|
3069 | * @category List
|
3070 | * @sig a -> [a] -> Number
|
3071 | * @param {*} target The item to find.
|
3072 | * @param {Array} list The array to search in.
|
3073 | * @return {Number} the index of the target, or -1 if the target is not found.
|
3074 | *
|
3075 | * @example
|
3076 | *
|
3077 | * R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6
|
3078 | * R.lastIndexOf(10, [1,2,3,4]); //=> -1
|
3079 | */
|
3080 | var lastIndexOf = _curry2(function lastIndexOf(target, list) {
|
3081 | return _lastIndexOf(list, target);
|
3082 | });
|
3083 |
|
3084 | /**
|
3085 | * Returns the number of elements in the array by returning `list.length`.
|
3086 | *
|
3087 | * @func
|
3088 | * @memberOf R
|
3089 | * @category List
|
3090 | * @sig [a] -> Number
|
3091 | * @param {Array} list The array to inspect.
|
3092 | * @return {Number} The length of the array.
|
3093 | * @example
|
3094 | *
|
3095 | * R.length([]); //=> 0
|
3096 | * R.length([1, 2, 3]); //=> 3
|
3097 | */
|
3098 | var length = _curry1(function length(list) {
|
3099 | return list != null && is(Number, list.length) ? list.length : NaN;
|
3100 | });
|
3101 |
|
3102 | /**
|
3103 | * Creates a lens. Supply a function to `get` values from inside an object, and a `set`
|
3104 | * function to change values on an object. (n.b.: This can, and should, be done without
|
3105 | * mutating the original object!) The lens is a function wrapped around the input `get`
|
3106 | * function, with the `set` function attached as a property on the wrapper. A `map`
|
3107 | * function is also attached to the returned function that takes a function to operate
|
3108 | * on the specified (`get`) property, which is then `set` before returning. The attached
|
3109 | * `set` and `map` functions are curried.
|
3110 | *
|
3111 | * @func
|
3112 | * @memberOf R
|
3113 | * @category Object
|
3114 | * @sig (k -> v) -> (v -> a -> *) -> (a -> b)
|
3115 | * @param {Function} get A function that gets a value by property name
|
3116 | * @param {Function} set A function that sets a value by property name
|
3117 | * @return {Function} the returned function has `set` and `map` properties that are
|
3118 | * also curried functions.
|
3119 | * @example
|
3120 | *
|
3121 | * var headLens = R.lens(
|
3122 | * function get(arr) { return arr[0]; },
|
3123 | * function set(val, arr) { return [val].concat(arr.slice(1)); }
|
3124 | * );
|
3125 | * headLens([10, 20, 30, 40]); //=> 10
|
3126 | * headLens.set('mu', [10, 20, 30, 40]); //=> ['mu', 20, 30, 40]
|
3127 | * headLens.map(function(x) { return x + 1; }, [10, 20, 30, 40]); //=> [11, 20, 30, 40]
|
3128 | *
|
3129 | * var phraseLens = R.lens(
|
3130 | * function get(obj) { return obj.phrase; },
|
3131 | * function set(val, obj) {
|
3132 | * var out = R.clone(obj);
|
3133 | * out.phrase = val;
|
3134 | * return out;
|
3135 | * }
|
3136 | * );
|
3137 | * var obj1 = { phrase: 'Absolute filth . . . and I LOVED it!'};
|
3138 | * var obj2 = { phrase: "What's all this, then?"};
|
3139 | * phraseLens(obj1); // => 'Absolute filth . . . and I LOVED it!'
|
3140 | * phraseLens(obj2); // => "What's all this, then?"
|
3141 | * phraseLens.set('Ooh Betty', obj1); //=> { phrase: 'Ooh Betty'}
|
3142 | * phraseLens.map(R.toUpper, obj2); //=> { phrase: "WHAT'S ALL THIS, THEN?"}
|
3143 | */
|
3144 | var lens = _curry2(function lens(get, set) {
|
3145 | var lns = function (a) {
|
3146 | return get(a);
|
3147 | };
|
3148 | lns.set = _curry2(set);
|
3149 | lns.map = _curry2(function (fn, a) {
|
3150 | return set(fn(get(a)), a);
|
3151 | });
|
3152 | return lns;
|
3153 | });
|
3154 |
|
3155 | /**
|
3156 | * Returns a lens associated with the provided object.
|
3157 | *
|
3158 | * @func
|
3159 | * @memberOf R
|
3160 | * @category Object
|
3161 | * @sig ({} -> v) -> (v -> a -> *) -> {} -> (a -> b)
|
3162 | * @see R.lens
|
3163 | * @param {Function} get A function that gets a value by property name
|
3164 | * @param {Function} set A function that sets a value by property name
|
3165 | * @param {Object} the actual object of interest
|
3166 | * @return {Function} the returned function has `set` and `map` properties that are
|
3167 | * also curried functions.
|
3168 | * @example
|
3169 | *
|
3170 | * var xo = {x: 1};
|
3171 | * var xoLens = R.lensOn(function get(o) { return o.x; },
|
3172 | * function set(v) { return {x: v}; },
|
3173 | * xo);
|
3174 | * xoLens(); //=> 1
|
3175 | * xoLens.set(1000); //=> {x: 1000}
|
3176 | * xoLens.map(R.add(1)); //=> {x: 2}
|
3177 | */
|
3178 | var lensOn = _curry3(function lensOn(get, set, obj) {
|
3179 | var lns = function () {
|
3180 | return get(obj);
|
3181 | };
|
3182 | lns.set = set;
|
3183 | lns.map = function (fn) {
|
3184 | return set(fn(get(obj)));
|
3185 | };
|
3186 | return lns;
|
3187 | });
|
3188 |
|
3189 | /**
|
3190 | * Returns true if the first parameter is less than the second.
|
3191 | *
|
3192 | * @func
|
3193 | * @memberOf R
|
3194 | * @category Math
|
3195 | * @sig Number -> Number -> Boolean
|
3196 | * @param {Number} a
|
3197 | * @param {Number} b
|
3198 | * @return {Boolean} a < b
|
3199 | * @example
|
3200 | *
|
3201 | * R.lt(2, 6); //=> true
|
3202 | * R.lt(2, 0); //=> false
|
3203 | * R.lt(2, 2); //=> false
|
3204 | * R.lt(5)(10); //=> true
|
3205 | * R.lt(R.__, 5)(10); //=> false // right-sectioned currying
|
3206 | */
|
3207 | var lt = _curry2(_lt);
|
3208 |
|
3209 | /**
|
3210 | * Returns true if the first parameter is less than or equal to the second.
|
3211 | *
|
3212 | * @func
|
3213 | * @memberOf R
|
3214 | * @category Math
|
3215 | * @sig Number -> Number -> Boolean
|
3216 | * @param {Number} a
|
3217 | * @param {Number} b
|
3218 | * @return {Boolean} a <= b
|
3219 | * @example
|
3220 | *
|
3221 | * R.lte(2, 6); //=> true
|
3222 | * R.lte(2, 0); //=> false
|
3223 | * R.lte(2, 2); //=> true
|
3224 | * R.lte(R.__, 2)(1); //=> true
|
3225 | * R.lte(2)(10); //=> true
|
3226 | */
|
3227 | var lte = _curry2(function lte(a, b) {
|
3228 | return a <= b;
|
3229 | });
|
3230 |
|
3231 | /**
|
3232 | * The mapAccum function behaves like a combination of map and reduce; it applies a
|
3233 | * function to each element of a list, passing an accumulating parameter from left to
|
3234 | * right, and returning a final value of this accumulator together with the new list.
|
3235 | *
|
3236 | * The iterator function receives two arguments, *acc* and *value*, and should return
|
3237 | * a tuple *[acc, value]*.
|
3238 | *
|
3239 | * @func
|
3240 | * @memberOf R
|
3241 | * @category List
|
3242 | * @sig (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
|
3243 | * @param {Function} fn The function to be called on every element of the input `list`.
|
3244 | * @param {*} acc The accumulator value.
|
3245 | * @param {Array} list The list to iterate over.
|
3246 | * @return {*} The final, accumulated value.
|
3247 | * @example
|
3248 | *
|
3249 | * var digits = ['1', '2', '3', '4'];
|
3250 | * var append = function(a, b) {
|
3251 | * return [a + b, a + b];
|
3252 | * }
|
3253 | *
|
3254 | * R.mapAccum(append, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
|
3255 | */
|
3256 | var mapAccum = _curry3(function mapAccum(fn, acc, list) {
|
3257 | var idx = -1, len = list.length, result = [], tuple = [acc];
|
3258 | while (++idx < len) {
|
3259 | tuple = fn(tuple[0], list[idx]);
|
3260 | result[idx] = tuple[1];
|
3261 | }
|
3262 | return [
|
3263 | tuple[0],
|
3264 | result
|
3265 | ];
|
3266 | });
|
3267 |
|
3268 | /**
|
3269 | * The mapAccumRight function behaves like a combination of map and reduce; it applies a
|
3270 | * function to each element of a list, passing an accumulating parameter from right
|
3271 | * to left, and returning a final value of this accumulator together with the new list.
|
3272 | *
|
3273 | * Similar to `mapAccum`, except moves through the input list from the right to the
|
3274 | * left.
|
3275 | *
|
3276 | * The iterator function receives two arguments, *acc* and *value*, and should return
|
3277 | * a tuple *[acc, value]*.
|
3278 | *
|
3279 | * @func
|
3280 | * @memberOf R
|
3281 | * @category List
|
3282 | * @sig (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
|
3283 | * @param {Function} fn The function to be called on every element of the input `list`.
|
3284 | * @param {*} acc The accumulator value.
|
3285 | * @param {Array} list The list to iterate over.
|
3286 | * @return {*} The final, accumulated value.
|
3287 | * @example
|
3288 | *
|
3289 | * var digits = ['1', '2', '3', '4'];
|
3290 | * var append = function(a, b) {
|
3291 | * return [a + b, a + b];
|
3292 | * }
|
3293 | *
|
3294 | * R.mapAccumRight(append, 0, digits); //=> ['04321', ['04321', '0432', '043', '04']]
|
3295 | */
|
3296 | var mapAccumRight = _curry3(function mapAccumRight(fn, acc, list) {
|
3297 | var idx = list.length, result = [], tuple = [acc];
|
3298 | while (--idx >= 0) {
|
3299 | tuple = fn(tuple[0], list[idx]);
|
3300 | result[idx] = tuple[1];
|
3301 | }
|
3302 | return [
|
3303 | tuple[0],
|
3304 | result
|
3305 | ];
|
3306 | });
|
3307 |
|
3308 | /**
|
3309 | * Like `map`, but but passes additional parameters to the mapping function.
|
3310 | * `fn` receives three arguments: *(value, index, list)*.
|
3311 | *
|
3312 | * Note: `R.mapIndexed` does not skip deleted or unassigned indices (sparse arrays), unlike
|
3313 | * the native `Array.prototype.map` method. For more details on this behavior, see:
|
3314 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description
|
3315 | *
|
3316 | * @func
|
3317 | * @memberOf R
|
3318 | * @category List
|
3319 | * @sig (a,i,[b] -> b) -> [a] -> [b]
|
3320 | * @param {Function} fn The function to be called on every element of the input `list`.
|
3321 | * @param {Array} list The list to be iterated over.
|
3322 | * @return {Array} The new list.
|
3323 | * @example
|
3324 | *
|
3325 | * var squareEnds = function(elt, idx, list) {
|
3326 | * if (idx === 0 || idx === list.length - 1) {
|
3327 | * return elt * elt;
|
3328 | * }
|
3329 | * return elt;
|
3330 | * };
|
3331 | *
|
3332 | * R.mapIndexed(squareEnds, [8, 5, 3, 0, 9]); //=> [64, 5, 3, 0, 81]
|
3333 | */
|
3334 | var mapIndexed = _curry2(function mapIndexed(fn, list) {
|
3335 | var idx = -1, len = list.length, result = [];
|
3336 | while (++idx < len) {
|
3337 | result[idx] = fn(list[idx], idx, list);
|
3338 | }
|
3339 | return result;
|
3340 | });
|
3341 |
|
3342 | /**
|
3343 | * mathMod behaves like the modulo operator should mathematically, unlike the `%`
|
3344 | * operator (and by extension, R.modulo). So while "-17 % 5" is -2,
|
3345 | * mathMod(-17, 5) is 3. mathMod requires Integer arguments, and returns NaN
|
3346 | * when the modulus is zero or negative.
|
3347 | *
|
3348 | * @func
|
3349 | * @memberOf R
|
3350 | * @category Math
|
3351 | * @sig Number -> Number -> Number
|
3352 | * @param {Number} m The dividend.
|
3353 | * @param {Number} p the modulus.
|
3354 | * @return {Number} The result of `b mod a`.
|
3355 | * @see R.moduloBy
|
3356 | * @example
|
3357 | *
|
3358 | * R.mathMod(-17, 5); //=> 3
|
3359 | * R.mathMod(17, 5); //=> 2
|
3360 | * R.mathMod(17, -5); //=> NaN
|
3361 | * R.mathMod(17, 0); //=> NaN
|
3362 | * R.mathMod(17.2, 5); //=> NaN
|
3363 | * R.mathMod(17, 5.3); //=> NaN
|
3364 | *
|
3365 | * var clock = R.mathMod(R.__, 12);
|
3366 | * clock(15); //=> 3
|
3367 | * clock(24); //=> 0
|
3368 | *
|
3369 | * var seventeenMod = R.mathMod(17);
|
3370 | * seventeenMod(3); //=> 2
|
3371 | * seventeenMod(4); //=> 1
|
3372 | * seventeenMod(10); //=> 7
|
3373 | */
|
3374 | var mathMod = _curry2(function mathMod(m, p) {
|
3375 | if (!_isInteger(m)) {
|
3376 | return NaN;
|
3377 | }
|
3378 | if (!_isInteger(p) || p < 1) {
|
3379 | return NaN;
|
3380 | }
|
3381 | return (m % p + p) % p;
|
3382 | });
|
3383 |
|
3384 | /**
|
3385 | * Determines the largest of a list of items as determined by pairwise comparisons from the supplied comparator.
|
3386 | * Note that this will return undefined if supplied an empty list.
|
3387 | *
|
3388 | * @func
|
3389 | * @memberOf R
|
3390 | * @category Math
|
3391 | * @sig (a -> Number) -> [a] -> a
|
3392 | * @param {Function} keyFn A comparator function for elements in the list
|
3393 | * @param {Array} list A list of comparable elements
|
3394 | * @return {*} The greatest element in the list. `undefined` if the list is empty.
|
3395 | * @see R.max
|
3396 | * @example
|
3397 | *
|
3398 | * function cmp(obj) { return obj.x; }
|
3399 | * var a = {x: 1}, b = {x: 2}, c = {x: 3};
|
3400 | * R.maxBy(cmp, [a, b, c]); //=> {x: 3}
|
3401 | */
|
3402 | var maxBy = _curry2(_createMaxMinBy(_gt));
|
3403 |
|
3404 | /**
|
3405 | * Determines the smallest of a list of items as determined by pairwise comparisons from the supplied comparator
|
3406 | * Note that this will return undefined if supplied an empty list.
|
3407 | *
|
3408 | * @func
|
3409 | * @memberOf R
|
3410 | * @category Math
|
3411 | * @sig (a -> Number) -> [a] -> a
|
3412 | * @param {Function} keyFn A comparator function for elements in the list
|
3413 | * @param {Array} list A list of comparable elements
|
3414 | * @see R.min
|
3415 | * @return {*} The greatest element in the list. `undefined` if the list is empty.
|
3416 | * @example
|
3417 | *
|
3418 | * function cmp(obj) { return obj.x; }
|
3419 | * var a = {x: 1}, b = {x: 2}, c = {x: 3};
|
3420 | * R.minBy(cmp, [a, b, c]); //=> {x: 1}
|
3421 | */
|
3422 | var minBy = _curry2(_createMaxMinBy(_lt));
|
3423 |
|
3424 | /**
|
3425 | * Divides the second parameter by the first and returns the remainder.
|
3426 | * Note that this functions preserves the JavaScript-style behavior for
|
3427 | * modulo. For mathematical modulo see `mathMod`
|
3428 | *
|
3429 | * @func
|
3430 | * @memberOf R
|
3431 | * @category Math
|
3432 | * @sig Number -> Number -> Number
|
3433 | * @param {Number} a The value to the divide.
|
3434 | * @param {Number} b The pseudo-modulus
|
3435 | * @return {Number} The result of `b % a`.
|
3436 | * @see R.mathMod
|
3437 | * @example
|
3438 | *
|
3439 | * R.modulo(17, 3); //=> 2
|
3440 | * // JS behavior:
|
3441 | * R.modulo(-17, 3); //=> -2
|
3442 | * R.modulo(17, -3); //=> 2
|
3443 | *
|
3444 | * var isOdd = R.modulo(R.__, 2);
|
3445 | * isOdd(42); //=> 0
|
3446 | * isOdd(21); //=> 1
|
3447 | */
|
3448 | var modulo = _curry2(function modulo(a, b) {
|
3449 | return a % b;
|
3450 | });
|
3451 |
|
3452 | /**
|
3453 | * Multiplies two numbers. Equivalent to `a * b` but curried.
|
3454 | *
|
3455 | * @func
|
3456 | * @memberOf R
|
3457 | * @category Math
|
3458 | * @sig Number -> Number -> Number
|
3459 | * @param {Number} a The first value.
|
3460 | * @param {Number} b The second value.
|
3461 | * @return {Number} The result of `a * b`.
|
3462 | * @example
|
3463 | *
|
3464 | * var double = R.multiply(2);
|
3465 | * var triple = R.multiply(3);
|
3466 | * double(3); //=> 6
|
3467 | * triple(4); //=> 12
|
3468 | * R.multiply(2, 5); //=> 10
|
3469 | */
|
3470 | var multiply = _curry2(_multiply);
|
3471 |
|
3472 | /**
|
3473 | * Wraps a function of any arity (including nullary) in a function that accepts exactly `n`
|
3474 | * parameters. Any extraneous parameters will not be passed to the supplied function.
|
3475 | *
|
3476 | * @func
|
3477 | * @memberOf R
|
3478 | * @category Function
|
3479 | * @sig Number -> (* -> a) -> (* -> a)
|
3480 | * @param {Number} n The desired arity of the new function.
|
3481 | * @param {Function} fn The function to wrap.
|
3482 | * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
|
3483 | * arity `n`.
|
3484 | * @example
|
3485 | *
|
3486 | * var takesTwoArgs = function(a, b) {
|
3487 | * return [a, b];
|
3488 | * };
|
3489 | * takesTwoArgs.length; //=> 2
|
3490 | * takesTwoArgs(1, 2); //=> [1, 2]
|
3491 | *
|
3492 | * var takesOneArg = R.nAry(1, takesTwoArgs);
|
3493 | * takesOneArg.length; //=> 1
|
3494 | * // Only `n` arguments are passed to the wrapped function
|
3495 | * takesOneArg(1, 2); //=> [1, undefined]
|
3496 | */
|
3497 | var nAry = _curry2(function (n, fn) {
|
3498 | switch (n) {
|
3499 | case 0:
|
3500 | return function () {
|
3501 | return fn.call(this);
|
3502 | };
|
3503 | case 1:
|
3504 | return function (a0) {
|
3505 | return fn.call(this, a0);
|
3506 | };
|
3507 | case 2:
|
3508 | return function (a0, a1) {
|
3509 | return fn.call(this, a0, a1);
|
3510 | };
|
3511 | case 3:
|
3512 | return function (a0, a1, a2) {
|
3513 | return fn.call(this, a0, a1, a2);
|
3514 | };
|
3515 | case 4:
|
3516 | return function (a0, a1, a2, a3) {
|
3517 | return fn.call(this, a0, a1, a2, a3);
|
3518 | };
|
3519 | case 5:
|
3520 | return function (a0, a1, a2, a3, a4) {
|
3521 | return fn.call(this, a0, a1, a2, a3, a4);
|
3522 | };
|
3523 | case 6:
|
3524 | return function (a0, a1, a2, a3, a4, a5) {
|
3525 | return fn.call(this, a0, a1, a2, a3, a4, a5);
|
3526 | };
|
3527 | case 7:
|
3528 | return function (a0, a1, a2, a3, a4, a5, a6) {
|
3529 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6);
|
3530 | };
|
3531 | case 8:
|
3532 | return function (a0, a1, a2, a3, a4, a5, a6, a7) {
|
3533 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);
|
3534 | };
|
3535 | case 9:
|
3536 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
|
3537 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);
|
3538 | };
|
3539 | case 10:
|
3540 | return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
|
3541 | return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
|
3542 | };
|
3543 | default:
|
3544 | throw new Error('First argument to nAry must be a non-negative integer no greater than ten');
|
3545 | }
|
3546 | });
|
3547 |
|
3548 | /**
|
3549 | * Negates its argument.
|
3550 | *
|
3551 | * @func
|
3552 | * @memberOf R
|
3553 | * @category Math
|
3554 | * @sig Number -> Number
|
3555 | * @param {Number} n
|
3556 | * @return {Number}
|
3557 | * @example
|
3558 | *
|
3559 | * R.negate(42); //=> -42
|
3560 | */
|
3561 | var negate = _curry1(function negate(n) {
|
3562 | return -n;
|
3563 | });
|
3564 |
|
3565 | /**
|
3566 | * A function that returns the `!` of its argument. It will return `true` when
|
3567 | * passed false-y value, and `false` when passed a truth-y one.
|
3568 | *
|
3569 | * @func
|
3570 | * @memberOf R
|
3571 | * @category Logic
|
3572 | * @sig * -> Boolean
|
3573 | * @param {*} a any value
|
3574 | * @return {Boolean} the logical inverse of passed argument.
|
3575 | * @see complement
|
3576 | * @example
|
3577 | *
|
3578 | * R.not(true); //=> false
|
3579 | * R.not(false); //=> true
|
3580 | * R.not(0); => true
|
3581 | * R.not(1); => false
|
3582 | */
|
3583 | var not = _curry1(function not(a) {
|
3584 | return !a;
|
3585 | });
|
3586 |
|
3587 | /**
|
3588 | * Returns the nth element in a list.
|
3589 | * If n is negative the element at index length + n is returned.
|
3590 | *
|
3591 | * @func
|
3592 | * @memberOf R
|
3593 | * @category List
|
3594 | * @sig Number -> [a] -> a
|
3595 | * @param {Number} idx
|
3596 | * @param {Array} list
|
3597 | * @return {*} The nth element of the list.
|
3598 | * @example
|
3599 | *
|
3600 | * var list = ['foo', 'bar', 'baz', 'quux'];
|
3601 | * R.nth(1, list); //=> 'bar'
|
3602 | * R.nth(-1, list); //=> 'quux'
|
3603 | * R.nth(-99, list); //=> undefined
|
3604 | */
|
3605 | var nth = _curry2(_nth);
|
3606 |
|
3607 | /**
|
3608 | * Returns a function which returns its nth argument.
|
3609 | *
|
3610 | * @func
|
3611 | * @memberOf R
|
3612 | * @category Function
|
3613 | * @sig Number -> *... -> *
|
3614 | * @param {Number} n
|
3615 | * @return {Function}
|
3616 | * @example
|
3617 | *
|
3618 | * R.nthArg(1)('a', 'b', 'c'); //=> 'b'
|
3619 | * R.nthArg(-1)('a', 'b', 'c'); //=> 'c'
|
3620 | */
|
3621 | var nthArg = _curry1(function nthArg(n) {
|
3622 | return function () {
|
3623 | return _nth(n, arguments);
|
3624 | };
|
3625 | });
|
3626 |
|
3627 | /**
|
3628 | * Returns the nth character of the given string.
|
3629 | *
|
3630 | * @func
|
3631 | * @memberOf R
|
3632 | * @category String
|
3633 | * @sig Number -> String -> String
|
3634 | * @param {Number} n
|
3635 | * @param {String} str
|
3636 | * @return {String}
|
3637 | * @example
|
3638 | *
|
3639 | * R.nthChar(2, 'Ramda'); //=> 'm'
|
3640 | * R.nthChar(-2, 'Ramda'); //=> 'd'
|
3641 | */
|
3642 | var nthChar = _curry2(function nthChar(n, str) {
|
3643 | return str.charAt(n < 0 ? str.length + n : n);
|
3644 | });
|
3645 |
|
3646 | /**
|
3647 | * Returns the character code of the nth character of the given string.
|
3648 | *
|
3649 | * @func
|
3650 | * @memberOf R
|
3651 | * @category String
|
3652 | * @sig Number -> String -> Number
|
3653 | * @param {Number} n
|
3654 | * @param {String} str
|
3655 | * @return {Number}
|
3656 | * @example
|
3657 | *
|
3658 | * R.nthCharCode(2, 'Ramda'); //=> 'm'.charCodeAt(0)
|
3659 | * R.nthCharCode(-2, 'Ramda'); //=> 'd'.charCodeAt(0)
|
3660 | */
|
3661 | var nthCharCode = _curry2(function nthCharCode(n, str) {
|
3662 | return str.charCodeAt(n < 0 ? str.length + n : n);
|
3663 | });
|
3664 |
|
3665 | /**
|
3666 | * Returns a singleton array containing the value provided.
|
3667 | *
|
3668 | * Note this `of` is different from the ES6 `of`; See
|
3669 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
|
3670 | *
|
3671 | * @func
|
3672 | * @memberOf R
|
3673 | * @category Function
|
3674 | * @sig a -> [a]
|
3675 | * @param {*} x any value
|
3676 | * @return {Array} An array wrapping `x`.
|
3677 | * @example
|
3678 | *
|
3679 | * R.of(null); //=> [null]
|
3680 | * R.of([42]); //=> [[42]]
|
3681 | */
|
3682 | var of = _curry1(function of(x) {
|
3683 | return [x];
|
3684 | });
|
3685 |
|
3686 | /**
|
3687 | * Returns a partial copy of an object omitting the keys specified.
|
3688 | *
|
3689 | * @func
|
3690 | * @memberOf R
|
3691 | * @category Object
|
3692 | * @sig [String] -> {String: *} -> {String: *}
|
3693 | * @param {Array} names an array of String property names to omit from the new object
|
3694 | * @param {Object} obj The object to copy from
|
3695 | * @return {Object} A new object with properties from `names` not on it.
|
3696 | * @example
|
3697 | *
|
3698 | * R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}
|
3699 | */
|
3700 | var omit = _curry2(function omit(names, obj) {
|
3701 | var result = {};
|
3702 | for (var prop in obj) {
|
3703 | if (_indexOf(names, prop) < 0) {
|
3704 | result[prop] = obj[prop];
|
3705 | }
|
3706 | }
|
3707 | return result;
|
3708 | });
|
3709 |
|
3710 | /**
|
3711 | * Accepts a function `fn` and returns a function that guards invocation of `fn` such that
|
3712 | * `fn` can only ever be called once, no matter how many times the returned function is
|
3713 | * invoked.
|
3714 | *
|
3715 | * @func
|
3716 | * @memberOf R
|
3717 | * @category Function
|
3718 | * @sig (a... -> b) -> (a... -> b)
|
3719 | * @param {Function} fn The function to wrap in a call-only-once wrapper.
|
3720 | * @return {Function} The wrapped function.
|
3721 | * @example
|
3722 | *
|
3723 | * var addOneOnce = R.once(function(x){ return x + 1; });
|
3724 | * addOneOnce(10); //=> 11
|
3725 | * addOneOnce(addOneOnce(50)); //=> 11
|
3726 | */
|
3727 | var once = _curry1(function once(fn) {
|
3728 | var called = false, result;
|
3729 | return function () {
|
3730 | if (called) {
|
3731 | return result;
|
3732 | }
|
3733 | called = true;
|
3734 | result = fn.apply(this, arguments);
|
3735 | return result;
|
3736 | };
|
3737 | });
|
3738 |
|
3739 | /**
|
3740 | * Retrieve the value at a given path.
|
3741 | *
|
3742 | * @func
|
3743 | * @memberOf R
|
3744 | * @category Object
|
3745 | * @sig [String] -> {*} -> *
|
3746 | * @param {Array} path The path to use.
|
3747 | * @return {*} The data at `path`.
|
3748 | * @example
|
3749 | *
|
3750 | * R.path(['a', 'b'], {a: {b: 2}}); //=> 2
|
3751 | */
|
3752 | var path = _curry2(_path);
|
3753 |
|
3754 | /**
|
3755 | * Determines whether a nested path on an object has a specific value.
|
3756 | * Most likely used to filter a list.
|
3757 | *
|
3758 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
3759 | * are not considered equal.
|
3760 | *
|
3761 | * @func
|
3762 | * @memberOf R
|
3763 | * @category Relation
|
3764 | * @sig [String] -> * -> {String: *} -> Boolean
|
3765 | * @param {Array} path The path of the nested property to use
|
3766 | * @param {*} val The value to compare the nested property with
|
3767 | * @param {Object} obj The object to check the nested property in
|
3768 | * @return {Boolean} `true` if the value equals the nested object property,
|
3769 | * `false` otherwise.
|
3770 | * @example
|
3771 | *
|
3772 | * var user1 = { address: { zipCode: 90210 } };
|
3773 | * var user2 = { address: { zipCode: 55555 } };
|
3774 | * var user3 = { name: 'Bob' };
|
3775 | * var users = [ user1, user2, user3 ];
|
3776 | * var isFamous = R.pathEq(['address', 'zipCode'], 90210);
|
3777 | * R.filter(isFamous, users); //=> [ user1 ]
|
3778 | */
|
3779 | var pathEq = _curry3(function pathEq(path, val, obj) {
|
3780 | return _eq(_path(path, obj), val);
|
3781 | });
|
3782 |
|
3783 | /**
|
3784 | * Returns a partial copy of an object containing only the keys specified. If the key does not exist, the
|
3785 | * property is ignored.
|
3786 | *
|
3787 | * @func
|
3788 | * @memberOf R
|
3789 | * @category Object
|
3790 | * @sig [String] -> {String: *} -> {String: *}
|
3791 | * @param {Array} names an array of String property names to copy onto a new object
|
3792 | * @param {Object} obj The object to copy from
|
3793 | * @return {Object} A new object with only properties from `names` on it.
|
3794 | * @example
|
3795 | *
|
3796 | * R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
|
3797 | * R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}
|
3798 | */
|
3799 | var pick = _curry2(function pick(names, obj) {
|
3800 | var result = {};
|
3801 | for (var prop in obj) {
|
3802 | if (_indexOf(names, prop) >= 0) {
|
3803 | result[prop] = obj[prop];
|
3804 | }
|
3805 | }
|
3806 | return result;
|
3807 | });
|
3808 |
|
3809 | /**
|
3810 | * Similar to `pick` except that this one includes a `key: undefined` pair for properties that don't exist.
|
3811 | *
|
3812 | * @func
|
3813 | * @memberOf R
|
3814 | * @category Object
|
3815 | * @sig [k] -> {k: v} -> {k: v}
|
3816 | * @param {Array} names an array of String property names to copy onto a new object
|
3817 | * @param {Object} obj The object to copy from
|
3818 | * @return {Object} A new object with only properties from `names` on it.
|
3819 | * @see R.pick
|
3820 | * @example
|
3821 | *
|
3822 | * R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
|
3823 | * R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined}
|
3824 | */
|
3825 | var pickAll = _curry2(function pickAll(names, obj) {
|
3826 | var result = {};
|
3827 | var idx = -1;
|
3828 | var len = names.length;
|
3829 | while (++idx < len) {
|
3830 | var name = names[idx];
|
3831 | result[name] = obj[name];
|
3832 | }
|
3833 | return result;
|
3834 | });
|
3835 |
|
3836 | /**
|
3837 | * Returns a partial copy of an object containing only the keys that
|
3838 | * satisfy the supplied predicate.
|
3839 | *
|
3840 | * @func
|
3841 | * @memberOf R
|
3842 | * @category Object
|
3843 | * @sig (v, k -> Boolean) -> {k: v} -> {k: v}
|
3844 | * @param {Function} pred A predicate to determine whether or not a key
|
3845 | * should be included on the output object.
|
3846 | * @param {Object} obj The object to copy from
|
3847 | * @return {Object} A new object with only properties that satisfy `pred`
|
3848 | * on it.
|
3849 | * @see R.pick
|
3850 | * @example
|
3851 | *
|
3852 | * var isUpperCase = function(val, key) { return key.toUpperCase() === key; }
|
3853 | * R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4}
|
3854 | */
|
3855 | var pickBy = _curry2(function pickBy(test, obj) {
|
3856 | var result = {};
|
3857 | for (var prop in obj) {
|
3858 | if (test(obj[prop], prop, obj)) {
|
3859 | result[prop] = obj[prop];
|
3860 | }
|
3861 | }
|
3862 | return result;
|
3863 | });
|
3864 |
|
3865 | /**
|
3866 | * Returns a new list with the given element at the front, followed by the contents of the
|
3867 | * list.
|
3868 | *
|
3869 | * @func
|
3870 | * @memberOf R
|
3871 | * @category List
|
3872 | * @sig a -> [a] -> [a]
|
3873 | * @param {*} el The item to add to the head of the output list.
|
3874 | * @param {Array} list The array to add to the tail of the output list.
|
3875 | * @return {Array} A new array.
|
3876 | * @example
|
3877 | *
|
3878 | * R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum']
|
3879 | */
|
3880 | var prepend = _curry2(_prepend);
|
3881 |
|
3882 | /**
|
3883 | * Returns a function that when supplied an object returns the indicated property of that object, if it exists.
|
3884 | *
|
3885 | * @func
|
3886 | * @memberOf R
|
3887 | * @category Object
|
3888 | * @sig s -> {s: a} -> a
|
3889 | * @param {String} p The property name
|
3890 | * @param {Object} obj The object to query
|
3891 | * @return {*} The value at `obj.p`.
|
3892 | * @example
|
3893 | *
|
3894 | * R.prop('x', {x: 100}); //=> 100
|
3895 | * R.prop('x', {}); //=> undefined
|
3896 | */
|
3897 | var prop = _curry2(function prop(p, obj) {
|
3898 | return obj[p];
|
3899 | });
|
3900 |
|
3901 | /**
|
3902 | * Determines whether the given property of an object has a specific value.
|
3903 | * Most likely used to filter a list.
|
3904 | *
|
3905 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
3906 | * are not considered equal.
|
3907 | *
|
3908 | * @func
|
3909 | * @memberOf R
|
3910 | * @category Relation
|
3911 | * @sig k -> v -> {k: v} -> Boolean
|
3912 | * @param {Number|String} name The property name (or index) to use.
|
3913 | * @param {*} val The value to compare the property with.
|
3914 | * @return {Boolean} `true` if the properties are equal, `false` otherwise.
|
3915 | * @example
|
3916 | *
|
3917 | * var abby = {name: 'Abby', age: 7, hair: 'blond'};
|
3918 | * var fred = {name: 'Fred', age: 12, hair: 'brown'};
|
3919 | * var rusty = {name: 'Rusty', age: 10, hair: 'brown'};
|
3920 | * var alois = {name: 'Alois', age: 15, disposition: 'surly'};
|
3921 | * var kids = [abby, fred, rusty, alois];
|
3922 | * var hasBrownHair = R.propEq('hair', 'brown');
|
3923 | * R.filter(hasBrownHair, kids); //=> [fred, rusty]
|
3924 | */
|
3925 | var propEq = _curry3(function propEq(name, val, obj) {
|
3926 | return _eq(obj[name], val);
|
3927 | });
|
3928 |
|
3929 | /**
|
3930 | * If the given, non-null object has an own property with the specified name,
|
3931 | * returns the value of that property.
|
3932 | * Otherwise returns the provided default value.
|
3933 | *
|
3934 | * @func
|
3935 | * @memberOf R
|
3936 | * @category Object
|
3937 | * @sig a -> String -> Object -> a
|
3938 | * @param {*} val The default value.
|
3939 | * @param {String} p The name of the property to return.
|
3940 | * @param {Object} obj The object to query.
|
3941 | * @return {*} The value of given property of the supplied object or the default value.
|
3942 | * @example
|
3943 | *
|
3944 | * var alice = {
|
3945 | * name: 'ALICE',
|
3946 | * age: 101
|
3947 | * };
|
3948 | * var favorite = R.prop('favoriteLibrary');
|
3949 | * var favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary');
|
3950 | *
|
3951 | * favorite(alice); //=> undefined
|
3952 | * favoriteWithDefault(alice); //=> 'Ramda'
|
3953 | */
|
3954 | var propOr = _curry3(function propOr(val, p, obj) {
|
3955 | return _has(p, obj) ? obj[p] : val;
|
3956 | });
|
3957 |
|
3958 | /**
|
3959 | * Acts as multiple `get`: array of keys in, array of values out. Preserves order.
|
3960 | *
|
3961 | * @func
|
3962 | * @memberOf R
|
3963 | * @category Object
|
3964 | * @sig [k] -> {k: v} -> [v]
|
3965 | * @param {Array} ps The property names to fetch
|
3966 | * @param {Object} obj The object to query
|
3967 | * @return {Array} The corresponding values or partially applied function.
|
3968 | * @example
|
3969 | *
|
3970 | * R.props(['x', 'y'], {x: 1, y: 2}); //=> [1, 2]
|
3971 | * R.props(['c', 'a', 'b'], {b: 2, a: 1}); //=> [undefined, 1, 2]
|
3972 | *
|
3973 | * var fullName = R.compose(R.join(' '), R.props(['first', 'last']));
|
3974 | * fullName({last: 'Bullet-Tooth', age: 33, first: 'Tony'}); //=> 'Tony Bullet-Tooth'
|
3975 | */
|
3976 | var props = _curry2(function props(ps, obj) {
|
3977 | var len = ps.length;
|
3978 | var out = [];
|
3979 | var idx = -1;
|
3980 | while (++idx < len) {
|
3981 | out[idx] = obj[ps[idx]];
|
3982 | }
|
3983 | return out;
|
3984 | });
|
3985 |
|
3986 | /**
|
3987 | * Returns a list of numbers from `from` (inclusive) to `to`
|
3988 | * (exclusive).
|
3989 | *
|
3990 | * @func
|
3991 | * @memberOf R
|
3992 | * @category List
|
3993 | * @sig Number -> Number -> [Number]
|
3994 | * @param {Number} from The first number in the list.
|
3995 | * @param {Number} to One more than the last number in the list.
|
3996 | * @return {Array} The list of numbers in tthe set `[a, b)`.
|
3997 | * @example
|
3998 | *
|
3999 | * R.range(1, 5); //=> [1, 2, 3, 4]
|
4000 | * R.range(50, 53); //=> [50, 51, 52]
|
4001 | */
|
4002 | var range = _curry2(function range(from, to) {
|
4003 | var result = [];
|
4004 | var n = from;
|
4005 | while (n < to) {
|
4006 | result[result.length] = n;
|
4007 | n += 1;
|
4008 | }
|
4009 | return result;
|
4010 | });
|
4011 |
|
4012 | /**
|
4013 | * Like `reduce`, but passes additional parameters to the predicate function.
|
4014 | *
|
4015 | * The iterator function receives four values: *(acc, value, index, list)*
|
4016 | *
|
4017 | * Note: `R.reduceIndexed` does not skip deleted or unassigned indices (sparse arrays),
|
4018 | * unlike the native `Array.prototype.reduce` method. For more details on this behavior,
|
4019 | * see:
|
4020 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
|
4021 | *
|
4022 | * @func
|
4023 | * @memberOf R
|
4024 | * @category List
|
4025 | * @sig (a,b,i,[b] -> a) -> a -> [b] -> a
|
4026 | * @param {Function} fn The iterator function. Receives four values: the accumulator, the
|
4027 | * current element from `list`, that element's index, and the entire `list` itself.
|
4028 | * @param {*} acc The accumulator value.
|
4029 | * @param {Array} list The list to iterate over.
|
4030 | * @return {*} The final, accumulated value.
|
4031 | * @example
|
4032 | *
|
4033 | * var letters = ['a', 'b', 'c'];
|
4034 | * var objectify = function(accObject, elem, idx, list) {
|
4035 | * accObject[elem] = idx;
|
4036 | * return accObject;
|
4037 | * };
|
4038 | *
|
4039 | * R.reduceIndexed(objectify, {}, letters); //=> { 'a': 0, 'b': 1, 'c': 2 }
|
4040 | */
|
4041 | var reduceIndexed = _curry3(function reduceIndexed(fn, acc, list) {
|
4042 | var idx = -1, len = list.length;
|
4043 | while (++idx < len) {
|
4044 | acc = fn(acc, list[idx], idx, list);
|
4045 | }
|
4046 | return acc;
|
4047 | });
|
4048 |
|
4049 | /**
|
4050 | * Returns a single item by iterating through the list, successively calling the iterator
|
4051 | * function and passing it an accumulator value and the current value from the array, and
|
4052 | * then passing the result to the next call.
|
4053 | *
|
4054 | * Similar to `reduce`, except moves through the input list from the right to the left.
|
4055 | *
|
4056 | * The iterator function receives two values: *(acc, value)*
|
4057 | *
|
4058 | * Note: `R.reduceRight` does not skip deleted or unassigned indices (sparse arrays), unlike
|
4059 | * the native `Array.prototype.reduce` method. For more details on this behavior, see:
|
4060 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
|
4061 | *
|
4062 | * @func
|
4063 | * @memberOf R
|
4064 | * @category List
|
4065 | * @sig (a,b -> a) -> a -> [b] -> a
|
4066 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
4067 | * current element from the array.
|
4068 | * @param {*} acc The accumulator value.
|
4069 | * @param {Array} list The list to iterate over.
|
4070 | * @return {*} The final, accumulated value.
|
4071 | * @example
|
4072 | *
|
4073 | * var pairs = [ ['a', 1], ['b', 2], ['c', 3] ];
|
4074 | * var flattenPairs = function(acc, pair) {
|
4075 | * return acc.concat(pair);
|
4076 | * };
|
4077 | *
|
4078 | * R.reduceRight(flattenPairs, [], pairs); //=> [ 'c', 3, 'b', 2, 'a', 1 ]
|
4079 | */
|
4080 | var reduceRight = _curry3(function reduceRight(fn, acc, list) {
|
4081 | var idx = list.length;
|
4082 | while (--idx >= 0) {
|
4083 | acc = fn(acc, list[idx]);
|
4084 | }
|
4085 | return acc;
|
4086 | });
|
4087 |
|
4088 | /**
|
4089 | * Like `reduceRight`, but passes additional parameters to the predicate function. Moves through
|
4090 | * the input list from the right to the left.
|
4091 | *
|
4092 | * The iterator function receives four values: *(acc, value, index, list)*.
|
4093 | *
|
4094 | * Note: `R.reduceRightIndexed` does not skip deleted or unassigned indices (sparse arrays),
|
4095 | * unlike the native `Array.prototype.reduce` method. For more details on this behavior,
|
4096 | * see:
|
4097 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
|
4098 | *
|
4099 | * @func
|
4100 | * @memberOf R
|
4101 | * @category List
|
4102 | * @sig (a,b,i,[b] -> a -> [b] -> a
|
4103 | * @param {Function} fn The iterator function. Receives four values: the accumulator, the
|
4104 | * current element from `list`, that element's index, and the entire `list` itself.
|
4105 | * @param {*} acc The accumulator value.
|
4106 | * @param {Array} list The list to iterate over.
|
4107 | * @return {*} The final, accumulated value.
|
4108 | * @example
|
4109 | *
|
4110 | * var letters = ['a', 'b', 'c'];
|
4111 | * var objectify = function(accObject, elem, idx, list) {
|
4112 | * accObject[elem] = idx;
|
4113 | * return accObject;
|
4114 | * };
|
4115 | *
|
4116 | * R.reduceRightIndexed(objectify, {}, letters); //=> { 'c': 2, 'b': 1, 'a': 0 }
|
4117 | */
|
4118 | var reduceRightIndexed = _curry3(function reduceRightIndexed(fn, acc, list) {
|
4119 | var idx = list.length;
|
4120 | while (--idx >= 0) {
|
4121 | acc = fn(acc, list[idx], idx, list);
|
4122 | }
|
4123 | return acc;
|
4124 | });
|
4125 |
|
4126 | /**
|
4127 | * Like `reject`, but passes additional parameters to the predicate function. The predicate
|
4128 | * function is passed three arguments: *(value, index, list)*.
|
4129 | *
|
4130 | * @func
|
4131 | * @memberOf R
|
4132 | * @category List
|
4133 | * @sig (a, i, [a] -> Boolean) -> [a] -> [a]
|
4134 | * @param {Function} fn The function called per iteration.
|
4135 | * @param {Array} list The collection to iterate over.
|
4136 | * @return {Array} The new filtered array.
|
4137 | * @example
|
4138 | *
|
4139 | * var lastTwo = function(val, idx, list) {
|
4140 | * return list.length - idx <= 2;
|
4141 | * };
|
4142 | *
|
4143 | * R.rejectIndexed(lastTwo, [8, 6, 7, 5, 3, 0, 9]); //=> [8, 6, 7, 5, 3]
|
4144 | */
|
4145 | var rejectIndexed = _curry2(function rejectIndexed(fn, list) {
|
4146 | return _filterIndexed(_complement(fn), list);
|
4147 | });
|
4148 |
|
4149 | /**
|
4150 | * Removes the sub-list of `list` starting at index `start` and containing
|
4151 | * `count` elements. _Note that this is not destructive_: it returns a
|
4152 | * copy of the list with the changes.
|
4153 | * <small>No lists have been harmed in the application of this function.</small>
|
4154 | *
|
4155 | * @func
|
4156 | * @memberOf R
|
4157 | * @category List
|
4158 | * @sig Number -> Number -> [a] -> [a]
|
4159 | * @param {Number} start The position to start removing elements
|
4160 | * @param {Number} count The number of elements to remove
|
4161 | * @param {Array} list The list to remove from
|
4162 | * @return {Array} A new Array with `count` elements from `start` removed.
|
4163 | * @example
|
4164 | *
|
4165 | * R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8]
|
4166 | */
|
4167 | var remove = _curry3(function remove(start, count, list) {
|
4168 | return _concat(_slice(list, 0, Math.min(start, list.length)), _slice(list, Math.min(list.length, start + count)));
|
4169 | });
|
4170 |
|
4171 | /**
|
4172 | * Replace a substring or regex match in a string with a replacement.
|
4173 | *
|
4174 | * @func
|
4175 | * @memberOf R
|
4176 | * @category String
|
4177 | * @sig RegExp|String -> String -> String -> String
|
4178 | * @param {RegExp|String} pattern A regular expression or a substring to match.
|
4179 | * @param {String} replacement The string to replace the matches with.
|
4180 | * @param {String} str The String to do the search and replacement in.
|
4181 | * @return {String} The result.
|
4182 | * @example
|
4183 | *
|
4184 | * R.replace('foo', 'bar', 'foo foo foo'); //=> 'bar foo foo'
|
4185 | * R.replace(/foo/, 'bar', 'foo foo foo'); //=> 'bar foo foo'
|
4186 | *
|
4187 | * // Use the "g" (global) flag to replace all occurrences:
|
4188 | * R.replace(/foo/g, 'bar', 'foo foo foo'); //=> 'bar bar bar'
|
4189 | */
|
4190 | var replace = _curry3(function replace(regex, replacement, str) {
|
4191 | return str.replace(regex, replacement);
|
4192 | });
|
4193 |
|
4194 | /**
|
4195 | * Returns a new list with the same elements as the original list, just
|
4196 | * in the reverse order.
|
4197 | *
|
4198 | * @func
|
4199 | * @memberOf R
|
4200 | * @category List
|
4201 | * @sig [a] -> [a]
|
4202 | * @param {Array} list The list to reverse.
|
4203 | * @return {Array} A copy of the list in reverse order.
|
4204 | * @example
|
4205 | *
|
4206 | * R.reverse([1, 2, 3]); //=> [3, 2, 1]
|
4207 | * R.reverse([1, 2]); //=> [2, 1]
|
4208 | * R.reverse([1]); //=> [1]
|
4209 | * R.reverse([]); //=> []
|
4210 | */
|
4211 | var reverse = _curry1(function reverse(list) {
|
4212 | return _slice(list).reverse();
|
4213 | });
|
4214 |
|
4215 | /**
|
4216 | * Scan is similar to reduce, but returns a list of successively reduced values from the left
|
4217 | *
|
4218 | * @func
|
4219 | * @memberOf R
|
4220 | * @category List
|
4221 | * @sig (a,b -> a) -> a -> [b] -> [a]
|
4222 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
4223 | * current element from the array
|
4224 | * @param {*} acc The accumulator value.
|
4225 | * @param {Array} list The list to iterate over.
|
4226 | * @return {Array} A list of all intermediately reduced values.
|
4227 | * @example
|
4228 | *
|
4229 | * var numbers = [1, 2, 3, 4];
|
4230 | * var factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]
|
4231 | */
|
4232 | var scan = _curry3(function scan(fn, acc, list) {
|
4233 | var idx = 0, len = list.length + 1, result = [acc];
|
4234 | while (++idx < len) {
|
4235 | acc = fn(acc, list[idx - 1]);
|
4236 | result[idx] = acc;
|
4237 | }
|
4238 | return result;
|
4239 | });
|
4240 |
|
4241 | /**
|
4242 | * Returns a copy of the list, sorted according to the comparator function, which should accept two values at a
|
4243 | * time and return a negative number if the first value is smaller, a positive number if it's larger, and zero
|
4244 | * if they are equal. Please note that this is a **copy** of the list. It does not modify the original.
|
4245 | *
|
4246 | * @func
|
4247 | * @memberOf R
|
4248 | * @category List
|
4249 | * @sig (a,a -> Number) -> [a] -> [a]
|
4250 | * @param {Function} comparator A sorting function :: a -> b -> Int
|
4251 | * @param {Array} list The list to sort
|
4252 | * @return {Array} a new array with its elements sorted by the comparator function.
|
4253 | * @example
|
4254 | *
|
4255 | * var diff = function(a, b) { return a - b; };
|
4256 | * R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
|
4257 | */
|
4258 | var sort = _curry2(function sort(comparator, list) {
|
4259 | return _slice(list).sort(comparator);
|
4260 | });
|
4261 |
|
4262 | /**
|
4263 | * Sorts the list according to a key generated by the supplied function.
|
4264 | *
|
4265 | * @func
|
4266 | * @memberOf R
|
4267 | * @category Relation
|
4268 | * @sig (a -> String) -> [a] -> [a]
|
4269 | * @param {Function} fn The function mapping `list` items to keys.
|
4270 | * @param {Array} list The list to sort.
|
4271 | * @return {Array} A new list sorted by the keys generated by `fn`.
|
4272 | * @example
|
4273 | *
|
4274 | * var sortByFirstItem = R.sortBy(prop(0));
|
4275 | * var sortByNameCaseInsensitive = R.sortBy(compose(R.toLower, prop('name')));
|
4276 | * var pairs = [[-1, 1], [-2, 2], [-3, 3]];
|
4277 | * sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
|
4278 | * var alice = {
|
4279 | * name: 'ALICE',
|
4280 | * age: 101
|
4281 | * };
|
4282 | * var bob = {
|
4283 | * name: 'Bob',
|
4284 | * age: -10
|
4285 | * };
|
4286 | * var clara = {
|
4287 | * name: 'clara',
|
4288 | * age: 314.159
|
4289 | * };
|
4290 | * var people = [clara, bob, alice];
|
4291 | * sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
|
4292 | */
|
4293 | var sortBy = _curry2(function sortBy(fn, list) {
|
4294 | return _slice(list).sort(function (a, b) {
|
4295 | var aa = fn(a);
|
4296 | var bb = fn(b);
|
4297 | return aa < bb ? -1 : aa > bb ? 1 : 0;
|
4298 | });
|
4299 | });
|
4300 |
|
4301 | /**
|
4302 | * Finds the first index of a substring in a string, returning -1 if it's not present
|
4303 | *
|
4304 | * @func
|
4305 | * @memberOf R
|
4306 | * @category String
|
4307 | * @sig String -> String -> Number
|
4308 | * @param {String} c A string to find.
|
4309 | * @param {String} str The string to search in
|
4310 | * @return {Number} The first index of `c` or -1 if not found.
|
4311 | * @example
|
4312 | *
|
4313 | * R.strIndexOf('c', 'abcdefg'); //=> 2
|
4314 | */
|
4315 | var strIndexOf = _curry2(function strIndexOf(c, str) {
|
4316 | return str.indexOf(c);
|
4317 | });
|
4318 |
|
4319 | /**
|
4320 | *
|
4321 | * Finds the last index of a substring in a string, returning -1 if it's not present
|
4322 | *
|
4323 | * @func
|
4324 | * @memberOf R
|
4325 | * @category String
|
4326 | * @sig String -> String -> Number
|
4327 | * @param {String} c A string to find.
|
4328 | * @param {String} str The string to search in
|
4329 | * @return {Number} The last index of `c` or -1 if not found.
|
4330 | * @example
|
4331 | *
|
4332 | * R.strLastIndexOf('a', 'banana split'); //=> 5
|
4333 | */
|
4334 | var strLastIndexOf = _curry2(function (c, str) {
|
4335 | return str.lastIndexOf(c);
|
4336 | });
|
4337 |
|
4338 | /**
|
4339 | * Subtracts two numbers. Equivalent to `a - b` but curried.
|
4340 | *
|
4341 | * @func
|
4342 | * @memberOf R
|
4343 | * @category Math
|
4344 | * @sig Number -> Number -> Number
|
4345 | * @param {Number} a The first value.
|
4346 | * @param {Number} b The second value.
|
4347 | * @return {Number} The result of `a - b`.
|
4348 | * @example
|
4349 | *
|
4350 | * R.subtract(10, 8); //=> 2
|
4351 | *
|
4352 | * var minus5 = R.subtract(R.__, 5);
|
4353 | * minus5(17); //=> 12
|
4354 | *
|
4355 | * var complementaryAngle = R.subtract(90);
|
4356 | * complementaryAngle(30); //=> 60
|
4357 | * complementaryAngle(72); //=> 18
|
4358 | */
|
4359 | var subtract = _curry2(function subtract(a, b) {
|
4360 | return a - b;
|
4361 | });
|
4362 |
|
4363 | /**
|
4364 | * Runs the given function with the supplied object, then returns the object.
|
4365 | *
|
4366 | * @func
|
4367 | * @memberOf R
|
4368 | * @category Function
|
4369 | * @sig (a -> *) -> a -> a
|
4370 | * @param {Function} fn The function to call with `x`. The return value of `fn` will be thrown away.
|
4371 | * @param {*} x
|
4372 | * @return {*} `x`.
|
4373 | * @example
|
4374 | *
|
4375 | * var sayX = function(x) { console.log('x is ' + x); };
|
4376 | * R.tap(sayX, 100); //=> 100
|
4377 | * //-> 'x is 100'
|
4378 | */
|
4379 | var tap = _curry2(function tap(fn, x) {
|
4380 | fn(x);
|
4381 | return x;
|
4382 | });
|
4383 |
|
4384 | /**
|
4385 | * Determines whether a given string matches a given regular expression.
|
4386 | *
|
4387 | * @func
|
4388 | * @memberOf R
|
4389 | * @category String
|
4390 | * @sig RegExp -> String -> Boolean
|
4391 | * @param {RegExp} pattern
|
4392 | * @param {String} str
|
4393 | * @return {Boolean}
|
4394 | * @example
|
4395 | *
|
4396 | * R.test(/^x/, 'xyz'); //=> true
|
4397 | * R.test(/^y/, 'xyz'); //=> false
|
4398 | */
|
4399 | var test = _curry2(function test(pattern, str) {
|
4400 | return _cloneRegExp(pattern).test(str);
|
4401 | });
|
4402 |
|
4403 | /**
|
4404 | * Calls an input function `n` times, returning an array containing the results of those
|
4405 | * function calls.
|
4406 | *
|
4407 | * `fn` is passed one argument: The current value of `n`, which begins at `0` and is
|
4408 | * gradually incremented to `n - 1`.
|
4409 | *
|
4410 | * @func
|
4411 | * @memberOf R
|
4412 | * @category List
|
4413 | * @sig (i -> a) -> i -> [a]
|
4414 | * @param {Function} fn The function to invoke. Passed one argument, the current value of `n`.
|
4415 | * @param {Number} n A value between `0` and `n - 1`. Increments after each function call.
|
4416 | * @return {Array} An array containing the return values of all calls to `fn`.
|
4417 | * @example
|
4418 | *
|
4419 | * R.times(R.identity, 5); //=> [0, 1, 2, 3, 4]
|
4420 | */
|
4421 | var times = _curry2(function times(fn, n) {
|
4422 | var len = Number(n);
|
4423 | var list = new Array(len);
|
4424 | var idx = 0;
|
4425 | while (idx < len) {
|
4426 | list[idx] = fn(idx);
|
4427 | idx += 1;
|
4428 | }
|
4429 | return list;
|
4430 | });
|
4431 |
|
4432 | /**
|
4433 | * Converts an object into an array of key, value arrays.
|
4434 | * Only the object's own properties are used.
|
4435 | * Note that the order of the output array is not guaranteed to be
|
4436 | * consistent across different JS platforms.
|
4437 | *
|
4438 | * @func
|
4439 | * @memberOf R
|
4440 | * @category Object
|
4441 | * @sig {String: *} -> [[String,*]]
|
4442 | * @param {Object} obj The object to extract from
|
4443 | * @return {Array} An array of key, value arrays from the object's own properties.
|
4444 | * @example
|
4445 | *
|
4446 | * R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]]
|
4447 | */
|
4448 | var toPairs = _curry1(function toPairs(obj) {
|
4449 | var pairs = [];
|
4450 | for (var prop in obj) {
|
4451 | if (_has(prop, obj)) {
|
4452 | pairs[pairs.length] = [
|
4453 | prop,
|
4454 | obj[prop]
|
4455 | ];
|
4456 | }
|
4457 | }
|
4458 | return pairs;
|
4459 | });
|
4460 |
|
4461 | /**
|
4462 | * Converts an object into an array of key, value arrays.
|
4463 | * The object's own properties and prototype properties are used.
|
4464 | * Note that the order of the output array is not guaranteed to be
|
4465 | * consistent across different JS platforms.
|
4466 | *
|
4467 | * @func
|
4468 | * @memberOf R
|
4469 | * @category Object
|
4470 | * @sig {String: *} -> [[String,*]]
|
4471 | * @param {Object} obj The object to extract from
|
4472 | * @return {Array} An array of key, value arrays from the object's own
|
4473 | * and prototype properties.
|
4474 | * @example
|
4475 | *
|
4476 | * var F = function() { this.x = 'X'; };
|
4477 | * F.prototype.y = 'Y';
|
4478 | * var f = new F();
|
4479 | * R.toPairsIn(f); //=> [['x','X'], ['y','Y']]
|
4480 | */
|
4481 | var toPairsIn = _curry1(function toPairsIn(obj) {
|
4482 | var pairs = [];
|
4483 | for (var prop in obj) {
|
4484 | pairs[pairs.length] = [
|
4485 | prop,
|
4486 | obj[prop]
|
4487 | ];
|
4488 | }
|
4489 | return pairs;
|
4490 | });
|
4491 |
|
4492 | /**
|
4493 | * Removes (strips) whitespace from both ends of the string.
|
4494 | *
|
4495 | * @func
|
4496 | * @memberOf R
|
4497 | * @category String
|
4498 | * @sig String -> String
|
4499 | * @param {String} str The string to trim.
|
4500 | * @return {String} Trimmed version of `str`.
|
4501 | * @example
|
4502 | *
|
4503 | * R.trim(' xyz '); //=> 'xyz'
|
4504 | * R.map(R.trim, R.split(',', 'x, y, z')); //=> ['x', 'y', 'z']
|
4505 | */
|
4506 | var trim = function () {
|
4507 | 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';
|
4508 | var zeroWidth = '\u200B';
|
4509 | var hasProtoTrim = typeof String.prototype.trim === 'function';
|
4510 | if (!hasProtoTrim || (ws.trim() || !zeroWidth.trim())) {
|
4511 | return _curry1(function trim(str) {
|
4512 | var beginRx = new RegExp('^[' + ws + '][' + ws + ']*');
|
4513 | var endRx = new RegExp('[' + ws + '][' + ws + ']*$');
|
4514 | return str.replace(beginRx, '').replace(endRx, '');
|
4515 | });
|
4516 | } else {
|
4517 | return _curry1(function trim(str) {
|
4518 | return str.trim();
|
4519 | });
|
4520 | }
|
4521 | }();
|
4522 |
|
4523 | /**
|
4524 | * Gives a single-word string description of the (native) type of a value, returning such
|
4525 | * answers as 'Object', 'Number', 'Array', or 'Null'. Does not attempt to distinguish user
|
4526 | * Object types any further, reporting them all as 'Object'.
|
4527 | *
|
4528 | * @func
|
4529 | * @memberOf R
|
4530 | * @category Type
|
4531 | * @sig (* -> {*}) -> String
|
4532 | * @param {*} val The value to test
|
4533 | * @return {String}
|
4534 | * @example
|
4535 | *
|
4536 | * R.type({}); //=> "Object"
|
4537 | * R.type(1); //=> "Number"
|
4538 | * R.type(false); //=> "Boolean"
|
4539 | * R.type('s'); //=> "String"
|
4540 | * R.type(null); //=> "Null"
|
4541 | * R.type([]); //=> "Array"
|
4542 | * R.type(/[A-z]/); //=> "RegExp"
|
4543 | */
|
4544 | var type = _curry1(function type(val) {
|
4545 | return val === null ? 'Null' : val === undefined ? 'Undefined' : Object.prototype.toString.call(val).slice(8, -1);
|
4546 | });
|
4547 |
|
4548 | /**
|
4549 | * Takes a function `fn`, which takes a single array argument, and returns
|
4550 | * a function which:
|
4551 | *
|
4552 | * - takes any number of positional arguments;
|
4553 | * - passes these arguments to `fn` as an array; and
|
4554 | * - returns the result.
|
4555 | *
|
4556 | * In other words, R.unapply derives a variadic function from a function
|
4557 | * which takes an array. R.unapply is the inverse of R.apply.
|
4558 | *
|
4559 | * @func
|
4560 | * @memberOf R
|
4561 | * @category Function
|
4562 | * @sig ([*...] -> a) -> (*... -> a)
|
4563 | * @param {Function} fn
|
4564 | * @return {Function}
|
4565 | * @see R.apply
|
4566 | * @example
|
4567 | *
|
4568 | * R.unapply(JSON.stringify)(1, 2, 3); //=> '[1,2,3]'
|
4569 | */
|
4570 | var unapply = _curry1(function unapply(fn) {
|
4571 | return function () {
|
4572 | return fn(_slice(arguments));
|
4573 | };
|
4574 | });
|
4575 |
|
4576 | /**
|
4577 | * Wraps a function of any arity (including nullary) in a function that accepts exactly 1
|
4578 | * parameter. Any extraneous parameters will not be passed to the supplied function.
|
4579 | *
|
4580 | * @func
|
4581 | * @memberOf R
|
4582 | * @category Function
|
4583 | * @sig (* -> b) -> (a -> b)
|
4584 | * @param {Function} fn The function to wrap.
|
4585 | * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
|
4586 | * arity 1.
|
4587 | * @example
|
4588 | *
|
4589 | * var takesTwoArgs = function(a, b) {
|
4590 | * return [a, b];
|
4591 | * };
|
4592 | * takesTwoArgs.length; //=> 2
|
4593 | * takesTwoArgs(1, 2); //=> [1, 2]
|
4594 | *
|
4595 | * var takesOneArg = R.unary(takesTwoArgs);
|
4596 | * takesOneArg.length; //=> 1
|
4597 | * // Only 1 argument is passed to the wrapped function
|
4598 | * takesOneArg(1, 2); //=> [1, undefined]
|
4599 | */
|
4600 | var unary = _curry1(function unary(fn) {
|
4601 | return nAry(1, fn);
|
4602 | });
|
4603 |
|
4604 | /**
|
4605 | * Returns a function of arity `n` from a (manually) curried function.
|
4606 | *
|
4607 | * @func
|
4608 | * @memberOf R
|
4609 | * @category Function
|
4610 | * @sig Number -> (a -> b) -> (a -> c)
|
4611 | * @param {Number} length The arity for the returned function.
|
4612 | * @param {Function} fn The function to uncurry.
|
4613 | * @return {Function} A new function.
|
4614 | * @see R.curry
|
4615 | * @example
|
4616 | *
|
4617 | * var addFour = function(a) {
|
4618 | * return function(b) {
|
4619 | * return function(c) {
|
4620 | * return function(d) {
|
4621 | * return a + b + c + d;
|
4622 | * };
|
4623 | * };
|
4624 | * };
|
4625 | * };
|
4626 | *
|
4627 | * var uncurriedAddFour = R.uncurryN(4, addFour);
|
4628 | * curriedAddFour(1, 2, 3, 4); //=> 10
|
4629 | */
|
4630 | var uncurryN = _curry2(function uncurryN(depth, fn) {
|
4631 | return curryN(depth, function () {
|
4632 | var currentDepth = 1;
|
4633 | var value = fn;
|
4634 | var idx = 0;
|
4635 | var endIdx;
|
4636 | while (currentDepth <= depth && typeof value === 'function') {
|
4637 | endIdx = currentDepth === depth ? arguments.length : idx + value.length;
|
4638 | value = value.apply(this, _slice(arguments, idx, endIdx));
|
4639 | currentDepth += 1;
|
4640 | idx = endIdx;
|
4641 | }
|
4642 | return value;
|
4643 | });
|
4644 | });
|
4645 |
|
4646 | /**
|
4647 | * Builds a list from a seed value. Accepts an iterator function, which returns either false
|
4648 | * to stop iteration or an array of length 2 containing the value to add to the resulting
|
4649 | * list and the seed to be used in the next call to the iterator function.
|
4650 | *
|
4651 | * The iterator function receives one argument: *(seed)*.
|
4652 | *
|
4653 | * @func
|
4654 | * @memberOf R
|
4655 | * @category List
|
4656 | * @sig (a -> [b]) -> * -> [b]
|
4657 | * @param {Function} fn The iterator function. receives one argument, `seed`, and returns
|
4658 | * either false to quit iteration or an array of length two to proceed. The element
|
4659 | * at index 0 of this array will be added to the resulting array, and the element
|
4660 | * at index 1 will be passed to the next call to `fn`.
|
4661 | * @param {*} seed The seed value.
|
4662 | * @return {Array} The final list.
|
4663 | * @example
|
4664 | *
|
4665 | * var f = function(n) { return n > 50 ? false : [-n, n + 10] };
|
4666 | * R.unfold(f, 10); //=> [-10, -20, -30, -40, -50]
|
4667 | */
|
4668 | var unfold = _curry2(function unfold(fn, seed) {
|
4669 | var pair = fn(seed);
|
4670 | var result = [];
|
4671 | while (pair && pair.length) {
|
4672 | result[result.length] = pair[0];
|
4673 | pair = fn(pair[1]);
|
4674 | }
|
4675 | return result;
|
4676 | });
|
4677 |
|
4678 | /**
|
4679 | * Returns a new list containing only one copy of each element in the original list, based
|
4680 | * upon the value returned by applying the supplied predicate to two list elements. Prefers
|
4681 | * the first item if two items compare equal based on the predicate.
|
4682 | *
|
4683 | * @func
|
4684 | * @memberOf R
|
4685 | * @category List
|
4686 | * @sig (a, a -> Boolean) -> [a] -> [a]
|
4687 | * @param {Function} pred A predicate used to test whether two items are equal.
|
4688 | * @param {Array} list The array to consider.
|
4689 | * @return {Array} The list of unique items.
|
4690 | * @example
|
4691 | *
|
4692 | * var strEq = function(a, b) { return String(a) === String(b); };
|
4693 | * R.uniqWith(strEq)([1, '1', 2, 1]); //=> [1, 2]
|
4694 | * R.uniqWith(strEq)([{}, {}]); //=> [{}]
|
4695 | * R.uniqWith(strEq)([1, '1', 1]); //=> [1]
|
4696 | * R.uniqWith(strEq)(['1', 1, 1]); //=> ['1']
|
4697 | */
|
4698 | var uniqWith = _curry2(function uniqWith(pred, list) {
|
4699 | var idx = -1, len = list.length;
|
4700 | var result = [], item;
|
4701 | while (++idx < len) {
|
4702 | item = list[idx];
|
4703 | if (!_containsWith(pred, item, result)) {
|
4704 | result[result.length] = item;
|
4705 | }
|
4706 | }
|
4707 | return result;
|
4708 | });
|
4709 |
|
4710 | /**
|
4711 | * Returns a new copy of the array with the element at the
|
4712 | * provided index replaced with the given value.
|
4713 | *
|
4714 | * @func
|
4715 | * @memberOf R
|
4716 | * @category List
|
4717 | * @sig Number -> a -> [a] -> [a]
|
4718 | * @param {Number} idx The index to update.
|
4719 | * @param {*} x The value to exist at the given index of the returned array.
|
4720 | * @param {Array|Arguments} list The source array-like object to be updated.
|
4721 | * @return {Array} A copy of `list` with the value at index `idx` replaced with `x`.
|
4722 | * @example
|
4723 | *
|
4724 | * R.update(1, 11, [0, 1, 2]); //=> [0, 11, 2]
|
4725 | * R.update(1)(11)([0, 1, 2]); //=> [0, 11, 2]
|
4726 | */
|
4727 | var update = _curry3(function (idx, x, list) {
|
4728 | return adjust(always(x), idx, list);
|
4729 | });
|
4730 |
|
4731 | /**
|
4732 | * Returns a list of all the properties, including prototype properties,
|
4733 | * of the supplied object.
|
4734 | * Note that the order of the output array is not guaranteed to be
|
4735 | * consistent across different JS platforms.
|
4736 | *
|
4737 | * @func
|
4738 | * @memberOf R
|
4739 | * @category Object
|
4740 | * @sig {k: v} -> [v]
|
4741 | * @param {Object} obj The object to extract values from
|
4742 | * @return {Array} An array of the values of the object's own and prototype properties.
|
4743 | * @example
|
4744 | *
|
4745 | * var F = function() { this.x = 'X'; };
|
4746 | * F.prototype.y = 'Y';
|
4747 | * var f = new F();
|
4748 | * R.valuesIn(f); //=> ['X', 'Y']
|
4749 | */
|
4750 | var valuesIn = _curry1(function valuesIn(obj) {
|
4751 | var prop, vs = [];
|
4752 | for (prop in obj) {
|
4753 | vs[vs.length] = obj[prop];
|
4754 | }
|
4755 | return vs;
|
4756 | });
|
4757 |
|
4758 | /**
|
4759 | * Takes a spec object and a test object; returns true if the test satisfies
|
4760 | * the spec. Each of the spec's own properties must be a predicate function.
|
4761 | * Each predicate is applied to the value of the corresponding property of
|
4762 | * the test object. `where` returns true if all the predicates return true,
|
4763 | * false otherwise.
|
4764 | *
|
4765 | * `where` is well suited to declaratively expressing constraints for other
|
4766 | * functions such as `filter` and `find`.
|
4767 | *
|
4768 | * @func
|
4769 | * @memberOf R
|
4770 | * @category Object
|
4771 | * @sig {String: (* -> Boolean)} -> {String: *} -> Boolean
|
4772 | * @param {Object} spec
|
4773 | * @param {Object} testObj
|
4774 | * @return {Boolean}
|
4775 | * @example
|
4776 | *
|
4777 | * // pred :: Object -> Boolean
|
4778 | * var pred = R.where({
|
4779 | * a: R.eq('foo'),
|
4780 | * b: R.complement(R.eq('bar')),
|
4781 | * x: R.gt(_, 10),
|
4782 | * y: R.lt(_, 20)
|
4783 | * });
|
4784 | *
|
4785 | * pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
|
4786 | * pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
|
4787 | * pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
|
4788 | * pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
|
4789 | * pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false
|
4790 | */
|
4791 | var where = _curry2(function where(spec, testObj) {
|
4792 | for (var prop in spec) {
|
4793 | if (_has(prop, spec) && !spec[prop](testObj[prop])) {
|
4794 | return false;
|
4795 | }
|
4796 | }
|
4797 | return true;
|
4798 | });
|
4799 |
|
4800 | /**
|
4801 | * Wrap a function inside another to allow you to make adjustments to the parameters, or do
|
4802 | * other processing either before the internal function is called or with its results.
|
4803 | *
|
4804 | * @func
|
4805 | * @memberOf R
|
4806 | * @category Function
|
4807 | * @sig (a... -> b) -> ((a... -> b) -> a... -> c) -> (a... -> c)
|
4808 | * @param {Function} fn The function to wrap.
|
4809 | * @param {Function} wrapper The wrapper function.
|
4810 | * @return {Function} The wrapped function.
|
4811 | * @example
|
4812 | *
|
4813 | * var greet = function(name) {return 'Hello ' + name;};
|
4814 | *
|
4815 | * var shoutedGreet = R.wrap(greet, function(gr, name) {
|
4816 | * return gr(name).toUpperCase();
|
4817 | * });
|
4818 | * shoutedGreet("Kathy"); //=> "HELLO KATHY"
|
4819 | *
|
4820 | * var shortenedGreet = R.wrap(greet, function(gr, name) {
|
4821 | * return gr(name.substring(0, 3));
|
4822 | * });
|
4823 | * shortenedGreet("Robert"); //=> "Hello Rob"
|
4824 | */
|
4825 | var wrap = _curry2(function wrap(fn, wrapper) {
|
4826 | return curryN(fn.length, function () {
|
4827 | return wrapper.apply(this, _concat([fn], arguments));
|
4828 | });
|
4829 | });
|
4830 |
|
4831 | /**
|
4832 | * Creates a new list out of the two supplied by creating each possible
|
4833 | * pair from the lists.
|
4834 | *
|
4835 | * @func
|
4836 | * @memberOf R
|
4837 | * @category List
|
4838 | * @sig [a] -> [b] -> [[a,b]]
|
4839 | * @param {Array} as The first list.
|
4840 | * @param {Array} bs The second list.
|
4841 | * @return {Array} The list made by combining each possible pair from
|
4842 | * `as` and `bs` into pairs (`[a, b]`).
|
4843 | * @example
|
4844 | *
|
4845 | * R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
|
4846 | */
|
4847 | // = xprodWith(prepend); (takes about 3 times as long...)
|
4848 | var xprod = _curry2(function xprod(a, b) {
|
4849 | // = xprodWith(prepend); (takes about 3 times as long...)
|
4850 | var idx = -1;
|
4851 | var ilen = a.length;
|
4852 | var j;
|
4853 | var jlen = b.length;
|
4854 | var result = [];
|
4855 | while (++idx < ilen) {
|
4856 | j = -1;
|
4857 | while (++j < jlen) {
|
4858 | result[result.length] = [
|
4859 | a[idx],
|
4860 | b[j]
|
4861 | ];
|
4862 | }
|
4863 | }
|
4864 | return result;
|
4865 | });
|
4866 |
|
4867 | /**
|
4868 | * Creates a new list out of the two supplied by pairing up
|
4869 | * equally-positioned items from both lists. The returned list is
|
4870 | * truncated to the length of the shorter of the two input lists.
|
4871 | * Note: `zip` is equivalent to `zipWith(function(a, b) { return [a, b] })`.
|
4872 | *
|
4873 | * @func
|
4874 | * @memberOf R
|
4875 | * @category List
|
4876 | * @sig [a] -> [b] -> [[a,b]]
|
4877 | * @param {Array} list1 The first array to consider.
|
4878 | * @param {Array} list2 The second array to consider.
|
4879 | * @return {Array} The list made by pairing up same-indexed elements of `list1` and `list2`.
|
4880 | * @example
|
4881 | *
|
4882 | * R.zip([1, 2, 3], ['a', 'b', 'c']); //=> [[1, 'a'], [2, 'b'], [3, 'c']]
|
4883 | */
|
4884 | var zip = _curry2(function zip(a, b) {
|
4885 | var rv = [];
|
4886 | var idx = -1;
|
4887 | var len = Math.min(a.length, b.length);
|
4888 | while (++idx < len) {
|
4889 | rv[idx] = [
|
4890 | a[idx],
|
4891 | b[idx]
|
4892 | ];
|
4893 | }
|
4894 | return rv;
|
4895 | });
|
4896 |
|
4897 | /**
|
4898 | * Creates a new object out of a list of keys and a list of values.
|
4899 | *
|
4900 | * @func
|
4901 | * @memberOf R
|
4902 | * @category List
|
4903 | * @sig [String] -> [*] -> {String: *}
|
4904 | * @param {Array} keys The array that will be properties on the output object.
|
4905 | * @param {Array} values The list of values on the output object.
|
4906 | * @return {Object} The object made by pairing up same-indexed elements of `keys` and `values`.
|
4907 | * @example
|
4908 | *
|
4909 | * R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3}
|
4910 | */
|
4911 | var zipObj = _curry2(function zipObj(keys, values) {
|
4912 | var idx = -1, len = keys.length, out = {};
|
4913 | while (++idx < len) {
|
4914 | out[keys[idx]] = values[idx];
|
4915 | }
|
4916 | return out;
|
4917 | });
|
4918 |
|
4919 | /**
|
4920 | * Creates a new list out of the two supplied by applying the function to
|
4921 | * each equally-positioned pair in the lists. The returned list is
|
4922 | * truncated to the length of the shorter of the two input lists.
|
4923 | *
|
4924 | * @function
|
4925 | * @memberOf R
|
4926 | * @category List
|
4927 | * @sig (a,b -> c) -> [a] -> [b] -> [c]
|
4928 | * @param {Function} fn The function used to combine the two elements into one value.
|
4929 | * @param {Array} list1 The first array to consider.
|
4930 | * @param {Array} list2 The second array to consider.
|
4931 | * @return {Array} The list made by combining same-indexed elements of `list1` and `list2`
|
4932 | * using `fn`.
|
4933 | * @example
|
4934 | *
|
4935 | * var f = function(x, y) {
|
4936 | * // ...
|
4937 | * };
|
4938 | * R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
|
4939 | * //=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
|
4940 | */
|
4941 | var zipWith = _curry3(function zipWith(fn, a, b) {
|
4942 | var rv = [], idx = -1, len = Math.min(a.length, b.length);
|
4943 | while (++idx < len) {
|
4944 | rv[idx] = fn(a[idx], b[idx]);
|
4945 | }
|
4946 | return rv;
|
4947 | });
|
4948 |
|
4949 | /**
|
4950 | * A function that always returns `false`. Any passed in parameters are ignored.
|
4951 | *
|
4952 | * @func
|
4953 | * @memberOf R
|
4954 | * @category Function
|
4955 | * @sig * -> false
|
4956 | * @see R.always
|
4957 | * @return {Boolean} false
|
4958 | * @example
|
4959 | *
|
4960 | * R.F(); //=> false
|
4961 | */
|
4962 | var F = always(false);
|
4963 |
|
4964 | /**
|
4965 | * A function that always returns `true`. Any passed in parameters are ignored.
|
4966 | *
|
4967 | * @func
|
4968 | * @memberOf R
|
4969 | * @category Function
|
4970 | * @sig * -> true
|
4971 | * @see R.always
|
4972 | * @return {Boolean} `true`.
|
4973 | * @example
|
4974 | *
|
4975 | * R.T(); //=> true
|
4976 | */
|
4977 | var T = always(true);
|
4978 |
|
4979 | var _append = function _append(el, list) {
|
4980 | return _concat(list, [el]);
|
4981 | };
|
4982 |
|
4983 | var _assocPath = function _assocPath(path, val, obj) {
|
4984 | switch (path.length) {
|
4985 | case 0:
|
4986 | return obj;
|
4987 | case 1:
|
4988 | return _assoc(path[0], val, obj);
|
4989 | default:
|
4990 | return _assoc(path[0], _assocPath(_slice(path, 1), val, Object(obj[path[0]])), obj);
|
4991 | }
|
4992 | };
|
4993 |
|
4994 | /**
|
4995 | * Copies an object.
|
4996 | *
|
4997 | * @private
|
4998 | * @param {*} value The value to be copied
|
4999 | * @param {Array} refFrom Array containing the source references
|
5000 | * @param {Array} refTo Array containing the copied source references
|
5001 | * @return {*} The copied value.
|
5002 | */
|
5003 | var _baseCopy = function _baseCopy(value, refFrom, refTo) {
|
5004 | var copy = function copy(copiedValue) {
|
5005 | var len = refFrom.length;
|
5006 | var idx = -1;
|
5007 | while (++idx < len) {
|
5008 | if (value === refFrom[idx]) {
|
5009 | return refTo[idx];
|
5010 | }
|
5011 | }
|
5012 | refFrom[idx + 1] = value;
|
5013 | refTo[idx + 1] = copiedValue;
|
5014 | for (var key in value) {
|
5015 | copiedValue[key] = _baseCopy(value[key], refFrom, refTo);
|
5016 | }
|
5017 | return copiedValue;
|
5018 | };
|
5019 | switch (type(value)) {
|
5020 | case 'Object':
|
5021 | return copy({});
|
5022 | case 'Array':
|
5023 | return copy([]);
|
5024 | case 'Date':
|
5025 | return new Date(value);
|
5026 | case 'RegExp':
|
5027 | return _cloneRegExp(value);
|
5028 | default:
|
5029 | return value;
|
5030 | }
|
5031 | };
|
5032 |
|
5033 | /**
|
5034 | * Similar to hasMethod, this checks whether a function has a [methodname]
|
5035 | * function. If it isn't an array it will execute that function otherwise it will
|
5036 | * default to the ramda implementation.
|
5037 | *
|
5038 | * @private
|
5039 | * @param {Function} fn ramda implemtation
|
5040 | * @param {String} methodname property to check for a custom implementation
|
5041 | * @return {Object} Whatever the return value of the method is.
|
5042 | */
|
5043 | var _checkForMethod = function _checkForMethod(methodname, fn) {
|
5044 | return function () {
|
5045 | var length = arguments.length;
|
5046 | if (length === 0) {
|
5047 | return fn();
|
5048 | }
|
5049 | var obj = arguments[length - 1];
|
5050 | return _isArray(obj) || typeof obj[methodname] !== 'function' ? fn.apply(this, arguments) : obj[methodname].apply(obj, _slice(arguments, 0, length - 1));
|
5051 | };
|
5052 | };
|
5053 |
|
5054 | var _composeL = function _composeL(innerLens, outerLens) {
|
5055 | return lens(_compose(innerLens, outerLens), function (x, source) {
|
5056 | var newInnerValue = innerLens.set(x, outerLens(source));
|
5057 | return outerLens.set(newInnerValue, source);
|
5058 | });
|
5059 | };
|
5060 |
|
5061 | /**
|
5062 | * A right-associative two-argument composition function like `_compose`
|
5063 | * but with automatic handling of promises (or, more precisely,
|
5064 | * "thenables"). This function is used to construct a more general
|
5065 | * `composeP` function, which accepts any number of arguments.
|
5066 | *
|
5067 | * @private
|
5068 | * @category Function
|
5069 | * @param {Function} f A function.
|
5070 | * @param {Function} g A function.
|
5071 | * @return {Function} A new function that is the equivalent of `f(g(x))`.
|
5072 | * @example
|
5073 | *
|
5074 | * var Q = require('q');
|
5075 | * var double = function(x) { return x * 2; };
|
5076 | * var squareAsync = function(x) { return Q.when(x * x); };
|
5077 | * var squareAsyncThenDouble = _composeP(double, squareAsync);
|
5078 | *
|
5079 | * squareAsyncThenDouble(5)
|
5080 | * .then(function(result) {
|
5081 | * // the result is now 50.
|
5082 | * });
|
5083 | */
|
5084 | var _composeP = function _composeP(f, g) {
|
5085 | return function () {
|
5086 | var context = this;
|
5087 | var value = g.apply(this, arguments);
|
5088 | if (_isThenable(value)) {
|
5089 | return value.then(function (result) {
|
5090 | return f.call(context, result);
|
5091 | });
|
5092 | } else {
|
5093 | return f.call(this, value);
|
5094 | }
|
5095 | };
|
5096 | };
|
5097 |
|
5098 | var _contains = function _contains(a, list) {
|
5099 | return _indexOf(list, a) >= 0;
|
5100 | };
|
5101 |
|
5102 | /*
|
5103 | * Returns a function that makes a multi-argument version of compose from
|
5104 | * either _compose or _composeP.
|
5105 | */
|
5106 | var _createComposer = function _createComposer(composeFunction) {
|
5107 | return function () {
|
5108 | var idx = arguments.length - 1;
|
5109 | var fn = arguments[idx];
|
5110 | var length = fn.length;
|
5111 | while (--idx >= 0) {
|
5112 | fn = composeFunction(arguments[idx], fn);
|
5113 | }
|
5114 | return arity(length, fn);
|
5115 | };
|
5116 | };
|
5117 |
|
5118 | /**
|
5119 | * Create a function which takes a list
|
5120 | * and determines the winning value by a comparator. Used internally
|
5121 | * by `R.max` and `R.min`
|
5122 | *
|
5123 | * @private
|
5124 | * @param {Function} compatator a function to compare two items
|
5125 | * @param {*} intialVal, default value if nothing else wins
|
5126 | * @category Math
|
5127 | * @return {Function}
|
5128 | */
|
5129 | var _createMaxMin = function _createMaxMin(comparator, initialVal) {
|
5130 | return _curry1(function (list) {
|
5131 | var idx = -1, winner = initialVal, computed;
|
5132 | while (++idx < list.length) {
|
5133 | computed = +list[idx];
|
5134 | if (comparator(computed, winner)) {
|
5135 | winner = computed;
|
5136 | }
|
5137 | }
|
5138 | return winner;
|
5139 | });
|
5140 | };
|
5141 |
|
5142 | var _createPartialApplicator = function _createPartialApplicator(concat) {
|
5143 | return function (fn) {
|
5144 | var args = _slice(arguments, 1);
|
5145 | return arity(Math.max(0, fn.length - args.length), function () {
|
5146 | return fn.apply(this, concat(args, arguments));
|
5147 | });
|
5148 | };
|
5149 | };
|
5150 |
|
5151 | /**
|
5152 | * Returns a function that dispatches with different strategies based on the
|
5153 | * object in list position (last argument). If it is an array, executes [fn].
|
5154 | * Otherwise, if it has a function with [methodname], it will execute that
|
5155 | * function (functor case). Otherwise, if it is a transformer, uses transducer
|
5156 | * [xf] to return a new transformer (transducer case). Otherwise, it will
|
5157 | * default to executing [fn].
|
5158 | *
|
5159 | * @private
|
5160 | * @param {String} methodname property to check for a custom implementation
|
5161 | * @param {Function} xf transducer to initialize if object is transformer
|
5162 | * @param {Function} fn default ramda implementation
|
5163 | * @return {Function} A function that dispatches on object in list position
|
5164 | */
|
5165 | var _dispatchable = function _dispatchable(methodname, xf, fn) {
|
5166 | return function () {
|
5167 | var length = arguments.length;
|
5168 | if (length === 0) {
|
5169 | return fn();
|
5170 | }
|
5171 | var obj = arguments[length - 1];
|
5172 | if (!_isArray(obj)) {
|
5173 | var args = _slice(arguments, 0, length - 1);
|
5174 | if (typeof obj[methodname] === 'function') {
|
5175 | return obj[methodname].apply(obj, args);
|
5176 | }
|
5177 | if (_isTransformer(obj)) {
|
5178 | var transducer = xf.apply(null, args);
|
5179 | return transducer(obj);
|
5180 | }
|
5181 | }
|
5182 | return fn.apply(this, arguments);
|
5183 | };
|
5184 | };
|
5185 |
|
5186 | var _dissocPath = function _dissocPath(path, obj) {
|
5187 | switch (path.length) {
|
5188 | case 0:
|
5189 | return obj;
|
5190 | case 1:
|
5191 | return _dissoc(path[0], obj);
|
5192 | default:
|
5193 | var head = path[0];
|
5194 | var tail = _slice(path, 1);
|
5195 | return obj[head] == null ? obj : _assoc(head, _dissocPath(tail, obj[head]), obj);
|
5196 | }
|
5197 | };
|
5198 |
|
5199 | /**
|
5200 | * Private function that determines whether or not a provided object has a given method.
|
5201 | * Does not ignore methods stored on the object's prototype chain. Used for dynamically
|
5202 | * dispatching Ramda methods to non-Array objects.
|
5203 | *
|
5204 | * @private
|
5205 | * @param {String} methodName The name of the method to check for.
|
5206 | * @param {Object} obj The object to test.
|
5207 | * @return {Boolean} `true` has a given method, `false` otherwise.
|
5208 | * @example
|
5209 | *
|
5210 | * var person = { name: 'John' };
|
5211 | * person.shout = function() { alert(this.name); };
|
5212 | *
|
5213 | * _hasMethod('shout', person); //=> true
|
5214 | * _hasMethod('foo', person); //=> false
|
5215 | */
|
5216 | var _hasMethod = function _hasMethod(methodName, obj) {
|
5217 | return obj != null && !_isArray(obj) && typeof obj[methodName] === 'function';
|
5218 | };
|
5219 |
|
5220 | /**
|
5221 | * `_makeFlat` is a helper function that returns a one-level or fully recursive function
|
5222 | * based on the flag passed in.
|
5223 | *
|
5224 | * @private
|
5225 | */
|
5226 | var _makeFlat = function _makeFlat(recursive) {
|
5227 | return function flatt(list) {
|
5228 | var value, result = [], idx = -1, j, ilen = list.length, jlen;
|
5229 | while (++idx < ilen) {
|
5230 | if (isArrayLike(list[idx])) {
|
5231 | value = recursive ? flatt(list[idx]) : list[idx];
|
5232 | j = -1;
|
5233 | jlen = value.length;
|
5234 | while (++j < jlen) {
|
5235 | result[result.length] = value[j];
|
5236 | }
|
5237 | } else {
|
5238 | result[result.length] = list[idx];
|
5239 | }
|
5240 | }
|
5241 | return result;
|
5242 | };
|
5243 | };
|
5244 |
|
5245 | var _reduce = function () {
|
5246 | function _arrayReduce(xf, acc, list) {
|
5247 | var idx = -1, len = list.length;
|
5248 | while (++idx < len) {
|
5249 | acc = xf['@@transducer/step'](acc, list[idx]);
|
5250 | if (acc && acc['@@transducer/reduced']) {
|
5251 | acc = acc['@@transducer/value'];
|
5252 | break;
|
5253 | }
|
5254 | }
|
5255 | return xf['@@transducer/result'](acc);
|
5256 | }
|
5257 | function _iterableReduce(xf, acc, iter) {
|
5258 | var step = iter.next();
|
5259 | while (!step.done) {
|
5260 | acc = xf['@@transducer/step'](acc, step.value);
|
5261 | if (acc && acc['@@transducer/reduced']) {
|
5262 | acc = acc['@@transducer/value'];
|
5263 | break;
|
5264 | }
|
5265 | step = iter.next();
|
5266 | }
|
5267 | return xf['@@transducer/result'](acc);
|
5268 | }
|
5269 | function _methodReduce(xf, acc, obj) {
|
5270 | return xf['@@transducer/result'](obj.reduce(bind(xf['@@transducer/step'], xf), acc));
|
5271 | }
|
5272 | var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
|
5273 | return function _reduce(fn, acc, list) {
|
5274 | if (typeof fn === 'function') {
|
5275 | fn = _xwrap(fn);
|
5276 | }
|
5277 | if (isArrayLike(list)) {
|
5278 | return _arrayReduce(fn, acc, list);
|
5279 | }
|
5280 | if (typeof list.reduce === 'function') {
|
5281 | return _methodReduce(fn, acc, list);
|
5282 | }
|
5283 | if (list[symIterator] != null) {
|
5284 | return _iterableReduce(fn, acc, list[symIterator]());
|
5285 | }
|
5286 | if (typeof list.next === 'function') {
|
5287 | return _iterableReduce(fn, acc, list);
|
5288 | }
|
5289 | throw new TypeError('reduce: list must be array or iterable');
|
5290 | };
|
5291 | }();
|
5292 |
|
5293 | var _xall = function () {
|
5294 | function XAll(f, xf) {
|
5295 | this.xf = xf;
|
5296 | this.f = f;
|
5297 | this.all = true;
|
5298 | }
|
5299 | XAll.prototype['@@transducer/init'] = _xfBase.init;
|
5300 | XAll.prototype['@@transducer/result'] = function (result) {
|
5301 | if (this.all) {
|
5302 | result = this.xf['@@transducer/step'](result, true);
|
5303 | }
|
5304 | return this.xf['@@transducer/result'](result);
|
5305 | };
|
5306 | XAll.prototype['@@transducer/step'] = function (result, input) {
|
5307 | if (!this.f(input)) {
|
5308 | this.all = false;
|
5309 | result = _reduced(this.xf['@@transducer/step'](result, false));
|
5310 | }
|
5311 | return result;
|
5312 | };
|
5313 | return _curry2(function _xall(f, xf) {
|
5314 | return new XAll(f, xf);
|
5315 | });
|
5316 | }();
|
5317 |
|
5318 | var _xany = function () {
|
5319 | function XAny(f, xf) {
|
5320 | this.xf = xf;
|
5321 | this.f = f;
|
5322 | this.any = false;
|
5323 | }
|
5324 | XAny.prototype['@@transducer/init'] = _xfBase.init;
|
5325 | XAny.prototype['@@transducer/result'] = function (result) {
|
5326 | if (!this.any) {
|
5327 | result = this.xf['@@transducer/step'](result, false);
|
5328 | }
|
5329 | return this.xf['@@transducer/result'](result);
|
5330 | };
|
5331 | XAny.prototype['@@transducer/step'] = function (result, input) {
|
5332 | if (this.f(input)) {
|
5333 | this.any = true;
|
5334 | result = _reduced(this.xf['@@transducer/step'](result, true));
|
5335 | }
|
5336 | return result;
|
5337 | };
|
5338 | return _curry2(function _xany(f, xf) {
|
5339 | return new XAny(f, xf);
|
5340 | });
|
5341 | }();
|
5342 |
|
5343 | var _xdrop = function () {
|
5344 | function XDrop(n, xf) {
|
5345 | this.xf = xf;
|
5346 | this.n = n;
|
5347 | }
|
5348 | XDrop.prototype['@@transducer/init'] = _xfBase.init;
|
5349 | XDrop.prototype['@@transducer/result'] = _xfBase.result;
|
5350 | XDrop.prototype.step = function (result, input) {
|
5351 | if (this.n > 0) {
|
5352 | this.n -= 1;
|
5353 | return result;
|
5354 | }
|
5355 | return this.xf['@@transducer/step'](result, input);
|
5356 | };
|
5357 | return _curry2(function _xdrop(n, xf) {
|
5358 | return new XDrop(n, xf);
|
5359 | });
|
5360 | }();
|
5361 |
|
5362 | var _xdropWhile = function () {
|
5363 | function XDropWhile(f, xf) {
|
5364 | this.xf = xf;
|
5365 | this.f = f;
|
5366 | }
|
5367 | XDropWhile.prototype['@@transducer/init'] = _xfBase.init;
|
5368 | XDropWhile.prototype['@@transducer/result'] = _xfBase.result;
|
5369 | XDropWhile.prototype['@@transducer/step'] = function (result, input) {
|
5370 | if (this.f) {
|
5371 | if (this.f(input)) {
|
5372 | return result;
|
5373 | }
|
5374 | this.f = null;
|
5375 | }
|
5376 | return this.xf['@@transducer/step'](result, input);
|
5377 | };
|
5378 | return _curry2(function _xdropWhile(f, xf) {
|
5379 | return new XDropWhile(f, xf);
|
5380 | });
|
5381 | }();
|
5382 |
|
5383 | var _xgroupBy = function () {
|
5384 | function XGroupBy(f, xf) {
|
5385 | this.xf = xf;
|
5386 | this.f = f;
|
5387 | this.inputs = {};
|
5388 | }
|
5389 | XGroupBy.prototype['@@transducer/init'] = _xfBase.init;
|
5390 | XGroupBy.prototype['@@transducer/result'] = function (result) {
|
5391 | var key;
|
5392 | for (key in this.inputs) {
|
5393 | if (_has(key, this.inputs)) {
|
5394 | result = this.xf['@@transducer/step'](result, this.inputs[key]);
|
5395 | if (result['@@transducer/reduced']) {
|
5396 | result = result['@@transducer/value'];
|
5397 | break;
|
5398 | }
|
5399 | }
|
5400 | }
|
5401 | return this.xf['@@transducer/result'](result);
|
5402 | };
|
5403 | XGroupBy.prototype['@@transducer/step'] = function (result, input) {
|
5404 | var key = this.f(input);
|
5405 | this.inputs[key] = this.inputs[key] || [
|
5406 | key,
|
5407 | []
|
5408 | ];
|
5409 | this.inputs[key][1] = _append(input, this.inputs[key][1]);
|
5410 | return result;
|
5411 | };
|
5412 | return _curry2(function _xgroupBy(f, xf) {
|
5413 | return new XGroupBy(f, xf);
|
5414 | });
|
5415 | }();
|
5416 |
|
5417 | /**
|
5418 | * Returns `true` if all elements of the list match the predicate, `false` if there are any
|
5419 | * that don't.
|
5420 | *
|
5421 | * Acts as a transducer if a transformer is given in list position.
|
5422 | * @see R.transduce
|
5423 | *
|
5424 | * @func
|
5425 | * @memberOf R
|
5426 | * @category List
|
5427 | * @sig (a -> Boolean) -> [a] -> Boolean
|
5428 | * @param {Function} fn The predicate function.
|
5429 | * @param {Array} list The array to consider.
|
5430 | * @return {Boolean} `true` if the predicate is satisfied by every element, `false`
|
5431 | * otherwise.
|
5432 | * @example
|
5433 | *
|
5434 | * var lessThan2 = R.flip(R.lt)(2);
|
5435 | * var lessThan3 = R.flip(R.lt)(3);
|
5436 | * R.all(lessThan2)([1, 2]); //=> false
|
5437 | * R.all(lessThan3)([1, 2]); //=> true
|
5438 | */
|
5439 | var all = _curry2(_dispatchable('all', _xall, _all));
|
5440 |
|
5441 | /**
|
5442 | * A function that returns the first argument if it's falsy otherwise the second
|
5443 | * argument. Note that this is NOT short-circuited, meaning that if expressions
|
5444 | * are passed they are both evaluated.
|
5445 | *
|
5446 | * Dispatches to the `and` method of the first argument if applicable.
|
5447 | *
|
5448 | * @func
|
5449 | * @memberOf R
|
5450 | * @category Logic
|
5451 | * @sig * -> * -> *
|
5452 | * @param {*} a any value
|
5453 | * @param {*} b any other value
|
5454 | * @return {*} the first argument if falsy otherwise the second argument.
|
5455 | * @example
|
5456 | *
|
5457 | * R.and(false, true); //=> false
|
5458 | * R.and(0, []); //=> 0
|
5459 | * R.and(null, ''); => null
|
5460 | */
|
5461 | var and = _curry2(function and(a, b) {
|
5462 | return _hasMethod('and', a) ? a.and(b) : a && b;
|
5463 | });
|
5464 |
|
5465 | /**
|
5466 | * Returns `true` if at least one of elements of the list match the predicate, `false`
|
5467 | * otherwise.
|
5468 | *
|
5469 | * Acts as a transducer if a transformer is given in list position.
|
5470 | * @see R.transduce
|
5471 | *
|
5472 | * @func
|
5473 | * @memberOf R
|
5474 | * @category List
|
5475 | * @sig (a -> Boolean) -> [a] -> Boolean
|
5476 | * @param {Function} fn The predicate function.
|
5477 | * @param {Array} list The array to consider.
|
5478 | * @return {Boolean} `true` if the predicate is satisfied by at least one element, `false`
|
5479 | * otherwise.
|
5480 | * @example
|
5481 | *
|
5482 | * var lessThan0 = R.flip(R.lt)(0);
|
5483 | * var lessThan2 = R.flip(R.lt)(2);
|
5484 | * R.any(lessThan0)([1, 2]); //=> false
|
5485 | * R.any(lessThan2)([1, 2]); //=> true
|
5486 | */
|
5487 | var any = _curry2(_dispatchable('any', _xany, _any));
|
5488 |
|
5489 | /**
|
5490 | * Returns a new list containing the contents of the given list, followed by the given
|
5491 | * element.
|
5492 | *
|
5493 | * @func
|
5494 | * @memberOf R
|
5495 | * @category List
|
5496 | * @sig a -> [a] -> [a]
|
5497 | * @param {*} el The element to add to the end of the new list.
|
5498 | * @param {Array} list The list whose contents will be added to the beginning of the output
|
5499 | * list.
|
5500 | * @return {Array} A new list containing the contents of the old list followed by `el`.
|
5501 | * @example
|
5502 | *
|
5503 | * R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
|
5504 | * R.append('tests', []); //=> ['tests']
|
5505 | * R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
|
5506 | */
|
5507 | var append = _curry2(_append);
|
5508 |
|
5509 | /**
|
5510 | * Makes a shallow clone of an object, setting or overriding the nodes
|
5511 | * required to create the given path, and placing the specific value at the
|
5512 | * tail end of that path. Note that this copies and flattens prototype
|
5513 | * properties onto the new object as well. All non-primitive properties
|
5514 | * are copied by reference.
|
5515 | *
|
5516 | * @func
|
5517 | * @memberOf R
|
5518 | * @category Object
|
5519 | * @sig [String] -> a -> {k: v} -> {k: v}
|
5520 | * @param {Array} path the path to set
|
5521 | * @param {*} val the new value
|
5522 | * @param {Object} obj the object to clone
|
5523 | * @return {Object} a new object similar to the original except along the specified path.
|
5524 | * @example
|
5525 | *
|
5526 | * R.assocPath(['a', 'b', 'c'], 42, {a: {b: {c: 0}}}); //=> {a: {b: {c: 42}}}
|
5527 | */
|
5528 | var assocPath = _curry3(_assocPath);
|
5529 |
|
5530 | /**
|
5531 | * Wraps a function of any arity (including nullary) in a function that accepts exactly 2
|
5532 | * parameters. Any extraneous parameters will not be passed to the supplied function.
|
5533 | *
|
5534 | * @func
|
5535 | * @memberOf R
|
5536 | * @category Function
|
5537 | * @sig (* -> c) -> (a, b -> c)
|
5538 | * @param {Function} fn The function to wrap.
|
5539 | * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
|
5540 | * arity 2.
|
5541 | * @example
|
5542 | *
|
5543 | * var takesThreeArgs = function(a, b, c) {
|
5544 | * return [a, b, c];
|
5545 | * };
|
5546 | * takesThreeArgs.length; //=> 3
|
5547 | * takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
|
5548 | *
|
5549 | * var takesTwoArgs = R.binary(takesThreeArgs);
|
5550 | * takesTwoArgs.length; //=> 2
|
5551 | * // Only 2 arguments are passed to the wrapped function
|
5552 | * takesTwoArgs(1, 2, 3); //=> [1, 2, undefined]
|
5553 | */
|
5554 | var binary = _curry1(function binary(fn) {
|
5555 | return nAry(2, fn);
|
5556 | });
|
5557 |
|
5558 | /**
|
5559 | * Creates a deep copy of the value which may contain (nested) `Array`s and
|
5560 | * `Object`s, `Number`s, `String`s, `Boolean`s and `Date`s. `Function`s are
|
5561 | * not copied, but assigned by their reference.
|
5562 | *
|
5563 | * @func
|
5564 | * @memberOf R
|
5565 | * @category Object
|
5566 | * @sig {*} -> {*}
|
5567 | * @param {*} value The object or array to clone
|
5568 | * @return {*} A new object or array.
|
5569 | * @example
|
5570 | *
|
5571 | * var objects = [{}, {}, {}];
|
5572 | * var objectsClone = R.clone(objects);
|
5573 | * objects[0] === objectsClone[0]; //=> false
|
5574 | */
|
5575 | var clone = _curry1(function clone(value) {
|
5576 | return _baseCopy(value, [], []);
|
5577 | });
|
5578 |
|
5579 | /**
|
5580 | * Creates a new function that runs each of the functions supplied as parameters in turn,
|
5581 | * passing the return value of each function invocation to the next function invocation,
|
5582 | * beginning with whatever arguments were passed to the initial invocation.
|
5583 | *
|
5584 | * Note that `compose` is a right-associative function, which means the functions provided
|
5585 | * will be invoked in order from right to left. In the example `var h = compose(f, g)`,
|
5586 | * the function `h` is equivalent to `f( g(x) )`, where `x` represents the arguments
|
5587 | * originally passed to `h`.
|
5588 | *
|
5589 | * @func
|
5590 | * @memberOf R
|
5591 | * @category Function
|
5592 | * @sig ((y -> z), (x -> y), ..., (b -> c), (a... -> b)) -> (a... -> z)
|
5593 | * @param {...Function} functions A variable number of functions.
|
5594 | * @return {Function} A new function which represents the result of calling each of the
|
5595 | * input `functions`, passing the result of each function call to the next, from
|
5596 | * right to left.
|
5597 | * @example
|
5598 | *
|
5599 | * var triple = function(x) { return x * 3; };
|
5600 | * var double = function(x) { return x * 2; };
|
5601 | * var square = function(x) { return x * x; };
|
5602 | * var squareThenDoubleThenTriple = R.compose(triple, double, square);
|
5603 | *
|
5604 | * //≅ triple(double(square(5)))
|
5605 | * squareThenDoubleThenTriple(5); //=> 150
|
5606 | */
|
5607 | var compose = _createComposer(_compose);
|
5608 |
|
5609 | /**
|
5610 | * Creates a new lens that allows getting and setting values of nested properties, by
|
5611 | * following each given lens in succession.
|
5612 | *
|
5613 | * Note that `composeL` is a right-associative function, which means the lenses provided
|
5614 | * will be invoked in order from right to left.
|
5615 | *
|
5616 | * @func
|
5617 | * @memberOf R
|
5618 | * @category Function
|
5619 | * @see R.lens
|
5620 | * @sig ((y -> z), (x -> y), ..., (b -> c), (a -> b)) -> (a -> z)
|
5621 | * @param {...Function} lenses A variable number of lenses.
|
5622 | * @return {Function} A new lens which represents the result of calling each of the
|
5623 | * input `lenses`, passing the result of each getter/setter as the source
|
5624 | * to the next, from right to left.
|
5625 | * @example
|
5626 | *
|
5627 | * var headLens = R.lensIndex(0);
|
5628 | * var secondLens = R.lensIndex(1);
|
5629 | * var xLens = R.lensProp('x');
|
5630 | * var secondOfXOfHeadLens = R.composeL(secondLens, xLens, headLens);
|
5631 | *
|
5632 | * var source = [{x: [0, 1], y: [2, 3]}, {x: [4, 5], y: [6, 7]}];
|
5633 | * secondOfXOfHeadLens(source); //=> 1
|
5634 | * secondOfXOfHeadLens.set(123, source); //=> [{x: [0, 123], y: [2, 3]}, {x: [4, 5], y: [6, 7]}]
|
5635 | */
|
5636 | var composeL = function () {
|
5637 | var idx = arguments.length - 1;
|
5638 | var fn = arguments[idx];
|
5639 | while (--idx >= 0) {
|
5640 | fn = _composeL(arguments[idx], fn);
|
5641 | }
|
5642 | return fn;
|
5643 | };
|
5644 |
|
5645 | /**
|
5646 | * Similar to `compose` but with automatic handling of promises (or, more
|
5647 | * precisely, "thenables"). The behavior is identical to that of
|
5648 | * compose() if all composed functions return something other than
|
5649 | * promises (i.e., objects with a .then() method). If one of the function
|
5650 | * returns a promise, however, then the next function in the composition
|
5651 | * is called asynchronously, in the success callback of the promise, using
|
5652 | * the resolved value as an input. Note that `composeP` is a right-
|
5653 | * associative function, just like `compose`.
|
5654 | *
|
5655 | * @func
|
5656 | * @memberOf R
|
5657 | * @category Function
|
5658 | * @sig ((y -> z), (x -> y), ..., (b -> c), (a... -> b)) -> (a... -> z)
|
5659 | * @param {...Function} functions A variable number of functions.
|
5660 | * @return {Function} A new function which represents the result of calling each of the
|
5661 | * input `functions`, passing either the returned result or the asynchronously
|
5662 | * resolved value) of each function call to the next, from right to left.
|
5663 | * @example
|
5664 | *
|
5665 | * var Q = require('q');
|
5666 | * var triple = function(x) { return x * 3; };
|
5667 | * var double = function(x) { return x * 2; };
|
5668 | * var squareAsync = function(x) { return Q.when(x * x); };
|
5669 | * var squareAsyncThenDoubleThenTriple = R.composeP(triple, double, squareAsync);
|
5670 | *
|
5671 | * //≅ squareAsync(5).then(function(x) { return triple(double(x)) };
|
5672 | * squareAsyncThenDoubleThenTriple(5)
|
5673 | * .then(function(result) {
|
5674 | * // result is 150
|
5675 | * });
|
5676 | */
|
5677 | var composeP = _createComposer(_composeP);
|
5678 |
|
5679 | /**
|
5680 | * Returns a new list consisting of the elements of the first list followed by the elements
|
5681 | * of the second.
|
5682 | *
|
5683 | * @func
|
5684 | * @memberOf R
|
5685 | * @category List
|
5686 | * @sig [a] -> [a] -> [a]
|
5687 | * @param {Array} list1 The first list to merge.
|
5688 | * @param {Array} list2 The second set to merge.
|
5689 | * @return {Array} A new array consisting of the contents of `list1` followed by the
|
5690 | * contents of `list2`. If, instead of an Array for `list1`, you pass an
|
5691 | * object with a `concat` method on it, `concat` will call `list1.concat`
|
5692 | * and pass it the value of `list2`.
|
5693 | *
|
5694 | * @example
|
5695 | *
|
5696 | * R.concat([], []); //=> []
|
5697 | * R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
|
5698 | * R.concat('ABC', 'DEF'); // 'ABCDEF'
|
5699 | */
|
5700 | var concat = _curry2(function (set1, set2) {
|
5701 | if (_isArray(set2)) {
|
5702 | return _concat(set1, set2);
|
5703 | } else if (_hasMethod('concat', set1)) {
|
5704 | return set1.concat(set2);
|
5705 | } else {
|
5706 | throw new TypeError('can\'t concat ' + typeof set1);
|
5707 | }
|
5708 | });
|
5709 |
|
5710 | /**
|
5711 | * Returns `true` if the specified item is somewhere in the list, `false` otherwise.
|
5712 | * Equivalent to `indexOf(a, list) >= 0`.
|
5713 | *
|
5714 | * Has `Object.is` semantics: `NaN` is considered equal to `NaN`; `0` and `-0`
|
5715 | * are not considered equal.
|
5716 | *
|
5717 | * @func
|
5718 | * @memberOf R
|
5719 | * @category List
|
5720 | * @sig a -> [a] -> Boolean
|
5721 | * @param {Object} a The item to compare against.
|
5722 | * @param {Array} list The array to consider.
|
5723 | * @return {Boolean} `true` if the item is in the list, `false` otherwise.
|
5724 | *
|
5725 | * @example
|
5726 | *
|
5727 | * R.contains(3)([1, 2, 3]); //=> true
|
5728 | * R.contains(4)([1, 2, 3]); //=> false
|
5729 | * R.contains({})([{}, {}]); //=> false
|
5730 | * var obj = {};
|
5731 | * R.contains(obj)([{}, obj, {}]); //=> true
|
5732 | */
|
5733 | var contains = _curry2(_contains);
|
5734 |
|
5735 | /**
|
5736 | * Returns a curried equivalent of the provided function. The curried
|
5737 | * function has two unusual capabilities. First, its arguments needn't
|
5738 | * be provided one at a time. If `f` is a ternary function and `g` is
|
5739 | * `R.curry(f)`, the following are equivalent:
|
5740 | *
|
5741 | * - `g(1)(2)(3)`
|
5742 | * - `g(1)(2, 3)`
|
5743 | * - `g(1, 2)(3)`
|
5744 | * - `g(1, 2, 3)`
|
5745 | *
|
5746 | * Secondly, the special placeholder value `R.__` may be used to specify
|
5747 | * "gaps", allowing partial application of any combination of arguments,
|
5748 | * regardless of their positions. If `g` is as above and `_` is `R.__`,
|
5749 | * the following are equivalent:
|
5750 | *
|
5751 | * - `g(1, 2, 3)`
|
5752 | * - `g(_, 2, 3)(1)`
|
5753 | * - `g(_, _, 3)(1)(2)`
|
5754 | * - `g(_, _, 3)(1, 2)`
|
5755 | * - `g(_, 2)(1)(3)`
|
5756 | * - `g(_, 2)(1, 3)`
|
5757 | * - `g(_, 2)(_, 3)(1)`
|
5758 | *
|
5759 | * @func
|
5760 | * @memberOf R
|
5761 | * @category Function
|
5762 | * @sig (* -> a) -> (* -> a)
|
5763 | * @param {Function} fn The function to curry.
|
5764 | * @return {Function} A new, curried function.
|
5765 | * @see R.curryN
|
5766 | * @example
|
5767 | *
|
5768 | * var addFourNumbers = function(a, b, c, d) {
|
5769 | * return a + b + c + d;
|
5770 | * };
|
5771 | *
|
5772 | * var curriedAddFourNumbers = R.curry(addFourNumbers);
|
5773 | * var f = curriedAddFourNumbers(1, 2);
|
5774 | * var g = f(3);
|
5775 | * g(4); //=> 10
|
5776 | */
|
5777 | var curry = _curry1(function curry(fn) {
|
5778 | return curryN(fn.length, fn);
|
5779 | });
|
5780 |
|
5781 | /**
|
5782 | * Finds the set (i.e. no duplicates) of all elements in the first list not contained in the second list.
|
5783 | *
|
5784 | * @func
|
5785 | * @memberOf R
|
5786 | * @category Relation
|
5787 | * @sig [a] -> [a] -> [a]
|
5788 | * @param {Array} list1 The first list.
|
5789 | * @param {Array} list2 The second list.
|
5790 | * @return {Array} The elements in `list1` that are not in `list2`.
|
5791 | * @see R.differenceWith
|
5792 | * @example
|
5793 | *
|
5794 | * R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
|
5795 | * R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
|
5796 | */
|
5797 | var difference = _curry2(function difference(first, second) {
|
5798 | var out = [];
|
5799 | var idx = -1;
|
5800 | var firstLen = first.length;
|
5801 | while (++idx < firstLen) {
|
5802 | if (!_contains(first[idx], second) && !_contains(first[idx], out)) {
|
5803 | out[out.length] = first[idx];
|
5804 | }
|
5805 | }
|
5806 | return out;
|
5807 | });
|
5808 |
|
5809 | /**
|
5810 | * Makes a shallow clone of an object, omitting the property at the
|
5811 | * given path. Note that this copies and flattens prototype properties
|
5812 | * onto the new object as well. All non-primitive properties are copied
|
5813 | * by reference.
|
5814 | *
|
5815 | * @func
|
5816 | * @memberOf R
|
5817 | * @category Object
|
5818 | * @sig [String] -> {k: v} -> {k: v}
|
5819 | * @param {Array} path the path to set
|
5820 | * @param {Object} obj the object to clone
|
5821 | * @return {Object} a new object without the property at path
|
5822 | * @example
|
5823 | *
|
5824 | * R.dissocPath(['a', 'b', 'c'], {a: {b: {c: 42}}}); //=> {a: {b: {}}}
|
5825 | */
|
5826 | var dissocPath = _curry2(_dissocPath);
|
5827 |
|
5828 | /**
|
5829 | * Returns a list containing all but the first `n` elements of the given `list`.
|
5830 | *
|
5831 | * Acts as a transducer if a transformer is given in list position.
|
5832 | * @see R.transduce
|
5833 | *
|
5834 | * @func
|
5835 | * @memberOf R
|
5836 | * @category List
|
5837 | * @sig Number -> [a] -> [a]
|
5838 | * @param {Number} n The number of elements of `list` to skip.
|
5839 | * @param {Array} list The array to consider.
|
5840 | * @return {Array} The last `n` elements of `list`.
|
5841 | * @example
|
5842 | *
|
5843 | * R.drop(3, [1,2,3,4,5,6,7]); //=> [4,5,6,7]
|
5844 | */
|
5845 | var drop = _curry2(_dispatchable('drop', _xdrop, function drop(n, list) {
|
5846 | return n <= 0 ? list : _slice(list, n);
|
5847 | }));
|
5848 |
|
5849 | /**
|
5850 | * Returns a new list containing the last `n` elements of a given list, passing each value
|
5851 | * to the supplied predicate function, skipping elements while the predicate function returns
|
5852 | * `true`. The predicate function is passed one argument: *(value)*.
|
5853 | *
|
5854 | * Acts as a transducer if a transformer is given in list position.
|
5855 | * @see R.transduce
|
5856 | *
|
5857 | * @func
|
5858 | * @memberOf R
|
5859 | * @category List
|
5860 | * @sig (a -> Boolean) -> [a] -> [a]
|
5861 | * @param {Function} fn The function called per iteration.
|
5862 | * @param {Array} list The collection to iterate over.
|
5863 | * @return {Array} A new array.
|
5864 | * @example
|
5865 | *
|
5866 | * var lteTwo = function(x) {
|
5867 | * return x <= 2;
|
5868 | * };
|
5869 | *
|
5870 | * R.dropWhile(lteTwo, [1, 2, 3, 4]); //=> [3, 4]
|
5871 | */
|
5872 | var dropWhile = _curry2(_dispatchable('dropWhile', _xdropWhile, function dropWhile(pred, list) {
|
5873 | var idx = -1, len = list.length;
|
5874 | while (++idx < len && pred(list[idx])) {
|
5875 | }
|
5876 | return _slice(list, idx);
|
5877 | }));
|
5878 |
|
5879 | /**
|
5880 | * `empty` wraps any object in an array. This implementation is compatible with the
|
5881 | * Fantasy-land Monoid spec, and will work with types that implement that spec.
|
5882 | *
|
5883 | * @func
|
5884 | * @memberOf R
|
5885 | * @category Function
|
5886 | * @sig * -> []
|
5887 | * @return {Array} An empty array.
|
5888 | * @example
|
5889 | *
|
5890 | * R.empty([1,2,3,4,5]); //=> []
|
5891 | */
|
5892 | var empty = _curry1(function empty(x) {
|
5893 | return _hasMethod('empty', x) ? x.empty() : [];
|
5894 | });
|
5895 |
|
5896 | /**
|
5897 | * Returns a new list containing only those items that match a given predicate function.
|
5898 | * The predicate function is passed one argument: *(value)*.
|
5899 | *
|
5900 | * Note that `R.filter` does not skip deleted or unassigned indices, unlike the native
|
5901 | * `Array.prototype.filter` method. For more details on this behavior, see:
|
5902 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Description
|
5903 | *
|
5904 | * Acts as a transducer if a transformer is given in list position.
|
5905 | * @see R.transduce
|
5906 | *
|
5907 | * @func
|
5908 | * @memberOf R
|
5909 | * @category List
|
5910 | * @sig (a -> Boolean) -> [a] -> [a]
|
5911 | * @param {Function} fn The function called per iteration.
|
5912 | * @param {Array} list The collection to iterate over.
|
5913 | * @return {Array} The new filtered array.
|
5914 | * @example
|
5915 | *
|
5916 | * var isEven = function(n) {
|
5917 | * return n % 2 === 0;
|
5918 | * };
|
5919 | * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
|
5920 | */
|
5921 | var filter = _curry2(_dispatchable('filter', _xfilter, _filter));
|
5922 |
|
5923 | /**
|
5924 | * Returns the first element of the list which matches the predicate, or `undefined` if no
|
5925 | * element matches.
|
5926 | *
|
5927 | * Acts as a transducer if a transformer is given in list position.
|
5928 | * @see R.transduce
|
5929 | *
|
5930 | * @func
|
5931 | * @memberOf R
|
5932 | * @category List
|
5933 | * @sig (a -> Boolean) -> [a] -> a | undefined
|
5934 | * @param {Function} fn The predicate function used to determine if the element is the
|
5935 | * desired one.
|
5936 | * @param {Array} list The array to consider.
|
5937 | * @return {Object} The element found, or `undefined`.
|
5938 | * @example
|
5939 | *
|
5940 | * var xs = [{a: 1}, {a: 2}, {a: 3}];
|
5941 | * R.find(R.propEq('a', 2))(xs); //=> {a: 2}
|
5942 | * R.find(R.propEq('a', 4))(xs); //=> undefined
|
5943 | */
|
5944 | var find = _curry2(_dispatchable('find', _xfind, function find(fn, list) {
|
5945 | var idx = -1;
|
5946 | var len = list.length;
|
5947 | while (++idx < len) {
|
5948 | if (fn(list[idx])) {
|
5949 | return list[idx];
|
5950 | }
|
5951 | }
|
5952 | }));
|
5953 |
|
5954 | /**
|
5955 | * Returns the index of the first element of the list which matches the predicate, or `-1`
|
5956 | * if no element matches.
|
5957 | *
|
5958 | * Acts as a transducer if a transformer is given in list position.
|
5959 | * @see R.transduce
|
5960 | *
|
5961 | * @func
|
5962 | * @memberOf R
|
5963 | * @category List
|
5964 | * @sig (a -> Boolean) -> [a] -> Number
|
5965 | * @param {Function} fn The predicate function used to determine if the element is the
|
5966 | * desired one.
|
5967 | * @param {Array} list The array to consider.
|
5968 | * @return {Number} The index of the element found, or `-1`.
|
5969 | * @example
|
5970 | *
|
5971 | * var xs = [{a: 1}, {a: 2}, {a: 3}];
|
5972 | * R.findIndex(R.propEq('a', 2))(xs); //=> 1
|
5973 | * R.findIndex(R.propEq('a', 4))(xs); //=> -1
|
5974 | */
|
5975 | var findIndex = _curry2(_dispatchable('findIndex', _xfindIndex, function findIndex(fn, list) {
|
5976 | var idx = -1;
|
5977 | var len = list.length;
|
5978 | while (++idx < len) {
|
5979 | if (fn(list[idx])) {
|
5980 | return idx;
|
5981 | }
|
5982 | }
|
5983 | return -1;
|
5984 | }));
|
5985 |
|
5986 | /**
|
5987 | * Returns the last element of the list which matches the predicate, or `undefined` if no
|
5988 | * element matches.
|
5989 | *
|
5990 | * Acts as a transducer if a transformer is given in list position.
|
5991 | * @see R.transduce
|
5992 | *
|
5993 | * @func
|
5994 | * @memberOf R
|
5995 | * @category List
|
5996 | * @sig (a -> Boolean) -> [a] -> a | undefined
|
5997 | * @param {Function} fn The predicate function used to determine if the element is the
|
5998 | * desired one.
|
5999 | * @param {Array} list The array to consider.
|
6000 | * @return {Object} The element found, or `undefined`.
|
6001 | * @example
|
6002 | *
|
6003 | * var xs = [{a: 1, b: 0}, {a:1, b: 1}];
|
6004 | * R.findLast(R.propEq('a', 1))(xs); //=> {a: 1, b: 1}
|
6005 | * R.findLast(R.propEq('a', 4))(xs); //=> undefined
|
6006 | */
|
6007 | var findLast = _curry2(_dispatchable('findLast', _xfindLast, function findLast(fn, list) {
|
6008 | var idx = list.length;
|
6009 | while (--idx >= 0) {
|
6010 | if (fn(list[idx])) {
|
6011 | return list[idx];
|
6012 | }
|
6013 | }
|
6014 | }));
|
6015 |
|
6016 | /**
|
6017 | * Returns the index of the last element of the list which matches the predicate, or
|
6018 | * `-1` if no element matches.
|
6019 | *
|
6020 | * Acts as a transducer if a transformer is given in list position.
|
6021 | * @see R.transduce
|
6022 | *
|
6023 | * @func
|
6024 | * @memberOf R
|
6025 | * @category List
|
6026 | * @sig (a -> Boolean) -> [a] -> Number
|
6027 | * @param {Function} fn The predicate function used to determine if the element is the
|
6028 | * desired one.
|
6029 | * @param {Array} list The array to consider.
|
6030 | * @return {Number} The index of the element found, or `-1`.
|
6031 | * @example
|
6032 | *
|
6033 | * var xs = [{a: 1, b: 0}, {a:1, b: 1}];
|
6034 | * R.findLastIndex(R.propEq('a', 1))(xs); //=> 1
|
6035 | * R.findLastIndex(R.propEq('a', 4))(xs); //=> -1
|
6036 | */
|
6037 | var findLastIndex = _curry2(_dispatchable('findLastIndex', _xfindLastIndex, function findLastIndex(fn, list) {
|
6038 | var idx = list.length;
|
6039 | while (--idx >= 0) {
|
6040 | if (fn(list[idx])) {
|
6041 | return idx;
|
6042 | }
|
6043 | }
|
6044 | return -1;
|
6045 | }));
|
6046 |
|
6047 | /**
|
6048 | * Returns a new list by pulling every item out of it (and all its sub-arrays) and putting
|
6049 | * them in a new array, depth-first.
|
6050 | *
|
6051 | * @func
|
6052 | * @memberOf R
|
6053 | * @category List
|
6054 | * @sig [a] -> [b]
|
6055 | * @param {Array} list The array to consider.
|
6056 | * @return {Array} The flattened list.
|
6057 | * @example
|
6058 | *
|
6059 | * R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
|
6060 | * //=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
6061 | */
|
6062 | var flatten = _curry1(_makeFlat(true));
|
6063 |
|
6064 | /**
|
6065 | * Returns a new function much like the supplied one, except that the first two arguments'
|
6066 | * order is reversed.
|
6067 | *
|
6068 | * @func
|
6069 | * @memberOf R
|
6070 | * @category Function
|
6071 | * @sig (a -> b -> c -> ... -> z) -> (b -> a -> c -> ... -> z)
|
6072 | * @param {Function} fn The function to invoke with its first two parameters reversed.
|
6073 | * @return {*} The result of invoking `fn` with its first two parameters' order reversed.
|
6074 | * @example
|
6075 | *
|
6076 | * var mergeThree = function(a, b, c) {
|
6077 | * return ([]).concat(a, b, c);
|
6078 | * };
|
6079 | *
|
6080 | * mergeThree(1, 2, 3); //=> [1, 2, 3]
|
6081 | *
|
6082 | * R.flip(mergeThree)(1, 2, 3); //=> [2, 1, 3]
|
6083 | */
|
6084 | var flip = _curry1(function flip(fn) {
|
6085 | return curry(function (a, b) {
|
6086 | var args = _slice(arguments);
|
6087 | args[0] = b;
|
6088 | args[1] = a;
|
6089 | return fn.apply(this, args);
|
6090 | });
|
6091 | });
|
6092 |
|
6093 | /**
|
6094 | * Returns a list of function names of object's own and prototype functions
|
6095 | *
|
6096 | * @func
|
6097 | * @memberOf R
|
6098 | * @category Object
|
6099 | * @sig {*} -> [String]
|
6100 | * @param {Object} obj The objects with functions in it
|
6101 | * @return {Array} A list of the object's own properties and prototype
|
6102 | * properties that map to functions.
|
6103 | * @example
|
6104 | *
|
6105 | * R.functionsIn(R); // returns list of ramda's own and prototype function names
|
6106 | *
|
6107 | * var F = function() { this.x = function(){}; this.y = 1; }
|
6108 | * F.prototype.z = function() {};
|
6109 | * F.prototype.a = 100;
|
6110 | * R.functionsIn(new F()); //=> ["x", "z"]
|
6111 | */
|
6112 | var functionsIn = _curry1(_functionsWith(keysIn));
|
6113 |
|
6114 | /**
|
6115 | * Splits a list into sub-lists stored in an object, based on the result of calling a String-returning function
|
6116 | * on each element, and grouping the results according to values returned.
|
6117 | *
|
6118 | * Acts as a transducer if a transformer is given in list position.
|
6119 | * @see R.transduce
|
6120 | *
|
6121 | * @func
|
6122 | * @memberOf R
|
6123 | * @category List
|
6124 | * @sig (a -> String) -> [a] -> {String: [a]}
|
6125 | * @param {Function} fn Function :: a -> String
|
6126 | * @param {Array} list The array to group
|
6127 | * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
|
6128 | * that produced that key when passed to `fn`.
|
6129 | * @example
|
6130 | *
|
6131 | * var byGrade = R.groupBy(function(student) {
|
6132 | * var score = student.score;
|
6133 | * return score < 65 ? 'F' :
|
6134 | * score < 70 ? 'D' :
|
6135 | * score < 80 ? 'C' :
|
6136 | * score < 90 ? 'B' : 'A';
|
6137 | * });
|
6138 | * var students = [{name: 'Abby', score: 84},
|
6139 | * {name: 'Eddy', score: 58},
|
6140 | * // ...
|
6141 | * {name: 'Jack', score: 69}];
|
6142 | * byGrade(students);
|
6143 | * // {
|
6144 | * // 'A': [{name: 'Dianne', score: 99}],
|
6145 | * // 'B': [{name: 'Abby', score: 84}]
|
6146 | * // // ...,
|
6147 | * // 'F': [{name: 'Eddy', score: 58}]
|
6148 | * // }
|
6149 | */
|
6150 | var groupBy = _curry2(_dispatchable('groupBy', _xgroupBy, function groupBy(fn, list) {
|
6151 | return _reduce(function (acc, elt) {
|
6152 | var key = fn(elt);
|
6153 | acc[key] = _append(elt, acc[key] || (acc[key] = []));
|
6154 | return acc;
|
6155 | }, {}, list);
|
6156 | }));
|
6157 |
|
6158 | /**
|
6159 | * Returns the first element in a list.
|
6160 | * In some libraries this function is named `first`.
|
6161 | *
|
6162 | * @func
|
6163 | * @memberOf R
|
6164 | * @category List
|
6165 | * @sig [a] -> a
|
6166 | * @param {Array} list The array to consider.
|
6167 | * @return {*} The first element of the list, or `undefined` if the list is empty.
|
6168 | * @example
|
6169 | *
|
6170 | * R.head(['fi', 'fo', 'fum']); //=> 'fi'
|
6171 | */
|
6172 | var head = nth(0);
|
6173 |
|
6174 | /**
|
6175 | * Inserts the supplied element into the list, at index `index`. _Note
|
6176 | * that this is not destructive_: it returns a copy of the list with the changes.
|
6177 | * <small>No lists have been harmed in the application of this function.</small>
|
6178 | *
|
6179 | * @func
|
6180 | * @memberOf R
|
6181 | * @category List
|
6182 | * @sig Number -> a -> [a] -> [a]
|
6183 | * @param {Number} index The position to insert the element
|
6184 | * @param {*} elt The element to insert into the Array
|
6185 | * @param {Array} list The list to insert into
|
6186 | * @return {Array} A new Array with `elt` inserted at `index`.
|
6187 | * @example
|
6188 | *
|
6189 | * R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
|
6190 | */
|
6191 | var insert = _curry3(function insert(idx, elt, list) {
|
6192 | idx = idx < list.length && idx >= 0 ? idx : list.length;
|
6193 | return _concat(_append(elt, _slice(list, 0, idx)), _slice(list, idx));
|
6194 | });
|
6195 |
|
6196 | /**
|
6197 | * Combines two lists into a set (i.e. no duplicates) composed of those
|
6198 | * elements common to both lists. Duplication is determined according
|
6199 | * to the value returned by applying the supplied predicate to two list
|
6200 | * elements.
|
6201 | *
|
6202 | * @func
|
6203 | * @memberOf R
|
6204 | * @category Relation
|
6205 | * @sig (a,a -> Boolean) -> [a] -> [a] -> [a]
|
6206 | * @param {Function} pred A predicate function that determines whether
|
6207 | * the two supplied elements are equal.
|
6208 | * @param {Array} list1 One list of items to compare
|
6209 | * @param {Array} list2 A second list of items to compare
|
6210 | * @see R.intersection
|
6211 | * @return {Array} A new list containing those elements common to both lists.
|
6212 | * @example
|
6213 | *
|
6214 | * var buffaloSpringfield = [
|
6215 | * {id: 824, name: 'Richie Furay'},
|
6216 | * {id: 956, name: 'Dewey Martin'},
|
6217 | * {id: 313, name: 'Bruce Palmer'},
|
6218 | * {id: 456, name: 'Stephen Stills'},
|
6219 | * {id: 177, name: 'Neil Young'}
|
6220 | * ];
|
6221 | * var csny = [
|
6222 | * {id: 204, name: 'David Crosby'},
|
6223 | * {id: 456, name: 'Stephen Stills'},
|
6224 | * {id: 539, name: 'Graham Nash'},
|
6225 | * {id: 177, name: 'Neil Young'}
|
6226 | * ];
|
6227 | *
|
6228 | * var sameId = function(o1, o2) {return o1.id === o2.id;};
|
6229 | *
|
6230 | * R.intersectionWith(sameId, buffaloSpringfield, csny);
|
6231 | * //=> [{id: 456, name: 'Stephen Stills'}, {id: 177, name: 'Neil Young'}]
|
6232 | */
|
6233 | var intersectionWith = _curry3(function intersectionWith(pred, list1, list2) {
|
6234 | var results = [], idx = -1;
|
6235 | while (++idx < list1.length) {
|
6236 | if (_containsWith(pred, list1[idx], list2)) {
|
6237 | results[results.length] = list1[idx];
|
6238 | }
|
6239 | }
|
6240 | return uniqWith(pred, results);
|
6241 | });
|
6242 |
|
6243 | /**
|
6244 | * Creates a new list with the separator interposed between elements.
|
6245 | *
|
6246 | * @func
|
6247 | * @memberOf R
|
6248 | * @category List
|
6249 | * @sig a -> [a] -> [a]
|
6250 | * @param {*} separator The element to add to the list.
|
6251 | * @param {Array} list The list to be interposed.
|
6252 | * @return {Array} The new list.
|
6253 | * @example
|
6254 | *
|
6255 | * R.intersperse('n', ['ba', 'a', 'a']); //=> ['ba', 'n', 'a', 'n', 'a']
|
6256 | */
|
6257 | var intersperse = _curry2(_checkForMethod('intersperse', function intersperse(separator, list) {
|
6258 | var out = [];
|
6259 | var idx = -1;
|
6260 | var length = list.length;
|
6261 | while (++idx < length) {
|
6262 | if (idx === length - 1) {
|
6263 | out.push(list[idx]);
|
6264 | } else {
|
6265 | out.push(list[idx], separator);
|
6266 | }
|
6267 | }
|
6268 | return out;
|
6269 | }));
|
6270 |
|
6271 | /**
|
6272 | * Returns the result of applying `obj[methodName]` to `args`.
|
6273 | *
|
6274 | * @func
|
6275 | * @memberOf R
|
6276 | * @category Object
|
6277 | * @sig String -> [*] -> Object -> *
|
6278 | * @param {String} methodName
|
6279 | * @param {Array} args
|
6280 | * @param {Object} obj
|
6281 | * @return {*}
|
6282 | * @example
|
6283 | *
|
6284 | * // toBinary :: Number -> String
|
6285 | * var toBinary = R.invoke('toString', [2])
|
6286 | *
|
6287 | * toBinary(42); //=> '101010'
|
6288 | * toBinary(63); //=> '111111'
|
6289 | */
|
6290 | var invoke = curry(function invoke(methodName, args, obj) {
|
6291 | return obj[methodName].apply(obj, args);
|
6292 | });
|
6293 |
|
6294 | /**
|
6295 | * Turns a named method with a specified arity into a function
|
6296 | * that can be called directly supplied with arguments and a target object.
|
6297 | *
|
6298 | * The returned function is curried and accepts `len + 1` parameters where
|
6299 | * the final parameter is the target object.
|
6300 | *
|
6301 | * @func
|
6302 | * @memberOf R
|
6303 | * @category Function
|
6304 | * @sig (Number, String) -> (a... -> c -> b)
|
6305 | * @param {Number} len Number of arguments the returned function should take
|
6306 | * before the target object.
|
6307 | * @param {Function} method Name of the method to call.
|
6308 | * @return {Function} A new curried function.
|
6309 | * @example
|
6310 | *
|
6311 | * var sliceFrom = R.invoker(1, 'slice');
|
6312 | * sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
|
6313 | * var sliceFrom6 = R.invoker(2, 'slice', 6);
|
6314 | * sliceFrom6(8, 'abcdefghijklm'); //=> 'gh'
|
6315 | */
|
6316 | var invoker = curry(function invoker(arity, method) {
|
6317 | var initialArgs = _slice(arguments, 2);
|
6318 | var len = arity - initialArgs.length;
|
6319 | return curryN(len + 1, function () {
|
6320 | var target = arguments[len];
|
6321 | var args = initialArgs.concat(_slice(arguments, 0, len));
|
6322 | return target[method].apply(target, args);
|
6323 | });
|
6324 | });
|
6325 |
|
6326 | /**
|
6327 | * Returns a string made by inserting the `separator` between each
|
6328 | * element and concatenating all the elements into a single string.
|
6329 | *
|
6330 | * @func
|
6331 | * @memberOf R
|
6332 | * @category List
|
6333 | * @sig String -> [a] -> String
|
6334 | * @param {Number|String} separator The string used to separate the elements.
|
6335 | * @param {Array} xs The elements to join into a string.
|
6336 | * @return {String} str The string made by concatenating `xs` with `separator`.
|
6337 | * @example
|
6338 | *
|
6339 | * var spacer = R.join(' ');
|
6340 | * spacer(['a', 2, 3.4]); //=> 'a 2 3.4'
|
6341 | * R.join('|', [1, 2, 3]); //=> '1|2|3'
|
6342 | */
|
6343 | var join = invoker(1, 'join');
|
6344 |
|
6345 | /**
|
6346 | * Returns a list containing the names of all the enumerable own
|
6347 | * properties of the supplied object.
|
6348 | * Note that the order of the output array is not guaranteed to be
|
6349 | * consistent across different JS platforms.
|
6350 | *
|
6351 | * @func
|
6352 | * @memberOf R
|
6353 | * @category Object
|
6354 | * @sig {k: v} -> [k]
|
6355 | * @param {Object} obj The object to extract properties from
|
6356 | * @return {Array} An array of the object's own properties.
|
6357 | * @example
|
6358 | *
|
6359 | * R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
|
6360 | */
|
6361 | // cover IE < 9 keys issues
|
6362 | var keys = function () {
|
6363 | // cover IE < 9 keys issues
|
6364 | var hasEnumBug = !{ toString: null }.propertyIsEnumerable('toString');
|
6365 | var nonEnumerableProps = [
|
6366 | 'constructor',
|
6367 | 'valueOf',
|
6368 | 'isPrototypeOf',
|
6369 | 'toString',
|
6370 | 'propertyIsEnumerable',
|
6371 | 'hasOwnProperty',
|
6372 | 'toLocaleString'
|
6373 | ];
|
6374 | return _curry1(function keys(obj) {
|
6375 | if (Object(obj) !== obj) {
|
6376 | return [];
|
6377 | }
|
6378 | if (Object.keys) {
|
6379 | return Object.keys(obj);
|
6380 | }
|
6381 | var prop, ks = [], nIdx;
|
6382 | for (prop in obj) {
|
6383 | if (_has(prop, obj)) {
|
6384 | ks[ks.length] = prop;
|
6385 | }
|
6386 | }
|
6387 | if (hasEnumBug) {
|
6388 | nIdx = nonEnumerableProps.length;
|
6389 | while (--nIdx >= 0) {
|
6390 | prop = nonEnumerableProps[nIdx];
|
6391 | if (_has(prop, obj) && !_contains(prop, ks)) {
|
6392 | ks[ks.length] = prop;
|
6393 | }
|
6394 | }
|
6395 | }
|
6396 | return ks;
|
6397 | });
|
6398 | }();
|
6399 |
|
6400 | /**
|
6401 | * Returns the last element from a list.
|
6402 | *
|
6403 | * @func
|
6404 | * @memberOf R
|
6405 | * @category List
|
6406 | * @sig [a] -> a
|
6407 | * @param {Array} list The array to consider.
|
6408 | * @return {*} The last element of the list, or `undefined` if the list is empty.
|
6409 | * @example
|
6410 | *
|
6411 | * R.last(['fi', 'fo', 'fum']); //=> 'fum'
|
6412 | */
|
6413 | var last = nth(-1);
|
6414 |
|
6415 | /**
|
6416 | * Creates a lens that will focus on index `n` of the source array.
|
6417 | *
|
6418 | * @func
|
6419 | * @memberOf R
|
6420 | * @category List
|
6421 | * @see R.lens
|
6422 | * @sig Number -> (a -> b)
|
6423 | * @param {Number} n The index of the array that the returned lens will focus on.
|
6424 | * @return {Function} the returned function has `set` and `map` properties that are
|
6425 | * also curried functions.
|
6426 | * @example
|
6427 | *
|
6428 | * var headLens = R.lensIndex(0);
|
6429 | * headLens([10, 20, 30, 40]); //=> 10
|
6430 | * headLens.set('mu', [10, 20, 30, 40]); //=> ['mu', 20, 30, 40]
|
6431 | * headLens.map(function(x) { return x + 1; }, [10, 20, 30, 40]); //=> [11, 20, 30, 40]
|
6432 | */
|
6433 | var lensIndex = function lensIndex(n) {
|
6434 | return lens(nth(n), function (x, xs) {
|
6435 | return _slice(xs, 0, n).concat([x], _slice(xs, n + 1));
|
6436 | });
|
6437 | };
|
6438 |
|
6439 | /**
|
6440 | * Creates a lens that will focus on property `k` of the source object.
|
6441 | *
|
6442 | * @func
|
6443 | * @memberOf R
|
6444 | * @category Object
|
6445 | * @see R.lens
|
6446 | * @sig String -> (a -> b)
|
6447 | * @param {String} k A string that represents a property to focus on.
|
6448 | * @return {Function} the returned function has `set` and `map` properties that are
|
6449 | * also curried functions.
|
6450 | * @example
|
6451 | *
|
6452 | * var phraseLens = R.lensProp('phrase');
|
6453 | * var obj1 = { phrase: 'Absolute filth . . . and I LOVED it!'};
|
6454 | * var obj2 = { phrase: "What's all this, then?"};
|
6455 | * phraseLens(obj1); // => 'Absolute filth . . . and I LOVED it!'
|
6456 | * phraseLens(obj2); // => "What's all this, then?"
|
6457 | * phraseLens.set('Ooh Betty', obj1); //=> { phrase: 'Ooh Betty'}
|
6458 | * phraseLens.map(R.toUpper, obj2); //=> { phrase: "WHAT'S ALL THIS, THEN?"}
|
6459 | */
|
6460 | var lensProp = function (k) {
|
6461 | return lens(prop(k), assoc(k));
|
6462 | };
|
6463 |
|
6464 | /**
|
6465 | * Returns a new list, constructed by applying the supplied function to every element of the
|
6466 | * supplied list.
|
6467 | *
|
6468 | * Note: `R.map` does not skip deleted or unassigned indices (sparse arrays), unlike the
|
6469 | * native `Array.prototype.map` method. For more details on this behavior, see:
|
6470 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description
|
6471 | *
|
6472 | * Acts as a transducer if a transformer is given in list position.
|
6473 | * @see R.transduce
|
6474 | *
|
6475 | * @func
|
6476 | * @memberOf R
|
6477 | * @category List
|
6478 | * @sig (a -> b) -> [a] -> [b]
|
6479 | * @param {Function} fn The function to be called on every element of the input `list`.
|
6480 | * @param {Array} list The list to be iterated over.
|
6481 | * @return {Array} The new list.
|
6482 | * @example
|
6483 | *
|
6484 | * var double = function(x) {
|
6485 | * return x * 2;
|
6486 | * };
|
6487 | *
|
6488 | * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
|
6489 | */
|
6490 | var map = _curry2(_dispatchable('map', _xmap, _map));
|
6491 |
|
6492 | /**
|
6493 | * Map, but for objects. Creates an object with the same keys as `obj` and values
|
6494 | * generated by running each property of `obj` through `fn`. `fn` is passed one argument:
|
6495 | * *(value)*.
|
6496 | *
|
6497 | * @func
|
6498 | * @memberOf R
|
6499 | * @category Object
|
6500 | * @sig (v -> v) -> {k: v} -> {k: v}
|
6501 | * @param {Function} fn A function called for each property in `obj`. Its return value will
|
6502 | * become a new property on the return object.
|
6503 | * @param {Object} obj The object to iterate over.
|
6504 | * @return {Object} A new object with the same keys as `obj` and values that are the result
|
6505 | * of running each property through `fn`.
|
6506 | * @example
|
6507 | *
|
6508 | * var values = { x: 1, y: 2, z: 3 };
|
6509 | * var double = function(num) {
|
6510 | * return num * 2;
|
6511 | * };
|
6512 | *
|
6513 | * R.mapObj(double, values); //=> { x: 2, y: 4, z: 6 }
|
6514 | */
|
6515 | var mapObj = _curry2(function mapObject(fn, obj) {
|
6516 | return _reduce(function (acc, key) {
|
6517 | acc[key] = fn(obj[key]);
|
6518 | return acc;
|
6519 | }, {}, keys(obj));
|
6520 | });
|
6521 |
|
6522 | /**
|
6523 | * Like `mapObj`, but but passes additional arguments to the predicate function. The
|
6524 | * predicate function is passed three arguments: *(value, key, obj)*.
|
6525 | *
|
6526 | * @func
|
6527 | * @memberOf R
|
6528 | * @category Object
|
6529 | * @sig (v, k, {k: v} -> v) -> {k: v} -> {k: v}
|
6530 | * @param {Function} fn A function called for each property in `obj`. Its return value will
|
6531 | * become a new property on the return object.
|
6532 | * @param {Object} obj The object to iterate over.
|
6533 | * @return {Object} A new object with the same keys as `obj` and values that are the result
|
6534 | * of running each property through `fn`.
|
6535 | * @example
|
6536 | *
|
6537 | * var values = { x: 1, y: 2, z: 3 };
|
6538 | * var prependKeyAndDouble = function(num, key, obj) {
|
6539 | * return key + (num * 2);
|
6540 | * };
|
6541 | *
|
6542 | * R.mapObjIndexed(prependKeyAndDouble, values); //=> { x: 'x2', y: 'y4', z: 'z6' }
|
6543 | */
|
6544 | var mapObjIndexed = _curry2(function mapObjectIndexed(fn, obj) {
|
6545 | return _reduce(function (acc, key) {
|
6546 | acc[key] = fn(obj[key], key, obj);
|
6547 | return acc;
|
6548 | }, {}, keys(obj));
|
6549 | });
|
6550 |
|
6551 | /**
|
6552 | * Tests a regular expression against a String
|
6553 | *
|
6554 | * @func
|
6555 | * @memberOf R
|
6556 | * @category String
|
6557 | * @sig RegExp -> String -> [String] | null
|
6558 | * @param {RegExp} rx A regular expression.
|
6559 | * @param {String} str The string to match against
|
6560 | * @return {Array} The list of matches, or null if no matches found.
|
6561 | * @see R.invoker
|
6562 | * @example
|
6563 | *
|
6564 | * R.match(/([a-z]a)/g, 'bananas'); //=> ['ba', 'na', 'na']
|
6565 | */
|
6566 | var match = invoker(1, 'match');
|
6567 |
|
6568 | /**
|
6569 | * Determines the largest of a list of numbers (or elements that can be cast to numbers)
|
6570 | *
|
6571 | * @func
|
6572 | * @memberOf R
|
6573 | * @category Math
|
6574 | * @sig [Number] -> Number
|
6575 | * @see R.maxBy
|
6576 | * @param {Array} list A list of numbers
|
6577 | * @return {Number} The greatest number in the list.
|
6578 | * @example
|
6579 | *
|
6580 | * R.max([7, 3, 9, 2, 4, 9, 3]); //=> 9
|
6581 | */
|
6582 | var max = _createMaxMin(_gt, -Infinity);
|
6583 |
|
6584 | /**
|
6585 | * Determines the smallest of a list of numbers (or elements that can be cast to numbers)
|
6586 | *
|
6587 | * @func
|
6588 | * @memberOf R
|
6589 | * @category Math
|
6590 | * @sig [Number] -> Number
|
6591 | * @param {Array} list A list of numbers
|
6592 | * @return {Number} The greatest number in the list.
|
6593 | * @see R.minBy
|
6594 | * @example
|
6595 | *
|
6596 | * R.min([7, 3, 9, 2, 4, 9, 3]); //=> 2
|
6597 | */
|
6598 | var min = _createMaxMin(_lt, Infinity);
|
6599 |
|
6600 | /**
|
6601 | * Returns `true` if no elements of the list match the predicate,
|
6602 | * `false` otherwise.
|
6603 | *
|
6604 | * @func
|
6605 | * @memberOf R
|
6606 | * @category List
|
6607 | * @sig (a -> Boolean) -> [a] -> Boolean
|
6608 | * @param {Function} fn The predicate function.
|
6609 | * @param {Array} list The array to consider.
|
6610 | * @return {Boolean} `true` if the predicate is not satisfied by every element, `false` otherwise.
|
6611 | * @example
|
6612 | *
|
6613 | * R.none(R.isNaN, [1, 2, 3]); //=> true
|
6614 | * R.none(R.isNaN, [1, 2, 3, NaN]); //=> false
|
6615 | */
|
6616 | var none = _curry2(_complement(_dispatchable('any', _xany, _any)));
|
6617 |
|
6618 | /**
|
6619 | * A function that returns the first truthy of two arguments otherwise the
|
6620 | * last argument. Note that this is NOT short-circuited, meaning that if
|
6621 | * expressions are passed they are both evaluated.
|
6622 | *
|
6623 | * Dispatches to the `or` method of the first argument if applicable.
|
6624 | *
|
6625 | * @func
|
6626 | * @memberOf R
|
6627 | * @category Logic
|
6628 | * @sig * -> * -> *
|
6629 | * @param {*} a any value
|
6630 | * @param {*} b any other value
|
6631 | * @return {*} the first truthy argument, otherwise the last argument.
|
6632 | * @example
|
6633 | *
|
6634 | * R.or(false, true); //=> true
|
6635 | * R.or(0, []); //=> []
|
6636 | * R.or(null, ''); => ''
|
6637 | */
|
6638 | var or = _curry2(function or(a, b) {
|
6639 | return _hasMethod('or', a) ? a.or(b) : a || b;
|
6640 | });
|
6641 |
|
6642 | /**
|
6643 | * Accepts as its arguments a function and any number of values and returns a function that,
|
6644 | * when invoked, calls the original function with all of the values prepended to the
|
6645 | * original function's arguments list. In some libraries this function is named `applyLeft`.
|
6646 | *
|
6647 | * @func
|
6648 | * @memberOf R
|
6649 | * @category Function
|
6650 | * @sig (a -> b -> ... -> i -> j -> ... -> m -> n) -> a -> b-> ... -> i -> (j -> ... -> m -> n)
|
6651 | * @param {Function} fn The function to invoke.
|
6652 | * @param {...*} [args] Arguments to prepend to `fn` when the returned function is invoked.
|
6653 | * @return {Function} A new function wrapping `fn`. When invoked, it will call `fn`
|
6654 | * with `args` prepended to `fn`'s arguments list.
|
6655 | * @example
|
6656 | *
|
6657 | * var multiply = function(a, b) { return a * b; };
|
6658 | * var double = R.partial(multiply, 2);
|
6659 | * double(2); //=> 4
|
6660 | *
|
6661 | * var greet = function(salutation, title, firstName, lastName) {
|
6662 | * return salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
|
6663 | * };
|
6664 | * var sayHello = R.partial(greet, 'Hello');
|
6665 | * var sayHelloToMs = R.partial(sayHello, 'Ms.');
|
6666 | * sayHelloToMs('Jane', 'Jones'); //=> 'Hello, Ms. Jane Jones!'
|
6667 | */
|
6668 | var partial = curry(_createPartialApplicator(_concat));
|
6669 |
|
6670 | /**
|
6671 | * Accepts as its arguments a function and any number of values and returns a function that,
|
6672 | * when invoked, calls the original function with all of the values appended to the original
|
6673 | * function's arguments list.
|
6674 | *
|
6675 | * Note that `partialRight` is the opposite of `partial`: `partialRight` fills `fn`'s arguments
|
6676 | * from the right to the left. In some libraries this function is named `applyRight`.
|
6677 | *
|
6678 | * @func
|
6679 | * @memberOf R
|
6680 | * @category Function
|
6681 | * @sig (a -> b-> ... -> i -> j -> ... -> m -> n) -> j -> ... -> m -> n -> (a -> b-> ... -> i)
|
6682 | * @param {Function} fn The function to invoke.
|
6683 | * @param {...*} [args] Arguments to append to `fn` when the returned function is invoked.
|
6684 | * @return {Function} A new function wrapping `fn`. When invoked, it will call `fn` with
|
6685 | * `args` appended to `fn`'s arguments list.
|
6686 | * @example
|
6687 | *
|
6688 | * var greet = function(salutation, title, firstName, lastName) {
|
6689 | * return salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!';
|
6690 | * };
|
6691 | * var greetMsJaneJones = R.partialRight(greet, 'Ms.', 'Jane', 'Jones');
|
6692 | *
|
6693 | * greetMsJaneJones('Hello'); //=> 'Hello, Ms. Jane Jones!'
|
6694 | */
|
6695 | var partialRight = curry(_createPartialApplicator(flip(_concat)));
|
6696 |
|
6697 | /**
|
6698 | * Takes a predicate and a list and returns the pair of lists of
|
6699 | * elements which do and do not satisfy the predicate, respectively.
|
6700 | *
|
6701 | * @func
|
6702 | * @memberOf R
|
6703 | * @category List
|
6704 | * @sig (a -> Boolean) -> [a] -> [[a],[a]]
|
6705 | * @param {Function} pred A predicate to determine which array the element belongs to.
|
6706 | * @param {Array} list The array to partition.
|
6707 | * @return {Array} A nested array, containing first an array of elements that satisfied the predicate,
|
6708 | * and second an array of elements that did not satisfy.
|
6709 | * @example
|
6710 | *
|
6711 | * R.partition(R.contains('s'), ['sss', 'ttt', 'foo', 'bars']);
|
6712 | * //=> [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
|
6713 | */
|
6714 | var partition = _curry2(function partition(pred, list) {
|
6715 | return _reduce(function (acc, elt) {
|
6716 | var xs = acc[pred(elt) ? 0 : 1];
|
6717 | xs[xs.length] = elt;
|
6718 | return acc;
|
6719 | }, [
|
6720 | [],
|
6721 | []
|
6722 | ], list);
|
6723 | });
|
6724 |
|
6725 | /**
|
6726 | * Creates a new function that runs each of the functions supplied as parameters in turn,
|
6727 | * passing the return value of each function invocation to the next function invocation,
|
6728 | * beginning with whatever arguments were passed to the initial invocation.
|
6729 | *
|
6730 | * `pipe` is the mirror version of `compose`. `pipe` is left-associative, which means that
|
6731 | * each of the functions provided is executed in order from left to right.
|
6732 | *
|
6733 | * In some libraries this function is named `sequence`.
|
6734 | * @func
|
6735 | * @memberOf R
|
6736 | * @category Function
|
6737 | * @sig ((a... -> b), (b -> c), ..., (x -> y), (y -> z)) -> (a... -> z)
|
6738 | * @param {...Function} functions A variable number of functions.
|
6739 | * @return {Function} A new function which represents the result of calling each of the
|
6740 | * input `functions`, passing the result of each function call to the next, from
|
6741 | * left to right.
|
6742 | * @example
|
6743 | *
|
6744 | * var triple = function(x) { return x * 3; };
|
6745 | * var double = function(x) { return x * 2; };
|
6746 | * var square = function(x) { return x * x; };
|
6747 | * var squareThenDoubleThenTriple = R.pipe(square, double, triple);
|
6748 | *
|
6749 | * //≅ triple(double(square(5)))
|
6750 | * squareThenDoubleThenTriple(5); //=> 150
|
6751 | */
|
6752 | var pipe = function pipe() {
|
6753 | return compose.apply(this, reverse(arguments));
|
6754 | };
|
6755 |
|
6756 | /**
|
6757 | * Creates a new lens that allows getting and setting values of nested properties, by
|
6758 | * following each given lens in succession.
|
6759 | *
|
6760 | * `pipeL` is the mirror version of `composeL`. `pipeL` is left-associative, which means that
|
6761 | * each of the functions provided is executed in order from left to right.
|
6762 | *
|
6763 | * @func
|
6764 | * @memberOf R
|
6765 | * @category Function
|
6766 | * @see R.lens
|
6767 | * @sig ((a -> b), (b -> c), ..., (x -> y), (y -> z)) -> (a -> z)
|
6768 | * @param {...Function} lenses A variable number of lenses.
|
6769 | * @return {Function} A new lens which represents the result of calling each of the
|
6770 | * input `lenses`, passing the result of each getter/setter as the source
|
6771 | * to the next, from right to left.
|
6772 | * @example
|
6773 | *
|
6774 | * var headLens = R.lensIndex(0);
|
6775 | * var secondLens = R.lensIndex(1);
|
6776 | * var xLens = R.lensProp('x');
|
6777 | * var headThenXThenSecondLens = R.pipeL(headLens, xLens, secondLens);
|
6778 | *
|
6779 | * var source = [{x: [0, 1], y: [2, 3]}, {x: [4, 5], y: [6, 7]}];
|
6780 | * headThenXThenSecondLens(source); //=> 1
|
6781 | * headThenXThenSecondLens.set(123, source); //=> [{x: [0, 123], y: [2, 3]}, {x: [4, 5], y: [6, 7]}]
|
6782 | */
|
6783 | var pipeL = compose(apply(composeL), unapply(reverse));
|
6784 |
|
6785 | /**
|
6786 | * Creates a new function that runs each of the functions supplied as parameters in turn,
|
6787 | * passing to the next function invocation either the value returned by the previous
|
6788 | * function or the resolved value if the returned value is a promise. In other words,
|
6789 | * if some of the functions in the sequence return promises, `pipeP` pipes the values
|
6790 | * asynchronously. If none of the functions return promises, the behavior is the same as
|
6791 | * that of `pipe`.
|
6792 | *
|
6793 | * `pipeP` is the mirror version of `composeP`. `pipeP` is left-associative, which means that
|
6794 | * each of the functions provided is executed in order from left to right.
|
6795 | *
|
6796 | * @func
|
6797 | * @memberOf R
|
6798 | * @category Function
|
6799 | * @sig ((a... -> b), (b -> c), ..., (x -> y), (y -> z)) -> (a... -> z)
|
6800 | * @param {...Function} functions A variable number of functions.
|
6801 | * @return {Function} A new function which represents the result of calling each of the
|
6802 | * input `functions`, passing either the returned result or the asynchronously
|
6803 | * resolved value) of each function call to the next, from left to right.
|
6804 | * @example
|
6805 | *
|
6806 | * var Q = require('q');
|
6807 | * var triple = function(x) { return x * 3; };
|
6808 | * var double = function(x) { return x * 2; };
|
6809 | * var squareAsync = function(x) { return Q.when(x * x); };
|
6810 | * var squareAsyncThenDoubleThenTriple = R.pipeP(squareAsync, double, triple);
|
6811 | *
|
6812 | * //≅ squareAsync(5).then(function(x) { return triple(double(x)) };
|
6813 | * squareAsyncThenDoubleThenTriple(5)
|
6814 | * .then(function(result) {
|
6815 | * // result is 150
|
6816 | * });
|
6817 | */
|
6818 | var pipeP = function pipeP() {
|
6819 | return composeP.apply(this, reverse(arguments));
|
6820 | };
|
6821 |
|
6822 | /**
|
6823 | * Returns a single item by iterating through the list, successively calling the iterator
|
6824 | * function and passing it an accumulator value and the current value from the array, and
|
6825 | * then passing the result to the next call.
|
6826 | *
|
6827 | * The iterator function receives two values: *(acc, value)*
|
6828 | *
|
6829 | * Note: `R.reduce` does not skip deleted or unassigned indices (sparse arrays), unlike
|
6830 | * the native `Array.prototype.reduce` method. For more details on this behavior, see:
|
6831 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
|
6832 | *
|
6833 | * @func
|
6834 | * @memberOf R
|
6835 | * @category List
|
6836 | * @sig (a,b -> a) -> a -> [b] -> a
|
6837 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
6838 | * current element from the array.
|
6839 | * @param {*} acc The accumulator value.
|
6840 | * @param {Array} list The list to iterate over.
|
6841 | * @return {*} The final, accumulated value.
|
6842 | * @example
|
6843 | *
|
6844 | * var numbers = [1, 2, 3];
|
6845 | * var add = function(a, b) {
|
6846 | * return a + b;
|
6847 | * };
|
6848 | *
|
6849 | * R.reduce(add, 10, numbers); //=> 16
|
6850 | */
|
6851 | var reduce = _curry3(_reduce);
|
6852 |
|
6853 | /**
|
6854 | * Similar to `filter`, except that it keeps only values for which the given predicate
|
6855 | * function returns falsy. The predicate function is passed one argument: *(value)*.
|
6856 | *
|
6857 | * Acts as a transducer if a transformer is given in list position.
|
6858 | * @see R.transduce
|
6859 | *
|
6860 | * @func
|
6861 | * @memberOf R
|
6862 | * @category List
|
6863 | * @sig (a -> Boolean) -> [a] -> [a]
|
6864 | * @param {Function} fn The function called per iteration.
|
6865 | * @param {Array} list The collection to iterate over.
|
6866 | * @return {Array} The new filtered array.
|
6867 | * @example
|
6868 | *
|
6869 | * var isOdd = function(n) {
|
6870 | * return n % 2 === 1;
|
6871 | * };
|
6872 | * R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
|
6873 | */
|
6874 | var reject = _curry2(function reject(fn, list) {
|
6875 | return filter(_complement(fn), list);
|
6876 | });
|
6877 |
|
6878 | /**
|
6879 | * Returns a fixed list of size `n` containing a specified identical value.
|
6880 | *
|
6881 | * @func
|
6882 | * @memberOf R
|
6883 | * @category List
|
6884 | * @sig a -> n -> [a]
|
6885 | * @param {*} value The value to repeat.
|
6886 | * @param {Number} n The desired size of the output list.
|
6887 | * @return {Array} A new array containing `n` `value`s.
|
6888 | * @example
|
6889 | *
|
6890 | * R.repeat('hi', 5); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
|
6891 | *
|
6892 | * var obj = {};
|
6893 | * var repeatedObjs = R.repeat(obj, 5); //=> [{}, {}, {}, {}, {}]
|
6894 | * repeatedObjs[0] === repeatedObjs[1]; //=> true
|
6895 | */
|
6896 | var repeat = _curry2(function repeat(value, n) {
|
6897 | return times(always(value), n);
|
6898 | });
|
6899 |
|
6900 | /**
|
6901 | * Returns a list containing the elements of `xs` from `fromIndex` (inclusive)
|
6902 | * to `toIndex` (exclusive).
|
6903 | *
|
6904 | * @func
|
6905 | * @memberOf R
|
6906 | * @category List
|
6907 | * @sig Number -> Number -> [a] -> [a]
|
6908 | * @param {Number} fromIndex The start index (inclusive).
|
6909 | * @param {Number} toIndex The end index (exclusive).
|
6910 | * @param {Array} xs The list to take elements from.
|
6911 | * @return {Array} The slice of `xs` from `fromIndex` to `toIndex`.
|
6912 | * @example
|
6913 | *
|
6914 | * var xs = R.range(0, 10);
|
6915 | * R.slice(2, 5)(xs); //=> [2, 3, 4]
|
6916 | */
|
6917 | var slice = _curry3(_checkForMethod('slice', function slice(fromIndex, toIndex, xs) {
|
6918 | return Array.prototype.slice.call(xs, fromIndex, toIndex);
|
6919 | }));
|
6920 |
|
6921 | /**
|
6922 | * Splits a string into an array of strings based on the given
|
6923 | * separator.
|
6924 | *
|
6925 | * @func
|
6926 | * @memberOf R
|
6927 | * @category String
|
6928 | * @sig String -> String -> [String]
|
6929 | * @param {String} sep The separator string.
|
6930 | * @param {String} str The string to separate into an array.
|
6931 | * @return {Array} The array of strings from `str` separated by `str`.
|
6932 | * @example
|
6933 | *
|
6934 | * var pathComponents = R.split('/');
|
6935 | * R.tail(pathComponents('/usr/local/bin/node')); //=> ['usr', 'local', 'bin', 'node']
|
6936 | *
|
6937 | * R.split('.', 'a.b.c.xyz.d'); //=> ['a', 'b', 'c', 'xyz', 'd']
|
6938 | */
|
6939 | var split = invoker(1, 'split');
|
6940 |
|
6941 | /**
|
6942 | * Returns a string containing the characters of `str` from `fromIndex`
|
6943 | * (inclusive) to `toIndex` (exclusive).
|
6944 | *
|
6945 | * @func
|
6946 | * @memberOf R
|
6947 | * @category String
|
6948 | * @sig Number -> Number -> String -> String
|
6949 | * @param {Number} fromIndex The start index (inclusive).
|
6950 | * @param {Number} toIndex The end index (exclusive).
|
6951 | * @param {String} str The string to slice.
|
6952 | * @return {String}
|
6953 | * @see R.slice
|
6954 | * @example
|
6955 | *
|
6956 | * R.substring(2, 5, 'abcdefghijklm'); //=> 'cde'
|
6957 | */
|
6958 | var substring = slice;
|
6959 |
|
6960 | /**
|
6961 | * Returns a string containing the characters of `str` from `fromIndex`
|
6962 | * (inclusive) to the end of `str`.
|
6963 | *
|
6964 | * @func
|
6965 | * @memberOf R
|
6966 | * @category String
|
6967 | * @sig Number -> String -> String
|
6968 | * @param {Number} fromIndex
|
6969 | * @param {String} str
|
6970 | * @return {String}
|
6971 | * @example
|
6972 | *
|
6973 | * R.substringFrom(3, 'Ramda'); //=> 'da'
|
6974 | * R.substringFrom(-2, 'Ramda'); //=> 'da'
|
6975 | */
|
6976 | var substringFrom = substring(__, Infinity);
|
6977 |
|
6978 | /**
|
6979 | * Returns a string containing the first `toIndex` characters of `str`.
|
6980 | *
|
6981 | * @func
|
6982 | * @memberOf R
|
6983 | * @category String
|
6984 | * @sig Number -> String -> String
|
6985 | * @param {Number} toIndex
|
6986 | * @param {String} str
|
6987 | * @return {String}
|
6988 | * @example
|
6989 | *
|
6990 | * R.substringTo(3, 'Ramda'); //=> 'Ram'
|
6991 | * R.substringTo(-2, 'Ramda'); //=> 'Ram'
|
6992 | */
|
6993 | var substringTo = substring(0);
|
6994 |
|
6995 | /**
|
6996 | * Adds together all the elements of a list.
|
6997 | *
|
6998 | * @func
|
6999 | * @memberOf R
|
7000 | * @category Math
|
7001 | * @sig [Number] -> Number
|
7002 | * @param {Array} list An array of numbers
|
7003 | * @return {Number} The sum of all the numbers in the list.
|
7004 | * @see reduce
|
7005 | * @example
|
7006 | *
|
7007 | * R.sum([2,4,6,8,100,1]); //=> 121
|
7008 | */
|
7009 | var sum = reduce(_add, 0);
|
7010 |
|
7011 | /**
|
7012 | * Returns all but the first element of a list. If the list provided has the `tail` method,
|
7013 | * it will instead return `list.tail()`.
|
7014 | *
|
7015 | * @func
|
7016 | * @memberOf R
|
7017 | * @category List
|
7018 | * @sig [a] -> [a]
|
7019 | * @param {Array} list The array to consider.
|
7020 | * @return {Array} A new array containing all but the first element of the input list, or an
|
7021 | * empty list if the input list is empty.
|
7022 | * @example
|
7023 | *
|
7024 | * R.tail(['fi', 'fo', 'fum']); //=> ['fo', 'fum']
|
7025 | */
|
7026 | var tail = _checkForMethod('tail', function (list) {
|
7027 | return _slice(list, 1);
|
7028 | });
|
7029 |
|
7030 | /**
|
7031 | * Returns a new list containing the first `n` elements of the given list. If
|
7032 | * `n > * list.length`, returns a list of `list.length` elements.
|
7033 | *
|
7034 | * Acts as a transducer if a transformer is given in list position.
|
7035 | * @see R.transduce
|
7036 | *
|
7037 | * @func
|
7038 | * @memberOf R
|
7039 | * @category List
|
7040 | * @sig Number -> [a] -> [a]
|
7041 | * @param {Number} n The number of elements to return.
|
7042 | * @param {Array} list The array to query.
|
7043 | * @return {Array} A new array containing the first elements of `list`.
|
7044 | * @example
|
7045 | *
|
7046 | * R.take(3,[1,2,3,4,5]); //=> [1,2,3]
|
7047 | *
|
7048 | * var members= [ "Paul Desmond","Bob Bates","Joe Dodge","Ron Crotty","Lloyd Davis","Joe Morello","Norman Bates",
|
7049 | * "Eugene Wright","Gerry Mulligan","Jack Six","Alan Dawson","Darius Brubeck","Chris Brubeck",
|
7050 | * "Dan Brubeck","Bobby Militello","Michael Moore","Randy Jones"];
|
7051 | * var takeFive = R.take(5);
|
7052 | * takeFive(members); //=> ["Paul Desmond","Bob Bates","Joe Dodge","Ron Crotty","Lloyd Davis"]
|
7053 | */
|
7054 | var take = _curry2(_dispatchable('take', _xtake, function take(n, list) {
|
7055 | return _slice(list, 0, n);
|
7056 | }));
|
7057 |
|
7058 | /**
|
7059 | * Returns a new list containing the first `n` elements of a given list, passing each value
|
7060 | * to the supplied predicate function, and terminating when the predicate function returns
|
7061 | * `false`. Excludes the element that caused the predicate function to fail. The predicate
|
7062 | * function is passed one argument: *(value)*.
|
7063 | *
|
7064 | * Acts as a transducer if a transformer is given in list position.
|
7065 | * @see R.transduce
|
7066 | *
|
7067 | * @func
|
7068 | * @memberOf R
|
7069 | * @category List
|
7070 | * @sig (a -> Boolean) -> [a] -> [a]
|
7071 | * @param {Function} fn The function called per iteration.
|
7072 | * @param {Array} list The collection to iterate over.
|
7073 | * @return {Array} A new array.
|
7074 | * @example
|
7075 | *
|
7076 | * var isNotFour = function(x) {
|
7077 | * return !(x === 4);
|
7078 | * };
|
7079 | *
|
7080 | * R.takeWhile(isNotFour, [1, 2, 3, 4]); //=> [1, 2, 3]
|
7081 | */
|
7082 | var takeWhile = _curry2(_dispatchable('takeWhile', _xtakeWhile, function takeWhile(fn, list) {
|
7083 | var idx = -1, len = list.length;
|
7084 | while (++idx < len && fn(list[idx])) {
|
7085 | }
|
7086 | return _slice(list, 0, idx);
|
7087 | }));
|
7088 |
|
7089 | /**
|
7090 | * The lower case version of a string.
|
7091 | *
|
7092 | * @func
|
7093 | * @memberOf R
|
7094 | * @category String
|
7095 | * @sig String -> String
|
7096 | * @param {String} str The string to lower case.
|
7097 | * @return {String} The lower case version of `str`.
|
7098 | * @example
|
7099 | *
|
7100 | * R.toLower('XYZ'); //=> 'xyz'
|
7101 | */
|
7102 | var toLower = invoker(0, 'toLowerCase');
|
7103 |
|
7104 | /**
|
7105 | * The upper case version of a string.
|
7106 | *
|
7107 | * @func
|
7108 | * @memberOf R
|
7109 | * @category String
|
7110 | * @sig String -> String
|
7111 | * @param {String} str The string to upper case.
|
7112 | * @return {String} The upper case version of `str`.
|
7113 | * @example
|
7114 | *
|
7115 | * R.toUpper('abc'); //=> 'ABC'
|
7116 | */
|
7117 | var toUpper = invoker(0, 'toUpperCase');
|
7118 |
|
7119 | /**
|
7120 | * Initializes a transducer using supplied iterator function. Returns a single item by
|
7121 | * iterating through the list, successively calling the transformed iterator function and
|
7122 | * passing it an accumulator value and the current value from the array, and then passing
|
7123 | * the result to the next call.
|
7124 | *
|
7125 | * The iterator function receives two values: *(acc, value)*. It will be wrapped as a
|
7126 | * transformer to initialize the transducer. A transformer can be passed directly in place
|
7127 | * of an iterator function.
|
7128 | *
|
7129 | * A transducer is a function that accepts a transformer and returns a transformer and can
|
7130 | * be composed directly.
|
7131 | *
|
7132 | * A transformer is an an object that provides a 2-arity reducing iterator function, step,
|
7133 | * 0-arity initial value function, init, and 1-arity result extraction function, result.
|
7134 | * The step function is used as the iterator function in reduce. The result function is used
|
7135 | * to convert the final accumulator into the return type and in most cases is R.identity.
|
7136 | * The init function can be used to provide an initial accumulator, but is ignored by transduce.
|
7137 | *
|
7138 | * The iteration is performed with R.reduce after initializing the transducer.
|
7139 | *
|
7140 | * @func
|
7141 | * @memberOf R
|
7142 | * @category List
|
7143 | * @sig (c -> c) -> (a,b -> a) -> a -> [b] -> a
|
7144 | * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
|
7145 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
7146 | * current element from the array. Wrapped as transformer, if necessary, and used to
|
7147 | * initialize the transducer
|
7148 | * @param {*} acc The initial accumulator value.
|
7149 | * @param {Array} list The list to iterate over.
|
7150 | * @see R.into
|
7151 | * @return {*} The final, accumulated value.
|
7152 | * @example
|
7153 | *
|
7154 | * var numbers = [1, 2, 3, 4];
|
7155 | * var transducer = R.compose(R.map(R.add(1)), R.take(2));
|
7156 | *
|
7157 | * R.transduce(transducer, R.flip(R.append), [], numbers); //=> [2, 3]
|
7158 | */
|
7159 | var transduce = curryN(4, function (xf, fn, acc, list) {
|
7160 | return _reduce(xf(typeof fn === 'function' ? _xwrap(fn) : fn), acc, list);
|
7161 | });
|
7162 |
|
7163 | /**
|
7164 | * Combines two lists into a set (i.e. no duplicates) composed of the elements of each list. Duplication is
|
7165 | * determined according to the value returned by applying the supplied predicate to two list elements.
|
7166 | *
|
7167 | * @func
|
7168 | * @memberOf R
|
7169 | * @category Relation
|
7170 | * @sig (a,a -> Boolean) -> [a] -> [a] -> [a]
|
7171 | * @param {Function} pred A predicate used to test whether two items are equal.
|
7172 | * @param {Array} list1 The first list.
|
7173 | * @param {Array} list2 The second list.
|
7174 | * @return {Array} The first and second lists concatenated, with
|
7175 | * duplicates removed.
|
7176 | * @see R.union
|
7177 | * @example
|
7178 | *
|
7179 | * function cmp(x, y) { return x.a === y.a; }
|
7180 | * var l1 = [{a: 1}, {a: 2}];
|
7181 | * var l2 = [{a: 1}, {a: 4}];
|
7182 | * R.unionWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}, {a: 4}]
|
7183 | */
|
7184 | var unionWith = _curry3(function unionWith(pred, list1, list2) {
|
7185 | return uniqWith(pred, _concat(list1, list2));
|
7186 | });
|
7187 |
|
7188 | /**
|
7189 | * Returns a new list containing only one copy of each element in the original list.
|
7190 | * Equality is strict here, meaning reference equality for objects and non-coercing equality
|
7191 | * for primitives.
|
7192 | *
|
7193 | * @func
|
7194 | * @memberOf R
|
7195 | * @category List
|
7196 | * @sig [a] -> [a]
|
7197 | * @param {Array} list The array to consider.
|
7198 | * @return {Array} The list of unique items.
|
7199 | * @example
|
7200 | *
|
7201 | * R.uniq([1, 1, 2, 1]); //=> [1, 2]
|
7202 | * R.uniq([{}, {}]); //=> [{}, {}]
|
7203 | * R.uniq([1, '1']); //=> [1, '1']
|
7204 | */
|
7205 | var uniq = uniqWith(eq);
|
7206 |
|
7207 | /**
|
7208 | * Returns a new list by pulling every item at the first level of nesting out, and putting
|
7209 | * them in a new array.
|
7210 | *
|
7211 | * @func
|
7212 | * @memberOf R
|
7213 | * @category List
|
7214 | * @sig [a] -> [b]
|
7215 | * @param {Array} list The array to consider.
|
7216 | * @return {Array} The flattened list.
|
7217 | * @example
|
7218 | *
|
7219 | * R.unnest([1, [2], [[3]]]); //=> [1, 2, [3]]
|
7220 | * R.unnest([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6]
|
7221 | */
|
7222 | var unnest = _curry1(_makeFlat(false));
|
7223 |
|
7224 | /**
|
7225 | * Accepts a function `fn` and any number of transformer functions and returns a new
|
7226 | * function. When the new function is invoked, it calls the function `fn` with parameters
|
7227 | * consisting of the result of calling each supplied handler on successive arguments to the
|
7228 | * new function.
|
7229 | *
|
7230 | * If more arguments are passed to the returned function than transformer functions, those
|
7231 | * arguments are passed directly to `fn` as additional parameters. If you expect additional
|
7232 | * arguments that don't need to be transformed, although you can ignore them, it's best to
|
7233 | * pass an identity function so that the new function reports the correct arity.
|
7234 | *
|
7235 | * @func
|
7236 | * @memberOf R
|
7237 | * @category Function
|
7238 | * @sig (x1 -> x2 -> ... -> z) -> ((a -> x1), (b -> x2), ...) -> (a -> b -> ... -> z)
|
7239 | * @param {Function} fn The function to wrap.
|
7240 | * @param {...Function} transformers A variable number of transformer functions
|
7241 | * @return {Function} The wrapped function.
|
7242 | * @example
|
7243 | *
|
7244 | * // Example 1:
|
7245 | *
|
7246 | * // Number -> [Person] -> [Person]
|
7247 | * var byAge = R.useWith(R.filter, R.propEq('age'), R.identity);
|
7248 | *
|
7249 | * var kids = [
|
7250 | * {name: 'Abbie', age: 6},
|
7251 | * {name: 'Brian', age: 5},
|
7252 | * {name: 'Chris', age: 6},
|
7253 | * {name: 'David', age: 4},
|
7254 | * {name: 'Ellie', age: 5}
|
7255 | * ];
|
7256 | *
|
7257 | * byAge(5, kids); //=> [{name: 'Brian', age: 5}, {name: 'Ellie', age: 5}]
|
7258 | *
|
7259 | * // Example 2:
|
7260 | *
|
7261 | * var double = function(y) { return y * 2; };
|
7262 | * var square = function(x) { return x * x; };
|
7263 | * var add = function(a, b) { return a + b; };
|
7264 | * // Adds any number of arguments together
|
7265 | * var addAll = function() {
|
7266 | * return R.reduce(add, 0, arguments);
|
7267 | * };
|
7268 | *
|
7269 | * // Basic example
|
7270 | * var addDoubleAndSquare = R.useWith(addAll, double, square);
|
7271 | *
|
7272 | * //≅ addAll(double(10), square(5));
|
7273 | * addDoubleAndSquare(10, 5); //=> 45
|
7274 | *
|
7275 | * // Example of passing more arguments than transformers
|
7276 | * //≅ addAll(double(10), square(5), 100);
|
7277 | * addDoubleAndSquare(10, 5, 100); //=> 145
|
7278 | *
|
7279 | * // If there are extra _expected_ arguments that don't need to be transformed, although
|
7280 | * // you can ignore them, it might be best to pass in the identity function so that the new
|
7281 | * // function correctly reports arity.
|
7282 | * var addDoubleAndSquareWithExtraParams = R.useWith(addAll, double, square, R.identity);
|
7283 | * // addDoubleAndSquareWithExtraParams.length //=> 3
|
7284 | * //≅ addAll(double(10), square(5), R.identity(100));
|
7285 | * addDoubleAndSquare(10, 5, 100); //=> 145
|
7286 | */
|
7287 | /*, transformers */
|
7288 | var useWith = curry(function useWith(fn) {
|
7289 | var transformers = _slice(arguments, 1);
|
7290 | var tlen = transformers.length;
|
7291 | return curry(arity(tlen, function () {
|
7292 | var args = [], idx = -1;
|
7293 | while (++idx < tlen) {
|
7294 | args[idx] = transformers[idx](arguments[idx]);
|
7295 | }
|
7296 | return fn.apply(this, args.concat(_slice(arguments, tlen)));
|
7297 | }));
|
7298 | });
|
7299 |
|
7300 | /**
|
7301 | * Returns a list of all the enumerable own properties of the supplied object.
|
7302 | * Note that the order of the output array is not guaranteed across
|
7303 | * different JS platforms.
|
7304 | *
|
7305 | * @func
|
7306 | * @memberOf R
|
7307 | * @category Object
|
7308 | * @sig {k: v} -> [v]
|
7309 | * @param {Object} obj The object to extract values from
|
7310 | * @return {Array} An array of the values of the object's own properties.
|
7311 | * @example
|
7312 | *
|
7313 | * R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3]
|
7314 | */
|
7315 | var values = _curry1(function values(obj) {
|
7316 | var props = keys(obj);
|
7317 | var len = props.length;
|
7318 | var vals = [];
|
7319 | var idx = -1;
|
7320 | while (++idx < len) {
|
7321 | vals[idx] = obj[props[idx]];
|
7322 | }
|
7323 | return vals;
|
7324 | });
|
7325 |
|
7326 | /**
|
7327 | * Takes a spec object and a test object; returns true if the test satisfies
|
7328 | * the spec, false otherwise. An object satisfies the spec if, for each of the
|
7329 | * spec's own properties, accessing that property of the object gives the same
|
7330 | * value (in `R.eq` terms) as accessing that property of the spec.
|
7331 | *
|
7332 | * `whereEq` is a specialization of [`where`](#where).
|
7333 | *
|
7334 | * @func
|
7335 | * @memberOf R
|
7336 | * @category Object
|
7337 | * @sig {String: *} -> {String: *} -> Boolean
|
7338 | * @param {Object} spec
|
7339 | * @param {Object} testObj
|
7340 | * @return {Boolean}
|
7341 | * @see R.where
|
7342 | * @example
|
7343 | *
|
7344 | * // pred :: Object -> Boolean
|
7345 | * var pred = R.where({a: 1, b: 2});
|
7346 | *
|
7347 | * pred({a: 1}); //=> false
|
7348 | * pred({a: 1, b: 2}); //=> true
|
7349 | * pred({a: 1, b: 2, c: 3}); //=> true
|
7350 | * pred({a: 1, b: 1}); //=> false
|
7351 | */
|
7352 | var whereEq = _curry2(function whereEq(spec, testObj) {
|
7353 | return where(mapObj(eq, spec), testObj);
|
7354 | });
|
7355 |
|
7356 | // The algorithm used to handle cyclic structures is
|
7357 | // inspired by underscore's isEqual
|
7358 | // RegExp equality algorithm: http://stackoverflow.com/a/10776635
|
7359 | var _eqDeep = function _eqDeep(a, b, stackA, stackB) {
|
7360 | var typeA = type(a);
|
7361 | if (typeA !== type(b)) {
|
7362 | return false;
|
7363 | }
|
7364 | if (eq(a, b)) {
|
7365 | return true;
|
7366 | }
|
7367 | if (typeA == 'RegExp') {
|
7368 | // RegExp equality algorithm: http://stackoverflow.com/a/10776635
|
7369 | return a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline && a.sticky === b.sticky && a.unicode === b.unicode;
|
7370 | }
|
7371 | if (Object(a) === a) {
|
7372 | if (typeA === 'Date' && a.getTime() != b.getTime()) {
|
7373 | return false;
|
7374 | }
|
7375 | var keysA = keys(a);
|
7376 | if (keysA.length !== keys(b).length) {
|
7377 | return false;
|
7378 | }
|
7379 | var idx = stackA.length;
|
7380 | while (--idx >= 0) {
|
7381 | if (stackA[idx] === a) {
|
7382 | return stackB[idx] === b;
|
7383 | }
|
7384 | }
|
7385 | stackA[stackA.length] = a;
|
7386 | stackB[stackB.length] = b;
|
7387 | idx = keysA.length;
|
7388 | while (--idx >= 0) {
|
7389 | var key = keysA[idx];
|
7390 | if (!_has(key, b) || !_eqDeep(b[key], a[key], stackA, stackB)) {
|
7391 | return false;
|
7392 | }
|
7393 | }
|
7394 | stackA.pop();
|
7395 | stackB.pop();
|
7396 | return true;
|
7397 | }
|
7398 | return false;
|
7399 | };
|
7400 |
|
7401 | /**
|
7402 | * Assigns own enumerable properties of the other object to the destination
|
7403 | * object preferring items in other.
|
7404 | *
|
7405 | * @private
|
7406 | * @memberOf R
|
7407 | * @category Object
|
7408 | * @param {Object} destination The destination object.
|
7409 | * @param {Object} other The other object to merge with destination.
|
7410 | * @return {Object} The destination object.
|
7411 | * @example
|
7412 | *
|
7413 | * _extend({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
|
7414 | * //=> { 'name': 'fred', 'age': 40 }
|
7415 | */
|
7416 | var _extend = function _extend(destination, other) {
|
7417 | var props = keys(other);
|
7418 | var idx = -1, length = props.length;
|
7419 | while (++idx < length) {
|
7420 | destination[props[idx]] = other[props[idx]];
|
7421 | }
|
7422 | return destination;
|
7423 | };
|
7424 |
|
7425 | var _pluck = function _pluck(p, list) {
|
7426 | return map(prop(p), list);
|
7427 | };
|
7428 |
|
7429 | /**
|
7430 | * Create a predicate wrapper which will call a pick function (all/any) for each predicate
|
7431 | *
|
7432 | * @private
|
7433 | * @see R.all
|
7434 | * @see R.any
|
7435 | */
|
7436 | // Call function immediately if given arguments
|
7437 | // Return a function which will call the predicates with the provided arguments
|
7438 | var _predicateWrap = function _predicateWrap(predPicker) {
|
7439 | return function (preds) {
|
7440 | var predIterator = function () {
|
7441 | var args = arguments;
|
7442 | return predPicker(function (predicate) {
|
7443 | return predicate.apply(null, args);
|
7444 | }, preds);
|
7445 | };
|
7446 | return arguments.length > 1 ? // Call function immediately if given arguments
|
7447 | predIterator.apply(null, _slice(arguments, 1)) : // Return a function which will call the predicates with the provided arguments
|
7448 | arity(max(_pluck('length', preds)), predIterator);
|
7449 | };
|
7450 | };
|
7451 |
|
7452 | // Function, RegExp, user-defined types
|
7453 | var _toString = function _toString(x, seen) {
|
7454 | var recur = function recur(y) {
|
7455 | var xs = seen.concat([x]);
|
7456 | return _indexOf(xs, y) >= 0 ? '<Circular>' : _toString(y, xs);
|
7457 | };
|
7458 | switch (Object.prototype.toString.call(x)) {
|
7459 | case '[object Arguments]':
|
7460 | return '(function() { return arguments; }(' + _map(recur, x).join(', ') + '))';
|
7461 | case '[object Array]':
|
7462 | return '[' + _map(recur, x).join(', ') + ']';
|
7463 | case '[object Boolean]':
|
7464 | return typeof x === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString();
|
7465 | case '[object Date]':
|
7466 | return 'new Date(' + _quote(_toISOString(x)) + ')';
|
7467 | case '[object Null]':
|
7468 | return 'null';
|
7469 | case '[object Number]':
|
7470 | return typeof x === 'object' ? 'new Number(' + recur(x.valueOf()) + ')' : 1 / x === -Infinity ? '-0' : x.toString(10);
|
7471 | case '[object String]':
|
7472 | return typeof x === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : _quote(x);
|
7473 | case '[object Undefined]':
|
7474 | return 'undefined';
|
7475 | default:
|
7476 | return typeof x.constructor === 'function' && x.constructor.name !== 'Object' && typeof x.toString === 'function' && x.toString() !== '[object Object]' ? x.toString() : // Function, RegExp, user-defined types
|
7477 | '{' + _map(function (k) {
|
7478 | return _quote(k) + ': ' + recur(x[k]);
|
7479 | }, keys(x).sort()).join(', ') + '}';
|
7480 | }
|
7481 | };
|
7482 |
|
7483 | /**
|
7484 | * Given a list of predicates, returns a new predicate that will be true exactly when all of them are.
|
7485 | *
|
7486 | * @func
|
7487 | * @memberOf R
|
7488 | * @category Logic
|
7489 | * @sig [(*... -> Boolean)] -> (*... -> Boolean)
|
7490 | * @param {Array} list An array of predicate functions
|
7491 | * @param {*} optional Any arguments to pass into the predicates
|
7492 | * @return {Function} a function that applies its arguments to each of
|
7493 | * the predicates, returning `true` if all are satisfied.
|
7494 | * @example
|
7495 | *
|
7496 | * var gt10 = function(x) { return x > 10; };
|
7497 | * var even = function(x) { return x % 2 === 0};
|
7498 | * var f = R.allPass([gt10, even]);
|
7499 | * f(11); //=> false
|
7500 | * f(12); //=> true
|
7501 | */
|
7502 | var allPass = curry(_predicateWrap(_all));
|
7503 |
|
7504 | /**
|
7505 | * Given a list of predicates returns a new predicate that will be true exactly when any one of them is.
|
7506 | *
|
7507 | * @func
|
7508 | * @memberOf R
|
7509 | * @category Logic
|
7510 | * @sig [(*... -> Boolean)] -> (*... -> Boolean)
|
7511 | * @param {Array} list An array of predicate functions
|
7512 | * @param {*} optional Any arguments to pass into the predicates
|
7513 | * @return {Function} A function that applies its arguments to each of the predicates, returning
|
7514 | * `true` if all are satisfied.
|
7515 | * @example
|
7516 | *
|
7517 | * var gt10 = function(x) { return x > 10; };
|
7518 | * var even = function(x) { return x % 2 === 0};
|
7519 | * var f = R.anyPass([gt10, even]);
|
7520 | * f(11); //=> true
|
7521 | * f(8); //=> true
|
7522 | * f(9); //=> false
|
7523 | */
|
7524 | var anyPass = curry(_predicateWrap(_any));
|
7525 |
|
7526 | /**
|
7527 | * ap applies a list of functions to a list of values.
|
7528 | *
|
7529 | * @func
|
7530 | * @memberOf R
|
7531 | * @category Function
|
7532 | * @sig [f] -> [a] -> [f a]
|
7533 | * @param {Array} fns An array of functions
|
7534 | * @param {Array} vs An array of values
|
7535 | * @return {Array} An array of results of applying each of `fns` to all of `vs` in turn.
|
7536 | * @example
|
7537 | *
|
7538 | * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
|
7539 | */
|
7540 | var ap = _curry2(function ap(fns, vs) {
|
7541 | return _hasMethod('ap', fns) ? fns.ap(vs) : _reduce(function (acc, fn) {
|
7542 | return _concat(acc, map(fn, vs));
|
7543 | }, [], fns);
|
7544 | });
|
7545 |
|
7546 | /**
|
7547 | * Returns the result of calling its first argument with the remaining
|
7548 | * arguments. This is occasionally useful as a converging function for
|
7549 | * `R.converge`: the left branch can produce a function while the right
|
7550 | * branch produces a value to be passed to that function as an argument.
|
7551 | *
|
7552 | * @func
|
7553 | * @memberOf R
|
7554 | * @category Function
|
7555 | * @sig (*... -> a),*... -> a
|
7556 | * @param {Function} fn The function to apply to the remaining arguments.
|
7557 | * @param {...*} args Any number of positional arguments.
|
7558 | * @return {*}
|
7559 | * @example
|
7560 | *
|
7561 | * var indentN = R.pipe(R.times(R.always(' ')),
|
7562 | * R.join(''),
|
7563 | * R.replace(/^(?!$)/gm));
|
7564 | *
|
7565 | * var format = R.converge(R.call,
|
7566 | * R.pipe(R.prop('indent'), indentN),
|
7567 | * R.prop('value'));
|
7568 | *
|
7569 | * format({indent: 2, value: 'foo\nbar\nbaz\n'}); //=> ' foo\n bar\n baz\n'
|
7570 | */
|
7571 | var call = curry(function call(fn) {
|
7572 | return fn.apply(this, _slice(arguments, 1));
|
7573 | });
|
7574 |
|
7575 | /**
|
7576 | * `chain` maps a function over a list and concatenates the results.
|
7577 | * This implementation is compatible with the
|
7578 | * Fantasy-land Chain spec, and will work with types that implement that spec.
|
7579 | * `chain` is also known as `flatMap` in some libraries
|
7580 | *
|
7581 | * @func
|
7582 | * @memberOf R
|
7583 | * @category List
|
7584 | * @sig (a -> [b]) -> [a] -> [b]
|
7585 | * @param {Function} fn
|
7586 | * @param {Array} list
|
7587 | * @return {Array}
|
7588 | * @example
|
7589 | *
|
7590 | * var duplicate = function(n) {
|
7591 | * return [n, n];
|
7592 | * };
|
7593 | * R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3]
|
7594 | */
|
7595 | var chain = _curry2(_checkForMethod('chain', function chain(f, list) {
|
7596 | return unnest(_map(f, list));
|
7597 | }));
|
7598 |
|
7599 | /**
|
7600 | * Turns a list of Functors into a Functor of a list, applying
|
7601 | * a mapping function to the elements of the list along the way.
|
7602 | *
|
7603 | * Note: `commuteMap` may be more useful to convert a list of non-Array Functors (e.g.
|
7604 | * Maybe, Either, etc.) to Functor of a list.
|
7605 | *
|
7606 | * @func
|
7607 | * @memberOf R
|
7608 | * @category List
|
7609 | * @see R.commute
|
7610 | * @sig (a -> (b -> c)) -> (x -> [x]) -> [[*]...]
|
7611 | * @param {Function} fn The transformation function
|
7612 | * @param {Function} of A function that returns the data type to return
|
7613 | * @param {Array} list An Array (or other Functor) of Arrays (or other Functors)
|
7614 | * @return {Array}
|
7615 | * @example
|
7616 | *
|
7617 | * var plus10map = R.map(function(x) { return x + 10; });
|
7618 | * var as = [[1], [3, 4]];
|
7619 | * R.commuteMap(R.map(function(x) { return x + 10; }), R.of, as); //=> [[11, 13], [11, 14]]
|
7620 | *
|
7621 | * var bs = [[1, 2], [3]];
|
7622 | * R.commuteMap(plus10map, R.of, bs); //=> [[11, 13], [12, 13]]
|
7623 | *
|
7624 | * var cs = [[1, 2], [3, 4]];
|
7625 | * R.commuteMap(plus10map, R.of, cs); //=> [[11, 13], [12, 13], [11, 14], [12, 14]]
|
7626 | */
|
7627 | var commuteMap = _curry3(function commuteMap(fn, of, list) {
|
7628 | function consF(acc, ftor) {
|
7629 | return ap(map(append, fn(ftor)), acc);
|
7630 | }
|
7631 | return _reduce(consF, of([]), list);
|
7632 | });
|
7633 |
|
7634 | /**
|
7635 | * Wraps a constructor function inside a curried function that can be called with the same
|
7636 | * arguments and returns the same type. The arity of the function returned is specified
|
7637 | * to allow using variadic constructor functions.
|
7638 | *
|
7639 | * @func
|
7640 | * @memberOf R
|
7641 | * @category Function
|
7642 | * @sig Number -> (* -> {*}) -> (* -> {*})
|
7643 | * @param {Number} n The arity of the constructor function.
|
7644 | * @param {Function} Fn The constructor function to wrap.
|
7645 | * @return {Function} A wrapped, curried constructor function.
|
7646 | * @example
|
7647 | *
|
7648 | * // Variadic constructor function
|
7649 | * var Widget = function() {
|
7650 | * this.children = Array.prototype.slice.call(arguments);
|
7651 | * // ...
|
7652 | * };
|
7653 | * Widget.prototype = {
|
7654 | * // ...
|
7655 | * };
|
7656 | * var allConfigs = {
|
7657 | * // ...
|
7658 | * };
|
7659 | * R.map(R.constructN(1, Widget), allConfigs); // a list of Widgets
|
7660 | */
|
7661 | var constructN = _curry2(function constructN(n, Fn) {
|
7662 | if (n > 10) {
|
7663 | throw new Error('Constructor with greater than ten arguments');
|
7664 | }
|
7665 | if (n === 0) {
|
7666 | return function () {
|
7667 | return new Fn();
|
7668 | };
|
7669 | }
|
7670 | return curry(nAry(n, function ($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
|
7671 | switch (arguments.length) {
|
7672 | case 1:
|
7673 | return new Fn($0);
|
7674 | case 2:
|
7675 | return new Fn($0, $1);
|
7676 | case 3:
|
7677 | return new Fn($0, $1, $2);
|
7678 | case 4:
|
7679 | return new Fn($0, $1, $2, $3);
|
7680 | case 5:
|
7681 | return new Fn($0, $1, $2, $3, $4);
|
7682 | case 6:
|
7683 | return new Fn($0, $1, $2, $3, $4, $5);
|
7684 | case 7:
|
7685 | return new Fn($0, $1, $2, $3, $4, $5, $6);
|
7686 | case 8:
|
7687 | return new Fn($0, $1, $2, $3, $4, $5, $6, $7);
|
7688 | case 9:
|
7689 | return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8);
|
7690 | case 10:
|
7691 | return new Fn($0, $1, $2, $3, $4, $5, $6, $7, $8, $9);
|
7692 | }
|
7693 | }));
|
7694 | });
|
7695 |
|
7696 | /**
|
7697 | * Returns a new list without any consecutively repeating elements. Equality is
|
7698 | * determined by applying the supplied predicate two consecutive elements.
|
7699 | * The first element in a series of equal element is the one being preserved.
|
7700 | *
|
7701 | * Acts as a transducer if a transformer is given in list position.
|
7702 | * @see R.transduce
|
7703 | *
|
7704 | * @func
|
7705 | * @memberOf R
|
7706 | * @category List
|
7707 | * @sig (a, a -> Boolean) -> [a] -> [a]
|
7708 | * @param {Function} pred A predicate used to test whether two items are equal.
|
7709 | * @param {Array} list The array to consider.
|
7710 | * @return {Array} `list` without repeating elements.
|
7711 | * @example
|
7712 | *
|
7713 | * function lengthEq(x, y) { return Math.abs(x) === Math.abs(y); };
|
7714 | * var l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
|
7715 | * R.dropRepeatsWith(lengthEq, l); //=> [1, 3, 4, -5, 3]
|
7716 | */
|
7717 | var dropRepeatsWith = _curry2(_dispatchable('dropRepeatsWith', _xdropRepeatsWith, function dropRepeatsWith(pred, list) {
|
7718 | var result = [];
|
7719 | var idx = 0;
|
7720 | var len = list.length;
|
7721 | if (len !== 0) {
|
7722 | result[0] = list[0];
|
7723 | while (++idx < len) {
|
7724 | if (!pred(last(result), list[idx])) {
|
7725 | result[result.length] = list[idx];
|
7726 | }
|
7727 | }
|
7728 | }
|
7729 | return result;
|
7730 | }));
|
7731 |
|
7732 | /**
|
7733 | * Performs a deep test on whether two items are equal.
|
7734 | * Equality implies the two items are semmatically equivalent.
|
7735 | * Cyclic structures are handled as expected
|
7736 | *
|
7737 | * @func
|
7738 | * @memberOf R
|
7739 | * @category Relation
|
7740 | * @sig a -> b -> Boolean
|
7741 | * @param {*} a
|
7742 | * @param {*} b
|
7743 | * @return {Boolean}
|
7744 | * @example
|
7745 | *
|
7746 | * var o = {};
|
7747 | * R.eqDeep(o, o); //=> true
|
7748 | * R.eqDeep(o, {}); //=> true
|
7749 | * R.eqDeep(1, 1); //=> true
|
7750 | * R.eqDeep(1, '1'); //=> false
|
7751 | *
|
7752 | * var a = {}; a.v = a;
|
7753 | * var b = {}; b.v = b;
|
7754 | * R.eqDeep(a, b); //=> true
|
7755 | */
|
7756 | var eqDeep = _curry2(function eqDeep(a, b) {
|
7757 | return _eqDeep(a, b, [], []);
|
7758 | });
|
7759 |
|
7760 | /**
|
7761 | * Creates a new object by evolving a shallow copy of `object`, according to the
|
7762 | * `transformation` functions. All non-primitive properties are copied by reference.
|
7763 | *
|
7764 | * @func
|
7765 | * @memberOf R
|
7766 | * @category Object
|
7767 | * @sig {k: (v -> v)} -> {k: v} -> {k: v}
|
7768 | * @param {Object} transformations The object specifying transformation functions to apply
|
7769 | * to the object.
|
7770 | * @param {Object} object The object to be transformed.
|
7771 | * @return {Object} The transformed object.
|
7772 | * @example
|
7773 | *
|
7774 | * R.evolve({ elapsed: R.add(1), remaining: R.add(-1) }, { name: 'Tomato', elapsed: 100, remaining: 1400 }); //=> { name: 'Tomato', elapsed: 101, remaining: 1399 }
|
7775 | */
|
7776 | var evolve = _curry2(function evolve(transformations, object) {
|
7777 | return _extend(_extend({}, object), mapObjIndexed(function (fn, key) {
|
7778 | return fn(object[key]);
|
7779 | }, transformations));
|
7780 | });
|
7781 |
|
7782 | /**
|
7783 | * Returns a list of function names of object's own functions
|
7784 | *
|
7785 | * @func
|
7786 | * @memberOf R
|
7787 | * @category Object
|
7788 | * @sig {*} -> [String]
|
7789 | * @param {Object} obj The objects with functions in it
|
7790 | * @return {Array} A list of the object's own properties that map to functions.
|
7791 | * @example
|
7792 | *
|
7793 | * R.functions(R); // returns list of ramda's own function names
|
7794 | *
|
7795 | * var F = function() { this.x = function(){}; this.y = 1; }
|
7796 | * F.prototype.z = function() {};
|
7797 | * F.prototype.a = 100;
|
7798 | * R.functions(new F()); //=> ["x"]
|
7799 | */
|
7800 | var functions = _curry1(_functionsWith(keys));
|
7801 |
|
7802 | /**
|
7803 | * Returns all but the last element of a list.
|
7804 | *
|
7805 | * @func
|
7806 | * @memberOf R
|
7807 | * @category List
|
7808 | * @sig [a] -> [a]
|
7809 | * @param {Array} list The array to consider.
|
7810 | * @return {Array} A new array containing all but the last element of the input list, or an
|
7811 | * empty list if the input list is empty.
|
7812 | * @example
|
7813 | *
|
7814 | * R.init(['fi', 'fo', 'fum']); //=> ['fi', 'fo']
|
7815 | */
|
7816 | var init = slice(0, -1);
|
7817 |
|
7818 | /**
|
7819 | * Combines two lists into a set (i.e. no duplicates) composed of those elements common to both lists.
|
7820 | *
|
7821 | * @func
|
7822 | * @memberOf R
|
7823 | * @category Relation
|
7824 | * @sig [a] -> [a] -> [a]
|
7825 | * @param {Array} list1 The first list.
|
7826 | * @param {Array} list2 The second list.
|
7827 | * @see R.intersectionWith
|
7828 | * @return {Array} The list of elements found in both `list1` and `list2`.
|
7829 | * @example
|
7830 | *
|
7831 | * R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
|
7832 | */
|
7833 | var intersection = _curry2(function intersection(list1, list2) {
|
7834 | return uniq(_filter(flip(_contains)(list1), list2));
|
7835 | });
|
7836 |
|
7837 | /**
|
7838 | * Same as R.invertObj, however this accounts for objects
|
7839 | * with duplicate values by putting the values into an
|
7840 | * array.
|
7841 | *
|
7842 | * @func
|
7843 | * @memberOf R
|
7844 | * @category Object
|
7845 | * @sig {s: x} -> {x: [ s, ... ]}
|
7846 | * @param {Object} obj The object or array to invert
|
7847 | * @return {Object} out A new object with keys
|
7848 | * in an array.
|
7849 | * @example
|
7850 | *
|
7851 | * var raceResultsByFirstName = {
|
7852 | * first: 'alice',
|
7853 | * second: 'jake',
|
7854 | * third: 'alice',
|
7855 | * };
|
7856 | * R.invert(raceResultsByFirstName);
|
7857 | * //=> { 'alice': ['first', 'third'], 'jake':['second'] }
|
7858 | */
|
7859 | var invert = _curry1(function invert(obj) {
|
7860 | var props = keys(obj);
|
7861 | var len = props.length;
|
7862 | var idx = -1;
|
7863 | var out = {};
|
7864 | while (++idx < len) {
|
7865 | var key = props[idx];
|
7866 | var val = obj[key];
|
7867 | var list = _has(val, out) ? out[val] : out[val] = [];
|
7868 | list[list.length] = key;
|
7869 | }
|
7870 | return out;
|
7871 | });
|
7872 |
|
7873 | /**
|
7874 | * Returns a new object with the keys of the given object
|
7875 | * as values, and the values of the given object as keys.
|
7876 | *
|
7877 | * @func
|
7878 | * @memberOf R
|
7879 | * @category Object
|
7880 | * @sig {s: x} -> {x: s}
|
7881 | * @param {Object} obj The object or array to invert
|
7882 | * @return {Object} out A new object
|
7883 | * @example
|
7884 | *
|
7885 | * var raceResults = {
|
7886 | * first: 'alice',
|
7887 | * second: 'jake'
|
7888 | * };
|
7889 | * R.invertObj(raceResults);
|
7890 | * //=> { 'alice': 'first', 'jake':'second' }
|
7891 | *
|
7892 | * // Alternatively:
|
7893 | * var raceResults = ['alice', 'jake'];
|
7894 | * R.invertObj(raceResults);
|
7895 | * //=> { 'alice': '0', 'jake':'1' }
|
7896 | */
|
7897 | var invertObj = _curry1(function invertObj(obj) {
|
7898 | var props = keys(obj);
|
7899 | var len = props.length;
|
7900 | var idx = -1;
|
7901 | var out = {};
|
7902 | while (++idx < len) {
|
7903 | var key = props[idx];
|
7904 | out[obj[key]] = key;
|
7905 | }
|
7906 | return out;
|
7907 | });
|
7908 |
|
7909 | /**
|
7910 | * "lifts" a function to be the specified arity, so that it may "map over" that many
|
7911 | * lists (or other Functors).
|
7912 | *
|
7913 | * @func
|
7914 | * @memberOf R
|
7915 | * @see R.lift
|
7916 | * @category Function
|
7917 | * @sig Number -> (*... -> *) -> ([*]... -> [*])
|
7918 | * @param {Function} fn The function to lift into higher context
|
7919 | * @return {Function} The function `fn` applicable to mappable objects.
|
7920 | * @example
|
7921 | *
|
7922 | * var madd3 = R.liftN(3, R.curryN(3, function() {
|
7923 | * return R.reduce(R.add, 0, arguments);
|
7924 | * }));
|
7925 | * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
|
7926 | */
|
7927 | var liftN = _curry2(function liftN(arity, fn) {
|
7928 | var lifted = curryN(arity, fn);
|
7929 | return curryN(arity, function () {
|
7930 | return _reduce(ap, map(lifted, arguments[0]), _slice(arguments, 1));
|
7931 | });
|
7932 | });
|
7933 |
|
7934 | /**
|
7935 | * Returns the mean of the given list of numbers.
|
7936 | *
|
7937 | * @func
|
7938 | * @memberOf R
|
7939 | * @category Math
|
7940 | * @sig [Number] -> Number
|
7941 | * @param {Array} list
|
7942 | * @return {Number}
|
7943 | * @example
|
7944 | *
|
7945 | * R.mean([2, 7, 9]); //=> 6
|
7946 | * R.mean([]); //=> NaN
|
7947 | */
|
7948 | var mean = _curry1(function mean(list) {
|
7949 | return sum(list) / list.length;
|
7950 | });
|
7951 |
|
7952 | /**
|
7953 | * Returns the median of the given list of numbers.
|
7954 | *
|
7955 | * @func
|
7956 | * @memberOf R
|
7957 | * @category Math
|
7958 | * @sig [Number] -> Number
|
7959 | * @param {Array} list
|
7960 | * @return {Number}
|
7961 | * @example
|
7962 | *
|
7963 | * R.median([2, 9, 7]); //=> 7
|
7964 | * R.median([7, 2, 10, 9]); //=> 8
|
7965 | * R.median([]); //=> NaN
|
7966 | */
|
7967 | var median = _curry1(function median(list) {
|
7968 | var len = list.length;
|
7969 | if (len === 0) {
|
7970 | return NaN;
|
7971 | }
|
7972 | var width = 2 - len % 2;
|
7973 | var idx = (len - width) / 2;
|
7974 | return mean(_slice(list).sort(function (a, b) {
|
7975 | return a < b ? -1 : a > b ? 1 : 0;
|
7976 | }).slice(idx, idx + width));
|
7977 | });
|
7978 |
|
7979 | /**
|
7980 | * Create a new object with the own properties of a
|
7981 | * merged with the own properties of object b.
|
7982 | * This function will *not* mutate passed-in objects.
|
7983 | *
|
7984 | * @func
|
7985 | * @memberOf R
|
7986 | * @category Object
|
7987 | * @sig {k: v} -> {k: v} -> {k: v}
|
7988 | * @param {Object} a source object
|
7989 | * @param {Object} b object with higher precedence in output
|
7990 | * @return {Object} The destination object.
|
7991 | * @example
|
7992 | *
|
7993 | * R.merge({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
|
7994 | * //=> { 'name': 'fred', 'age': 40 }
|
7995 | *
|
7996 | * var resetToDefault = R.merge(R.__, {x: 0});
|
7997 | * resetToDefault({x: 5, y: 2}); //=> {x: 0, y: 2}
|
7998 | */
|
7999 | var merge = _curry2(function merge(a, b) {
|
8000 | return _extend(_extend({}, a), b);
|
8001 | });
|
8002 |
|
8003 | /**
|
8004 | * Merges a list of objects together into one object.
|
8005 | *
|
8006 | * @func
|
8007 | * @memberOf R
|
8008 | * @category List
|
8009 | * @sig [{k: v}] -> {k: v}
|
8010 | * @param {Array} list An array of objects
|
8011 | * @return {Object} A merged object.
|
8012 | * @see reduce
|
8013 | * @example
|
8014 | *
|
8015 | * R.mergeAll([{foo:1},{bar:2},{baz:3}]); //=> {foo:1,bar:2,baz:3}
|
8016 | * R.mergeAll([{foo:1},{foo:2},{bar:2}]); //=> {foo:2,bar:2}
|
8017 | */
|
8018 | var mergeAll = _curry1(function mergeAll(list) {
|
8019 | return reduce(merge, {}, list);
|
8020 | });
|
8021 |
|
8022 | /**
|
8023 | * Returns a new list by plucking the same named property off all objects in the list supplied.
|
8024 | *
|
8025 | * @func
|
8026 | * @memberOf R
|
8027 | * @category List
|
8028 | * @sig String -> {*} -> [*]
|
8029 | * @param {Number|String} key The key name to pluck off of each object.
|
8030 | * @param {Array} list The array to consider.
|
8031 | * @return {Array} The list of values for the given key.
|
8032 | * @example
|
8033 | *
|
8034 | * R.pluck('a')([{a: 1}, {a: 2}]); //=> [1, 2]
|
8035 | * R.pluck(0)([[1, 2], [3, 4]]); //=> [1, 3]
|
8036 | */
|
8037 | var pluck = _curry2(_pluck);
|
8038 |
|
8039 | /**
|
8040 | * Multiplies together all the elements of a list.
|
8041 | *
|
8042 | * @func
|
8043 | * @memberOf R
|
8044 | * @category Math
|
8045 | * @sig [Number] -> Number
|
8046 | * @param {Array} list An array of numbers
|
8047 | * @return {Number} The product of all the numbers in the list.
|
8048 | * @see reduce
|
8049 | * @example
|
8050 | *
|
8051 | * R.product([2,4,6,8,100,1]); //=> 38400
|
8052 | */
|
8053 | var product = reduce(_multiply, 1);
|
8054 |
|
8055 | /**
|
8056 | * Reasonable analog to SQL `select` statement.
|
8057 | *
|
8058 | * @func
|
8059 | * @memberOf R
|
8060 | * @category Object
|
8061 | * @category Relation
|
8062 | * @sig [k] -> [{k: v}] -> [{k: v}]
|
8063 | * @param {Array} props The property names to project
|
8064 | * @param {Array} objs The objects to query
|
8065 | * @return {Array} An array of objects with just the `props` properties.
|
8066 | * @example
|
8067 | *
|
8068 | * var abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2};
|
8069 | * var fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7};
|
8070 | * var kids = [abby, fred];
|
8071 | * R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}]
|
8072 | */
|
8073 | // passing `identity` gives correct arity
|
8074 | var project = useWith(_map, pickAll, identity);
|
8075 |
|
8076 | /**
|
8077 | * Returns the string representation of the given value. `eval`'ing the output
|
8078 | * should result in a value equivalent to the input value. Many of the built-in
|
8079 | * `toString` methods do not satisfy this requirement.
|
8080 | *
|
8081 | * If the given value is an `[object Object]` with a `toString` method other
|
8082 | * than `Object.prototype.toString`, this method is invoked with no arguments
|
8083 | * to produce the return value. This means user-defined constructor functions
|
8084 | * can provide a suitable `toString` method. For example:
|
8085 | *
|
8086 | * function Point(x, y) {
|
8087 | * this.x = x;
|
8088 | * this.y = y;
|
8089 | * }
|
8090 | *
|
8091 | * Point.prototype.toString = function() {
|
8092 | * return 'new Point(' + this.x + ', ' + this.y + ')';
|
8093 | * };
|
8094 | *
|
8095 | * R.toString(new Point(1, 2)); //=> 'new Point(1, 2)'
|
8096 | *
|
8097 | * @func
|
8098 | * @memberOf R
|
8099 | * @category String
|
8100 | * @sig * -> String
|
8101 | * @param {*} val
|
8102 | * @return {String}
|
8103 | * @example
|
8104 | *
|
8105 | * R.toString(42); //=> '42'
|
8106 | * R.toString('abc'); //=> '"abc"'
|
8107 | * R.toString([1, 2, 3]); //=> '[1, 2, 3]'
|
8108 | * R.toString({foo: 1, bar: 2, baz: 3}); //=> '{"bar": 2, "baz": 3, "foo": 1}'
|
8109 | * R.toString(new Date('2001-02-03T04:05:06Z')); //=> 'new Date("2001-02-03T04:05:06.000Z")'
|
8110 | */
|
8111 | var toString = _curry1(function toString(val) {
|
8112 | return _toString(val, []);
|
8113 | });
|
8114 |
|
8115 | /**
|
8116 | * Combines two lists into a set (i.e. no duplicates) composed of the
|
8117 | * elements of each list.
|
8118 | *
|
8119 | * @func
|
8120 | * @memberOf R
|
8121 | * @category Relation
|
8122 | * @sig [a] -> [a] -> [a]
|
8123 | * @param {Array} as The first list.
|
8124 | * @param {Array} bs The second list.
|
8125 | * @return {Array} The first and second lists concatenated, with
|
8126 | * duplicates removed.
|
8127 | * @example
|
8128 | *
|
8129 | * R.union([1, 2, 3], [2, 3, 4]); //=> [1, 2, 3, 4]
|
8130 | */
|
8131 | var union = _curry2(compose(uniq, _concat));
|
8132 |
|
8133 | var _stepCat = function () {
|
8134 | var _stepCatArray = {
|
8135 | '@@transducer/init': Array,
|
8136 | '@@transducer/step': function (xs, x) {
|
8137 | return _concat(xs, [x]);
|
8138 | },
|
8139 | '@@transducer/result': _identity
|
8140 | };
|
8141 | var _stepCatString = {
|
8142 | '@@transducer/init': String,
|
8143 | '@@transducer/step': _add,
|
8144 | '@@transducer/result': _identity
|
8145 | };
|
8146 | var _stepCatObject = {
|
8147 | '@@transducer/init': Object,
|
8148 | '@@transducer/step': function (result, input) {
|
8149 | return merge(result, isArrayLike(input) ? _createMapEntry(input[0], input[1]) : input);
|
8150 | },
|
8151 | '@@transducer/result': _identity
|
8152 | };
|
8153 | return function _stepCat(obj) {
|
8154 | if (_isTransformer(obj)) {
|
8155 | return obj;
|
8156 | }
|
8157 | if (isArrayLike(obj)) {
|
8158 | return _stepCatArray;
|
8159 | }
|
8160 | if (typeof obj === 'string') {
|
8161 | return _stepCatString;
|
8162 | }
|
8163 | if (typeof obj === 'object') {
|
8164 | return _stepCatObject;
|
8165 | }
|
8166 | throw new Error('Cannot create transformer for ' + obj);
|
8167 | };
|
8168 | }();
|
8169 |
|
8170 | /**
|
8171 | * Turns a list of Functors into a Functor of a list.
|
8172 | *
|
8173 | * Note: `commute` may be more useful to convert a list of non-Array Functors (e.g.
|
8174 | * Maybe, Either, etc.) to Functor of a list.
|
8175 | *
|
8176 | * @func
|
8177 | * @memberOf R
|
8178 | * @category List
|
8179 | * @see R.commuteMap
|
8180 | * @sig (x -> [x]) -> [[*]...]
|
8181 | * @param {Function} of A function that returns the data type to return
|
8182 | * @param {Array} list An Array (or other Functor) of Arrays (or other Functors)
|
8183 | * @return {Array}
|
8184 | * @example
|
8185 | *
|
8186 | * var as = [[1], [3, 4]];
|
8187 | * R.commute(R.of, as); //=> [[1, 3], [1, 4]]
|
8188 | *
|
8189 | * var bs = [[1, 2], [3]];
|
8190 | * R.commute(R.of, bs); //=> [[1, 3], [2, 3]]
|
8191 | *
|
8192 | * var cs = [[1, 2], [3, 4]];
|
8193 | * R.commute(R.of, cs); //=> [[1, 3], [2, 3], [1, 4], [2, 4]]
|
8194 | */
|
8195 | var commute = commuteMap(map(identity));
|
8196 |
|
8197 | /**
|
8198 | * Wraps a constructor function inside a curried function that can be called with the same
|
8199 | * arguments and returns the same type.
|
8200 | *
|
8201 | * @func
|
8202 | * @memberOf R
|
8203 | * @category Function
|
8204 | * @sig (* -> {*}) -> (* -> {*})
|
8205 | * @param {Function} Fn The constructor function to wrap.
|
8206 | * @return {Function} A wrapped, curried constructor function.
|
8207 | * @example
|
8208 | *
|
8209 | * // Constructor function
|
8210 | * var Widget = function(config) {
|
8211 | * // ...
|
8212 | * };
|
8213 | * Widget.prototype = {
|
8214 | * // ...
|
8215 | * };
|
8216 | * var allConfigs = {
|
8217 | * // ...
|
8218 | * };
|
8219 | * R.map(R.construct(Widget), allConfigs); // a list of Widgets
|
8220 | */
|
8221 | var construct = _curry1(function construct(Fn) {
|
8222 | return constructN(Fn.length, Fn);
|
8223 | });
|
8224 |
|
8225 | /**
|
8226 | * Accepts at least three functions and returns a new function. When invoked, this new
|
8227 | * function will invoke the first function, `after`, passing as its arguments the
|
8228 | * results of invoking the subsequent functions with whatever arguments are passed to
|
8229 | * the new function.
|
8230 | *
|
8231 | * @func
|
8232 | * @memberOf R
|
8233 | * @category Function
|
8234 | * @sig (x1 -> x2 -> ... -> z) -> ((a -> b -> ... -> x1), (a -> b -> ... -> x2), ...) -> (a -> b -> ... -> z)
|
8235 | * @param {Function} after A function. `after` will be invoked with the return values of
|
8236 | * `fn1` and `fn2` as its arguments.
|
8237 | * @param {...Function} functions A variable number of functions.
|
8238 | * @return {Function} A new function.
|
8239 | * @example
|
8240 | *
|
8241 | * var add = function(a, b) { return a + b; };
|
8242 | * var multiply = function(a, b) { return a * b; };
|
8243 | * var subtract = function(a, b) { return a - b; };
|
8244 | *
|
8245 | * //≅ multiply( add(1, 2), subtract(1, 2) );
|
8246 | * R.converge(multiply, add, subtract)(1, 2); //=> -3
|
8247 | *
|
8248 | * var add3 = function(a, b, c) { return a + b + c; };
|
8249 | * R.converge(add3, multiply, add, subtract)(1, 2); //=> 4
|
8250 | */
|
8251 | var converge = curryN(3, function (after) {
|
8252 | var fns = _slice(arguments, 1);
|
8253 | return curryN(max(pluck('length', fns)), function () {
|
8254 | var args = arguments;
|
8255 | var context = this;
|
8256 | return after.apply(context, _map(function (fn) {
|
8257 | return fn.apply(context, args);
|
8258 | }, fns));
|
8259 | });
|
8260 | });
|
8261 |
|
8262 | /**
|
8263 | * Returns a new list without any consecutively repeating elements.
|
8264 | *
|
8265 | * Acts as a transducer if a transformer is given in list position.
|
8266 | * @see R.transduce
|
8267 | *
|
8268 | * @func
|
8269 | * @memberOf R
|
8270 | * @category List
|
8271 | * @sig [a] -> [a]
|
8272 | * @param {Array} list The array to consider.
|
8273 | * @return {Array} `list` without repeating elements.
|
8274 | * @example
|
8275 | *
|
8276 | * R.dropRepeats([1, 1, 1, 2, 3, 4, 4, 2, 2]); //=> [1, 2, 3, 4, 2]
|
8277 | */
|
8278 | var dropRepeats = _curry1(_dispatchable('dropRepeats', _xdropRepeatsWith(_eq), dropRepeatsWith(_eq)));
|
8279 |
|
8280 | /**
|
8281 | * Transforms the items of the list with the transducer and appends the transformed items to
|
8282 | * the accumulator using an appropriate iterator function based on the accumulator type.
|
8283 | *
|
8284 | * The accumulator can be an array, string, object or a transformer. Iterated items will
|
8285 | * be appended to arrays and concatenated to strings. Objects will be merged directly or 2-item
|
8286 | * arrays will be merged as key, value pairs.
|
8287 | *
|
8288 | * The accumulator can also be a transformer object that provides a 2-arity reducing iterator
|
8289 | * function, step, 0-arity initial value function, init, and 1-arity result extraction function
|
8290 | * result. The step function is used as the iterator function in reduce. The result function is
|
8291 | * used to convert the final accumulator into the return type and in most cases is R.identity.
|
8292 | * The init function is used to provide the initial accumulator.
|
8293 | *
|
8294 | * The iteration is performed with R.reduce after initializing the transducer.
|
8295 | *
|
8296 | * @func
|
8297 | * @memberOf R
|
8298 | * @category List
|
8299 | * @sig a -> (b -> b) -> [c] -> a
|
8300 | * @param {*} acc The initial accumulator value.
|
8301 | * @param {Function} xf The transducer function. Receives a transformer and returns a transformer.
|
8302 | * @param {Array} list The list to iterate over.
|
8303 | * @return {*} The final, accumulated value.
|
8304 | * @example
|
8305 | *
|
8306 | * var numbers = [1, 2, 3, 4];
|
8307 | * var transducer = R.compose(R.map(R.add(1)), R.take(2));
|
8308 | *
|
8309 | * R.into([], transducer, numbers); //=> [2, 3]
|
8310 | *
|
8311 | * var intoArray = R.into([]);
|
8312 | * intoArray(transducer, numbers); //=> [2, 3]
|
8313 | */
|
8314 | var into = _curry3(function into(acc, xf, list) {
|
8315 | return _isTransformer(acc) ? _reduce(xf(acc), acc['@@transducer/init'](), list) : _reduce(xf(_stepCat(acc)), acc, list);
|
8316 | });
|
8317 |
|
8318 | /**
|
8319 | * "lifts" a function of arity > 1 so that it may "map over" an Array or
|
8320 | * other Functor.
|
8321 | *
|
8322 | * @func
|
8323 | * @memberOf R
|
8324 | * @see R.liftN
|
8325 | * @category Function
|
8326 | * @sig (*... -> *) -> ([*]... -> [*])
|
8327 | * @param {Function} fn The function to lift into higher context
|
8328 | * @return {Function} The function `fn` applicable to mappable objects.
|
8329 | * @example
|
8330 | *
|
8331 | * var madd3 = R.lift(R.curry(function(a, b, c) {
|
8332 | * return a + b + c;
|
8333 | * }));
|
8334 | * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
|
8335 | *
|
8336 | * var madd5 = R.lift(R.curry(function(a, b, c, d, e) {
|
8337 | * return a + b + c + d + e;
|
8338 | * }));
|
8339 | * madd5([1,2], [3], [4, 5], [6], [7, 8]); //=> [21, 22, 22, 23, 22, 23, 23, 24]
|
8340 | */
|
8341 | var lift = _curry1(function lift(fn) {
|
8342 | return liftN(fn.length, fn);
|
8343 | });
|
8344 |
|
8345 | /**
|
8346 | * Creates a new function that, when invoked, caches the result of calling `fn` for a given
|
8347 | * argument set and returns the result. Subsequent calls to the memoized `fn` with the same
|
8348 | * argument set will not result in an additional call to `fn`; instead, the cached result
|
8349 | * for that set of arguments will be returned.
|
8350 | *
|
8351 | * @func
|
8352 | * @memberOf R
|
8353 | * @category Function
|
8354 | * @sig (*... -> a) -> (*... -> a)
|
8355 | * @param {Function} fn The function to memoize.
|
8356 | * @return {Function} Memoized version of `fn`.
|
8357 | * @example
|
8358 | *
|
8359 | * var count = 0;
|
8360 | * var factorial = R.memoize(function(n) {
|
8361 | * count += 1;
|
8362 | * return R.product(R.range(1, n + 1));
|
8363 | * });
|
8364 | * factorial(5); //=> 120
|
8365 | * factorial(5); //=> 120
|
8366 | * factorial(5); //=> 120
|
8367 | * count; //=> 1
|
8368 | */
|
8369 | var memoize = _curry1(function memoize(fn) {
|
8370 | var cache = {};
|
8371 | return function () {
|
8372 | var key = toString(arguments);
|
8373 | if (!_has(key, cache)) {
|
8374 | cache[key] = fn.apply(this, arguments);
|
8375 | }
|
8376 | return cache[key];
|
8377 | };
|
8378 | });
|
8379 |
|
8380 | var R = {
|
8381 | F: F,
|
8382 | T: T,
|
8383 | __: __,
|
8384 | add: add,
|
8385 | adjust: adjust,
|
8386 | all: all,
|
8387 | allPass: allPass,
|
8388 | always: always,
|
8389 | and: and,
|
8390 | any: any,
|
8391 | anyPass: anyPass,
|
8392 | ap: ap,
|
8393 | aperture: aperture,
|
8394 | append: append,
|
8395 | apply: apply,
|
8396 | arity: arity,
|
8397 | assoc: assoc,
|
8398 | assocPath: assocPath,
|
8399 | binary: binary,
|
8400 | bind: bind,
|
8401 | both: both,
|
8402 | call: call,
|
8403 | chain: chain,
|
8404 | clone: clone,
|
8405 | commute: commute,
|
8406 | commuteMap: commuteMap,
|
8407 | comparator: comparator,
|
8408 | complement: complement,
|
8409 | compose: compose,
|
8410 | composeL: composeL,
|
8411 | composeP: composeP,
|
8412 | concat: concat,
|
8413 | cond: cond,
|
8414 | construct: construct,
|
8415 | constructN: constructN,
|
8416 | contains: contains,
|
8417 | containsWith: containsWith,
|
8418 | converge: converge,
|
8419 | countBy: countBy,
|
8420 | createMapEntry: createMapEntry,
|
8421 | curry: curry,
|
8422 | curryN: curryN,
|
8423 | dec: dec,
|
8424 | defaultTo: defaultTo,
|
8425 | difference: difference,
|
8426 | differenceWith: differenceWith,
|
8427 | dissoc: dissoc,
|
8428 | dissocPath: dissocPath,
|
8429 | divide: divide,
|
8430 | drop: drop,
|
8431 | dropRepeats: dropRepeats,
|
8432 | dropRepeatsWith: dropRepeatsWith,
|
8433 | dropWhile: dropWhile,
|
8434 | either: either,
|
8435 | empty: empty,
|
8436 | eq: eq,
|
8437 | eqDeep: eqDeep,
|
8438 | eqProps: eqProps,
|
8439 | evolve: evolve,
|
8440 | filter: filter,
|
8441 | filterIndexed: filterIndexed,
|
8442 | find: find,
|
8443 | findIndex: findIndex,
|
8444 | findLast: findLast,
|
8445 | findLastIndex: findLastIndex,
|
8446 | flatten: flatten,
|
8447 | flip: flip,
|
8448 | forEach: forEach,
|
8449 | forEachIndexed: forEachIndexed,
|
8450 | fromPairs: fromPairs,
|
8451 | functions: functions,
|
8452 | functionsIn: functionsIn,
|
8453 | groupBy: groupBy,
|
8454 | gt: gt,
|
8455 | gte: gte,
|
8456 | has: has,
|
8457 | hasIn: hasIn,
|
8458 | head: head,
|
8459 | identity: identity,
|
8460 | ifElse: ifElse,
|
8461 | inc: inc,
|
8462 | indexOf: indexOf,
|
8463 | init: init,
|
8464 | insert: insert,
|
8465 | insertAll: insertAll,
|
8466 | intersection: intersection,
|
8467 | intersectionWith: intersectionWith,
|
8468 | intersperse: intersperse,
|
8469 | into: into,
|
8470 | invert: invert,
|
8471 | invertObj: invertObj,
|
8472 | invoke: invoke,
|
8473 | invoker: invoker,
|
8474 | is: is,
|
8475 | isArrayLike: isArrayLike,
|
8476 | isEmpty: isEmpty,
|
8477 | isNaN: isNaN,
|
8478 | isNil: isNil,
|
8479 | isSet: isSet,
|
8480 | join: join,
|
8481 | keys: keys,
|
8482 | keysIn: keysIn,
|
8483 | last: last,
|
8484 | lastIndexOf: lastIndexOf,
|
8485 | length: length,
|
8486 | lens: lens,
|
8487 | lensIndex: lensIndex,
|
8488 | lensOn: lensOn,
|
8489 | lensProp: lensProp,
|
8490 | lift: lift,
|
8491 | liftN: liftN,
|
8492 | lt: lt,
|
8493 | lte: lte,
|
8494 | map: map,
|
8495 | mapAccum: mapAccum,
|
8496 | mapAccumRight: mapAccumRight,
|
8497 | mapIndexed: mapIndexed,
|
8498 | mapObj: mapObj,
|
8499 | mapObjIndexed: mapObjIndexed,
|
8500 | match: match,
|
8501 | mathMod: mathMod,
|
8502 | max: max,
|
8503 | maxBy: maxBy,
|
8504 | mean: mean,
|
8505 | median: median,
|
8506 | memoize: memoize,
|
8507 | merge: merge,
|
8508 | mergeAll: mergeAll,
|
8509 | min: min,
|
8510 | minBy: minBy,
|
8511 | modulo: modulo,
|
8512 | multiply: multiply,
|
8513 | nAry: nAry,
|
8514 | negate: negate,
|
8515 | none: none,
|
8516 | not: not,
|
8517 | nth: nth,
|
8518 | nthArg: nthArg,
|
8519 | nthChar: nthChar,
|
8520 | nthCharCode: nthCharCode,
|
8521 | of: of,
|
8522 | omit: omit,
|
8523 | once: once,
|
8524 | or: or,
|
8525 | partial: partial,
|
8526 | partialRight: partialRight,
|
8527 | partition: partition,
|
8528 | path: path,
|
8529 | pathEq: pathEq,
|
8530 | pick: pick,
|
8531 | pickAll: pickAll,
|
8532 | pickBy: pickBy,
|
8533 | pipe: pipe,
|
8534 | pipeL: pipeL,
|
8535 | pipeP: pipeP,
|
8536 | pluck: pluck,
|
8537 | prepend: prepend,
|
8538 | product: product,
|
8539 | project: project,
|
8540 | prop: prop,
|
8541 | propEq: propEq,
|
8542 | propOr: propOr,
|
8543 | props: props,
|
8544 | range: range,
|
8545 | reduce: reduce,
|
8546 | reduceIndexed: reduceIndexed,
|
8547 | reduceRight: reduceRight,
|
8548 | reduceRightIndexed: reduceRightIndexed,
|
8549 | reject: reject,
|
8550 | rejectIndexed: rejectIndexed,
|
8551 | remove: remove,
|
8552 | repeat: repeat,
|
8553 | replace: replace,
|
8554 | reverse: reverse,
|
8555 | scan: scan,
|
8556 | slice: slice,
|
8557 | sort: sort,
|
8558 | sortBy: sortBy,
|
8559 | split: split,
|
8560 | strIndexOf: strIndexOf,
|
8561 | strLastIndexOf: strLastIndexOf,
|
8562 | substring: substring,
|
8563 | substringFrom: substringFrom,
|
8564 | substringTo: substringTo,
|
8565 | subtract: subtract,
|
8566 | sum: sum,
|
8567 | tail: tail,
|
8568 | take: take,
|
8569 | takeWhile: takeWhile,
|
8570 | tap: tap,
|
8571 | test: test,
|
8572 | times: times,
|
8573 | toLower: toLower,
|
8574 | toPairs: toPairs,
|
8575 | toPairsIn: toPairsIn,
|
8576 | toString: toString,
|
8577 | toUpper: toUpper,
|
8578 | transduce: transduce,
|
8579 | trim: trim,
|
8580 | type: type,
|
8581 | unapply: unapply,
|
8582 | unary: unary,
|
8583 | uncurryN: uncurryN,
|
8584 | unfold: unfold,
|
8585 | union: union,
|
8586 | unionWith: unionWith,
|
8587 | uniq: uniq,
|
8588 | uniqWith: uniqWith,
|
8589 | unnest: unnest,
|
8590 | update: update,
|
8591 | useWith: useWith,
|
8592 | values: values,
|
8593 | valuesIn: valuesIn,
|
8594 | where: where,
|
8595 | whereEq: whereEq,
|
8596 | wrap: wrap,
|
8597 | xprod: xprod,
|
8598 | zip: zip,
|
8599 | zipObj: zipObj,
|
8600 | zipWith: zipWith
|
8601 | };
|
8602 |
|
8603 | /* TEST_ENTRY_POINT */
|
8604 |
|
8605 | if (true) {
|
8606 | module.exports = R;
|
8607 | } else if (typeof define === 'function' && define.amd) {
|
8608 | define(function() { return R; });
|
8609 | } else {
|
8610 | this.R = R;
|
8611 | }
|
8612 |
|
8613 | }.call(this));
|
8614 |
|
8615 |
|
8616 | /***/ },
|
8617 | /* 12 */
|
8618 | /***/ function(module, exports, __webpack_require__) {
|
8619 |
|
8620 | var __WEBPACK_AMD_DEFINE_RESULT__;/**
|
8621 | * This module exports functions for checking types
|
8622 | * and throwing exceptions.
|
8623 | */
|
8624 |
|
8625 | /*globals define, module */
|
8626 |
|
8627 | (function (globals) {
|
8628 | ;
|
8629 |
|
8630 | var messages, predicates, functions, verify, maybe, not;
|
8631 |
|
8632 | predicates = {
|
8633 | like: like,
|
8634 | instance: instance,
|
8635 | emptyObject: emptyObject,
|
8636 | nulled: nulled,
|
8637 | defined: defined,
|
8638 | object: object,
|
8639 | length: length,
|
8640 | array: array,
|
8641 | date: date,
|
8642 | fn: fn,
|
8643 | webUrl: webUrl,
|
8644 | gitUrl: gitUrl,
|
8645 | email: email,
|
8646 | unemptyString: unemptyString,
|
8647 | string: string,
|
8648 | evenNumber: evenNumber,
|
8649 | oddNumber: oddNumber,
|
8650 | positiveNumber: positiveNumber,
|
8651 | negativeNumber: negativeNumber,
|
8652 | intNumber : intNumber,
|
8653 | floatNumber : floatNumber,
|
8654 | number: number,
|
8655 | bool: bool
|
8656 | };
|
8657 |
|
8658 | messages = {
|
8659 | like: 'Invalid type',
|
8660 | instance: 'Invalid type',
|
8661 | emptyObject: 'Invalid object',
|
8662 | nulled: 'Not null',
|
8663 | defined: 'Not defined',
|
8664 | object: 'Invalid object',
|
8665 | length: 'Invalid length',
|
8666 | array: 'Invalid array',
|
8667 | date: 'Invalid date',
|
8668 | fn: 'Invalid function',
|
8669 | webUrl: 'Invalid URL',
|
8670 | gitUrl: 'Invalid git URL',
|
8671 | email: 'Invalid email',
|
8672 | unemptyString: 'Invalid string',
|
8673 | string: 'Invalid string',
|
8674 | evenNumber: 'Invalid number',
|
8675 | oddNumber: 'Invalid number',
|
8676 | positiveNumber: 'Invalid number',
|
8677 | negativeNumber: 'Invalid number',
|
8678 | intNumber: 'Invalid number',
|
8679 | floatNumber: 'Invalid number',
|
8680 | number: 'Invalid number',
|
8681 | bool: 'Invalid boolean'
|
8682 | };
|
8683 |
|
8684 | functions = {
|
8685 | map: map,
|
8686 | every: every,
|
8687 | any: any
|
8688 | };
|
8689 |
|
8690 | functions = mixin(functions, predicates);
|
8691 | verify = createModifiedPredicates(verifyModifier);
|
8692 | maybe = createModifiedPredicates(maybeModifier);
|
8693 | not = createModifiedPredicates(notModifier);
|
8694 | verify.maybe = createModifiedFunctions(verifyModifier, maybe);
|
8695 | verify.not = createModifiedFunctions(verifyModifier, not);
|
8696 |
|
8697 | exportFunctions(mixin(functions, {
|
8698 | verify: verify,
|
8699 | maybe: maybe,
|
8700 | not: not
|
8701 | }));
|
8702 |
|
8703 | /**
|
8704 | * Public function `like`.
|
8705 | *
|
8706 | * Tests whether an object 'quacks like a duck'.
|
8707 | * Returns `true` if the first argument has all of
|
8708 | * the properties of the second, archetypal argument
|
8709 | * (the 'duck'). Returns `false` otherwise. If either
|
8710 | * argument is not an object, an exception is thrown.
|
8711 | *
|
8712 | * @param thing {object} The object to test.
|
8713 | * @param duck {object} The archetypal object, or
|
8714 | * 'duck', that the test is
|
8715 | * against.
|
8716 | */
|
8717 | function like (thing, duck) {
|
8718 | var name;
|
8719 |
|
8720 | verify.object(thing);
|
8721 | verify.object(duck);
|
8722 |
|
8723 | for (name in duck) {
|
8724 | if (duck.hasOwnProperty(name)) {
|
8725 | if (thing.hasOwnProperty(name) === false || typeof thing[name] !== typeof duck[name]) {
|
8726 | return false;
|
8727 | }
|
8728 |
|
8729 | if (object(thing[name]) && like(thing[name], duck[name]) === false) {
|
8730 | return false;
|
8731 | }
|
8732 | }
|
8733 | }
|
8734 |
|
8735 | return true;
|
8736 | }
|
8737 |
|
8738 | /**
|
8739 | * Public function `instance`.
|
8740 | *
|
8741 | * Returns `true` if an object is an instance of a prototype,
|
8742 | * `false` otherwise.
|
8743 | *
|
8744 | * @param thing {object} The object to test.
|
8745 | * @param prototype {function} The prototype that the
|
8746 | * test is against.
|
8747 | */
|
8748 | function instance (thing, prototype) {
|
8749 | if (!defined(thing) || nulled(thing)) {
|
8750 | return false;
|
8751 | }
|
8752 |
|
8753 | if (fn(prototype) && thing instanceof prototype) {
|
8754 | return true;
|
8755 | }
|
8756 |
|
8757 | return false;
|
8758 | }
|
8759 |
|
8760 | /**
|
8761 | * Public function `emptyObject`.
|
8762 | *
|
8763 | * Returns `true` if something is an empty, non-null,
|
8764 | * non-array object, `false` otherwise.
|
8765 | *
|
8766 | * @param thing The thing to test.
|
8767 | */
|
8768 | function emptyObject (thing) {
|
8769 | var property;
|
8770 |
|
8771 | if (object(thing)) {
|
8772 | for (property in thing) {
|
8773 | if (thing.hasOwnProperty(property)) {
|
8774 | return false;
|
8775 | }
|
8776 | }
|
8777 |
|
8778 | return true;
|
8779 | }
|
8780 |
|
8781 | return false;
|
8782 | }
|
8783 |
|
8784 | /**
|
8785 | * Public function `nulled`.
|
8786 | *
|
8787 | * Returns `true` if something is null,
|
8788 | * `false` otherwise.
|
8789 | *
|
8790 | * @param thing The thing to test.
|
8791 | */
|
8792 | function nulled (thing) {
|
8793 | return thing === null;
|
8794 | }
|
8795 |
|
8796 | /**
|
8797 | * Public function `defined`.
|
8798 | *
|
8799 | * Returns `true` if something is not undefined,
|
8800 | * `false` otherwise.
|
8801 | *
|
8802 | * @param thing The thing to test.
|
8803 | */
|
8804 | function defined (thing) {
|
8805 | return thing !== void 0;
|
8806 | }
|
8807 |
|
8808 | /**
|
8809 | * Public function `object`.
|
8810 | *
|
8811 | * Returns `true` if something is a non-null, non-array,
|
8812 | * non-date object, `false` otherwise.
|
8813 | *
|
8814 | * @param thing The thing to test.
|
8815 | */
|
8816 | function object (thing) {
|
8817 | return typeof thing === 'object' && !nulled(thing) && !array(thing) && !date(thing);
|
8818 | }
|
8819 |
|
8820 | /**
|
8821 | * Public function `length`.
|
8822 | *
|
8823 | * Returns `true` if something is has a length property
|
8824 | * that equals `value`, `false` otherwise.
|
8825 | *
|
8826 | * @param thing The thing to test.
|
8827 | * @param value The required length to test against.
|
8828 | */
|
8829 | function length (thing, value) {
|
8830 | return thing && thing.length === value;
|
8831 | }
|
8832 |
|
8833 | /**
|
8834 | * Public function `array`.
|
8835 | *
|
8836 | * Returns `true` something is an array, `false` otherwise.
|
8837 | *
|
8838 | * @param thing The thing to test.
|
8839 | */
|
8840 | function array (thing) {
|
8841 | if (Array.isArray) {
|
8842 | return Array.isArray(thing);
|
8843 | }
|
8844 |
|
8845 | return Object.prototype.toString.call(thing) === '[object Array]';
|
8846 | }
|
8847 |
|
8848 | /**
|
8849 | * Public function `date`.
|
8850 | *
|
8851 | * Returns `true` something is a date, `false` otherwise.
|
8852 | *
|
8853 | * @param thing The thing to test.
|
8854 | */
|
8855 | function date (thing) {
|
8856 | return Object.prototype.toString.call(thing) === '[object Date]';
|
8857 | }
|
8858 |
|
8859 | /**
|
8860 | * Public function `fn`.
|
8861 | *
|
8862 | * Returns `true` if something is function, `false` otherwise.
|
8863 | *
|
8864 | * @param thing The thing to test.
|
8865 | */
|
8866 | function fn (thing) {
|
8867 | return typeof thing === 'function';
|
8868 | }
|
8869 |
|
8870 | /**
|
8871 | * Public function `webUrl`.
|
8872 | *
|
8873 | * Returns `true` if something is an HTTP or HTTPS URL,
|
8874 | * `false` otherwise.
|
8875 | *
|
8876 | * @param thing The thing to test.
|
8877 | */
|
8878 | function webUrl (thing) {
|
8879 | return unemptyString(thing) && /^https?:\/\/.+/.test(thing);
|
8880 | }
|
8881 |
|
8882 | /**
|
8883 | * Public function `gitUrl`.
|
8884 | *
|
8885 | * Returns `true` if something is a git+ssh, git+http or git+https URL,
|
8886 | * `false` otherwise.
|
8887 | *
|
8888 | * @param thing The thing to test.
|
8889 | */
|
8890 | function gitUrl (thing) {
|
8891 | return unemptyString(thing) && /^git\+(ssh|https?):\/\/.+/.test(thing);
|
8892 | }
|
8893 |
|
8894 | /**
|
8895 | * Public function `email`.
|
8896 | *
|
8897 | * Returns `true` if something seems like a valid email address,
|
8898 | * `false` otherwise.
|
8899 | *
|
8900 | * @param thing The thing to test.
|
8901 | */
|
8902 | function email (thing) {
|
8903 | return unemptyString(thing) && /\S+@\S+/.test(thing);
|
8904 | }
|
8905 |
|
8906 | /**
|
8907 | * Public function `unemptyString`.
|
8908 | *
|
8909 | * Returns `true` if something is a non-empty string, `false`
|
8910 | * otherwise.
|
8911 | *
|
8912 | * @param thing The thing to test.
|
8913 | */
|
8914 | function unemptyString (thing) {
|
8915 | return string(thing) && thing !== '';
|
8916 | }
|
8917 |
|
8918 | /**
|
8919 | * Public function `string`.
|
8920 | *
|
8921 | * Returns `true` if something is a string, `false` otherwise.
|
8922 | *
|
8923 | * @param thing The thing to test.
|
8924 | */
|
8925 | function string (thing) {
|
8926 | return typeof thing === 'string';
|
8927 | }
|
8928 |
|
8929 | /**
|
8930 | * Public function `oddNumber`.
|
8931 | *
|
8932 | * Returns `true` if something is an odd number,
|
8933 | * `false` otherwise.
|
8934 | *
|
8935 | * @param thing The thing to test.
|
8936 | */
|
8937 | function oddNumber (thing) {
|
8938 | return number(thing) && (thing % 2 === 1 || thing % 2 === -1);
|
8939 | }
|
8940 |
|
8941 | /**
|
8942 | * Public function `evenNumber`.
|
8943 | *
|
8944 | * Returns `true` if something is an even number,
|
8945 | * `false` otherwise.
|
8946 | *
|
8947 | * @param thing The thing to test.
|
8948 | */
|
8949 | function evenNumber (thing) {
|
8950 | return number(thing) && thing % 2 === 0;
|
8951 | }
|
8952 |
|
8953 | /**
|
8954 | * Public function `intNumber`.
|
8955 | *
|
8956 | * Returns `true` if something is an integer number,
|
8957 | * `false` otherwise.
|
8958 | *
|
8959 | * @param thing The thing to test.
|
8960 | */
|
8961 | function intNumber (thing) {
|
8962 | return number(thing) && thing % 1 === 0;
|
8963 | }
|
8964 |
|
8965 | /**
|
8966 | * Public function `floatNumber`.
|
8967 | *
|
8968 | * Returns `true` if something is a float number,
|
8969 | * `false` otherwise.
|
8970 | *
|
8971 | * @param thing The thing to test.
|
8972 | */
|
8973 | function floatNumber (thing) {
|
8974 | return number(thing) && thing % 1 !== 0;
|
8975 | }
|
8976 |
|
8977 | /**
|
8978 | * Public function `positiveNumber`.
|
8979 | *
|
8980 | * Returns `true` if something is a positive number,
|
8981 | * `false` otherwise.
|
8982 | *
|
8983 | * @param thing The thing to test.
|
8984 | */
|
8985 | function positiveNumber (thing) {
|
8986 | return number(thing) && thing > 0;
|
8987 | }
|
8988 |
|
8989 | /**
|
8990 | * Public function `negativeNumber`.
|
8991 | *
|
8992 | * Returns `true` if something is a positive number,
|
8993 | * `false` otherwise.
|
8994 | *
|
8995 | * @param thing The thing to test.
|
8996 | */
|
8997 | function negativeNumber (thing) {
|
8998 | return number(thing) && thing < 0;
|
8999 | }
|
9000 |
|
9001 | /**
|
9002 | * Public function `number`.
|
9003 | *
|
9004 | * Returns `true` if something is a real number,
|
9005 | * `false` otherwise.
|
9006 | *
|
9007 | * @param thing The thing to test.
|
9008 | */
|
9009 | function number (thing) {
|
9010 | return typeof thing === 'number' &&
|
9011 | isNaN(thing) === false &&
|
9012 | thing !== Number.POSITIVE_INFINITY &&
|
9013 | thing !== Number.NEGATIVE_INFINITY;
|
9014 | }
|
9015 |
|
9016 | /**
|
9017 | * Public function `bool`.
|
9018 | *
|
9019 | * Returns `true` if something is a bool,
|
9020 | * `false` otherwise.
|
9021 | *
|
9022 | * @param thing The thing to test.
|
9023 | */
|
9024 | function bool (thing) {
|
9025 | return thing === false || thing === true;
|
9026 | }
|
9027 |
|
9028 | /**
|
9029 | * Public function `map`.
|
9030 | *
|
9031 | * Returns the results hash of mapping each predicate to the
|
9032 | * corresponding thing's property. Similar to `like` but
|
9033 | * with functions instead of values.
|
9034 | *
|
9035 | * @param things {object} The things to test.
|
9036 | * @param predicates {object} The map of functions to call against
|
9037 | * the corresponding properties from `things`.
|
9038 | */
|
9039 | function map (things, predicates) {
|
9040 | var property, result = {}, predicate;
|
9041 |
|
9042 | verify.object(things);
|
9043 | verify.object(predicates);
|
9044 |
|
9045 | for (property in predicates) {
|
9046 | if (predicates.hasOwnProperty(property)) {
|
9047 | predicate = predicates[property];
|
9048 |
|
9049 | if (fn(predicate)) {
|
9050 | result[property] = predicate(things[property]);
|
9051 | } else if (object(predicate)) {
|
9052 | result[property] = map(things[property], predicate);
|
9053 | }
|
9054 | }
|
9055 | }
|
9056 |
|
9057 | return result;
|
9058 | }
|
9059 |
|
9060 | /**
|
9061 | * Public function `every`
|
9062 | *
|
9063 | * Returns the conjunction of all booleans in a hash.
|
9064 | *
|
9065 | * @param predicateResults {object} The hash of evaluated predicates.
|
9066 | */
|
9067 | function every (predicateResults) {
|
9068 | var property, value;
|
9069 |
|
9070 | verify.object(predicateResults);
|
9071 |
|
9072 | for (property in predicateResults) {
|
9073 | if (predicateResults.hasOwnProperty(property)) {
|
9074 | value = predicateResults[property];
|
9075 |
|
9076 | if (object(value) && every(value) === false) {
|
9077 | return false;
|
9078 | }
|
9079 |
|
9080 | if (value === false) {
|
9081 | return false;
|
9082 | }
|
9083 | }
|
9084 | }
|
9085 | return true;
|
9086 | }
|
9087 |
|
9088 | /**
|
9089 | * Public function `any`
|
9090 | *
|
9091 | * Returns the disjunction of all booleans in a hash.
|
9092 | *
|
9093 | * @param predicateResults {object} The hash of evaluated predicates.
|
9094 | */
|
9095 | function any (predicateResults) {
|
9096 | var property, value;
|
9097 |
|
9098 | verify.object(predicateResults);
|
9099 |
|
9100 | for (property in predicateResults) {
|
9101 | if (predicateResults.hasOwnProperty(property)) {
|
9102 | value = predicateResults[property];
|
9103 |
|
9104 | if (object(value) && any(value)) {
|
9105 | return true;
|
9106 | }
|
9107 |
|
9108 | if (value === true) {
|
9109 | return true;
|
9110 | }
|
9111 | }
|
9112 | }
|
9113 |
|
9114 | return false;
|
9115 | }
|
9116 |
|
9117 | function mixin (target, source) {
|
9118 | var property;
|
9119 |
|
9120 | for (property in source) {
|
9121 | if (source.hasOwnProperty(property)) {
|
9122 | target[property] = source[property];
|
9123 | }
|
9124 | }
|
9125 |
|
9126 | return target;
|
9127 | }
|
9128 |
|
9129 | /**
|
9130 | * Public modifier `verify`.
|
9131 | *
|
9132 | * Throws if `predicate` returns `false`.
|
9133 | */
|
9134 | function verifyModifier (predicate, defaultMessage) {
|
9135 | return function () {
|
9136 | var message;
|
9137 |
|
9138 | if (predicate.apply(null, arguments) === false) {
|
9139 | message = arguments[arguments.length - 1];
|
9140 | throw new Error(unemptyString(message) ? message : defaultMessage);
|
9141 | }
|
9142 | };
|
9143 | }
|
9144 |
|
9145 | /**
|
9146 | * Public modifier `maybe`.
|
9147 | *
|
9148 | * Returns `true` if `predicate` is `null` or `undefined`,
|
9149 | * otherwise propagates the return value from `predicate`.
|
9150 | */
|
9151 | function maybeModifier (predicate) {
|
9152 | return function () {
|
9153 | if (!defined(arguments[0]) || nulled(arguments[0])) {
|
9154 | return true;
|
9155 | }
|
9156 |
|
9157 | return predicate.apply(null, arguments);
|
9158 | };
|
9159 | }
|
9160 |
|
9161 | /**
|
9162 | * Public modifier `not`.
|
9163 | *
|
9164 | * Negates `predicate`.
|
9165 | */
|
9166 | function notModifier (predicate) {
|
9167 | return function () {
|
9168 | return !predicate.apply(null, arguments);
|
9169 | };
|
9170 | }
|
9171 |
|
9172 | function createModifiedPredicates (modifier) {
|
9173 | return createModifiedFunctions(modifier, predicates);
|
9174 | }
|
9175 |
|
9176 | function createModifiedFunctions (modifier, functions) {
|
9177 | var name, result = {};
|
9178 |
|
9179 | for (name in functions) {
|
9180 | if (functions.hasOwnProperty(name)) {
|
9181 | result[name] = modifier(functions[name], messages[name]);
|
9182 | }
|
9183 | }
|
9184 |
|
9185 | return result;
|
9186 | }
|
9187 |
|
9188 | function exportFunctions (functions) {
|
9189 | if (true) {
|
9190 | !(__WEBPACK_AMD_DEFINE_RESULT__ = function () {
|
9191 | return functions;
|
9192 | }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
|
9193 | } else if (typeof module !== 'undefined' && module !== null && module.exports) {
|
9194 | module.exports = functions;
|
9195 | } else {
|
9196 | globals.check = functions;
|
9197 | }
|
9198 | }
|
9199 | }(this));
|
9200 |
|
9201 |
|
9202 | /***/ }
|
9203 | /******/ ])
|
9204 | });
|
9205 | ; |
\ | No newline at end of file |