Skip to content
Create a copy of an object with just the bits you actually need
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
scripts
src
test
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
appveyor.yml
mocha.opts
package-lock.json
package.json
rollup.config.js
tsconfig.json

README.md

object-cull

Create a copy of an object, based on which properties were accessed.

import { prepare, apply } from 'object-cull';

const proxy = prepare({
	firstname: 'Terrell',
	lastname: 'Snider',
	friends: [
		{ firstname: 'Rachelle', lastname: 'Knight' },
		{ firstname: 'Ila', lastname: 'Farrell' },
		{ firstname: 'Vasquez', lastname: 'Flynn' }
	],
	pets: [
		{ name: 'Bobo', species: 'Great Dane' }
	]
});

console.log(proxy.firstname); // Terrell
console.log(proxy.friends[0].firstname); // Rachelle

const { kept, culled } = apply(proxy);

console.log(kept);
/*
{ firstname: 'Terrell', friends: [ { firstname: 'Rachelle' } ] }
*/

console.log(culled);
/*
[
	{ path: 'lastname', value: 'Snider' },
	{ path: 'friends.0.lastname', value: 'Knight' },
	{ path: 'friends.1', value: { firstname: 'Ila', lastname: 'Farrell' } },
	{ path: 'friends.2', value: { firstname: 'Vasquez', lastname: 'Flynn' } },
	{ path: 'pets', value: [ { name: 'Bobo', species: 'Great Dane' } ] }
]
*/

const unused_bytes = culled.reduce((total, item) => {
	return total + JSON.stringify(item.value).length;
}, 0);

const percent_unused = 100 * unused_bytes / JSON.stringify(proxy).length;

console.log(`${percent_unused}% of the data was unused`);

Why?

Mostly for server-side rendering. You might not need to serialize all your data to send it to the client.

Prior art

License

MIT

You can’t perform that action at this time.