A way to prevent properties to be added to an object if they are null or undefined.

Naveen Chawla naveen.chwl at gmail.com
Sat Dec 2 11:09:10 UTC 2017


Sounds like if you just use undefined instead of null, your mongodb case
will work

On Fri, 1 Dec 2017, 12:17 am Rodrigo Carranza, <rodrigocarranza at outlook.com>
wrote:

> The end goal is to have a way to control if a property is added or not on
> declaration.
>
> I’ve shown an use case for mongodb driver, you have pointed that it could
> also be used for serialization, but it could have many other uses.
>
>
>
> This object:
>
>
>
> ```js
>
> let foo = {
>
>   bar : null
>
> }
>
> ```
>
>
>
> Is completely different from this one:
>
>
>
> ```js
>
> let foo = {}
>
> ```
>
> And a library could behave differently if the property is present with
> null or not. Then you would have to remember to clean your objects every
> time to find if there is a null inside.
>
>
>
> You could do this imperatively but it is too cumbersome. It is better to
> have a more declarative way to do this.
>
> There is a proposal for null aware operator for chaining but it could be
> extended to other behaviors.
>
>
>
> *De:* Naveen Chawla [mailto:naveen.chwl at gmail.com]
> *Enviado el:* miércoles, 29 de noviembre de 2017 1:54
> *Para:* Rodrigo Carranza
> *CC:* jdschulteis at yahoo.com; es-discuss at mozilla.org
>
>
> *Asunto:* Re: A way to prevent properties to be added to an object if
> they are null or undefined.
>
>
>
> What is the end goal of this proposal? To reduce the size of a serialized
> JSON? I can't see any other use for it. If that's the case, then perhaps a
> stringify that skips undefineds and nulls is more appropriate. Otherwise,
> what is the purpose / example case where this would be useful?
>
>
>
> On Wed, 29 Nov 2017 at 12:01 Rodrigo Carranza <rodrigocarranza at outlook.com>
> wrote:
>
> ------------------------------
>
> > I was about to hit send on a post also suggesting a helper function, but after thinking about it a bit more, Rodrigo's suggestion resembles extending the optional chaining proposal
>
> > https://github.com/tc39/proposal-optional-chaining <https://github.com/tc39/proposal-optional-chaining>
>
> > into the object literal notation. I suggest using the same character pair, "?.", as is proposed for optional chaining, instead of just "?".
>
> > ```js
>
> > let ret = {
>
> >     couldBeNull?.,
>
> >     bar?.: couldBeNull
>
> > }
>
> > ```
>
> >
>
> > The helper function might be best in the case where an empty string value needs the same treatment as null or undefined.
>
> That proposal is awesome, but I think the real operator here is the `?`, in combination with the chaining operator `?.` but it could be anything with the same semantics `?:` for objects `?.` for chaining `?=` for assignment `??` binary comparison. Just like in Dart but with extended functionality.
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171202/6b7092b9/attachment.html>


More information about the es-discuss mailing list