Comparing version 0.4.0 to 1.0.0
@@ -0,1 +1,14 @@ | ||
# v1.0.0 | ||
- changed default name for `func` combinator | ||
- changed default name for `list` combinator | ||
- `t.update` is now overridable | ||
**BREAKING** | ||
- removed `options.onFail`, override `exports.fail` instead | ||
- removed `getKind` | ||
- removed `util` namespace | ||
- renamed `getName` to `getTypeName` | ||
# v0.4.0 | ||
@@ -2,0 +15,0 @@ |
465
index.js
@@ -14,42 +14,16 @@ (function (root, factory) { | ||
var failed = false; | ||
function onFail(message) { | ||
function fail(message) { | ||
// start debugger only once | ||
if (!failed) { | ||
/* | ||
DEBUG HINT: | ||
if you are reading this, chances are that there is a bug in your system | ||
see the Call Stack to find out what's wrong.. | ||
*/ | ||
if (!fail.failed) { | ||
/*jshint debug: true*/ | ||
debugger; | ||
} | ||
failed = true; | ||
throw new Error(message); | ||
fail.failed = true; | ||
throw new TypeError(message); | ||
} | ||
var options = { | ||
onFail: onFail | ||
}; | ||
function fail(message) { | ||
/* | ||
DEBUG HINT: | ||
if you are reading this, chances are that there is a bug in your system | ||
see the Call Stack to find out what's wrong.. | ||
*/ | ||
options.onFail(message); | ||
} | ||
function assert(guard) { | ||
function assert(guard, message) { | ||
if (guard !== true) { | ||
var args = slice.call(arguments, 1); | ||
var message = args[0] ? format.apply(null, args) : 'assert failed'; | ||
/* | ||
DEBUG HINT: | ||
if you are reading this, chances are that there is a bug in your system | ||
see the Call Stack to find out what's wrong.. | ||
*/ | ||
fail(message); | ||
message = message ? format.apply(null, slice.call(arguments, 1)) : 'assert failed'; | ||
exports.fail(message); | ||
} | ||
@@ -65,5 +39,3 @@ } | ||
function mixin(target, source, overwrite) { | ||
if (Nil.is(source)) { | ||
return target; | ||
} | ||
if (Nil.is(source)) { return target; } | ||
for (var k in source) { | ||
@@ -101,13 +73,15 @@ if (source.hasOwnProperty(k)) { | ||
function getFunctionName(f) { | ||
assert(typeof f === 'function', 'Invalid argument `f` = `%s` supplied to `getFunctionName()`', f); | ||
return f.displayName || f.name || format('<function%s>', f.length); | ||
} | ||
function replacer(key, value) { | ||
if (typeof value === 'function') { | ||
return format('Func', value.name); | ||
} | ||
return value; | ||
return Func.is(value) ? getFunctionName(value) : value; | ||
} | ||
format.formatters = { | ||
s: function formatString(x) { return String(x); }, | ||
j: function formatJSON(x) { | ||
try { | ||
s: function (x) { return String(x); }, | ||
j: function (x) { | ||
try { // handle circular references | ||
return JSON.stringify(x, replacer); | ||
@@ -120,19 +94,9 @@ } catch (e) { | ||
function getName(type) { | ||
assert(Type.is(type), 'Invalid argument `type` = `%s` supplied to `getName()`', type); | ||
function getTypeName(type) { | ||
assert(Type.is(type), 'Invalid argument `type` = `%s` supplied to `getTypeName()`', type); | ||
return type.meta.name; | ||
} | ||
function getFunctionName(f) { | ||
assert(typeof f === 'function', 'Invalid argument `f` = `%s` supplied to `getFunctionName()`', f); | ||
return f.displayName || f.name || format('<function%s>', f.length); | ||
} | ||
function getKind(type) { | ||
assert(Type.is(type), 'Invalid argument `type` = `%s` supplied to `geKind()`', type); | ||
return type.meta.kind; | ||
} | ||
function blockNew(x, type) { | ||
assert(!(x instanceof type), 'Operator `new` is forbidden for type `%s`', getName(type)); | ||
assert(!(x instanceof type), 'Operator `new` is forbidden for type `%s`', getTypeName(type)); | ||
} | ||
@@ -161,7 +125,7 @@ | ||
update.commands = { | ||
'$apply': function $apply(f, value) { | ||
'$apply': function (f, value) { | ||
assert(Func.is(f)); | ||
return f(value); | ||
}, | ||
'$push': function $push(elements, arr) { | ||
'$push': function (elements, arr) { | ||
assert(Arr.is(elements)); | ||
@@ -171,3 +135,3 @@ assert(Arr.is(arr)); | ||
}, | ||
'$remove': function $remove(keys, obj) { | ||
'$remove': function (keys, obj) { | ||
assert(Arr.is(keys)); | ||
@@ -180,9 +144,9 @@ assert(Obj.is(obj)); | ||
}, | ||
'$set': function $set(value) { | ||
'$set': function (value) { | ||
return value; | ||
}, | ||
'$splice': function $splice(splices, arr) { | ||
'$splice': function (splices, arr) { | ||
assert(list(Arr).is(splices)); | ||
assert(Arr.is(arr)); | ||
return splices.reduce(function reducer(acc, splice) { | ||
return splices.reduce(function (acc, splice) { | ||
acc.splice.apply(acc, splice); | ||
@@ -192,3 +156,3 @@ return acc; | ||
}, | ||
'$swap': function $swap(config, arr) { | ||
'$swap': function (config, arr) { | ||
assert(Obj.is(config)); | ||
@@ -203,3 +167,3 @@ assert(Num.is(config.from)); | ||
}, | ||
'$unshift': function $unshift(elements, arr) { | ||
'$unshift': function (elements, arr) { | ||
assert(Arr.is(elements)); | ||
@@ -220,17 +184,8 @@ assert(Arr.is(arr)); | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a string | ||
assert(typeof name === 'string', 'Invalid argument `name` = `%s` supplied to `irreducible()`', name); | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a function | ||
assert(typeof is === 'function', 'Invalid argument `is` = `%s` supplied to `irreducible()`', is); | ||
function Irreducible(value) { | ||
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden | ||
blockNew(this, Irreducible); | ||
// DEBUG HINT: if the debugger stops here, the first argument is invalid | ||
// mouse over the `value` variable to see what's wrong. In `name` there is the name of the type | ||
assert(is(value), 'Invalid argument `value` = `%s` supplied to irreducible type `%s`', value, name); | ||
return value; | ||
@@ -251,47 +206,47 @@ } | ||
var Any = irreducible('Any', function isAny() { | ||
var Any = irreducible('Any', function () { | ||
return true; | ||
}); | ||
var Nil = irreducible('Nil', function isNil(x) { | ||
var Nil = irreducible('Nil', function (x) { | ||
return x === null || x === void 0; | ||
}); | ||
var Str = irreducible('Str', function isStr(x) { | ||
var Str = irreducible('Str', function (x) { | ||
return typeof x === 'string'; | ||
}); | ||
var Num = irreducible('Num', function isNum(x) { | ||
var Num = irreducible('Num', function (x) { | ||
return typeof x === 'number' && isFinite(x) && !isNaN(x); | ||
}); | ||
var Bool = irreducible('Bool', function isBool(x) { | ||
var Bool = irreducible('Bool', function (x) { | ||
return x === true || x === false; | ||
}); | ||
var Arr = irreducible('Arr', function isArr(x) { | ||
var Arr = irreducible('Arr', function (x) { | ||
return x instanceof Array; | ||
}); | ||
var Obj = irreducible('Obj', function isObj(x) { | ||
var Obj = irreducible('Obj', function (x) { | ||
return !Nil.is(x) && typeof x === 'object' && !Arr.is(x); | ||
}); | ||
var Func = irreducible('Func', function isFunc(x) { | ||
var Func = irreducible('Func', function (x) { | ||
return typeof x === 'function'; | ||
}); | ||
var Err = irreducible('Err', function isErr(x) { | ||
var Err = irreducible('Err', function (x) { | ||
return x instanceof Error; | ||
}); | ||
var Re = irreducible('Re', function isRe(x) { | ||
var Re = irreducible('Re', function (x) { | ||
return x instanceof RegExp; | ||
}); | ||
var Dat = irreducible('Dat', function isDat(x) { | ||
var Dat = irreducible('Dat', function (x) { | ||
return x instanceof Date; | ||
}); | ||
var Type = irreducible('Type', function isType(x) { | ||
var Type = irreducible('Type', function (x) { | ||
return Func.is(x) && Obj.is(x.meta); | ||
@@ -302,17 +257,9 @@ }); | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a dict of types | ||
// mouse over the `props` variable to see what's wrong | ||
assert(dict(Str, Type).is(props), 'Invalid argument `props` = `%s` supplied to `struct` combinator', props); | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `struct` combinator', name); | ||
// DEBUG HINT: always give a name to a type, the debug will be easier | ||
name = name || format('{%s}', Object.keys(props).map(function (prop) { | ||
return format('%s: %s', prop, getName(props[prop])); | ||
return format('%s: %s', prop, getTypeName(props[prop])); | ||
}).join(', ')); | ||
function Struct(value, mut) { | ||
// makes Struct idempotent | ||
@@ -322,7 +269,3 @@ if (Struct.is(value)) { | ||
} | ||
// DEBUG HINT: if the debugger stops here, the first argument is invalid | ||
// mouse over the `value` variable to see what's wrong. In `name` there is the name of the type | ||
assert(Obj.is(value), 'Invalid argument `value` = `%s` supplied to struct type `%s`', value, name); | ||
// makes `new` optional | ||
@@ -332,3 +275,2 @@ if (!(this instanceof Struct)) { | ||
} | ||
for (var k in props) { | ||
@@ -338,8 +280,5 @@ if (props.hasOwnProperty(k)) { | ||
var actual = value[k]; | ||
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `expected` type is invalid | ||
// mouse over the `actual` and `expected` variables to see what's wrong | ||
this[k] = expected(actual, mut); | ||
} | ||
} | ||
if (mut !== true) { | ||
@@ -358,11 +297,11 @@ Object.freeze(this); | ||
Struct.is = function isStruct(x) { | ||
Struct.is = function (x) { | ||
return x instanceof Struct; | ||
}; | ||
Struct.update = function updateStruct(instance, spec, value) { | ||
return new Struct(update(instance, spec, value)); | ||
Struct.update = function (instance, spec) { | ||
return new Struct(exports.update(instance, spec)); | ||
}; | ||
Struct.extend = function extendStruct(arr, name) { | ||
Struct.extend = function (arr, name) { | ||
arr = [].concat(arr).map(function (x) { | ||
@@ -382,32 +321,14 @@ return Obj.is(x) ? x : x.meta.props; | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a list of types | ||
assert(list(Type).is(types), 'Invalid argument `types` = `%s` supplied to `union` combinator', types); | ||
var len = types.length; | ||
var defaultName = types.map(getName).join(' | '); | ||
// DEBUG HINT: if the debugger stops here, there are too few types (they must be at least two) | ||
var defaultName = types.map(getTypeName).join(' | '); | ||
assert(len >= 2, 'Invalid argument `types` = `%s` supplied to `union` combinator, provide at least two types', defaultName); | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `union` combinator', name); | ||
name = name || defaultName; | ||
function Union(value, mut) { | ||
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden | ||
blockNew(this, Union); | ||
// DEBUG HINT: if the debugger stops here, you must implement the `dispatch` static method for this type | ||
assert(Func.is(Union.dispatch), 'Unimplemented `dispatch()` function for union type `%s`', name); | ||
var type = Union.dispatch(value); | ||
// DEBUG HINT: if the debugger stops here, the `dispatch` static method returns no type | ||
assert(Type.is(type), 'The `dispatch()` function of union type `%s` returns no type constructor', name); | ||
// DEBUG HINT: if the debugger stops here, `value` can't be converted to `type` | ||
// mouse over the `value` and `type` variables to see what's wrong | ||
return type(value, mut); | ||
@@ -424,4 +345,4 @@ } | ||
Union.is = function isUnion(x) { | ||
return types.some(function isType(type) { | ||
Union.is = function (x) { | ||
return types.some(function (type) { | ||
return type.is(x); | ||
@@ -432,4 +353,4 @@ }); | ||
// default dispatch implementation | ||
Union.dispatch = function dispatch(x) { | ||
for (var i = 0, len = types.length ; i < len ; i++ ) { | ||
Union.dispatch = function (x) { | ||
for (var i = 0 ; i < len ; i++ ) { | ||
if (types[i].is(x)) { | ||
@@ -446,23 +367,12 @@ return types[i]; | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a type | ||
assert(Type.is(type), 'Invalid argument `type` = `%s` supplied to `maybe` combinator', type); | ||
// makes the combinator idempotent and handle Any, Nil | ||
if (getKind(type) === 'maybe' || type === Any || type === Nil) { | ||
if (type.meta.kind === 'maybe' || type === Any || type === Nil) { | ||
return type; | ||
} | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(Nil.is(name) || Str.is(name), 'Invalid argument `name` = `%s` supplied to `maybe` combinator', name); | ||
name = name || ('?' + getTypeName(type)); | ||
name = name || ('?' + getName(type)); | ||
function Maybe(value, mut) { | ||
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden | ||
blockNew(this, Maybe); | ||
// DEBUG HINT: if the debugger stops here, `value` can't be converted to `type` | ||
// mouse over the `value` and `type` variables to see what's wrong | ||
return Nil.is(value) ? null : type(value, mut); | ||
@@ -479,3 +389,3 @@ } | ||
Maybe.is = function isMaybe(x) { | ||
Maybe.is = function (x) { | ||
return Nil.is(x) || type.is(x); | ||
@@ -489,24 +399,10 @@ }; | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a hash | ||
// mouse over the `map` variable to see what's wrong | ||
assert(Obj.is(map), 'Invalid argument `map` = `%s` supplied to `enums` combinator', map); | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `enums` combinator', name); | ||
// cache enums | ||
var keys = Object.keys(map); | ||
var keys = Object.keys(map); // cache enums | ||
name = name || keys.map(function (k) { return JSON.stringify(k); }).join(' | '); | ||
function Enums(value) { | ||
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden | ||
blockNew(this, Enums); | ||
// DEBUG HINT: if the debugger stops here, the value is not one of the defined enums | ||
// mouse over the `value`, `name` and `keys` variables to see what's wrong | ||
assert(Enums.is(value), 'Invalid argument `value` = `%s` supplied to enums type `%s`, expected one of %j', value, name, keys); | ||
return value; | ||
@@ -523,3 +419,3 @@ } | ||
Enums.is = function isEnums(x) { | ||
Enums.is = function (x) { | ||
return Str.is(x) && map.hasOwnProperty(x); | ||
@@ -531,6 +427,6 @@ }; | ||
enums.of = function enumsOf(keys, name) { | ||
enums.of = function (keys, name) { | ||
keys = Str.is(keys) ? keys.split(' ') : keys; | ||
var value = {}; | ||
keys.forEach(function setEnum(k) { | ||
keys.forEach(function (k) { | ||
value[k] = k; | ||
@@ -543,26 +439,20 @@ }); | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a list of types | ||
assert(list(Type).is(types), 'Invalid argument `types` = `%s` supplied to `tuple` combinator', types); | ||
var len = types.length; | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
var len = types.length; // cache types length | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `tuple` combinator', name); | ||
name = name || format('[%s]', types.map(getTypeName).join(', ')); | ||
name = name || format('[%s]', types.map(getName).join(', ')); | ||
function isTuple(x) { | ||
return types.every(function (type, i) { | ||
return type.is(x[i]); | ||
}); | ||
} | ||
function Tuple(value, mut) { | ||
// DEBUG HINT: if the debugger stops here, the value is not one of the defined enums | ||
// mouse over the `value`, `name` and `len` variables to see what's wrong | ||
assert(Arr.is(value) && value.length === len, 'Invalid argument `value` = `%s` supplied to tuple type `%s`, expected an `Arr` of length `%s`', value, name, len); | ||
var frozen = (mut !== true); | ||
// makes Tuple idempotent | ||
if (Tuple.isTuple(value) && Object.isFrozen(value) === frozen) { | ||
if (isTuple(value) && Object.isFrozen(value) === frozen) { | ||
return value; | ||
} | ||
var arr = []; | ||
@@ -572,7 +462,4 @@ for (var i = 0 ; i < len ; i++) { | ||
var actual = value[i]; | ||
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `expected` type is invalid | ||
// mouse over the `actual` and `expected` variables to see what's wrong | ||
arr.push(expected(actual, mut)); | ||
} | ||
if (frozen) { | ||
@@ -593,16 +480,10 @@ Object.freeze(arr); | ||
Tuple.isTuple = function isTuple(x) { | ||
return types.every(function isType(type, i) { | ||
return type.is(x[i]); | ||
}); | ||
Tuple.is = function (x) { | ||
return Arr.is(x) && x.length === len && isTuple(x); | ||
}; | ||
Tuple.is = function isTuple(x) { | ||
return Arr.is(x) && x.length === len && Tuple.isTuple(x); | ||
Tuple.update = function (instance, spec) { | ||
return Tuple(exports.update(instance, spec)); | ||
}; | ||
Tuple.update = function updateTuple(instance, spec, value) { | ||
return Tuple(update(instance, spec, value)); | ||
}; | ||
return Tuple; | ||
@@ -613,25 +494,10 @@ } | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a type | ||
assert(Type.is(type), 'Invalid argument `type` = `%s` supplied to `subtype` combinator', type); | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a function | ||
assert(Func.is(predicate), 'Invalid argument `predicate` = `%s` supplied to `subtype` combinator', predicate); | ||
// DEBUG HINT: if the debugger stops here, the third argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `subtype` combinator', name); | ||
name = name || format('{%s | %s}', getTypeName(type), getFunctionName(predicate)); | ||
// DEBUG HINT: always give a name to a type, the debug will be easier | ||
name = name || format('{%s | %s}', getName(type), getFunctionName(predicate)); | ||
function Subtype(value, mut) { | ||
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden | ||
blockNew(this, Subtype); | ||
// DEBUG HINT: if the debugger stops here, the value cannot be converted to the base type | ||
var x = type(value, mut); | ||
// DEBUG HINT: if the debugger stops here, the value is converted to the base type | ||
// but the predicate returns `false` | ||
assert(predicate(x), 'Invalid argument `value` = `%s` supplied to subtype type `%s`', value, name); | ||
@@ -650,8 +516,8 @@ return x; | ||
Subtype.is = function isSubtype(x) { | ||
Subtype.is = function (x) { | ||
return type.is(x) && predicate(x); | ||
}; | ||
Subtype.update = function updateSubtype(instance, spec, value) { | ||
return Subtype(update(instance, spec, value)); | ||
Subtype.update = function (instance, spec) { | ||
return Subtype(exports.update(instance, spec)); | ||
}; | ||
@@ -664,35 +530,22 @@ | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a type | ||
assert(Type.is(type), 'Invalid argument `type` = `%s` supplied to `list` combinator', type); | ||
// DEBUG HINT: if the debugger stops here, the third argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `list` combinator', name); | ||
name = name || format('Array<%s>', getTypeName(type)); | ||
// DEBUG HINT: always give a name to a type, the debug will be easier | ||
name = name || format('Array<%s>', getName(type)); | ||
function isList(x) { | ||
return x.every(type.is); | ||
} | ||
function List(value, mut) { | ||
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden | ||
// DEBUG HINT: if the debugger stops here, the value is not one of the defined enums | ||
// mouse over the `value` and `name` variables to see what's wrong | ||
assert(Arr.is(value), 'Invalid argument `value` = `%s` supplied to list type `%s`', value, name); | ||
var frozen = (mut !== true); | ||
// makes List idempotent | ||
if (List.isList(value) && Object.isFrozen(value) === frozen) { | ||
if (isList(value) && Object.isFrozen(value) === frozen) { | ||
return value; | ||
} | ||
var arr = []; | ||
for (var i = 0, len = value.length ; i < len ; i++ ) { | ||
var actual = value[i]; | ||
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `type` type is invalid | ||
// mouse over the `actual` and `type` variables to see what's wrong | ||
arr.push(type(actual, mut)); | ||
} | ||
if (frozen) { | ||
@@ -712,14 +565,10 @@ Object.freeze(arr); | ||
List.isList = function isList(x) { | ||
return x.every(type.is); | ||
List.is = function (x) { | ||
return Arr.is(x) && isList(x); | ||
}; | ||
List.is = function isList(x) { | ||
return Arr.is(x) && List.isList(x); | ||
List.update = function (instance, spec) { | ||
return List(exports.update(instance, spec)); | ||
}; | ||
List.update = function updateList(instance, spec, value) { | ||
return List(update(instance, spec, value)); | ||
}; | ||
return List; | ||
@@ -730,41 +579,31 @@ } | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a type | ||
assert(Type.is(domain), 'Invalid argument `domain` = `%s` supplied to `dict` combinator', domain); | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a type | ||
assert(Type.is(codomain), 'Invalid argument `codomain` = `%s` supplied to `dict` combinator', codomain); | ||
// DEBUG HINT: if the debugger stops here, the third argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `dict` combinator', name); | ||
name = name || format('{[key:%s]: %s}', getTypeName(domain), getTypeName(codomain)); | ||
// DEBUG HINT: always give a name to a type, the debug will be easier | ||
name = name || format('{[key:%s]: %s}', getName(domain), getName(codomain)); | ||
function isDict(x) { | ||
for (var k in x) { | ||
if (x.hasOwnProperty(k)) { | ||
if (!domain.is(k) || !codomain.is(x[k])) { return false; } | ||
} | ||
} | ||
return true; | ||
} | ||
function Dict(value, mut) { | ||
// DEBUG HINT: if the debugger stops here, the value is not an object | ||
// mouse over the `value` and `name` variables to see what's wrong | ||
assert(Obj.is(value), 'Invalid argument `value` = `%s` supplied to dict type `%s`', value, name); | ||
var frozen = (mut !== true); | ||
// makes Dict idempotent | ||
if (Dict.isDict(value) && Object.isFrozen(value) === frozen) { | ||
if (isDict(value) && Object.isFrozen(value) === frozen) { | ||
return value; | ||
} | ||
var obj = {}; | ||
for (var k in value) { | ||
if (value.hasOwnProperty(k)) { | ||
// DEBUG HINT: if the debugger stops here, the `k` value supplied to the `domain` type is invalid | ||
// mouse over the `k` and `domain` variables to see what's wrong | ||
k = domain(k); | ||
var actual = value[k]; | ||
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `codomain` type is invalid | ||
// mouse over the `actual` and `codomain` variables to see what's wrong | ||
obj[k] = codomain(actual, mut); | ||
} | ||
} | ||
if (frozen) { | ||
@@ -785,23 +624,17 @@ Object.freeze(obj); | ||
Dict.isDict = function isDict(x) { | ||
for (var k in x) { | ||
if (x.hasOwnProperty(k)) { | ||
if (!domain.is(k) || !codomain.is(x[k])) { return false; } | ||
} | ||
} | ||
return true; | ||
Dict.is = function (x) { | ||
return Obj.is(x) && isDict(x); | ||
}; | ||
Dict.is = function isDict(x) { | ||
return Obj.is(x) && Dict.isDict(x); | ||
Dict.update = function (instance, spec) { | ||
return Dict(exports.update(instance, spec)); | ||
}; | ||
Dict.update = function updateDict(instance, spec, value) { | ||
return Dict(update(instance, spec, value)); | ||
}; | ||
return Dict; | ||
} | ||
function isInstrumented(f) { | ||
return Func.is(f) && Obj.is(f.type); | ||
} | ||
function func(domain, codomain, name) { | ||
@@ -811,30 +644,14 @@ | ||
domain = Arr.is(domain) ? domain : [domain]; | ||
// DEBUG HINT: if the debugger stops here, the first argument is not a list of types | ||
assert(list(Type).is(domain), 'Invalid argument `domain` = `%s` supplied to `func` combinator', domain); | ||
// DEBUG HINT: if the debugger stops here, the second argument is not a type | ||
assert(Type.is(codomain), 'Invalid argument `codomain` = `%s` supplied to `func` combinator', codomain); | ||
// DEBUG HINT: if the debugger stops here, the third argument is not a string | ||
// mouse over the `name` variable to see what's wrong | ||
assert(maybe(Str).is(name), 'Invalid argument `name` = `%s` supplied to `func` combinator', name); | ||
name = name || format('(%s) => %s', domain.map(getTypeName).join(', '), getTypeName(codomain)); | ||
var domainLen = domain.length; // cache the domain length | ||
// DEBUG HINT: always give a name to a type, the debug will be easier | ||
name = name || format('(%s) -> %s', domain.map(getName).join(', '), getName(codomain)); | ||
// cache the domain length | ||
var domainLen = domain.length; | ||
function Func(value) { | ||
// automatically instrument the function if is not already instrumented | ||
if (!func.is(value)) { | ||
value = Func.of(value); | ||
// automatically instrument the function | ||
if (!isInstrumented(value)) { | ||
return Func.of(value); | ||
} | ||
// DEBUG HINT: if the debugger stops here, the first argument is invalid | ||
// mouse over the `value` and `name` variables to see what's wrong | ||
assert(Func.is(value), 'Invalid argument `value` = `%s` supplied to func type `%s`', value, name); | ||
return value; | ||
@@ -852,14 +669,13 @@ } | ||
Func.is = function isFunc(x) { | ||
return func.is(x) && | ||
x.func.domain.length === domain.length && | ||
x.func.domain.every(function isEqual(type, i) { | ||
Func.is = function (x) { | ||
return isInstrumented(x) && | ||
x.type.domain.length === domainLen && | ||
x.type.domain.every(function (type, i) { | ||
return type === domain[i]; | ||
}) && | ||
x.func.codomain === codomain; | ||
x.type.codomain === codomain; | ||
}; | ||
Func.of = function funcOf(f) { | ||
Func.of = function (f) { | ||
// DEBUG HINT: if the debugger stops here, f is not a function | ||
assert(typeof f === 'function'); | ||
@@ -873,32 +689,17 @@ | ||
function fn() { | ||
var args = slice.call(arguments); | ||
var len = Math.min(args.length, domainLen); | ||
// DEBUG HINT: if the debugger stops here, you provided wrong arguments to the function | ||
// mouse over the `args` variable to see what's wrong | ||
args = tuple(domain.slice(0, len))(args); | ||
var len = args.length; | ||
var argsType = tuple(domain.slice(0, len)); | ||
args = argsType(args); | ||
if (len === domainLen) { | ||
/* jshint validthis: true */ | ||
var r = f.apply(this, args); | ||
// DEBUG HINT: if the debugger stops here, the return value of the function is invalid | ||
// mouse over the `r` variable to see what's wrong | ||
r = codomain(r); | ||
return r; | ||
return codomain(f.apply(this, args)); | ||
} else { | ||
var curried = Function.prototype.bind.apply(f, [this].concat(args)); | ||
var newdomain = func(domain.slice(len), codomain); | ||
return newdomain.of(curried); | ||
} | ||
} | ||
fn.func = { | ||
fn.type = { | ||
domain: domain, | ||
@@ -909,2 +710,4 @@ codomain: codomain, | ||
fn.displayName = getFunctionName(f); | ||
return fn; | ||
@@ -918,24 +721,12 @@ | ||
// returns true if x is an instrumented function | ||
func.is = function isFunc(f) { | ||
return Func.is(f) && Obj.is(f.func); | ||
}; | ||
return { | ||
util: { | ||
format: format, | ||
getKind: getKind, | ||
getFunctionName: getFunctionName, | ||
getName: getName, | ||
mixin: mixin, | ||
slice: slice, | ||
shallowCopy: shallowCopy, | ||
update: update | ||
}, | ||
options: options, | ||
var exports = { | ||
format: format, | ||
getFunctionName: getFunctionName, | ||
getTypeName: getTypeName, | ||
mixin: mixin, | ||
slice: slice, | ||
shallowCopy: shallowCopy, | ||
update: update, | ||
assert: assert, | ||
fail: fail, | ||
Any: Any, | ||
@@ -953,3 +744,2 @@ Nil: Nil, | ||
Type: Type, | ||
irreducible: irreducible, | ||
@@ -966,2 +756,5 @@ struct: struct, | ||
}; | ||
return exports; | ||
})); |
{ | ||
"name": "tcomb", | ||
"version": "0.4.0", | ||
"description": "Pragmatic runtime type checking for JavaScript", | ||
"version": "1.0.0", | ||
"description": "Type checking for JavaScript", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,6 +0,6 @@ | ||
// tcomb 0.4.0 | ||
// tcomb 1.0.0 | ||
// https://github.com/gcanti/tcomb | ||
// (c) 2014 Giulio Canti <giulio.canti@gmail.com> | ||
// tcomb may be freely distributed under the MIT license. | ||
!function(a,b){"use strict";"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?module.exports=b():a.t=b()}(this,function(){"use strict";function a(a){throw w=!0,new Error(a)}function b(a){x.onFail(a)}function c(a){if(a!==!0){var c=y.call(arguments,1),d=c[0]?e.apply(null,c):"assert failed";b(d)}}function d(a,b,d){if(A.is(b))return a;for(var e in b)b.hasOwnProperty(e)&&(d!==!0&&c(!a.hasOwnProperty(e),"Cannot overwrite property %s",e),a[e]=b[e]);return a}function e(){function a(a,f){if("%%"===a)return"%";if(d>=c)return a;var g=e.formatters[f];return g?g(b[d++]):a}var b=y.call(arguments),c=b.length,d=1,f=b[0],g=f.replace(/%([a-z%])/g,a);return c>d&&(g+=" "+b.slice(d).join(" ")),g}function f(a,b){return"function"==typeof b?e("Func",b.name):b}function g(a){return c(K.is(a),"Invalid argument `type` = `%s` supplied to `getName()`",a),a.meta.name}function h(a){return c("function"==typeof a,"Invalid argument `f` = `%s` supplied to `getFunctionName()`",a),a.displayName||a.name||e("<function%s>",a.length)}function i(a){return c(K.is(a),"Invalid argument `type` = `%s` supplied to `geKind()`",a),a.meta.kind}function j(a,b){c(!(a instanceof b),"Operator `new` is forbidden for type `%s`",g(b))}function k(a){return E.is(a)?a.concat():F.is(a)?d({},a):a}function l(a,b){c(F.is(b));var d=k(a);for(var e in b)if(b.hasOwnProperty(e)){if(l.commands.hasOwnProperty(e))return c(1===Object.keys(b).length),l.commands[e](b[e],d);d[e]=l(d[e],b[e])}return d}function m(a,b){function d(e){return j(this,d),c(b(e),"Invalid argument `value` = `%s` supplied to irreducible type `%s`",e,a),e}return c("string"==typeof a,"Invalid argument `name` = `%s` supplied to `irreducible()`",a),c("function"==typeof b,"Invalid argument `is` = `%s` supplied to `irreducible()`",b),d.meta={kind:"irreducible",name:a},d.displayName=a,d.is=b,d}function n(a,b){function f(d,e){if(f.is(d))return d;if(c(F.is(d),"Invalid argument `value` = `%s` supplied to struct type `%s`",d,b),!(this instanceof f))return new f(d,e);for(var g in a)if(a.hasOwnProperty(g)){var h=a[g],i=d[g];this[g]=h(i,e)}e!==!0&&Object.freeze(this)}return c(u(B,K).is(a),"Invalid argument `props` = `%s` supplied to `struct` combinator",a),c(p(B).is(b),"Invalid argument `name` = `%s` supplied to `struct` combinator",b),b=b||e("{%s}",Object.keys(a).map(function(b){return e("%s: %s",b,g(a[b]))}).join(", ")),f.meta={kind:"struct",props:a,name:b},f.displayName=b,f.is=function(a){return a instanceof f},f.update=function(a,b,c){return new f(l(a,b,c))},f.extend=function(b,c){b=[].concat(b).map(function(a){return F.is(a)?a:a.meta.props}),b.unshift(a);var e=n(b.reduce(d,{}),c);return d(e.prototype,f.prototype),e},f}function o(a,b){function d(a,e){j(this,d),c(G.is(d.dispatch),"Unimplemented `dispatch()` function for union type `%s`",b);var f=d.dispatch(a);return c(K.is(f),"The `dispatch()` function of union type `%s` returns no type constructor",b),f(a,e)}c(t(K).is(a),"Invalid argument `types` = `%s` supplied to `union` combinator",a);var e=a.length,f=a.map(g).join(" | ");return c(e>=2,"Invalid argument `types` = `%s` supplied to `union` combinator, provide at least two types",f),c(p(B).is(b),"Invalid argument `name` = `%s` supplied to `union` combinator",b),b=b||f,d.meta={kind:"union",types:a,name:b},d.displayName=b,d.is=function(b){return a.some(function(a){return a.is(b)})},d.dispatch=function(b){for(var c=0,d=a.length;d>c;c++)if(a[c].is(b))return a[c]},d}function p(a,b){function d(b,c){return j(this,d),A.is(b)?null:a(b,c)}return c(K.is(a),"Invalid argument `type` = `%s` supplied to `maybe` combinator",a),"maybe"===i(a)||a===z||a===A?a:(c(A.is(b)||B.is(b),"Invalid argument `name` = `%s` supplied to `maybe` combinator",b),b=b||"?"+g(a),d.meta={kind:"maybe",type:a,name:b},d.displayName=b,d.is=function(b){return A.is(b)||a.is(b)},d)}function q(a,b){function d(a){return j(this,d),c(d.is(a),"Invalid argument `value` = `%s` supplied to enums type `%s`, expected one of %j",a,b,e),a}c(F.is(a),"Invalid argument `map` = `%s` supplied to `enums` combinator",a),c(p(B).is(b),"Invalid argument `name` = `%s` supplied to `enums` combinator",b);var e=Object.keys(a);return b=b||e.map(function(a){return JSON.stringify(a)}).join(" | "),d.meta={kind:"enums",map:a,name:b},d.displayName=b,d.is=function(b){return B.is(b)&&a.hasOwnProperty(b)},d}function r(a,b){function d(e,g){c(E.is(e)&&e.length===f,"Invalid argument `value` = `%s` supplied to tuple type `%s`, expected an `Arr` of length `%s`",e,b,f);var h=g!==!0;if(d.isTuple(e)&&Object.isFrozen(e)===h)return e;for(var i=[],j=0;f>j;j++){var k=a[j],l=e[j];i.push(k(l,g))}return h&&Object.freeze(i),i}c(t(K).is(a),"Invalid argument `types` = `%s` supplied to `tuple` combinator",a);var f=a.length;return c(p(B).is(b),"Invalid argument `name` = `%s` supplied to `tuple` combinator",b),b=b||e("[%s]",a.map(g).join(", ")),d.meta={kind:"tuple",types:a,length:f,name:b},d.displayName=b,d.isTuple=function(b){return a.every(function(a,c){return a.is(b[c])})},d.is=function(a){return E.is(a)&&a.length===f&&d.isTuple(a)},d.update=function(a,b,c){return d(l(a,b,c))},d}function s(a,b,d){function f(e,g){j(this,f);var h=a(e,g);return c(b(h),"Invalid argument `value` = `%s` supplied to subtype type `%s`",e,d),h}return c(K.is(a),"Invalid argument `type` = `%s` supplied to `subtype` combinator",a),c(G.is(b),"Invalid argument `predicate` = `%s` supplied to `subtype` combinator",b),c(p(B).is(d),"Invalid argument `name` = `%s` supplied to `subtype` combinator",d),d=d||e("{%s | %s}",g(a),h(b)),f.meta={kind:"subtype",type:a,predicate:b,name:d},f.displayName=d,f.is=function(c){return a.is(c)&&b(c)},f.update=function(a,b,c){return f(l(a,b,c))},f}function t(a,b){function d(e,f){c(E.is(e),"Invalid argument `value` = `%s` supplied to list type `%s`",e,b);var g=f!==!0;if(d.isList(e)&&Object.isFrozen(e)===g)return e;for(var h=[],i=0,j=e.length;j>i;i++){var k=e[i];h.push(a(k,f))}return g&&Object.freeze(h),h}return c(K.is(a),"Invalid argument `type` = `%s` supplied to `list` combinator",a),c(p(B).is(b),"Invalid argument `name` = `%s` supplied to `list` combinator",b),b=b||e("Array<%s>",g(a)),d.meta={kind:"list",type:a,name:b},d.displayName=b,d.isList=function(b){return b.every(a.is)},d.is=function(a){return E.is(a)&&d.isList(a)},d.update=function(a,b,c){return d(l(a,b,c))},d}function u(a,b,d){function f(e,g){c(F.is(e),"Invalid argument `value` = `%s` supplied to dict type `%s`",e,d);var h=g!==!0;if(f.isDict(e)&&Object.isFrozen(e)===h)return e;var i={};for(var j in e)if(e.hasOwnProperty(j)){j=a(j);var k=e[j];i[j]=b(k,g)}return h&&Object.freeze(i),i}return c(K.is(a),"Invalid argument `domain` = `%s` supplied to `dict` combinator",a),c(K.is(b),"Invalid argument `codomain` = `%s` supplied to `dict` combinator",b),c(p(B).is(d),"Invalid argument `name` = `%s` supplied to `dict` combinator",d),d=d||e("{[key:%s]: %s}",g(a),g(b)),f.meta={kind:"dict",domain:a,codomain:b,name:d},f.displayName=d,f.isDict=function(c){for(var d in c)if(c.hasOwnProperty(d)&&(!a.is(d)||!b.is(c[d])))return!1;return!0},f.is=function(a){return F.is(a)&&f.isDict(a)},f.update=function(a,b,c){return f(l(a,b,c))},f}function v(a,b,d){function f(a){return v.is(a)||(a=f.of(a)),c(f.is(a),"Invalid argument `value` = `%s` supplied to func type `%s`",a,d),a}a=E.is(a)?a:[a],c(t(K).is(a),"Invalid argument `domain` = `%s` supplied to `func` combinator",a),c(K.is(b),"Invalid argument `codomain` = `%s` supplied to `func` combinator",b),c(p(B).is(d),"Invalid argument `name` = `%s` supplied to `func` combinator",d),d=d||e("(%s) -> %s",a.map(g).join(", "),g(b));var h=a.length;return f.meta={kind:"func",domain:a,codomain:b,name:d},f.displayName=d,f.is=function(c){return v.is(c)&&c.func.domain.length===a.length&&c.func.domain.every(function(b,c){return b===a[c]})&&c.func.codomain===b},f.of=function(d){function e(){var c=y.call(arguments),e=Math.min(c.length,h);if(c=r(a.slice(0,e))(c),e===h){var f=d.apply(this,c);return f=b(f)}var g=Function.prototype.bind.apply(d,[this].concat(c)),i=v(a.slice(e),b);return i.of(g)}return c("function"==typeof d),f.is(d)?d:(e.func={domain:a,codomain:b,f:d},e)},f}var w=!1,x={onFail:a},y=Array.prototype.slice;e.formatters={s:function(a){return String(a)},j:function(a){try{return JSON.stringify(a,f)}catch(b){return String(a)}}},l.commands={$apply:function(a,b){return c(G.is(a)),a(b)},$push:function(a,b){return c(E.is(a)),c(E.is(b)),b.concat(a)},$remove:function(a,b){c(E.is(a)),c(F.is(b));for(var d=0,e=a.length;e>d;d++)delete b[a[d]];return b},$set:function(a){return a},$splice:function(a,b){return c(t(E).is(a)),c(E.is(b)),a.reduce(function(a,b){return a.splice.apply(a,b),a},b)},$swap:function(a,b){c(F.is(a)),c(C.is(a.from)),c(C.is(a.to)),c(E.is(b));var d=b[a.to];return b[a.to]=b[a.from],b[a.from]=d,b},$unshift:function(a,b){return c(E.is(a)),c(E.is(b)),a.concat(b)},$merge:function(a,b){return d(d({},b),a,!0)}};var z=m("Any",function(){return!0}),A=m("Nil",function(a){return null===a||void 0===a}),B=m("Str",function(a){return"string"==typeof a}),C=m("Num",function(a){return"number"==typeof a&&isFinite(a)&&!isNaN(a)}),D=m("Bool",function(a){return a===!0||a===!1}),E=m("Arr",function(a){return a instanceof Array}),F=m("Obj",function(a){return!A.is(a)&&"object"==typeof a&&!E.is(a)}),G=m("Func",function(a){return"function"==typeof a}),H=m("Err",function(a){return a instanceof Error}),I=m("Re",function(a){return a instanceof RegExp}),J=m("Dat",function(a){return a instanceof Date}),K=m("Type",function(a){return G.is(a)&&F.is(a.meta)});return q.of=function(a,b){a=B.is(a)?a.split(" "):a;var c={};return a.forEach(function(a){c[a]=a}),q(c,b)},v.is=function(a){return G.is(a)&&F.is(a.func)},{util:{format:e,getKind:i,getFunctionName:h,getName:g,mixin:d,slice:y,shallowCopy:k,update:l},options:x,assert:c,fail:b,Any:z,Nil:A,Str:B,Num:C,Bool:D,Arr:E,Obj:F,Func:G,Err:H,Re:I,Dat:J,Type:K,irreducible:m,struct:n,enums:q,union:o,maybe:p,tuple:r,subtype:s,list:t,dict:u,func:v}}); | ||
!function(a,b){"use strict";"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?module.exports=b():a.t=b()}(this,function(){"use strict";function a(b){throw!a.failed,a.failed=!0,new TypeError(b)}function b(a,b){a!==!0&&(b=b?d.apply(null,v.call(arguments,1)):"assert failed",I.fail(b))}function c(a,c,d){if(x.is(c))return a;for(var e in c)c.hasOwnProperty(e)&&(d!==!0&&b(!a.hasOwnProperty(e),"Cannot overwrite property %s",e),a[e]=c[e]);return a}function d(){function a(a,f){if("%%"===a)return"%";if(e>=c)return a;var g=d.formatters[f];return g?g(b[e++]):a}var b=v.call(arguments),c=b.length,e=1,f=b[0],g=f.replace(/%([a-z%])/g,a);return c>e&&(g+=" "+b.slice(e).join(" ")),g}function e(a){return b("function"==typeof a,"Invalid argument `f` = `%s` supplied to `getFunctionName()`",a),a.displayName||a.name||d("<function%s>",a.length)}function f(a,b){return D.is(b)?e(b):b}function g(a){return b(H.is(a),"Invalid argument `type` = `%s` supplied to `getTypeName()`",a),a.meta.name}function h(a,c){b(!(a instanceof c),"Operator `new` is forbidden for type `%s`",g(c))}function i(a){return B.is(a)?a.concat():C.is(a)?c({},a):a}function j(a,c){b(C.is(c));var d=i(a);for(var e in c)if(c.hasOwnProperty(e)){if(j.commands.hasOwnProperty(e))return b(1===Object.keys(c).length),j.commands[e](c[e],d);d[e]=j(d[e],c[e])}return d}function k(a,c){function d(e){return h(this,d),b(c(e),"Invalid argument `value` = `%s` supplied to irreducible type `%s`",e,a),e}return b("string"==typeof a,"Invalid argument `name` = `%s` supplied to `irreducible()`",a),b("function"==typeof c,"Invalid argument `is` = `%s` supplied to `irreducible()`",c),d.meta={kind:"irreducible",name:a},d.displayName=a,d.is=c,d}function l(a,e){function f(c,d){if(f.is(c))return c;if(b(C.is(c),"Invalid argument `value` = `%s` supplied to struct type `%s`",c,e),!(this instanceof f))return new f(c,d);for(var g in a)if(a.hasOwnProperty(g)){var h=a[g],i=c[g];this[g]=h(i,d)}d!==!0&&Object.freeze(this)}return b(s(y,H).is(a),"Invalid argument `props` = `%s` supplied to `struct` combinator",a),b(n(y).is(e),"Invalid argument `name` = `%s` supplied to `struct` combinator",e),e=e||d("{%s}",Object.keys(a).map(function(b){return d("%s: %s",b,g(a[b]))}).join(", ")),f.meta={kind:"struct",props:a,name:e},f.displayName=e,f.is=function(a){return a instanceof f},f.update=function(a,b){return new f(I.update(a,b))},f.extend=function(b,d){b=[].concat(b).map(function(a){return C.is(a)?a:a.meta.props}),b.unshift(a);var e=l(b.reduce(c,{}),d);return c(e.prototype,f.prototype),e},f}function m(a,c){function d(a,e){h(this,d),b(D.is(d.dispatch),"Unimplemented `dispatch()` function for union type `%s`",c);var f=d.dispatch(a);return b(H.is(f),"The `dispatch()` function of union type `%s` returns no type constructor",c),f(a,e)}b(r(H).is(a),"Invalid argument `types` = `%s` supplied to `union` combinator",a);var e=a.length,f=a.map(g).join(" | ");return b(e>=2,"Invalid argument `types` = `%s` supplied to `union` combinator, provide at least two types",f),b(n(y).is(c),"Invalid argument `name` = `%s` supplied to `union` combinator",c),c=c||f,d.meta={kind:"union",types:a,name:c},d.displayName=c,d.is=function(b){return a.some(function(a){return a.is(b)})},d.dispatch=function(b){for(var c=0;e>c;c++)if(a[c].is(b))return a[c]},d}function n(a,c){function d(b,c){return h(this,d),x.is(b)?null:a(b,c)}return b(H.is(a),"Invalid argument `type` = `%s` supplied to `maybe` combinator",a),"maybe"===a.meta.kind||a===w||a===x?a:(b(x.is(c)||y.is(c),"Invalid argument `name` = `%s` supplied to `maybe` combinator",c),c=c||"?"+g(a),d.meta={kind:"maybe",type:a,name:c},d.displayName=c,d.is=function(b){return x.is(b)||a.is(b)},d)}function o(a,c){function d(a){return h(this,d),b(d.is(a),"Invalid argument `value` = `%s` supplied to enums type `%s`, expected one of %j",a,c,e),a}b(C.is(a),"Invalid argument `map` = `%s` supplied to `enums` combinator",a),b(n(y).is(c),"Invalid argument `name` = `%s` supplied to `enums` combinator",c);var e=Object.keys(a);return c=c||e.map(function(a){return JSON.stringify(a)}).join(" | "),d.meta={kind:"enums",map:a,name:c},d.displayName=c,d.is=function(b){return y.is(b)&&a.hasOwnProperty(b)},d}function p(a,c){function e(b){return a.every(function(a,c){return a.is(b[c])})}function f(d,f){b(B.is(d)&&d.length===h,"Invalid argument `value` = `%s` supplied to tuple type `%s`, expected an `Arr` of length `%s`",d,c,h);var g=f!==!0;if(e(d)&&Object.isFrozen(d)===g)return d;for(var i=[],j=0;h>j;j++){var k=a[j],l=d[j];i.push(k(l,f))}return g&&Object.freeze(i),i}b(r(H).is(a),"Invalid argument `types` = `%s` supplied to `tuple` combinator",a);var h=a.length;return b(n(y).is(c),"Invalid argument `name` = `%s` supplied to `tuple` combinator",c),c=c||d("[%s]",a.map(g).join(", ")),f.meta={kind:"tuple",types:a,length:h,name:c},f.displayName=c,f.is=function(a){return B.is(a)&&a.length===h&&e(a)},f.update=function(a,b){return f(I.update(a,b))},f}function q(a,c,f){function i(d,e){h(this,i);var g=a(d,e);return b(c(g),"Invalid argument `value` = `%s` supplied to subtype type `%s`",d,f),g}return b(H.is(a),"Invalid argument `type` = `%s` supplied to `subtype` combinator",a),b(D.is(c),"Invalid argument `predicate` = `%s` supplied to `subtype` combinator",c),b(n(y).is(f),"Invalid argument `name` = `%s` supplied to `subtype` combinator",f),f=f||d("{%s | %s}",g(a),e(c)),i.meta={kind:"subtype",type:a,predicate:c,name:f},i.displayName=f,i.is=function(b){return a.is(b)&&c(b)},i.update=function(a,b){return i(I.update(a,b))},i}function r(a,c){function e(b){return b.every(a.is)}function f(d,f){b(B.is(d),"Invalid argument `value` = `%s` supplied to list type `%s`",d,c);var g=f!==!0;if(e(d)&&Object.isFrozen(d)===g)return d;for(var h=[],i=0,j=d.length;j>i;i++){var k=d[i];h.push(a(k,f))}return g&&Object.freeze(h),h}return b(H.is(a),"Invalid argument `type` = `%s` supplied to `list` combinator",a),b(n(y).is(c),"Invalid argument `name` = `%s` supplied to `list` combinator",c),c=c||d("Array<%s>",g(a)),f.meta={kind:"list",type:a,name:c},f.displayName=c,f.is=function(a){return B.is(a)&&e(a)},f.update=function(a,b){return f(I.update(a,b))},f}function s(a,c,e){function f(b){for(var d in b)if(b.hasOwnProperty(d)&&(!a.is(d)||!c.is(b[d])))return!1;return!0}function h(d,g){b(C.is(d),"Invalid argument `value` = `%s` supplied to dict type `%s`",d,e);var h=g!==!0;if(f(d)&&Object.isFrozen(d)===h)return d;var i={};for(var j in d)if(d.hasOwnProperty(j)){j=a(j);var k=d[j];i[j]=c(k,g)}return h&&Object.freeze(i),i}return b(H.is(a),"Invalid argument `domain` = `%s` supplied to `dict` combinator",a),b(H.is(c),"Invalid argument `codomain` = `%s` supplied to `dict` combinator",c),b(n(y).is(e),"Invalid argument `name` = `%s` supplied to `dict` combinator",e),e=e||d("{[key:%s]: %s}",g(a),g(c)),h.meta={kind:"dict",domain:a,codomain:c,name:e},h.displayName=e,h.is=function(a){return C.is(a)&&f(a)},h.update=function(a,b){return h(I.update(a,b))},h}function t(a){return D.is(a)&&C.is(a.type)}function u(a,c,f){function h(a){return t(a)?(b(h.is(a),"Invalid argument `value` = `%s` supplied to func type `%s`",a,f),a):h.of(a)}a=B.is(a)?a:[a],b(r(H).is(a),"Invalid argument `domain` = `%s` supplied to `func` combinator",a),b(H.is(c),"Invalid argument `codomain` = `%s` supplied to `func` combinator",c),b(n(y).is(f),"Invalid argument `name` = `%s` supplied to `func` combinator",f),f=f||d("(%s) => %s",a.map(g).join(", "),g(c));var i=a.length;return h.meta={kind:"func",domain:a,codomain:c,name:f},h.displayName=f,h.is=function(b){return t(b)&&b.type.domain.length===i&&b.type.domain.every(function(b,c){return b===a[c]})&&b.type.codomain===c},h.of=function(d){function f(){var b=v.call(arguments),e=b.length,f=p(a.slice(0,e));if(b=f(b),e===i)return c(d.apply(this,b));var g=Function.prototype.bind.apply(d,[this].concat(b)),h=u(a.slice(e),c);return h.of(g)}return b("function"==typeof d),h.is(d)?d:(f.type={domain:a,codomain:c,f:d},f.displayName=e(d),f)},h}var v=Array.prototype.slice;d.formatters={s:function(a){return String(a)},j:function(a){try{return JSON.stringify(a,f)}catch(b){return String(a)}}},j.commands={$apply:function(a,c){return b(D.is(a)),a(c)},$push:function(a,c){return b(B.is(a)),b(B.is(c)),c.concat(a)},$remove:function(a,c){b(B.is(a)),b(C.is(c));for(var d=0,e=a.length;e>d;d++)delete c[a[d]];return c},$set:function(a){return a},$splice:function(a,c){return b(r(B).is(a)),b(B.is(c)),a.reduce(function(a,b){return a.splice.apply(a,b),a},c)},$swap:function(a,c){b(C.is(a)),b(z.is(a.from)),b(z.is(a.to)),b(B.is(c));var d=c[a.to];return c[a.to]=c[a.from],c[a.from]=d,c},$unshift:function(a,c){return b(B.is(a)),b(B.is(c)),a.concat(c)},$merge:function(a,b){return c(c({},b),a,!0)}};var w=k("Any",function(){return!0}),x=k("Nil",function(a){return null===a||void 0===a}),y=k("Str",function(a){return"string"==typeof a}),z=k("Num",function(a){return"number"==typeof a&&isFinite(a)&&!isNaN(a)}),A=k("Bool",function(a){return a===!0||a===!1}),B=k("Arr",function(a){return a instanceof Array}),C=k("Obj",function(a){return!x.is(a)&&"object"==typeof a&&!B.is(a)}),D=k("Func",function(a){return"function"==typeof a}),E=k("Err",function(a){return a instanceof Error}),F=k("Re",function(a){return a instanceof RegExp}),G=k("Dat",function(a){return a instanceof Date}),H=k("Type",function(a){return D.is(a)&&C.is(a.meta)});o.of=function(a,b){a=y.is(a)?a.split(" "):a;var c={};return a.forEach(function(a){c[a]=a}),o(c,b)};var I={format:d,getFunctionName:e,getTypeName:g,mixin:c,slice:v,shallowCopy:i,update:j,assert:b,fail:a,Any:w,Nil:x,Str:y,Num:z,Bool:A,Arr:B,Obj:C,Func:D,Err:E,Re:F,Dat:G,Type:H,irreducible:k,struct:l,enums:o,union:m,maybe:n,tuple:p,subtype:q,list:r,dict:s,func:u};return I}); | ||
//# sourceMappingURL=tcomb.min.js.map |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
48267
636