Skip to content

Instantly share code, notes, and snippets.

@zenparsing
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save zenparsing/9ff3036b6eb15fa436e4 to your computer and use it in GitHub Desktop.

Select an option

Save zenparsing/9ff3036b6eb15fa436e4 to your computer and use it in GitHub Desktop.
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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Author

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