Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

union-type

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

union-type - npm Package Compare versions

Comparing version 0.1.6 to 0.2.0

.npmignore

138

dist/union-type.js
(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){
// shim for using process in browser
var process = module.exports = {};
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function cleanUpNextTick() {
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
}
var timeout = setTimeout(cleanUpNextTick);
draining = true;
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
currentQueue[queueIndex].run();
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
clearTimeout(timeout);
}
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
setTimeout(drainQueue, 0);
}
};
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
// TODO(shtylman)
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function() { return 0; };
},{}],2:[function(require,module,exports){
var _curry2 = require('./internal/_curry2');

@@ -50,3 +142,3 @@

},{"./internal/_curry2":4}],2:[function(require,module,exports){
},{"./internal/_curry2":5}],3:[function(require,module,exports){
var _curry2 = require('./internal/_curry2');

@@ -104,3 +196,3 @@ var _curryN = require('./internal/_curryN');

},{"./arity":1,"./internal/_curry2":4,"./internal/_curryN":5}],3:[function(require,module,exports){
},{"./arity":2,"./internal/_curry2":5,"./internal/_curryN":6}],4:[function(require,module,exports){
/**

@@ -126,3 +218,3 @@ * Optimized internal two-arity curry function.

},{}],4:[function(require,module,exports){
},{}],5:[function(require,module,exports){
var _curry1 = require('./_curry1');

@@ -161,3 +253,3 @@

},{"./_curry1":3}],5:[function(require,module,exports){
},{"./_curry1":4}],6:[function(require,module,exports){
var arity = require('../arity');

@@ -202,3 +294,4 @@

},{"../arity":1}],6:[function(require,module,exports){
},{"../arity":2}],7:[function(require,module,exports){
(function (process){
var curryN = require('ramda/src/curryN');

@@ -215,3 +308,3 @@

var mapConstrToFn = curryN(2, function(group, constr) {
var mapConstrToFn = function(group, constr) {
return constr === String ? isString

@@ -224,16 +317,19 @@ : constr === Number ? isNumber

: 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);
return curryN(validators.length, function() {
var val = [], v, validator, i;
if (Type.disableChecking === true) {
for (i = 0; i < arguments.length; ++i) val[i] = arguments[i];
} else {
for (i = 0; i < arguments.length; ++i) {
v = arguments[i];
validator = mapConstrToFn(group, validators[i]);
if ((typeof validator === 'function' && validator(v)) ||
(v !== undefined && v !== null && v.of === validator)) {
val[i] = v;
} else {
throw new TypeError('wrong value ' + v + ' passed to location ' + i + ' in ' + name);
}
}

@@ -245,3 +341,2 @@ }

});
return constructor;
}

@@ -274,5 +369,8 @@

Type.disableChecking = process !== undefined && process.env.NODE_ENV === 'production';
module.exports = Type;
},{"ramda/src/curryN":2}]},{},[6])(6)
}).call(this,require('_process'))
},{"_process":1,"ramda/src/curryN":3}]},{},[7])(7)
});

13

package.json
{
"name": "union-type",
"version": "0.1.6",
"version": "0.2.0",
"description": "Union types for JavaScript",

@@ -13,7 +13,12 @@ "main": "union-type.js",

"devDependencies": {
"browserify": "^10.2.4"
"browserify": "^10.2.4",
"mocha": "^2.3.4",
"xyz": "0.5.x"
},
"scripts": {
"test": "mocha",
"build": "browserify -s unionType ./union-type.js -o dist/union-type.js"
"build": "browserify -s unionType ./union-type.js -o dist/union-type.js",
"release-major": "xyz --repo git@github.com:paldepind/union-type.git --increment major",
"release-minor": "xyz --repo git@github.com:paldepind/union-type.git --increment minor",
"release-patch": "xyz --repo git@github.com:paldepind/union-type.git --increment patch",
"test": "mocha"
},

@@ -20,0 +25,0 @@ "repository": {

@@ -97,8 +97,8 @@ # union-type

```javascript
var Move = Type({Up: [], Right: [], Down: [], Left: []});
var Action = Type({Up: [], Right: [], Down: [], Left: [], Jump: [], Fire: [Number]});
var player = {x: 0, y: 0};
var advancePlayer = function(move, player) {
return Move.case({
var advancePlayer = function(action, player) {
return Action.case({
Up: function() { return {x: player.x, y: player.y - 1}; },

@@ -108,3 +108,4 @@ Right: function() { return {x: player.x + 1, y: player.y}; },

Left: function() { return {x: player.x - 1, y: player.y}; },
}, move);
_: function() { return player; }
}, action);
};

@@ -116,4 +117,4 @@ ```

```javascript
const advancePlayer = (move, player) =>
Move.case({
const advancePlayer = (action, player) =>
Action.case({
Up: () => ({x: player.x, y: player.y - 1}),

@@ -123,3 +124,4 @@ Right: () => ({x: player.x + 1, y: player.y}),

Left: () => ({x: player.x - 1, y: player.y}),
}, move);
_: () => player,
}, action);
```

@@ -137,3 +139,3 @@

Circle: (radius, _) => Math.PI * radius * radius,
Rectangle: (p1, p2) => (p2[0] - p1[0]) * (p2[1] - p1[0])
Rectangle: (p1, p2) => (p2[0] - p1[0]) * (p2[1] - p1[1])
}, shape);

@@ -148,3 +150,3 @@ ```

Circle: (radius, _) => Math.PI * radius * radius,
Rectangle: (p1, p2) => (p2[0] - p1[0]) * (p2[1] - p1[0])
Rectangle: (p1, p2) => (p2[0] - p1[0]) * (p2[1] - p1[1])
});

@@ -159,18 +161,21 @@ ```

// No need to wrap this into a function that passes `player`
const advancePlayer = Move.caseOn({
const advancePlayer = Action.caseOn({
Up: (player) => ({x: player.x, y: player.y - 1}),
Right: (player) => ({x: player.x + 1, y: player.y}),
Down: (player) => ({x: player.x, y: player.y + 1}),
Left: (player) => ({x: player.x - 1, y: player.y})
Left: (player) => ({x: player.x - 1, y: player.y}),
_: (player) => player
});
advancePlayer(Move.Up(), player);
advancePlayer(Action.Up(), player);
```
As a catch all you can supply a property with the key `_` to case. When a type
doesn't match another handler `_` will be used.
doesn't match another handler `_` will be used. The fields will NOT be extracted
when matching on `_` as this may result in inconsistent argument positions.
```javascript
const advancePlayerOnlyUp = (move, player) =>
Move.case({
const advancePlayerOnlyUp = (action, player) =>
Action.case({
Up: () => ({x: player.x, y: player.y - 1}),

@@ -228,2 +233,1 @@ _: () => player,

released under the MIT license. I hope you find it useful.

@@ -47,2 +47,16 @@ var assert = require('assert');

});
it('accepts boolean true with primitive constructors', function() {
var Exists = Type({Exists: [Boolean]});
assert.equal(Exists.Exists(true)[0], true);
});
it('accepts boolean false with primitive constructors', function() {
var Exists = Type({Exists: [Boolean]});
assert.equal(Exists.Exists(false)[0], false);
});
it('throws on boolean with primitive constructors', function() {
var Exists = Type({Exists: [Boolean]});
assert.throws(function() {
Exists.Exists('12');
}, /wrong value/);
});
});

@@ -119,2 +133,13 @@ it('nest types', function() {

});
describe('caseOn _', function() {
var Action = Type({Jump: [], Move: [Number]});
var Context = {x: 1, y: 2}
var update = Action.caseOn({
_: function(context) { return context; },
});
it('does not extract fields when matching _', function() {
assert.deepEqual(update(Action.Jump(), Context), Context);
assert.deepEqual(update(Action.Move(5), Context), Context);
});
});
describe('recursive data types', function() {

@@ -121,0 +146,0 @@ var List = Type({Nil: [], Cons: [T, List]});

@@ -5,2 +5,3 @@ var curryN = require('ramda/src/curryN');

function isNumber(n) { return typeof n === 'number'; }
function isBoolean(b) { return typeof b === 'boolean'; }
function isObject(value) {

@@ -13,5 +14,6 @@ var type = typeof value;

var mapConstrToFn = curryN(2, function(group, constr) {
var mapConstrToFn = function(group, constr) {
return constr === String ? isString
: constr === Number ? isNumber
: constr === Boolean ? isBoolean
: constr === Object ? isObject

@@ -22,16 +24,19 @@ : constr === Array ? isArray

: 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);
return curryN(validators.length, function() {
var val = [], validator, i, v;
if (Type.disableChecking === true) {
for (i = 0; i < arguments.length; ++i) val[i] = arguments[i];
} else {
for (i = 0; i < arguments.length; ++i) {
v = arguments[i];
validator = mapConstrToFn(group, validators[i]);
if ((typeof validator === 'function' && validator(v)) ||
(v !== undefined && v !== null && v.of === validator)) {
val[i] = v;
} else {
throw new TypeError('wrong value ' + v + ' passed to location ' + i + ' in ' + name);
}
}

@@ -43,3 +48,2 @@ }

});
return constructor;
}

@@ -55,3 +59,7 @@

} else {
return cases[name].apply(undefined, arg !== undefined ? action.concat([arg]) : action);
var args = name === "_" ? [arg]
: arg !== undefined ? action.concat([arg])
: action
return cases[name].apply(undefined, args);
}

@@ -73,2 +81,4 @@ }

Type.disableChecking = process !== undefined && process.env.NODE_ENV === 'production';
module.exports = Type;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc