This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmaybe.js
113 lines (88 loc) · 3.27 KB
/
maybe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* globals describe, it */
var assert = require('assert');
var t = require('../index');
var util = require('./util');
var Point = t.struct({
x: t.Number,
y: t.Number
});
describe('t.maybe(type, [name])', function () {
describe('combinator', function () {
it('should throw if used with wrong arguments', function () {
util.throwsWithMessage(function () {
t.maybe();
}, '[tcomb] Invalid argument type undefined supplied to maybe(type, [name]) combinator (expected a type)');
util.throwsWithMessage(function () {
t.maybe(Point, 1);
}, '[tcomb] Invalid argument name 1 supplied to maybe(type, [name]) combinator (expected a string)');
});
it('should be idempotent', function () {
var MaybeStr = t.maybe(t.String);
assert.ok(t.maybe(MaybeStr) === MaybeStr);
});
it('should be idempotent in production', util.production(function () {
var MaybeStr = t.maybe(t.String);
assert.ok(t.maybe(MaybeStr) === MaybeStr);
}));
it('should be noop with Any', function () {
assert.ok(t.maybe(t.Any) === t.Any);
});
it('should be noop with Nil', function () {
assert.ok(t.maybe(t.Nil) === t.Nil);
});
});
describe('constructor', function () {
it('should throw if used with new', function () {
util.throwsWithMessage(function () {
var T = t.maybe(t.String, 'T');
var x = new T(); // eslint-disable-line
}, '[tcomb] Cannot use the new operator to instantiate the type T');
});
it('should hydrate the elements of the maybe', function () {
var T = t.maybe(Point);
assert.deepEqual(T(null), null);
assert.deepEqual(T(undefined), null);
assert.ok(Point.is(T({x: 0, y: 0})));
});
it('should hydrate the elements of the maybe in production', util.production(function () {
var T = t.maybe(Point);
assert.deepEqual(T(null), null);
assert.ok(typeof T(undefined) === 'undefined');
assert.ok(Point.is(T({x: 0, y: 0})));
}));
it('should be idempotent', function () {
var T = t.maybe(Point);
var p0 = {x: 0, y: 0};
var p1 = T();
var p2 = T(p1);
assert.equal(p0 === p1, false);
assert.equal(p1 === p2, true);
});
it('should be idempotent in production', util.production(function () {
var T = t.maybe(Point);
var p0 = {x: 0, y: 0};
var p1 = T();
var p2 = T(p1);
assert.equal(p0 === p1, false);
assert.equal(p1 === p2, true);
}));
it('should be idempotent on Nil values', function () {
var T = t.maybe(t.String, 'T');
assert.deepEqual(T(null), null);
assert.ok(typeof T(undefined) === 'undefined');
});
it('should play well with JSON.stringify (#227)', function () {
var thing = t.struct({ foo: t.maybe(t.struct({ bar: t.String })) });
assert.strictEqual(JSON.stringify(thing({foo: null})), '{"foo":null}');
assert.strictEqual(JSON.stringify(thing({foo: undefined})), '{}');
});
});
describe('is(x)', function () {
it('should return true when x is an instance of the maybe', function () {
var Radio = t.maybe(t.String);
assert.strictEqual(Radio.is('a'), true);
assert.strictEqual(Radio.is(null), true);
assert.strictEqual(Radio.is(undefined), true);
});
});
});