Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete mutability helper #2906

Closed
flying-sheep opened this issue Jan 22, 2015 · 8 comments
Closed

Add delete mutability helper #2906

flying-sheep opened this issue Jan 22, 2015 · 8 comments

Comments

@flying-sheep
Copy link

JS sparse arrays have some advantages.

Setting elements to undefined isn’t the same as deleting them, e.g. [1,,,3].map(console.log) only prints 1 3, not 1 undefined undefined 3.

Therefore there should be a mutability helper that returns a copy with a deleted entry:

{ update } = React.addons

let a = [1,,,2,,3]
let b = update(a, {$delete: 1})

console.log(a)  // → [1, undefined×2, 2, undefined×1, 3]
console.log(b)  // → [1, undefined×4, 3]

PS: the same of course also applies to objects: {foo: undefined} isn’t the same as {}.

@gaearon
Copy link
Collaborator

gaearon commented Jan 22, 2015

You can do this with splice, something like $splice: [[index, 1]]

@flying-sheep
Copy link
Author

no, not at all.

splice replaces items a[index] to a[index+length] with the supplied elements.

if there are only two parameters, or the third is undefined (e.g. by calling splice.apply(..., [index, length,]), the elements are removed.

if there are more than three, … eeh, test it yourself. doesn’t work, that’s for sure. (make sure you use the browser console, not the JSBin console to see what elements are actually empty instead of set to “undefined”!)

@gaearon
Copy link
Collaborator

gaearon commented Jan 22, 2015

Oh sorry. I just realized what you meant and in fact I needed the exact same thing today (for objects).

@zpao
Copy link
Member

zpao commented Jan 22, 2015

Would #2362 cover this?

@gaearon
Copy link
Collaborator

gaearon commented Jan 22, 2015

It's similar but has different behavior for arrays (from Mongo), setting items to null.
@flying-sheep What is your use case for sparse arrays with React?

@gaearon
Copy link
Collaborator

gaearon commented Jan 22, 2015

Honestly I think delete behavior for arrays makes more sense than following what Mongo does.

My rule of thumb is following: if you can do something with update, you need to be able to undo this as well. Since it's possible to set things on an array, you need to be able to unset them to return to the previous state. Special nullifying behavior seems strange in this case.

Edit: I thought about it some more, and I'm in favor of non-holey $unset. It's rare that people use sparse arrays on purpose, and if you do, you can always write a little helper and use it with $apply.

@flying-sheep
Copy link
Author

yeah. it’s pretty strange that it special cases arrays without reason. i’d say we just change #2362 to unconditionally do delKeys.forEach(function(delKey){ delete nextValue[delKey] })

@gaearon
Copy link
Collaborator

gaearon commented Apr 1, 2016

We are handing over update() to the community so I’m closing this.
Please see my note in #6353 (comment).

@gaearon gaearon closed this as completed Apr 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants