zenika-open-source / immutadot

immutadot is a JavaScript library to deal with nested immutable structures.
https://immutadot.zenika.com
MIT License
178 stars 5 forks source link

Apply modifications on object root #274

Closed thcolin closed 6 years ago

thcolin commented 6 years ago

Prerequisites

Object.assign({}, { entities: {}, result: [] })

Only for bugs :bug:

Version : 1.0.0

Expected behavior

Be able to assign values on object root

Actual behavior

Throw TypeError: path should not be empty

How to reproduce

See runkit

Only for feature requests :sparkles:

Don't know if it's possible or a good practice, but it would be nice to be able to assign values on object root

Example

const { assign } = require('immutadot')

const current = {}
const next = assign(current, '', { entities: {}, result: [] })
// { entities: {}, result: [] }
frinyvonnick commented 6 years ago

To apply operations on root object like you do with assign, you can write it with a . as path parameter :

const { assign } = require('immutadot')

const current = {}
const next = assign(current, '.', { entities: {}, result: [] })
// { entities: {}, result: [] }
thcolin commented 6 years ago

Thanks a lot, I should tried this ! You can close it if you want !

frinyvonnick commented 6 years ago

My bad it will create an empty named property with the value :

{
  "": { entities: {}, result: [] }
}
frinyvonnick commented 6 years ago

We won't implement operations on root object because javascript already provides tools for that. In your example Object.assign({}, { entities: {}, result: [] }) is already immutable.

I close this issue, feel free to reopen it if you have more arguments.

thcolin commented 6 years ago

No problems, thanks for your time !