From 814c14bb80e9d17f31e0b92e676f2bec582157d2 Mon Sep 17 00:00:00 2001 From: Edan Schwartz Date: Mon, 22 Sep 2014 08:35:03 -0500 Subject: [PATCH 1/2] Ch4: use `validator` to create the `hasKeys` validator - We just talked about abstracting the creation of validator functions. Why go back to using the clunky `fn.message` assignment now? --- chapter04.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/chapter04.js b/chapter04.js index a15c1a6..d2d779e 100644 --- a/chapter04.js +++ b/chapter04.js @@ -201,15 +201,13 @@ var checkCommand = checker(validator("must be a map", aMap)); function hasKeys() { var KEYS = _.toArray(arguments); + var message = cat(["Must have values for keys:"], KEYS).join(" ") - var fun = function(obj) { + return validator(message, function(obj) { return _.every(KEYS, function(k) { return _.has(obj, k); }); - }; - - fun.message = cat(["Must have values for keys:"], KEYS).join(" "); - return fun; + }); } var checkCommand = checker(validator("must be a map", aMap), hasKeys('msg', 'type')); From 88e06ef49e7426c54d00919a71fc156847db65f9 Mon Sep 17 00:00:00 2001 From: Edan Schwartz Date: Mon, 22 Sep 2014 08:37:00 -0500 Subject: [PATCH 2/2] Ch4: Rename `hasKeys` to `hasKeysValidator` - It seems as though there is a de-facto naming standard in "Functional Javascript" of using nouns for functions which return functions. eg. `checker` returns a function, while `checkCommand` returns an array. If this is correct, it would follow that `hasKeys` would return a boolean, while in fact, if returns a validator function. Renaming the function to `hasKeysValidator` makes it clear that the function returns a validator function. --- chapter04.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chapter04.js b/chapter04.js index d2d779e..91a5862 100644 --- a/chapter04.js +++ b/chapter04.js @@ -199,9 +199,9 @@ function aMap(obj) { var checkCommand = checker(validator("must be a map", aMap)); -function hasKeys() { +function hasKeysValidator() { var KEYS = _.toArray(arguments); - var message = cat(["Must have values for keys:"], KEYS).join(" ") + var message = cat(["Must have values for keys:"], KEYS).join(" "); return validator(message, function(obj) { return _.every(KEYS, function(k) { @@ -210,4 +210,4 @@ function hasKeys() { }); } -var checkCommand = checker(validator("must be a map", aMap), hasKeys('msg', 'type')); +var checkCommand = checker(validator("must be a map", aMap), hasKeysValidator('msg', 'type'));