Live data from Hacker News

Show HN: Nothing.js – A chainable mock object which always returns itself

github.com

1–10 of 43 posts

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#2
Perhaps use the hint parameter of Symbol.toPrimitive() to fix some gotchas like Nothing + 123 => '123'?

See MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Edit: Won't help, because hint == 'number' is only triggered if used with unary plus or minus. Disregard this comment.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#3
post #2

Perhaps use the hint parameter of Symbol.toPrimitive() to fix some gotchas like Nothing + 123 => '123'? See MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Edit: Won't help, because hint == 'number' is only triggered if used with unary plus or minus. Disregard this comment.

Nope, hint won't help here as it gets `default` instead of `number`.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#4
Wouldn't this swallow errors that you wouldn't want it to swallow? e.g. you try to access a property on a value that's null (which you think isn't null), and instead of getting an error, nothing happens. Idx[0] is a way better solution to dealing with the boilerplate of checking for null/undefined that has 0 runtime cost due to being compiled away by babel. It works with type systems, and you can even use it as a babel macro[1].

With it

    idx(props, _ => _.user.friends[0].friends)
compiles to

    props.user == null ? props.user :
    props.user.friends == null ? props.user.friends :
    props.user.friends[0] == null ? props.user.friends[0] :
    props.user.friends[0].friends
Optional chaining[2] would add this to JS at the language level by using ?. as the operator.

[0]: https://github.com/facebookincubator/idx [1]: https://github.com/dralletje/idx.macro [2]: https://github.com/tc39/proposal-optional-chaining

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#5
post #4

Wouldn't this swallow errors that you wouldn't want it to swallow? e.g. you try to access a property on a value that's null (which you think isn't null), and instead of getting an error, nothing happens. Idx[0] is a way better solution to dealing with the boilerplate of checking for null/undefined that has 0 runtime cost due to being compiled away by babel. It works with type systems, and you can even use it as a bab…

idx requires a transpilation step.

> you try to access a property on a value that's null (which you think isn't null), and instead of getting an error, nothing happens

You can check for Nothing if you know you shouldn't get one. It's not about swallowing errors, it's about removing try/catch and all this `a && a.b && a.b.c && a.b.c()` boilerplate code.

> Optional chaining[2] would add this to JS at the language level by using ?. as the operator

But it's not there yet and it will require a transpilation step for quite some time before all the major browsers will introduce this feature.

Nothing returns "safe" default values instead of null/undefined, which might come in useful in some cases. Also, it can be used in unit tests to mock some deeply-nested constructs.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#7

> The implementation uses Symbol and Proxy behind the hood so you might need to use appropriate polyfills... Does anyone know of a working Proxy polyfill? I've never been able to find one.

It's not possible to polyfill.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#8

> The implementation uses Symbol and Proxy behind the hood so you might need to use appropriate polyfills... Does anyone know of a working Proxy polyfill? I've never been able to find one.

I've seen this one: https://github.com/GoogleChrome/proxy-polyfill

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#9
This would really benefit from adding comments in the 'how to use' code samples, or at least using standard foos and baars to make it clear what is user code and what is part of Nothing.js.

For example it is not really clear if 'executeAction()' etc are part of Nothing.js. I assumed they were, but then the next sample seems to do property chain traversal transparently, without them.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#10
Interesting project, but I'd stick with idx[0] since I have transpilation setup most of the time anyway.

Also some of you could be interested in optional.js[1], it's Java's Optional for js, makes it arguably more pleasant to deal with null values, no help for chaining though!

[0] https://github.com/facebookincubator/idx [1] https://github.com/JasonStorey/Optional.js

Post reply on HN