-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e51b07
commit d67f9bc
Showing
5 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Object Agent | ||
|
||
> A javascript library for working with objects | ||
> | ||
> [![npm][npm]][npm-url] | ||
[![build][build]][build-url] | ||
[![coverage][coverage]][coverage-url] | ||
[![deps][deps]][deps-url] | ||
[![size][size]][size-url] | ||
[![vulnerabilities][vulnerabilities]][vulnerabilities-url] | ||
[![license][license]][license-url] | ||
|
||
|
||
<br><a name="forOwnReduce"></a> | ||
|
||
### forOwnReduce(object, callback, initialValue) ⇒ <code>\*</code> | ||
> Iterates over own properties of an object and returns a reduced value | ||
**Returns**: <code>\*</code> - The accumulated result | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| object | <code>Object</code> | | | ||
| callback | <code>function</code> | Provides three args: result, value, and key | | ||
| initialValue | <code>\*</code> | | | ||
|
||
**Example** | ||
``` javascript | ||
import { forOwnReduce } from 'object-agent'; | ||
|
||
const thing = { | ||
a: 'b', | ||
c: 'd' | ||
}; | ||
|
||
const output = forOwnReduce(thing, (result, value, key) => { | ||
result.push([value, key]); | ||
return result; | ||
}, []); | ||
|
||
console.log(output); | ||
// => [['b', 'a'], ['d', 'c']] | ||
``` | ||
|
||
[npm]: https://img.shields.io/npm/v/object-agent.svg | ||
[npm-url]: https://npmjs.com/package/object-agent | ||
[build]: https://travis-ci.org/DarrenPaulWright/object-agent.svg?branch=master | ||
[build-url]: https://travis-ci.org/DarrenPaulWright/object-agent | ||
[coverage]: https://coveralls.io/repos/github/DarrenPaulWright/object-agent/badge.svg?branch=master | ||
[coverage-url]: https://coveralls.io/github/DarrenPaulWright/object-agent?branch=master | ||
[deps]: https://david-dm.org/darrenpaulwright/object-agent.svg | ||
[deps-url]: https://david-dm.org/darrenpaulwright/object-agent | ||
[size]: https://packagephobia.now.sh/badge?p=object-agent | ||
[size-url]: https://packagephobia.now.sh/result?p=object-agent | ||
[vulnerabilities]: https://snyk.io/test/github/DarrenPaulWright/object-agent/badge.svg?targetFile=package.json | ||
[vulnerabilities-url]: https://snyk.io/test/github/DarrenPaulWright/object-agent?targetFile=package.json | ||
[license]: https://img.shields.io/github/license/DarrenPaulWright/object-agent.svg | ||
[license-url]: https://npmjs.com/package/object-agent/LICENSE.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import forOwn from './forOwn'; | ||
|
||
/** | ||
* Iterates over own properties of an object and returns a reduced value | ||
* | ||
* @example | ||
* ``` javascript | ||
* import { forOwnReduce } from 'object-agent'; | ||
* | ||
* const thing = { | ||
* a: 'b', | ||
* c: 'd' | ||
* }; | ||
* | ||
* const output = forOwnReduce(thing, (result, value, key) => { | ||
* result.push([value, key]); | ||
* return result; | ||
* }, []); | ||
* | ||
* console.log(output); | ||
* // => [['b', 'a'], ['d', 'c']] | ||
* ``` | ||
* | ||
* @function forOwnReduce | ||
* | ||
* @arg {Object} object | ||
* @arg {Function} callback - Provides three args: result, value, and key | ||
* @arg {*} initialValue | ||
* | ||
* @returns {*} The accumulated result | ||
*/ | ||
export default (object, callback, initialValue) => { | ||
forOwn(object, (value, key) => { | ||
initialValue = callback(initialValue, value, key); | ||
}); | ||
|
||
return initialValue; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { assert } from 'chai'; | ||
import { forOwnReduce } from '../src'; | ||
|
||
describe('forOwnReduce', () => { | ||
it('should return the reduced value', () => { | ||
const object = { | ||
key1: 'something1', | ||
key2: 'something2', | ||
key3: 'something3' | ||
}; | ||
|
||
const output = forOwnReduce(object, (result, value, key) => { | ||
result.push([value, key]); | ||
return result; | ||
}, []); | ||
|
||
assert.deepEqual(output, [['something1', 'key1'], ['something2', 'key2'], ['something3', 'key3']]); | ||
}); | ||
}); |