Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alveflo/pick
Javascript object extender
https://github.com/alveflo/pick
Last synced: 14 days ago
JSON representation
Javascript object extender
- Host: GitHub
- URL: https://github.com/alveflo/pick
- Owner: alveflo
- License: mit
- Created: 2014-10-06T19:52:37.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-10-16T19:16:57.000Z (about 10 years ago)
- Last Synced: 2023-08-04T01:03:42.331Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 180 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
.pick
====A javascript object and array extender that lets you write more elegant code.
## API
### Object.prototype functions
#### .pick(key)
```javascript
var foo = { x: 1, y: 2 };
foo.pick('x');
// yields { x: 1 }
```
#### .into(obj)
```javascript
var foo = { x: 1, y: 2 };
var bar = { z: 34 };
foo.pick('x', 'y')
.into(bar);
// -> bar = { z: 34, x: 1, y: 2 }
```
#### .mergeWith(array)
```javascript
var foo = { x: 1, y: 2 };
var bar = [ { x: 1 }, { x: 2 }];
foo.pick('y')
.mergeWith(bar);
// -> bar = [ { x: 1, y: 2 }, { x: 2, y: 2 } ]
```
### Array.prototype functions
#### .pick(key)
```javascript
var foo = [ { y: 1, z: 2 }, { y: 2, z: 3 }];
foo.pick('y')
// yields [ { y: 1 }, { y: 2 } ]
```
#### .into(array)
```javascript
var foo = [ { y: 1, z: 2 }, { y: 2, z: 3 }];
var bar = [ { x: 1 }, { x: 2 }];
foo.pick('y')
.into(bar)
// -> bar = [ { x: 1 }, { x: 2 }, { y: 1 }, { y: 2 } ]
```
#### .mergeWith(array)
```javascript
var foo = [ { y: 1, z: 2 }, { y: 2, z: 3 }];
var bar = [ { x: 1 }, { x: 2 }];
foo.pick('y')
.mergeWith(bar)
// -> bar = [ { x: 1, y: 2 }, { x: 2, y: 2 } ]
```
## Notes
This library contains functions that is jacked into Object's and Array's prototype. Take in consideration that this can cause some funny behavior when iterating objects/arrays. Remember, as you always should do, to check `hasOwnProperty` when iterating objects/arrays:
```javascript
for (var key in someObject) {
if (someObject.hasOwnProperty(key)) {
// do some stuff
}
}
```
## Licence
MIT