This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partial.js
71 lines (57 loc) · 2 KB
/
partial.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
var expect = require("must")
, $ = require("../lib/partial")
describe("`partial`", function() {
describe("given a function", function() {
describe("and no other parameters", function() {
it("should just return the given function", function() {
var fun = $(test)
expect(fun).to.equal(test)
function test(a, b) {}
})
})
})
describe("and some parameters", function() {
it("should return a left-to-right partially applied function", function() {
expect(test(2, 5)).to.equal(7)
var fun = $(test, 2)
expect(fun(5)).to.equal(7)
function test(a, b) {
expect(a).to.equal(2)
expect(b).to.equal(5)
return a + b
}
})
describe("when a parameter is identical to the `partial` function", function() {
describe("and when the partially applied function is called", function() {
it("should replace the `partial` parameter with the first argument passed", function() {
var π = Math.PI
expect(test(1, "wibble", 3)).to.equal(π)
var fun = $(test, 1, $, 3)
expect(fun("wibble")).to.equal(π)
function test(a, b, c) {
expect(a).to.equal(1)
expect(b).to.equal("wibble")
expect(c).to.equal(3)
return π
}
})
})
})
describe("when there are more than one `partial` parameter", function() {
describe("and when the partially applied function is called", function() {
it("should replace the `partial` parameters in a left-to-right fashion", function() {
expect(test(9, "7", false, null)).to.equal("wibble")
var fun = $(test, $, "7", $, null)
expect(fun(9, false)).to.equal("wibble")
function test(a, b, c, d) {
expect(a).to.equal(9)
expect(b).to.equal("7")
expect(c).to.equal(false)
expect(d).to.equal(null)
return "wibble"
}
})
})
})
})
})