find-value-locations

2.0.5 • Public • Published

find-value-locations Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

Given an object, and a value, return a tuple of the property name, the object on which it is an own property, and the property descriptor.

Works with string keys, Symbol keys, both enumerable and non-enumerable keys, and crawls up the prototype chain to find the exact object it's located on.

Example

var findValue = require('find-value-locations');
var assert = require('assert');

var value = {};

function Foo() {}
function Bar() {}
function Baz() {}

Object.prototype.property = value;
Function.prototype.property = value;

Bar.prototype = Baz;

Foo.prototype = new Bar();
Foo.prototype.property = value;
var symbol = Symbol('a symbol property');
Foo.prototype[symbol] = value;

var tuples = findValue(new Foo(), value);

assert.deepEqual(tuples, [
	[Foo.prototype, 'property', Object.getOwnPropertyDescriptor(Foo.prototype, 'property')],
	[Foo.prototype, symbol, Object.getOwnPropertyDescriptor(Foo.prototype, symbol)],
	[Function.prototype, 'property', Object.getOwnPropertyDescriptor(Function.prototype, 'property')],
	[Object.prototype, 'property', Object.getOwnPropertyDescriptor(Object.prototype, 'property')]
]);

Tests

Simply clone the repo, npm install, and run npm test

Package Sidebar

Install

npm i find-value-locations

Weekly Downloads

1

Version

2.0.5

License

MIT

Unpacked Size

18.1 kB

Total Files

10

Last publish

Collaborators

  • ljharb