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 pathirreducibles.js
370 lines (287 loc) · 8.99 KB
/
irreducibles.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* globals describe, it */
var assert = require('assert');
var t = require('../index');
var throwsWithMessage = require('./util').throwsWithMessage;
var noop = function () {};
var ko = function (x, message) { assert.strictEqual(x, false, message); };
describe('t.Any', function () {
var T = t.Any;
describe('constructor', function () {
it('should behave like identity', function () {
var value = {};
assert.strictEqual(t.Any(value), value);
});
it('should throw if used with new', function () {
throwsWithMessage(function () {
var x = new T(); // eslint-disable-line
}, '[tcomb] Cannot use the new operator to instantiate the type Any');
});
});
describe('#is(x)', function () {
it('should always return true', function () {
assert.ok(T.is(null));
assert.ok(T.is(undefined));
assert.ok(T.is(0));
assert.ok(T.is(true));
assert.ok(T.is(''));
assert.ok(T.is([]));
assert.ok(T.is({}));
assert.ok(T.is(noop));
assert.ok(T.is(/a/));
assert.ok(T.is(new RegExp('a')));
assert.ok(T.is(new Error()));
});
});
});
//
// irreducible types
//
describe('irreducibles types', function () {
[
{T: t.Nil, x: null},
{T: t.String, x: 'a'},
{T: t.Number, x: 1},
{T: t.Boolean, x: true},
{T: t.Array, x: []},
{T: t.Object, x: {}},
{T: t.Function, x: noop},
{T: t.Error, x: new Error()},
{T: t.RegExp, x: /a/},
{T: t.Date, x: new Date()}
].forEach(function (o) {
var T = o.T;
var x = o.x;
it('should accept only valid values', function () {
assert.deepEqual(T(x), x);
});
it('should throw if used with new', function () {
throwsWithMessage(function () {
var x = new T(); // eslint-disable-line
}, '[tcomb] Cannot use the new operator to instantiate the type ' + t.getTypeName(T));
});
});
});
describe('t.Nil', function () {
describe('#is(x)', function () {
it('should return true when x is null or undefined', function () {
assert.ok(t.Nil.is(null));
assert.ok(t.Nil.is(undefined));
});
it('should return false when x is neither null nor undefined', function () {
ko(t.Nil.is(0));
ko(t.Nil.is(true));
ko(t.Nil.is(''));
ko(t.Nil.is([]));
ko(t.Nil.is({}));
ko(t.Nil.is(noop));
ko(t.Nil.is(new Error()));
ko(t.Nil.is(new Date()));
ko(t.Nil.is(/a/));
ko(t.Nil.is(new RegExp('a')));
});
});
});
describe('t.Boolean', function () {
describe('#is(x)', function () {
it('should return true when x is true or false', function () {
assert.ok(t.Boolean.is(true));
assert.ok(t.Boolean.is(false));
});
it('should return false when x is neither true nor false', function () {
ko(t.Boolean.is(null));
ko(t.Boolean.is(undefined));
ko(t.Boolean.is(0));
ko(t.Boolean.is(''));
ko(t.Boolean.is([]));
ko(t.Boolean.is({}));
ko(t.Boolean.is(noop));
ko(t.Boolean.is(/a/));
ko(t.Boolean.is(new RegExp('a')));
ko(t.Boolean.is(new Error()));
ko(t.Boolean.is(new Date()));
});
});
});
describe('t.Number', function () {
describe('#is(x)', function () {
it('should return true when x is a number', function () {
assert.ok(t.Number.is(0));
assert.ok(t.Number.is(1));
ko(t.Number.is(new Number(1))); // eslint-disable-line
});
it('should return false when x is not a number', function () {
ko(t.Number.is(NaN));
ko(t.Number.is(Infinity));
ko(t.Number.is(-Infinity));
ko(t.Number.is(null));
ko(t.Number.is(undefined));
ko(t.Number.is(true));
ko(t.Number.is(''));
ko(t.Number.is([]));
ko(t.Number.is({}));
ko(t.Number.is(noop));
ko(t.Number.is(/a/));
ko(t.Number.is(new RegExp('a')));
ko(t.Number.is(new Error()));
ko(t.Number.is(new Date()));
});
});
});
describe('t.String', function () {
describe('#is(x)', function () {
it('should return true when x is a string', function () {
assert.ok(t.String.is(''));
assert.ok(t.String.is('a'));
/* jshint ignore:start */
ko(t.String.is(new String('a'))); // eslint-disable-line
/* jshint ignore:end */
});
it('should return false when x is not a string', function () {
ko(t.String.is(NaN));
ko(t.String.is(Infinity));
ko(t.String.is(-Infinity));
ko(t.String.is(null));
ko(t.String.is(undefined));
ko(t.String.is(true));
ko(t.String.is(1));
ko(t.String.is([]));
ko(t.String.is({}));
ko(t.String.is(noop));
ko(t.String.is(/a/));
ko(t.String.is(new RegExp('a')));
ko(t.String.is(new Error()));
ko(t.String.is(new Date()));
});
});
});
describe('t.Array', function () {
describe('#is(x)', function () {
it('should return true when x is an array', function () {
assert.ok(t.Array.is([]));
});
it('should return false when x is not an array', function () {
ko(t.Array.is(NaN));
ko(t.Array.is(Infinity));
ko(t.Array.is(-Infinity));
ko(t.Array.is(null));
ko(t.Array.is(undefined));
ko(t.Array.is(true));
ko(t.Array.is(1));
ko(t.Array.is('a'));
ko(t.Array.is({}));
ko(t.Array.is(noop));
ko(t.Array.is(/a/));
ko(t.Array.is(new RegExp('a')));
ko(t.Array.is(new Error()));
ko(t.Array.is(new Date()));
});
});
});
describe('t.Object', function () {
describe('#is(x)', function () {
it('should return true when x is an object', function () {
function A() {}
assert.ok(t.Object.is({}));
assert.ok(t.Object.is(new A()));
});
it('should return false when x is not an object', function () {
ko(t.Object.is(null));
ko(t.Object.is(undefined));
ko(t.Object.is(0));
ko(t.Object.is(''));
ko(t.Object.is([]));
ko(t.Object.is(noop));
});
});
});
describe('t.Function', function () {
describe('#is(x)', function () {
it('should return true when x is a function', function () {
assert.ok(t.Function.is(noop));
assert.ok(t.Function.is(new Function())); // eslint-disable-line
});
it('should return false when x is not a function', function () {
ko(t.Function.is(null));
ko(t.Function.is(undefined));
ko(t.Function.is(0));
ko(t.Function.is(''));
ko(t.Function.is([]));
ko(t.Function.is({}));
ko(t.Function.is(new String('1'))); // eslint-disable-line
ko(t.Function.is(new Number(1))); // eslint-disable-line
ko(t.Function.is(new Boolean())); // eslint-disable-line
ko(t.Function.is(/a/));
ko(t.Function.is(new RegExp('a')));
ko(t.Function.is(new Error()));
ko(t.Function.is(new Date()));
});
});
});
describe('t.Error', function () {
describe('#is(x)', function () {
it('should return true when x is an error', function () {
assert.ok(t.Error.is(new Error()));
});
it('should return false when x is not an error', function () {
ko(t.Error.is(null));
ko(t.Error.is(undefined));
ko(t.Error.is(0));
ko(t.Error.is(''));
ko(t.Error.is([]));
ko(t.Error.is(new String('1'))); // eslint-disable-line
ko(t.Error.is(new Number(1))); // eslint-disable-line
ko(t.Error.is(new Boolean())); // eslint-disable-line
ko(t.Error.is(/a/));
ko(t.Error.is(new RegExp('a')));
ko(t.Error.is(new Date()));
});
});
});
describe('t.RegExp', function () {
describe('#is(x)', function () {
it('should return true when x is a regexp', function () {
assert.ok(t.RegExp.is(/a/));
assert.ok(t.RegExp.is(new RegExp('a')));
});
it('should return false when x is not a regexp', function () {
ko(t.RegExp.is(null));
ko(t.RegExp.is(undefined));
ko(t.RegExp.is(0));
ko(t.RegExp.is(''));
ko(t.RegExp.is([]));
ko(t.RegExp.is(new String('1'))); // eslint-disable-line
ko(t.RegExp.is(new Number(1))); // eslint-disable-line
ko(t.RegExp.is(new Boolean())); // eslint-disable-line
ko(t.RegExp.is(new Error()));
ko(t.RegExp.is(new Date()));
});
});
});
describe('t.Date', function () {
describe('#is(x)', function () {
it('should return true when x is a Dat', function () {
assert.ok(t.Date.is(new Date()));
});
it('should return false when x is not a Dat', function () {
ko(t.Date.is(null));
ko(t.Date.is(undefined));
ko(t.Date.is(0));
ko(t.Date.is(''));
ko(t.Date.is([]));
ko(t.Date.is(new String('1'))); // eslint-disable-line
ko(t.Date.is(new Number(1))); // eslint-disable-line
ko(t.Date.is(new Boolean())); // eslint-disable-line
ko(t.Date.is(new Error()));
ko(t.Date.is(/a/));
ko(t.Date.is(new RegExp('a')));
});
});
});
describe('t.Type', function () {
describe('#is(x)', function () {
it('should return true if x is a tcomb type', function () {
assert.equal(t.Type.is(t.String), true);
assert.equal(t.Type.is(1), false);
});
});
});