Created
February 1, 2012 23:58
-
-
Save searls/1720249 to your computer and use it in GitHub Desktop.
object without properties
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
| _.mixin | |
| withoutProperties: (object,failTest) -> | |
| newObj = _(object).clone() | |
| _(object).each (val,key) -> | |
| delete newObj[key] if failTest(val,key) == true | |
| newObj |
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 "foo", -> | |
| Given -> @data = a: 0, b: "foo" | |
| When -> @result = _(@data).withoutProperties (v,k) -> k == 'b' | |
| Then -> expect(@result).toEqual(a:0) |
Author
As opposed to optional?
The failTest serves the opposite of a truthTest in, say, a select function. te user passes in the test to the function an if it returns true, then that property will be removed from the cloned object.
It seems unnecessary to me is all, because you can blindly issue "delete newObj[key]" without any test and whether it succeeded or failed it won't throw an error. The api for your function could then be simplified such that you can just pass in a list of keys you want deleted, ie
_(@data).withoutProperties(["a", "b", "c"]) ...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm curious, why is the failTest required?