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 .push for pushing to arrays (and .slice, splice etc?) #1

Open
ebdrup opened this issue Nov 27, 2015 · 8 comments
Open

Add .push for pushing to arrays (and .slice, splice etc?) #1

ebdrup opened this issue Nov 27, 2015 · 8 comments

Comments

@ebdrup
Copy link

ebdrup commented Nov 27, 2015

No description provided.

@laere
Copy link

laere commented Apr 21, 2016

Don't think you need pushing or slice/splicing for this library at all. It's pretty easy to use, and using push within a reducer is mutating state iirc.

@LinusU
Copy link

LinusU commented Jan 21, 2017

I think that idea was to expose an immutable push, e.g.

const obj = { names: ['Alice'] }

dotProp.push(obj, 'names', 'Bob')
// => { names: ['Alice', 'Bob'] }

@ajoslin
Copy link

ajoslin commented Feb 5, 2017

FYI you can do this in two lines:

function push (object, path, value) {
  const array = dotProp.get(object, path)

  return dotProp.set(object, path, array.concat(value))
}

Push would be convenient, though.

@LinusU
Copy link

LinusU commented Feb 6, 2017

Just a small nit, your function will concatenate the value if it is an array, so you can't use it to push arrays to arrays...

could be solved like this:

function push (object, path, value) {
  const array = dotProp.get(object, path)

  return dotProp.set(object, path, [...array, value])
}

@ajoslin
Copy link

ajoslin commented Feb 6, 2017

@LinusU actually, Array.concat handles arrays and single values perfectly.

> [1,2,3].concat([4,5],6)
[ 1, 2, 3, 4, 5, 6 ]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

@LinusU
Copy link

LinusU commented Feb 6, 2017

What I meant is that push and concat doesn't do the same thing:

let a = []

a.push([1, 2])
a.push([3, 4])

console.log(a)
// => [[1, 2], [3, 4]]
let a = []

a = a.concat([1, 2])
a = a.concat([3, 4])

console.log(a)
// => [1, 2, 3, 4]

If the function is named push, it should behave like the builtin push (but being immutable instead)

@ajoslin
Copy link

ajoslin commented Feb 6, 2017

Aha, of course. I see what you mean.

@wellguimaraes
Copy link

Unshift would be welcome too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants