Skip to content

Commit

Permalink
refactor: Add curried versions to multiple functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Oct 12, 2020
1 parent 568fb2e commit ee27342
Show file tree
Hide file tree
Showing 106 changed files with 697 additions and 701 deletions.
25 changes: 5 additions & 20 deletions src/all/all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pipe } from "../pipe/pipe"
import { curry } from "../curry/curry"
import { isMatch } from "../is-match/is-match"

const _all = (_fn, _source) => {
Expand All @@ -14,6 +15,8 @@ const _all = (_fn, _source) => {
return true
}

const _allWith = (subset, source) => _all(isMatch(subset), source)

/**
* Test if all elements of array satisfy a function
*
Expand All @@ -24,7 +27,6 @@ const _all = (_fn, _source) => {
*
* @name all
* @tag Array
* @signature (fn: Function|Function[]) => (source: Array): Boolean
* @signature (fn: Function|Function[], source: Array): Boolean
*
* @see {@link allWith}
Expand All @@ -38,15 +40,7 @@ const _all = (_fn, _source) => {
* all(is, [1, "asd", null])
* // => false
*/
export const all = (...params) => {
// @signature (fn) => (source)
if (params.length <= 1) {
return source => _all(params[0], source)
}

// @signature (fn, source)
return _all(...params)
}
export const all = curry(_all)

/**
* Test if all elements in array match object
Expand All @@ -58,7 +52,6 @@ export const all = (...params) => {
*
* @name allWith
* @tag Array
* @signature (subset: Object) => (source: Array): Boolean
* @signature (subset: Object, source: Array): Boolean
*
* @see {@link all}
Expand All @@ -73,12 +66,4 @@ export const all = (...params) => {
* allWith(is, [1, "asd", null])
* // => false
*/
export const allWith = (...params) => {
// @signature (subset) => (source)
if (params.length <= 1) {
return source => _all(isMatch(params[0]), source)
}

// @signature (subset, source)
return _all(isMatch(params[0]), params[1])
}
export const allWith = curry(_allWith)
37 changes: 24 additions & 13 deletions src/all/all.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
import test from "tape"
import { all, allWith, is, get } from ".."
import { all, allWith } from "./all"

import { is } from "../is/is"
import { read } from "../read/read"

const isEven = source => source % 2 === 0

test("all(With)", t => {
t.equal(all(isEven)([2, 6, 4]), true, "Check all number are even (curried)")

t.equal(all(isEven, [2, 6, 4]), true, "Check all number are even (uncurried)")
t.equal(all(isEven, [2, 6, 4]), true, "Check all number are even")

t.equal(
all(item => is(item.id))([{}, { id: 2 }, {}]),
all(item => is(item.id), [{}, { id: 2 }, {}]),
false,
'Check all elements have "id" property'
)

t.equal(
all([get("id"), is], [{}, { id: 2 }, {}]),
all([read("id"), is], [{}, { id: 2 }, {}]),
false,
'Check all elements have "id" property'
'Check all elements have "id" property using pipe functions'
)

t.end()
})

test("allWith", t => {
t.equal(
allWith({
id: value => is(value),
})([{}, { id: 2 }, {}]),
allWith(
{
id: value => is(value),
},
[{}, { id: 2 }, {}]
),
false,
'Not all elements have "id" property via match subset'
)

t.equal(
allWith({
id: value => is(value),
})([{ id: 1 }, { id: 2 }, { id: 3, name: "test" }]),
allWith(
{
id: value => is(value),
},
[{ id: 1 }, { id: 2 }, { id: 3, name: "test" }]
),
true,
'All elements have "id" property via match subset'
)
Expand Down
25 changes: 5 additions & 20 deletions src/any/any.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pipe } from "../pipe/pipe"
import { curry } from "../curry/curry"
import { isMatch } from "../is-match/is-match"

const _any = (_fn, _source) => {
Expand All @@ -15,6 +16,8 @@ const _any = (_fn, _source) => {
return false
}

const _anyWith = (subset, source) => _any(isMatch(subset), source)

/**
* Test if at least one element in array matches predicate
*
Expand All @@ -26,7 +29,6 @@ const _any = (_fn, _source) => {
* @name any
* @alias has
* @tag Array
* @signature (fn: Function|Function[]) => (source: Array): Boolean
* @signature (fn: Function|Function[], source: Array): Boolean
*
* @see {@link anyWith}
Expand All @@ -40,15 +42,7 @@ const _any = (_fn, _source) => {
* any([get("id"), is], [{title: ""}, {}])
* // => false
*/
export const any = (...params) => {
// @signature (fn) => (source)
if (params.length <= 1) {
return source => _any(params[0], source)
}

// @signature (fn, source)
return _any(...params)
}
export const any = curry(_any)

/**
* Test if at least one element in array matches object
Expand All @@ -61,7 +55,6 @@ export const any = (...params) => {
* @name anyWith
* @alias hasWith
* @tag Array
* @signature (subset: Object) => (source: Array): Boolean
* @signature (subset: Object, source: Array): Boolean
*
* @see {@link any}
Expand All @@ -76,12 +69,4 @@ export const any = (...params) => {
* anyWith({ tags: is })([{id: 1}, {id: 2, comments: []}])
* // => false
*/
export const anyWith = (...params) => {
// @signature (subset) => (source)
if (params.length <= 1) {
return source => _any(isMatch(params[0]), source)
}

// @signature (subset, source)
return _any(isMatch(params[0]), params[1])
}
export const anyWith = curry(_anyWith)
43 changes: 15 additions & 28 deletions src/any/any.test.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,40 @@
import test from "tape"
import { get, any, anyWith } from ".."

import { any, anyWith } from "./any"
import { read } from "../read/read"

const isNumber = source => Number.isFinite(source)

test("any(With)", t => {
test("any", t => {
t.equal(
any(1, [1, "string", NaN]),
true,
"Check any element is equal to primitive"
)

t.equal(
any(isNumber)([1, "string", NaN]),
true,
"Check any element is number (curried)"
)

t.equal(
any(isNumber, [1, "string", NaN]),
true,
"Check any element is number (uncurried)"
"Check any element is number"
)

t.equal(
any(
[get("boolFlag"), item => item === true],
[read("boolFlag"), item => item === true],
[null, "2", { boolFlag: true }]
),
true,
"Check any element has a field (uncurried, multiple functions)"
"Check any element has a field using piped functions"
)

t.equal(any(isNumber)([null, "2", {}]), false, "Check any element is number")
t.equal(any(isNumber, [null, "2", {}]), false, "Check any element is number")

t.equal(any(isNumber)(2), true, "Check non array input")
t.equal(any(isNumber, 2), true, "Check non array input")

t.equal(
anyWith({
id: isNumber,
name: "lorem",
})([
{ id: "uuid", name: "lorem" },
{ id: 2, name: "foo" },
{ id: 3, name: "lorem", foo: "bar" },
]),
true,
"Array should contain object that satisfies conditions"
)
t.end()
})

test("anyWith", t => {
t.equal(
anyWith(
{
Expand All @@ -57,11 +44,11 @@ test("any(With)", t => {
[
{ id: "uuid", name: "lorem" },
{ id: 2, name: "foo" },
{ id: "3", name: "lorem", foo: "bar" },
{ id: 3, name: "lorem", foo: "bar" },
]
),
false,
"Array should not contain object that satisfies conditions"
true,
"Array should contain object that satisfies conditions"
)

t.end()
Expand Down
21 changes: 6 additions & 15 deletions src/append/append.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const _append = (subset, source) => {
if (subset === undefined) {
return source
}
import { curry } from "../curry/curry"

const _append = (subset, source) => {
if (Array.isArray(source)) {
return source.concat(subset)
}
Expand All @@ -18,22 +16,15 @@ const _append = (subset, source) => {
*
* @returns {Array}
*
* @name append
* @tag Array
* @signature (subset: String) => (source: String) => String
* @signature (subset: String, source: String) => String
* @signature (subset: mixed|Array) => (source: Array) => Array
* @signature (subset: mixed|Array, source: Array) => Array
*
* @see {@link prepend}
*
* @example
* append([1])([4, 5])
* // => [1, 4, 5]
*/
export const append = (...params) => {
// @signature (fn) => (source)
if (params.length <= 1) {
return source => _append(params[0], source)
}

// @signature (fn, source)
return _append(...params)
}
export const append = curry(_append)
8 changes: 1 addition & 7 deletions src/append/append.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ test("append", t => {

t.deepEquals(append(" ipsum", "lorem"), "lorem ipsum", "Append 2 strings")

t.deepEquals(append([])([]), [], "Append 2 empty arrays")

t.deepEquals(
append()([1]),
[1],
"Append undefined should not change source array"
)
t.deepEquals(append([], []), [], "Append 2 empty arrays")

t.end()
})
37 changes: 9 additions & 28 deletions src/bottom/bottom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { is } from ".."
import { is } from "../is/is"

const bottomX = (limit, source) => {
const _bottom = (limit, source) => {
if (
(!Array.isArray(source) && typeof source !== "string") ||
source.length <= 1
Expand All @@ -23,7 +23,6 @@ const bottomX = (limit, source) => {
*
* @tag Array
* @signature (limit: integer, source: Array): Array
* @signature (limit: integer) => (source: Array): Array
* @signature (source: Array): Array
*
* @example
Expand All @@ -39,35 +38,17 @@ const bottomX = (limit, source) => {
* bottom(2)([1, 2, 3])
* // => [2, 3]
*/
const bottom = (...params) => {
/*
* @signature (limit: integer) => (source: Array): Array
*
* bottom(2)([1, 2, 3, 4]) => [3, 4]
*/
export const bottom = (...params) => {
// @signature (limit: Integer) => (source: Array): Array
if (params.length === 1 && typeof params[0] === "number") {
const [limit] = params

return source => bottomX(limit, source)
return source => _bottom(params[0], source)
}

/*
* @signature (source: Array): Array
*
* bottom([1, 2, 3, 4]) => [2, 3, 4]
*/
// @signature (source: Array): Array
if (params.length === 1 && typeof params[0] !== "number") {
const [source] = params

return bottomX(null, source)
return _bottom(null, params[0])
}

/*
* @signature (limit: integer, source: Array): Array
*
* bottom(2, [1, 2, 3, 4]) => [3, 4]
*/
return bottomX(params[0], params[1])
// @signature (limit: integer, source: Array): Array
return _bottom(...params)
}

export { bottom }
3 changes: 2 additions & 1 deletion src/bottom/bottom.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from "tape"
import { bottom } from ".."

import { bottom } from "./bottom"

test("bottom", t => {
t.deepEquals(
Expand Down
Loading

0 comments on commit ee27342

Please sign in to comment.