Skip to content

Instantly share code, notes, and snippets.

@searls
Created February 1, 2012 23:58
Show Gist options
  • Select an option

  • Save searls/1720249 to your computer and use it in GitHub Desktop.

Select an option

Save searls/1720249 to your computer and use it in GitHub Desktop.
object without properties
_.mixin
withoutProperties: (object,failTest) ->
newObj = _(object).clone()
_(object).each (val,key) ->
delete newObj[key] if failTest(val,key) == true
newObj
describe "foo", ->
Given -> @data = a: 0, b: "foo"
When -> @result = _(@data).withoutProperties (v,k) -> k == 'b'
Then -> expect(@result).toEqual(a:0)
@davemo

davemo commented Feb 3, 2012

Copy link
Copy Markdown

I'm curious, why is the failTest required?

@searls

searls commented Feb 3, 2012

Copy link
Copy Markdown
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.

@davemo

davemo commented Feb 4, 2012

Copy link
Copy Markdown

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