Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
eq (S.is ($.Either ($.String) ($.Integer)) (S.Left (''))) (true);
eq (S.is ($.Either ($.String) ($.Integer)) (S.Right (0))) (true);
const a = $.TypeVariable ('a');
eq (S.is ($.Array (a)) ([])) (true);
eq (S.is ($.Array (a)) ([1, 2, 3])) (true);
eq (S.is ($.Array (a)) (['foo', 'bar', 'baz'])) (true);
eq (S.is ($.Array (a)) (['foo', true, 42])) (false);
eq (S.is ($.Array (a)) ([Sum (1), Sum (2), Sum (3)])) (false);
eq ((S.create ({checkTypes: true, env: []})).is ($.Array (a)) ([])) (false);
eq ((S.create ({checkTypes: true, env: [$.String]})).is ($.Array (a)) ([])) (true);
eq ((S.create ({checkTypes: true, env: [$.String]})).is ($.Array (a)) ([1, 2, 3])) (false);
eq ((S.create ({checkTypes: true, env: [$.Number]})).is ($.Array (a)) ([1, 2, 3])) (true);
eq ((S.create ({checkTypes: true, env: [Sum.Type]})).is ($.Array (a)) ([Sum (1), Sum (2), Sum (3)])) (true);
});
test ('get', () => {
eq (S.show (S.get)) ('get :: (Any -> Boolean) -> String -> a -> Maybe b');
eq (S.get (S.is ($.Number)) ('x') ({x: 0, y: 42})) (S.Just (0));
eq (S.get (S.is ($.Number)) ('y') ({x: 0, y: 42})) (S.Just (42));
eq (S.get (S.is ($.Number)) ('z') ({x: 0, y: 42})) (S.Nothing);
eq (S.get (S.is ($.String)) ('z') ({x: 0, y: 42})) (S.Nothing);
eq (S.get (S.is ($.String)) ('x') ({x: 0, y: 42})) (S.Nothing);
eq (S.get (S.is ($.RegExp)) ('x') ({x: vm.runInNewContext ('/.*/')})) (S.Just (/.*/));
eq (S.get (S.K (true)) ('valueOf') (null)) (S.Nothing);
eq (S.get (S.K (true)) ('valueOf') (undefined)) (S.Nothing);
eq (S.get (S.is ($.Array ($.Number))) ('x') ({x: [1, 2]})) (S.Just ([1, 2]));
eq (S.get (S.is ($.Array ($.Number))) ('x') ({x: [1, 2, null]})) (S.Nothing);
});
test ('parseJson', () => {
eq (S.show (S.parseJson)) ('parseJson :: (Any -> Boolean) -> String -> Maybe a');
eq (S.parseJson (S.is ($.Any)) ('[Invalid JSON]')) (S.Nothing);
eq (S.parseJson (S.is ($.Array ($.Any))) ('{"foo":"bar"}')) (S.Nothing);
eq (S.parseJson (S.is ($.Array ($.Any))) ('["foo","bar"]')) (S.Just (['foo', 'bar']));
eq (S.parseJson (S.is ($.Array ($.Number))) ('[1,2]')) (S.Just ([1, 2]));
eq (S.parseJson (S.is ($.Array ($.Number))) ('[1,2,null]')) (S.Nothing);
});
eq (S.show (S.gets)) ('gets :: (Any -> Boolean) -> Array String -> a -> Maybe b');
eq (S.gets (S.is ($.Number)) (['x']) ({x: {z: 0}, y: 42})) (S.Nothing);
eq (S.gets (S.is ($.Number)) (['y']) ({x: {z: 0}, y: 42})) (S.Just (42));
eq (S.gets (S.is ($.Number)) (['z']) ({x: {z: 0}, y: 42})) (S.Nothing);
eq (S.gets (S.is ($.Number)) (['x', 'z']) ({x: {z: 0}, y: 42})) (S.Just (0));
eq (S.gets (S.is ($.Number)) (['a', 'b', 'c']) ({x: {z: 0}, y: 42})) (S.Nothing);
eq (S.gets (S.is ($.Number)) ([]) ({x: {z: 0}, y: 42})) (S.Nothing);
eq (S.gets (S.is ($.Object)) ([]) ({x: {z: 0}, y: 42})) (S.Just ({x: {z: 0}, y: 42}));
eq (S.gets (S.is ($.RegExp)) (['x']) ({x: vm.runInNewContext ('/.*/')})) (S.Just (/.*/));
eq (S.gets (S.K (true)) (['valueOf']) (null)) (S.Nothing);
eq (S.gets (S.K (true)) (['valueOf']) (undefined)) (S.Nothing);
eq (S.gets (S.is ($.Array ($.Number))) (['x']) ({x: [1, 2]})) (S.Just ([1, 2]));
eq (S.gets (S.is ($.Array ($.Number))) (['x']) ({x: [1, 2, null]})) (S.Nothing);
});
it ('should resolve lists', () => {
const {types} = resolve ($) ([]) ($.env) ('foo :: [Number] -> [String]');
const expecteds = [$.Array ($.Number), $.Array ($.String)];
const lists = S.zip (types) (expecteds);
assertTypePairs (lists);
});
import $priv from 'sanctuary-def';
import * as Sig from './signature';
const def = $priv.create ({checkTypes: true, env: $priv.env});
const Parameters = $priv.RecordType ({
$: $priv.Object,
checkTypes: $priv.Boolean,
env: $priv.Array ($priv.Type),
typeClasses: $priv.Array ($priv.TypeClass),
});
export const create = def
('create')
({})
([
Parameters,
$priv.String,
$priv.AnyFunction,
$priv.AnyFunction,
])
(({$, checkTypes, env, typeClasses}) => {
const $def = $.create ({checkTypes, env});
const resovleSig = Sig.resolve ($) (typeClasses) (env);
return $def