1.0.1 • Published 9 years ago
haccessed v1.0.1
HACCESSED
Snoop on your objects and see what they're up to
About
Haccessed is a function that "hijacks" an object, clones it, and exposes a "hidden" __print__ method. This __print__ method will return the original object filtering out un-accessed properties. It does this by using the Object.defineProperty method, which keeps the original object intact whilst monitoring lookups on properties.
Compatibility
This module is wrapped in UMD, and will work in any JavaScript environment that supports ES5 features.
You can:
- Bundle it in your
webpackbuild. requireit in node.- Use it in
rewireorjest.mock. requireit AMD style forrequirejs.
Use Cases
- Validate which fields of an API are being actively used.
- Inject into a test to validate lookups.
- Verify that fields aren't being used after a deprecation.
- Ensure pseudo-private
_hiddenfields aren't getting called.
Simple Example
const haccessed = require('haccessed');
const myObject = { id: 123, name: 'joel' };
const hijacked = haccessed(myObject);
hijacked.id; // referencing the id
hijacked.__print__(); // => { id: 123 }Complex Example
const haccessed = require('haccessed');
const myObject = {
id: 1,
name: 'joel',
address: {
zip: '55555',
state: 'wa'
},
friends: [{
id: 2,
name: 'bob'
}, {
id: 3,
name: 'jane'
} , {
id: 4,
name: 'sue'
}]
};
const hijacked = haccessed(myObject);
hijacked.id;
hijacked.address.state;
hijacked.friends[0].name;
hijacked.__print__();
/*
{
id: 1,
address: {
state: 'wa'
},
friends: [{ name: 'bob' }]
}
*/TODO
eslinttravis-ci- More fully-featured interface (accessed, inaccessed, unavailable)
- More edgcase tests
- Code coverage