Show HN: Nothing.js – A chainable mock object which always returns itself
github.com
github.com
Where they might have caused a bug, 90% of the time Flow will catch them, and insist that I do a null check and handle that case.
And when they do crop up, it's usually more of a serious failure that I'm happy to trigger an exception early, rather than having it propagate further down the stack by using something like nothing.js. Especially given the list of gotchas. The fact that `Nothing` is not falsy is really problematic.
I'm pretty convinced at this point that `a && a.b && a.b.c && a.b.c.d` is an anti-pattern in javascript anyway; if I'm that unsure about the structure of my data, I have more serious problems than just trying to avoid null pointers.
[0] https://www.npmjs.com/package/babel-plugin-transform-optiona...
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
> 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.
Proxy pays a big performance penalty, and I doubt it'll be improving any time soon. In addition, I believe it's not possible to polyfill Proxy. An alternative could be to just wrap the deep property access in a try/catch:
function getQux (input) {
try {
return input.foo.bar.baz.qux()
} catch (e) {
return null
}
}It's good for unit testing though.
All in all, I'd like to have some kind of "?" operator in Python, I do think the pros are most of the time greater than the cons.
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.
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
class Whatever; def method_missing *_; self; end; end
Sample usage: irb(main):001:0> Whatever.new.foo.bar.baz - 2
=> #<Whatever:0x007fd7ed0a2a68>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.
However, it is a handy feature for writing tests. I wrote a similar library for testing called magical-mock based on the python library. https://www.npmjs.com/package/magical-mock
TS-like syntax for clarity:
Function Nothing(vTestVar:any, vIfnull:any, sObjPath?: string, bStrict?: boolean) {... return vResultOfCheck}Does anyone know of a working Proxy polyfill? I've never been able to find one.
The implementation uses `Symbol` and [`Proxy`](https://github.com/GoogleChrome/proxy-polyfill) behind the hood so you might need to use apropriate polyfills in case you want to support older browsers.
2. Personally, I find the name Nothing a bit too long. Why not use a shorter name like None?