Skip to content

Commit

Permalink
reinstate S.unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Apr 6, 2018
1 parent a3fd05a commit db8d85b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
28 changes: 27 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@
//. const S = create ({checkTypes, env});
//. ```
//.
//. Occasionally one may wish to perform an operation which is not type safe,
//. such as mapping over an object with heterogeneous values. This is possible
//. via selective use of [`unchecked`](#unchecked) functions.
//.
//. ## API

(function(f) {
Expand Down Expand Up @@ -411,7 +415,7 @@
function create(opts) {
var def = $.create (opts);
var S = {
env: defaultEnv,
env: opts.env,
is: def ('is') ({}) ([$.Type, $.Any, $.Boolean]) ($.test (opts.env)),
MaybeType: $Maybe,
Maybe: Maybe,
Expand All @@ -422,6 +426,8 @@
(Object.keys (_)).forEach (function(name) {
S[name] = def (name) (_[name].consts) (_[name].types) (_[name].impl);
});
S.unchecked = opts.checkTypes ? create ({checkTypes: false, env: opts.env})
: S;
return S;
}
_.create = {
Expand All @@ -435,6 +441,26 @@
//. The default environment, which may be used as is or as the basis of a
//. custom environment in conjunction with [`create`](#create).

//# unchecked :: Module
//.
//. A complete Sanctuary module which performs no type checking. This is
//. useful as it permits operations which Sanctuary's type checking would
//. disallow, such as mapping over an object with heterogeneous values.
//.
//. See also [`create`](#create).
//.
//. ```javascript
//. > S.unchecked.map (S.toString) ({x: 'foo', y: true, z: 42})
//. {x: '"foo"', y: 'true', z: '42'}
//. ```
//.
//. Opting out of type checking may cause type errors to go unnoticed.
//.
//. ```javascript
//. > S.unchecked.add (2) ('2')
//. '22'
//. ```

//. ### Classify

//# type :: Any -> { namespace :: Maybe String, name :: String, version :: NonNegativeInteger }
Expand Down
5 changes: 5 additions & 0 deletions test/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ test ('create', function() {
eq (S.sort (Object.keys (uncheckedDefaultEnv))) (expected);
eq (S.sort (Object.keys (uncheckedCustomEnv))) (expected);

eq (S.toString (checkedDefaultEnv.unchecked.env)) (S.toString (checkedDefaultEnv.env));
eq (S.toString (checkedCustomEnv.unchecked.env)) (S.toString (checkedCustomEnv.env));
eq (S.toString (uncheckedDefaultEnv.unchecked.env)) (S.toString (uncheckedDefaultEnv.env));
eq (S.toString (uncheckedCustomEnv.unchecked.env)) (S.toString (uncheckedCustomEnv.env));

eq (uncheckedDefaultEnv.add (1) (42)) (S.add (1) (42));
eq (uncheckedDefaultEnv.add (1) ('XXX')) ('1XXX');

Expand Down
15 changes: 15 additions & 0 deletions test/unchecked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var S = require ('..');

var eq = require ('./internal/eq');


test ('unchecked', function() {

eq (S.unchecked.add (2) (2)) (4);
eq (S.unchecked.add (2) ('2')) ('22');
eq (S.unchecked.add ('2') (2)) ('22');
eq (S.unchecked.add ('2') ('2')) ('22');

});

0 comments on commit db8d85b

Please sign in to comment.