Skip to content

Commit

Permalink
Merge pull request #145 from sanctuary-js/davidchambers/non-empty
Browse files Browse the repository at this point in the history
types: define NonEmpty type constructor
  • Loading branch information
davidchambers authored May 11, 2017
2 parents 1d73b2f + f43b752 commit 4fecf08
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,23 @@
function(x) { return Number_._test(x) && x < 0; }
);

//# NonEmpty :: Type -> Type
//.
//. Constructor for non-empty types. `$.NonEmpty($.String)`, for example, is
//. the type comprising every [`String`][] value except `''`.
//.
//. The given type must satisfy the [Monoid][] and [Setoid][] specifications.
var NonEmpty = UnaryType(
'sanctuary-def/NonEmpty',
functionUrl('NonEmpty'),
function(x) {
return Z.Monoid.test(x) &&
Z.Setoid.test(x) &&
!Z.equals(x, Z.empty(x.constructor));
},
function(monoid) { return [monoid]; }
);

//# NonGlobalRegExp :: Type
//.
//. Type comprising every [`RegExp`][] value whose `global` flag is `false`.
Expand Down Expand Up @@ -2406,7 +2423,7 @@
{},
[String_,
StrMap(Array_(TypeClass)),
Array_(Type),
NonEmpty(Array_(Type)),
AnyFunction,
AnyFunction],
def);
Expand Down Expand Up @@ -2450,6 +2467,7 @@
NegativeFiniteNumber: NegativeFiniteNumber,
NegativeInteger: NegativeInteger,
NegativeNumber: NegativeNumber,
NonEmpty: NonEmpty,
NonGlobalRegExp: NonGlobalRegExp,
NonZeroFiniteNumber: NonZeroFiniteNumber,
NonZeroInteger: NonZeroInteger,
Expand Down Expand Up @@ -2489,6 +2507,8 @@
}));

//. [FL:Semigroup]: https://github.com/fantasyland/fantasy-land#semigroup
//. [Monoid]: https://github.com/fantasyland/fantasy-land#monoid
//. [Setoid]: https://github.com/fantasyland/fantasy-land#setoid
//. [`Array`]: #Array
//. [`BinaryType`]: #BinaryType
//. [`Date`]: #Date
Expand All @@ -2501,6 +2521,7 @@
//. [`Pair`]: #Pair
//. [`RegExp`]: #RegExp
//. [`RegexFlags`]: #RegexFlags
//. [`String`]: #String
//. [`SyntaxError`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError
//. [`TypeClass`]: https://github.com/sanctuary-js/sanctuary-type-classes#TypeClass
//. [`TypeError`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError
Expand Down
44 changes: 28 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('def', function() {
TypeError,
'Invalid value\n' +
'\n' +
'def :: String -> StrMap (Array TypeClass) -> Array Type -> Function -> Function\n' +
'def :: String -> StrMap (Array TypeClass) -> NonEmpty (Array Type) -> Function -> Function\n' +
' ^^^^^^\n' +
' 1\n' +
'\n' +
Expand All @@ -267,7 +267,7 @@ describe('def', function() {
TypeError,
'Invalid value\n' +
'\n' +
'def :: String -> StrMap (Array TypeClass) -> Array Type -> Function -> Function\n' +
'def :: String -> StrMap (Array TypeClass) -> NonEmpty (Array Type) -> Function -> Function\n' +
' ^^^^^^^^^^^^^^^^^^^^^^^^\n' +
' 1\n' +
'\n' +
Expand All @@ -277,41 +277,41 @@ describe('def', function() {
'\n' +
'See https://github.com/sanctuary-js/sanctuary-def/tree/v' + version + '#StrMap for information about the sanctuary-def/StrMap type.\n');

throws(function() { def('', {}, null, null); },
throws(function() { def('', {}, [], null); },
TypeError,
'Invalid value\n' +
'\n' +
'def :: String -> StrMap (Array TypeClass) -> Array Type -> Function -> Function\n' +
' ^^^^^^^^^^\n' +
' 1\n' +
'def :: String -> StrMap (Array TypeClass) -> NonEmpty (Array Type) -> Function -> Function\n' +
' ^^^^^^^^^^^^^^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'1) [] :: Array ???\n' +
'\n' +
'The value at position 1 is not a member of ‘Array Type’.\n' +
'The value at position 1 is not a member of ‘NonEmpty (Array Type)’.\n' +
'\n' +
'See https://github.com/sanctuary-js/sanctuary-def/tree/v' + version + '#Array for information about the Array type.\n');
'See https://github.com/sanctuary-js/sanctuary-def/tree/v' + version + '#NonEmpty for information about the sanctuary-def/NonEmpty type.\n');

throws(function() { def('', {}, [1, 2, 3], null); },
TypeError,
'Invalid value\n' +
'\n' +
'def :: String -> StrMap (Array TypeClass) -> Array Type -> Function -> Function\n' +
' ^^^^\n' +
' 1\n' +
'def :: String -> StrMap (Array TypeClass) -> NonEmpty (Array Type) -> Function -> Function\n' +
' ^^^^\n' +
' 1\n' +
'\n' +
'1) 1 :: Number\n' +
'\n' +
'The value at position 1 is not a member of ‘Type’.\n' +
'\n' +
'See https://github.com/sanctuary-js/sanctuary-def/tree/v' + version + '#Type for information about the Type type.\n');

throws(function() { def('', {}, [], null); },
throws(function() { def('', {}, [$.Null], null); },
TypeError,
'Invalid value\n' +
'\n' +
'def :: String -> StrMap (Array TypeClass) -> Array Type -> Function -> Function\n' +
' ^^^^^^^^\n' +
' 1\n' +
'def :: String -> StrMap (Array TypeClass) -> NonEmpty (Array Type) -> Function -> Function\n' +
' ^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'\n' +
Expand Down Expand Up @@ -1442,6 +1442,18 @@ describe('def', function() {
eq($.Function([a, a]).url, '');
});

it('provides the "NonEmpty" type constructor', function() {
eq($.NonEmpty($.String).name, 'sanctuary-def/NonEmpty');
eq($.NonEmpty($.String).url, 'https://github.com/sanctuary-js/sanctuary-def/tree/v' + version + '#NonEmpty');

function isNonEmptyIntegerArray(x) {
return $.test($.env, $.NonEmpty($.Array($.Integer)), x);
}
eq(isNonEmptyIntegerArray([]), false);
eq(isNonEmptyIntegerArray([0]), true);
eq(isNonEmptyIntegerArray([0.5]), false);
});

it('provides the "Null" type', function() {
eq($.Null.name, 'Null');
eq($.Null.url, 'https://github.com/sanctuary-js/sanctuary-def/tree/v' + version + '#Null');
Expand Down

0 comments on commit 4fecf08

Please sign in to comment.