Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/Rich-Harris/object-cull
- Owner: Rich-Harris
- License: mit
- Created: 2019-05-10T17:26:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-01T02:28:57.000Z (over 1 year ago)
- Last Synced: 2024-10-02T13:34:50.917Z (about 1 month ago)
- Language: TypeScript
- Size: 122 KB
- Stars: 144
- Watchers: 4
- Forks: 2
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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); // Rachelleconst { 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