Skip to content

Commit 81e8f1c

Browse files
committed
Add Oolong.property.
1 parent 807f951 commit 81e8f1c

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Unreleased
2+
- Adds [`Oolong.property`][property].
3+
4+
[property]: https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.property
5+
16
## 1.12.0 (Jun 26, 2015)
27
- Adds [`Oolong.setPrototypeOf`][setPrototypeOf].
38

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ For extended documentation on all functions, please see the
6565
- [.mapKeys](https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.mapKeys)(object, callback, [thisArg])
6666
- [.merge](https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.merge)(target, source...)
6767
- [.ownKeys](https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.ownKeys)(object)
68+
- [.property](https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.property)(key)
6869
- [.reject](https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.reject)(object, callback, [thisArg])
6970
- [.setPrototypeOf](https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.setPrototypeOf)(object, prototype)
7071
- [.values](https://github.com/moll/js-oolong/blob/master/doc/API.md#Oolong.values)(object)

doc/API.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Oolong.js API Documentation
2828
- [.mapKeys](#Oolong.mapKeys)(object, callback, [thisArg])
2929
- [.merge](#Oolong.merge)(target, source...)
3030
- [.ownKeys](#Oolong.ownKeys)(object)
31+
- [.property](#Oolong.property)(key)
3132
- [.reject](#Oolong.reject)(object, callback, [thisArg])
3233
- [.setPrototypeOf](#Oolong.setPrototypeOf)(object, prototype)
3334
- [.values](#Oolong.values)(object)
@@ -407,6 +408,16 @@ person.age = 42
407408
Oolong.ownKeys(person) // => ["age"]
408409
```
409410

411+
<a name="Oolong.property" />
412+
### Oolong.property(key)
413+
Returns a function that returns the given property of an object.
414+
415+
**Examples**:
416+
```javascript
417+
var getName = Oolong.property("name")
418+
getName({name: "John"}) // => "John
419+
```
420+
410421
<a name="Oolong.reject" />
411422
### Oolong.reject(object, callback, [thisArg])
412423
Rejects all enumerable properties and returns a new object without those

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,21 @@ exports.merge = function merge(target) {
593593
*/
594594
exports.ownKeys = Object.keys
595595

596+
/**
597+
* Returns a function that returns the given property of an object.
598+
*
599+
* @example
600+
* var getName = Oolong.property("name")
601+
* getName({name: "John"}) // => "John
602+
*
603+
* @static
604+
* @method property
605+
* @param key
606+
*/
607+
exports.property = function(key) {
608+
return function(obj) { return obj[key] }
609+
}
610+
596611
/**
597612
* Rejects all enumerable properties and returns a new object without those
598613
* properties for which the given function returned truthy for.

test/oolong/property_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var $ = require("../..")
2+
3+
describe("Oolong.property", function() {
4+
it("must return a function that returns the given property", function() {
5+
var obj = {name: "John"}
6+
$.property("name")(obj).must.equal("John")
7+
})
8+
9+
it("must return inherited properties", function() {
10+
var obj = Object.create({name: "John"})
11+
$.property("name")(obj).must.equal("John")
12+
})
13+
})

0 commit comments

Comments
 (0)