-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from dictor93/feature/flow
Features: flow, invert, mapValues, uniqBy
- Loading branch information
Showing
14 changed files
with
291 additions
and
2 deletions.
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
#1.13.0 | ||
- feat: flow | ||
- feat: invert | ||
- feat: mapValues | ||
- feat: uniqBy | ||
- feat: groupBy | ||
|
||
#1.12.0 | ||
|
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
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,16 @@ | ||
'use strict' | ||
|
||
/** | ||
* Executes a series of functions in a pipeline, passing the result of each function as the argument to the next function. | ||
* | ||
* @param {Array<Function>} fns - An array of functions to be executed in order. | ||
* @returns {any} - The final result of the pipeline. | ||
* @throws {Error} - Throws an error if any element in the array is not a function. | ||
*/ | ||
const flow = (fns = []) => { | ||
if (!fns.length) return (arg) => arg | ||
if (fns.find(fn => typeof fn !== 'function')) throw new Error('All elements should be functions') | ||
return (...arg) => fns.reduce((acc, fn, i) => i ? fn(acc) : fn(...arg), arg) | ||
} | ||
|
||
module.exports = flow |
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,17 @@ | ||
'use strict' | ||
|
||
/** | ||
* Inverts the key-value pairs of an object. | ||
* | ||
* @param {object} object - The object to invert. | ||
* @returns {object} - The new object with inverted key-value pairs. | ||
*/ | ||
const invert = (object) => { | ||
const newObject = {} | ||
for (const key in object) { | ||
newObject[object[key]] = key | ||
} | ||
return newObject | ||
} | ||
|
||
module.exports = invert |
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,21 @@ | ||
'use strict' | ||
|
||
const get = require('./get') | ||
|
||
/** | ||
* Maps the values of an object or array using the provided iteratee function or property path. | ||
* | ||
* @param {object|Array} src - The source object or array to map. | ||
* @param {Function|string} iteratee - The iteratee function or property path to map the values. | ||
* @returns {object} - The new object with mapped values. | ||
*/ | ||
const mapValues = (src, iteratee) => { | ||
const result = {} | ||
const _iteratee = typeof iteratee === 'function' ? iteratee : (value) => get(value, iteratee) | ||
for (const key in src) { | ||
result[key] = _iteratee(src[key]) | ||
} | ||
return result | ||
} | ||
|
||
module.exports = mapValues |
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,23 @@ | ||
'use strict' | ||
|
||
const get = require('./get') | ||
|
||
/** | ||
* Get unique values of array by the iteratee function or property path. | ||
* | ||
* @param {Array} arr - The src array. | ||
* @param {Function|string} iteratee - The iteratee function or property path to check the unique values. | ||
* @returns {Array} - The result array unique by iteratee. | ||
*/ | ||
const uniqBy = (arr, iteratee) => { | ||
const set = new Set() | ||
const _iteratee = typeof iteratee === 'function' ? iteratee : (value) => get(value, iteratee) | ||
return arr.filter(el => { | ||
const val = iteratee ? _iteratee(el) : el | ||
if (set.has(val)) return false | ||
set.add(val) | ||
return true | ||
}) | ||
} | ||
|
||
module.exports = uniqBy |
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,46 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const flow = require('../src/flow') | ||
|
||
describe('flow', () => { | ||
it('should return the input argument if no functions are provided', () => { | ||
const result = flow()('input') | ||
assert.strictEqual(result, 'input') | ||
}) | ||
|
||
it('should apply the provided functions in sequence', () => { | ||
const addOne = (num) => num + 1 | ||
const double = (num) => num * 2 | ||
const subtractFive = (num) => num - 5 | ||
|
||
const result = flow([addOne, double, subtractFive])(10) | ||
assert.strictEqual(result, 17) | ||
}) | ||
|
||
it('should throw an error if any element in the array is not a function', () => { | ||
const fn1 = () => { } | ||
const fn2 = 123 | ||
const fn3 = () => { } | ||
|
||
assert.throws(() => flow([fn1, fn2, fn3]), Error) | ||
}) | ||
|
||
it('should work with functions that take multiple arguments', () => { | ||
const add = (a, b, c) => a + b + c | ||
const square = (a) => a * a | ||
|
||
const result = flow([add, square])(2, 3, 4) | ||
assert.strictEqual(result, 81) | ||
}) | ||
|
||
it('should work with complex arguments', () => { | ||
const add = (a, b) => [{ a, b }, a + b] | ||
const multiply = ([args, res]) => [args, args.a * args.b * res] | ||
const subtract = ([args, res]) => res - args.a | ||
|
||
assert.strictEqual(flow([add, multiply, subtract])(2, 3), 28) | ||
}) | ||
}) |
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,32 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const getErrorMessage = require('../src/getErrorMessage') | ||
|
||
describe('getErrorMessage', () => { | ||
it('should return the error message when it exists', () => { | ||
const error = new Error('Something went wrong') | ||
const errorMessage = getErrorMessage(error) | ||
assert.strictEqual(errorMessage, 'Something went wrong') | ||
}) | ||
|
||
it('should return an empty string when the error message is not provided', () => { | ||
const error = new Error() | ||
const errorMessage = getErrorMessage(error) | ||
assert.strictEqual(errorMessage, '') | ||
}) | ||
|
||
it('should return the base error code when it exists', () => { | ||
const error = new Error('foo bar ERR_INVALID_INPUT') | ||
const errorMessage = getErrorMessage(error) | ||
assert.strictEqual(errorMessage, 'ERR_INVALID_INPUT') | ||
}) | ||
|
||
it('should return the full error message when it does not match the base error code pattern', () => { | ||
const error = new Error('Some other error') | ||
const errorMessage = getErrorMessage(error) | ||
assert.strictEqual(errorMessage, 'Some other error') | ||
}) | ||
}) |
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,26 @@ | ||
'use strict' | ||
|
||
/* eslint-env jest */ | ||
|
||
const assert = require('assert') | ||
const invert = require('../src/invert') | ||
|
||
describe('invert', () => { | ||
it('should invert an object', () => { | ||
const object = { a: 1, b: 2 } | ||
const actual = invert(object) | ||
|
||
assert.deepStrictEqual(actual, { 1: 'a', 2: 'b' }) | ||
assert.deepStrictEqual(invert(actual), { a: '1', b: '2' }) | ||
}) | ||
|
||
it('should work with values that shadow keys on `Object.prototype`', () => { | ||
const object = { a: 'hasOwnProperty', b: 'constructor' } | ||
assert.deepStrictEqual(invert(object), { hasOwnProperty: 'a', constructor: 'b' }) | ||
}) | ||
|
||
it('should work with an object that has a `length` property', () => { | ||
const object = { 0: 'a', 1: 'b', length: 2 } | ||
assert.deepStrictEqual(invert(object), { a: '0', b: '1', 2: 'length' }) | ||
}) | ||
}) |
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,26 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const mapValues = require('../src/mapValues') | ||
|
||
describe('mapValues', () => { | ||
const array = [1, 2] | ||
const object = { a: 1, b: 2 } | ||
|
||
it('should map values in `object` to a new object', () => { | ||
const actual = mapValues(object, String) | ||
assert.deepStrictEqual(actual, { a: '1', b: '2' }) | ||
}) | ||
|
||
it('should treat arrays like objects', () => { | ||
const actual = mapValues(array, String) | ||
assert.deepStrictEqual(actual, { 0: '1', 1: '2' }) | ||
}) | ||
|
||
it('should work with `_.property` shorthands', () => { | ||
const actual = mapValues({ a: { b: 2 } }, 'b') | ||
assert.deepStrictEqual(actual, { a: 2 }) | ||
}) | ||
}) |
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
Oops, something went wrong.