Skip to content

mk-pmb/getown-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Lookup an object property only if it's its own property.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published