union-type
Version:
Union types for JavaScript
331 lines (304 loc) • 11.3 kB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.unionType = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var _curry2 = require('./internal/_curry2');
/**
* Wraps a function of any arity (including nullary) in a function that accepts exactly `n`
* parameters. Unlike `nAry`, which passes only `n` arguments to the wrapped function,
* functions produced by `arity` will pass all provided arguments to the wrapped function.
*
* @func
* @memberOf R
* @sig (Number, (* -> *)) -> (* -> *)
* @category Function
* @param {Number} n The desired arity of the returned function.
* @param {Function} fn The function to wrap.
* @return {Function} A new function wrapping `fn`. The new function is
* guaranteed to be of arity `n`.
* @deprecated since v0.15.0
* @example
*
* var takesTwoArgs = function(a, b) {
* return [a, b];
* };
* takesTwoArgs.length; //=> 2
* takesTwoArgs(1, 2); //=> [1, 2]
*
* var takesOneArg = R.arity(1, takesTwoArgs);
* takesOneArg.length; //=> 1
* // All arguments are passed through to the wrapped function
* takesOneArg(1, 2); //=> [1, 2]
*/
module.exports = _curry2(function(n, fn) {
// jshint unused:vars
switch (n) {
case 0: return function() {return fn.apply(this, arguments);};
case 1: return function(a0) {return fn.apply(this, arguments);};
case 2: return function(a0, a1) {return fn.apply(this, arguments);};
case 3: return function(a0, a1, a2) {return fn.apply(this, arguments);};
case 4: return function(a0, a1, a2, a3) {return fn.apply(this, arguments);};
case 5: return function(a0, a1, a2, a3, a4) {return fn.apply(this, arguments);};
case 6: return function(a0, a1, a2, a3, a4, a5) {return fn.apply(this, arguments);};
case 7: return function(a0, a1, a2, a3, a4, a5, a6) {return fn.apply(this, arguments);};
case 8: return function(a0, a1, a2, a3, a4, a5, a6, a7) {return fn.apply(this, arguments);};
case 9: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8) {return fn.apply(this, arguments);};
case 10: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {return fn.apply(this, arguments);};
default: throw new Error('First argument to arity must be a non-negative integer no greater than ten');
}
});
},{"./internal/_curry2":4}],2:[function(require,module,exports){
var _curry2 = require('./internal/_curry2');
var _curryN = require('./internal/_curryN');
var arity = require('./arity');
/**
* Returns a curried equivalent of the provided function, with the
* specified arity. The curried function has two unusual capabilities.
* First, its arguments needn't be provided one at a time. If `g` is
* `R.curryN(3, f)`, the following are equivalent:
*
* - `g(1)(2)(3)`
* - `g(1)(2, 3)`
* - `g(1, 2)(3)`
* - `g(1, 2, 3)`
*
* Secondly, the special placeholder value `R.__` may be used to specify
* "gaps", allowing partial application of any combination of arguments,
* regardless of their positions. If `g` is as above and `_` is `R.__`,
* the following are equivalent:
*
* - `g(1, 2, 3)`
* - `g(_, 2, 3)(1)`
* - `g(_, _, 3)(1)(2)`
* - `g(_, _, 3)(1, 2)`
* - `g(_, 2)(1)(3)`
* - `g(_, 2)(1, 3)`
* - `g(_, 2)(_, 3)(1)`
*
* @func
* @memberOf R
* @category Function
* @sig Number -> (* -> a) -> (* -> a)
* @param {Number} length The arity for the returned function.
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @see R.curry
* @example
*
* var addFourNumbers = function() {
* return R.sum([].slice.call(arguments, 0, 4));
* };
*
* var curriedAddFourNumbers = R.curryN(4, addFourNumbers);
* var f = curriedAddFourNumbers(1, 2);
* var g = f(3);
* g(4); //=> 10
*/
module.exports = _curry2(function curryN(length, fn) {
return arity(length, _curryN(length, [], fn));
});
},{"./arity":1,"./internal/_curry2":4,"./internal/_curryN":5}],3:[function(require,module,exports){
/**
* Optimized internal two-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
module.exports = function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0) {
return f1;
} else if (a != null && a['@@functional/placeholder'] === true) {
return f1;
} else {
return fn(a);
}
};
};
},{}],4:[function(require,module,exports){
var _curry1 = require('./_curry1');
/**
* Optimized internal two-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
module.exports = function _curry2(fn) {
return function f2(a, b) {
var n = arguments.length;
if (n === 0) {
return f2;
} else if (n === 1 && a != null && a['@@functional/placeholder'] === true) {
return f2;
} else if (n === 1) {
return _curry1(function(b) { return fn(a, b); });
} else if (n === 2 && a != null && a['@@functional/placeholder'] === true &&
b != null && b['@@functional/placeholder'] === true) {
return f2;
} else if (n === 2 && a != null && a['@@functional/placeholder'] === true) {
return _curry1(function(a) { return fn(a, b); });
} else if (n === 2 && b != null && b['@@functional/placeholder'] === true) {
return _curry1(function(b) { return fn(a, b); });
} else {
return fn(a, b);
}
};
};
},{"./_curry1":3}],5:[function(require,module,exports){
var arity = require('../arity');
/**
* Internal curryN function.
*
* @private
* @category Function
* @param {Number} length The arity of the curried function.
* @return {array} An array of arguments received thus far.
* @param {Function} fn The function to curry.
*/
module.exports = function _curryN(length, received, fn) {
return function() {
var combined = [];
var argsIdx = 0;
var left = length;
var combinedIdx = 0;
while (combinedIdx < received.length || argsIdx < arguments.length) {
var result;
if (combinedIdx < received.length &&
(received[combinedIdx] == null ||
received[combinedIdx]['@@functional/placeholder'] !== true ||
argsIdx >= arguments.length)) {
result = received[combinedIdx];
} else {
result = arguments[argsIdx];
argsIdx += 1;
}
combined[combinedIdx] = result;
if (result == null || result['@@functional/placeholder'] !== true) {
left -= 1;
}
combinedIdx += 1;
}
return left <= 0 ? fn.apply(this, combined) : arity(left, _curryN(length, combined, fn));
};
};
},{"../arity":1}],6:[function(require,module,exports){
var curryN = require('ramda/src/curryN');
var isString = function(s) { return typeof s === 'string'; };
var isNumber = function(n) { return typeof n === 'number'; };
var isBoolean = function(b) { return typeof b === 'boolean'; };
var isObject = function(value) {
var type = typeof value;
return !!value && (type == 'object' || type == 'function');
};
var isFunction = function(f) { return typeof f === 'function'; };
var isArray = Array.isArray || function(a) { return 'length' in a; };
var mapConstrToFn = function(group, constr) {
return constr === String ? isString
: constr === Number ? isNumber
: constr === Boolean ? isBoolean
: constr === Object ? isObject
: constr === Array ? isArray
: constr === Function ? isFunction
: constr === undefined ? group
: constr;
};
var numToStr = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth'];
var validate = function(group, validators, name, args) {
var validator, v, i;
for (i = 0; i < args.length; ++i) {
v = args[i];
validator = mapConstrToFn(group, validators[i]);
if (Type.check === true &&
(validator.prototype === undefined || !validator.prototype.isPrototypeOf(v)) &&
(typeof validator !== 'function' || !validator(v))) {
throw new TypeError('wrong value ' + v + ' passed to location ' + numToStr[i] + ' in ' + name);
}
}
};
function valueToArray(value) {
var i, arr = [];
for (i = 0; i < value.keys.length; ++i) {
arr.push(value[value.keys[i]]);
}
return arr;
}
function extractValues(keys, obj) {
var arr = [], i;
for (i = 0; i < keys.length; ++i) arr[i] = obj[keys[i]];
return arr;
}
function constructor(group, name, fields) {
var validators, keys = Object.keys(fields), i;
if (isArray(fields)) {
validators = fields;
} else {
validators = extractValues(keys, fields);
}
function construct() {
var val = Object.create(group.prototype), i;
val.keys = keys;
val.name = name;
if (Type.check === true) {
validate(group, validators, name, arguments);
}
for (i = 0; i < arguments.length; ++i) {
val[keys[i]] = arguments[i];
}
return val;
}
group[name] = curryN(keys.length, construct);
if (keys !== undefined) {
group[name+'Of'] = function(obj) {
return construct.apply(undefined, extractValues(keys, obj));
};
}
}
function rawCase(type, cases, value, arg) {
var wildcard = false;
var handler = cases[value.name];
if (handler === undefined) {
handler = cases['_'];
wildcard = true;
}
if (Type.check === true) {
if (!type.prototype.isPrototypeOf(value)) {
throw new TypeError('wrong type passed to case');
} else if (handler === undefined) {
throw new Error('non-exhaustive patterns in a function');
}
}
var args = wildcard === true ? [arg]
: arg !== undefined ? valueToArray(value).concat([arg])
: valueToArray(value);
return handler.apply(undefined, args);
}
var typeCase = curryN(3, rawCase);
var caseOn = curryN(4, rawCase);
function createIterator() {
return {
idx: 0,
val: this,
next: function() {
var keys = this.val.keys;
return this.idx === keys.length
? {done: true}
: {value: this.val[keys[this.idx++]]};
}
};
}
function Type(desc) {
var key, res, obj = {};
obj.prototype = {};
obj.prototype[Symbol ? Symbol.iterator : '@@iterator'] = createIterator;
obj.case = typeCase(obj);
obj.caseOn = caseOn(obj);
for (key in desc) {
res = constructor(obj, key, desc[key]);
}
return obj;
}
Type.check = true;
module.exports = Type;
},{"ramda/src/curryN":2}]},{},[6])(6)
});