Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Rich-Harris/object-cull

Create a copy of an object with just the bits you actually need
https://github.com/Rich-Harris/object-cull

Last synced: 16 days ago
JSON representation

Create a copy of an object with just the bits you actually need

Awesome Lists containing this project

README

        

# object-cull

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

```js
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

* [js-off](https://github.com/reconbot/js-off)

## License

MIT