1.0.1 • Published 5 months ago
getown v1.0.1
getown
Lookup an object property only if it's its own property.
API
This module exports one function that holds a method:
getown(obj, prop, fallback)
Returns obj[prop]
if obj
is truthy and has an own property prop
,
otherwise returns fallback
.
getown.voc(dict, word)
Alias for getown(dict, word, word)
.
Useful to not repeat yourself.
Especially useful in case you calculate word
on the fly and don't want
to evaluate the formula twice.
Usage
from test/usage.mjs:
import getown from 'getown';
const dict = { foo: 'bar' };
same(getown(dict, 'foo'), 'bar');
same(getown(dict, 'bar'), undefined);
same(getown(dict, 'bar', 321), 321);
same(typeof dict.toString, 'function');
same(getown(dict, 'toString'), undefined);
same(getown.voc(dict, 'foo'), 'bar');
same(getown.voc(dict, 'bar'), 'bar');
same(getown.voc(dict, 'qux'), 'qux');
same(getown(null, 'wow'), undefined);
same(getown(null, 'wow', 'doge'), 'doge');
Known issues
- Needs more/better tests and docs.
License
ISC