https://github.com/trapcodeio/object-collection
Break objects into pieces and control them from those pieces.
https://github.com/trapcodeio/object-collection
collection javascript lodash object
Last synced: 12 months ago
JSON representation
Break objects into pieces and control them from those pieces.
- Host: GitHub
- URL: https://github.com/trapcodeio/object-collection
- Owner: trapcodeio
- Created: 2019-06-03T08:09:05.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-16T18:38:23.000Z (about 3 years ago)
- Last Synced: 2025-03-18T16:15:54.455Z (12 months ago)
- Topics: collection, javascript, lodash, object
- Language: TypeScript
- Homepage:
- Size: 435 KB
- Stars: 5
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Javascript object collection.
[**NPM**](https://www.npmjs.com/package/object-collection) |
[**YARN**](https://yarnpkg.com/en/package/object-collection)
Built from **Lodash**'s object functions api.
so instead of `_.extend(obj, data)` you do `data.extend({})`.
All lodash helpers that **mutates** object returns `this`
### When to use ObjectCollection.
ObjectCollection is best used when accessing large objects **e.g** Api data, Your config object e.t.c
### Usage
```javascript
const Obj = require("object-collection");
// Creates empty object
let data = new Obj();
// => {}
const User = {name: "John", age: 32, gender: "male"};
// Use already existing object.
const user = new Obj(User);
// => {name: "John", age: 32, gender: 'male'}
// OR Use is a static helper to create new collection instance
const user = Obj.use(User);
// => {name: "John", age: 32, gender: "male"}
user.has("name")
// => true
user.pick(['name', 'age']);
// => {name: "John", age: 32}
user.set({hobbies: ['code', 'eat', 'sleep']});
// => {name: "John", age: 32, gender: "male", hobbies: ['code', 'eat', 'sleep']}
```
You get the idea right? All object helpers in `lodash` are available on `this`
We also added a few more helpers. e.g
If a path in your object holds an `object` we can access it as a collection using `.path` helper
```javascript
// Add contact_details to User
user.set('contact_details', {
address: 'No 1 Astro World',
phone: '+123456789',
country: 'US',
});
user.path("contact_details");
// Returns message value as a collection
user.get("contact_details.address");
//OR
user.path("contact_details").get('address');
// => No 1 Astro World
user.path("contact_details").pick(['phone', 'US'])
// => {phone: '+123456789', country: 'US'}
```
#### Full Docs coming soon