Last active
August 29, 2015 14:01
-
-
Save jussi-kalliokoski/76d02cb80c8a2c2def86 to your computer and use it in GitHub Desktop.
Natural language constructs in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createSentence = function () { | |
var names = [].slice.call(arguments); | |
var fn = names.pop(); | |
if ( names.length === 0 ) { | |
return fn; | |
} | |
var name = names.shift(); | |
return function (value) { | |
var obj = {}; | |
obj[name] = createSentence.apply(null, names.concat(fn.bind(null, value))); | |
return obj; | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("createSentence", function () { | |
it("should create a one-level chain given one name", function () { | |
var combine = createSentence("with", function (a, b) { | |
return a + " " + b; | |
}); | |
combine("foo").with("bar").should.equal("foo bar"); | |
}); | |
it("should create a two-level chain given two names", function () { | |
var createPerson = createSentence("withFirstName", "withLastName", function (nickName, firstName, lastName) { | |
return { | |
nickName: nickName, | |
firstName: firstName, | |
lastName: lastName | |
}; | |
}); | |
createPerson("blackjack").withFirstName("Jack").withLastName("Black").should.deep.equal({ | |
nickName: "blackjack", | |
firstName: "Jack", | |
lastName: "Black" | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment