Skip to content

Commit

Permalink
exposed getDefaultName on combinator constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Sep 15, 2015
1 parent 352d76b commit 4782792
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 51 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- **New Feature**
- check if the type returned by a union dispatch function is correct, fix #136 (thanks @fcracker79)
- added `refinement` alias to `subtype` (which is deprecated), fix #140
- **Internal**
- exposed `getDefaultName` on combinator constructors

## v2.4.0

Expand Down
108 changes: 57 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ function isTypeName(name) {

// returns true if x is an instance of type
function is(x, type) {
return isType(type) ? type.is(x) : x instanceof type; // type should be a class constructor
if (isType(type)) {
return type.is(x);
}
return x instanceof type; // type should be a class constructor
}

// return true if the type constructor behaves like the identity function (exceptions are the structs)
function isIdentity(type) {
return isType(type) ? type.meta.identity : true; // ES6 classes are identity for tcomb
if (isType(type)) {
return type.meta.identity;
}
return true; // ES6 classes are identity for tcomb
}

// creates an instance of a type, handling the optional new operator
Expand Down Expand Up @@ -311,24 +317,18 @@ var Dat = irreducible('Date', function (x) {
return x instanceof Date;
});

function getDefaultStructName(props) {
return '{' + Object.keys(props).map(function (prop) {
return prop + ': ' + getTypeName(props[prop]);
}).join(', ') + '}';
}

function struct(props, name) {

if (process.env.NODE_ENV !== 'production') {
assert(dict(Str, Func).is(props), function () { return 'Invalid argument props ' + exports.stringify(props) + ' supplied to struct(props, [name]) combinator (expected a dictionary String -> Type)'; });
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to struct(props, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultStructName(props);
var displayName = name || struct.getDefaultName(props);

function Struct(value, path) {

if (Struct.is(value)) { // makes Struct idempotent
if (Struct.is(value)) { // implements idempotency
return value;
}

Expand All @@ -337,7 +337,7 @@ function struct(props, name) {
assert(isObject(value), function () { return 'Invalid value ' + exports.stringify(value) + ' supplied to ' + path.join('/') + ' (expected an object)'; });
}

if (!(this instanceof Struct)) { // makes `new` optional
if (!(this instanceof Struct)) { // `new` is optional
return new Struct(value);
}

Expand Down Expand Up @@ -395,9 +395,11 @@ function struct(props, name) {
return Struct;
}

function getDefaultUnionName(types) {
return types.map(getTypeName).join(' | ');
}
struct.getDefaultName = function (props) {
return '{' + Object.keys(props).map(function (prop) {
return prop + ': ' + getTypeName(props[prop]);
}).join(', ') + '}';
};

function union(types, name) {

Expand All @@ -406,7 +408,7 @@ function union(types, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to union(types, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultUnionName(types);
var displayName = name || union.getDefaultName(types);
var identity = types.every(isIdentity);

function Union(value, path) {
Expand Down Expand Up @@ -467,9 +469,9 @@ function union(types, name) {
return Union;
}

function getDefaultIntersectionName(types) {
return types.map(getTypeName).join(' & ');
}
union.getDefaultName = function (types) {
return types.map(getTypeName).join(' | ');
};

function intersection(types, name) {

Expand All @@ -478,7 +480,7 @@ function intersection(types, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to intersection(types, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultIntersectionName(types);
var displayName = name || intersection.getDefaultName(types);
var identity = types.every(isIdentity);

function Intersection(value, path) {
Expand Down Expand Up @@ -514,9 +516,9 @@ function intersection(types, name) {
return Intersection;
}

function getDefaultMaybeName(type) {
return '?' + getTypeName(type);
}
intersection.getDefaultName = function (types) {
return types.map(getTypeName).join(' & ');
};

function maybe(type, name) {

Expand All @@ -532,7 +534,7 @@ function maybe(type, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to maybe(type, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultMaybeName(type);
var displayName = name || maybe.getDefaultName(type);
var identity = isIdentity(type);

function Maybe(value, path) {
Expand All @@ -558,9 +560,9 @@ function maybe(type, name) {
return Maybe;
}

function getDefaultEnumsName(map) {
return Object.keys(map).map(function (k) { return exports.stringify(k); }).join(' | ');
}
maybe.getDefaultName = function (type) {
return '?' + getTypeName(type);
};

function enums(map, name) {

Expand All @@ -569,7 +571,7 @@ function enums(map, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to enums(map, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultEnumsName(map);
var displayName = name || enums.getDefaultName(map);

function Enums(value, path) {

Expand Down Expand Up @@ -598,6 +600,10 @@ function enums(map, name) {
return Enums;
}

enums.getDefaultName = function (map) {
return Object.keys(map).map(function (k) { return exports.stringify(k); }).join(' | ');
};

enums.of = function (keys, name) {
keys = isString(keys) ? keys.split(' ') : keys;
var value = {};
Expand All @@ -607,18 +613,14 @@ enums.of = function (keys, name) {
return enums(value, name);
};

function getDefaultTupleName(types) {
return '[' + types.map(getTypeName).join(', ') + ']';
}

function tuple(types, name) {

if (process.env.NODE_ENV !== 'production') {
assert(isArray(types) && types.every(isFunction), function () { return 'Invalid argument types ' + exports.stringify(types) + ' supplied to tuple(types, [name]) combinator (expected an array of types)'; });
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to tuple(types, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultTupleName(types);
var displayName = name || tuple.getDefaultName(types);
var identity = types.every(isIdentity);

function Tuple(value, path) {
Expand All @@ -644,7 +646,7 @@ function tuple(types, name) {
ret.push(instance);
}

if (idempotent) {
if (idempotent) { // implements idempotency
ret = value;
}

Expand Down Expand Up @@ -679,9 +681,9 @@ function tuple(types, name) {
return Tuple;
}

function getDefaultRefinementName(type, predicate) {
return '{' + getTypeName(type) + ' | ' + getFunctionName(predicate) + '}';
}
tuple.getDefaultName = function (types) {
return '[' + types.map(getTypeName).join(', ') + ']';
};

function refinement(type, predicate, name) {

Expand All @@ -691,7 +693,7 @@ function refinement(type, predicate, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to refinement(type, predicate, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultRefinementName(type, predicate);
var displayName = name || refinement.getDefaultName(type, predicate);
var identity = isIdentity(type);

function Refinement(value, path) {
Expand Down Expand Up @@ -731,9 +733,9 @@ function refinement(type, predicate, name) {
return Refinement;
}

function getDefaultListName(type) {
return 'Array<' + getTypeName(type) + '>';
}
refinement.getDefaultName = function (type, predicate) {
return '{' + getTypeName(type) + ' | ' + getFunctionName(predicate) + '}';
};

function list(type, name) {

Expand All @@ -742,7 +744,7 @@ function list(type, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to list(type, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultListName(type);
var displayName = name || list.getDefaultName(type);
var typeNameCache = getTypeName(type);
var identity = isIdentity(type); // the list is identity iif type is identity

Expand All @@ -768,7 +770,7 @@ function list(type, name) {
ret.push(instance);
}

if (idempotent) {
if (idempotent) { // implements idempotency
ret = value;
}

Expand Down Expand Up @@ -801,9 +803,9 @@ function list(type, name) {
return List;
}

function getDefaultDictName(domain, codomain) {
return '{[key: ' + getTypeName(domain) + ']: ' + getTypeName(codomain) + '}';
}
list.getDefaultName = function (type) {
return 'Array<' + getTypeName(type) + '>';
};

function dict(domain, codomain, name) {

Expand All @@ -813,7 +815,7 @@ function dict(domain, codomain, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to dict(domain, codomain, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultDictName(domain, codomain);
var displayName = name || dict.getDefaultName(domain, codomain);
var domainNameCache = getTypeName(domain);
var codomainNameCache = getTypeName(codomain);
var identity = isIdentity(domain) && isIdentity(codomain);
Expand Down Expand Up @@ -880,14 +882,14 @@ function dict(domain, codomain, name) {
return Dict;
}

dict.getDefaultName = function (domain, codomain) {
return '{[key: ' + getTypeName(domain) + ']: ' + getTypeName(codomain) + '}';
};

function isInstrumented(f) {
return isFunction(f) && isObject(f.instrumentation);
}

function getDefaultFuncName(domain, codomain) {
return '(' + domain.map(getTypeName).join(', ') + ') => ' + getTypeName(codomain);
}

function func(domain, codomain, name) {

domain = isArray(domain) ? domain : [domain]; // handle handy syntax for unary functions
Expand All @@ -898,7 +900,7 @@ function func(domain, codomain, name) {
assert(isTypeName(name), function () { return 'Invalid argument name ' + exports.stringify(name) + ' supplied to func(domain, codomain, [name]) combinator (expected a string)'; });
}

var displayName = name || getDefaultFuncName(domain, codomain);
var displayName = name || func.getDefaultName(domain, codomain);

function FuncType(value, uncurried) {

Expand Down Expand Up @@ -977,6 +979,10 @@ function func(domain, codomain, name) {

}

func.getDefaultName = function (domain, codomain) {
return '(' + domain.map(getTypeName).join(', ') + ') => ' + getTypeName(codomain);
};

function match(x) {
var type, guard, f, count;
for (var i = 1, len = arguments.length; i < len; ) {
Expand Down

0 comments on commit 4782792

Please sign in to comment.