Skip to content

Commit

Permalink
Adding function forOwnReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrenPaulWright committed Jul 28, 2019
1 parent 7e51b07 commit d67f9bc
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
58 changes: 58 additions & 0 deletions docs/forOwnReduce.md
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&#x3D;master
[build-url]: https://travis-ci.org/DarrenPaulWright/object-agent
[coverage]: https://coveralls.io/repos/github/DarrenPaulWright/object-agent/badge.svg?branch&#x3D;master
[coverage-url]: https://coveralls.io/github/DarrenPaulWright/object-agent?branch&#x3D;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&#x3D;object-agent
[size-url]: https://packagephobia.now.sh/result?p&#x3D;object-agent
[vulnerabilities]: https://snyk.io/test/github/DarrenPaulWright/object-agent/badge.svg?targetFile&#x3D;package.json
[vulnerabilities-url]: https://snyk.io/test/github/DarrenPaulWright/object-agent?targetFile&#x3D;package.json
[license]: https://img.shields.io/github/license/DarrenPaulWright/object-agent.svg
[license-url]: https://npmjs.com/package/object-agent/LICENSE.md
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"docs-unset": "jsdoc2md src/unset.js > docs/unset.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-forIn": "jsdoc2md src/forIn.js > docs/forIn.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-forOwn": "jsdoc2md src/forOwn.js > docs/forOwn.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-forOwnReduce": "jsdoc2md src/forOwnReduce.js > docs/forOwnReduce.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-mapOwn": "jsdoc2md src/mapOwn.js > docs/mapOwn.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-traverse": "jsdoc2md src/traverse.js > docs/traverse.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-isEmpty": "jsdoc2md src/isEmpty.js > docs/isEmpty.md -t docs/docs.hbs --plugin dmd-readable -d 3",
Expand All @@ -33,7 +34,7 @@
"docs-repeat": "jsdoc2md src/repeat.js > docs/repeat.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-fill": "jsdoc2md src/fill.js > docs/fill.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs-pathUtilities": "jsdoc2md src/utility/*.js > docs/pathUtilities.md -t docs/docs.hbs --plugin dmd-readable -d 3",
"docs": "npm run docs-readme && npm run docs-get && npm run docs-has && npm run docs-set && npm run docs-unset && npm run docs-forIn && npm run docs-forOwn && npm run docs-mapOwn && npm run docs-traverse && npm run docs-isEmpty && npm run docs-isEqual && npm run docs-deepEqual && npm run docs-diffUpdate && npm run docs-pull && npm run docs-intersection && npm run docs-clone && npm run docs-combo && npm run docs-mix && npm run docs-powerset && npm run docs-nestedEach && npm run docs-repeat && npm run docs-fill && npm run docs-pathUtilities"
"docs": "npm run docs-readme && npm run docs-get && npm run docs-has && npm run docs-set && npm run docs-unset && npm run docs-forIn && npm run docs-forOwn && npm run docs-forOwnReduce && npm run docs-mapOwn && npm run docs-traverse && npm run docs-isEmpty && npm run docs-isEqual && npm run docs-deepEqual && npm run docs-diffUpdate && npm run docs-pull && npm run docs-intersection && npm run docs-clone && npm run docs-combo && npm run docs-mix && npm run docs-powerset && npm run docs-nestedEach && npm run docs-repeat && npm run docs-fill && npm run docs-pathUtilities"
},
"badges": [
{
Expand Down
38 changes: 38 additions & 0 deletions src/forOwnReduce.js
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;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* - Analysis
* - [forIn](docs/forIn.md)
* - [forOwn](docs/forOwn.md)
* - [forOwnReduce](docs/forOwnReduce.md)
* - [mapOwn](docs/mapOwn.md)
* - [traverse](docs/traverse.md)
* - Comparison
Expand Down Expand Up @@ -47,6 +48,7 @@ export { default as set } from './set';
export { default as unset } from './unset';
export { default as forIn } from './forIn';
export { default as forOwn } from './forOwn';
export { default as forOwnReduce } from './forOwnReduce';
export { default as mapOwn } from './mapOwn';
export { default as traverse } from './traverse';
export { default as isEmpty } from './isEmpty';
Expand Down
19 changes: 19 additions & 0 deletions tests/forOwnReduce.Test.js
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']]);
});
});

0 comments on commit d67f9bc

Please sign in to comment.