zenika-open-source/immutadot

Apply modifications on object root

thcolin opened this issue · 5 comments

Prerequisites

Description

I can't assign values on object root like I sometimes do in vanilla:

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

Only for bugs 🐛

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 ✨

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: [] }

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: [] }

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

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

{
  "": { entities: {}, result: [] }
}

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.

No problems, thanks for your time !