Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapValues method, docs, tests #141

Merged
merged 3 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ A ES module bundle is included under `dist/absurdum.esm.js` and is defined as th
- [invert][objects.invert]
- [keys][objects.keys]
- [mapKeys][objects.mapKeys]
- [mapValues][objects.mapValues]
- [merge][objects.merge]
- [pick][objects.pick]
- [values][objects.values]
Expand All @@ -155,6 +156,7 @@ A ES module bundle is included under `dist/absurdum.esm.js` and is defined as th
[objects.invert]: ./docs/objects/invert.md
[objects.keys]: ./docs/objects/keys.md
[objects.mapKeys]: ./docs/objects/mapKeys.md
[objects.mapValues]: ./docs/objects/mapValues.md
[objects.merge]: ./docs/objects/merge.md
[objects.pick]: ./docs/objects/pick.md
[objects.values]: ./docs/objects/values.md
Expand Down
1 change: 1 addition & 0 deletions src/objects/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { include } from "./include.js";
export { invert } from "./invert.js";
export { keys } from "./keys.js";
export { mapKeys } from "./mapKeys.js";
export { mapValues } from "./mapValues.js";
export { merge } from "./merge.js";
export { pick } from "./pick.js";
export { values } from "./values.js";
1 change: 1 addition & 0 deletions src/objects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { include } from './include.js';
export { invert } from './invert.js';
export { keys } from './keys.js';
export { mapKeys } from './mapKeys.js';
export { mapValues } from './mapValues.js';
export { merge } from './merge.js';
export { pick } from './pick.js';
export { values } from './values.js';
21 changes: 21 additions & 0 deletions src/objects/mapValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* MapKeys iterates over an object and applies a function to each value
*
* @param {Object} object input object
* @param {Function} func map function
* @returns {Object} object with mutated values
*
* @example
* const result = objects.mapValues({ a: 1, b: 2, c: 3 }, value => `neat_${value}`);
* console.log(result);
* > { a: neat_1, b: neat_2, c: neat_3 }
*/
function mapValues (object, func) {
if (typeof func !== 'function') { return object; }
return Object.entries(object).reduce((acc, [key, value]) => ({
...acc,
[key]: func(value, key, object)
}), {});
}

export { mapValues };
44 changes: 44 additions & 0 deletions src/objects/mapValues.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import test from 'tape';
import { objects } from '../../index.js';

test('objects.mapValues(object, func) - returns an object with updated values', t => {
const expect = { a: 'neat_1', b: 'neat_2', c: 'neat_3' };
const result = objects.mapValues({ a: 1, b: 2, c: 3 }, value => `neat_${value}`);

t.equal(Object.prototype.toString.call(result), '[object Object]', 'return type');
t.deepEqual(result, expect, 'output value');

t.end();
});

test('objects.mapValues(object, func) - returns an object with updated keys using keys, values, and the object as arguments', t => {
const expect = { a: 'a_1_1', b: 'b_2_1', c: 'c_3_1' };
const result = objects.mapValues({ a: 1, b: 2, c: 3 }, function (value, key, object) {
return `${key}_${value}_${object.a}`;
});

t.equal(Object.prototype.toString.call(result), '[object Object]', 'return type');
t.deepEqual(result, expect, 'output value');

t.end();
});

test('objects.mapValues(object) - returns the input object if no function is applied', t => {
const expect = { a: 1, b: 2, c: 3 };
const result = objects.mapValues({ a: 1, b: 2, c: 3 });

t.equal(Object.prototype.toString.call(result), '[object Object]', 'return type');
t.deepEqual(result, expect, 'output value');

t.end();
});

test('objects.mapValues(object, func) - should not mutate the input', t => {
const input = { a: 1, b: 2, c: 3 };
const expect = { a: 1, b: 2, c: 3 };
objects.get(input, value => value + 3);

t.deepEqual(input, expect, 'input mutation');

t.end();
});