sodiray / radash

Functional utility library - modern, simple, typed, powerful
https://radash-docs.vercel.app
MIT License
4.18k stars 167 forks source link

construct removes falsy values #316

Open JacobWeisenburger opened 1 year ago

JacobWeisenburger commented 1 year ago
console.log( construct( { 'name.first': 'John', 'name.last': 'Smith' } ) )
// { name: { first: 'John', last: 'Smith' } }

console.log( construct( { 'name.first': 'John', 'name.last': null } ) )
// { name: { first: 'John' }, but expected { name: { first: 'John', last: null } }

https://radash-docs.vercel.app/docs/object/construct this is not documented and I don't understand why this is needed. I would like to be able to construct objects without removing falsy values.

JacobWeisenburger commented 1 year ago

the problem seems to be with set.

console.log( set( {}, 'name.first', null ) )
// {}, but expected { name: { first: null } }

https://github.com/rayepps/radash/blob/03dd3152f560414e933cedcd3bda3c6db3e8306b/src/object.ts#L240-L264

JacobWeisenburger commented 1 year ago

I've fixed this for my use case using lodash. But I would really like to be able to use this with only radash. So it would be nice if this was fixed in the future.

export const construct = <TObject extends object> ( obj: TObject ): object => {
    if ( !obj ) return {}
    return Object.keys( obj ).reduce( ( acc, path ) => {
        /* uses lodash.set instead of radash.set */
        return lodash.set( acc, path, ( obj as any )[ path ] )
    }, {} )
}
aleclarson commented 1 month ago

Hi @JacobWeisenburger, this is fixed in the Radashi fork, along with many other things. Check us out!