forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructN.js
52 lines (43 loc) · 1.45 KB
/
constructN.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
var assert = require('assert');
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('constructN', function() {
var Circle = function(r) {
this.r = r;
this.colors = Array.prototype.slice.call(arguments, 1);
};
Circle.prototype.area = function() {return Math.PI * Math.pow(this.r, 2);};
it('turns a constructor function into a function with n arguments', function() {
var circle = R.constructN(2, Circle);
var c1 = circle(1, 'red');
eq(c1.constructor, Circle);
eq(c1.r, 1);
eq(c1.area(), Math.PI);
eq(c1.colors, ['red']);
var regex = R.constructN(1, RegExp);
var pattern = regex('[a-z]');
eq(pattern.constructor, RegExp);
eq(pattern.source, '[a-z]');
});
it('can be used to create Date object', function() {
var date = R.constructN(3, Date)(1984, 3, 26);
eq(date.constructor, Date);
eq(date.getFullYear(), 1984);
});
it('supports constructors with no arguments', function() {
function Foo() {}
var foo = R.constructN(0, Foo)();
eq(foo.constructor, Foo);
});
it('does not support constructor with greater than ten arguments', function() {
assert.throws(function() {
function Foo($0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10) {
this.eleventh = $10;
}
R.constructN(11, Foo);
}, function(err) {
return (err instanceof Error &&
err.message === 'Constructor with greater than ten arguments');
});
});
});