find-value-locations

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


Keywords
find, value, property, location, prototype, key, object, symbol, locate, javascript, locations
License
MIT
Install
npm install find-value-locations@2.0.0

Documentation

find-value-locations Version Badge

github actions coverage 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