From 5ede05a717ef7cf5c0c4aa218420c0e180d120f5 Mon Sep 17 00:00:00 2001 From: paldepind Date: Mon, 22 Jun 2015 12:09:56 +0200 Subject: [PATCH] Distribute dependency free build #2 --- dist/union-type.js | 269 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 9 +- test/index.js | 2 +- 3 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 dist/union-type.js diff --git a/dist/union-type.js b/dist/union-type.js new file mode 100644 index 0000000..b9e62fc --- /dev/null +++ b/dist/union-type.js @@ -0,0 +1,269 @@ +(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 *)) -> (* -> *) + * @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'); + +function isString(s) { return typeof s === 'string'; } +function isNumber(n) { return typeof n === 'number'; } +function isObject(value) { + var type = typeof value; + return !!value && (type == 'object' || type == 'function'); +} +function isFunction(f) { return typeof f === 'function'; } +var isArray = Array.isArray || function(a) { return 'length' in a; }; + +var mapConstrToFn = curryN(2, function(group, constr) { + return constr === String ? isString + : constr === Number ? isNumber + : constr === Object ? isObject + : constr === Array ? isArray + : constr === Function ? isFunction + : constr === undefined ? group + : constr; +}); + +function Constructor(group, name, validators) { + validators = validators.map(mapConstrToFn(group)); + var constructor = curryN(validators.length, function() { + var val = [], v, validator; + for (var i = 0; i < arguments.length; ++i) { + v = arguments[i]; + validator = validators[i]; + if ((typeof validator === 'function' && validator(v)) || + (v !== undefined && v !== null && v.of === validator)) { + val[i] = arguments[i]; + } else { + throw new TypeError('wrong value ' + v + ' passed to location ' + i + ' in ' + name); + } + } + val.of = group; + val.name = name; + return val; + }); + return constructor; +} + +function rawCase(type, cases, action, arg) { + if (type !== action.of) throw new TypeError('wrong type passed to case'); + var name = action.name in cases ? action.name + : '_' in cases ? '_' + : undefined; + if (name === undefined) { + throw new Error('unhandled value passed to case'); + } else { + return cases[name].apply(undefined, arg !== undefined ? action.concat([arg]) : action); + } +} + +var typeCase = curryN(3, rawCase); +var caseOn = curryN(4, rawCase); + +function Type(desc) { + var obj = {}; + for (var key in desc) { + obj[key] = Constructor(obj, key, desc[key]); + } + obj.case = typeCase(obj); + obj.caseOn = caseOn(obj); + return obj; +} + +module.exports = Type; + +},{"ramda/src/curryN":2}]},{},[6])(6) +}); \ No newline at end of file diff --git a/package.json b/package.json index 091e692..990307e 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,14 @@ "test": "test" }, "dependencies": { - "ramda": "^0.14.0" + "ramda": "^0.15.0" + }, + "devDependencies": { + "browserify": "^10.2.4" }, - "devDependencies": {}, "scripts": { - "test": "mocha" + "test": "mocha", + "build": "browserify -s unionType ./union-type.js -o dist/union-type.js" }, "repository": { "type": "git", diff --git a/test/index.js b/test/index.js index d694f8c..fe26642 100644 --- a/test/index.js +++ b/test/index.js @@ -3,7 +3,7 @@ var Type = require('../union-type.js'); function isNumber(n) { return typeof n === 'number'; } -function T() { return true; }; +function T() { return true; } describe('union type', function() { it('returns type with constructors', function() {