Immutable-js: Set toggle()

Created on 3 Sep 2015  路  3Comments  路  Source: immutable-js/immutable-js

Hi

I've currently have a util function that does something along these lines :

 function toggle (set, v) {
  return set.includes(v) ? set.delete(v) : set.add(v)
}

What about having it directly on the prototype? Something like :
newSet = set.toggle(value)

All 3 comments

Adding things to the prototype is a slippery slope to a hard to follow API and growing library weight. Especially for simple functions, it's preferred to have an external function provide the functionality.

I wished the same today. I agree that simple helpers should not clutter the library, but this seems to be a very common use case of a Set.

While writing a simple helper is not such a big issue, Immutable uses methods in favor of this-less functions so custom helper functions will look very different to builtin methods and it'll create a mixed API. (Adding new methods to the prototype would solve this, but it's almost universally frowned upon.)

Came here to suggest this idea. Looks like someone else has proposed it in #1742 as well

Was this page helpful?
0 / 5 - 0 ratings

Related issues

papercuptech picture papercuptech  路  4Comments

evenstensberg picture evenstensberg  路  4Comments

jshthornton picture jshthornton  路  3Comments

Haroenv picture Haroenv  路  3Comments

ntgn81 picture ntgn81  路  3Comments