Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Definitely Maybe with Proxy
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))
@fflorent

This comment has been minimized.

Copy link

fflorent commented Apr 24, 2015

That's neat. Why do you return true line 15 (has() { return true },)? Because ("foo" in Maybe(null)) === true sounds weird to me.

Florent

@fflorent

This comment has been minimized.

Copy link

fflorent commented Apr 24, 2015

Also I get this error (using Firefox):

>>> Maybe(self).document.querySelectorAll("foo")
TypeError: 'querySelectorAll' called on an object that does not implement interface Document.

I don't know if there is anything to do to prevent this error?

@zenparsing

This comment has been minimized.

Copy link
Owner Author

zenparsing commented May 4, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.