Last active
August 29, 2015 14:19
-
-
Save zenparsing/9ff3036b6eb15fa436e4 to your computer and use it in GitHub Desktop.
Definitely Maybe with Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const unwrapSymbol = Symbol(); | |
| function Definitely(x) { | |
| if (x == null || !(unwrapSymbol in x)) | |
| return x; | |
| return x[unwrapSymbol]; | |
| } | |
| const Nothing = new Proxy(Object.seal(function() {}), { | |
| get(target, key) { return key === unwrapSymbol ? void 0 : Nothing }, | |
| set() {}, | |
| has() { return true }, | |
| apply() { return Nothing }, | |
| construct() { return Nothing }, | |
| }); | |
| const maybeHandler = { | |
| get(target, key) { return key === unwrapSymbol ? target : Maybe(target[key]) }, | |
| has(target, key) { return key === unwrapSymbol || key in target }, | |
| apply(target, thisArg, argumentList) { | |
| if (thisArg !== Nothing) | |
| thisArg = Definitely(thisArg); | |
| return Maybe(target.apply(thisArg, argumentList)); | |
| }, | |
| }; | |
| function Maybe(x) { | |
| return x === Nothing || x == null ? Nothing : new Proxy(Object(x), maybeHandler); | |
| } | |
| // alert(Definitely(Maybe(self).document.scripts[0].parentNode.nodeName)) | |
| // alert(Definitely(Maybe(self).document.querySelectorAll("div").length)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah yes. We can't use the "thisArg" supplied to the apply hook in that case, because a Proxy won't work there. Instead, we just call Definitely on the "thisArg" to unwrap it first. Should work now : )
For Nothing's "has" hook, I was thinking that it "has" everything, with the value of all properties being Nothing.