diff --git a/__tests__/functions.test.js b/__tests__/functions.test.js index efa6c3c..7c93d59 100644 --- a/__tests__/functions.test.js +++ b/__tests__/functions.test.js @@ -1,19 +1,14 @@ -import { compose, curry, get, set, setIndex, toUpper } from '../src/functions' +import { curry, get, set, setIndex, toUpper } from '../src/functions' -const cubed = (num, exp) => num ** exp -const exp = curry(cubed) const obj = { foo: 'bar' } const arr = [1, 2, 3] describe('Function Operators', () => { test('curry -> should curry functions', () => { - expect(exp(5)(3)).toBe(125) - }) + const cubed = (num, exp) => num ** exp + const exp = curry(cubed) - test('compose -> should compose N functions correctly', () => { - const inc = x => x + 1 - - expect(compose(inc, exp(5))(1)).toBe(inc(exp(5, 1))) + expect(exp(5)(3)).toBe(125) }) test('toUpper -> should do what is expected ehem', () => { diff --git a/__tests__/operations.test.js b/__tests__/operations.test.js index 2b09ba1..c4a7cf4 100644 --- a/__tests__/operations.test.js +++ b/__tests__/operations.test.js @@ -1,8 +1,8 @@ import { OpticComposeError, UnavailableOpticOperationError } from '../src/errors' -import { toUpper } from '../src/functions' +import { curry, toUpper } from '../src/functions' import { getter } from '../src/Getter' import { alter } from '../src/Lens' -import { collect, optic, over, path, set, view } from '../src/operations' +import { collect, compose, optic, over, path, set, view } from '../src/operations' import { setter } from '../src/Setter' const theme = { @@ -27,6 +27,14 @@ const themeWithoutFontFamily = { } describe('Operations over Optics', () => { + test('compose -> should compose N functions correctly', () => { + const inc = x => x + 1 + const cubed = (num, exp) => num ** exp + const exp = curry(cubed) + + expect(compose(inc, exp(5))(1)).toBe(inc(exp(5, 1))) + }) + test('view should read from a lens', () => { const codeLens = path(['styles', 'CodeSurfer', 'code']) const codeSurferLens = path(['styles', 'CodeSurfer']) diff --git a/src/functions.js b/src/functions.js index fbc593a..46158a3 100644 --- a/src/functions.js +++ b/src/functions.js @@ -12,13 +12,6 @@ export const curry = (f, arity = f.length, ...args) => arity <= args.length ? f(...args) : (...argz) => curry(f, arity, ...args, ...argz) -/** - * Function composition! - * - * @param {...any} fns - Comma-separated list of functions to be composed (right -> left) - */ -export const compose = (...fns) => args => fns.reduceRight((x, f) => f(x), args) - // get : s -> {s: a} -> Maybe a export const get = curry((key, obj) => obj[key]) diff --git a/src/operations.js b/src/operations.js index 39dde09..770d177 100644 --- a/src/operations.js +++ b/src/operations.js @@ -204,6 +204,13 @@ export const collect = template => export const transform = getter +/** + * Function composition! + * + * @param {...any} fns - Comma-separated list of functions to be composed (right -> left) + */ +export const compose = (...fns) => args => fns.reduceRight((x, f) => f(x), args) + // OPERATIONS // ==========