UNPKG

28.4 kBJavaScriptView Raw
1'use strict';
2
3function _isPlaceholder(a) {
4 return a != null && typeof a === 'object' && a['@@functional/placeholder'] === true;
5}
6
7/**
8 * Optimized internal one-arity curry function.
9 *
10 * @private
11 * @category Function
12 * @param {Function} fn The function to curry.
13 * @return {Function} The curried function.
14 */
15function _curry1(fn) {
16 return function f1(a) {
17 if (arguments.length === 0 || _isPlaceholder(a)) {
18 return f1;
19 } else {
20 return fn.apply(this, arguments);
21 }
22 };
23}
24
25/**
26 * Optimized internal two-arity curry function.
27 *
28 * @private
29 * @category Function
30 * @param {Function} fn The function to curry.
31 * @return {Function} The curried function.
32 */
33function _curry2(fn) {
34 return function f2(a, b) {
35 switch (arguments.length) {
36 case 0:
37 return f2;
38 case 1:
39 return _isPlaceholder(a) ? f2 : _curry1(function (_b) {
40 return fn(a, _b);
41 });
42 default:
43 return _isPlaceholder(a) && _isPlaceholder(b) ? f2 : _isPlaceholder(a) ? _curry1(function (_a) {
44 return fn(_a, b);
45 }) : _isPlaceholder(b) ? _curry1(function (_b) {
46 return fn(a, _b);
47 }) : fn(a, b);
48 }
49 };
50}
51
52/**
53 * Private `concat` function to merge two array-like objects.
54 *
55 * @private
56 * @param {Array|Arguments} [set1=[]] An array-like object.
57 * @param {Array|Arguments} [set2=[]] An array-like object.
58 * @return {Array} A new, merged array.
59 * @example
60 *
61 * _concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
62 */
63function _concat(set1, set2) {
64 set1 = set1 || [];
65 set2 = set2 || [];
66 var idx;
67 var len1 = set1.length;
68 var len2 = set2.length;
69 var result = [];
70
71 idx = 0;
72 while (idx < len1) {
73 result[result.length] = set1[idx];
74 idx += 1;
75 }
76 idx = 0;
77 while (idx < len2) {
78 result[result.length] = set2[idx];
79 idx += 1;
80 }
81 return result;
82}
83
84function _arity(n, fn) {
85 /* eslint-disable no-unused-vars */
86 switch (n) {
87 case 0:
88 return function () {
89 return fn.apply(this, arguments);
90 };
91 case 1:
92 return function (a0) {
93 return fn.apply(this, arguments);
94 };
95 case 2:
96 return function (a0, a1) {
97 return fn.apply(this, arguments);
98 };
99 case 3:
100 return function (a0, a1, a2) {
101 return fn.apply(this, arguments);
102 };
103 case 4:
104 return function (a0, a1, a2, a3) {
105 return fn.apply(this, arguments);
106 };
107 case 5:
108 return function (a0, a1, a2, a3, a4) {
109 return fn.apply(this, arguments);
110 };
111 case 6:
112 return function (a0, a1, a2, a3, a4, a5) {
113 return fn.apply(this, arguments);
114 };
115 case 7:
116 return function (a0, a1, a2, a3, a4, a5, a6) {
117 return fn.apply(this, arguments);
118 };
119 case 8:
120 return function (a0, a1, a2, a3, a4, a5, a6, a7) {
121 return fn.apply(this, arguments);
122 };
123 case 9:
124 return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
125 return fn.apply(this, arguments);
126 };
127 case 10:
128 return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
129 return fn.apply(this, arguments);
130 };
131 default:
132 throw new Error('First argument to _arity must be a non-negative integer no greater than ten');
133 }
134}
135
136/**
137 * Internal curryN function.
138 *
139 * @private
140 * @category Function
141 * @param {Number} length The arity of the curried function.
142 * @param {Array} received An array of arguments received thus far.
143 * @param {Function} fn The function to curry.
144 * @return {Function} The curried function.
145 */
146function _curryN(length, received, fn) {
147 return function () {
148 var combined = [];
149 var argsIdx = 0;
150 var left = length;
151 var combinedIdx = 0;
152 while (combinedIdx < received.length || argsIdx < arguments.length) {
153 var result;
154 if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
155 result = received[combinedIdx];
156 } else {
157 result = arguments[argsIdx];
158 argsIdx += 1;
159 }
160 combined[combinedIdx] = result;
161 if (!_isPlaceholder(result)) {
162 left -= 1;
163 }
164 combinedIdx += 1;
165 }
166 return left <= 0 ? fn.apply(this, combined) : _arity(left, _curryN(length, combined, fn));
167 };
168}
169
170/**
171 * Returns a curried equivalent of the provided function, with the specified
172 * arity. The curried function has two unusual capabilities. First, its
173 * arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
174 * following are equivalent:
175 *
176 * - `g(1)(2)(3)`
177 * - `g(1)(2, 3)`
178 * - `g(1, 2)(3)`
179 * - `g(1, 2, 3)`
180 *
181 * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
182 * "gaps", allowing partial application of any combination of arguments,
183 * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
184 * the following are equivalent:
185 *
186 * - `g(1, 2, 3)`
187 * - `g(_, 2, 3)(1)`
188 * - `g(_, _, 3)(1)(2)`
189 * - `g(_, _, 3)(1, 2)`
190 * - `g(_, 2)(1)(3)`
191 * - `g(_, 2)(1, 3)`
192 * - `g(_, 2)(_, 3)(1)`
193 *
194 * @func
195 * @memberOf R
196 * @since v0.5.0
197 * @category Function
198 * @sig Number -> (* -> a) -> (* -> a)
199 * @param {Number} length The arity for the returned function.
200 * @param {Function} fn The function to curry.
201 * @return {Function} A new, curried function.
202 * @see R.curry
203 * @example
204 *
205 * const sumArgs = (...args) => R.sum(args);
206 *
207 * const curriedAddFourNumbers = R.curryN(4, sumArgs);
208 * const f = curriedAddFourNumbers(1, 2);
209 * const g = f(3);
210 * g(4); //=> 10
211 */
212var curryN = /*#__PURE__*/_curry2(function curryN(length, fn) {
213 if (length === 1) {
214 return _curry1(fn);
215 }
216 return _arity(length, _curryN(length, [], fn));
217});
218
219/**
220 * Tests whether or not an object is an array.
221 *
222 * @private
223 * @param {*} val The object to test.
224 * @return {Boolean} `true` if `val` is an array, `false` otherwise.
225 * @example
226 *
227 * _isArray([]); //=> true
228 * _isArray(null); //=> false
229 * _isArray({}); //=> false
230 */
231var _isArray = Array.isArray || function _isArray(val) {
232 return val != null && val.length >= 0 && Object.prototype.toString.call(val) === '[object Array]';
233};
234
235function _isTransformer(obj) {
236 return obj != null && typeof obj['@@transducer/step'] === 'function';
237}
238
239/**
240 * Returns a function that dispatches with different strategies based on the
241 * object in list position (last argument). If it is an array, executes [fn].
242 * Otherwise, if it has a function with one of the given method names, it will
243 * execute that function (functor case). Otherwise, if it is a transformer,
244 * uses transducer [xf] to return a new transformer (transducer case).
245 * Otherwise, it will default to executing [fn].
246 *
247 * @private
248 * @param {Array} methodNames properties to check for a custom implementation
249 * @param {Function} xf transducer to initialize if object is transformer
250 * @param {Function} fn default ramda implementation
251 * @return {Function} A function that dispatches on object in list position
252 */
253function _dispatchable(methodNames, xf, fn) {
254 return function () {
255 if (arguments.length === 0) {
256 return fn();
257 }
258 var args = Array.prototype.slice.call(arguments, 0);
259 var obj = args.pop();
260 if (!_isArray(obj)) {
261 var idx = 0;
262 while (idx < methodNames.length) {
263 if (typeof obj[methodNames[idx]] === 'function') {
264 return obj[methodNames[idx]].apply(obj, args);
265 }
266 idx += 1;
267 }
268 if (_isTransformer(obj)) {
269 var transducer = xf.apply(null, args);
270 return transducer(obj);
271 }
272 }
273 return fn.apply(this, arguments);
274 };
275}
276
277var _xfBase = {
278 init: function () {
279 return this.xf['@@transducer/init']();
280 },
281 result: function (result) {
282 return this.xf['@@transducer/result'](result);
283 }
284};
285
286function _map(fn, functor) {
287 var idx = 0;
288 var len = functor.length;
289 var result = Array(len);
290 while (idx < len) {
291 result[idx] = fn(functor[idx]);
292 idx += 1;
293 }
294 return result;
295}
296
297function _isString(x) {
298 return Object.prototype.toString.call(x) === '[object String]';
299}
300
301/**
302 * Tests whether or not an object is similar to an array.
303 *
304 * @private
305 * @category Type
306 * @category List
307 * @sig * -> Boolean
308 * @param {*} x The object to test.
309 * @return {Boolean} `true` if `x` has a numeric length property and extreme indices defined; `false` otherwise.
310 * @example
311 *
312 * _isArrayLike([]); //=> true
313 * _isArrayLike(true); //=> false
314 * _isArrayLike({}); //=> false
315 * _isArrayLike({length: 10}); //=> false
316 * _isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
317 */
318var _isArrayLike = /*#__PURE__*/_curry1(function isArrayLike(x) {
319 if (_isArray(x)) {
320 return true;
321 }
322 if (!x) {
323 return false;
324 }
325 if (typeof x !== 'object') {
326 return false;
327 }
328 if (_isString(x)) {
329 return false;
330 }
331 if (x.nodeType === 1) {
332 return !!x.length;
333 }
334 if (x.length === 0) {
335 return true;
336 }
337 if (x.length > 0) {
338 return x.hasOwnProperty(0) && x.hasOwnProperty(x.length - 1);
339 }
340 return false;
341});
342
343var XWrap = /*#__PURE__*/function () {
344 function XWrap(fn) {
345 this.f = fn;
346 }
347 XWrap.prototype['@@transducer/init'] = function () {
348 throw new Error('init not implemented on XWrap');
349 };
350 XWrap.prototype['@@transducer/result'] = function (acc) {
351 return acc;
352 };
353 XWrap.prototype['@@transducer/step'] = function (acc, x) {
354 return this.f(acc, x);
355 };
356
357 return XWrap;
358}();
359
360function _xwrap(fn) {
361 return new XWrap(fn);
362}
363
364/**
365 * Creates a function that is bound to a context.
366 * Note: `R.bind` does not provide the additional argument-binding capabilities of
367 * [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
368 *
369 * @func
370 * @memberOf R
371 * @since v0.6.0
372 * @category Function
373 * @category Object
374 * @sig (* -> *) -> {*} -> (* -> *)
375 * @param {Function} fn The function to bind to context
376 * @param {Object} thisObj The context to bind `fn` to
377 * @return {Function} A function that will execute in the context of `thisObj`.
378 * @see R.partial
379 * @example
380 *
381 * const log = R.bind(console.log, console);
382 * R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3}
383 * // logs {a: 2}
384 * @symb R.bind(f, o)(a, b) = f.call(o, a, b)
385 */
386var bind = /*#__PURE__*/_curry2(function bind(fn, thisObj) {
387 return _arity(fn.length, function () {
388 return fn.apply(thisObj, arguments);
389 });
390});
391
392function _arrayReduce(xf, acc, list) {
393 var idx = 0;
394 var len = list.length;
395 while (idx < len) {
396 acc = xf['@@transducer/step'](acc, list[idx]);
397 if (acc && acc['@@transducer/reduced']) {
398 acc = acc['@@transducer/value'];
399 break;
400 }
401 idx += 1;
402 }
403 return xf['@@transducer/result'](acc);
404}
405
406function _iterableReduce(xf, acc, iter) {
407 var step = iter.next();
408 while (!step.done) {
409 acc = xf['@@transducer/step'](acc, step.value);
410 if (acc && acc['@@transducer/reduced']) {
411 acc = acc['@@transducer/value'];
412 break;
413 }
414 step = iter.next();
415 }
416 return xf['@@transducer/result'](acc);
417}
418
419function _methodReduce(xf, acc, obj, methodName) {
420 return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc));
421}
422
423var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
424
425function _reduce(fn, acc, list) {
426 if (typeof fn === 'function') {
427 fn = _xwrap(fn);
428 }
429 if (_isArrayLike(list)) {
430 return _arrayReduce(fn, acc, list);
431 }
432 if (typeof list['fantasy-land/reduce'] === 'function') {
433 return _methodReduce(fn, acc, list, 'fantasy-land/reduce');
434 }
435 if (list[symIterator] != null) {
436 return _iterableReduce(fn, acc, list[symIterator]());
437 }
438 if (typeof list.next === 'function') {
439 return _iterableReduce(fn, acc, list);
440 }
441 if (typeof list.reduce === 'function') {
442 return _methodReduce(fn, acc, list, 'reduce');
443 }
444
445 throw new TypeError('reduce: list must be array or iterable');
446}
447
448var XMap = /*#__PURE__*/function () {
449 function XMap(f, xf) {
450 this.xf = xf;
451 this.f = f;
452 }
453 XMap.prototype['@@transducer/init'] = _xfBase.init;
454 XMap.prototype['@@transducer/result'] = _xfBase.result;
455 XMap.prototype['@@transducer/step'] = function (result, input) {
456 return this.xf['@@transducer/step'](result, this.f(input));
457 };
458
459 return XMap;
460}();
461
462var _xmap = /*#__PURE__*/_curry2(function _xmap(f, xf) {
463 return new XMap(f, xf);
464});
465
466function _has(prop, obj) {
467 return Object.prototype.hasOwnProperty.call(obj, prop);
468}
469
470var toString = Object.prototype.toString;
471var _isArguments = /*#__PURE__*/function () {
472 return toString.call(arguments) === '[object Arguments]' ? function _isArguments(x) {
473 return toString.call(x) === '[object Arguments]';
474 } : function _isArguments(x) {
475 return _has('callee', x);
476 };
477}();
478
479// cover IE < 9 keys issues
480var hasEnumBug = ! /*#__PURE__*/{ toString: null }.propertyIsEnumerable('toString');
481var nonEnumerableProps = ['constructor', 'valueOf', 'isPrototypeOf', 'toString', 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
482// Safari bug
483var hasArgsEnumBug = /*#__PURE__*/function () {
484
485 return arguments.propertyIsEnumerable('length');
486}();
487
488var contains = function contains(list, item) {
489 var idx = 0;
490 while (idx < list.length) {
491 if (list[idx] === item) {
492 return true;
493 }
494 idx += 1;
495 }
496 return false;
497};
498
499/**
500 * Returns a list containing the names of all the enumerable own properties of
501 * the supplied object.
502 * Note that the order of the output array is not guaranteed to be consistent
503 * across different JS platforms.
504 *
505 * @func
506 * @memberOf R
507 * @since v0.1.0
508 * @category Object
509 * @sig {k: v} -> [k]
510 * @param {Object} obj The object to extract properties from
511 * @return {Array} An array of the object's own properties.
512 * @see R.keysIn, R.values
513 * @example
514 *
515 * R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c']
516 */
517var keys = typeof Object.keys === 'function' && !hasArgsEnumBug ? /*#__PURE__*/_curry1(function keys(obj) {
518 return Object(obj) !== obj ? [] : Object.keys(obj);
519}) : /*#__PURE__*/_curry1(function keys(obj) {
520 if (Object(obj) !== obj) {
521 return [];
522 }
523 var prop, nIdx;
524 var ks = [];
525 var checkArgsLength = hasArgsEnumBug && _isArguments(obj);
526 for (prop in obj) {
527 if (_has(prop, obj) && (!checkArgsLength || prop !== 'length')) {
528 ks[ks.length] = prop;
529 }
530 }
531 if (hasEnumBug) {
532 nIdx = nonEnumerableProps.length - 1;
533 while (nIdx >= 0) {
534 prop = nonEnumerableProps[nIdx];
535 if (_has(prop, obj) && !contains(ks, prop)) {
536 ks[ks.length] = prop;
537 }
538 nIdx -= 1;
539 }
540 }
541 return ks;
542});
543
544/**
545 * Takes a function and
546 * a [functor](https://github.com/fantasyland/fantasy-land#functor),
547 * applies the function to each of the functor's values, and returns
548 * a functor of the same shape.
549 *
550 * Ramda provides suitable `map` implementations for `Array` and `Object`,
551 * so this function may be applied to `[1, 2, 3]` or `{x: 1, y: 2, z: 3}`.
552 *
553 * Dispatches to the `map` method of the second argument, if present.
554 *
555 * Acts as a transducer if a transformer is given in list position.
556 *
557 * Also treats functions as functors and will compose them together.
558 *
559 * @func
560 * @memberOf R
561 * @since v0.1.0
562 * @category List
563 * @sig Functor f => (a -> b) -> f a -> f b
564 * @param {Function} fn The function to be called on every element of the input `list`.
565 * @param {Array} list The list to be iterated over.
566 * @return {Array} The new list.
567 * @see R.transduce, R.addIndex
568 * @example
569 *
570 * const double = x => x * 2;
571 *
572 * R.map(double, [1, 2, 3]); //=> [2, 4, 6]
573 *
574 * R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
575 * @symb R.map(f, [a, b]) = [f(a), f(b)]
576 * @symb R.map(f, { x: a, y: b }) = { x: f(a), y: f(b) }
577 * @symb R.map(f, functor_o) = functor_o.map(f)
578 */
579var map = /*#__PURE__*/_curry2( /*#__PURE__*/_dispatchable(['fantasy-land/map', 'map'], _xmap, function map(fn, functor) {
580 switch (Object.prototype.toString.call(functor)) {
581 case '[object Function]':
582 return curryN(functor.length, function () {
583 return fn.call(this, functor.apply(this, arguments));
584 });
585 case '[object Object]':
586 return _reduce(function (acc, key) {
587 acc[key] = fn(functor[key]);
588 return acc;
589 }, {}, keys(functor));
590 default:
591 return _map(fn, functor);
592 }
593}));
594
595/**
596 * ap applies a list of functions to a list of values.
597 *
598 * Dispatches to the `ap` method of the second argument, if present. Also
599 * treats curried functions as applicatives.
600 *
601 * @func
602 * @memberOf R
603 * @since v0.3.0
604 * @category Function
605 * @sig [a -> b] -> [a] -> [b]
606 * @sig Apply f => f (a -> b) -> f a -> f b
607 * @sig (r -> a -> b) -> (r -> a) -> (r -> b)
608 * @param {*} applyF
609 * @param {*} applyX
610 * @return {*}
611 * @example
612 *
613 * R.ap([R.multiply(2), R.add(3)], [1,2,3]); //=> [2, 4, 6, 4, 5, 6]
614 * R.ap([R.concat('tasty '), R.toUpper], ['pizza', 'salad']); //=> ["tasty pizza", "tasty salad", "PIZZA", "SALAD"]
615 *
616 * // R.ap can also be used as S combinator
617 * // when only two functions are passed
618 * R.ap(R.concat, R.toUpper)('Ramda') //=> 'RamdaRAMDA'
619 * @symb R.ap([f, g], [a, b]) = [f(a), f(b), g(a), g(b)]
620 */
621var ap = /*#__PURE__*/_curry2(function ap(applyF, applyX) {
622 return typeof applyX['fantasy-land/ap'] === 'function' ? applyX['fantasy-land/ap'](applyF) : typeof applyF.ap === 'function' ? applyF.ap(applyX) : typeof applyF === 'function' ? function (x) {
623 return applyF(x)(applyX(x));
624 } : _reduce(function (acc, f) {
625 return _concat(acc, map(f, applyX));
626 }, [], applyF);
627});
628
629function _isFunction(x) {
630 return Object.prototype.toString.call(x) === '[object Function]';
631}
632
633/**
634 * "lifts" a function to be the specified arity, so that it may "map over" that
635 * many lists, Functions or other objects that satisfy the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
636 *
637 * @func
638 * @memberOf R
639 * @since v0.7.0
640 * @category Function
641 * @sig Number -> (*... -> *) -> ([*]... -> [*])
642 * @param {Function} fn The function to lift into higher context
643 * @return {Function} The lifted function.
644 * @see R.lift, R.ap
645 * @example
646 *
647 * const madd3 = R.liftN(3, (...args) => R.sum(args));
648 * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
649 */
650var liftN = /*#__PURE__*/_curry2(function liftN(arity, fn) {
651 var lifted = curryN(arity, fn);
652 return curryN(arity, function () {
653 return _reduce(ap, map(lifted, arguments[0]), Array.prototype.slice.call(arguments, 1));
654 });
655});
656
657/**
658 * "lifts" a function of arity > 1 so that it may "map over" a list, Function or other
659 * object that satisfies the [FantasyLand Apply spec](https://github.com/fantasyland/fantasy-land#apply).
660 *
661 * @func
662 * @memberOf R
663 * @since v0.7.0
664 * @category Function
665 * @sig (*... -> *) -> ([*]... -> [*])
666 * @param {Function} fn The function to lift into higher context
667 * @return {Function} The lifted function.
668 * @see R.liftN
669 * @example
670 *
671 * const madd3 = R.lift((a, b, c) => a + b + c);
672 *
673 * madd3([1,2,3], [1,2,3], [1]); //=> [3, 4, 5, 4, 5, 6, 5, 6, 7]
674 *
675 * const madd5 = R.lift((a, b, c, d, e) => a + b + c + d + e);
676 *
677 * madd5([1,2], [3], [4, 5], [6], [7, 8]); //=> [21, 22, 22, 23, 22, 23, 23, 24]
678 */
679var lift = /*#__PURE__*/_curry1(function lift(fn) {
680 return liftN(fn.length, fn);
681});
682
683/**
684 * Returns `true` if one or both of its arguments are `true`. Returns `false`
685 * if both arguments are `false`.
686 *
687 * @func
688 * @memberOf R
689 * @since v0.1.0
690 * @category Logic
691 * @sig a -> b -> a | b
692 * @param {Any} a
693 * @param {Any} b
694 * @return {Any} the first argument if truthy, otherwise the second argument.
695 * @see R.either
696 * @example
697 *
698 * R.or(true, true); //=> true
699 * R.or(true, false); //=> true
700 * R.or(false, true); //=> true
701 * R.or(false, false); //=> false
702 */
703var or = /*#__PURE__*/_curry2(function or(a, b) {
704 return a || b;
705});
706
707/**
708 * A function wrapping calls to the two functions in an `||` operation,
709 * returning the result of the first function if it is truth-y and the result
710 * of the second function otherwise. Note that this is short-circuited,
711 * meaning that the second function will not be invoked if the first returns a
712 * truth-y value.
713 *
714 * In addition to functions, `R.either` also accepts any fantasy-land compatible
715 * applicative functor.
716 *
717 * @func
718 * @memberOf R
719 * @since v0.12.0
720 * @category Logic
721 * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
722 * @param {Function} f a predicate
723 * @param {Function} g another predicate
724 * @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
725 * @see R.or
726 * @example
727 *
728 * const gt10 = x => x > 10;
729 * const even = x => x % 2 === 0;
730 * const f = R.either(gt10, even);
731 * f(101); //=> true
732 * f(8); //=> true
733 *
734 * R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
735 * R.either([false, false, 'a'], [11]) // => [11, 11, "a"]
736 */
737var either = /*#__PURE__*/_curry2(function either(f, g) {
738 return _isFunction(f) ? function _either() {
739 return f.apply(this, arguments) || g.apply(this, arguments);
740 } : lift(or)(f, g);
741});
742
743/** Detect free variable `global` from Node.js. */
744var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
745
746/** Detect free variable `self`. */
747var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
748
749/** Used as a reference to the global object. */
750var root = freeGlobal || freeSelf || Function('return this')();
751
752/** Built-in value references. */
753var Symbol$1 = root.Symbol;
754
755/** Used for built-in method references. */
756var objectProto = Object.prototype;
757
758/** Used to check objects for own properties. */
759var hasOwnProperty = objectProto.hasOwnProperty;
760
761/**
762 * Used to resolve the
763 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
764 * of values.
765 */
766var nativeObjectToString = objectProto.toString;
767
768/** Built-in value references. */
769var symToStringTag = Symbol$1 ? Symbol$1.toStringTag : undefined;
770
771/**
772 * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.
773 *
774 * @private
775 * @param {*} value The value to query.
776 * @returns {string} Returns the raw `toStringTag`.
777 */
778function getRawTag(value) {
779 var isOwn = hasOwnProperty.call(value, symToStringTag),
780 tag = value[symToStringTag];
781
782 try {
783 value[symToStringTag] = undefined;
784 var unmasked = true;
785 } catch (e) {}
786
787 var result = nativeObjectToString.call(value);
788 if (unmasked) {
789 if (isOwn) {
790 value[symToStringTag] = tag;
791 } else {
792 delete value[symToStringTag];
793 }
794 }
795 return result;
796}
797
798/** Used for built-in method references. */
799var objectProto$1 = Object.prototype;
800
801/**
802 * Used to resolve the
803 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
804 * of values.
805 */
806var nativeObjectToString$1 = objectProto$1.toString;
807
808/**
809 * Converts `value` to a string using `Object.prototype.toString`.
810 *
811 * @private
812 * @param {*} value The value to convert.
813 * @returns {string} Returns the converted string.
814 */
815function objectToString(value) {
816 return nativeObjectToString$1.call(value);
817}
818
819/** `Object#toString` result references. */
820var nullTag = '[object Null]',
821 undefinedTag = '[object Undefined]';
822
823/** Built-in value references. */
824var symToStringTag$1 = Symbol$1 ? Symbol$1.toStringTag : undefined;
825
826/**
827 * The base implementation of `getTag` without fallbacks for buggy environments.
828 *
829 * @private
830 * @param {*} value The value to query.
831 * @returns {string} Returns the `toStringTag`.
832 */
833function baseGetTag(value) {
834 if (value == null) {
835 return value === undefined ? undefinedTag : nullTag;
836 }
837 return (symToStringTag$1 && symToStringTag$1 in Object(value))
838 ? getRawTag(value)
839 : objectToString(value);
840}
841
842/**
843 * Checks if `value` is the
844 * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
845 * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
846 *
847 * @static
848 * @memberOf _
849 * @since 0.1.0
850 * @category Lang
851 * @param {*} value The value to check.
852 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
853 * @example
854 *
855 * _.isObject({});
856 * // => true
857 *
858 * _.isObject([1, 2, 3]);
859 * // => true
860 *
861 * _.isObject(_.noop);
862 * // => true
863 *
864 * _.isObject(null);
865 * // => false
866 */
867function isObject(value) {
868 var type = typeof value;
869 return value != null && (type == 'object' || type == 'function');
870}
871
872/** `Object#toString` result references. */
873var asyncTag = '[object AsyncFunction]',
874 funcTag = '[object Function]',
875 genTag = '[object GeneratorFunction]',
876 proxyTag = '[object Proxy]';
877
878/**
879 * Checks if `value` is classified as a `Function` object.
880 *
881 * @static
882 * @memberOf _
883 * @since 0.1.0
884 * @category Lang
885 * @param {*} value The value to check.
886 * @returns {boolean} Returns `true` if `value` is a function, else `false`.
887 * @example
888 *
889 * _.isFunction(_);
890 * // => true
891 *
892 * _.isFunction(/abc/);
893 * // => false
894 */
895function isFunction(value) {
896 if (!isObject(value)) {
897 return false;
898 }
899 // The use of `Object#toString` avoids issues with the `typeof` operator
900 // in Safari 9 which returns 'object' for typed arrays and other constructors.
901 var tag = baseGetTag(value);
902 return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
903}
904
905/**
906 * Utility function mapping a Rollup config's `external` option into a function.
907
908 * In Rollup, the `external` config option can be provided as an array or a function.
909 * https://rollupjs.org/#peer-dependencies
910 *
911 * An `external` configuration in array format can be represented in the function
912 * format, but not vice-versa. This utility accepts either format and returns the
913 * function representation.
914 *
915 * @param {Array|Function} external The `external` property from Rollup's config.
916 * @returns {Function} Function equivalent of the passed in `external`.
917 */
918
919function externalToFn(external) {
920 if (isFunction(external)) {
921 return external;
922 } else if (Array.isArray(external)) {
923 return function (id) {
924 return external.some(function (module) {
925 return module === id;
926 });
927 };
928 } else {
929 throw new Error("rollup-plugin-peer-deps-external: 'external' option must be a function or an array.");
930 }
931}
932
933/**
934 * Creates a test function from a list of module names. The resulting function
935 * accepts a string id and returns whether it matches a module in the list.
936 *
937 * The string id can be a module name (e.g. `lodash`) or a
938 * "module path" (e.g. `lodash/map`).
939 *
940 * @param {Array} modulesNames Array of module names to match against.
941 * @returns {function(String): (boolean)} Predicate function accepting a string id.
942 */
943function getModulesMatcher(modulesNames) {
944 var regexps = modulesNames.map(moduleRegExp);
945 return function (id) {
946 return regexps.some(function (regexp) {
947 return regexp.test(id);
948 });
949 };
950}
951
952var moduleRegExp = function moduleRegExp(module) {
953 return new RegExp("^".concat(module, "(\\/.+)*$"));
954};
955
956var _require = require('path'),
957 resolve = _require.resolve;
958
959function getDeps() {
960 var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : resolve(process.cwd(), 'package.json');
961 var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'peerDependencies';
962
963 try {
964 var pkg = require(path);
965
966 return Object.keys(pkg[type]);
967 } catch (err) {
968 return [];
969 }
970}
971
972function PeerDepsExternalPlugin() {
973 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
974 packageJsonPath = _ref.packageJsonPath,
975 includeDependencies = _ref.includeDependencies;
976
977 return {
978 name: 'peer-deps-external',
979 options: function options(opts) {
980 opts.external = either( // Retain existing `external` config
981 externalToFn(opts.external), // Add `peerDependencies` to `external` config
982 getModulesMatcher(getDeps(packageJsonPath, 'peerDependencies').concat(includeDependencies ? getDeps(packageJsonPath, 'dependencies') : [])));
983 return opts;
984 }
985 };
986}
987
988module.exports = PeerDepsExternalPlugin;