rambda
Lightweight and faster alternative to Ramda with included TS definitions
Last updated 3 months ago by self_refactor .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install rambda 
SYNC missed versions from official npm registry.

Rambda

Rambda is TypeScript-focused utility library similar to Remeda, Ramda and Radashi. - Documentation site

Commit activity Library size install size PR's Welcome GitHub contributors

❯ Example use

import { pipe, filter, map } from 'rambda'

const result = pipe(
  [1, 2, 3, 4],
  filter(x => x > 2),
  map(x => x * 2),
)
//=> [6, 8]

You can test this example in Rambda's REPL

---------------

❯ Rambda's features

❯ Goals

Typescript focus

Mixing Functional Programming and TypeScript is not easy.

One way to solve this is to focus what can be actually achieved and refrain from what is not possible.

R.pipe as the main way to use Rambda

  • All methods are meant to be used as part of R.pipe chain

  • This is the main purpose of functional programming, i.e. to pass data through a chain of functions.

  • Having R.pipe(input, ...fns) helps TypeScript to infer the types of the input and the output.

Here is one example why R.pipe is better than Ramda.pipe:

const list = [1, 2, 3];

it('within pipe', () => {
	const result = pipe(
		list,
		filter((x) => {
			x; // $ExpectType number
			return x > 1;
		}),
	);
	result; // $ExpectType number[]
});
it('within Ramda.pipe requires explicit types', () => {
	Ramda.pipe(
		(x) => x,
		filter<number>((x) => {
			x; // $ExpectType number
			return x > 1;
		}),
		filter((x: number) => {
			x; // $ExpectType number
			return x > 1;
		}),
	)(list);
});

:exclamation: IMPORTANT - all methods are tested to deliver correct types when they are part of R.pipe/R.pipeAsync chains.

In other words:

R.filter(x => x > 1)([1,2,3])

might trigger TS error as it not the same as


R.pipe([1,2,3], R.filter(x => x > 1)

:exclamation: All methods are curried

There is one way to use Rambda methods and it is with currying, i.e. using R.filter(fn, list) will not work as it is inteded to be R.filter(fn)(list).

The reason is that all methods are supposed to be used inside R.pipe. After all, building chains is the very base of functional programming.

Of course, there is value in supporting the case where you can pass all inputs at once, but I find that the price in terms of maintainability is not worth it.

Keep only the most useful methods

The idea is to give TypeScript users only the most useful methods and let them implement the rest. No magic logic methods that are hard to remember. You shouldn't need to read the documentation to understand what a method does. Its name and signature should be enough.

  • Methods that are simply to remember only by its name. Complex logic shouldn't be part of utility library, but part of your codebase.

  • Keep only methods which are both useful and which behaviour is obvious from its name. For example, R.innerJoin is kept, but R.identical, R.move is removed. Methods such as R.toLower, R.length provide little value. Such method are omitted from Rambda on purpose.

  • Some generic methods such as curry and assoc is not easy to be expressed in TypeScript. For this reason Rambda omits such methods.

  • No R.cond or R.ifElse as they make the chain less readable.

  • No R.length as it adds very little value.

  • No R.difference as user must remember the order of the inputs, i.e. which is compared to and which is compared against.

One way to use each method

Because of the focus on R.pipe, there is only one way to use each method. This helps with testing and also with TypeScript definitions.

  • All methods that 2 inputs, will have to be called with R.methodName(input1)(input2)
  • All methods that 3 inputs, will have to be called with R.methodName(input1, input2)(input3)

Deno support

import * as R from "https://deno.land/x/rambda/mod.ts";

R.filter(x => x > 1)([1, 2, 3])

Dot notation for R.path

Standard usage of R.path is R.path(['a', 'b'])({a: {b: 1} }).

In Rambda you have the choice to use dot notation(which is arguably more readable):

R.path('a.b')({a: {b: 1} })

Please note that since path input is turned into array, i.e. if you want R.path(['a','1', 'b'])({a: {'1': {b: 2}}}) to return 2, you will have to pass array path, not string path. If you pass a.1.b, it will turn path input to ['a', 1, 'b'].

Comma notation for R.pick and R.omit

Similar to dot notation, but the separator is comma(,) instead of dot(.).

R.pick('a,b', {a: 1 , b: 2, c: 3} })
// No space allowed between properties

Differences between Rambda and Ramda

Up until version 9.4.2, the aim of Rambda was to match as much as possible the Ramda API.

You can find documentation site of Rambda version 9.4.2 is here.

From version 10.0.0 onwards, Rambda is no longer aiming to be drop-in replacement for Ramda.

---------------

API

addProp


addProp<T extends object, P extends PropertyKey, V extends unknown>(
	prop: P,
	value: V
): (obj: T) => MergeTypes<T & Record<P, V>>

It adds new key-value pair to the object.

const result = R.pipe(
	{ a: 1, b: 'foo' },
	R.addProp('c', 3)
)
// => { a: 1, b: 'foo', c: 3 }

Try this R.addProp example in Rambda REPL

<summary>All TypeScript definitions</summary>
addProp<T extends object, P extends PropertyKey, V extends unknown>(
	prop: P,
	value: V
): (obj: T) => MergeTypes<T & Record<P, V>>;
<summary>R.addProp source</summary>
export function addProp(key, value) {
  return obj => ({ ...obj, [key]: value })
}
<summary>Tests</summary>
import { addProp } from './addProp'
import { pipe } from './pipe'

test('happy', () => {
  const result = pipe({ a: 1, b: 'foo' }, addProp('c', 3))
  expectTypeOf(result.a).toEqualTypeOf<number>()
  expectTypeOf(result.b).toEqualTypeOf<string>()
  expectTypeOf(result.c).toEqualTypeOf<number>()
  expect(result).toEqual({ a: 1, b: 'foo', c: 3 })
})

---------------

addPropToObjects


addPropToObjects<
  T extends object,
  K extends string,
  R
>(
	property: K,
  fn: (input: T) => R
): (list: T[]) => MergeTypes<T & { [P in K]: R }>[]

It receives list of objects and add new property to each item.

The value is based on result of fn function, which receives the current object as argument.

const result = R.pipe(
	[
		{a: 1, b: 2},
		{a: 3, b: 4},
	],
	R.addPropToObjects(
		'c',
		(x) => String(x.a + x.b),
	)
)
// => [{a: 1, b: 2, c: '3'}, {a: 3, b: 4, c: '7'}]

Try this R.addPropToObjects example in Rambda REPL

<summary>All TypeScript definitions</summary>
addPropToObjects<
  T extends object,
  K extends string,
  R
>(
	property: K,
  fn: (input: T) => R
): (list: T[]) => MergeTypes<T & { [P in K]: R }>[];
<summary>R.addPropToObjects source</summary>
import { mapFn } from './map.js'

export function addPropToObjects (
	property, 
	fn
){
	return listOfObjects => mapFn(
		(obj) => ({
			...(obj),
			[property]: fn(obj)
		}), 
		listOfObjects
	)
}
<summary>Tests</summary>
import { pipe } from './pipe'
import { addPropToObjects } from './addPropToObjects'

test('R.addPropToObjects', () => {
  let result = pipe(
    [
      {a: 1, b: 2},
      {a: 3, b: 4},
    ],
    addPropToObjects(
      'c',
      (x: { a: number; b: number }) => String(x.a + x.b),
    )
  )
  expectTypeOf(result).toEqualTypeOf<{ a: number; b: number; c: string }[]>()
  expect(result).toEqual([
    { a: 1, b: 2, c: '3' },
    { a: 3, b: 4, c: '7' },
  ])
})

---------------

all


all<T>(predicate: (x: T) => boolean): (list: T[]) => boolean

It returns true, if all members of array list returns true, when applied as argument to predicate function.

const list = [ 0, 1, 2, 3, 4 ]
const predicate = x => x > -1

const result = R.pipe(
	list,
	R.all(predicate)
) // => true

Try this R.all example in Rambda REPL

<summary>All TypeScript definitions</summary>
all<T>(predicate: (x: T) => boolean): (list: T[]) => boolean;
<summary>R.all source</summary>
export function all(predicate) {
  return list => {
    for (let i = 0; i < list.length; i++) {
      if (!predicate(list[i])) {
        return false
      }
    }

    return true
  }
}
<summary>Tests</summary>
import { all } from './all'
import { pipe } from './pipe'

const list = [0, 1, 2, 3, 4]

test('happy', () => {
  const result = pipe(
    [1, 2, 3],
    all((x: number) => {
      expectTypeOf(x).toEqualTypeOf<number>()
      return x > 0
    }),
  )
  expectTypeOf(result).toEqualTypeOf<boolean>()
  expect(result).toBe(true)
})

test('when true', () => {
  const fn = (x: number) => x > -1

  expect(all(fn)(list)).toBeTruthy()
})

test('when false', () => {
  const fn = (x: number) => x > 2

  expect(all(fn)(list)).toBeFalsy()
})

---------------

allPass


allPass<F extends (...args: any[]) => boolean>(predicates: readonly F[]): F

It returns true, if all functions of predicates return true, when input is their argument.

const list = [[1, 2, 3, 4], [3, 4, 5]]
const result = R.pipe(
	list,
	R.filter(R.allPass([R.includes(2), R.includes(3)]))
) // => [[1, 2, 3, 4]]

Try this R.allPass example in Rambda REPL

<summary>All TypeScript definitions</summary>
allPass<F extends (...args: any[]) => boolean>(predicates: readonly F[]): F;
<summary>R.allPass source</summary>
export function allPass(predicates) {
  return input => {
    let counter = 0
    while (counter < predicates.length) {
      if (!predicates[counter](input)) {
        return false
      }
      counter++
    }

    return true
  }
}
<summary>Tests</summary>
import { allPass } from './allPass'
import { filter } from './filter'
import { pipe } from './pipe'

const list = [
  [1, 2, 3, 4],
  [3, 4, 5],
]

test('happy', () => {
  const result = pipe(list, filter(allPass([(x: number[]) => x.includes(2), (x: number[]) => x.includes(3)])))
  expectTypeOf(result).toEqualTypeOf<number[][]>()
  expect(result).toEqual([[1, 2, 3, 4]])
})

test('when returns false', () => {
  const result = pipe(list, filter(allPass([(x: number[]) => x.includes(12), (x: number[]) => x.includes(31)])))
  expect(result).toEqual([])
})

---------------

any


any<T>(predicate: (x: T) => boolean): (list: T[]) => boolean

It returns true, if at least one member of list returns true, when passed to a predicate function.

const list = [1, 2, 3]
const predicate = x => x * x > 8
R.any(predicate)(list)
// => true

Try this R.any example in Rambda REPL

<summary>All TypeScript definitions</summary>
any<T>(predicate: (x: T) => boolean): (list: T[]) => boolean;
<summary>R.any source</summary>
export function any(predicate) {
  return list => {
    let counter = 0
    while (counter < list.length) {
      if (predicate(list[counter], counter)) {
        return true
      }
      counter++
    }

    return false
  }
}
<summary>Tests</summary>
import { any } from './any'
import { pipe } from './pipe'

test('happy', () => {
  const result = pipe(
    [1, 2, 3],
    any((x: number) => {
      expectTypeOf(x).toEqualTypeOf<number>()
      return x > 2
    }),
  )
  expectTypeOf(result).toEqualTypeOf<boolean>()
  expect(result).toBe(true)
})

---------------

anyPass


anyPass<T, TF1 extends T, TF2 extends T>(
  predicates: [(a: T) => a is TF1, (a: T) => a is TF2],
): (a: T) => a is TF1 | TF2

It accepts list of predicates and returns a function. This function with its input will return true, if any of predicates returns true for this input.

const isBig = x => x > 20
const isOdd = x => x % 2 === 1
const input = 11

const fn = R.anyPass(
  [isBig, isOdd]
)

const result = fn(input)
// => true

Try this R.anyPass example in Rambda REPL

<summary>All TypeScript definitions</summary>
anyPass<T, TF1 extends T, TF2 extends T>(
  predicates: [(a: T) => a is TF1, (a: T) => a is TF2],
): (a: T) => a is TF1 | TF2;
anyPass<T, TF1 extends T, TF2 extends T, TF3 extends T>(
  predicates: [(a: T) => a is TF1, (a: T) => a is TF2, (a: T) => a is TF3],
): (a: T) => a is TF1 | TF2 | TF3;
anyPass<T, TF1 extends T, TF2 extends T, TF3 extends T>(
  predicates: [(a: T) => a is TF1, (a: T) => a is TF2, (a: T) => a is TF3],
): (a: T) => a is TF1 | TF2 | TF3;
anyPass<T, TF1 extends T, TF2 extends T, TF3 extends T, TF4 extends T>(
  predicates: [(a: T) => a is TF1, (a: T) => a is TF2, (a: T) => a is TF3, (a: T) => a is TF4],
): (a: T) => a is TF1 | TF2 | TF3 | TF4;
...
...
<summary>R.anyPass source</summary>
export function anyPass(predicates) {
  return input => {
    let counter = 0
    while (counter < predicates.length) {
      if (predicates[counter](input)) {
        return true
      }
      counter++
    }
    return false
  }
}
<summary>Tests</summary>
import { anyPass } from "./anyPass"
import { filter } from "./filter"
import { pipe } from "./pipe"

test("happy", () => {
  const rules = [(x: any) => typeof x === "string", (x: any) => x > 10]
  const predicate = anyPass(rules)
  expect(predicate("foo")).toBeTruthy()
  expect(predicate(6)).toBeFalsy()
})

test("happy 2", () => {
  const rules = [(x: any) => typeof x === "string", (x: any) => x > 10]
  expect(anyPass(rules)(11)).toBeTruthy()
  expect(anyPass(rules)(undefined)).toBeFalsy()
})

const obj = {
  a: 1,
  b: 2,
}

test("when returns true", () => {
  const conditionArr = [(val: any) => val.a === 1, (val: any) => val.a === 2]
  expect(anyPass(conditionArr)(obj)).toBeTruthy()
})

test("when returns false", () => {
  const conditionArr = [(val: any) => val.a === 2, (val: any) => val.b === 3]
  expect(anyPass(conditionArr)(obj)).toBeFalsy()
})

test("with empty predicates list", () => {
	const result = pipe(3, anyPass([]))
	expectTypeOf(result).toEqualTypeOf<unknown>()
  expect(result).toBeFalsy()
})

test("issue #604", () => {
  const plusEq = (w: number, x: number, y: number, z: number) => w + x === y + z
  const result = anyPass([plusEq])(3, 3, 3, 3)
  expectTypeOf(result).toEqualTypeOf<boolean>()
  expect(result).toBe(false)
})

test("issue #642", () => {
  const isGreater = (num: number) => num > 5
  const pred = anyPass([isGreater])
  const xs = [0, 1, 2, 3]
  const filtered1 = filter(pred)(xs)
  expectTypeOf(filtered1).toEqualTypeOf<number[]>()
  const filtered2 = xs.filter(pred)
  expectTypeOf(filtered2).toEqualTypeOf<number[]>()
})

test("functions as a type guard", () => {
  const isString = (x: unknown): x is string => typeof x === "string"
  const isNumber = (x: unknown): x is number => typeof x === "number"
  const isBoolean = (x: unknown): x is boolean => typeof x === "boolean"
  const isStringNumberOrBoolean = anyPass([isString, isNumber, isBoolean])
  const aValue: unknown = 1
  if (isStringNumberOrBoolean(aValue)) {
    expectTypeOf(aValue).toEqualTypeOf<string | number | boolean>()
  }
})

---------------

append


append<T>(el: T): (list: readonly T[]) => T[]

It adds element x at the end of iterable.

const result = R.append('foo')(['bar', 'baz'])
// => ['bar', 'baz', 'foo']

Try this R.append example in Rambda REPL

<summary>All TypeScript definitions</summary>
append<T>(el: T): (list: readonly T[]) => T[];
append<T>(el: T): (list: T[]) => T[];
<summary>R.append source</summary>
import { cloneList } from './_internals/cloneList.js'

export function append(x) {
  return list => {
    const clone = cloneList(list)
    clone.push(x)

    return clone
  }
}
<summary>Tests</summary>
import { append } from './append'
import { prepend } from './prepend'
import { pipe } from './pipe'

const listOfNumbers = [1, 2, 3]

test('happy', () => {
  const result = pipe(listOfNumbers, append(4), prepend(0))
  expectTypeOf(result).toEqualTypeOf<number[]>()
  expect(result).toEqual([0, 1, 2, 3, 4])
})

test('append to empty array', () => {
  expect(append('tests')([])).toEqual(['tests'])
})

test('with object', () => {
  const result = pipe([{ a: 1 }], append({ a: 10 }), prepend({ a: 20 }))
  expectTypeOf(result).toEqualTypeOf<{ a: number }[]>()
  expect(result).toEqual([{ a: 20 }, { a: 1 }, { a: 10 }])
})

---------------

ascend


ascend<T>(fn: (obj: T) => Ord): (a: T, b: T)=> Ordering

Helper function to be used with R.sort to sort list in ascending order.

const result = R.pipe(
	[{a: 1}, {a: 2}, {a: 0}],
	R.sort(R.ascend(R.prop('a')))
)
// => [{a: 0}, {a: 1}, {a: 2}]

Try this R.ascend example in Rambda REPL

<summary>All TypeScript definitions</summary>
ascend<T>(fn: (obj: T) => Ord): (a: T, b: T)=> Ordering;
<summary>R.ascend source</summary>
export function createCompareFunction(a, b, winner, loser) {
  if (a === b) {
    return 0
  }

  return a < b ? winner : loser
}

export function ascend(getFunction) {
	return (a, b) => {
  const aValue = getFunction(a)
  const bValue = getFunction(b)

  return createCompareFunction(aValue, bValue, -1, 1)
}
}
<summary>Tests</summary>
import { ascend } from './ascend'
import { descend } from './descend'
import { sort } from './sort'

test('ascend', () => {
  const result = sort(
    ascend((x: { a: number }) => x.a))(
    [{a:1}, {a:3}, {a:2}],
  )
  expectTypeOf(result).toEqualTypeOf<{ a: number }[]>()
  expect(result).toEqual([{a:1}, {a:2}, {a:3}])
})

test('descend', () => {
  const result = sort(
    descend((x: { a: number }) => x.a))(
    [{a:1}, {a:3}, {a:2}],
  )
  expect(result).toEqual([{a:3}, {a:2}, {a:1}])
})

---------------

assertType


assertType<T, U extends T>(fn: (x: T) => x is U) : (x: T) => U

It helps to make sure that input is from specific type. Similar to R.convertToType, but it actually checks the type of the input value. If fn input returns falsy value, then the function will throw an error.

<summary>All TypeScript definitions</summary>
assertType<T, U extends T>(fn: (x: T) => x is U) : (x: T) => U;
assertType<T>(fn: (x: T) => boolean): (x: T) => T;
<summary>R.assertType source</summary>
export function assertType(fn) {
  return (x) => {
    if (fn(x)) {
      return x
    }
    throw new Error('type assertion failed in R.assertType')
  }
}
<summary>Tests</summary>
import { assertType } from './assertType'
import { pipe } from './pipe'

type Book = {
  title: string
  year: number
}

type BookToRead = Book & {
  bookmarkFlag: boolean
}

function isBookToRead(book: Book): book is BookToRead {
  return (book as BookToRead).bookmarkFlag !== undefined
}

test('happy', () => {
  const result = pipe(
    [1, 2, 3],
    assertType((x: number[]) => x.length === 3),
  )
  expect(result).toEqual([1, 2, 3])
})

test('throw', () => {
  expect(() => {
    pipe(
      [1, 2, 3],
      assertType((x: number[]) => x.length === 4),
    )
  }).toThrow('type assertion failed in R.assertType')
})

test('R.assertType', () => {
  const result = pipe(
    { title: 'Book1', year: 2020, bookmarkFlag: true },
    assertType(isBookToRead),
  )
  expectTypeOf(result).toEqualTypeOf<BookToRead>()
  expect(result).toEqual({ title: 'Book1', year: 2020, bookmarkFlag: true })
})

---------------

checkObjectWithSpec


checkObjectWithSpec<T>(spec: T): <U>(testObj: U) => boolean

It returns true if all each property in conditions returns true when applied to corresponding property in input object.

const condition = R.checkObjectWithSpec({
  a : x => typeof x === "string",
  b : x => x === 4
})
const input = {
  a : "foo",
  b : 4,
  c : 11,
}

const result = condition(input)
// => true

Try this R.checkObjectWithSpec example in Rambda REPL

<summary>All TypeScript definitions</summary>
checkObjectWithSpec<T>(spec: T): <U>(testObj: U) => boolean;
<summary>R.checkObjectWithSpec source</summary>
export function checkObjectWithSpec(conditions) {
  return input => {
    let shouldProceed = true
    for (const prop in conditions) {
      if (!shouldProceed) {
        continue
      }
      const result = conditions[prop](input[prop])
      if (shouldProceed && result === false) {
        shouldProceed = false
      }
    }

    return shouldProceed
  }
}
<summary>Tests</summary>
import { checkObjectWithSpec } from './checkObjectWithSpec'
import { equals } from './equals'

test('happy', () => {
  const input = {
    a: 'foo',
    b: 'bar',
    x: 11,
    y: 19,
  }
  const conditions = {
    a: equals('foo'),
    b: equals('bar'),
  }
  const result = checkObjectWithSpec(conditions)(input)
  expectTypeOf(result).toEqualTypeOf<boolean>()
  expect(result).toBe(true)
})

test('when true', () => {
  const result = checkObjectWithSpec({
    a: equals('foo'),
    b: equals('bar'),
  })({
    a: 'foo',
    b: 'bar',
    x: 11,
    y: 19,
  })
  expect(result).toBeTruthy()
})

test('when false | early exit', () => {
  let counter = 0
  const equalsFn = (expected: string) => (input: string) => {
    counter++
    return input === expected
  }
  const predicate = checkObjectWithSpec({
    a: equalsFn('foo'),
    b: equalsFn('baz'),
  })
  expect(
    predicate({
      a: 'notfoo',
      b: 'notbar',
    }),
  ).toBeFalsy()
  expect(counter).toBe(1)
})

---------------

compact


compact<T>(list: T[]): Array<StrictNonNullable<T>>

It removes null and undefined members from list or object input.

const result = R.pipe(
	{
		a: [ undefined, '', 'a', 'b', 'c'],
		b: [1,2, null, 0, undefined, 3],
		c: { a: 1, b: 2, c: 0, d: undefined, e: null, f: false },
	},
	x => ({
		a: R.compact(x.a),
		b: R.compact(x.b),
		c: R.compact(x.c)
	})
)
// => { a: ['a', 'b', 'c'], b: [1, 2, 3], c: { a: 1, b: 2, c: 0, f: false } }

Try this R.compact example in Rambda REPL

<summary>All TypeScript definitions</summary>
compact<T>(list: T[]): Array<StrictNonNullable<T>>;
compact<T extends object>(record: T): {
  [K in keyof T as Exclude<T[K], null | undefined> extends never
    ? never
    : K
  ]: Exclude<T[K], null | undefined>
};
<summary>R.compact source</summary>
import { isArray } from './_internals/isArray.js'
import { reject } from './reject.js'
import { rejectObject } from './rejectObject.js'

const isNullOrUndefined = x => x === null || x === undefined

export function compact(input){
	if(isArray(input)){
		return reject(isNullOrUndefined)(input)
	}
	return rejectObject(isNullOrUndefined)(input)
}
<summary>Tests</summary>
import { compact } from './compact'
import { pipe } from './pipe'

test('happy', () => {
  const result = pipe(
    {
      a: [ undefined, '', 'a', 'b', 'c', null ],
      b: [1,2, null, 0, undefined, 3],
      c: { a: 1, b: 2, c: 0, d: undefined, e: null, f: false },
    },
    (x: { a: (string | undefined | null)[]; b: (number | null | undefined)[]; c: { a: number; b: number; c: number; d: undefined; e: null; f: boolean } }) => ({
      a: compact(x.a),
      b: compact(x.b),
      c: compact(x.c),
    })
  )
  expectTypeOf(result.a).toEqualTypeOf<string[]>()
  expectTypeOf(result.b).toEqualTypeOf<number[]>()
  expectTypeOf(result.c).toEqualTypeOf<{ a: number; b: number; c: number; f: boolean }>()
  expect(result.a).toEqual(['', 'a', 'b', 'c'])
  expect(result.b).toEqual([1,2,0,3])
  expect(result.c).toEqual({ a: 1, b: 2, c: 0, f: false })
})

---------------

complement


complement<T extends any[]>(predicate: (...args: T) => unknown): (...args: T) => boolean

It returns inverted version of origin function that accept input as argument.

The return value of inverted is the negative boolean value of origin(input).

const fn = x => x > 5
const inverted = complement(fn)

const result = [
  fn(7),
  inverted(7)
] => [ true, false ]

Try this R.complement example in Rambda REPL

<summary>All TypeScript definitions</summary>
complement<T extends any[]>(predicate: (...args: T) => unknown): (...args: T) => boolean;
<summary>R.complement source</summary>
export function complement(fn) {
  return (...input) => !fn(...input)
}
<summary>Tests</summary>
import { complement } from './complement'

test('happy', () => {
  const fn = complement((x: number) => x > 10)
  const result = fn(1)
  expectTypeOf(result).toEqualTypeOf<boolean>()
  expect(result).toBe(true)
})

test('with multiple parameters', () => {
  const between = (a: number, b: number, c: number) => a < b && b < c
  const f = complement(between)
  expect(f(4, 5, 11)).toBeFalsy()
  expect(f(12, 2, 6)).toBeTruthy()
})

---------------

concat


concat<T>(x: T[]): (y: T[]) => T[]

It returns a new string or array, which is the result of merging x and y.

R.concat([1, 2])([3, 4]) // => [1, 2, 3, 4]
R.concat('foo')('bar') // => 'foobar'

Try this R.concat example in Rambda REPL

<summary>All TypeScript definitions</summary>
concat<T>(x: T[]): (y: T[]) => T[];
concat(x: string): (y: string) => string;
<summary>R.concat source</summary>
export function concat(x) {
  return y => (typeof x === 'string' ? `${x}${y}` : [...x, ...y])
}
<summary>Tests</summary>
import { concat } from './concat'
import { pipe } from './pipe'

const list1 = [1, 2, 3]
const list2 = [4, 5, 6]

test('R.concat', () => {
  const result = pipe(list1, concat(list2))
  expectTypeOf(result).toEqualTypeOf<number[]>()
  expect(result).toEqual([4, 5, 6, 1, 2, 3])
  const resultString = pipe('foo', concat('list2'))
  expectTypeOf(resultString).toEqualTypeOf<string>()
  expect(resultString).toBe('list2foo')
})

---------------

convertToType


convertToType<T>(x: unknown) : T

It helps to convert a value to a specific type. It is useful when you have to overcome TypeScript's type inference.

<summary>All TypeScript definitions</summary>
convertToType<T>(x: unknown) : T;
<summary>R.convertToType source</summary>
export function convertToType(x) {
  return x
}

---------------

count


count<T>(predicate: (x: T) => boolean): (list: T[]) => number

It counts how many times predicate function returns true, when supplied with iteration of list.

const list = [{a: 1}, 1, {a:2}]
const result = R.count(x => x.a !== undefined)(list)
// => 2

Try this R.count example in Rambda REPL

<summary>All TypeScript definitions</summary>
count<T>(predicate: (x: T) => boolean): (list: T[]) => number;
<summary>R.count source</summary>
import { isArray } from './_internals/isArray.js'

export function count(predicate) {
  return list => {
    if (!isArray(list)) {
      return 0
    }

    return list.filter(x => predicate(x)).length
  }
}
<summary>Tests</summary>
import { count } from './count'
import { pipe } from './pipe'

const predicate = (x: any) => x.a !== undefined

test('happy', () => {
  const list = [1, 2, 3]
  const result = pipe(list, count((x: number) => x > 1))
  expectTypeOf(result).toEqualTypeOf<number>()
  expect(result).toBe(2)
})

test('with empty list', () => {
  expect(count(predicate)([])).toBe(0)
})

---------------

countBy


countBy<T>(fn: (x: T) => string | number): (list: T[]) => { [index: string]: number }

It counts elements in a list after each instance of the input list is passed through transformFn function.

const list = [ 'a', 'A', 'b', 'B', 'c', 'C' ]

const result = countBy(x => x.toLowerCase())( list)
const expected = { a: 2, b: 2, c: 2 }
// => `result` is equal to `expected`

Try this R.countBy example in Rambda REPL

<summary>All TypeScript definitions</summary>
countBy<T>(fn: (x: T) => string | number): (list: T[]) => { [index: string]: number };
<summary>R.countBy source</summary>
export function countBy(fn) {
  return list => {
    const willReturn = {}

    list.forEach(item => {
      const key = fn(item)
      if (!willReturn[key]) {
        willReturn[key] = 1
      } else {
        willReturn[key]++
      }
    })

    return willReturn
  }
}
<summary>Tests</summary>
import { countBy } from './countBy'
import { pipe } from './pipe'

const list = ['a', 'A', 'b', 'B', 'c', 'C']

test('happy', () => {
  const result = pipe(
    list,
    countBy((x: string) => x.toLowerCase()),
  )
  expectTypeOf(result.a).toEqualTypeOf<number>()
  expectTypeOf(result.foo).toEqualTypeOf<number>()
  expectTypeOf(result).toEqualTypeOf<{ [index: string]: number }>()
  expect(result).toEqual({ a: 2, b: 2, c: 2 })
})

---------------

createObjectFromKeys


createObjectFromKeys<const K extends readonly PropertyKey[], V>(
	fn: (key: K[number]) => V
): (keys: K) => { [P in K[number]]: V }
const result = R.createObjectFromKeys(
	(x, index) => `${x}-${index}`
)(['a', 'b', 'c'])
// => {a: 'a-0', b: 'b-1', c: 'c-2'}

Try this R.createObjectFromKeys example in Rambda REPL

<summary>All TypeScript definitions</summary>
createObjectFromKeys<const K extends readonly PropertyKey[], V>(
	fn: (key: K[number]) => V
): (keys: K) => { [P in K[number]]: V };
createObjectFromKeys<const K extends readonly PropertyKey[], V>(
	fn: (key: K[number], index: number) => V
): (keys: K) => { [P in K[number]]: V };
<summary>R.createObjectFromKeys source</summary>
export function createObjectFromKeys(fn) {
	return keys => {
		const result = {}
		keys.forEach((key, index) => {
			result[key] = fn(key, index)
		})

		return result
	}
}
<summary>Tests</summary>
import { createObjectFromKeys } from './createObjectFromKeys'

test('happy', () => {
  const result = createObjectFromKeys<['a', 'b'], string>((key: string, index: number) => key.toUpperCase() + index)(['a', 'b'])
  const expected = { a: 'A0', b: 'B1' }
  expect(result).toEqual(expected)
})

---------------

defaultTo


defaultTo<T>(defaultValue: T): (input: unknown) => T

It returns defaultValue, if all of inputArguments are undefined, null or NaN.

Else, it returns the first truthy inputArguments instance(from left to right).

:boom: Typescript Note: Pass explicit type annotation when used with R.pipe/R.compose for better type inference

R.defaultTo('foo')('bar') // => 'bar'
R.defaultTo('foo'))(undefined) // => 'foo'

// Important - emtpy string is not falsy value
R.defaultTo('foo')('') // => 'foo'

Try this R.defaultTo example in Rambda REPL

<summary>All TypeScript definitions</summary>
defaultTo<T>(defaultValue: T): (input: unknown) => T;
<summary>R.defaultTo source</summary>
function isFalsy(input) {
  return input === undefined || input === null || Number.isNaN(input) === true
}

export function defaultTo(defaultArgument) {
  return input => isFalsy(input) ? defaultArgument : input
}
<summary>Tests</summary>
import { defaultTo } from './defaultTo'
import { pipe } from './pipe'

test('happy', () => {
  const result = pipe('bar' as unknown, defaultTo('foo'))
  expectTypeOf(result).toEqualTypeOf<string>()
  expect(result).toBe('bar')
})

test('with undefined', () => {
  expect(defaultTo('foo')(undefined)).toBe('foo')
})

test('with null', () => {
  expect(defaultTo('foo')(null)).toBe('foo')
})

test('with NaN', () => {
  expect(defaultTo('foo')(Number.NaN)).toBe('foo')
})

test('with empty string', () => {
  expect(defaultTo('foo')('')).toBe('')
})

test('with false', () => {
  expect(defaultTo('foo')(false)).toBeFalsy()
})

test('when inputArgument passes initial check', () => {
  expect(defaultTo('foo')('bar')).toBe('bar')
})

---------------

delay


delay(ms: number): Promise<'RAMBDA_DELAY'>

setTimeout as a promise that resolves to RAMBDA_DELAY string after ms milliseconds.

<summary>All TypeScript definitions</summary>
delay(ms: number): Promise<'RAMBDA_DELAY'>;
<summary>R.delay source</summary>
export const RAMBDA_DELAY = 'RAMBDA_DELAY'

export function delay(ms) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(RAMBDA_DELAY)
    }, ms)
  })
}

---------------

descend


descend<T>(fn: (obj: T) => Ord): (a: T, b: T)=> Ordering

Helper function to be used with R.sort to sort list in descending order.

const result = R.pipe(
	[{a: 1}, {a: 2}, {a: 0}],
	R.sort(R.descend(R.prop('a')))
)
// => [{a: 2}, {a: 1}, {a: 0}]

Try this R.descend example in Rambda REPL

<summary>All TypeScript definitions</summary>
descend<T>(fn: (obj: T) => Ord): (a: T, b: T)=> Ordering;
<summary>R.descend source</summary>
import { createCompareFunction } from './ascend.js'

export function descend(getFunction) {
  return (a, b) => {
    const aValue = getFunction(a)
    const bValue = getFunction(b)

    return createCompareFunction(aValue, bValue, 1, -1)
  }
}

---------------

difference


difference<T>(x: T[]): (y: T[]) => T[]

It returns a merged list of x and y with all equal elements removed.

R.equals is used to determine equality.

const x = [ 1, 2, 3, 4 ]
const y = [ 3, 4, 5, 6 ]

const result = R.difference(x)(y)
// => [ 1, 2, 5, 6 ]

Try this R.difference example in Rambda REPL

<summary>All TypeScript definitions</summary>
difference<T>(x: T[]): (y: T[]) => T[];
<summary>R.difference source</summary>
import { filter } from './filter.js'
import { excludes } from './excludes.js'

export function difference(listA) {
	return listB => ([
		...filter(value => excludes(listB)(value))(listA),
		...filter(value => excludes(listA)(value))(listB),
	])
}
<summary>Tests</summary>
import { difference } from './difference'

test('difference', () => {
  const list1 = [1, 2, 3, 4]
  const list2 = [3, 4, 5, 6]
  expect(difference(list1)(list2)).toEqual([1, 2, 5, 6])
  expect(difference([])([])).toEqual([])
})

test('difference with objects', () => {
  const list1 = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]
  const list2 = [{ id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }]
  const result = difference(list1)(list2)
  expectTypeOf(result).toEqualTypeOf<{ id: number }[]>()
  expect(result).toEqual([
    { id: 1 },
    { id: 2 },
    { id: 5 },
    { id: 6 },
  ])
})

---------------

drop


drop<T>(howMany: number): (list: T[]) => T[]

It returns howMany items dropped from beginning of list.

R.drop(2)(['foo', 'bar', 'baz']) // => ['baz']

Try this R.drop example in Rambda REPL

<summary>All TypeScript definitions</summary>
drop<T>(howMany: number): (list: T[]) => T[];
<summary>R.drop source</summary>
export function drop(howManyToDrop) {
  return list => list.slice(howManyToDrop > 0 ? howManyToDrop : 0)
}
<summary>Tests</summary>
import { drop } from './drop'
import { pipe } from './pipe'

test('happy', () => {
  const result = pipe([1, 2, 3, 4], drop(2))
  expectTypeOf(result).toEqualTypeOf<number[]>()
  expect(result).toEqual([3, 4])
})

test('with array', () => {
  expect(drop(2)(['foo', 'bar', 'baz'])).toEqual(['baz'])
  expect(drop(3)(['foo', 'bar', 'baz'])).toEqual([])
  expect(drop(4)(['foo', 'bar', 'baz'])).toEqual([])
})

test('with non-positive count', () => {
  expect(drop(0)([1, 2, 3])).toEqual([1, 2, 3])
  expect(drop(-1)([1, 2, 3])).toEqual([1, 2, 3])
  expect(drop(Number.NEGATIVE_INFINITY)([1, 2, 3])).toEqual([1, 2, 3])
})

---------------

dropLast


dropLast<T>(howMany: number): (list: T[]) => T[]

It returns howMany items dropped from the end of list.

<summary>All TypeScript definitions</summary>
dropLast<T>(howMany: number): (list: T[]) => T[];
<summary>R.dropLast source</summary>
export function dropLast(numberItems) {
  return list => (numberItems > 0 ? list.slice(0, -numberItems) : list.slice())
}
<summary>Tests</summary>
import { dropLast } from './dropLast'

test('with array', () => {
  expect(dropLast(2)(['foo', 'bar', 'baz'])).toEqual(['foo'])
  expect(dropLast(3)(['foo', 'bar', 'baz'])).toEqual([])
  expect(dropLast(4)(['foo', 'bar', 'baz'])).toEqual([])
})

test('with non-positive count', () => {
  expect(dropLast(0)([1, 2, 3])).toEqual([1, 2, 3])
  expect(dropLast(-1)([1, 2, 3])).toEqual([1, 2, 3])
  expect(dropLast(Number.NEGATIVE_INFINITY)([1, 2, 3])).toEqual([1, 2, 3])
})

---------------

dropLastWhile


dropLastWhile<T>(predicate: (x: T, index: number) => boolean): (list: T[]) => T[]
const list = [1, 2, 3, 4, 5];
const predicate = x => x >= 3

const result = dropLastWhile(predicate)(list);
// => [1, 2]

Try this R.dropLastWhile example in Rambda REPL

<summary>All TypeScript definitions</summary>
dropLastWhile<T>(predicate: (x: T, index: number) => boolean): (list: T[]) => T[];
dropLastWhile<T>(predicate: (x: T) => boolean): (list: T[]) => T[];
<summary>R.dropLastWhile source</summary>
export function dropLastWhile(predicate) {
  return list => {
    if (list.length === 0) {
      return list
    }

    const toReturn = []
    let counter = list.length

    while (counter) {
      const item = list[--counter]
      if (!predicate(item, counter)) {
        toReturn.push(item)
        break
      }
    }

    while (counter) {
      toReturn.push(list[--counter])
    }

    return toReturn.reverse()
  }
}
<summary>Tests</summary>
import { dropLastWhile } from './dropLastWhile'

const list = [1, 2, 3, 4, 5]

test('with list', () => {
  const result = dropLastWhile((x: number) => x >= 3)(list)
  expect(result).toEqual([1, 2])
})

test('with empty list', () => {
  expect(dropLastWhile(() => true)([])).toEqual([])
})

---------------

dropRepeatsBy


dropRepeatsBy<T, U>(fn: (x: T) => U): (list: T[]) => T[]
const result = R.dropRepeatsBy(
  Math.abs,
  [1, -1, 2, 3, -3]
)
// => [1, 2, 3]

Try this R.dropRepeatsBy example in Rambda REPL

<summary>All TypeScript definitions</summary>
dropRepeatsBy<T, U>(fn: (x: T) => U): (list: T[]) => T[];

---------------

dropRepeatsWith


dropRepeatsWith<T>(predicate: (x: T, y: T) => boolean): (list: T[]) => T[]
const list = [{a:1,b:2}, {a:1,b:3}, {a:2, b:4}]
const result = R.dropRepeatsWith(R.prop('a'))(list)

// => [{a:1,b:2}, {a:2, b:4}]

Try this R.dropRepeatsWith example in Rambda REPL

<summary>All TypeScript definitions</summary>
dropRepeatsWith<T>(predicate: (x: T, y: T) => boolean): (list: T[]) => T[];

---------------

dropWhile


dropWhile<T>(predicate: (x: T, index: number) => boolean): (list: T[]) => T[]
const list = [1, 2, 3, 4]
const predicate = x => x < 3
const result = R.dropWhile(predicate)(list)
// => [3, 4]

Try this R.dropWhile example in Rambda REPL

<summary>All TypeScript definitions</summary>
dropWhile<T>(predicate: (x: T, index: number) => boolean): (list: T[]) => T[];
dropWhile<T>(predicate: (x: T) => boolean): (list: T[]) => T[];
<summary>R.dropWhile source</summary>
export function dropWhile(predicate) {
  return iterable => {
    const toReturn = []
    let counter = 0

    while (counter < iterable.length) {
      const item = iterable[counter++]
      if (!predicate(item, counter)) {
        toReturn.push(item)
        break
      }
    }

    while (counter < iterable.length) {
      toReturn.push(iterable[counter++])
    }

    return toReturn
  }
}
<summary>Tests</summary>
import { dropWhile } from './dropWhile'
import { pipe } from './pipe'

const list = [1, 2, 3, 4]

test('happy', () => {
  const result = pipe(
    [1, 2, 3],
    dropWhile((x: number) => x < 3),
  )
  expectTypeOf(result).toEqualTypeOf<number[]>()
  expect(result).toEqual([3])
})

test('always false', () => {
  const predicate = () => false
  const result = dropWhile(predicate)(list)
  expect(result).toEqual(list)
})

test('with index', () => {
  const result = pipe(
    [1, 2, 3],
    dropWhile((x: number, i: number) => {
      expectTypeOf(i).toEqualTypeOf<number>()
      return x + i > 2
    }),
  )
  expectTypeOf(result).toEqualTypeOf<number[]>()
  expect(result).toEqual([1, 2, 3])
})

---------------

duplicateBy


duplicateBy<T, U>(fn: (x: T) => U): (list: T[]) => T[]
const list = [{a:1}, {a:2}, {a:1}]
const result = R.duplicateBy(x => x)(list)

// => [{a:1}]

Try this R.duplicateBy example in Rambda REPL

<summary>All TypeScript definitions</summary>
duplicateBy<T, U>(fn: (x: T) => U): (list: T[]) => T[];
<summary>R.duplicateBy source</summary>
import { _Set } from '../src/_internals/set.js'

export function duplicateBy(fn) {
  return list => {
    const set = new _Set()

    return list.filter(item => !set.checkUniqueness(fn(item)))
  }
}
<summary>Tests</summary>
import { duplicateBy } from './duplicateBy'

test('happy', () => {
  expect(duplicateBy(Math.abs)([-2, -1, 0, 1, 2])).toEqual([1, 2])
})

test('returns an empty array for an empty array', () => {
  expect(duplicateBy(Math.abs)([])).toEqual([])
})

test('uses R.uniq', () => {
  const list = [{ a: 1 }, { a: 2 }, { a: 1 }]
  const expected = [{ a: 1 }]
  expect(duplicateBy((x: any) => x)(list)).toEqual(expected)
})

---------------

eqBy


eqBy<T>(fn: (x: T) => unknown, a: T): (b: T) => boolean
const result = R.eqBy(Math.abs, 5)(-5)
// => true

Try this R.eqBy example in Rambda REPL

<summary>All TypeScript definitions</summary>
eqBy<T>(fn: (x: T) => unknown, a: T): (b: T) => boolean;
<summary>R.eqBy source</summary>
import { equalsFn } from './equals.js'

export function eqBy(fn, a) {
  return b => equalsFn(fn(a), fn(b))
}
<summary>Tests</summary>
import { eqBy } from './eqBy'

test('deteremines whether two values map to the same value in the codomain', () => {
  expect(eqBy<number>(Math.abs, 5)(5)).toBe(true)
  expect(eqBy<number>(Math.abs, 5)(-5)).toBe(true)
  expect(eqBy<number>(Math.abs, -5)(5)).toBe(true)
  expect(eqBy<number>(Math.abs, -5)(-5)).toBe(true)
  expect(eqBy<number>(Math.abs, 42)(99)).toBe(false)
})

test('has R.equals semantics', () => {
  expect(eqBy(Math.abs, Number.NaN)(Number.NaN)).toBe(true)
  expect(eqBy((x: number[]) => x.length, [42])([42])).toBe(true)
  expect(eqBy((x: any) => x, { a: 1 })({ a: 1 })).toBe(true)
  expect(eqBy((x: any) => x, { a: 1 })({ a: 2 })).toBe(false)
})

---------------

eqProps


eqProps<T, K extends keyof T>(prop: K, obj1: T): (obj2: T) => boolean

It returns true if property prop in obj1 is equal to property prop in obj2 according to R.equals.

const obj1 = {a: 1, b:2}
const obj2 = {a: 1, b:3}
const result = R.eqProps('a', obj1)(obj2)
// => true

Try this R.eqProps example in Rambda REPL

<summary>All TypeScript definitions</summary>
eqProps<T, K extends keyof T>(prop: K, obj1: T): (obj2: T) => boolean;
<summary>R.eqProps source</summary>
import { equalsFn } from './equals.js'

export function eqProps(property, objA) {
  return objB => equalsFn( objA[property], objB[property] )
}
<summary>Tests</summary>
import { eqProps } from './eqProps'
import { pipe } from './pipe'

const obj1 = {
  a: 1,
  b: 2,
}
const obj2 = {
  a: 1,
  b: 3,
}

test('happy', () => {
  const objA = { a: { b: 1 }, c: 2 }
  const objB = { a: { b: 1 }, c: 3 }
  const result = pipe(objA, eqProps('a', objB))
  expectTypeOf(result).toEqualTypeOf<boolean>()
  expect(result).toBe(true)
})

test('props are equal', () => {
  const result = eqProps('a', obj1)(obj2)
  expect(result).toBeTruthy()
})

test('props are not equal', () => {
  const result = eqProps('b', obj1)(obj2)
  expect(result).toBeFalsy()
})

test('prop does not exist', () => {
  const result = eqProps('c' as 'a' | 'b', obj1)(obj2)
  expect(result).toBeTruthy()
})

---------------

equals


equals<T>(x: T): (y: T) => boolean

It deeply compares x and y and returns true if they are equal.

:boom: It doesn't handle cyclical data structures and functions

R.equals(
  [1, {a:2}, [{b: 3}]],
  [1, {a:2}, [{b: 3}]]
) // => true

Try this R.equals example in Rambda REPL

<summary>All TypeScript definitions</summary>
equals<T>(x: T): (y: T) => boolean;
<summary>R.equals source</summary>
import { isArray } from './_internals/isArray.js'
import { type } from './type.js'

export function _lastIndexOf(valueToFind, list) {
  if (!isArray(list)) {
    throw new Error(`Cannot read property 'indexOf' of ${list}`)
  }

  const typeOfValue = type(valueToFind)
  if (!['Array', 'NaN', 'Object', 'RegExp'].includes(typeOfValue)) {
    return list.lastIndexOf(valueToFind)
  }

  const { length } = list
  let index = length
  let foundIndex = -1

  while (--index > -1 && foundIndex === -1) {
    if (equalsFn(list[index], valueToFind)) {
      foundIndex = index
    }
  }

  return foundIndex
}

export function _indexOf(valueToFind, list) {
  if (!isArray(list)) {
    throw new Error(`Cannot read property 'indexOf' of ${list}`)
  }

  const typeOfValue = type(valueToFind)
  if (!['Array', 'NaN', 'Object', 'RegExp'].includes(typeOfValue)) {
    return list.indexOf(valueToFind)
  }

  let index = -1
  let foundIndex = -1
  const { length } = list

  while (++index < length && foundIndex === -1) {
    if (equalsFn(list[index], valueToFind)) {
      foundIndex = index
    }
  }

  return foundIndex
}

function _arrayFromIterator(iter) {
  const list = []
  let next
  while (!(next = iter.next()).done) {
    list.push(next.value)
  }

  return list
}

function _compareSets(a, b) {
  if (a.size !== b.size) {
    return false
  }

  const aList = _arrayFromIterator(a.values())
  const bList = _arrayFromIterator(b.values())

  const filtered = aList.filter(aInstance => _indexOf(aInstance, bList) === -1)

  return filtered.length === 0
}

function compareErrors(a, b) {
  if (a.message !== b.message) {
    return false
  }
  if (a.toString !== b.toString) {
    return false
  }

  return a.toString() === b.toString()
}

function parseDate(maybeDate) {
  if (!maybeDate.toDateString) {
    return [false]
  }

  return [true, maybeDate.getTime()]
}

function parseRegex(maybeRegex) {
  if (maybeRegex.constructor !== RegExp) {
    return [false]
  }

  return [true, maybeRegex.toString()]
}

export function equalsFn(a, b) {
  if (Object.is(a, b)) {
    return true
  }

  const aType = type(a)

  if (aType !== type(b)) {
    return false
  }
  if (aType === 'Function') {
    return a.name === undefined ? false : a.name === b.name
  }

  if (['NaN', 'Null', 'Undefined'].includes(aType)) {
    return true
  }

  if (['BigInt', 'Number'].includes(aType)) {
    if (Object.is(-0, a) !== Object.is(-0, b)) {
      return false
    }

    return a.toString() === b.toString()
  }

  if (['Boolean', 'String'].includes(aType)) {
    return a.toString() === b.toString()
  }

  if (aType === 'Array') {
    const aClone = Array.from(a)
    const bClone = Array.from(b)

    if (aClone.toString() !== bClone.toString()) {
      return false
    }

    let loopArrayFlag = true
    aClone.forEach((aCloneInstance, aCloneIndex) => {
      if (loopArrayFlag) {
        if (
          aCloneInstance !== bClone[aCloneIndex] &&
          !equalsFn(aCloneInstance, bClone[aCloneIndex])
        ) {
          loopArrayFlag = false
        }
      }
    })

    return loopArrayFlag
  }

  const aRegex = parseRegex(a)
  const bRegex = parseRegex(b)

  if (aRegex[0]) {
    return bRegex[0] ? aRegex[1] === bRegex[1] : false
  }
  if (bRegex[0]) {
    return false
  }

  const aDate = parseDate(a)
  const bDate = parseDate(b)

  if (aDate[0]) {
    return bDate[0] ? aDate[1] === bDate[1] : false
  }
  if (bDate[0]) {
    return false
  }

  if (a instanceof Error) {
    if (!(b instanceof Error)) {
      return false
    }

    return compareErrors(a, b)
  }

  if (aType === 'Set') {
    return _compareSets(a, b)
  }

  if (aType === 'Object') {
    const aKeys = Object.keys(a)

    if (aKeys.length !== Object.keys(b).length) {
      return false
    }

    let loopObjectFlag = true
    aKeys.forEach(aKeyInstance => {
      if (loopObjectFlag) {
        const aValue = a[aKeyInstance]
        const bValue = b[aKeyInstance]

        if (aValue !== bValue && !equalsFn(aValue, bValue)) {
          loopObjectFlag = false
        }
      }
    })

    return loopObjectFlag
  }

  return false
}
export function equals(a) {
  return b => equalsFn(a, b)
}
<summary>Tests</summary>
import { equals } from './equals'

test('happy', () => {
  const result = equals(4)(1)
  expectTypeOf(result).toEqualTypeOf<boolean>()
  expect(result).toBe(false)

  const foo = { a: 1 }
  const bar = { a: 2 }
  const result2 = equals(foo)(bar)
  expectTypeOf(result2).toEqualTypeOf<boolean>()
  expect(result2).toBe(false)
})

test('compare functions', () => {
  function foo() 

Current Tags

  • 6.7.0-beta.0                                ...           beta (6 years ago)
  • 11.3.0                                ...           latest (3 months ago)

222 Versions

  • 11.3.0                                ...           3 months ago
  • 11.2.0                                ...           6 months ago
  • 11.1.0                                ...           10 months ago
  • 11.0.1                                ...           a year ago
  • 11.0.0                                ...           a year ago
  • 10.3.4                                ...           a year ago
  • 10.3.3                                ...           a year ago
  • 10.3.2                                ...           a year ago
  • 10.3.1                                ...           a year ago
  • 10.3.0                                ...           a year ago
  • 10.2.0                                ...           2 years ago
  • 10.1.0                                ...           2 years ago
  • 10.0.1                                ...           2 years ago
  • 10.0.0                                ...           2 years ago
  • 10.0.0-beta.3                                ...           2 years ago
  • 10.0.0-beta.1                                ...           2 years ago
  • 10.0.0-alpha.0                                ...           2 years ago
  • 9.4.2                                ...           2 years ago
  • 9.4.1                                ...           2 years ago
  • 9.4.0                                ...           2 years ago
  • 9.3.0                                ...           2 years ago
  • 9.2.1                                ...           2 years ago
  • 9.2.0                                ...           3 years ago
  • 9.1.1                                ...           3 years ago
  • 9.1.0                                ...           3 years ago
  • 9.0.1                                ...           3 years ago
  • 9.0.0                                ...           3 years ago
  • 8.6.0                                ...           3 years ago
  • 8.5.0                                ...           3 years ago
  • 8.4.0                                ...           3 years ago
  • 8.3.0                                ...           3 years ago
  • 8.2.0                                ...           3 years ago
  • 8.1.0                                ...           3 years ago
  • 8.0.0                                ...           3 years ago
  • 7.5.0                                ...           4 years ago
  • 7.4.0                                ...           4 years ago
  • 7.3.0                                ...           4 years ago
  • 7.2.1                                ...           4 years ago
  • 7.2.0                                ...           4 years ago
  • 7.1.4                                ...           5 years ago
  • 7.1.3                                ...           5 years ago
  • 7.1.2                                ...           5 years ago
  • 7.1.1                                ...           5 years ago
  • 7.1.0                                ...           5 years ago
  • 7.0.3                                ...           5 years ago
  • 7.0.2                                ...           5 years ago
  • 7.0.1                                ...           5 years ago
  • 7.0.0                                ...           5 years ago
  • 6.9.0                                ...           5 years ago
  • 6.8.3                                ...           5 years ago
  • 6.8.2                                ...           5 years ago
  • 6.8.1                                ...           5 years ago
  • 6.8.0                                ...           5 years ago
  • 6.7.0                                ...           6 years ago
  • 6.7.0-beta.0                                ...           6 years ago
  • 6.6.0                                ...           6 years ago
  • 6.5.3                                ...           6 years ago
  • 6.5.2                                ...           6 years ago
  • 6.5.1                                ...           6 years ago
  • 6.5.0                                ...           6 years ago
  • 6.4.0                                ...           6 years ago
  • 6.3.1                                ...           6 years ago
  • 6.3.0                                ...           6 years ago
  • 6.2.0                                ...           6 years ago
  • 6.1.0                                ...           6 years ago
  • 6.0.1                                ...           6 years ago
  • 6.0.0                                ...           6 years ago
  • 5.13.1                                ...           6 years ago
  • 5.13.0                                ...           6 years ago
  • 5.12.1                                ...           6 years ago
  • 5.12.0                                ...           6 years ago
  • 5.11.0                                ...           6 years ago
  • 5.10.0                                ...           6 years ago
  • 5.9.0                                ...           6 years ago
  • 5.8.0                                ...           6 years ago
  • 5.7.0                                ...           6 years ago
  • 5.6.3                                ...           6 years ago
  • 5.6.2                                ...           6 years ago
  • 5.6.1                                ...           6 years ago
  • 5.6.0                                ...           6 years ago
  • 5.5.0                                ...           6 years ago
  • 5.4.3                                ...           6 years ago
  • 5.4.2                                ...           6 years ago
  • 5.4.1                                ...           7 years ago
  • 5.4.0                                ...           7 years ago
  • 5.3.0                                ...           7 years ago
  • 5.2.1                                ...           7 years ago
  • 5.2.0                                ...           7 years ago
  • 5.1.1                                ...           7 years ago
  • 5.1.0                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 4.6.0                                ...           7 years ago
  • 4.5.0                                ...           7 years ago
  • 4.4.0                                ...           7 years ago
  • 4.3.0                                ...           7 years ago
  • 4.2.0                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.0.2                                ...           7 years ago
  • 4.0.1                                ...           7 years ago
  • 4.0.0                                ...           7 years ago
  • 3.3.0                                ...           7 years ago
  • 3.2.5                                ...           7 years ago
  • 3.2.1                                ...           7 years ago
  • 3.2.0                                ...           7 years ago
  • 3.1.1                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.1                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.14.5                                ...           7 years ago
  • 2.14.4                                ...           7 years ago
  • 2.14.3                                ...           7 years ago
  • 2.14.2                                ...           7 years ago
  • 2.14.1                                ...           7 years ago
  • 2.14.0                                ...           7 years ago
  • 2.13.1                                ...           7 years ago
  • 2.13.0                                ...           7 years ago
  • 2.12.0                                ...           7 years ago
  • 2.11.2                                ...           7 years ago
  • 2.11.1                                ...           7 years ago
  • 2.11.0                                ...           7 years ago
  • 2.10.2                                ...           8 years ago
  • 2.10.1                                ...           8 years ago
  • 2.10.0                                ...           8 years ago
  • 2.9.0                                ...           8 years ago
  • 2.8.0                                ...           8 years ago
  • 2.7.1                                ...           8 years ago
  • 2.7.0                                ...           8 years ago
  • 2.6.0                                ...           8 years ago
  • 2.5.0                                ...           8 years ago
  • 2.4.1                                ...           8 years ago
  • 2.4.0                                ...           8 years ago
  • 2.3.2                                ...           8 years ago
  • 2.3.1                                ...           8 years ago
  • 2.3.0                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.1                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.2.6                                ...           8 years ago
  • 1.2.5                                ...           8 years ago
  • 1.2.4                                ...           8 years ago
  • 1.2.2                                ...           8 years ago
  • 1.2.1                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.5                                ...           8 years ago
  • 1.1.4                                ...           8 years ago
  • 1.1.3                                ...           8 years ago
  • 1.1.2                                ...           9 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.13                                ...           9 years ago
  • 1.0.12                                ...           9 years ago
  • 1.0.11                                ...           9 years ago
  • 1.0.10                                ...           9 years ago
  • 1.0.9                                ...           9 years ago
  • 1.0.8                                ...           9 years ago
  • 1.0.7                                ...           9 years ago
  • 1.0.6                                ...           9 years ago
  • 1.0.5                                ...           9 years ago
  • 1.0.4                                ...           9 years ago
  • 1.0.3                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.9.8                                ...           9 years ago
  • 0.9.7                                ...           9 years ago
  • 0.9.6                                ...           9 years ago
  • 0.9.5                                ...           9 years ago
  • 0.9.4                                ...           9 years ago
  • 0.9.3                                ...           9 years ago
  • 0.9.2                                ...           9 years ago
  • 0.9.1                                ...           9 years ago
  • 0.8.10                                ...           9 years ago
  • 0.9.0                                ...           9 years ago
  • 0.8.9                                ...           9 years ago
  • 0.8.8                                ...           9 years ago
  • 0.8.7                                ...           9 years ago
  • 0.8.6                                ...           9 years ago
  • 0.8.5                                ...           9 years ago
  • 0.8.4                                ...           9 years ago
  • 0.8.3                                ...           9 years ago
  • 0.8.2                                ...           9 years ago
  • 0.8.1                                ...           9 years ago
  • 0.8.0                                ...           9 years ago
  • 0.7.6                                ...           9 years ago
  • 0.7.5                                ...           9 years ago
  • 0.7.4                                ...           9 years ago
  • 0.7.3                                ...           9 years ago
  • 0.7.2                                ...           9 years ago
  • 0.7.1                                ...           9 years ago
  • 0.7.0                                ...           9 years ago
  • 0.6.1                                ...           9 years ago
  • 0.5.13                                ...           9 years ago
  • 0.6.0                                ...           10 years ago
  • 0.5.12                                ...           10 years ago
  • 0.5.11                                ...           10 years ago
  • 0.5.10                                ...           10 years ago
  • 0.5.9                                ...           10 years ago
  • 0.5.8                                ...           10 years ago
  • 0.5.7                                ...           10 years ago
  • 0.5.6                                ...           10 years ago
  • 0.5.5                                ...           10 years ago
  • 0.5.4                                ...           10 years ago
  • 0.5.3                                ...           10 years ago
  • 0.5.2                                ...           10 years ago
  • 0.5.1                                ...           10 years ago
  • 0.5.0                                ...           10 years ago
  • 0.4.3                                ...           10 years ago
  • 0.4.2                                ...           10 years ago
  • 0.4.1                                ...           10 years ago
  • 0.4.0                                ...           10 years ago
  • 0.3.4                                ...           10 years ago
  • 0.3.3                                ...           10 years ago
  • 0.3.2                                ...           10 years ago
  • 0.3.1                                ...           10 years ago
  • 0.3.0                                ...           10 years ago
  • 0.2.1                                ...           10 years ago
  • 0.2.0                                ...           10 years ago
  • 0.1.5                                ...           10 years ago
  • 0.1.4                                ...           10 years ago
  • 0.1.3                                ...           10 years ago
  • 0.1.2                                ...           10 years ago
  • 0.1.1                                ...           10 years ago
  • 0.1.0                                ...           10 years ago
Maintainers (1)
Downloads
Total 1,045
Today 0
This Week 107
This Month 718
Last Day 14
Last Week 67
Last Month 252
Dependencies (0)
None
Dev Dependencies (14)

© 2010 - cnpmjs.org x YWFE | Home | YWFE