CoffeeScript turns user?.id
into
if (typeof user !== "undefined" && user !== null) {
user.id;
}
Is it possible to create a JavaScript function exists
that would do something similar? i.e.
exists(user).id
would result in either user.id
or null
It would be easier if a function accepts another parameter, i.e. exists(user, 'id')
, but that wouldn't look as nice.
No, you can't produce such a function. The problem is that this:
any_function(undeclared_variable)
will produce a ReferenceError if undeclared_variable
was not declared anywhere. For example, if you run this stand alone code:
function f() { }
f(pancakes);
you'll get a ReferenceError because pancakes
was not declared anywhere. Demo: http://jsfiddle.net/ambiguous/wSZaL/
However, the typeof
operator can be used on something that has not been declared so this:
console.log(typeof pancakes);
will simply log an undefined
in the console. Demo: http://jsfiddle.net/ambiguous/et2Nv/
If you don't mind possible ReferenceErrors then you already have the necessary function in your question:
function exists(obj, key) {
if (typeof obj !== "undefined" && obj !== null)
return obj[key];
return null; // Maybe you'd want undefined instead
}
or, since you don't need to be able to use typeof
on undeclared variables here, you can simplify it down to:
function exists(obj, key) {
if(obj != null)
return obj[key];
return null;
}
Note the change to !=
, undefined == null
is true even though undefined === null
is not.
Very old question but made me thinking about this solution.
exists = (obj) => obj || {}
exists(nullableObject).propName;
I think this functional approach might be interesting until optional chaining is included in JavaScript (State 1 of TC39):
Using proxies and a Maybe
monad, you can implement optional chaining with default value return in case of failure.
A wrap()
function is used to wrap objects on which you want to apply optional chaining. Internally, wrap
creates a Proxy around your object and manages missing values using a Maybe
wrapper.
At the end of the chain, you unwrap the value by chaining getOrElse(default)
with a default value which is returned when the chain is not valid:
const obj = {
a: 1,
b: {
c: [4, 1, 2]
},
c: () => 'yes'
};
console.log(wrap(obj).a.getOrElse(null)) // returns 1
console.log(wrap(obj).a.b.c.d.e.f.getOrElse(null)) // returns null
console.log(wrap(obj).b.c.getOrElse([])) // returns [4, 1, 2]
console.log(wrap(obj).b.c[0].getOrElse(null)) // returns 4
console.log(wrap(obj).b.c[100].getOrElse(-1)) // returns -1
console.log(wrap(obj).c.getOrElse(() => 'no')()) // returns 'yes'
console.log(wrap(obj).d.getOrElse(() => 'no')()) // returns 'no'
wrap(obj).noArray.getOrElse([1]).forEach(v => console.log(v)) // Shows 1
wrap(obj).b.c.getOrElse([]).forEach(v => console.log(v)) // Shows 4, 1, 2
The complete example:
class Maybe {
constructor(value) {
this.__value = value;
}
static of(value){
if (value instanceof Maybe) return value;
return new Maybe(value);
}
getOrElse(elseVal) {
return this.isNothing() ? elseVal : this.__value;
}
isNothing() {
return this.__value === null || this.__value === undefined;
}
map(fn) {
return this.isNothing()
? Maybe.of(null)
: Maybe.of(fn(this.__value));
}
}
function wrap(obj) {
function fix(object, property) {
const value = object[property];
return typeof value === 'function' ? value.bind(object) : value;
}
return new Proxy(Maybe.of(obj), {
get: function(target, property) {
if (property in target) {
return fix(target, property);
} else {
return wrap(target.map(val => fix(val, property)));
}
}
});
}
const obj = { a: 1, b: { c: [4, 1, 2] }, c: () => 'yes' };
console.log(wrap(obj).a.getOrElse(null))
console.log(wrap(obj).a.b.c.d.e.f.getOrElse(null))
console.log(wrap(obj).b.c.getOrElse([]))
console.log(wrap(obj).b.c[0].getOrElse(null))
console.log(wrap(obj).b.c[100].getOrElse(-1))
console.log(wrap(obj).c.getOrElse(() => 'no')())
console.log(wrap(obj).d.getOrElse(() => 'no')())
wrap(obj).noArray.getOrElse([1]).forEach(v => console.log(v)) // Shows 1
wrap(obj).b.c.getOrElse([]).forEach(v => console.log(v)) // Shows 4, 1, 2