Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Extended version of Rambda(utility library) - Documentation
Rambda
is smaller and faster alternative to the popular functional programming library Ramda. - Documentation
Rambdax passthrough all Rambda methods and introduce some new functions.
The idea of Rambdax is to extend Rambda without worring for Ramda compatibility.
import { composeAsync, filter, delay, mapAsync } from 'rambdax'
const result = await composeAsync(
mapAsync(async x => {
await delay(100)
return x + 1
}),
filter(x => x > 1)
)([1, 2, 3])
// => [3, 4]
You can test this example in Rambda's REPL
TypeScript definitions are included in the library, in comparison to Ramda, where you need to additionally install @types/ramda
.
Still, you need to be aware that functional programming features in TypeScript
are in development, which means that using R.compose/R.pipe can be problematic.
Important - Rambdax version 9.0.0
(or higher) requires TypeScript version 4.3.3
(or higher).
R.path
, R.paths
, R.assocPath
and R.lensPath
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} })
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
Rambdax
implements some methods from Ramda
community projects, such as R.lensSatisfies
, R.lensEq
and R.viewOr
.
Ramda
uses a lot of internals, which hides a lot of logic. Reading the full source code of a method can be challenging.
If the project is written in Javascript, then go to source definition
action will lead you to actual implementation of the method.
import * as R from "https://deno.land/x/rambdax/mod.ts";
Alternative TS definitions are available as rambdax/immutable
. These are Rambdax definitions linted with ESLint functional/prefer-readonly-type
plugin.
.prototype
Ramda
example doesn't looks convincingR.pipe
/R.compose
R.pipe
/R.compose
to add more composition methodsleft-pad
types of debacles happens partially because of such methods that should not be hidden, bur rather part of your code base even if they need to exist.R.partial
, so I don't want to add more methods that are based on itright/left
methods confusing so I added them only where it makes sense.Rambdax
has such method and there it works very differentlyR.scan
and I find that it doesn't help with readabilityThe following methods are not going to be added(reason for exclusion is provided as a comment):
yarn add rambdax
For UMD usage either use ./dist/rambdax.umd.js
or the following CDN link:
https://unpkg.com/rambdax@CURRENT_VERSION/dist/rambdax.umd.js
import {add} from "https://deno.land/x/rambda/mod.ts";
Rambda's type detects async functions and unresolved Promises
. The returned values are 'Async'
and 'Promise'
.
Rambda's type handles NaN input, in which case it returns NaN
.
Rambda's forEach can iterate over objects not only arrays.
Rambda's map, filter, partition when they iterate over objects, they pass property and input object as predicate's argument.
Rambda's filter returns empty array with bad input(null
or undefined
), while Ramda throws.
Ramda's clamp work with strings, while Rambda's method work only with numbers.
Ramda's indexOf/lastIndexOf work with strings and lists, while Rambda's method work only with lists as iterable input.
Error handling, when wrong inputs are provided, may not be the same. This difference will be better documented once all brute force tests are completed.
TypeScript definitions between rambda
and @types/ramda
may vary.
There are methods which are benchmarked only with Ramda
and Rambda
(i.e. no Lodash
).
Note that some of these methods, are called with and without curring. This is done in order to give more detailed performance feedback.
The benchmarks results are produced from latest versions of Rambda, Lodash(4.17.21) and Ramda(0.30.1).
method | Rambda | Ramda | Lodash |
---|---|---|---|
add | 🚀 Fastest | 21.52% slower | 82.15% slower |
adjust | 8.48% slower | 🚀 Fastest | 🔳 |
all | 🚀 Fastest | 7.18% slower | 🔳 |
allPass | 🚀 Fastest | 88.25% slower | 🔳 |
allPass | 🚀 Fastest | 98.56% slower | 🔳 |
and | 🚀 Fastest | 89.09% slower | 🔳 |
any | 🚀 Fastest | 92.87% slower | 45.82% slower |
anyPass | 🚀 Fastest | 98.25% slower | 🔳 |
append | 🚀 Fastest | 2.07% slower | 🔳 |
applySpec | 🚀 Fastest | 80.43% slower | 🔳 |
assoc | 72.32% slower | 60.08% slower | 🚀 Fastest |
clone | 🚀 Fastest | 91.86% slower | 86.48% slower |
compose | 6.07% slower | 16.89% slower | 🚀 Fastest |
converge | 78.63% slower | 🚀 Fastest | 🔳 |
curry | 🚀 Fastest | 28.86% slower | 🔳 |
curryN | 🚀 Fastest | 41.05% slower | 🔳 |
defaultTo | 🚀 Fastest | 48.91% slower | 🔳 |
drop | 🚀 Fastest | 82.35% slower | 🔳 |
dropLast | 🚀 Fastest | 86.74% slower | 🔳 |
equals | 58.37% slower | 96.73% slower | 🚀 Fastest |
filter | 6.7% slower | 72.03% slower | 🚀 Fastest |
find | 🚀 Fastest | 85.14% slower | 42.65% slower |
findIndex | 🚀 Fastest | 86.48% slower | 72.27% slower |
flatten | 🚀 Fastest | 85.68% slower | 3.57% slower |
ifElse | 🚀 Fastest | 58.56% slower | 🔳 |
includes | 🚀 Fastest | 81.64% slower | 🔳 |
indexOf | 🚀 Fastest | 80.17% slower | 🔳 |
indexOf | 🚀 Fastest | 82.2% slower | 🔳 |
init | 🚀 Fastest | 92.24% slower | 13.3% slower |
is | 🚀 Fastest | 57.69% slower | 🔳 |
isEmpty | 🚀 Fastest | 97.14% slower | 54.99% slower |
last | 🚀 Fastest | 93.43% slower | 5.28% slower |
lastIndexOf | 🚀 Fastest | 85.19% slower | 🔳 |
map | 🚀 Fastest | 86.6% slower | 11.73% slower |
match | 🚀 Fastest | 44.83% slower | 🔳 |
merge | 🚀 Fastest | 12.21% slower | 55.76% slower |
none | 🚀 Fastest | 96.48% slower | 🔳 |
objOf | 🚀 Fastest | 38.05% slower | 🔳 |
omit | 🚀 Fastest | 69.95% slower | 97.34% slower |
over | 🚀 Fastest | 56.23% slower | 🔳 |
path | 37.81% slower | 77.81% slower | 🚀 Fastest |
pick | 🚀 Fastest | 19.07% slower | 80.2% slower |
pipe | 🚀 Fastest | 0.11% slower | 🔳 |
prop | 🚀 Fastest | 87.95% slower | 🔳 |
propEq | 🚀 Fastest | 91.92% slower | 🔳 |
range | 🚀 Fastest | 61.8% slower | 57.44% slower |
reduce | 60.48% slower | 77.1% slower | 🚀 Fastest |
repeat | 48.57% slower | 68.98% slower | 🚀 Fastest |
replace | 33.45% slower | 33.99% slower | 🚀 Fastest |
set | 🚀 Fastest | 50.35% slower | 🔳 |
sort | 🚀 Fastest | 40.23% slower | 🔳 |
sortBy | 🚀 Fastest | 25.29% slower | 56.88% slower |
split | 🚀 Fastest | 55.37% slower | 17.64% slower |
splitEvery | 🚀 Fastest | 71.98% slower | 🔳 |
take | 🚀 Fastest | 91.96% slower | 4.72% slower |
takeLast | 🚀 Fastest | 93.39% slower | 19.22% slower |
test | 🚀 Fastest | 82.34% slower | 🔳 |
type | 🚀 Fastest | 48.6% slower | 🔳 |
uniq | 🚀 Fastest | 84.9% slower | 🔳 |
uniqBy | 51.93% slower | 🚀 Fastest | 🔳 |
uniqWith | 8.29% slower | 🚀 Fastest | 🔳 |
uniqWith | 14.23% slower | 🚀 Fastest | 🔳 |
update | 🚀 Fastest | 52.35% slower | 🔳 |
view | 🚀 Fastest | 76.15% slower | 🔳 |
Walmart Canada reported by w-b-dev
It adds a
and b
.
:boom: It doesn't work with strings, as the inputs are parsed to numbers before calculation.
Try this R.add example in Rambda REPL
Try this R.addIndex example in Rambda REPL
Same as R.addIndex
, but it will passed indexes are decreasing, instead of increasing.
adjust<T>(index: number, replaceFn: (x: T) => T, list: T[]): T[]
It replaces index
in array list
with the result of replaceFn(list[i])
.
const result = R.adjust(
0,
a => a + 1,
[0, 100]
) // => [1, 100]
Try this R.adjust example in Rambda REPL
import { cloneList } from './_internals/cloneList.js'
import { curry } from './curry.js'
function adjustFn(
index, replaceFn, list
){
const actualIndex = index < 0 ? list.length + index : index
if (index >= list.length || actualIndex < 0) return list
const clone = cloneList(list)
clone[ actualIndex ] = replaceFn(clone[ actualIndex ])
return clone
}
export const adjust = curry(adjustFn)
import { add } from './add.js'
import { adjust } from './adjust.js'
import { pipe } from './pipe.js'
const list = [ 0, 1, 2 ]
const expected = [ 0, 11, 2 ]
test('happy', () => {})
test('happy', () => {
expect(adjust(
1, add(10), list
)).toEqual(expected)
})
test('with curring type 1 1 1', () => {
expect(adjust(1)(add(10))(list)).toEqual(expected)
})
test('with curring type 1 2', () => {
expect(adjust(1)(add(10), list)).toEqual(expected)
})
test('with curring type 2 1', () => {
expect(adjust(1, add(10))(list)).toEqual(expected)
})
test('with negative index', () => {
expect(adjust(
-2, add(10), list
)).toEqual(expected)
})
test('when index is out of bounds', () => {
const list = [ 0, 1, 2, 3 ]
expect(adjust(
4, add(1), list
)).toEqual(list)
expect(adjust(
-5, add(1), list
)).toEqual(list)
})
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.all(predicate, list)
// => true
Try this R.all example in Rambda REPL
export function all(predicate, list){
if (arguments.length === 1) return _list => all(predicate, _list)
for (let i = 0; i < list.length; i++){
if (!predicate(list[ i ])) return false
}
return true
}
import { all } from './all.js'
const list = [ 0, 1, 2, 3, 4 ]
test('when true', () => {
const fn = x => x > -1
expect(all(fn)(list)).toBeTrue()
})
test('when false', () => {
const fn = x => x > 2
expect(all(fn, list)).toBeFalse()
})
allFalse(...inputs: any[]): boolean
It returns true
if all inputs
arguments are falsy(empty objects and empty arrays are considered falsy).
Functions are valid inputs, but these functions cannot have their own arguments.
This method is very similar to R.anyFalse
, R.anyTrue
and R.allTrue
R.allFalse(0, null, [], {}, '', () => false)
// => true
Try this R.allFalse example in Rambda REPL
import { isTruthy } from './_internals/isTruthy.js'
import { type } from './type.js'
export function allFalse(...inputs){
let counter = 0
while (counter < inputs.length){
const x = inputs[ counter ]
if (type(x) === 'Function'){
if (isTruthy(x())){
return false
}
} else if (isTruthy(x)){
return false
}
counter++
}
return true
}
import { runTests } from 'helpers-fn'
import { allFalse } from './allFalse.js'
const happy = { ok : [ () => false, () => [], () => {}, null, false, [] ] }
const withArray = { fail : [ ...happy.ok, [ 1 ] ] }
const withObject = { fail : [ ...happy.ok, { a : 1 } ] }
const withFunction = { fail : [ ...happy.ok, () => ({ a : 1 }) ] }
const withBoolean = { fail : [ ...happy.ok, true ] }
const testData = {
label : 'R.allFalse',
data : [ happy, withArray, withObject, withFunction, withBoolean ],
fn : input => allFalse(...input),
}
runTests(testData)
allPass<T>(predicates: ((x: T) => boolean)[]): (input: T) => boolean
It returns true
, if all functions of predicates
return true
, when input
is their argument.
const input = {
a : 1,
b : 2,
}
const predicates = [
x => x.a === 1,
x => x.b === 2,
]
const result = R.allPass(predicates)(input) // => true
Try this R.allPass example in Rambda REPL
export function allPass(predicates){
return (...input) => {
let counter = 0
while (counter < predicates.length){
if (!predicates[ counter ](...input)){
return false
}
counter++
}
return true
}
}
import { allPass } from './allPass.js'
test('happy', () => {
const rules = [ x => typeof x === 'number', x => x > 10, x => x * 7 < 100 ]
expect(allPass(rules)(11)).toBeTrue()
expect(allPass(rules)(undefined)).toBeFalse()
})
test('when returns true', () => {
const conditionArr = [ val => val.a === 1, val => val.b === 2 ]
expect(allPass(conditionArr)({
a : 1,
b : 2,
})).toBeTrue()
})
test('when returns false', () => {
const conditionArr = [ val => val.a === 1, val => val.b === 3 ]
expect(allPass(conditionArr)({
a : 1,
b : 2,
})).toBeFalse()
})
test('works with multiple inputs', () => {
const fn = function (
w, x, y, z
){
return w + x === y + z
}
expect(allPass([ fn ])(
3, 3, 3, 3
)).toBeTrue()
})
allTrue(...input: any[]): boolean
It returns true
if all inputs
arguments are truthy(empty objects and empty arrays are considered falsy).
R.allTrue(1, true, {a: 1}, [1], 'foo', () => true)
// => true
Try this R.allTrue example in Rambda REPL
import { isFalsy } from './_internals/isFalsy.js'
import { type } from './type.js'
export function allTrue(...inputs){
let counter = 0
while (counter < inputs.length){
const x = inputs[ counter ]
if (type(x) === 'Function'){
if (isFalsy(x())){
return false
}
} else if (isFalsy(x)){
return false
}
counter++
}
return true
}
import { allTrue } from './allTrue.js'
test('with functions', () => {
const foo = () => 1
const bar = () => false
const baz = () => JSON.parse('{sda')
const result = allTrue(
foo, bar, baz
)
expect(result).toBeFalse()
})
test('usage with non boolean', () => {
const foo = { a : 1 }
const baz = [ 1, 2, 3 ]
const result = allTrue(
foo, foo, baz
)
expect(result).toBeTrue()
})
test('usage with boolean', () => {
const foo = 4
const baz = [ 1, 2, 3 ]
const result = allTrue(foo > 2, baz.length === 3)
expect(result).toBeTrue()
})
test('escapes early - case 0', () => {
const foo = undefined
const result = allTrue(foo, () => foo.a)
expect(result).toBeFalse()
})
test('escapes early - case 1', () => {
const foo = null
const result = allTrue(foo, () => foo.a)
expect(result).toBeFalse()
})
test('escapes early - case 2', () => {
const foo = { a : 'bar' }
const result = allTrue(
foo, foo.a, foo.a.b
)
expect(result).toBeFalse()
})
test('escapes early - case 3', () => {
const foo = { a : { b : 'foo' } }
const result = allTrue(
foo,
() => foo.a,
() => foo.a.b
)
expect(result).toBeTrue()
})
allType(targetType: RambdaTypes): (...input: any[]) => boolean
It returns a function which will return true
if all of its inputs
arguments belong to targetType
.
:boom:
targetType
is one of the possible returns ofR.type
const targetType = 'String'
const result = R.allType(
targetType
)('foo', 'bar', 'baz')
// => true
Try this R.allType example in Rambda REPL
import { type } from './type.js'
export function allType(targetType){
return (...inputs) => {
let counter = 0
while (counter < inputs.length){
if (type(inputs[ counter ]) !== targetType){
return false
}
counter++
}
return true
}
}
import { allType } from './allType.js'
test('when true', () => {
const result = allType('Array')(
[ 1, 2, 3 ], [], [ null ]
)
expect(result).toBeTrue()
})
test('when false', () => {
const result = allType('String')(
1, undefined, null, []
)
expect(result).toBeFalse()
})
It returns function that always returns x
.
Try this R.always example in Rambda REPL
Logical AND
Try this R.and example in Rambda REPL
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(fn, list)
// => true
Try this R.any example in Rambda REPL
export function any(predicate, list){
if (arguments.length === 1) return _list => any(predicate, _list)
let counter = 0
while (counter < list.length){
if (predicate(list[ counter ], counter)){
return true
}
counter++
}
return false
}
import { any } from './any.js'
const list = [ 1, 2, 3 ]
test('happy', () => {
expect(any(x => x < 0, list)).toBeFalse()
})
test('with curry', () => {
expect(any(x => x > 2)(list)).toBeTrue()
})
anyFalse(...input: any[]): boolean
It returns true
if any of inputs
is falsy(empty objects and empty arrays are considered falsy).
R.anyFalse(1, {a: 1}, [1], () => false)
// => true
Try this R.anyFalse example in Rambda REPL
import { isFalsy } from './_internals/isFalsy.js'
import { type } from './type.js'
export function anyFalse(...inputs){
let counter = 0
while (counter < inputs.length){
const x = inputs[ counter ]
if (type(x) === 'Function'){
if (isFalsy(x())){
return true
}
} else if (isFalsy(x)){
return true
}
counter++
}
return false
}
import { anyFalse } from './anyFalse.js'
test('when true', () => {
expect(anyFalse(
true, true, false
)).toBeTruthy()
})
test('when false', () => {
expect(anyFalse(true, true)).toBeFalsy()
})
test('supports function', () => {
expect(anyFalse(
true,
() => true,
() => false
)).toBeTruthy()
})
anyPass<T>(predicates: ((x: T) => boolean)[]): (input: T) => boolean
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
export function anyPass(predicates){
return (...input) => {
let counter = 0
while (counter < predicates.length){
if (predicates[ counter ](...input)){
return true
}
counter++
}
return false
}
}
import { anyPass } from './anyPass.js'
test('happy', () => {
const rules = [ x => typeof x === 'string', x => x > 10 ]
const predicate = anyPass(rules)
expect(predicate('foo')).toBeTrue()
expect(predicate(6)).toBeFalse()
})
test('happy', () => {
const rules = [ x => typeof x === 'string', x => x > 10 ]
expect(anyPass(rules)(11)).toBeTrue()
expect(anyPass(rules)(undefined)).toBeFalse()
})
const obj = {
a : 1,
b : 2,
}
test('when returns true', () => {
const conditionArr = [ val => val.a === 1, val => val.a === 2 ]
expect(anyPass(conditionArr)(obj)).toBeTrue()
})
test('when returns false + curry', () => {
const conditionArr = [ val => val.a === 2, val => val.b === 3 ]
expect(anyPass(conditionArr)(obj)).toBeFalse()
})
test('with empty predicates list', () => {
expect(anyPass([])(3)).toBeFalse()
})
test('works with multiple inputs', () => {
const fn = function (
w, x, y, z
){
console.log(
w, x, y, z
)
return w + x === y + z
}
expect(anyPass([ fn ])(
3, 3, 3, 3
)).toBeTrue()
})
anyTrue(...input: any[]): boolean
It returns true
if any of inputs
arguments are truthy(empty objects and empty arrays are considered falsy).
R.anyTrue(0, null, [], {}, '', () => true)
// => true
Try this R.anyTrue example in Rambda REPL
import { isTruthy } from './_internals/isTruthy.js'
import { type } from './type.js'
export function anyTrue(...inputs){
let counter = 0
while (counter < inputs.length){
const x = inputs[ counter ]
if (type(x) === 'Function'){
if (isTruthy(x())){
return true
}
} else if (isTruthy(x)){
return true
}
counter++
}
return false
}
import { anyTrue } from './anyTrue.js'
test('when true', () => {
expect(anyTrue(
true, true, false
)).toBeTruthy()
})
test('when false', () => {
expect(anyTrue(
false, false, false
)).toBeFalsy()
})
test('supports function', () => {
expect(anyTrue(
false,
false,
false,
() => false,
() => true
)).toBeTruthy()
})
anyType(targetType: RambdaTypes): (...input: any[]) => boolean
It returns a function which will return true
if at least one of its inputs
arguments belongs to targetType
.
targetType
is one of the possible returns of R.type
:boom:
targetType
is one of the possible returns ofR.type
const targetType = 'String'
const result = R.anyType(
targetType
)(1, {}, 'foo')
// => true
Try this R.anyType example in Rambda REPL
import { type } from './type.js'
export function anyType(targetType){
return (...inputs) => {
let counter = 0
while (counter < inputs.length){
if (type(inputs[ counter ]) === targetType){
return true
}
counter++
}
return false
}
}
import { anyType } from './anyType.js'
test('when true', () => {
const result = anyType('Array')(
1, undefined, null, []
)
expect(result).toBeTrue()
})
test('when false', () => {
const result = anyType('String')(
1, undefined, null, []
)
expect(result).toBeFalse()
})
ap<T, U>(fns: Array<(a: T) => U>[], vs: T[]): U[]
It takes a list of functions and a list of values. Then it returns a list of values obtained by applying each function to each value.
const result = R.ap(
[
x => x + 1,
x => x + 2,
],
[1, 2, 3]
)
// => [2, 3, 4, 3, 4, 5]
Try this R.ap example in Rambda REPL
export function ap(functions, input){
if (arguments.length === 1){
return _inputs => ap(functions, _inputs)
}
return functions.reduce((acc, fn) => [ ...acc, ...input.map(fn) ], [])
}
import { ap } from './ap.js'
function mult2(x){
return x * 2
}
function plus3(x){
return x + 3
}
test('happy', () => {
expect(ap([ mult2, plus3 ], [ 1, 2, 3 ])).toEqual([ 2, 4, 6, 4, 5, 6 ])
})
aperture<N extends number, T>(n: N, list: T[]): Array<Tuple<T, N>> | []
It returns a new list, composed of consecutive n
-tuples from a list
.
const result = R.aperture(2, [1, 2, 3, 4])
// => [[1, 2], [2, 3], [3, 4]]
Try this R.aperture example in Rambda REPL
export function aperture(step, list){
if (arguments.length === 1){
return _list => aperture(step, _list)
}
if (step > list.length) return []
let idx = 0
const limit = list.length - (step - 1)
const acc = new Array(limit)
while (idx < limit){
acc[ idx ] = list.slice(idx, idx + step)
idx += 1
}
return acc
}
import { aperture } from './aperture.js'
const list = [ 1, 2, 3, 4, 5, 6, 7 ]
test('happy', () => {
expect(aperture(1, list)).toEqual([ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ] ])
expect(aperture(2, list)).toEqual([
[ 1, 2 ],
[ 2, 3 ],
[ 3, 4 ],
[ 4, 5 ],
[ 5, 6 ],
[ 6, 7 ],
])
expect(aperture(3, list)).toEqual([
[ 1, 2, 3 ],
[ 2, 3, 4 ],
[ 3, 4, 5 ],
[ 4, 5, 6 ],
[ 5, 6, 7 ],
])
expect(aperture(8, list)).toEqual([])
})
append<T>(xToAppend: T, iterable: T[]): T[]
It adds element x
at the end of iterable
.
const x = 'foo'
const result = R.append(x, ['bar', 'baz'])
// => ['bar', 'baz', 'foo']
Try this R.append example in Rambda REPL
import { cloneList } from './_internals/cloneList.js'
export function append(x, input){
if (arguments.length === 1) return _input => append(x, _input)
if (typeof input === 'string') return input.split('').concat(x)
const clone = cloneList(input)
clone.push(x)
return clone
}
import { append } from './append.js'
test('happy', () => {
expect(append('tests', [ 'write', 'more' ])).toEqual([
'write',
'more',
'tests',
])
})
test('append to empty array', () => {
expect(append('tests')([])).toEqual([ 'tests' ])
})
test('with strings', () => {
expect(append('o', 'fo')).toEqual([ 'f', 'o', 'o' ])
})
apply<T = any>(fn: (...args: any[]) => T, args: any[]): T
It applies function fn
to the list of arguments.
This is useful for creating a fixed-arity function from a variadic function. fn
should be a bound function if context is significant.
const result = R.apply(Math.max, [42, -Infinity, 1337])
// => 1337
Try this R.apply example in Rambda REPL
export function apply(fn, args){
if (arguments.length === 1){
return _args => apply(fn, _args)
}
return fn.apply(this, args)
}
import { apply } from './apply.js'
import { bind } from './bind.js'
import { identity } from './identity.js'
test('happy', () => {
expect(apply(identity, [ 1, 2, 3 ])).toBe(1)
})
test('applies function to argument list', () => {
expect(apply(Math.max, [ 1, 2, 3, -99, 42, 6, 7 ])).toBe(42)
})
test('provides no way to specify context', () => {
const obj = {
method (){
return this === obj
},
}
expect(apply(obj.method, [])).toBeFalse()
expect(apply(bind(obj.method, obj), [])).toBeTrue()
})
applyDiff<Output>(rules: ApplyDiffRule[], obj: object): Output
It changes paths in an object according to a list of operations. Valid operations are add
, update
and delete
. Its use-case is while writing tests and you need to change the test data.
Note, that you cannot use update
operation, if the object path is missing in the input object.
Also, you cannot use add
operation, if the object path has a value.
const obj = {a: {b:1, c:2}}
const rules = [
{op: 'remove', path: 'a.c'},
{op: 'add', path: 'a.d', value: 4},
{op: 'update', path: 'a.b', value: 2},
]
const result = R.applyDiff(rules, Record<string, unknown>)
const expected = {a: {b: 2, d: 4}}
// => `result` is equal to `expected`
Try this R.applyDiff example in Rambda REPL
import { createPath } from './_internals/createPath.js'
import { assocPathFn } from './assocPath.js'
import { path as pathModule } from './path.js'
const ALLOWED_OPERATIONS = [ 'remove', 'add', 'update' ]
export function removeAtPath(path, obj){
const p = createPath(path)
const len = p.length
if (len === 0) return
if (len === 1) return delete obj[ p[ 0 ] ]
if (len === 2) return delete obj[ p[ 0 ] ][ p[ 1 ] ]
if (len === 3) return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ]
if (len === 4) return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ][ p[ 3 ] ]
if (len === 5) return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ][ p[ 3 ] ][ p[ 4 ] ]
if (len === 6)
return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ][ p[ 3 ] ][ p[ 4 ] ][ p[ 5 ] ]
if (len === 7)
return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ][ p[ 3 ] ][ p[ 4 ] ][ p[ 5 ] ][ p[ 6 ] ]
if (len === 8)
return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ][ p[ 3 ] ][ p[ 4 ] ][ p[ 5 ] ][ p[ 6 ] ][ p[ 7 ] ]
if (len === 9)
return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ][ p[ 3 ] ][ p[ 4 ] ][ p[ 5 ] ][ p[ 6 ] ][ p[ 7 ] ][ p[ 8 ] ]
if (len === 10)
return delete obj[ p[ 0 ] ][ p[ 1 ] ][ p[ 2 ] ][ p[ 3 ] ][ p[ 4 ] ][ p[ 5 ] ][ p[ 6 ] ][ p[ 7 ] ][ p[ 8 ] ][
p[ 9 ]
]
}
export function applyDiff(rules, obj){
if (arguments.length === 1) return _obj => applyDiff(rules, _obj)
let clone = { ...obj }
rules.forEach(({ op, path, value }) => {
if (!ALLOWED_OPERATIONS.includes(op)) return
if (op === 'add' && path && value !== undefined){
if (pathModule(path, obj)) return
clone = assocPathFn(
path, value, clone
)
return
}
if (op === 'remove'){
if (pathModule(path, obj) === undefined) return
removeAtPath(path, clone)
return
}
if (op === 'update' && path && value !== undefined){
if (pathModule(path, obj) === undefined) return
clone = assocPathFn(
path, value, clone
)
}
})
return clone
}
import { applyDiff } from './applyDiff.js'
test('remove operation', () => {
const rules = [
{
op : 'remove',
path : 'a.b',
},
]
const result = applyDiff(rules, {
a : {
b : 1,
c : 2,
},
})
expect(result).toEqual({ a : { c : 2 } })
})
test('update operation', () => {
const rules = [
{
op : 'update',
path : 'a.b',
value : 3,
},
{
op : 'update',
path : 'a.c.1',
value : 3,
},
{
op : 'update',
path : 'a.d',
value : 3,
},
]
expect(applyDiff(rules, {
a : {
b : 1,
c : [ 1, 2 ],
},
})).toEqual({
a : {
b : 3,
c : [ 1, 3 ],
},
})
})
test('add operation', () => {
const rules = [
{
op : 'add',
path : 'a.b',
value : 3,
},
{
op : 'add',
path : 'a.d',
value : 3,
},
]
const result = applyDiff(rules, {
a : {
b : 1,
c : 2,
},
})
expect(result).toEqual({
a : {
b : 1,
c : 2,
d : 3,
},
})
})
applySpec<Spec extends Record<string, AnyFunction>>(
spec: Spec
): (
...args: Parameters<ValueOfRecord<Spec>>
) => { [Key in keyof Spec]: ReturnType<Spec[Key]> }
:boom: The currying in this function works best with functions with 4 arguments or less. (arity of 4)
const fn = R.applySpec({
sum: R.add,
nested: { mul: R.multiply }
})
const result = fn(2, 4)
// => { sum: 6, nested: { mul: 8 } }
Try this R.applySpec example in Rambda REPL
import { isArray } from './_internals/isArray.js'
// recursively traverse the given spec object to find the highest arity function
export function __findHighestArity(spec, max = 0){
for (const key in spec){
if (spec.hasOwnProperty(key) === false || key === 'constructor') continue
if (typeof spec[ key ] === 'object'){
max = Math.max(max, __findHighestArity(spec[ key ]))
}
if (typeof spec[ key ] === 'function'){
max = Math.max(max, spec[ key ].length)
}
}
return max
}
function __filterUndefined(){
const defined = []
let i = 0
const l = arguments.length
while (i < l){
if (typeof arguments[ i ] === 'undefined') break
defined[ i ] = arguments[ i ]
i++
}
return defined
}
function __applySpecWithArity(
spec, arity, cache
){
const remaining = arity - cache.length
if (remaining === 1)
return x =>
__applySpecWithArity(
spec, arity, __filterUndefined(...cache, x)
)
if (remaining === 2)
return (x, y) =>
__applySpecWithArity(
spec, arity, __filterUndefined(
...cache, x, y
)
)
if (remaining === 3)
return (
x, y, z
) =>
__applySpecWithArity(
spec, arity, __filterUndefined(
...cache, x, y, z
)
)
if (remaining === 4)
return (
x, y, z, a
) =>
__applySpecWithArity(
spec,
arity,
__filterUndefined(
...cache, x, y, z, a
)
)
if (remaining > 4)
return (...args) =>
__applySpecWithArity(
spec, arity, __filterUndefined(...cache, ...args)
)
// handle spec as Array
if (isArray(spec)){
const ret = []
let i = 0
const l = spec.length
for (; i < l; i++){
// handle recursive spec inside array
if (typeof spec[ i ] === 'object' || isArray(spec[ i ])){
ret[ i ] = __applySpecWithArity(
spec[ i ], arity, cache
)
}
// apply spec to the key
if (typeof spec[ i ] === 'function'){
ret[ i ] = spec[ i ](...cache)
}
}
return ret
}
// handle spec as Object
const ret = {}
// apply callbacks to each property in the spec object
for (const key in spec){
if (spec.hasOwnProperty(key) === false || key === 'constructor') continue
// apply the spec recursively
if (typeof spec[ key ] === 'object'){
ret[ key ] = __applySpecWithArity(
spec[ key ], arity, cache
)
continue
}
// apply spec to the key
if (typeof spec[ key ] === 'function'){
ret[ key ] = spec[ key ](...cache)
}
}
return ret
}
export function applySpec(spec, ...args){
// get the highest arity spec function, cache the result and pass to __applySpecWithArity
const arity = __findHighestArity(spec)
if (arity === 0){
return () => ({})
}
const toReturn = __applySpecWithArity(
spec, arity, args
)
return toReturn
}
import { applySpec as applySpecRamda, nAry } from 'ramda'
import {
add,
always,
compose,
dec,
inc,
map,
path,
prop,
T,
} from '../rambda.js'
import { applySpec } from './applySpec.js'
test('different than Ramda when bad spec', () => {
const result = applySpec({ sum : { a : 1 } })(1, 2)
const ramdaResult = applySpecRamda({ sum : { a : 1 } })(1, 2)
expect(result).toEqual({})
expect(ramdaResult).toEqual({ sum : { a : {} } })
})
test('works with empty spec', () => {
expect(applySpec({})()).toEqual({})
expect(applySpec([])(1, 2)).toEqual({})
expect(applySpec(null)(1, 2)).toEqual({})
})
test('works with unary functions', () => {
const result = applySpec({
v : inc,
u : dec,
})(1)
const expected = {
v : 2,
u : 0,
}
expect(result).toEqual(expected)
})
test('works with binary functions', () => {
const result = applySpec({ sum : add })(1, 2)
expect(result).toEqual({ sum : 3 })
})
test('works with nested specs', () => {
const result = applySpec({
unnested : always(0),
nested : { sum : add },
})(1, 2)
const expected = {
unnested : 0,
nested : { sum : 3 },
}
expect(result).toEqual(expected)
})
test('works with arrays of nested specs', () => {
const result = applySpec({
unnested : always(0),
nested : [ { sum : add } ],
})(1, 2)
expect(result).toEqual({
unnested : 0,
nested : [ { sum : 3 } ],
})
})
test('works with arrays of spec objects', () => {
const result = applySpec([ { sum : add } ])(1, 2)
expect(result).toEqual([ { sum : 3 } ])
})
test('works with arrays of functions', () => {
const result = applySpec([ map(prop('a')), map(prop('b')) ])([
{
a : 'a1',
b : 'b1',
},
{
a : 'a2',
b : 'b2',
},
])
const expected = [
[ 'a1', 'a2' ],
[ 'b1', 'b2' ],
]
expect(result).toEqual(expected)
})
test('works with a spec defining a map key', () => {
expect(applySpec({ map : prop('a') })({ a : 1 })).toEqual({ map : 1 })
})
test('cannot retains the highest arity', () => {
const f = applySpec({
f1 : nAry(2, T),
f2 : nAry(5, T),
})
const fRamda = applySpecRamda({
f1 : nAry(2, T),
f2 : nAry(5, T),
})
expect(f).toHaveLength(0)
expect(fRamda).toHaveLength(5)
})
test('returns a curried function', () => {
expect(applySpec({ sum : add })(1)(2)).toEqual({ sum : 3 })
})
// Additional tests
// ============================================
test('arity', () => {
const spec = {
one : x1 => x1,
two : (x1, x2) => x1 + x2,
three : (
x1, x2, x3
) => x1 + x2 + x3,
}
expect(applySpec(
spec, 1, 2, 3
)).toEqual({
one : 1,
two : 3,
three : 6,
})
})
test('arity over 5 arguments', () => {
const spec = {
one : x1 => x1,
two : (x1, x2) => x1 + x2,
three : (
x1, x2, x3
) => x1 + x2 + x3,
four : (
x1, x2, x3, x4
) => x1 + x2 + x3 + x4,
five : (
x1, x2, x3, x4, x5
) => x1 + x2 + x3 + x4 + x5,
}
expect(applySpec(
spec, 1, 2, 3, 4, 5
)).toEqual({
one : 1,
two : 3,
three : 6,
four : 10,
five : 15,
})
})
test('curried', () => {
const spec = {
one : x1 => x1,
two : (x1, x2) => x1 + x2,
three : (
x1, x2, x3
) => x1 + x2 + x3,
}
expect(applySpec(spec)(1)(2)(3)).toEqual({
one : 1,
two : 3,
three : 6,
})
})
test('curried over 5 arguments', () => {
const spec = {
one : x1 => x1,
two : (x1, x2) => x1 + x2,
three : (
x1, x2, x3
) => x1 + x2 + x3,
four : (
x1, x2, x3, x4
) => x1 + x2 + x3 + x4,
five : (
x1, x2, x3, x4, x5
) => x1 + x2 + x3 + x4 + x5,
}
expect(applySpec(spec)(1)(2)(3)(4)(5)).toEqual({
one : 1,
two : 3,
three : 6,
four : 10,
five : 15,
})
})
test('undefined property', () => {
const spec = { prop : path([ 'property', 'doesnt', 'exist' ]) }
expect(applySpec(spec, {})).toEqual({ prop : undefined })
})
test('restructure json object', () => {
const spec = {
id : path('user.id'),
name : path('user.firstname'),
profile : path('user.profile'),
doesntExist : path('user.profile.doesntExist'),
info : { views : compose(inc, prop('views')) },
type : always('playa'),
}
const data = {
user : {
id : 1337,
firstname : 'john',
lastname : 'shaft',
profile : 'shaft69',
},
views : 42,
}
expect(applySpec(spec, data)).toEqual({
id : 1337,
name : 'john',
profile : 'shaft69',
doesntExist : undefined,
info : { views : 43 },
type : 'playa',
})
})
Try this R.applyTo example in Rambda REPL
Try this R.ascend example in Rambda REPL
It makes a shallow clone of obj
with setting or overriding the property prop
with newValue
.
:boom: This copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference.
Try this R.assoc example in Rambda REPL
assocPath<Output>(path: Path, newValue: any, obj: object): Output
It makes a shallow clone of obj
with setting or overriding with newValue
the property found with path
.
const path = 'b.c'
const newValue = 2
const obj = { a: 1 }
R.assocPath(path, newValue, Record<string, unknown>)
// => { a : 1, b : { c : 2 }}
Try this R.assocPath example in Rambda REPL
import { cloneList } from './_internals/cloneList.js'
import { createPath } from './_internals/createPath.js'
import { isArray } from './_internals/isArray.js'
import { isIndexInteger } from './_internals/isInteger.js'
import { assocFn } from './assoc.js'
import { curry } from './curry.js'
export function assocPathFn(
path, newValue, input
){
const pathArrValue = createPath(path)
if (pathArrValue.length === 0) return newValue
const index = pathArrValue[ 0 ]
if (pathArrValue.length > 1){
const condition =
typeof input !== 'object' ||
input === null ||
!input.hasOwnProperty(index)
const nextInput = condition ?
isIndexInteger(pathArrValue[ 1 ]) ?
[] :
{} :
input[ index ]
newValue = assocPathFn(
Array.prototype.slice.call(pathArrValue, 1),
newValue,
nextInput
)
}
if (isIndexInteger(index) && isArray(input)){
const arr = cloneList(input)
arr[ index ] = newValue
return arr
}
return assocFn(
index, newValue, input
)
}
export const assocPath = curry(assocPathFn)
import { assocPathFn } from './assocPath.js'
test.only('happy', () => {
const path = 'a.c.1'
const input = {
a : {
b : 1,
c : [ 1, 2 ],
},
}
assocPathFn(
path, 3, input
)
expect(input).toEqual({
a : {
b : 1,
c : [ 1, 2 ],
},
})
})
test('string can be used as path input', () => {
const testObj = {
a : [ { b : 1 }, { b : 2 } ],
d : 3,
}
const result1 = assocPathFn(
[ 'a', 0, 'b' ], 10, testObj
)
const result2 = assocPathFn(
'a.0.b', 10, testObj
)
const expected = {
a : [ { b : 10 }, { b : 2 } ],
d : 3,
}
expect(result1).toEqual(expected)
expect(result2).toEqual(expected)
})
test('difference with ramda - doesn\'t overwrite primitive values with keys in the path', () => {
const obj = { a : 'str' }
const result = assocPath(
[ 'a', 'b' ], 42, obj
)
expect(result).toEqual({
a : {
0 : 's',
1 : 't',
2 : 'r',
b : 42,
},
})
})
test('bug', () => {
/*
https://github.com/selfrefactor/rambda/issues/524
*/
const state = {}
const withDateLike = assocPath(
[ 'outerProp', '2020-03-10' ],
{ prop : 2 },
state
)
const withNumber = assocPath(
[ 'outerProp', '5' ], { prop : 2 }, state
)
const withDateLikeExpected = { outerProp : { '2020-03-10' : { prop : 2 } } }
const withNumberExpected = { outerProp : { 5 : { prop : 2 } } }
expect(withDateLike).toEqual(withDateLikeExpected)
expect(withNumber).toEqual(withNumberExpected)
})
test('adds a key to an empty object', () => {
expect(assocPath(
[ 'a' ], 1, {}
)).toEqual({ a : 1 })
})
test('adds a key to a non-empty object', () => {
expect(assocPath(
'b', 2, { a : 1 }
)).toEqual({
a : 1,
b : 2,
})
})
test('adds a nested key to a non-empty object', () => {
expect(assocPath(
'b.c', 2, { a : 1 }
)).toEqual({
a : 1,
b : { c : 2 },
})
})
test('adds a nested key to a nested non-empty object - curry case 1', () => {
expect(assocPath('b.d',
3)({
a : 1,
b : { c : 2 },
})).toEqual({
a : 1,
b : {
c : 2,
d : 3,
},
})
})
test('adds a key to a non-empty object - curry case 1', () => {
expect(assocPath('b', 2)({ a : 1 })).toEqual({
a : 1,
b : 2,
})
})
test('adds a nested key to a non-empty object - curry case 1', () => {
expect(assocPath('b.c', 2)({ a : 1 })).toEqual({
a : 1,
b : { c : 2 },
})
})
test('adds a key to a non-empty object - curry case 2', () => {
expect(assocPath('b')(2, { a : 1 })).toEqual({
a : 1,
b : 2,
})
})
test('adds a key to a non-empty object - curry case 3', () => {
const result = assocPath('b')(2)({ a : 1 })
expect(result).toEqual({
a : 1,
b : 2,
})
})
test('changes an existing key', () => {
expect(assocPath(
'a', 2, { a : 1 }
)).toEqual({ a : 2 })
})
test('undefined is considered an empty object', () => {
expect(assocPath(
'a', 1, undefined
)).toEqual({ a : 1 })
})
test('null is considered an empty object', () => {
expect(assocPath(
'a', 1, null
)).toEqual({ a : 1 })
})
test('value can be null', () => {
expect(assocPath(
'a', null, null
)).toEqual({ a : null })
})
test('value can be undefined', () => {
expect(assocPath(
'a', undefined, null
)).toEqual({ a : undefined })
})
test('assignment is shallow', () => {
expect(assocPath(
'a', { b : 2 }, { a : { c : 3 } }
)).toEqual({ a : { b : 2 } })
})
test('empty array as path', () => {
const result = assocPath(
[], 3, {
a : 1,
b : 2,
}
)
expect(result).toBe(3)
})
test('happy', () => {
const expected = { foo : { bar : { baz : 42 } } }
const result = assocPath(
[ 'foo', 'bar', 'baz' ], 42, { foo : null }
)
expect(result).toEqual(expected)
})
Try this R.binary example in Rambda REPL
bind<F extends AnyFunction, T>(fn: F, thisObj: T): (...args: Parameters<F>) => ReturnType<F>
Creates a function that is bound to a context.
:boom: R.bind does not provide the additional argument-binding capabilities of
Function.prototype.bind
.
const log = R.bind(console.log, console)
const result = R.pipe(
R.assoc('a', 2),
R.tap(log),
R.assoc('a', 3)
)({a: 1});
// => result - `{a: 3}`
// => console log - `{a: 2}`
Try this R.bind example in Rambda REPL
import { curryN } from './curryN.js'
export function bind(fn, thisObj){
if (arguments.length === 1){
return _thisObj => bind(fn, _thisObj)
}
return curryN(fn.length, (...args) => fn.apply(thisObj, args))
}
import { bind } from './bind.js'
function Foo(x){
this.x = x
}
function add(x){
return this.x + x
}
function Bar(x, y){
this.x = x
this.y = y
}
Bar.prototype = new Foo()
Bar.prototype.getX = function (){
return 'prototype getX'
}
test('returns a function', () => {
expect(typeof bind(add)(Foo)).toBe('function')
})
test('returns a function bound to the specified context object', () => {
const f = new Foo(12)
function isFoo(){
return this instanceof Foo
}
const isFooBound = bind(isFoo, f)
expect(isFoo()).toBeFalse()
expect(isFooBound()).toBeTrue()
})
test('works with built-in types', () => {
const abc = bind(String.prototype.toLowerCase, 'ABCDEFG')
expect(typeof abc).toBe('function')
expect(abc()).toBe('abcdefg')
})
test('works with user-defined types', () => {
const f = new Foo(12)
function getX(){
return this.x
}
const getXFooBound = bind(getX, f)
expect(getXFooBound()).toBe(12)
})
test('works with plain objects', () => {
const pojso = { x : 100 }
function incThis(){
return this.x + 1
}
const incPojso = bind(incThis, pojso)
expect(typeof incPojso).toBe('function')
expect(incPojso()).toBe(101)
})
test('does not interfere with existing object methods', () => {
const b = new Bar('a', 'b')
function getX(){
return this.x
}
const getXBarBound = bind(getX, b)
expect(b.getX()).toBe('prototype getX')
expect(getXBarBound()).toBe('a')
})
test('preserves arity', () => {
const f0 = function (){
return 0
}
const f1 = function (a){
return a
}
const f2 = function (a, b){
return a + b
}
const f3 = function (
a, b, c
){
return a + b + c
}
expect(bind(f0, {})).toHaveLength(0)
expect(bind(f1, {})).toHaveLength(1)
expect(bind(f2, {})).toHaveLength(2)
expect(bind(f3, {})).toHaveLength(3)
})
both(pred1: Pred, pred2: Pred): Pred
It returns a function with input
argument.
This function will return true
, if both firstCondition
and secondCondition
return true
when input
is passed as their argument.
const firstCondition = x => x > 10
const secondCondition = x => x < 20
const fn = R.both(firstCondition, secondCondition)
const result = [fn(15), fn(30)]
// => [true, false]
Try this R.both example in Rambda REPL
export function both(f, g){
if (arguments.length === 1) return _g => both(f, _g)
return (...input) => f(...input) && g(...input)
}
import { both } from './both.js'
const firstFn = val => val > 0
const secondFn = val => val < 10
test('with curry', () => {
expect(both(firstFn)(secondFn)(17)).toBeFalse()
})
test('without curry', () => {
expect(both(firstFn, secondFn)(7)).toBeTrue()
})
test('with multiple inputs', () => {
const between = function (
a, b, c
){
return a < b && b < c
}
const total20 = function (
a, b, c
){
return a + b + c === 20
}
const fn = both(between, total20)
expect(fn(
5, 7, 8
)).toBeTrue()
})
test('skip evaluation of the second expression', () => {
let effect = 'not evaluated'
const F = function (){
return false
}
const Z = function (){
effect = 'Z got evaluated'
}
both(F, Z)()
expect(effect).toBe('not evaluated')
})
Try this R.call example in Rambda REPL
chain<T, U>(fn: (n: T) => U[], list: T[]): U[]
The method is also known as flatMap
.
const duplicate = n => [ n, n ]
const list = [ 1, 2, 3 ]
const result = chain(duplicate, list)
// => [ 1, 1, 2, 2, 3, 3 ]
Try this R.chain example in Rambda REPL
export function chain(fn, list){
if (arguments.length === 1){
return _list => chain(fn, _list)
}
return [].concat(...list.map(fn))
}
import { chain as chainRamda } from 'ramda'
import { chain } from './chain.js'
const duplicate = n => [ n, n ]
test('happy', () => {
const fn = x => [ x * 2 ]
const list = [ 1, 2, 3 ]
const result = chain(fn, list)
expect(result).toEqual([ 2, 4, 6 ])
})
test('maps then flattens one level', () => {
expect(chain(duplicate, [ 1, 2, 3 ])).toEqual([ 1, 1, 2, 2, 3, 3 ])
})
test('maps then flattens one level - curry', () => {
expect(chain(duplicate)([ 1, 2, 3 ])).toEqual([ 1, 1, 2, 2, 3, 3 ])
})
test('flattens only one level', () => {
const nest = n => [ [ n ] ]
expect(chain(nest, [ 1, 2, 3 ])).toEqual([ [ 1 ], [ 2 ], [ 3 ] ])
})
test('can compose', () => {
function dec(x){
return [ x - 1 ]
}
function times2(x){
return [ x * 2 ]
}
const mdouble = chain(times2)
const mdec = chain(dec)
expect(mdec(mdouble([ 10, 20, 30 ]))).toEqual([ 19, 39, 59 ])
})
test('@types/ramda broken test', () => {
const score = {
maths : 90,
physics : 80,
}
const calculateTotal = score => {
const { maths, physics } = score
return maths + physics
}
const assocTotalToScore = (total, score) => ({
...score,
total,
})
const calculateAndAssocTotalToScore = chainRamda(assocTotalToScore,
calculateTotal)
expect(() =>
calculateAndAssocTotalToScore(score)).toThrowErrorMatchingInlineSnapshot('"fn(...) is not a function"')
})
Restrict a number input
to be within min
and max
limits.
If input
is bigger than max
, then the result is max
.
If input
is smaller than min
, then the result is min
.
Try this R.clamp example in Rambda REPL
It creates a deep copy of the input
, which may contain (nested) Arrays and Objects, Numbers, Strings, Booleans and Dates.
:boom: It doesn't work with very specific types, such as MongoDB's ObjectId.
Try this R.clone example in Rambda REPL
Try this R.collectBy example in Rambda REPL
It returns a comparator function that can be used in sort
method.
Try this R.comparator example in Rambda REPL
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)
.
Try this R.complement example in Rambda REPL
It performs right-to-left function composition.
Try this R.compose example in Rambda REPL
Asynchronous version of R.compose
. await
s the result of each function before passing it to the next. Returns a Promise
of the result.
Try this R.composeAsync example in Rambda REPL
Try this R.composeWith example in Rambda REPL
It returns a new string or array, which is the result of merging x
and y
.
Try this R.concat example in Rambda REPL
It takes list with conditions
and returns a new function fn
that expects input
as argument.
This function will start evaluating the conditions
in order to find the first winner(order of conditions matter).
The winner is this condition, which left side returns true
when input
is its argument. Then the evaluation of the right side of the winner will be the final result.
If no winner is found, then fn
returns undefined
.
Try this R.cond example in Rambda REPL
contains<T, U>(target: T, compareTo: U): boolean
It returns true
if all of target
object properties are R.equal
to compareTo
object.
const result = R.contains({a:1}, {a:1, b:2})
// => true
Try this R.contains example in Rambda REPL
import { equals } from './equals.js'
export function contains(target, toCompare){
if (arguments.length === 1){
return _toCompare => contains(target, _toCompare)
}
let willReturn = true
Object.keys(target).forEach(prop => {
if (!willReturn) return
if (
toCompare[ prop ] === undefined ||
!equals(target[ prop ], toCompare[ prop ])
){
willReturn = false
}
})
return willReturn
}
import { contains } from './contains.js'
const target = { a : 1 }
const compareTo = {
a : 1,
b : 2,
}
test('happy', () => {
expect(contains(target, compareTo)).toBeTrue()
})
test('curried', () => {
expect(contains({
...target,
c : 3,
},
compareTo)).toBeFalse()
})
Accepts a converging function and a list of branching functions and returns a new function. When invoked, this new function is applied to some arguments, each branching function is applied to those same arguments. The results of each branching function are passed as arguments to the converging function to produce the return value.
:boom: Explanation is taken from
Ramda
documentation
Try this R.converge example in Rambda REPL
It counts how many times predicate
function returns true
, when supplied with iteration of list
.
Try this R.count example in Rambda REPL
countBy<T extends unknown>(transformFn: (x: T) => any, list: T[]): Record<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(R.toLower, list)
const expected = { a: 2, b: 2, c: 2 }
// => `result` is equal to `expected`
Try this R.countBy example in Rambda REPL
export function countBy(fn, list){
if (arguments.length === 1){
return _list => countBy(fn, _list)
}
const willReturn = {}
list.forEach(item => {
const key = fn(item)
if (!willReturn[ key ]){
willReturn[ key ] = 1
} else {
willReturn[ key ]++
}
})
return willReturn
}
import { countBy } from './countBy.js'
const list = [ 'a', 'A', 'b', 'B', 'c', 'C' ]
test('happy', () => {
const result = countBy(x => x.toLowerCase(), list)
expect(result).toEqual({
a : 2,
b : 2,
c : 2,
})
})
It expects a function as input and returns its curried version.
Try this R.curry example in Rambda REPL
It returns a curried equivalent of the provided function, with the specified arity.
debounce<T, U>(fn: (input: T) => U, ms: number, immediate?: boolean): (input: T) => void
let counter = 0
const increment = () => {
counter++
}
const debounced = R.debounce(increment, 1000)
async function fn(){
debounced()
await R.delay(500)
debounced()
await R.delay(800)
console.log(counter) // => 0
await R.delay(1200)
console.log(counter) // => 1
return counter
}
const result = await fn()
// `result` resolves to `1`
Try this R.debounce example in Rambda REPL
export function debounce(
func, ms, immediate = false
){
let timeout
return function (...input){
const later = function (){
timeout = null
if (!immediate){
return func.apply(null, input)
}
}
const callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, ms)
if (callNow){
return func.apply(null, input)
}
}
}
import { debounce } from './debounce.js'
import { delay } from './delay.js'
test('happy', async () => {
let counter = 0
let aHolder
const inc = a => {
aHolder = a
counter++
}
const incWrapped = debounce(inc, 500)
incWrapped(1)
expect(counter).toBe(0)
await delay(200)
incWrapped(2)
expect(counter).toBe(0)
await delay(700)
expect(counter).toBe(1)
expect(aHolder).toBe(2)
})
test('immediate debounce', async () => {
let counter = 0
const inc = () => {
counter++
}
const incWrapped = debounce(
inc, 500, true
)
incWrapped()
expect(counter).toBe(1)
await delay(200)
incWrapped()
expect(counter).toBe(1)
await delay(700)
incWrapped()
expect(counter).toBe(2)
})
It decrements a number.
Try this R.dec example in Rambda REPL
defaultTo<T>(defaultValue: T, input: T | null | undefined): 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: Rambda's defaultTo accept indefinite number of arguments when non curried, i.e.
R.defaultTo(2, foo, bar, baz)
.
R.defaultTo('foo', 'bar') // => 'bar'
R.defaultTo('foo', undefined) // => 'foo'
// Important - emtpy string is not falsy value(same as Ramda)
R.defaultTo('foo', '') // => 'foo'
Try this R.defaultTo example in Rambda REPL
function isFalsy(input){
return (
input === undefined || input === null || Number.isNaN(input) === true
)
}
export function defaultTo(defaultArgument, input){
if (arguments.length === 1){
return _input => defaultTo(defaultArgument, _input)
}
return isFalsy(input) ? defaultArgument : input
}
import { defaultTo } from './defaultTo.js'
test('with undefined', () => {
expect(defaultTo('foo')(undefined)).toBe('foo')
})
test('with null', () => {
expect(defaultTo('foo')(null)).toBe('foo')
})
test('with NaN', () => {
expect(defaultTo('foo')(NaN)).toBe('foo')
})
test('with empty string', () => {
expect(defaultTo('foo', '')).toBe('')
})
test('with false', () => {
expect(defaultTo('foo', false)).toBeFalse()
})
test('when inputArgument passes initial check', () => {
expect(defaultTo('foo', 'bar')).toBe('bar')
})
delay(ms: number): Promise<'RAMBDAX_DELAY'>
setTimeout
as a promise that resolves to R.DELAY
variable after ms
milliseconds.
const result = R.delay(1000)
// `result` resolves to `RAMBDAX_DELAY`
Try this R.delay example in Rambda REPL
export const DELAY = 'RAMBDAX_DELAY'
export function delay(ms){
return new Promise(resolve => {
setTimeout(() => {
resolve(DELAY)
}, ms)
})
}
import { DELAY, delay } from './delay.js'
test('usage with variables', async () => {
await expect(delay(500)).resolves.toBe(DELAY)
})
Try this R.descend example in Rambda REPL
difference<T>(a: T[], b: T[]): T[]
It returns the uniq set of all elements in the first list a
not contained in the second list b
.
R.equals
is used to determine equality.
const a = [ 1, 2, 3, 4 ]
const b = [ 3, 4, 5, 6 ]
const result = R.difference(a, b)
// => [ 1, 2 ]
Try this R.difference example in Rambda REPL
import { includes } from './includes.js'
import { uniq } from './uniq.js'
export function difference(a, b){
if (arguments.length === 1) return _b => difference(a, _b)
return uniq(a).filter(aInstance => !includes(aInstance, b))
}
import { difference as differenceRamda } from 'ramda'
import { difference } from './difference.js'
test('difference', () => {
const a = [ 1, 2, 3, 4 ]
const b = [ 3, 4, 5, 6 ]
expect(difference(a)(b)).toEqual([ 1, 2 ])
expect(difference([], [])).toEqual([])
})
test('difference with objects', () => {
const a = [ { id : 1 }, { id : 2 }, { id : 3 }, { id : 4 } ]
const b = [ { id : 3 }, { id : 4 }, { id : 5 }, { id : 6 } ]
expect(difference(a, b)).toEqual([ { id : 1 }, { id : 2 } ])
})
test('no duplicates in first list', () => {
const M2 = [ 1, 2, 3, 4, 1, 2, 3, 4 ]
const N2 = [ 3, 3, 4, 4, 5, 5, 6, 6 ]
expect(difference(M2, N2)).toEqual([ 1, 2 ])
})
test('should use R.equals', () => {
expect(difference([ 1 ], [ 1 ])).toHaveLength(0)
expect(differenceRamda([ NaN ], [ NaN ])).toHaveLength(0)
})
differenceWith<T1, T2>(
pred: (a: T1, b: T2) => boolean,
list1: T1[],
list2: T2[],
): T1[]
const result = R.differenceWith(
(a, b) => a.x === b.x,
[{x: 1}, {x: 2}],
[{x: 1}, {x: 3}]
)
// => [{x: 2}]
Try this R.differenceWith example in Rambda REPL
import { curry } from './curry.js'
import { _indexOf } from './equals.js'
export function differenceWithFn(
fn, a, b
){
const willReturn = []
const [ first, second ] = a.length > b.length ? [ a, b ] : [ b, a ]
first.forEach(item => {
const hasItem = second.some(secondItem => fn(item, secondItem))
if (!hasItem && _indexOf(item, willReturn) === -1){
willReturn.push(item)
}
})
return willReturn
}
export const differenceWith = curry(differenceWithFn)
import { differenceWith } from './differenceWith.js'
test('happy', () => {
const foo = [ { a : 1 }, { a : 2 }, { a : 3 } ]
const bar = [ { a : 3 }, { a : 4 } ]
const fn = function (r, s){
return r.a === s.a
}
const result = differenceWith(
fn, foo, bar
)
expect(result).toEqual([ { a : 1 }, { a : 2 } ])
})
It returns a new object that does not contain property prop
.
Try this R.dissoc example in Rambda REPL
Try this R.dissocPath example in Rambda REPL
Try this R.divide example in Rambda REPL
drop<T>(howMany: number, input: T[]): T[]
It returns howMany
items dropped from beginning of list or string input
.
R.drop(2, ['foo', 'bar', 'baz']) // => ['baz']
R.drop(2, 'foobar') // => 'obar'
Try this R.drop example in Rambda REPL
export function drop(howManyToDrop, listOrString){
if (arguments.length === 1) return _list => drop(howManyToDrop, _list)
return listOrString.slice(howManyToDrop > 0 ? howManyToDrop : 0)
}
import assert from 'assert'
import { drop } from './drop.js'
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 string', () => {
expect(drop(3, 'rambda')).toBe('bda')
})
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(-Infinity, [ 1, 2, 3 ])).toEqual([ 1, 2, 3 ])
})
test('should return copy', () => {
const xs = [ 1, 2, 3 ]
assert.notStrictEqual(drop(0, xs), xs)
assert.notStrictEqual(drop(-1, xs), xs)
})
dropLast<T>(howMany: number, input: T[]): T[]
It returns howMany
items dropped from the end of list or string input
.
R.dropLast(2, ['foo', 'bar', 'baz']) // => ['foo']
R.dropLast(2, 'foobar') // => 'foob'
Try this R.dropLast example in Rambda REPL
export function dropLast(howManyToDrop, listOrString){
if (arguments.length === 1){
return _listOrString => dropLast(howManyToDrop, _listOrString)
}
return howManyToDrop > 0 ?
listOrString.slice(0, -howManyToDrop) :
listOrString.slice()
}
import assert from 'assert'
import { dropLast } from './dropLast.js'
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 string', () => {
expect(dropLast(3, 'rambda')).toBe('ram')
})
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(-Infinity, [ 1, 2, 3 ])).toEqual([ 1, 2, 3 ])
})
test('should return copy', () => {
const xs = [ 1, 2, 3 ]
assert.notStrictEqual(dropLast(0, xs), xs)
assert.notStrictEqual(dropLast(-1, xs), xs)
})
Try this R.dropLastWhile example in Rambda REPL
dropRepeats<T>(list: T[]): T[]
It removes any successive duplicates according to R.equals
.
const result = R.dropRepeats([
1,
1,
{a: 1},
{a:1},
1
])
// => [1, {a: 1}, 1]
Try this R.dropRepeats example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { equals } from './equals.js'
export function dropRepeats(list){
if (!isArray(list)){
throw new Error(`${ list } is not a list`)
}
const toReturn = []
list.reduce((prev, current) => {
if (!equals(prev, current)){
toReturn.push(current)
}
return current
}, undefined)
return toReturn
}
import { dropRepeats as dropRepeatsRamda } from 'ramda'
import { compareCombinations } from './_internals/testUtils.js'
import { add } from './add.js'
import { dropRepeats } from './dropRepeats.js'
const list = [ 1, 2, 2, 2, 3, 4, 4, 5, 5, 3, 2, 2, { a : 1 }, { a : 1 } ]
const listClean = [ 1, 2, 3, 4, 5, 3, 2, { a : 1 } ]
test('happy', () => {
const result = dropRepeats(list)
expect(result).toEqual(listClean)
})
const possibleLists = [
[ add(1), async () => {}, [ 1 ], [ 1 ], [ 2 ], [ 2 ] ],
[ add(1), add(1), add(2) ],
[],
1,
/foo/g,
Promise.resolve(1),
]
describe('brute force', () => {
compareCombinations({
firstInput : possibleLists,
callback : errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
{
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 0,
"RESULTS_MISMATCH": 1,
"SHOULD_NOT_THROW": 3,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 6,
}
`)
},
fn : dropRepeats,
fnRamda : dropRepeatsRamda,
})
})
Try this R.dropRepeatsBy example in Rambda REPL
Try this R.dropRepeatsWith example in Rambda REPL
Try this R.dropWhile example in Rambda REPL
either(firstPredicate: Pred, secondPredicate: Pred): Pred
It returns a new predicate
function from firstPredicate
and secondPredicate
inputs.
This predicate
function will return true
, if any of the two input predicates return true
.
const firstPredicate = x => x > 10
const secondPredicate = x => x % 2 === 0
const predicate = R.either(firstPredicate, secondPredicate)
const result = [
predicate(15),
predicate(8),
predicate(7),
]
// => [true, true, false]
Try this R.either example in Rambda REPL
export function either(firstPredicate, secondPredicate){
if (arguments.length === 1){
return _secondPredicate => either(firstPredicate, _secondPredicate)
}
return (...input) =>
Boolean(firstPredicate(...input) || secondPredicate(...input))
}
import { either } from './either.js'
test('with multiple inputs', () => {
const between = function (
a, b, c
){
return a < b && b < c
}
const total20 = function (
a, b, c
){
return a + b + c === 20
}
const fn = either(between, total20)
expect(fn(
7, 8, 5
)).toBeTrue()
})
test('skip evaluation of the second expression', () => {
let effect = 'not evaluated'
const F = function (){
return true
}
const Z = function (){
effect = 'Z got evaluated'
}
either(F, Z)()
expect(effect).toBe('not evaluated')
})
test('case 1', () => {
const firstFn = val => val > 0
const secondFn = val => val * 5 > 10
expect(either(firstFn, secondFn)(1)).toBeTrue()
})
test('case 2', () => {
const firstFn = val => val > 0
const secondFn = val => val === -10
const fn = either(firstFn)(secondFn)
expect(fn(-10)).toBeTrue()
})
Try this R.empty example in Rambda REPL
endsWith<T extends string>(question: T, str: string): boolean
When iterable is a string, then it behaves as String.prototype.endsWith
.
When iterable is a list, then it uses R.equals to determine if the target list ends in the same way as the given target.
const str = 'foo-bar'
const list = [{a:1}, {a:2}, {a:3}]
const result = [
R.endsWith('bar', str),
R.endsWith([{a:1}, {a:2}], list)
]
// => [true, true]
Try this R.endsWith example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { equals } from './equals.js'
export function endsWith(target, iterable){
if (arguments.length === 1) return _iterable => endsWith(target, _iterable)
if (typeof iterable === 'string'){
return iterable.endsWith(target)
}
if (!isArray(target)) return false
const diff = iterable.length - target.length
let correct = true
const filtered = target.filter((x, index) => {
if (!correct) return false
const result = equals(x, iterable[ index + diff ])
if (!result) correct = false
return result
})
return filtered.length === target.length
}
import { endsWith as endsWithRamda } from 'ramda'
import { compareCombinations } from './_internals/testUtils.js'
import { endsWith } from './endsWith.js'
test('with string', () => {
expect(endsWith('bar', 'foo-bar')).toBeTrue()
expect(endsWith('baz')('foo-bar')).toBeFalse()
})
test('use R.equals with array', () => {
const list = [ { a : 1 }, { a : 2 }, { a : 3 } ]
expect(endsWith({ a : 3 }, list)).toBeFalse(),
expect(endsWith([ { a : 3 } ], list)).toBeTrue()
expect(endsWith([ { a : 2 }, { a : 3 } ], list)).toBeTrue()
expect(endsWith(list, list)).toBeTrue()
expect(endsWith([ { a : 1 } ], list)).toBeFalse()
})
export const possibleTargets = [
NaN,
[ NaN ],
/foo/,
[ /foo/ ],
Promise.resolve(1),
[ Promise.resolve(1) ],
Error('foo'),
[ Error('foo') ],
]
export const possibleIterables = [
[ Promise.resolve(1), Promise.resolve(2) ],
[ /foo/, /bar/ ],
[ NaN ],
[ Error('foo'), Error('bar') ],
]
describe('brute force', () => {
compareCombinations({
fn : endsWith,
fnRamda : endsWithRamda,
firstInput : possibleTargets,
secondInput : possibleIterables,
callback : errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
{
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 0,
"RESULTS_MISMATCH": 0,
"SHOULD_NOT_THROW": 0,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 32,
}
`)
},
})
})
Try this R.eqBy example in Rambda REPL
It returns true
if property prop
in obj1
is equal to property prop
in obj2
according to R.equals
.
Try this R.eqProps example in Rambda REPL
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
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 (equals(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 (equals(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 equals(a, b){
if (arguments.length === 1) return _b => equals(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 ] &&
!equals(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
else if (bRegex[ 0 ]) return false
const aDate = parseDate(a)
const bDate = parseDate(b)
if (aDate[ 0 ])
return bDate[ 0 ] ? aDate[ 1 ] === bDate[ 1 ] : false
else 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 && !equals(aValue, bValue))
loopObjectFlag = false
}
})
return loopObjectFlag
}
return false
}
import { equals as equalsRamda } from 'ramda'
import { compareCombinations } from './_internals/testUtils.js'
import { variousTypes } from './benchmarks/_utils.js'
import { equals } from './equals.js'
test('compare functions', () => {
function foo(){}
function bar(){}
const baz = () => {}
const expectTrue = equals(foo, foo)
const expectFalseFirst = equals(foo, bar)
const expectFalseSecond = equals(foo, baz)
expect(expectTrue).toBeTrue()
expect(expectFalseFirst).toBeFalse()
expect(expectFalseSecond).toBeFalse()
})
test('with array of objects', () => {
const list1 = [ { a : 1 }, [ { b : 2 } ] ]
const list2 = [ { a : 1 }, [ { b : 2 } ] ]
const list3 = [ { a : 1 }, [ { b : 3 } ] ]
expect(equals(list1, list2)).toBeTrue()
expect(equals(list1, list3)).toBeFalse()
})
test('with regex', () => {
expect(equals(/s/, /s/)).toBeTrue()
expect(equals(/s/, /d/)).toBeFalse()
expect(equals(/a/gi, /a/gi)).toBeTrue()
expect(equals(/a/gim, /a/gim)).toBeTrue()
expect(equals(/a/gi, /a/i)).toBeFalse()
})
test('not a number', () => {
expect(equals([ NaN ], [ NaN ])).toBeTrue()
})
test('new number', () => {
expect(equals(new Number(0), new Number(0))).toBeTrue()
expect(equals(new Number(0), new Number(1))).toBeFalse()
expect(equals(new Number(1), new Number(0))).toBeFalse()
})
test('new string', () => {
expect(equals(new String(''), new String(''))).toBeTrue()
expect(equals(new String(''), new String('x'))).toBeFalse()
expect(equals(new String('x'), new String(''))).toBeFalse()
expect(equals(new String('foo'), new String('foo'))).toBeTrue()
expect(equals(new String('foo'), new String('bar'))).toBeFalse()
expect(equals(new String('bar'), new String('foo'))).toBeFalse()
})
test('new Boolean', () => {
expect(equals(new Boolean(true), new Boolean(true))).toBeTrue()
expect(equals(new Boolean(false), new Boolean(false))).toBeTrue()
expect(equals(new Boolean(true), new Boolean(false))).toBeFalse()
expect(equals(new Boolean(false), new Boolean(true))).toBeFalse()
})
test('new Error', () => {
expect(equals(new Error('XXX'), {})).toBeFalse()
expect(equals(new Error('XXX'), new TypeError('XXX'))).toBeFalse()
expect(equals(new Error('XXX'), new Error('YYY'))).toBeFalse()
expect(equals(new Error('XXX'), new Error('XXX'))).toBeTrue()
expect(equals(new Error('XXX'), new TypeError('YYY'))).toBeFalse()
expect(equals(new Error('XXX'), new Error('XXX'))).toBeTrue()
})
test('with dates', () => {
expect(equals(new Date(0), new Date(0))).toBeTrue()
expect(equals(new Date(1), new Date(1))).toBeTrue()
expect(equals(new Date(0), new Date(1))).toBeFalse()
expect(equals(new Date(1), new Date(0))).toBeFalse()
expect(equals(new Date(0), {})).toBeFalse()
expect(equals({}, new Date(0))).toBeFalse()
})
test('ramda spec', () => {
expect(equals({}, {})).toBeTrue()
expect(equals({
a : 1,
b : 2,
},
{
a : 1,
b : 2,
})).toBeTrue()
expect(equals({
a : 2,
b : 3,
},
{
a : 2,
b : 3,
})).toBeTrue()
expect(equals({
a : 2,
b : 3,
},
{
a : 3,
b : 3,
})).toBeFalse()
expect(equals({
a : 2,
b : 3,
c : 1,
},
{
a : 2,
b : 3,
})).toBeFalse()
})
test('works with boolean tuple', () => {
expect(equals([ true, false ], [ true, false ])).toBeTrue()
expect(equals([ true, false ], [ true, true ])).toBeFalse()
})
test('works with equal objects within array', () => {
const objFirst = {
a : {
b : 1,
c : 2,
d : [ 1 ],
},
}
const objSecond = {
a : {
b : 1,
c : 2,
d : [ 1 ],
},
}
const x = [ 1, 2, objFirst, null, '', [] ]
const y = [ 1, 2, objSecond, null, '', [] ]
expect(equals(x, y)).toBeTrue()
})
test('works with different objects within array', () => {
const objFirst = { a : { b : 1 } }
const objSecond = { a : { b : 2 } }
const x = [ 1, 2, objFirst, null, '', [] ]
const y = [ 1, 2, objSecond, null, '', [] ]
expect(equals(x, y)).toBeFalse()
})
test('works with undefined as second argument', () => {
expect(equals(1, undefined)).toBeFalse()
expect(equals(undefined, undefined)).toBeTrue()
})
test('compare sets', () => {
const toCompareDifferent = new Set([ { a : 1 }, { a : 2 } ])
const toCompareSame = new Set([ { a : 1 }, { a : 2 }, { a : 1 } ])
const testSet = new Set([ { a : 1 }, { a : 2 }, { a : 1 } ])
expect(equals(toCompareSame, testSet)).toBeTruthy()
expect(equals(toCompareDifferent, testSet)).toBeFalsy()
expect(equalsRamda(toCompareSame, testSet)).toBeTruthy()
expect(equalsRamda(toCompareDifferent, testSet)).toBeFalsy()
})
test('compare simple sets', () => {
const testSet = new Set([ '2', '3', '3', '2', '1' ])
expect(equals(new Set([ '3', '2', '1' ]), testSet)).toBeTruthy()
expect(equals(new Set([ '3', '2', '0' ]), testSet)).toBeFalsy()
})
test('various examples', () => {
expect(equals([ 1, 2, 3 ])([ 1, 2, 3 ])).toBeTrue()
expect(equals([ 1, 2, 3 ], [ 1, 2 ])).toBeFalse()
expect(equals(1, 1)).toBeTrue()
expect(equals(1, '1')).toBeFalse()
expect(equals({}, {})).toBeTrue()
expect(equals({
a : 1,
b : 2,
},
{
a : 1,
b : 2,
})).toBeTrue()
expect(equals({
a : 1,
b : 2,
},
{
a : 1,
b : 1,
})).toBeFalse()
expect(equals({
a : 1,
b : false,
},
{
a : 1,
b : 1,
})).toBeFalse()
expect(equals({
a : 1,
b : 2,
},
{
a : 1,
b : 2,
c : 3,
})).toBeFalse()
expect(equals({
x : {
a : 1,
b : 2,
},
},
{
x : {
a : 1,
b : 2,
c : 3,
},
})).toBeFalse()
expect(equals({
a : 1,
b : 2,
},
{
a : 1,
b : 3,
})).toBeFalse()
expect(equals({ a : { b : { c : 1 } } }, { a : { b : { c : 1 } } })).toBeTrue()
expect(equals({ a : { b : { c : 1 } } }, { a : { b : { c : 2 } } })).toBeFalse()
expect(equals({ a : {} }, { a : {} })).toBeTrue()
expect(equals('', '')).toBeTrue()
expect(equals('foo', 'foo')).toBeTrue()
expect(equals('foo', 'bar')).toBeFalse()
expect(equals(0, false)).toBeFalse()
expect(equals(/\s/g, null)).toBeFalse()
expect(equals(null, null)).toBeTrue()
expect(equals(false)(null)).toBeFalse()
})
test('with custom functions', () => {
function foo(){
return 1
}
foo.prototype.toString = () => ''
const result = equals(foo, foo)
expect(result).toBeTrue()
})
test('with classes', () => {
class Foo{}
const foo = new Foo()
const result = equals(foo, foo)
expect(result).toBeTrue()
})
test('with negative zero', () => {
expect(equals(-0, -0)).toBeTrue()
expect(equals(-0, 0)).toBeFalse()
expect(equals(0, 0)).toBeTrue()
expect(equals(-0, 1)).toBeFalse()
})
test('with big int', () => {
const a = BigInt(9007199254740991)
const b = BigInt(9007199254740991)
const c = BigInt(7007199254740991)
expect(equals(a, b)).toBeTrue()
expect(equals(a, c)).toBeFalse()
})
describe('brute force', () => {
compareCombinations({
callback : errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
{
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 0,
"RESULTS_MISMATCH": 0,
"SHOULD_NOT_THROW": 0,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 289,
}
`)
},
firstInput : variousTypes,
fn : equals,
fnRamda : equalsRamda,
secondInput : variousTypes,
})
})
evolve<T, U>(rules: ((x: T) => U)[], list: T[]): U[]
It takes object or array of functions as set of rules. These rules
are applied to the iterable
input to produce the result.
:boom: Error handling of this method differs between Ramda and Rambda. Ramda for some wrong inputs returns result and for other - it returns one of the inputs. Rambda simply throws when inputs are not correct. Full details for this mismatch are listed in
source/_snapshots/evolve.spec.js.snap
file.
const rules = {
foo : add(1),
bar : add(-1),
}
const input = {
a : 1,
foo : 2,
bar : 3,
}
const result = evolve(rules, input)
const expected = {
a : 1,
foo : 3,
bar : 2,
})
// => `result` is equal to `expected`
Try this R.evolve example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { mapArray, mapObject } from './map.js'
import { type } from './type.js'
export function evolveArray(rules, list){
return mapArray(
(x, i) => {
if (type(rules[ i ]) === 'Function'){
return rules[ i ](x)
}
return x
},
list,
true
)
}
export function evolveObject(rules, iterable){
return mapObject((x, prop) => {
if (type(x) === 'Object'){
const typeRule = type(rules[ prop ])
if (typeRule === 'Function'){
return rules[ prop ](x)
}
if (typeRule === 'Object'){
return evolve(rules[ prop ], x)
}
return x
}
if (type(rules[ prop ]) === 'Function'){
return rules[ prop ](x)
}
return x
}, iterable)
}
export function evolve(rules, iterable){
if (arguments.length === 1){
return _iterable => evolve(rules, _iterable)
}
const rulesType = type(rules)
const iterableType = type(iterable)
if (iterableType !== rulesType){
throw new Error('iterableType !== rulesType')
}
if (![ 'Object', 'Array' ].includes(rulesType)){
throw new Error(`'iterable' and 'rules' are from wrong type ${ rulesType }`)
}
if (iterableType === 'Object'){
return evolveObject(rules, iterable)
}
return evolveArray(rules, iterable)
}
import { evolve as evolveRamda } from 'ramda'
import { add } from '../rambda.js'
import { compareCombinations, compareToRamda } from './_internals/testUtils.js'
import { evolve } from './evolve.js'
test('happy', () => {
const rules = {
foo : add(1),
nested : { bar : x => Object.keys(x).length },
}
const input = {
a : 1,
foo : 2,
nested : { bar : { z : 3 } },
}
const result = evolve(rules, input)
expect(result).toEqual({
a : 1,
foo : 3,
nested : { bar : 1 },
})
})
test('nested rule is wrong', () => {
const rules = {
foo : add(1),
nested : { bar : 10 },
}
const input = {
a : 1,
foo : 2,
nested : { bar : { z : 3 } },
}
const result = evolve(rules)(input)
expect(result).toEqual({
a : 1,
foo : 3,
nested : { bar : { z : 3 } },
})
})
test('is recursive', () => {
const rules = {
nested : {
second : add(-1),
third : add(1),
},
}
const object = {
first : 1,
nested : {
second : 2,
third : 3,
},
}
const expected = {
first : 1,
nested : {
second : 1,
third : 4,
},
}
const result = evolve(rules, object)
expect(result).toEqual(expected)
})
test('ignores primitive values', () => {
const rules = {
n : 2,
m : 'foo',
}
const object = {
n : 0,
m : 1,
}
const expected = {
n : 0,
m : 1,
}
const result = evolve(rules, object)
expect(result).toEqual(expected)
})
test('with array', () => {
const rules = [ add(1), add(-1) ]
const list = [ 100, 1400 ]
const expected = [ 101, 1399 ]
const result = evolve(rules, list)
expect(result).toEqual(expected)
})
const rulesObject = { a : add(1) }
const rulesList = [ add(1) ]
const possibleIterables = [ null, undefined, '', 42, [], [ 1 ], { a : 1 } ]
const possibleRules = [ ...possibleIterables, rulesList, rulesObject ]
describe('brute force', () => {
compareCombinations({
firstInput : possibleRules,
callback : errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
{
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 4,
"RESULTS_MISMATCH": 0,
"SHOULD_NOT_THROW": 51,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 63,
}
`)
},
secondInput : possibleIterables,
fn : evolve,
fnRamda : evolveRamda,
})
})
Opposite of R.includes
R.equals
is used to determine equality.
Try this R.excludes example in Rambda REPL
F(): boolean
F() // => false
Try this R.F example in Rambda REPL
export function F(){
return false
}
filter<T>(predicate: Predicate<T>): (input: T[]) => T[]
It filters list or object input
using a predicate
function.
const list = [3, 4, 3, 2]
const listPredicate = x => x > 2
const object = {abc: 'fo', xyz: 'bar', baz: 'foo'}
const objectPredicate = (x, prop) => x.length + prop.length > 5
const result = [
R.filter(listPredicate, list),
R.filter(objectPredicate, object)
]
// => [ [3, 4], { xyz: 'bar', baz: 'foo'} ]
Try this R.filter example in Rambda REPL
import { isArray } from './_internals/isArray.js'
export function filterObject(predicate, obj){
const willReturn = {}
for (const prop in obj){
if (predicate(
obj[ prop ], prop, obj
)){
willReturn[ prop ] = obj[ prop ]
}
}
return willReturn
}
export function filterArray(
predicate, list, indexed = false
){
let index = 0
const len = list.length
const willReturn = []
while (index < len){
const predicateResult = indexed ?
predicate(list[ index ], index) :
predicate(list[ index ])
if (predicateResult){
willReturn.push(list[ index ])
}
index++
}
return willReturn
}
export function filter(predicate, iterable){
if (arguments.length === 1)
return _iterable => filter(predicate, _iterable)
if (!iterable){
throw new Error('Incorrect iterable input')
}
if (isArray(iterable)) return filterArray(
predicate, iterable, false
)
return filterObject(predicate, iterable)
}
import { filter as filterRamda } from 'ramda'
import { filter } from './filter.js'
import { T } from './T.js'
const sampleObject = {
a : 1,
b : 2,
c : 3,
d : 4,
}
test('happy', () => {
const isEven = n => n % 2 === 0
expect(filter(isEven, [ 1, 2, 3, 4 ])).toEqual([ 2, 4 ])
expect(filter(isEven, {
a : 1,
b : 2,
d : 3,
})).toEqual({ b : 2 })
})
test('predicate when input is object', () => {
const obj = {
a : 1,
b : 2,
}
const predicate = (
val, prop, inputObject
) => {
expect(inputObject).toEqual(obj)
expect(typeof prop).toBe('string')
return val < 2
}
expect(filter(predicate, obj)).toEqual({ a : 1 })
})
test('with object', () => {
const isEven = n => n % 2 === 0
const result = filter(isEven, sampleObject)
const expectedResult = {
b : 2,
d : 4,
}
expect(result).toEqual(expectedResult)
})
test('bad inputs difference between Ramda and Rambda', () => {
expect(() => filter(T, null)).toThrowWithMessage(Error,
'Incorrect iterable input')
expect(() => filter(T)(undefined)).toThrowWithMessage(Error,
'Incorrect iterable input')
expect(() => filterRamda(T, null)).toThrowWithMessage(TypeError,
'Cannot read properties of null (reading \'fantasy-land/filter\')')
expect(() => filterRamda(T, undefined)).toThrowWithMessage(TypeError,
'Cannot read properties of undefined (reading \'fantasy-land/filter\')')
})
filterArray<T>(predicate: Predicate<T>): (input: T[]) => T[]
const result = R.filterArray(
x => x > 1,
[1, 2, 3]
)
// => [1, 3]
Try this R.filterArray example in Rambda REPL
filterAsync<T>(fn: AsyncPredicate<T>, list: T[]): Promise<T[]>
Asynchronous version of R.filter
const predicate = async x => {
await R.delay(100)
return x % 2 === 1
}
const result = await R.filterAsync(predicate, [ 1, 2, 3 ])
// => [ 1, 3 ]
Try this R.filterAsync example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { filter } from './filter.js'
import { mapAsync } from './mapAsync.js'
export function filterAsyncFn(predicate, listOrObject){
return new Promise((resolve, reject) => {
mapAsync(predicate, listOrObject)
.then(predicateResult => {
if (isArray(predicateResult)){
const filtered = listOrObject.filter((_, i) => predicateResult[ i ])
return resolve(filtered)
}
const filtered = filter((_, prop) => predicateResult[ prop ],
listOrObject)
return resolve(filtered)
})
.catch(reject)
})
}
export function filterAsync(predicate, listOrObject){
if (arguments.length === 1){
return async _listOrObject => filterAsyncFn(predicate, _listOrObject)
}
return new Promise((resolve, reject) => {
filterAsyncFn(predicate, listOrObject).then(resolve)
.catch(reject)
})
}
import { delay } from './delay.js'
import { filterAsync } from './filterAsync.js'
test('happy', async () => {
const predicate = async (x, i) => {
expect(i).toBeNumber()
await delay(100)
return x % 2 === 1
}
const result = await filterAsync(predicate)([ 1, 2, 3 ])
expect(result).toEqual([ 1, 3 ])
})
test('with object', async () => {
const predicate = async (x, prop) => {
expect(prop).toBeString()
await delay(100)
return x % 2 === 1
}
const result = await filterAsync(predicate, {
a : 1,
b : 2,
c : 3,
d : 4,
e : 5,
})
expect(result).toEqual({
a : 1,
c : 3,
e : 5,
})
})
Same as R.filter
, but it passes index/property as second argument to the predicate, when looping over arrays/objects.
filterObject<T>(predicate: ObjectPredicate<T>): (x: Dictionary<T>) => Dictionary<T>
const obj = {a: 1, b:2}
const result = R.filterObject(
x => x > 1,
obj
)
// => {b: 2}
Try this R.filterObject example in Rambda REPL
find<T>(predicate: (x: T) => boolean, list: T[]): T | undefined
It returns the first element of list
that satisfy the predicate
.
If there is no such element, it returns undefined
.
const predicate = x => R.type(x.foo) === 'Number'
const list = [{foo: 'bar'}, {foo: 1}]
const result = R.find(predicate, list)
// => {foo: 1}
Try this R.find example in Rambda REPL
export function find(predicate, list){
if (arguments.length === 1) return _list => find(predicate, _list)
let index = 0
const len = list.length
while (index < len){
const x = list[ index ]
if (predicate(x)){
return x
}
index++
}
}
import { find } from './find.js'
import { propEq } from './propEq.js'
const list = [ { a : 1 }, { a : 2 }, { a : 3 } ]
test('happy', () => {
const fn = propEq(2, 'a')
expect(find(fn, list)).toEqual({ a : 2 })
})
test('with curry', () => {
const fn = propEq(4, 'a')
expect(find(fn)(list)).toBeUndefined()
})
test('with empty list', () => {
expect(find(() => true, [])).toBeUndefined()
})
Asynchronous version of R.find
.
Try this R.findAsync example in Rambda REPL
findIndex<T>(predicate: (x: T) => boolean, list: T[]): number
It returns the index of the first element of list
satisfying the predicate
function.
If there is no such element, then -1
is returned.
const predicate = x => R.type(x.foo) === 'Number'
const list = [{foo: 'bar'}, {foo: 1}]
const result = R.findIndex(predicate, list)
// => 1
Try this R.findIndex example in Rambda REPL
export function findIndex(predicate, list){
if (arguments.length === 1) return _list => findIndex(predicate, _list)
const len = list.length
let index = -1
while (++index < len){
if (predicate(list[ index ])){
return index
}
}
return -1
}
import { findIndex } from './findIndex.js'
import { propEq } from './propEq.js'
const list = [ { a : 1 }, { a : 2 }, { a : 3 } ]
test('happy', () => {
expect(findIndex(propEq(2, 'a'), list)).toBe(1)
expect(findIndex(propEq(1, 'a'))(list)).toBe(0)
expect(findIndex(propEq(4, 'a'))(list)).toBe(-1)
})
findLast<T>(fn: (x: T) => boolean, list: T[]): T | undefined
It returns the last element of list
satisfying the predicate
function.
If there is no such element, then undefined
is returned.
const predicate = x => R.type(x.foo) === 'Number'
const list = [{foo: 0}, {foo: 1}]
const result = R.findLast(predicate, list)
// => {foo: 1}
Try this R.findLast example in Rambda REPL
export function findLast(predicate, list){
if (arguments.length === 1) return _list => findLast(predicate, _list)
let index = list.length
while (--index >= 0){
if (predicate(list[ index ])){
return list[ index ]
}
}
return undefined
}
import { findLast } from './findLast.js'
test('happy', () => {
const result = findLast(x => x > 1, [ 1, 1, 1, 2, 3, 4, 1 ])
expect(result).toBe(4)
expect(findLast(x => x === 0, [ 0, 1, 1, 2, 3, 4, 1 ])).toBe(0)
})
test('with curry', () => {
expect(findLast(x => x > 1)([ 1, 1, 1, 2, 3, 4, 1 ])).toBe(4)
})
const obj1 = { x : 100 }
const obj2 = { x : 200 }
const a = [ 11, 10, 9, 'cow', obj1, 8, 7, 100, 200, 300, obj2, 4, 3, 2, 1, 0 ]
const even = function (x){
return x % 2 === 0
}
const gt100 = function (x){
return x > 100
}
const isStr = function (x){
return typeof x === 'string'
}
const xGt100 = function (o){
return o && o.x > 100
}
test('ramda 1', () => {
expect(findLast(even, a)).toBe(0)
expect(findLast(gt100, a)).toBe(300)
expect(findLast(isStr, a)).toBe('cow')
expect(findLast(xGt100, a)).toEqual(obj2)
})
test('ramda 2', () => {
expect(findLast(even, [ 'zing' ])).toBeUndefined()
})
test('ramda 3', () => {
expect(findLast(even, [ 2, 3, 5 ])).toBe(2)
})
test('ramda 4', () => {
expect(findLast(even, [])).toBeUndefined()
})
findLastIndex<T>(predicate: (x: T) => boolean, list: T[]): number
It returns the index of the last element of list
satisfying the predicate
function.
If there is no such element, then -1
is returned.
const predicate = x => R.type(x.foo) === 'Number'
const list = [{foo: 0}, {foo: 1}]
const result = R.findLastIndex(predicate, list)
// => 1
Try this R.findLastIndex example in Rambda REPL
export function findLastIndex(fn, list){
if (arguments.length === 1) return _list => findLastIndex(fn, _list)
let index = list.length
while (--index >= 0){
if (fn(list[ index ])){
return index
}
}
return -1
}
import { findLastIndex } from './findLastIndex.js'
test('happy', () => {
const result = findLastIndex(x => x > 1, [ 1, 1, 1, 2, 3, 4, 1 ])
expect(result).toBe(5)
expect(findLastIndex(x => x === 0, [ 0, 1, 1, 2, 3, 4, 1 ])).toBe(0)
})
test('with curry', () => {
expect(findLastIndex(x => x > 1)([ 1, 1, 1, 2, 3, 4, 1 ])).toBe(5)
})
const obj1 = { x : 100 }
const obj2 = { x : 200 }
const a = [ 11, 10, 9, 'cow', obj1, 8, 7, 100, 200, 300, obj2, 4, 3, 2, 1, 0 ]
const even = function (x){
return x % 2 === 0
}
const gt100 = function (x){
return x > 100
}
const isStr = function (x){
return typeof x === 'string'
}
const xGt100 = function (o){
return o && o.x > 100
}
test('ramda 1', () => {
expect(findLastIndex(even, a)).toBe(15)
expect(findLastIndex(gt100, a)).toBe(9)
expect(findLastIndex(isStr, a)).toBe(3)
expect(findLastIndex(xGt100, a)).toBe(10)
})
test('ramda 2', () => {
expect(findLastIndex(even, [ 'zing' ])).toBe(-1)
})
test('ramda 3', () => {
expect(findLastIndex(even, [ 2, 3, 5 ])).toBe(0)
})
test('ramda 4', () => {
expect(findLastIndex(even, [])).toBe(-1)
})
flatten<T>(list: any[]): T[]
It deeply flattens an array.
const result = R.flatten([
1,
2,
[3, 30, [300]],
[4]
])
// => [ 1, 2, 3, 30, 300, 4 ]
Try this R.flatten example in Rambda REPL
import { isArray } from './_internals/isArray.js'
export function flatten(list, input){
const willReturn = input === undefined ? [] : input
for (let i = 0; i < list.length; i++){
if (isArray(list[ i ])){
flatten(list[ i ], willReturn)
} else {
willReturn.push(list[ i ])
}
}
return willReturn
}
import { flatten } from './flatten.js'
test('happy', () => {
expect(flatten([ 1, 2, 3, [ [ [ [ [ 4 ] ] ] ] ] ])).toEqual([ 1, 2, 3, 4 ])
expect(flatten([ 1, [ 2, [ [ 3 ] ] ], [ 4 ] ])).toEqual([ 1, 2, 3, 4 ])
expect(flatten([ 1, [ 2, [ [ [ 3 ] ] ] ], [ 4 ] ])).toEqual([ 1, 2, 3, 4 ])
expect(flatten([ 1, 2, [ 3, 4 ], 5, [ 6, [ 7, 8, [ 9, [ 10, 11 ], 12 ] ] ] ])).toEqual([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ])
})
test('readme example', () => {
const result = flatten([ 1, 2, [ 3, 30, [ 300 ] ], [ 4 ] ])
expect(result).toEqual([ 1, 2, 3, 30, 300, 4 ])
})
It returns function which calls fn
with exchanged first and second argument.
:boom: Rambda's flip will throw if the arity of the input function is greater or equal to 5.
Try this R.flip example in Rambda REPL
forEach<T>(fn: Iterator<T, void>, list: T[]): T[]
It applies iterable
function over all members of list
and returns list
.
:boom: It works with objects, unlike
Ramda
.
const sideEffect = {}
const result = R.forEach(
x => sideEffect[`foo${x}`] = x
)([1, 2])
sideEffect // => {foo1: 1, foo2: 2}
result // => [1, 2]
Try this R.forEach example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { forEachObjIndexedFn } from './forEachObjIndexed.js'
export function forEach(fn, iterable){
if (arguments.length === 1) return _list => forEach(fn, _list)
if (iterable === undefined) return
if (isArray(iterable)){
let index = 0
const len = iterable.length
while (index < len){
fn(iterable[ index ])
index++
}
} else return forEachObjIndexedFn(fn, iterable)
return iterable
}
import { forEach } from './forEach.js'
import { type } from './type.js'
test('happy', () => {
const sideEffect = {}
forEach(x => sideEffect[ `foo${ x }` ] = x + 10)([ 1, 2 ])
expect(sideEffect).toEqual({
foo1 : 11,
foo2 : 12,
})
})
test('iterate over object', () => {
const obj = {
a : 1,
b : [ 1, 2 ],
c : { d : 7 },
f : 'foo',
}
const result = {}
const returned = forEach((
val, prop, inputObj
) => {
expect(type(inputObj)).toBe('Object')
result[ prop ] = `${ prop }-${ type(val) }`
})(obj)
const expected = {
a : 'a-Number',
b : 'b-Array',
c : 'c-Object',
f : 'f-String',
}
expect(result).toEqual(expected)
expect(returned).toEqual(obj)
})
test('with empty list', () => {
const list = []
const result = forEach(x => x * x)(list)
expect(result).toEqual(list)
})
test('with wrong input', () => {
const list = undefined
const result = forEach(x => x * x)(list)
expect(result).toBeUndefined()
})
test('returns the input', () => {
const list = [ 1, 2, 3 ]
const result = forEach(x => x * x)(list)
expect(result).toEqual(list)
})
It transforms a listOfPairs
to an object.
Try this R.fromPairs example in Rambda REPL
getter<T>(keyOrKeys: string | string[] | undefined): T
The set of methods R.setter
, R.getter
and R.reset
allow different parts of your logic to access communicate indirectly via shared cache object.
Usually these methods show that you might need to refactor to classes. Still, they can be helpful meanwhile.
R.getter
: It provides access to the cache object. If undefined
is used as a key, this method will return the whole cache object. If string
is passed, then it will return cache value for this key. If array of string
is passed, then it assume that this is array of keys and it will return the corresponding cache values for these keys.
R.setter
: It allows cache object's keys to be changed. You can either set individual key-value pairs with R.setter(key, value)
or you pass directly object, which will be merged with the cache object.
R.reset
: It resets the cache object.
R.setter('foo','bar')
R.setter('a', 1)
R.getter(['foo','a']) // => {foo: 'bar', a: 1}
R.setter('a', 2)
R.getter('a') // => 2
R.reset()
R.getter('a') // => undefined
Try this R.getter example in Rambda REPL
import { mergeRight } from './mergeRight.js'
import { pick } from './pick.js'
import { type } from './type.js'
let holder = {}
/**
* Pass string to get value
* Pass array to get object of values
* Pass undefined to get all data
*/
export function getter(key){
const typeKey = type(key)
if (typeKey === 'String') return holder[ key ]
if (typeKey === 'Array') return pick(key, holder)
return holder
}
export function setter(maybeKey, maybeValue){
const typeKey = type(maybeKey)
const typeValue = type(maybeValue)
if (typeKey === 'String'){
if (typeValue === 'Function'){
return holder[ maybeKey ] = maybeValue(holder[ maybeKey ])
}
return holder[ maybeKey ] = maybeValue
}
if (typeKey !== 'Object') return
holder = mergeRight(holder, maybeKey)
}
export function reset(){
holder = {}
}
import { add } from './add.js'
import { getter, reset, setter } from './getter.js'
afterEach(() => {
reset()
})
test('happy', () => {
const key = 'foo'
setter(key, 1)
expect(getter(key)).toBe(1)
})
test('docs example', () => {
setter('foo', 'bar')
setter('a', 1)
expect(getter([ 'foo', 'a' ])).toEqual({
foo : 'bar',
a : 1,
})
setter('a', 2)
expect(getter('a')).toBe(2)
reset()
expect(getter('a')).toBeUndefined()
})
test('when array is key in getter', () => {
setter({
a : 1,
b : 2,
c : 3,
})
expect(getter([ 'a', 'b' ])).toEqual({
a : 1,
b : 2,
})
})
test('getter with undefined as key returns all', () => {
const data = {
a : 1,
b : 2,
c : 3,
}
setter(data)
expect(getter()).toEqual(data)
})
test('function as setter value', () => {
const data = {
a : 1,
b : 2,
c : 3,
}
setter(data)
setter('a', add(10))
expect(getter()).toEqual({
a : 11,
b : 2,
c : 3,
})
})
test('setter fallbacks to undefined', () => {
expect(setter()).toBeUndefined
})
glue(input: string, glueString?: string): string
It transforms multiline string to single line by gluing together the separate lines with the glueString
and removing the empty spaces. By default glueString
is equal to single space, so if that is what you need, then you can just pass a single argument.
const result = R.glue(`
foo
bar
baz
`)
// => 'foo bar baz'
Try this R.glue example in Rambda REPL
export function glue(input, glueChar){
return input
.split('\n')
.filter(x => x.trim().length > 0)
.map(x => x.trim())
.join(glueChar === undefined ? ' ' : glueChar)
}
import { glue } from './glue.js'
test('empty string as a glue', () => {
const result = glue(`
foo
bar
baz
`,
'')
const expectedResult = 'foobarbaz'
expect(result).toBe(expectedResult)
})
test('case 0', () => {
const zero = 'node node_modules/jest'
const first = '--runInBand'
const last = '-- src/a.spec.js'
const flag = false
const result = glue(`
${ zero }
${ first }
${ flag ? '--env=node' : '' }
${ last }
`)
const expectedResult = `${ zero } ${ first } ${ last }`
expect(result).toBe(expectedResult)
})
test('case 1', () => {
const zero = 'node node_modules/jest'
const first = '--runInBand'
const last = '-- src/a.spec.js'
const flag = true
const result = glue(`
${ zero }
${ first }
${ flag ? '--env=node' : '' }
${ last }
`)
const expectedResult = `${ zero } ${ first } --env=node ${ last }`
expect(result).toBe(expectedResult)
})
test('case 2', () => {
const first = '--runInBand'
const result = glue(`
zero
${ first }
last
`)
const expectedResult = `zero ${ first } last`
expect(result).toBe(expectedResult)
})
test('case 3', () => {
const result = glue(`
foo
bar
baz
`)
const expectedResult = 'foo bar baz'
expect(result).toBe(expectedResult)
})
test('with glue', () => {
const result = glue(`
foo
bar
baz
`,
'==')
const expectedResult = 'foo==bar==baz'
expect(result).toBe(expectedResult)
})
It splits list
according to a provided groupFn
function and returns an object.
Try this R.groupBy example in Rambda REPL
It returns separated version of list or string input
, where separation is done with equality compareFn
function.
Try this R.groupWith example in Rambda REPL
Try this R.gt example in Rambda REPL
Try this R.gte example in Rambda REPL
has<T>(prop: string, obj: T): boolean
It returns true
if obj
has property prop
.
const obj = {a: 1}
const result = [
R.has('a', Record<string, unknown>),
R.has('b', Record<string, unknown>)
]
// => [true, false]
Try this R.has example in Rambda REPL
export function has(prop, obj){
if (arguments.length === 1) return _obj => has(prop, _obj)
if (!obj) return false
return obj.hasOwnProperty(prop)
}
import { has } from './has.js'
test('happy', () => {
expect(has('a')({ a : 1 })).toBeTrue()
expect(has('b', { a : 1 })).toBeFalse()
})
test('with non-object', () => {
expect(has('a', undefined)).toBeFalse()
expect(has('a', null)).toBeFalse()
expect(has('a', true)).toBeFalse()
expect(has('a', '')).toBeFalse()
expect(has('a', /a/)).toBeFalse()
})
Try this R.hasIn example in Rambda REPL
hasPath<T>(
path: string | string[],
input: object
): boolean
It will return true, if input
object has truthy path
(calculated with R.path
).
const path = 'a.b'
const pathAsArray = ['a', 'b']
const obj = {a: {b: []}}
const result = [
R.hasPath(path, Record<string, unknown>),
R.hasPath(pathAsArray, Record<string, unknown>),
R.hasPath('a.c', Record<string, unknown>),
]
// => [true, true, false]
Try this R.hasPath example in Rambda REPL
import { path } from './path.js'
export function hasPath(pathInput, obj){
if (arguments.length === 1){
return objHolder => hasPath(pathInput, objHolder)
}
return path(pathInput, obj) !== undefined
}
import { hasPath } from './hasPath.js'
test('when true', () => {
const path = 'a.b'
const obj = { a : { b : [] } }
const result = hasPath(path)(obj)
const expectedResult = true
expect(result).toEqual(expectedResult)
})
test('when false', () => {
const path = 'a.b'
const obj = {}
const result = hasPath(path, obj)
const expectedResult = false
expect(result).toEqual(expectedResult)
})
head(str: string): string
It returns the first element of list or string input
. It returns undefined
if array has length of 0.
const result = [
R.head([1, 2, 3]),
R.head('foo')
]
// => [1, 'f']
Try this R.head example in Rambda REPL
export function head(listOrString){
if (typeof listOrString === 'string') return listOrString[ 0 ] || ''
return listOrString[ 0 ]
}
import { head } from './head.js'
test('head', () => {
expect(head([ 'fi', 'fo', 'fum' ])).toBe('fi')
expect(head([])).toBeUndefined()
expect(head('foo')).toBe('f')
expect(head('')).toBe('')
})
It returns true
if its arguments a
and b
are identical.
Otherwise, it returns false
.
:boom: Values are identical if they reference the same memory.
NaN
is identical toNaN
;0
and-0
are not identical.
Try this R.identical example in Rambda REPL
identity<T>(input: T): T
It just passes back the supplied input
argument.
:boom: Logic
R.identity(7) // => 7
Try this R.identity example in Rambda REPL
export function identity(x){
return x
}
import { identity } from './identity.js'
test('happy', () => {
expect(identity(7)).toBe(7)
expect(identity(true)).toBeTrue()
expect(identity({ a : 1 })).toEqual({ a : 1 })
})
ifElse<T, TFiltered extends T, TOnTrueResult, TOnFalseResult>(
pred: (a: T) => a is TFiltered,
onTrue: (a: TFiltered) => TOnTrueResult,
onFalse: (a: Exclude<T, TFiltered>) => TOnFalseResult,
): (a: T) => TOnTrueResult | TOnFalseResult
It expects condition
, onTrue
and onFalse
functions as inputs and it returns a new function with example name of fn
.
When fn`` is called with
inputargument, it will return either
onTrue(input)or
onFalse(input)depending on
condition(input)` evaluation.
const fn = R.ifElse(
x => x>10,
x => x*2,
x => x*10
)
const result = [ fn(8), fn(18) ]
// => [80, 36]
Try this R.ifElse example in Rambda REPL
import { curry } from './curry.js'
function ifElseFn(
condition, onTrue, onFalse
){
return (...input) => {
const conditionResult =
typeof condition === 'boolean' ? condition : condition(...input)
if (conditionResult === true){
return onTrue(...input)
}
return onFalse(...input)
}
}
export const ifElse = curry(ifElseFn)
import { always } from './always.js'
import { has } from './has.js'
import { identity } from './identity.js'
import { ifElse } from './ifElse.js'
import { prop } from './prop.js'
const condition = has('foo')
const v = function (a){
return typeof a === 'number'
}
const t = function (a){
return a + 1
}
const ifFn = x => prop('foo', x).length
const elseFn = () => false
test('happy', () => {
const fn = ifElse(condition, ifFn)(elseFn)
expect(fn({ foo : 'bar' })).toBe(3)
expect(fn({ fo : 'bar' })).toBeFalse()
})
test('ramda spec', () => {
const ifIsNumber = ifElse(v)
expect(ifIsNumber(t, identity)(15)).toBe(16)
expect(ifIsNumber(t, identity)('hello')).toBe('hello')
})
test('pass all arguments', () => {
const identity = function (a){
return a
}
const v = function (){
return true
}
const onTrue = function (a, b){
expect(a).toBe(123)
expect(b).toBe('abc')
}
ifElse(
v, onTrue, identity
)(123, 'abc')
})
test('accept constant as condition', () => {
const fn = ifElse(true)(always(true))(always(false))
expect(fn()).toBeTrue()
})
test('accept constant as condition - case 2', () => {
const fn = ifElse(
false, always(true), always(false)
)
expect(fn()).toBeFalse()
})
test('curry 1', () => {
const fn = ifElse(condition, ifFn)(elseFn)
expect(fn({ foo : 'bar' })).toBe(3)
expect(fn({ fo : 'bar' })).toBeFalse()
})
test('curry 2', () => {
const fn = ifElse(condition)(ifFn)(elseFn)
expect(fn({ foo : 'bar' })).toBe(3)
expect(fn({ fo : 'bar' })).toBeFalse()
})
test('simple arity of 1', () => {
const condition = x => x > 5
const onTrue = x => x + 1
const onFalse = x => x + 10
const result = ifElse(
condition, onTrue, onFalse
)(1)
expect(result).toBe(11)
})
test('simple arity of 2', () => {
const condition = (x, y) => x + y > 5
const onTrue = (x, y) => x + y + 1
const onFalse = (x, y) => x + y + 10
const result = ifElse(
condition, onTrue, onFalse
)(1, 10)
expect(result).toBe(12)
})
ifElseAsync<T, U>(
condition: (x: T) => Promise<boolean>,
onTrue: (x: T) => U,
onFalse: (x: T) => U,
): (x: T) => Promise<U>
Asynchronous version of R.ifElse
. Any of condition
, ifFn
and elseFn
can be either asynchronous or synchronous function.
const condition = async x => {
await R.delay(100)
return x > 1
}
const ifFn = async x => {
await R.delay(100)
return x + 1
}
const elseFn = async x => {
await R.delay(100)
return x - 1
}
const result = await R.ifElseAsync(
condition,
ifFn,
elseFn
)(1)
// => 0
Try this R.ifElseAsync example in Rambda REPL
function createThenable(fn){
return async function (...input){
return fn(...input)
}
}
export function ifElseAsync(
condition, ifFn, elseFn
){
return (...inputs) =>
new Promise((resolve, reject) => {
const conditionPromise = createThenable(condition)
const ifFnPromise = createThenable(ifFn)
const elseFnPromise = createThenable(elseFn)
conditionPromise(...inputs)
.then(conditionResult => {
const promised =
conditionResult === true ? ifFnPromise : elseFnPromise
promised(...inputs)
.then(resolve)
.catch(reject)
})
.catch(reject)
})
}
import { delay } from './delay.js'
import { ifElseAsync } from './ifElseAsync.js'
test('arity of 1 - condition is async', async () => {
const condition = async x => {
await delay(100)
return x > 4
}
const whenTrue = x => x + 1
const whenFalse = x => x + 10
const fn = ifElseAsync(
condition, whenTrue, whenFalse
)
const result = await Promise.all([ fn(5), fn(1) ])
expect(result).toEqual([ 6, 11 ])
})
test('arity of 1 - condition is sync', async () => {
const condition = x => x > 4
const whenTrue = async x => {
await delay(100)
return x + 1
}
const whenFalse = async x => {
await delay(100)
return x + 10
}
const fn = ifElseAsync(
condition, whenTrue, whenFalse
)
const result = await Promise.all([ fn(5), fn(1) ])
expect(result).toEqual([ 6, 11 ])
})
test('arity of 1 - all inputs are async', async () => {
const condition = async x => {
await delay(100)
return x > 4
}
const whenTrue = async x => {
await delay(100)
return x + 1
}
const whenFalse = async x => {
await delay(100)
return x + 10
}
const fn = ifElseAsync(
condition, whenTrue, whenFalse
)
const result = await Promise.all([ fn(5), fn(1) ])
expect(result).toEqual([ 6, 11 ])
})
test('arity of 2 - condition is async', async () => {
const condition = async (x, y) => {
await delay(100)
return x + y > 4
}
const whenTrue = (x, y) => x + y + 1
const whenFalse = (x, y) => x + y + 10
const fn = ifElseAsync(
condition, whenTrue, whenFalse
)
const result = await Promise.all([ fn(14, 20), fn(1, 3) ])
expect(result).toEqual([ 35, 14 ])
})
test('arity of 2 - condition is sync', async () => {
const condition = (x, y) => x + y > 4
const whenTrue = async (x, y) => {
await delay(100)
return x + y + 1
}
const whenFalse = async (x, y) => {
await delay(100)
return x + y + 10
}
const fn = ifElseAsync(
condition, whenTrue, whenFalse
)
const result = await Promise.all([ fn(14, 20), fn(1, 3) ])
expect(result).toEqual([ 35, 14 ])
})
test('arity of 2 - all inputs are async', async () => {
const condition = async (x, y) => {
await delay(100)
return x + y > 4
}
const whenTrue = async (x, y) => {
await delay(100)
return x + y + 1
}
const whenFalse = async (x, y) => {
await delay(100)
return x + y + 10
}
const fn = ifElseAsync(
condition, whenTrue, whenFalse
)
const result = await Promise.all([ fn(14, 20), fn(1, 3) ])
expect(result).toEqual([ 35, 14 ])
})
It increments a number.
Try this R.inc example in Rambda REPL
includes<T extends string>(valueToFind: T, input: string): boolean
If input
is string, then this method work as native String.includes
.
If input
is array, then R.equals
is used to define if valueToFind
belongs to the list.
const result = [
R.includes('oo', 'foo'),
R.includes({a: 1}, [{a: 1}])
]
// => [true, true ]
Try this R.includes example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { _indexOf } from './equals.js'
export function includes(valueToFind, iterable){
if (arguments.length === 1)
return _iterable => includes(valueToFind, _iterable)
if (typeof iterable === 'string'){
return iterable.includes(valueToFind)
}
if (!iterable){
throw new TypeError(`Cannot read property \'indexOf\' of ${ iterable }`)
}
if (!isArray(iterable)) return false
return _indexOf(valueToFind, iterable) > -1
}
import { includes as includesRamda } from 'ramda'
import { includes } from './includes.js'
test('with string as iterable', () => {
const str = 'foo bar'
expect(includes('bar')(str)).toBeTrue()
expect(includesRamda('bar')(str)).toBeTrue()
expect(includes('never', str)).toBeFalse()
expect(includesRamda('never', str)).toBeFalse()
})
test('with array as iterable', () => {
const arr = [ 1, 2, 3 ]
expect(includes(2)(arr)).toBeTrue()
expect(includesRamda(2)(arr)).toBeTrue()
expect(includes(4, arr)).toBeFalse()
expect(includesRamda(4, arr)).toBeFalse()
})
test('with list of objects as iterable', () => {
const arr = [ { a : 1 }, { b : 2 }, { c : 3 } ]
expect(includes({ c : 3 }, arr)).toBeTrue()
expect(includesRamda({ c : 3 }, arr)).toBeTrue()
})
test('with NaN', () => {
const result = includes(NaN, [ NaN ])
const ramdaResult = includesRamda(NaN, [ NaN ])
expect(result).toBeTrue()
expect(ramdaResult).toBeTrue()
})
test('with wrong input that does not throw', () => {
const result = includes(1, /foo/g)
const ramdaResult = includesRamda(1, /foo/g)
expect(result).toBeFalse()
expect(ramdaResult).toBeFalse()
})
test('throws on wrong input - match ramda behaviour', () => {
expect(() => includes(2, null)).toThrowWithMessage(TypeError,
'Cannot read property \'indexOf\' of null')
expect(() => includesRamda(2, null)).toThrowWithMessage(TypeError,
'Cannot read properties of null (reading \'indexOf\')')
expect(() => includes(2, undefined)).toThrowWithMessage(TypeError,
'Cannot read property \'indexOf\' of undefined')
expect(() => includesRamda(2, undefined)).toThrowWithMessage(TypeError,
'Cannot read properties of undefined (reading \'indexOf\')')
})
It generates object with properties provided by condition
and values provided by list
array.
If condition
is a function, then all list members are passed through it.
If condition
is a string, then all list members are passed through R.path(condition)
.
Try this R.indexBy example in Rambda REPL
It returns the index of the first element of list
equals to valueToFind
.
If there is no such element, it returns -1
.
:boom: It uses
R.equals
for list of objects/arrays or nativeindexOf
for any other case.
Try this R.indexOf example in Rambda REPL
init<T extends unknown[]>(input: T): T extends readonly [...infer U, any] ? U : [...T]
It returns all but the last element of list or string input
.
const result = [
R.init([1, 2, 3]) ,
R.init('foo') // => 'fo'
]
// => [[1, 2], 'fo']
Try this R.init example in Rambda REPL
import baseSlice from './_internals/baseSlice.js'
export function init(listOrString){
if (typeof listOrString === 'string') return listOrString.slice(0, -1)
return listOrString.length ?
baseSlice(
listOrString, 0, -1
) :
[]
}
import { init } from './init.js'
test('with array', () => {
expect(init([ 1, 2, 3 ])).toEqual([ 1, 2 ])
expect(init([ 1, 2 ])).toEqual([ 1 ])
expect(init([ 1 ])).toEqual([])
expect(init([])).toEqual([])
expect(init([])).toEqual([])
expect(init([ 1 ])).toEqual([])
})
test('with string', () => {
expect(init('foo')).toBe('fo')
expect(init('f')).toBe('')
expect(init('')).toBe('')
})
It returns a new list by applying a predicate
function to all elements of list1
and list2
and keeping only these elements where predicate
returns true
.
Try this R.innerJoin example in Rambda REPL
Try this R.insert example in Rambda REPL
Try this R.insertAll example in Rambda REPL
interpolate(inputWithTags: string, templateArguments: object): string
It generates a new string from inputWithTags
by replacing all {{x}}
occurrences with values provided by templateArguments
.
const inputWithTags = 'foo is {{bar}} even {{a}} more'
const templateArguments = {"bar":"BAR", a: 1}
const result = R.interpolate(inputWithTags, templateArguments)
const expected = 'foo is BAR even 1 more'
// => `result` is equal to `expected`
Try this R.interpolate example in Rambda REPL
const getOccurrences = input => input.match(/{{\s*.+?\s*}}/g)
const getOccurrenceProp = occurrence =>
occurrence.replace(/{{\s*|\s*}}/g, '')
const replace = ({ inputHolder, prop, replacer }) => {
const regexBase = `{{${ prop }}}`
const regex = new RegExp(regexBase, 'g')
return inputHolder.replace(regex, replacer)
}
export function interpolate(input, templateInput){
if (arguments.length === 1){
return _templateInput => interpolate(input, _templateInput)
}
const occurrences = getOccurrences(input)
if (occurrences === null) return input
let inputHolder = input
for (const occurrence of occurrences){
const prop = getOccurrenceProp(occurrence)
inputHolder = replace({
inputHolder,
prop,
replacer : templateInput[ prop ],
})
}
return inputHolder
}
import { interpolate } from './interpolate.js'
test('within bracets', () => {
const input = 'foo is { {{bar}} } even {{a}} more'
const templateInput = {
bar : 'BAR',
a : 1,
}
const result = interpolate(input, templateInput)
const expectedResult = 'foo is { BAR } even 1 more'
expect(result).toEqual(expectedResult)
})
test('happy', () => {
const input = 'foo is {{bar}} even {{a}} more'
const templateInput = {
bar : 'BAR',
a : 1,
}
const result = interpolate(input, templateInput)
const expectedResult = 'foo is BAR even 1 more'
expect(result).toEqual(expectedResult)
})
test('no interpolation + curry', () => {
const input = 'foo is bar even more'
const templateInput = { bar : 'BAR' }
const result = interpolate(input)(templateInput)
const expectedResult = 'foo is bar even more'
expect(result).toEqual(expectedResult)
})
test('with missing template input', () => {
const input = 'foo is {{bar}} even {{a}} more'
const templateInput = {
baz : 'BAR',
a : 1,
}
const result = interpolate(input, templateInput)
const expectedResult = 'foo is undefined even 1 more'
expect(result).toEqual(expectedResult)
})
test('with arbitrary expression', () => {
const input = '1 + 2 = {{ 1 + 2 }}'
const templateInput = {}
const result = interpolate(input, templateInput)
expect(result).toEqual(input)
})
It loops through listA
and listB
and returns the intersection of the two according to R.equals
.
:boom: There is slight difference between Rambda and Ramda implementation. Ramda.intersection(['a', 'b', 'c'], ['c', 'b']) result is "[ 'c', 'b' ]", but Rambda result is "[ 'b', 'c' ]".
Try this R.intersection example in Rambda REPL
It adds a separator
between members of list
.
Try this R.intersperse example in Rambda REPL
It returns true
if x
is instance of targetPrototype
.
Try this R.is example in Rambda REPL
isEmpty<T>(x: T): boolean
It returns true
if x
is empty
.
const result = [
R.isEmpty(''),
R.isEmpty({ x : 0 })
]
// => [true, false]
Try this R.isEmpty example in Rambda REPL
import { type } from './type.js'
export function isEmpty(input){
const inputType = type(input)
if ([ 'Undefined', 'NaN', 'Number', 'Null' ].includes(inputType))
return false
if (!input) return true
if (inputType === 'Object'){
return Object.keys(input).length === 0
}
if (inputType === 'Array'){
return input.length === 0
}
return false
}
import { isEmpty } from './isEmpty.js'
test('happy', () => {
expect(isEmpty(undefined)).toBeFalse()
expect(isEmpty('')).toBeTrue()
expect(isEmpty(null)).toBeFalse()
expect(isEmpty(' ')).toBeFalse()
expect(isEmpty(new RegExp(''))).toBeFalse()
expect(isEmpty([])).toBeTrue()
expect(isEmpty([ [] ])).toBeFalse()
expect(isEmpty({})).toBeTrue()
expect(isEmpty({ x : 0 })).toBeFalse()
expect(isEmpty(0)).toBeFalse()
expect(isEmpty(NaN)).toBeFalse()
expect(isEmpty([ '' ])).toBeFalse()
})
isNil(x: any): x is null | undefined
It returns true
if x
is either null
or undefined
.
const result = [
R.isNil(null),
R.isNil(1),
]
// => [true, false]
Try this R.isNil example in Rambda REPL
export function isNil(x){
return x === undefined || x === null
}
import { isNil } from './isNil.js'
test('happy', () => {
expect(isNil(null)).toBeTrue()
expect(isNil(undefined)).toBeTrue()
expect(isNil([])).toBeFalse()
})
isNotEmpty<T>(value: T[]): value is NonEmptyArray<T>
Try this R.isNotNil example in Rambda REPL
isPromise(input: any): boolean
isType(targetType: RambdaTypes, input: any): boolean
It returns true if targetType
is equal to type of input
according to R.type
.
R.isType('Async',R.delay(1000))
// => true
Try this R.isType example in Rambda REPL
import { type } from './type.js'
export function isType(xType, x){
if (arguments.length === 1){
return xHolder => isType(xType, xHolder)
}
return type(x) === xType
}
import { delay } from './delay.js'
import { isType } from './isType.js'
const list = [ 1, 2, 3 ]
test('array', () => {
expect(isType('Array', list)).toBeTruthy()
expect(isType('Array')([])).toBeTruthy()
})
test('promise', () => {
expect(isType('Promise', Promise.resolve(1))).toBeTruthy()
})
test('async', () => {
async function fn(){}
expect(isType('Promise', fn)).toBeTruthy()
})
test('with R.delay', () => {
expect(isType('Function', delay)).toBeTruthy()
expect(isType('Promise', delay(100))).toBeTruthy()
})
isValid({input: object, schema: Schema}: IsValid): boolean
It checks if input
is following schema
specifications.
If validation fails, it returns false
.
Please check the detailed explanation as it is hard to write a short description for this method.
:boom: Independently, somebody else came with very similar idea called superstruct
const input = {a: ['foo', 'bar']}
const invalidInput = {a: ['foo', 'bar', 1]}
const schema = {a: [String]}
const result = [
R.isValid({schema, input}),
R.isValid({schema, input: invalidInput})
]
// => [true, false]
Try this R.isValid example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { all } from './all.js'
import { any } from './any.js'
import { includes } from './includes.js'
import { init } from './init.js'
import { test } from './test.js'
import { toLower } from './toLower.js'
import { type } from './type.js'
export function isPrototype(input){
const currentPrototype = input.prototype
const list = [ Number, String, Boolean, Promise ]
let toReturn = false
let counter = -1
while (++counter < list.length && !toReturn){
if (currentPrototype === list[ counter ].prototype) toReturn = true
}
return toReturn
}
export function prototypeToString(input){
const currentPrototype = input.prototype
const list = [ Number, String, Boolean, Promise ]
const translatedList = [ 'Number', 'String', 'Boolean', 'Promise' ]
let found
let counter = -1
while (++counter < list.length){
if (currentPrototype === list[ counter ].prototype) found = counter
}
return translatedList[ found ]
}
const typesWithoutPrototype = [ 'any', 'promise', 'async', 'function' ]
export function fromPrototypeToString(rule){
if (
isArray(rule) ||
rule === undefined ||
rule === null ||
rule.prototype === undefined ||
typesWithoutPrototype.includes(rule)
){
return {
rule,
parsed : false,
}
}
if (String.prototype === rule.prototype){
return {
rule : 'string',
parsed : true,
}
}
if (Boolean.prototype === rule.prototype){
return {
rule : 'boolean',
parsed : true,
}
}
if (Number.prototype === rule.prototype){
return {
rule : 'number',
parsed : true,
}
}
return {
rule : type(rule.prototype).toLowerCase(),
parsed : true,
}
}
function getRuleAndType(schema, requirementRaw){
const ruleRaw = schema[ requirementRaw ]
const typeIs = type(ruleRaw)
const { rule, parsed } = fromPrototypeToString(ruleRaw)
return {
rule,
ruleType : parsed ? 'String' : typeIs,
}
}
export function isValid({ input, schema }){
if (input === undefined || schema === undefined) return false
let flag = true
const boom = boomFlag => {
if (!boomFlag){
flag = false
}
}
for (const requirementRaw in schema){
if (flag){
const isOptional = requirementRaw.endsWith('?')
const requirement = isOptional ? init(requirementRaw) : requirementRaw
const { rule, ruleType } = getRuleAndType(schema, requirementRaw)
const inputProp = input[ requirement ]
const inputPropType = type(input[ requirement ])
const ok = isOptional && inputProp !== undefined || !isOptional
if (!ok || rule === 'any' && inputProp != null || rule === inputProp)
continue
if (ruleType === 'Object'){
/**
* This rule is standalone schema, so we recursevly call `isValid`
*/
const isValidResult = isValid({
input : inputProp,
schema : rule,
})
boom(isValidResult)
} else if (ruleType === 'String'){
/**
* Rule is actual rule such as 'number', so the two types are compared
*/
boom(toLower(inputPropType) === rule)
} else if (typeof rule === 'function'){
/**
* Rule is function so we pass to it the input
*/
boom(rule(inputProp))
} else if (ruleType === 'Array' && inputPropType === 'String'){
/**
* Enum case | rule is like a: ['foo', 'bar']
*/
boom(includes(inputProp, rule))
} else if (
ruleType === 'Array' &&
rule.length === 1 &&
inputPropType === 'Array'
){
/**
* 1. array of type | rule is like a: ['number']
* 2. rule is like a: [{foo: 'string', bar: 'number'}]
*/
const [ currentRule ] = rule
const currentRuleType = type(currentRule)
//Check if rule is invalid
boom(currentRuleType === 'String' ||
currentRuleType === 'Object' ||
isPrototype(currentRule))
if (currentRuleType === 'Object' && flag){
/**
* 2. rule is like a: [{from: 'string'}]
*/
const isValidResult = all(inputPropInstance =>
isValid({
input : inputPropInstance,
schema : currentRule,
}),
inputProp)
boom(isValidResult)
} else if (flag){
/**
* 1. array of type
*/
const actualRule =
currentRuleType === 'String' ?
currentRule :
prototypeToString(currentRule)
const isInvalidResult = any(inputPropInstance =>
type(inputPropInstance).toLowerCase() !==
actualRule.toLowerCase(),
inputProp)
boom(!isInvalidResult)
}
} else if (ruleType === 'RegExp' && inputPropType === 'String'){
boom(test(rule, inputProp))
} else {
boom(false)
}
}
}
return flag
}
import { delay } from './delay.js'
import { isPrototype, isValid } from './isValid.js'
test('is prototype', () => {
expect(isPrototype(Promise)).toBeTrue()
expect(isPrototype(Number)).toBeTrue()
expect(isPrototype(Boolean)).toBeTrue()
expect(isPrototype(String)).toBeTrue()
expect(isPrototype(0)).toBeFalse()
})
test('prototype inside array', () => {
const input = { a : [ 1, 2, 3, 4 ] }
const schema = { a : [ Number ] }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('with Promise prototype', () => {
const input = { a : [ delay(1), delay(2) ] }
const schema = { a : [ Promise ] }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('object prototype as rule - true', () => {
const input = { a : {} }
const schema = { a : Object }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('list of functions', () => {
const input = { a : [ () => {}, delay ] }
const schema = { a : [ 'function' ] }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('function schema type can be only string', () => {
const input = { a : [ () => {}, delay ] }
const schema = { a : [ Function ] }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('object prototype as rule - false', () => {
const input = { a : null }
const schema = { a : Object }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('number prototype as rule - true', () => {
const input = { a : 1 }
const schema = { a : Number }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('array prototype as rule - true', () => {
const input = { a : [ 1, 2, 3 ] }
const schema = { a : Array }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('array prototype as rule - false', () => {
const input = { a : null }
const schema = { a : Array }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('string prototype as rule - true', () => {
const input = { a : 'foo' }
const schema = { a : String }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('string prototype as rule - false', () => {
const input = { a : null }
const schema = { a : String }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('boolean prototype as rule - true', () => {
const input = { a : true }
const schema = { a : Boolean }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('boolean prototype as rule - false', () => {
const input = { a : null }
const schema = { a : Boolean }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('regex prototype cannot be rule - true', () => {
const input = { a : /foo/g }
const schema = { a : new RegExp('foo') }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('undefined as a rule - true', () => {
const input = { a : undefined }
const schema = { a : undefined }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('undefined as a rule - false', () => {
const input = { a : null }
const schema = { a : undefined }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('null as a rule - true', () => {
const input = { a : null }
const schema = { a : null }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('null as a rule - false', () => {
const input = { a : undefined }
const schema = { a : null }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('`any` safeguard against `null`', () => {
const input = { a : null }
const schema = { a : 'any' }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('`any` safeguard against `undefined`', () => {
const input = { a : undefined }
const schema = { a : 'any' }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('type can be `"any"`', () => {
const input = { a : () => {} }
const schema = { a : 'any' }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('type can be `"function"`', () => {
const input = { a : () => {} }
const schema = { a : 'function' }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('type can be `promise`', () => {
const input = {
a : delay(1999),
b : async () => {},
}
const schema = {
a : 'promise',
b : 'promise',
}
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('type can be `promise` list', () => {
const input = { a : [ delay(1999) ] }
const schema = { a : [ 'promise' ] }
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('function as schema - false', () => {
const input = {
a : {
ab : () => true,
ac : 3,
},
c : [ 1, 2 ],
}
const schema = {
'a' : {
ab : /fo/,
ac : 'number',
},
'b?' : 'string',
'c' : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('regex ok', () => {
const input = {
a : {
ab : 'foo',
ac : 3,
},
c : [ 1, 2 ],
}
const schema = {
'a' : {
ab : /fo/,
ac : 'number',
},
'b?' : 'string',
'c' : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('regex !ok', () => {
const input = {
a : {
ab : 'foo',
ac : 3,
},
c : [ 1, 2 ],
}
const schema = {
'a' : {
ab : /ba/,
ac : 'number',
},
'b?' : 'string',
'c' : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('optional props is missing', () => {
const input = {
a : {
ab : 'foo',
ac : 3,
},
c : [ 1, 2 ],
}
const schema = {
'a' : {
ab : 'string',
ac : 'number',
},
'b?' : 'string',
'c' : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('optional props is wrong type', () => {
const input = {
a : {
ab : 'foo',
ac : 3,
},
b : [],
c : [ 1, 2 ],
}
const schema = {
'a' : {
ab : 'string',
ac : 'number',
},
'b?' : 'string',
'c' : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('optional props - nested', () => {
const input = {
a : {
ab : 'foo',
ac : 3,
},
b : [],
c : [ 1, 2 ],
}
const schema = {
a : {
'ab' : 'string',
'ac?' : 'number',
},
b : 'array',
c : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('optional props is missing - nested', () => {
const input = {
a : { ab : 'foo' },
b : [],
c : [ 1, 2 ],
}
const schema = {
a : {
'ab' : 'string',
'ac?' : 'number',
},
b : 'array',
c : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('optional props is wrong type - nested', () => {
const input = {
a : {
ab : 'foo',
ac : 'bar',
},
b : [],
c : [ 1, 2 ],
}
const schema = {
a : {
'ab' : 'string',
'ac?' : 'number',
},
b : 'array',
c : [ 'number' ],
}
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('nested schema', () => {
const input = {
a : {
b : 'str',
c : 3,
d : 'str',
},
b : 'foo',
}
const schema = {
a : {
b : 'string',
c : 'number',
d : 'string',
},
b : 'string',
}
expect(isValid({
input,
schema,
})).toBeTruthy()
const invalidInputFirst = {
a : {
b : 'str',
c : 3,
d : 'str',
},
b : 5,
}
expect(isValid({
input : invalidInputFirst,
schema,
})).toBeFalsy()
const invalidInputSecond = {
a : {
b : 'str',
c : 'str',
d : 'str',
},
b : 5,
}
expect(isValid({
input : invalidInputSecond,
schema,
})).toBeFalsy()
const invalidInputThird = {
a : { b : 'str' },
b : 5,
}
expect(isValid({
input : invalidInputThird,
schema,
})).toBeFalsy()
})
test('array of type', () => {
const input = {
a : [ 1, 2 ],
b : 'foo',
}
const schema = {
a : [ 'number' ],
b : 'string',
}
expect(isValid({
input,
schema,
})).toBeTruthy()
const invalidInput = {
a : [ 1, '1' ],
b : 'foo',
}
expect(isValid({
input : invalidInput,
schema,
})).toBeFalsy()
})
test('function as rule', () => {
const input = {
a : [ 1, 2, 3, 4 ],
b : 'foo',
}
const invalidInput = {
a : [ 4 ],
b : 'foo',
}
const schema = {
a : x => x.length > 2,
b : 'string',
}
expect(isValid({
input,
schema,
})).toBeTruthy()
expect(isValid({
input : invalidInput,
schema,
})).toBeFalsy()
})
test('input prop is undefined', () => {
const input = { b : 3 }
const schema = { a : 'number' }
expect(isValid({
input,
schema,
})).toBeFalsy()
})
test('enum', () => {
const input = { a : 'foo' }
const invalidInput = { a : '' }
const schema = { a : [ 'foo', 'bar', 'baz' ] }
expect(isValid({
input,
schema,
})).toBeTruthy()
expect(isValid({
input : invalidInput,
schema,
})).toBeFalsy()
})
test('readme example', () => {
const basicSchema = { a : [ 'string' ] }
const schema = {
b : [ basicSchema ],
c : {
d : { e : 'boolean' },
f : 'array',
},
g : [ 'foo', 'bar', 'baz' ],
}
const input = {
b : [ { a : [ 'led', 'zeppelin' ] } ],
c : {
d : { e : true },
f : [ 'any', 1, null, 'value' ],
},
g : 'foo',
}
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('should allow additional properties', () => {
const input = {
title : 'You shook me',
year : 1969,
}
expect(isValid({
input,
schema : { title : 'string' },
})).toBeTruthy()
})
test('accepts values as schemas', () => {
const input = {
title : 'You shook me',
genre : 'Blues',
year : 1969,
}
const schema = {
title : 'You shook me',
year : 1969,
}
expect(isValid({
input,
schema,
})).toBeTruthy()
})
test('compatible schemas with nested object', () => {
const input = {
foo : 'bar',
baz : { a : { b : 'c' } },
}
const invalidInputFirst = {
foo : 'bar',
baz : { a : { b : 1 } },
}
const invalidInputSecond = {
foo : 'bar',
baz : { a : { b : [] } },
}
const invalidInputThird = {
foo : 'bar',
baz : { a : { b : null } },
}
const schema = {
foo : 'string',
baz : { a : { b : 'string' } },
}
expect(isValid({
input,
schema,
})).toBeTruthy()
expect(isValid({
input : invalidInputFirst,
schema,
})).toBeFalsy()
expect(isValid({
input : invalidInputSecond,
schema,
})).toBeFalsy()
expect(isValid({
input : invalidInputThird,
schema,
})).toBeFalsy()
})
test('should return true when schema is empty object', () => {
expect(isValid({
input : { a : 1 },
schema : {},
})).toBeTruthy()
})
test('when schema is undefined', () => {
expect(isValid({
input : { a : 1 },
schema : undefined,
})).toBeFalsy()
})
test('should return false with invalid schema rule', () => {
const input = {
foo : 'bar',
a : {},
}
const inputSecond = { foo : 'bar' }
const schema = {
foo : 'string',
baz : { a : {} },
}
expect(isValid({
input,
schema,
})).toBeFalsy()
expect(isValid({
input : inputSecond,
schema,
})).toBeFalsy()
})
test('array of schemas', () => {
const input = {
b : [
{
a : 'led',
b : 1,
},
{
a : 'dancing',
b : 1,
},
],
}
const basicSchema = {
a : String,
b : Number,
}
const schema = { b : [ basicSchema ] }
const result = isValid({
input,
schema,
})
expect(result).toBeTruthy()
})
isValidAsync(x: IsValidAsync): Promise<boolean>
Asynchronous version of R.isValid
const input = {a: 1, b: 2}
const invalidInput = {a: 1, b: 'foo'}
const schema = {a: Number, b: async x => {
await R.delay(100)
return typeof x === 'number'
}}
const result = await Promise.all([
R.isValidAsync({schema, input}),
R.isValidAsync({schema, input: invalidInput})
])
// => [true, false]
Try this R.isValidAsync example in Rambda REPL
import { forEach } from './forEach.js'
import { isPromise } from './isPromise.js'
import { isValid } from './isValid.js'
export async function isValidAsync({ schema, input }){
const asyncSchema = {}
const simpleSchema = {}
forEach((rule, prop) => {
if (isPromise(rule)){
asyncSchema[ prop ] = rule
} else {
simpleSchema[ prop ] = rule
}
}, schema)
if (Object.keys(asyncSchema).length === 0)
return isValid({
input,
schema,
})
if (
!isValid({
input,
schema : simpleSchema,
})
)
return false
let toReturn = true
for (const singleRuleProp in asyncSchema){
if (toReturn){
const validated = await asyncSchema[ singleRuleProp ](input[ singleRuleProp ])
if (!validated) toReturn = false
}
}
return toReturn
}
import { result } from 'lodash'
import { delay } from './delay.js'
import { isValidAsync } from './isValidAsync.js'
const simplePredicate = async x => {
await delay(100)
return x > 5
}
test('happy', async () => {
const input = {
a : 1,
b : 7,
c : 9,
additional : 'foo',
}
const invalidInput = {
a : 1,
b : 2,
c : 9,
}
const schema = {
a : Number,
b : simplePredicate,
c : simplePredicate,
}
const invalidSchema = {
a : Boolean,
b : simplePredicate,
c : simplePredicate,
}
const result = await isValidAsync({
input,
schema,
})
const invalidResult = await isValidAsync({
input,
schema : invalidSchema,
})
const withInvalidInput = await isValidAsync({
input : invalidInput,
schema,
})
expect(result).toBeTruthy()
expect(invalidResult).toBeFalsy()
expect(withInvalidInput).toBeFalsy()
})
test('without async rules', async () => {
const input = {
a : 1,
b : 7,
}
const schema = {
a : Number,
b : x => x > 2,
}
const invalidSchema = {
a : Number,
b : Boolean,
}
const result = await isValidAsync({
input,
schema,
})
const invalidResult = await isValidAsync({
input,
schema : invalidSchema,
})
expect(result).toBeTruthy()
expect(invalidResult).toBeFalsy()
})
test('readme example', async () => {
const input = {
a : 1,
b : 2,
}
const invalidInput = {
a : 1,
b : 'foo',
}
const schema = {
a : Number,
b : async x => {
await delay(100)
return typeof x === 'number'
},
}
const result = await Promise.all([
isValidAsync({
schema,
input,
}),
isValidAsync({
schema,
input : invalidInput,
}),
])
expect(result).toEqual([ true, false ])
})
join<T>(glue: string, list: T[]): string
It returns a string of all list
instances joined with a glue
.
R.join('-', [1, 2, 3]) // => '1-2-3'
Try this R.join example in Rambda REPL
export function join(glue, list){
if (arguments.length === 1) return _list => join(glue, _list)
return list.join(glue)
}
import { join } from './join.js'
test('curry', () => {
expect(join('|')([ 'foo', 'bar', 'baz' ])).toBe('foo|bar|baz')
expect(join('|', [ 1, 2, 3 ])).toBe('1|2|3')
const spacer = join(' ')
expect(spacer([ 'a', 2, 3.4 ])).toBe('a 2 3.4')
})
juxt<A extends any[], R1>(fns: [(...a: A) => R1]): (...a: A) => [R1]
It applies list of function to a list of inputs.
const getRange = juxt([ Math.min, Math.max, Math.min ])
const result = getRange(
3, 4, 9, -3
)
// => [-3, 9, -3]
Try this R.juxt example in Rambda REPL
export function juxt(listOfFunctions){
return (...args) => listOfFunctions.map(fn => fn(...args))
}
import { juxt } from './juxt.js'
test('happy', () => {
const fn = juxt([ Math.min, Math.max, Math.min ])
const result = fn(
3, 4, 9, -3
)
expect(result).toEqual([ -3, 9, -3 ])
})
keys<T extends object>(x: T): (keyof T & string)[]
It applies Object.keys
over x
and returns its keys.
R.keys({a:1, b:2}) // => ['a', 'b']
Try this R.keys example in Rambda REPL
export function keys(x){
return Object.keys(x)
}
import { keys } from './keys.js'
test('happy', () => {
expect(keys({ a : 1 })).toEqual([ 'a' ])
})
last(str: ''): undefined
It returns the last element of input
, as the input
can be either a string or an array. It returns undefined
if array has length of 0.
const result = [
R.last([1, 2, 3]),
R.last('foo'),
]
// => [3, 'o']
Try this R.last example in Rambda REPL
export function last(listOrString){
if (typeof listOrString === 'string'){
return listOrString[ listOrString.length - 1 ] || ''
}
return listOrString[ listOrString.length - 1 ]
}
import { last } from './last.js'
test('with list', () => {
expect(last([ 1, 2, 3 ])).toBe(3)
expect(last([])).toBeUndefined()
})
test('with string', () => {
expect(last('abc')).toBe('c')
expect(last('')).toBe('')
})
lastIndexOf<T>(target: T, list: T[]): number
It returns the last index of target
in list
array.
R.equals
is used to determine equality between target
and members of list
.
If there is no such index, then -1
is returned.
const list = [1, 2, 3, 1, 2, 3]
const result = [
R.lastIndexOf(2, list),
R.lastIndexOf(4, list),
]
// => [4, -1]
Try this R.lastIndexOf example in Rambda REPL
import { _lastIndexOf } from './equals.js'
export function lastIndexOf(valueToFind, list){
if (arguments.length === 1){
return _list => _lastIndexOf(valueToFind, _list)
}
return _lastIndexOf(valueToFind, list)
}
import { lastIndexOf as lastIndexOfRamda } from 'ramda'
import { compareCombinations } from './_internals/testUtils.js'
import { possibleIterables, possibleTargets } from './indexOf.spec.js'
import { lastIndexOf } from './lastIndexOf.js'
test('with NaN', () => {
expect(lastIndexOf(NaN, [ NaN ])).toBe(0)
})
test('will throw with bad input', () => {
expect(lastIndexOfRamda([], true)).toBe(-1)
expect(() => indexOf([], true)).toThrowErrorMatchingInlineSnapshot('"indexOf is not defined"')
})
test('without list of objects - no R.equals', () => {
expect(lastIndexOf(3, [ 1, 2, 3, 4 ])).toBe(2)
expect(lastIndexOf(10)([ 1, 2, 3, 4 ])).toBe(-1)
})
test('list of objects uses R.equals', () => {
const listOfObjects = [ { a : 1 }, { b : 2 }, { c : 3 } ]
expect(lastIndexOf({ c : 4 }, listOfObjects)).toBe(-1)
expect(lastIndexOf({ c : 3 }, listOfObjects)).toBe(2)
})
test('list of arrays uses R.equals', () => {
const listOfLists = [ [ 1 ], [ 2, 3 ], [ 2, 3, 4 ], [ 2, 3 ], [ 1 ], [] ]
expect(lastIndexOf([], listOfLists)).toBe(5)
expect(lastIndexOf([ 1 ], listOfLists)).toBe(4)
expect(lastIndexOf([ 2, 3, 4 ], listOfLists)).toBe(2)
expect(lastIndexOf([ 2, 3, 5 ], listOfLists)).toBe(-1)
})
test('with string as iterable', () => {
expect(() => lastIndexOf('a', 'abc')).toThrowErrorMatchingInlineSnapshot('"Cannot read property \'indexOf\' of abc"')
expect(lastIndexOfRamda('a', 'abc')).toBe(0)
})
describe('brute force', () => {
compareCombinations({
fn : lastIndexOf,
fnRamda : lastIndexOfRamda,
firstInput : possibleTargets,
secondInput : possibleIterables,
callback : errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
{
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 34,
"RESULTS_MISMATCH": 0,
"SHOULD_NOT_THROW": 51,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 170,
}
`)
},
})
})
length<T>(input: T[]): number
It returns the length
property of list or string input
.
const result = [
R.length([1, 2, 3, 4]),
R.length('foo'),
]
// => [4, 3]
Try this R.length example in Rambda REPL
import { isArray } from './_internals/isArray.js'
export function length(x){
if (isArray(x)) return x.length
if (typeof x === 'string') return x.length
return NaN
}
import { length as lengthRamda } from 'ramda'
import { length } from './length.js'
test('happy', () => {
expect(length('foo')).toBe(3)
expect(length([ 1, 2, 3 ])).toBe(3)
expect(length([])).toBe(0)
})
test('with empty string', () => {
expect(length('')).toBe(0)
})
test('with bad input returns NaN', () => {
expect(length(0)).toBeNaN()
expect(length({})).toBeNaN()
expect(length(null)).toBeNaN()
expect(length(undefined)).toBeNaN()
})
test('with length as property', () => {
const input1 = { length : '123' }
const input2 = { length : null }
const input3 = { length : '' }
expect(length(input1)).toBeNaN()
expect(lengthRamda(input1)).toBeNaN()
expect(length(input2)).toBeNaN()
expect(lengthRamda(input2)).toBeNaN()
expect(length(input3)).toBeNaN()
expect(lengthRamda(input3)).toBeNaN()
})
lens<S, A>(getter: (s: S) => A, setter: (a: A, s: S) => S): Lens<S, A>
It returns a lens
for the given getter
and setter
functions.
The getter
gets the value of the focus; the setter
sets the value of the focus.
The setter should not mutate the data structure.
const xLens = R.lens(R.prop('x'), R.assoc('x'));
R.view(xLens, {x: 1, y: 2}) // => 1
R.set(xLens, 4, {x: 1, y: 2}) // => {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2}) // => {x: -1, y: 2}
Try this R.lens example in Rambda REPL
export function lens(getter, setter){
return function (functor){
return function (target){
return functor(getter(target)).map(focus => setter(focus, target))
}
}
}
lensEq(lens: Function, value: any, data: any): boolean
It returns true
if data structure focused by the given lens equals to the target
value.
R.equals
is used to determine equality.
:boom: Idea for this method comes from
ramda-adjunct
library
const list = [ 1, 2, 3 ]
const lens = R.lensIndex(0)
const result = R.lensEq(
lens, 1, list
)
// => true
Try this R.lensEq example in Rambda REPL
import { curry } from './curry.js'
import { equals } from './equals.js'
import { view } from './view.js'
function lensEqFn(
lens, target, input
){
return equals(view(lens, input), target)
}
export const lensEq = curry(lensEqFn)
import { lensEq } from './lensEq.js'
import { lensIndex } from './lensIndex.js'
import { lensPath } from './lensPath.js'
test('with list', () => {
const list = [ 1, 2, 3 ]
const lens = lensIndex(0)
expect(lensEq(
lens, 1, list
)).toBeTrue()
expect(lensEq(lens, 2)(list)).toBeFalse()
})
test('with R.lensPath', () => {
const input = { a : { b : { c : 1 } } }
const target = { c : 1 }
const lens = lensPath('a.b')
expect(lensEq(lens)(target)(input)).toBeTrue()
expect(lensEq(
lens, target, { c : 2 }
)).toBeFalse()
})
lensIndex<A>(n: number): Lens<A[], A>
It returns a lens that focuses on specified index
.
const list = ['a', 'b', 'c']
const headLens = R.lensIndex(0)
R.view(headLens, list) // => 'a'
R.set(headLens, 'x', list) // => ['x', 'b', 'c']
R.over(headLens, R.toUpper, list) // => ['A', 'b', 'c']
Try this R.lensIndex example in Rambda REPL
import { lens } from './lens.js'
import { nth } from './nth.js'
import { update } from './update.js'
export function lensIndex(index){
return lens(nth(index), update(index))
}
import { compose } from './compose.js'
import { keys } from './keys.js'
import { lensIndex } from './lensIndex.js'
import { over } from './over.js'
import { set } from './set.js'
import { view } from './view.js'
const testList = [ { a : 1 }, { b : 2 }, { c : 3 } ]
test('focuses list element at the specified index', () => {
expect(view(lensIndex(0), testList)).toEqual({ a : 1 })
})
test('returns undefined if the specified index does not exist', () => {
expect(view(lensIndex(10), testList)).toBeUndefined()
})
test('sets the list value at the specified index', () => {
expect(set(
lensIndex(0), 0, testList
)).toEqual([ 0, { b : 2 }, { c : 3 } ])
})
test('applies function to the value at the specified list index', () => {
expect(over(
lensIndex(2), keys, testList
)).toEqual([ { a : 1 }, { b : 2 }, [ 'c' ] ])
})
test('can be composed', () => {
const nestedList = [ 0, [ 10, 11, 12 ], 1, 2 ]
const composedLens = compose(lensIndex(1), lensIndex(0))
expect(view(composedLens, nestedList)).toBe(10)
})
test('set s (get s) === s', () => {
expect(set(
lensIndex(0), view(lensIndex(0), testList), testList
)).toEqual(testList)
})
test('get (set s v) === v', () => {
expect(view(lensIndex(0), set(
lensIndex(0), 0, testList
))).toBe(0)
})
test('get (set(set s v1) v2) === v2', () => {
expect(view(lensIndex(0),
set(
lensIndex(0), 11, set(
lensIndex(0), 10, testList
)
))).toBe(11)
})
It returns a lens that focuses on specified path
.
Try this R.lensPath example in Rambda REPL
lensProp<S, K extends keyof S = keyof S>(prop: K): Lens<S, S[K]>
It returns a lens that focuses on specified property prop
.
const xLens = R.lensProp('x');
const input = {x: 1, y: 2}
R.view(xLens, input) // => 1
R.set(xLens, 4, input)
// => {x: 4, y: 2}
R.over(xLens, R.negate, input)
// => {x: -1, y: 2}
Try this R.lensProp example in Rambda REPL
import { assoc } from './assoc.js'
import { lens } from './lens.js'
import { prop } from './prop.js'
export function lensProp(key){
return lens(prop(key), assoc(key))
}
import { compose } from './compose.js'
import { identity } from './identity.js'
import { inc } from './inc.js'
import { lensProp } from './lensProp.js'
import { over } from './over.js'
import { set } from './set.js'
import { view } from './view.js'
const testObj = {
a : 1,
b : 2,
c : 3,
}
test('focuses object the specified object property', () => {
expect(view(lensProp('a'), testObj)).toBe(1)
})
test('returns undefined if the specified property does not exist', () => {
expect(view(lensProp('X'), testObj)).toBeUndefined()
})
test('sets the value of the object property specified', () => {
expect(set(
lensProp('a'), 0, testObj
)).toEqual({
a : 0,
b : 2,
c : 3,
})
})
test('adds the property to the object if it doesn\'t exist', () => {
expect(set(
lensProp('d'), 4, testObj
)).toEqual({
a : 1,
b : 2,
c : 3,
d : 4,
})
})
test('applies function to the value of the specified object property', () => {
expect(over(
lensProp('a'), inc, testObj
)).toEqual({
a : 2,
b : 2,
c : 3,
})
})
test('applies function to undefined and adds the property if it doesn\'t exist', () => {
expect(over(
lensProp('X'), identity, testObj
)).toEqual({
a : 1,
b : 2,
c : 3,
X : undefined,
})
})
test('can be composed', () => {
const nestedObj = {
a : { b : 1 },
c : 2,
}
const composedLens = compose(lensProp('a'), lensProp('b'))
expect(view(composedLens, nestedObj)).toBe(1)
})
test('set s (get s) === s', () => {
expect(set(
lensProp('a'), view(lensProp('a'), testObj), testObj
)).toEqual(testObj)
})
test('get (set s v) === v', () => {
expect(view(lensProp('a'), set(
lensProp('a'), 0, testObj
))).toBe(0)
})
test('get (set(set s v1) v2) === v2', () => {
expect(view(lensProp('a'),
set(
lensProp('a'), 11, set(
lensProp('a'), 10, testObj
)
))).toBe(11)
})
lensSatisfies<PredicateInput, Input>(predicate: (x: PredicateInput) => boolean, lens: Lens<PredicateInput, Input>, input: Input): boolean
It returns true
if data structure focused by the given lens satisfies the predicate.
:boom: Idea for this method comes from
ramda-adjunct
library
const fn = R.lensSatisfies(x => x > 5, R.lensIndex(0))
const result = [
fn([10, 20, 30]),
fn([1, 2, 3]),
]
// => [true, false]
Try this R.lensSatisfies example in Rambda REPL
import { curry } from './curry.js'
import { view } from './view.js'
function lensSatisfiesFn(
predicate, lens, input
){
return Boolean(predicate(view(lens, input)))
}
export const lensSatisfies = curry(lensSatisfiesFn)
import { lensIndex } from './lensIndex.js'
import { lensPath } from './lensPath.js'
import { lensSatisfies } from './lensSatisfies.js'
const predicate = x => x > 1
test('with list', () => {
const lens = lensIndex(0)
const fn = lensSatisfies(predicate, lens)
expect(fn([ 10, 20, 30 ])).toBeTrue()
expect(fn([ 1, 2, 3 ])).toBeFalse()
})
test('with R.lensPath', () => {
const input1 = { a : { b : 10 } }
const input2 = { a : { b : 1 } }
const lens = lensPath('a.b')
const fn = lensSatisfies(predicate, lens)
expect(fn(input1)).toBeTrue()
expect(fn(input2)).toBeFalse()
})
Try this R.lt example in Rambda REPL
Try this R.lte example in Rambda REPL
map<T, U>(fn: ObjectIterator<T, U>, iterable: Dictionary<T>): Dictionary<U>
It returns the result of looping through iterable
with fn
.
It works with both array and object.
:boom: Unlike Ramda's
map
, here property and input object are passed as arguments tofn
, wheniterable
is an object.
const fn = x => x * 2
const fnWhenObject = (val, prop)=>{
return `${prop}-${val}`
}
const iterable = [1, 2]
const obj = {a: 1, b: 2}
const result = [
R.map(fn, list),
R.map(fnWhenObject, Record<string, unknown>)
]
// => [ [1, 4], {a: 'a-1', b: 'b-2'}]
Try this R.map example in Rambda REPL
import { INCORRECT_ITERABLE_INPUT } from './_internals/constants.js'
import { isArray } from './_internals/isArray.js'
import { keys } from './_internals/keys.js'
export function mapArray(
fn, list, isIndexed = false
){
let index = 0
const willReturn = Array(list.length)
while (index < list.length){
willReturn[ index ] = isIndexed ? fn(list[ index ], index) : fn(list[ index ])
index++
}
return willReturn
}
export function mapObject(fn, obj){
if (arguments.length === 1){
return _obj => mapObject(fn, _obj)
}
let index = 0
const objKeys = keys(obj)
const len = objKeys.length
const willReturn = {}
while (index < len){
const key = objKeys[ index ]
willReturn[ key ] = fn(
obj[ key ], key, obj
)
index++
}
return willReturn
}
export const mapObjIndexed = mapObject
export function map(fn, iterable){
if (arguments.length === 1) return _iterable => map(fn, _iterable)
if (!iterable){
throw new Error(INCORRECT_ITERABLE_INPUT)
}
if (isArray(iterable)) return mapArray(fn, iterable)
return mapObject(fn, iterable)
}
import { map as mapRamda } from 'ramda'
import { map } from './map.js'
const double = x => x * 2
describe('with array', () => {
it('happy', () => {
expect(map(double, [ 1, 2, 3 ])).toEqual([ 2, 4, 6 ])
})
it('curried', () => {
expect(map(double)([ 1, 2, 3 ])).toEqual([ 2, 4, 6 ])
})
})
describe('with object', () => {
const obj = {
a : 1,
b : 2,
}
it('happy', () => {
expect(map(double, obj)).toEqual({
a : 2,
b : 4,
})
})
it('property as second and input object as third argument', () => {
const obj = {
a : 1,
b : 2,
}
const iterator = (
val, prop, inputObject
) => {
expect(prop).toBeString()
expect(inputObject).toEqual(obj)
return val * 2
}
expect(map(iterator)(obj)).toEqual({
a : 2,
b : 4,
})
})
})
test('bad inputs difference between Ramda and Rambda', () => {
expect(() => map(double, null)).toThrowErrorMatchingInlineSnapshot('"Incorrect iterable input"')
expect(() => map(double)(undefined)).toThrowErrorMatchingInlineSnapshot('"Incorrect iterable input"')
expect(() => mapRamda(double, null)).toThrowErrorMatchingInlineSnapshot('"Cannot read properties of null (reading \'fantasy-land/map\')"')
expect(() =>
mapRamda(double, undefined)).toThrowErrorMatchingInlineSnapshot('"Cannot read properties of undefined (reading \'fantasy-land/map\')"')
})
mapArray<T>(fn: Iterator<T, T>, iterable: T[]): T[]
const result = R.mapArray(x => x + 1, [1, 2])
// => [2, 3]
Try this R.mapArray example in Rambda REPL
mapAsync<T, K>(fn: AsyncIterable<T, K>, list: T[]): Promise<K[]>
Sequential asynchronous mapping with fn
over members of list
.
async function fn(x){
await R.delay(1000)
return x+1
}
const result = await R.mapAsync(fn, [1, 2, 3])
// `result` resolves after 3 seconds to `[2, 3, 4]`
Try this R.mapAsync example in Rambda REPL
import { isArray } from './_internals/isArray.js'
async function mapAsyncFn(fn, listOrObject){
if (isArray(listOrObject)){
const willReturn = []
let i = 0
for (const a of listOrObject){
willReturn.push(await fn(a, i++))
}
return willReturn
}
const willReturn = {}
for (const prop in listOrObject){
willReturn[ prop ] = await fn(listOrObject[ prop ], prop)
}
return willReturn
}
export function mapAsync(fn, listOrObject){
if (arguments.length === 1){
return async _listOrObject => mapAsyncFn(fn, _listOrObject)
}
return new Promise((resolve, reject) => {
mapAsyncFn(fn, listOrObject).then(resolve)
.catch(reject)
})
}
import { composeAsync } from './composeAsync.js'
import { delay } from './delay.js'
import { map } from './map.js'
import { mapAsync } from './mapAsync.js'
const rejectDelay = a =>
new Promise((_, reject) => {
setTimeout(() => {
reject(a + 20)
}, 100)
})
test('happy', async () => {
const fn = async (x, prop) => {
await delay(100)
expect(prop).toBeNumber()
return x + 1
}
const result = await mapAsync(fn, [ 1, 2, 3 ])
expect(result).toEqual([ 2, 3, 4 ])
})
test('with object', async () => {
const fn = async (x, prop) => {
expect(prop).toBeString()
return x + 1
}
const result = await mapAsync(fn, {
a : 1,
b : 2,
})
expect(result).toEqual({
a : 2,
b : 3,
})
})
test('with R.composeAsync', async () => {
const result = await composeAsync(
map(x => x + 1),
mapAsync(async x => {
delay(x)
return x
}),
map(x => x * 10)
)([ 1, 2, 3 ])
expect(result).toEqual([ 11, 21, 31 ])
})
test('error', async () => {
try {
await mapAsync(rejectDelay)([ 1, 2, 3 ])
} catch (err){
expect(err).toBe(21)
}
})
Try this R.mapcat example in Rambda REPL
Same as R.map
, but it passes index as second argument to the iterator, when looping over arrays.
mapKeys<T, U>(changeKeyFn: (x: string) => string, obj: { [key: string]: T}): U
It takes an object and returns a new object with changed keys according to changeKeyFn
function.
const obj = {a: 1, b: 2}
const changeKeyFn = prop => `{prop}_foo`
const result = R.mapKeys(changeKeyFn, Record<string, unknown>)
// => {a_foo: 1, b_foo: 2}
Try this R.mapKeys example in Rambda REPL
export function mapKeys(changeKeyFn, obj){
if (arguments.length === 1) return _obj => mapKeys(changeKeyFn, _obj)
const toReturn = {}
Object.keys(obj).forEach(prop => toReturn[ changeKeyFn(prop) ] = obj[ prop ])
return toReturn
}
import { mapKeys } from './mapKeys.js'
const obj = {
a : 1,
b : 2,
}
const changeKeyFn = prop => `${ prop }_foo`
const expected = {
a_foo : 1,
b_foo : 2,
}
test('happy', () => {
const result = mapKeys(changeKeyFn, obj)
expect(result).toEqual(expected)
})
test('curried', () => {
const result = mapKeys(changeKeyFn)(obj)
expect(result).toEqual(expected)
})
mapObject<T>(fn: ObjectIterator<T, T>, iterable: Dictionary<T>): Dictionary<T>
const result = R.mapObject(x => x + 1, {a:1, b:2})
// => {a:2, b:3}
Try this R.mapObject example in Rambda REPL
It works the same way as R.map
does for objects. It is added as Ramda also has this method.
Try this R.mapObjIndexed example in Rambda REPL
mapParallelAsync<T, K>(fn: AsyncIterable<T, K>, list: T[]): Promise<K[]>
Parallel asynchronous mapping with fn
over members of list
.
async function fn(x){
await R.delay(1000)
return x+1
}
const result = await R.mapParallelAsync(fn, [1, 2, 3])
// `result` resolves after 1 second to `[2, 3, 4]`
Try this R.mapParallelAsync example in Rambda REPL
export async function mapParallelAsyncFn(fn, arr){
const promised = arr.map((a, i) => fn(a, i))
return Promise.all(promised)
}
export function mapParallelAsync(fn, arr){
if (arguments.length === 1){
return async holder => mapParallelAsyncFn(fn, holder)
}
return new Promise((resolve, reject) => {
mapParallelAsyncFn(fn, arr).then(resolve)
.catch(reject)
})
}
import { willFailAssertion } from './_internals/testUtils.js'
import { composeAsync } from './composeAsync.js'
import { delay } from './delay.js'
import { map } from './map.js'
import { mapParallelAsync } from './mapParallelAsync.js'
test('happy', async () => {
const fn = async x => {
await delay(100)
return x + 10
}
const result = await mapParallelAsync(fn, [ 1, 2, 3 ])
expect(result).toEqual([ 11, 12, 13 ])
})
test('composeAsync', async () => {
const result = await composeAsync(
mapParallelAsync(async x => {
await delay(100)
return x + 1
}),
mapParallelAsync(async x => {
await delay(100)
return x + 10
}),
map(x => x * 10)
)([ 1, 2, 3 ])
expect(result).toEqual([ 21, 31, 41 ])
})
test('error', async () => {
try {
const fn = async () => {
JSON.parse('{:')
}
await mapParallelAsync(fn, [ 1, 2, 3 ])
willFailAssertion()
} catch (err){
expect(err.message).toBeTruthy()
}
})
test('pass index as second argument', async () => {
await mapParallelAsync((x, i) => {
expect(x % 10).toBe(0)
expect(typeof i).toBe('number')
},
[ 10, 20, 30 ])
})
mapParallelAsyncWithLimit<T, K>(fn: AsyncIterable<T, K>, limit: number, list: T[]): Promise<K[]>
It is similar to R.mapParallelAsync
in that it uses Promise.all
, but not over the whole list, rather than with only slice from list
with length limit
.
:boom: For example usage, please check
R.mapAsyncLimit
tests.
import { mapParallelAsync, mapParallelAsyncFn } from './mapParallelAsync.js'
import { splitEvery } from './splitEvery.js'
async function mapParallelAsyncWithLimitFn(
iterable, limit, list
){
if (list.length < limit) return mapParallelAsync(iterable, list)
const slices = splitEvery(limit, list)
let toReturn = []
for (const slice of slices){
const iterableResult = await mapParallelAsyncFn(iterable, slice)
toReturn = [ ...toReturn, ...iterableResult ]
}
return toReturn
}
export function mapParallelAsyncWithLimit(
iterable, limit, list
){
if (arguments.length === 2){
return async _list => mapParallelAsyncWithLimitFn(
iterable, limit, _list
)
}
return new Promise((resolve, reject) => {
mapParallelAsyncWithLimitFn(
iterable, limit, list
)
.then(resolve)
.catch(reject)
})
}
import isCI from 'is-ci'
import { composeAsync } from './composeAsync.js'
import { delay } from './delay.js'
import { mapAsync } from './mapAsync.js'
import { mapParallelAsyncWithLimit } from './mapParallelAsyncWithLimit.js'
import { toDecimal } from './toDecimal.js'
jest.setTimeout(30000)
test('happy', async () => {
const limit = 3
const startTime = new Date().getTime()
const list = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
const iterable = async x => {
await delay(500)
return x + 1
}
const result = await mapParallelAsyncWithLimit(
iterable, limit, list
)
const endTime = new Date().getTime()
const diffTime = endTime - startTime
const startTime2 = new Date().getTime()
await mapAsync(iterable, list)
const endTime2 = new Date().getTime()
const diffTime2 = endTime2 - startTime2
const methodScale = toDecimal((diffTime2 - diffTime) / 1000, 0)
expect(result).toEqual([ 2, 3, 4, 5, 6, 7, 8, 9, 10 ])
if (!isCI) expect(methodScale).toBe(limit)
})
const fn = async x => {
await delay(100)
return x + 1
}
test('with R.composeAsync', async () => {
const result = await composeAsync(mapParallelAsyncWithLimit(fn, 2), x =>
x.map(xx => xx + 1))([ 1, 2, 3, 4, 5, 6 ])
expect(result).toEqual([ 3, 4, 5, 6, 7, 8 ])
})
test('fallback to R.mapFastAsync', async () => {
const result = await mapParallelAsyncWithLimit(
fn, 4, [ 1, 2, 3 ]
)
expect(result).toEqual([ 2, 3, 4 ])
})
mapToObject<T, U extends object>(fn: (input: T) => U|false, list: readonly T[]): U
This method allows to generate an object from a list using input function fn
.
This function must return either an object or false
for every member of list
input.
If false
is returned, then this element of list
will be skipped in the calculation of the result.
All of returned objects will be merged to generate the final result.
const list = [1, 2, 3, 12]
const fn = x => {
if(x > 10) return false
return x % 2 ? {[x]: x + 1}: {[x]: x + 10}
}
const result = mapToObject(fn, list)
const expected = {'1': 2, '2': 12, '3': 4}
// => `result` is equal to `expected`
Try this R.mapToObject example in Rambda REPL
import { map } from './map.js'
import { mergeAll } from './mergeAll.js'
import { ok } from './ok.js'
import { type } from './type.js'
export function mapToObject(fn, list){
if (arguments.length === 1){
return listHolder => mapToObject(fn, listHolder)
}
ok(type(fn), type(list))('Function', 'Array')
return mergeAll(map(fn, list))
}
import { mapToObject } from './mapToObject.js'
const list = [ 1, 2, 3 ]
const fn = x => x % 2 ? { [ x ] : x + 1 } : { [ x ] : x + 10 }
const expected = {
1 : 2,
2 : 12,
3 : 4,
}
test('happy', () => {
const result = mapToObject(fn, list)
expect(result).toEqual(expected)
})
test('curried', () => {
const result = mapToObject(fn)(list)
expect(result).toEqual(expected)
})
test('string.fn test', () => {
const list = [ 'auto', 'bar=false', 'foo', 'baz=1.5', 's=more', 'k=2' ]
const fn = x => {
const [ key, value ] = x.split('=')
if (value === undefined || value === 'true'){
return { [ key ] : true }
}
if (value === 'false'){
return { [ key ] : false }
}
if (Number.isNaN(Number(value))){
return { [ key ] : value }
}
return { [ key ] : Number(value) }
}
const expectedResult = {
auto : true,
foo : true,
bar : false,
baz : 1.5,
s : 'more',
k : 2,
}
const result = mapToObject(fn, list)
expect(result).toEqual(expectedResult)
})
test('bad path', () => {
expect(() => mapToObject(1, null)).toThrowErrorMatchingInlineSnapshot(`
"Failed R.ok -
reason: {"input":"Number","schema":"Function"}
all inputs: ["Number","Null"]
all schemas: ["Function","Array"]"
`)
})
mapToObjectAsync<T, U extends object>(fn: (input: T) => Promise<U|false>, list: readonly T[]): Promise<U>
Asynchronous version of R.mapToObject
import { mapAsync } from './mapAsync.js'
export async function mapToObjectAsyncFn(fn, list){
let toReturn = {}
const innerIterable = async x => {
const intermediateResult = await fn(x)
if (intermediateResult === false) return
toReturn = {
...toReturn,
...intermediateResult,
}
}
await mapAsync(innerIterable, list)
return toReturn
}
export function mapToObjectAsync(fn, list){
if (arguments.length === 1){
return async _list => mapToObjectAsyncFn(fn, _list)
}
return new Promise((resolve, reject) => {
mapToObjectAsyncFn(fn, list).then(resolve)
.catch(reject)
})
}
import { composeAsync } from './composeAsync.js'
import { delay } from './delay.js'
import { mapToObjectAsync } from './mapToObjectAsync.js'
const list = [ 1, 2, 3, 12 ]
const fn = async x => {
await delay(100)
if (x > 10) return false
return x % 2 ? { [ `key${ x }` ] : x + 1 } : { [ `key${ x }` ] : x + 10 }
}
const expected = {
key1 : 2,
key2 : 12,
key3 : 4,
}
test('happy', async () => {
const result = await mapToObjectAsync(fn, list)
expect(result).toEqual(expected)
})
test('with R.composeAsync', async () => {
const result = await composeAsync(mapToObjectAsync(fn), x =>
x.filter(xx => xx > 1))(list)
expect(result).toEqual({
key2 : 12,
key3 : 4,
})
})
match(regExpression: RegExp, str: string): string[]
Curried version of String.prototype.match
which returns empty array, when there is no match.
const result = [
R.match('a', 'foo'),
R.match(/([a-z]a)/g, 'bananas')
]
// => [[], ['ba', 'na', 'na']]
Try this R.match example in Rambda REPL
export function match(pattern, input){
if (arguments.length === 1) return _input => match(pattern, _input)
const willReturn = input.match(pattern)
return willReturn === null ? [] : willReturn
}
import { equals } from './equals.js'
import { match } from './match.js'
test('happy', () => {
expect(match(/a./g)('foo bar baz')).toEqual([ 'ar', 'az' ])
})
test('fallback', () => {
expect(match(/a./g)('foo')).toEqual([])
})
test('with string', () => {
expect(match('a', 'foo')).toEqual([])
expect(equals(match('o', 'foo'), [ 'o' ])).toBeTrue()
})
test('throwing', () => {
expect(() => {
match(/a./g, null)
}).toThrowErrorMatchingInlineSnapshot('"Cannot read properties of null (reading \'match\')"')
})
R.mathMod
behaves like the modulo operator should mathematically, unlike the %
operator (and by extension, R.modulo
). So while -17 % 5
is -2
, mathMod(-17, 5)
is 3
.
:boom: Explanation is taken from
Ramda
documentation site.
Try this R.mathMod example in Rambda REPL
It returns the greater value between x
and y
.
Try this R.max example in Rambda REPL
It returns the greater value between x
and y
according to compareFn
function.
Try this R.maxBy example in Rambda REPL
maybe<T>(ifRule: boolean, whenIf: T | Func<T>, whenElse: T | Func<T>): T
It acts as ternary operator and it is helpful when we have nested ternaries.
All of the inputs can be either direct values or anonymous functions. This is helpful if we don't want to evaluate certain paths as we can wrap this logic in a function.
const x = 4
const y = 8
const ifRule = x > 2
const whenIf = y > 10 ? 3 : 7
const whenElse = () => {
// just to show that it won't be evaluated
return JSON.parse('{a:')
}
const result = R.maybe(
ifRule,
whenIf,
whenElse,
)
// `result` is `7`
Try this R.maybe example in Rambda REPL
import { type } from './type.js'
export function maybe(
ifRule, whenIf, whenElse
){
const whenIfInput =
ifRule && type(whenIf) === 'Function' ? whenIf() : whenIf
const whenElseInput =
!ifRule && type(whenElse) === 'Function' ? whenElse() : whenElse
return ifRule ? whenIfInput : whenElseInput
}
import { maybe } from './maybe.js'
const WHEN_IF = 'WHEN_IF'
const WHEN_ELSE = 'WHEN_ELSE'
test('prevent type error', () => {
const x = 5
const y = null
const ifRule = x > 3
const result = maybe(
ifRule, WHEN_IF, () => y.a === 'foo'
)
expect(result).toBe(WHEN_IF)
})
test('whenElse is a function', () => {
const x = 2
const y = { a : 1 }
const ifRule = x > 3
const result = maybe(
ifRule, WHEN_IF, () => y.a === 'foo'
)
expect(result).toBeFalse()
})
test('whenIf', () => {
const x = 5
const ifRule = x > 3
const result = maybe(
ifRule, WHEN_IF, WHEN_ELSE
)
expect(result).toBe(WHEN_IF)
})
test('whenIf is a function', () => {
const x = 5
const ifRule = () => x > 3
const result = maybe(
ifRule, () => WHEN_IF, WHEN_ELSE
)
expect(result).toBe(WHEN_IF)
})
test('whenElse', () => {
const x = 1
const ifRule = x > 3
const result = maybe(
ifRule, WHEN_IF, WHEN_ELSE
)
expect(result).toBe(WHEN_ELSE)
})
mean(list: number[]): number
It returns the mean value of list
input.
R.mean([ 2, 7 ])
// => 4.5
Try this R.mean example in Rambda REPL
import { sum } from './sum.js'
export function mean(list){
return sum(list) / list.length
}
import { mean } from './mean.js'
test('happy', () => {
expect(mean([ 2, 7 ])).toBe(4.5)
})
test('with NaN', () => {
expect(mean([])).toBeNaN()
})
median(list: number[]): number
It returns the median value of list
input.
R.median([ 7, 2, 10, 9 ]) // => 8
Try this R.median example in Rambda REPL
import { mean } from './mean.js'
export function median(list){
const len = list.length
if (len === 0) return NaN
const width = 2 - len % 2
const idx = (len - width) / 2
return mean(Array.prototype.slice
.call(list, 0)
.sort((a, b) => {
if (a === b) return 0
return a < b ? -1 : 1
})
.slice(idx, idx + width))
}
import { median } from './median.js'
test('happy', () => {
expect(median([ 2 ])).toBe(2)
expect(median([ 7, 2, 10, 2, 9 ])).toBe(7)
})
test('with empty array', () => {
expect(median([])).toBeNaN()
})
When fn
is called for a second time with the same input, then the cache result is returned instead of calling again fn
.
Try this R.memoize example in Rambda REPL
Creates a new function that, when invoked, caches the result of calling fn for a given argument set and returns the result.
Try this R.memoizeWith example in Rambda REPL
Same as R.mergeRight
.
mergeAll<T>(list: object[]): T
It merges all objects of list
array sequentially and returns the result.
const list = [
{a: 1},
{b: 2},
{c: 3}
]
const result = R.mergeAll(list)
const expected = {
a: 1,
b: 2,
c: 3
}
// => `result` is equal to `expected`
Try this R.mergeAll example in Rambda REPL
import { map } from './map.js'
import { mergeRight } from './mergeRight.js'
export function mergeAll(arr){
let willReturn = {}
map(val => {
willReturn = mergeRight(willReturn, val)
}, arr)
return willReturn
}
import { mergeAll } from './mergeAll.js'
test('case 1', () => {
const arr = [ { a : 1 }, { b : 2 }, { c : 3 } ]
const expectedResult = {
a : 1,
b : 2,
c : 3,
}
expect(mergeAll(arr)).toEqual(expectedResult)
})
test('case 2', () => {
expect(mergeAll([ { foo : 1 }, { bar : 2 }, { baz : 3 } ])).toEqual({
foo : 1,
bar : 2,
baz : 3,
})
})
describe('acts as if nil values are simply empty objects', () => {
it('if the first object is nil', () => {
expect(mergeAll([ null, { foo : 1 }, { foo : 2 }, { bar : 2 } ])).toEqual({
foo : 2,
bar : 2,
})
})
it('if the last object is nil', () => {
expect(mergeAll([ { foo : 1 }, { foo : 2 }, { bar : 2 }, undefined ])).toEqual({
foo : 2,
bar : 2,
})
})
it('if an intermediate object is nil', () => {
expect(mergeAll([ { foo : 1 }, { foo : 2 }, null, { bar : 2 } ])).toEqual({
foo : 2,
bar : 2,
})
})
})
Try this R.mergeDeepLeft example in Rambda REPL
mergeDeepRight<Output>(target: object, newProps: object): Output
Creates a new object with the own properties of the first object merged with the own properties of the second object. If a key exists in both objects:
import { clone } from './clone.js'
import { type } from './type.js'
export function mergeDeepRight(target, source){
if (arguments.length === 1){
return sourceHolder => mergeDeepRight(target, sourceHolder)
}
const willReturn = clone(target)
Object.keys(source).forEach(key => {
if (type(source[ key ]) === 'Object'){
if (type(target[ key ]) === 'Object'){
willReturn[ key ] = mergeDeepRight(target[ key ], source[ key ])
} else {
willReturn[ key ] = source[ key ]
}
} else {
willReturn[ key ] = source[ key ]
}
})
return willReturn
}
import { mergeDeepRight } from './mergeDeepRight.js'
const student = {
name : 'foo',
age : 10,
contact : {
a : 1,
email : 'foo@example.com',
},
}
const teacher = {
age : 40,
contact : { email : 'baz@example.com' },
songs : { title : 'Remains the same' },
}
test('when merging object with lists inside them', () => {
const a = {
a : [ 1, 2, 3 ],
b : [ 4, 5, 6 ],
}
const b = {
a : [ 7, 8, 9 ],
b : [ 10, 11, 12 ],
}
const result = mergeDeepRight(a, b)
const expected = {
a : [ 7, 8, 9 ],
b : [ 10, 11, 12 ],
}
expect(result).toEqual(expected)
})
test('happy', () => {
const result = mergeDeepRight(student, teacher)
const curryResult = mergeDeepRight(student)(teacher)
const expected = {
age : 40,
name : 'foo',
contact : {
a : 1,
email : 'baz@example.com',
},
songs : { title : 'Remains the same' },
}
expect(result).toEqual(expected)
expect(curryResult).toEqual(expected)
})
test('issue 650', () => {
expect(Object.keys(mergeDeepRight({ a : () => {} }, { b : () => {} }))).toEqual([
'a',
'b',
])
})
test('ramda compatible test 1', () => {
const a = {
w : 1,
x : 2,
y : { z : 3 },
}
const b = {
a : 4,
b : 5,
c : { d : 6 },
}
const result = mergeDeepRight(a, b)
const expected = {
w : 1,
x : 2,
y : { z : 3 },
a : 4,
b : 5,
c : { d : 6 },
}
expect(result).toEqual(expected)
})
test('ramda compatible test 2', () => {
const a = {
a : {
b : 1,
c : 2,
},
y : 0,
}
const b = {
a : {
b : 3,
d : 4,
},
z : 0,
}
const result = mergeDeepRight(a, b)
const expected = {
a : {
b : 3,
c : 2,
d : 4,
},
y : 0,
z : 0,
}
expect(result).toEqual(expected)
})
test('ramda compatible test 3', () => {
const a = {
w : 1,
x : { y : 2 },
}
const result = mergeDeepRight(a, { x : { y : 3 } })
const expected = {
w : 1,
x : { y : 3 },
}
expect(result).toEqual(expected)
})
test('functions are not discarded', () => {
const obj = { foo : () => {} }
expect(typeof mergeDeepRight(obj, {}).foo).toBe('function')
})
mergeLeft<Output>(newProps: object, target: object): Output
Same as R.merge
, but in opposite direction.
const result = R.mergeLeft(
{a: 10},
{a: 1, b: 2}
)
// => {a:10, b: 2}
Try this R.mergeLeft example in Rambda REPL
import { mergeRight } from './mergeRight.js'
export function mergeLeft(x, y){
if (arguments.length === 1) return _y => mergeLeft(x, _y)
return mergeRight(y, x)
}
import { mergeLeft } from './mergeLeft.js'
const obj = {
foo : 1,
bar : 2,
}
test('happy', () => {
expect(mergeLeft({ bar : 20 }, obj)).toEqual({
foo : 1,
bar : 20,
})
})
test('curry', () => {
expect(mergeLeft({ baz : 3 })(obj)).toEqual({
foo : 1,
bar : 2,
baz : 3,
})
})
test('when undefined or null instead of object', () => {
expect(mergeLeft(null, undefined)).toEqual({})
expect(mergeLeft(obj, null)).toEqual(obj)
expect(mergeLeft(obj, undefined)).toEqual(obj)
expect(mergeLeft(undefined, obj)).toEqual(obj)
})
It creates a copy of target
object with overwritten newProps
properties. Previously known as R.merge
but renamed after Ramda did the same.
Try this R.mergeRight example in Rambda REPL
mergeWith(fn: (x: any, z: any) => any, a: Record<string, unknown>, b: Record<string, unknown>): Record<string, unknown>
It takes two objects and a function, which will be used when there is an overlap between the keys.
const result = R.mergeWith(
R.concat,
{values : [ 10, 20 ]},
{values : [ 15, 35 ]}
)
// => [ 10, 20, 15, 35 ]
Try this R.mergeWith example in Rambda REPL
import { curry } from './curry.js'
export function mergeWithFn(
mergeFn, aInput, bInput
){
const a = aInput ?? {}
const b = bInput ?? {}
const willReturn = {}
Object.keys(a).forEach(key => {
if (b[ key ] === undefined) willReturn[ key ] = a[ key ]
else willReturn[ key ] = mergeFn(a[ key ], b[ key ])
})
Object.keys(b).forEach(key => {
if (willReturn[ key ] !== undefined) return
if (a[ key ] === undefined) willReturn[ key ] = b[ key ]
else willReturn[ key ] = mergeFn(a[ key ], b[ key ])
})
return willReturn
}
export const mergeWith = curry(mergeWithFn)
import { concat } from './concat.js'
import { mergeWithFn } from './mergeWith.js'
test('happy', () => {
const result = mergeWithFn(
concat,
{
a : true,
values : [ 10, 20 ],
},
{
b : true,
values : [ 15, 35 ],
}
)
const expected = {
a : true,
b : true,
values : [ 10, 20, 15, 35 ],
}
expect(result).toEqual(expected)
})
// https://github.com/ramda/ramda/pull/3222/files#diff-d925d9188b478d2f1d4b26012c6dddac374f9e9d7a336604d654b9a113bfc857
describe('acts as if nil values are simply empty objects', () => {
it('if the first object is nil and the second empty', () => {
expect(mergeWithFn(
concat, undefined, {}
)).toEqual({})
})
it('if the first object is empty and the second nil', () => {
expect(mergeWithFn(
concat, {}, null
)).toEqual({})
})
it('if both objects are nil', () => {
expect(mergeWithFn(
concat, undefined, null
)).toEqual({})
})
it('if the first object is not empty and the second is nil', () => {
expect(mergeWithFn(
concat, { a : 'a' }, null
)).toEqual({ a : 'a' })
})
it('if the first object is nil and the second is not empty', () => {
expect(mergeWithFn(
concat, undefined, { a : 'a' }
)).toEqual({ a : 'a' })
})
})
It returns the lesser value between x
and y
.
Try this R.min example in Rambda REPL
It returns the lesser value between x
and y
according to compareFn
function.
Try this R.minBy example in Rambda REPL
modify<K extends PropertyKey, T>(prop: K, fn: (value: T) => T): <U extends Record<K, T>>(object: U) => U
const result = R.modify()
// =>
Try this R.modify example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { isIterable } from './_internals/isIterable.js'
import { curry } from './curry.js'
import { updateFn } from './update.js'
function modifyFn(
property, fn, iterable
){
if (!isIterable(iterable)) return iterable
if (iterable[ property ] === undefined) return iterable
if (isArray(iterable)){
return updateFn(
property, fn(iterable[ property ]), iterable
)
}
return {
...iterable,
[ property ] : fn(iterable[ property ]),
}
}
export const modify = curry(modifyFn)
import { modify as modifyRamda } from 'ramda'
import { compareCombinations, FALSY_VALUES } from './_internals/testUtils.js'
import { add } from './add.js'
import { compose } from './compose.js'
import { modify } from './modify.js'
const person = {
name : 'foo',
age : 20,
}
test('happy', () => {
expect(modify(
'age', x => x + 1, person
)).toEqual({
name : 'foo',
age : 21,
})
})
test('property is missing', () => {
expect(modify(
'foo', x => x + 1, person
)).toEqual(person)
})
test('adjust if `array` at the given key with the `transformation` function', () => {
expect(modify(
1, add(1), [ 100, 1400 ]
)).toEqual([ 100, 1401 ])
})
describe('ignores transformations if the input value is not Array and Object', () => {
;[ 42, undefined, null, '' ].forEach(value => {
it(`${ value }`, () => {
expect(modify(
'a', add(1), value
)).toEqual(value)
})
})
})
const possibleProperties = [ ...FALSY_VALUES, 'foo', 0 ]
const possibleTransformers = [
...FALSY_VALUES,
add(1),
add('foo'),
compose,
String,
]
const possibleObjects = [
...FALSY_VALUES,
{},
[ 1, 2, 3 ],
{
a : 1,
foo : 2,
},
{
a : 1,
foo : [ 1 ],
},
{
a : 1,
foo : 'bar',
},
]
describe('brute force', () => {
compareCombinations({
fn : modify,
fnRamda : modifyRamda,
firstInput : possibleProperties,
secondInput : possibleTransformers,
thirdInput : possibleObjects,
callback : errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
{
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 0,
"RESULTS_MISMATCH": 0,
"SHOULD_NOT_THROW": 0,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 630,
}
`)
},
})
})
It changes a property of object on the base of provided path and transformer function.
Try this R.modifyPath example in Rambda REPL
Curried version of x%y
.
Try this R.modulo example in Rambda REPL
It returns a copy of list
with exchanged fromIndex
and toIndex
elements.
:boom: Rambda.move doesn't support negative indexes - it throws an error.
Try this R.move example in Rambda REPL
Curried version of x*y
.
Try this R.multiply example in Rambda REPL
Try this R.negate example in Rambda REPL
nextIndex(index: number, list: any[]): number
It returns the next index of the list.
If we have reached the end of the list, then it will return 0
.
const list = [1, 2, 3]
const result = [
R.nextIndex(0, list),
R.nextIndex(1, list),
R.nextIndex(2, list),
R.nextIndex(10, list)
]
// => [1, 2, 0, 0]
Try this R.nextIndex example in Rambda REPL
export function nextIndex(index, list){
return index >= list.length - 1 ? 0 : index + 1
}
import { nextIndex } from './nextIndex.js'
const list = [ 1, 2, 3, 4 ]
test('happy path', () => {
expect(nextIndex(2, list)).toBe(3)
})
test('go back to the start', () => {
expect(nextIndex(3, list)).toBe(0)
})
test('current index is too big', () => {
expect(nextIndex(32, list)).toBe(0)
})
none<T>(predicate: (x: T) => boolean, list: T[]): boolean
It returns true
, if all members of array list
returns false
, when applied as argument to predicate
function.
const list = [ 0, 1, 2, 3, 4 ]
const predicate = x => x > 6
const result = R.none(predicate, arr)
// => true
Try this R.none example in Rambda REPL
export function none(predicate, list){
if (arguments.length === 1) return _list => none(predicate, _list)
for (let i = 0; i < list.length; i++){
if (predicate(list[ i ])) return false
}
return true
}
import { none } from './none.js'
const isEven = n => n % 2 === 0
test('when true', () => {
expect(none(isEven, [ 1, 3, 5, 7 ])).toBeTrue()
})
test('when false curried', () => {
expect(none(input => input > 1, [ 1, 2, 3 ])).toBeFalse()
})
noop(): void
export function noop(){}
not(input: any): boolean
It returns a boolean negated version of input
.
R.not(false) // true
Try this R.not example in Rambda REPL
export function not(input){
return !input
}
import { not } from './not.js'
test('not', () => {
expect(not(false)).toBeTrue()
expect(not(true)).toBeFalse()
expect(not(0)).toBeTrue()
expect(not(1)).toBeFalse()
})
nth(index: number, input: string): string
Curried version of input[index]
.
const list = [1, 2, 3]
const str = 'foo'
const result = [
R.nth(2, list),
R.nth(6, list),
R.nth(0, str),
]
// => [3, undefined, 'f']
Try this R.nth example in Rambda REPL
export function nth(index, input){
if (arguments.length === 1) return _input => nth(index, _input)
const idx = index < 0 ? input.length + index : index
return Object.prototype.toString.call(input) === '[object String]' ?
input.charAt(idx) :
input[ idx ]
}
import { nth } from './nth.js'
test('happy', () => {
expect(nth(2, [ 1, 2, 3, 4 ])).toBe(3)
})
test('with curry', () => {
expect(nth(2)([ 1, 2, 3, 4 ])).toBe(3)
})
test('with string and correct index', () => {
expect(nth(2)('foo')).toBe('o')
})
test('with string and invalid index', () => {
expect(nth(20)('foo')).toBe('')
})
test('with negative index', () => {
expect(nth(-3)([ 1, 2, 3, 4 ])).toBe(2)
})
It creates an object with a single key-value pair.
Try this R.objOf example in Rambda REPL
of<T>(x: T): T[]
R.of(null); // => [null]
R.of([42]); // => [[42]]
Try this R.of example in Rambda REPL
export function of(value){
return [ value ]
}
import { of } from './of.js'
test('happy', () => {
expect(of(3)).toEqual([ 3 ])
expect(of(null)).toEqual([ null ])
})
ok(...inputs: any[]): (...schemas: any[]) => void | never
It checks if inputs
are following schemas
specifications according to R.isValid
.
If validation fails, it throws.
:boom: It is same as
R.pass
but instead of returningfalse
, it throws an error.
const result = R.ok(
1,
['foo', 'bar']
)(
Number,
[String]
)
// => undefined
Try this R.ok example in Rambda REPL
import { any } from './any.js'
import { glue } from './glue.js'
import { fromPrototypeToString, isValid } from './isValid.js'
import { map } from './map.js'
import { type } from './type.js'
export function schemaToString(schema){
if (type(schema) !== 'Object'){
return fromPrototypeToString(schema).rule
}
return map(x => {
const { rule, parsed } = fromPrototypeToString(x)
const xType = type(x)
if (xType === 'Function' && !parsed) return 'Function'
return parsed ? rule : xType
}, schema)
}
export function check(singleInput, schema){
return isValid({
input : { singleInput },
schema : { singleInput : schema },
})
}
export function ok(...inputs){
return (...schemas) => {
let failedSchema
const anyError = any((singleInput, i) => {
const schema = schemas[ i ] === undefined ? schemas[ 0 ] : schemas[ i ]
const checked = check(singleInput, schema)
if (!checked){
failedSchema = JSON.stringify({
input : singleInput,
schema : schemaToString(schema),
})
}
return !checked
}, inputs)
if (anyError){
const errorMessage =
inputs.length > 1 ?
glue(`
Failed R.ok -
reason: ${ failedSchema }
all inputs: ${ JSON.stringify(inputs) }
all schemas: ${ JSON.stringify(schemas.map(schemaToString)) }
`,
'\n') :
`Failed R.ok - ${ failedSchema }`
throw new Error(errorMessage)
}
}
}
import { ok, schemaToString } from './ok.js'
test('happy', () => {
expect(() => {
ok(
1, 'foo', {}
)(
'number', 'string', 'object'
)
}).not.toThrow()
})
test('when validation fails', () => {
expect(() => ok(
1, 'foo', {}
)(
'number', 'string', 'string'
))
.toThrowErrorMatchingInlineSnapshot(`
"Failed R.ok -
reason: {"input":{},"schema":"string"}
all inputs: [1,"foo",{}]
all schemas: ["number","string","string"]"
`)
})
test('schema in error message', () => {
const result = schemaToString({
_a : [ Number ],
a : Number,
b : x => x > 2,
c : [ 'foo', 'bar' ],
d : [ { a : String } ],
e : 'boolean',
f : Array,
h : Object,
})
expect(result).toMatchInlineSnapshot(`
{
"_a": "Array",
"a": "number",
"b": "Function",
"c": "Array",
"d": "Array",
"e": "String",
"f": "array",
"h": "object",
}
`)
})
test('error contains schema', () => {
try {
ok(
1, 'foo', {}
)(
{ a : Number }, String, String
)
expect(false).toBeTrue()
} catch (e){
expect(e.message.startsWith('Failed R.ok -')).toBeTruthy()
expect(e).toBeInstanceOf(Error)
}
})
test('when not throws with single schema', () => {
expect(() => ok(
1, 2, 3
)('number')).not.toThrow()
})
test('when throws with single schema', () => {
expect(() => ok(
1, 2, '3'
)('number')).toThrowErrorMatchingInlineSnapshot(`
"Failed R.ok -
reason: {"input":"3","schema":"number"}
all inputs: [1,2,"3"]
all schemas: ["number"]"
`)
})
test('when throws with single input', () => {
expect(() => ok('3')('number')).toThrowErrorMatchingInlineSnapshot('"Failed R.ok - {"input":"3","schema":"number"}"')
})
omit<T, K extends string>(propsToOmit: K[], obj: T): Omit<T, K>
It returns a partial copy of an obj
without propsToOmit
properties.
:boom: When using this method with
TypeScript
, it is much easier to passpropsToOmit
as an array. If passing a string, you will need to explicitly declare the output type.
const obj = {a: 1, b: 2, c: 3}
const propsToOmit = 'a,c,d'
const propsToOmitList = ['a', 'c', 'd']
const result = [
R.omit(propsToOmit, Record<string, unknown>),
R.omit(propsToOmitList, Record<string, unknown>)
]
// => [{b: 2}, {b: 2}]
Try this R.omit example in Rambda REPL
import { createPath } from './_internals/createPath.js'
import { includes } from './_internals/includes.js'
export function omit(propsToOmit, obj){
if (arguments.length === 1) return _obj => omit(propsToOmit, _obj)
if (obj === null || obj === undefined)
return undefined
const propsToOmitValue = createPath(propsToOmit, ',')
const willReturn = {}
for (const key in obj)
if (!includes(key, propsToOmitValue))
willReturn[ key ] = obj[ key ]
return willReturn
}
import { omit } from './omit.js'
test('with string as condition', () => {
const obj = {
a : 1,
b : 2,
c : 3,
}
const result = omit('a,c', obj)
const resultCurry = omit('a,c')(obj)
const expectedResult = { b : 2 }
expect(result).toEqual(expectedResult)
expect(resultCurry).toEqual(expectedResult)
})
test.only('with number as property to omit', () => {
const obj = {
1 : 1,
b : 2,
}
const result = omit([ 1 ], obj)
expect(result).toEqual({ b : 2 })
})
test('with null', () => {
expect(omit('a,b', null)).toBeUndefined()
})
test('happy', () => {
expect(omit([ 'a', 'c' ])({
a : 'foo',
b : 'bar',
c : 'baz',
})).toEqual({ b : 'bar' })
})
It passes the two inputs through unaryFn
and then the results are passed as inputs the the binaryFn
to receive the final result(binaryFn(unaryFn(FIRST_INPUT), unaryFn(SECOND_INPUT))
).
This method is also known as P combinator.
Try this R.on example in Rambda REPL
once<T extends AnyFunction, C = unknown>(fn: T, context?: C): T
It returns a function, which invokes only once fn
function.
let result = 0
const addOnce = R.once((x) => result = result + x)
addOnce(1)
addOnce(1)
// => 1
Try this R.once example in Rambda REPL
import { curry } from './curry.js'
function onceFn(fn, context){
let result
return function (){
if (fn){
result = fn.apply(context || this, arguments)
fn = null
}
return result
}
}
export function once(fn, context){
if (arguments.length === 1){
const wrap = onceFn(fn, context)
return curry(wrap)
}
return onceFn(fn, context)
}
import { once } from './once.js'
test('with counter', () => {
let counter = 0
const runOnce = once(x => {
counter++
return x + 2
})
expect(runOnce(1)).toBe(3)
runOnce(1)
runOnce(1)
runOnce(1)
expect(counter).toBe(1)
})
test('happy path', () => {
const addOneOnce = once((
a, b, c
) => a + b + c, 1)
expect(addOneOnce(
10, 20, 30
)).toBe(60)
expect(addOneOnce(40)).toBe(60)
})
test('with context', () => {
const context = { name: 'fris' }
const getNameOnce = once(function (){
return this.name
}, context)
expect(getNameOnce()).toBe('fris')
expect(getNameOnce()).toBe('fris')
expect(getNameOnce()).toBe('fris')
})
Logical OR
Try this R.or example in Rambda REPL
over<S, A>(lens: Lens<S, A>): {
(fn: (a: A) => A): (value: S) => S
It returns a copied Object or Array with modified value received by applying function fn
to lens
focus.
const headLens = R.lensIndex(0)
R.over(headLens, R.toUpper, ['foo', 'bar', 'baz']) // => ['FOO', 'bar', 'baz']
Try this R.over example in Rambda REPL
import { curry } from './curry.js'
const Identity = x => ({
x,
map : fn => Identity(fn(x)),
})
function overFn(
lens, fn, object
){
return lens(x => Identity(fn(x)))(object).x
}
export const over = curry(overFn)
import { assoc } from './assoc.js'
import { lens } from './lens.js'
import { lensIndex } from './lensIndex.js'
import { lensPath } from './lensPath.js'
import { over } from './over.js'
import { prop } from './prop.js'
import { toUpper } from './toUpper.js'
const testObject = {
foo : 'bar',
baz : {
a : 'x',
b : 'y',
},
}
test('assoc lens', () => {
const assocLens = lens(prop('foo'), assoc('foo'))
const result = over(
assocLens, toUpper, testObject
)
const expected = {
...testObject,
foo : 'BAR',
}
expect(result).toEqual(expected)
})
test('path lens', () => {
const pathLens = lensPath('baz.a')
const result = over(
pathLens, toUpper, testObject
)
const expected = {
...testObject,
baz : {
a : 'X',
b : 'y',
},
}
expect(result).toEqual(expected)
})
test('index lens', () => {
const indexLens = lensIndex(0)
const result = over(indexLens, toUpper)([ 'foo', 'bar' ])
expect(result).toEqual([ 'FOO', 'bar' ])
})
partial<V0, V1, T>(fn: (x0: V0, x1: V1) => T, args: [V0]): (x1: V1) => T
It is very similar to R.curry
, but you can pass initial arguments when you create the curried function.
R.partial
will keep returning a function until all the arguments that the function fn
expects are passed.
The name comes from the fact that you partially inject the inputs.
:boom: Rambda's partial doesn't need the input arguments to be wrapped as array.
const fn = (title, firstName, lastName) => {
return title + ' ' + firstName + ' ' + lastName + '!'
}
const canPassAnyNumberOfArguments = R.partial(fn, 'Hello')
const ramdaStyle = R.partial(fn, ['Hello'])
const finalFn = canPassAnyNumberOfArguments('Foo')
finalFn('Bar') // => 'Hello, Foo Bar!'
Try this R.partial example in Rambda REPL
import { isArray } from './_internals/isArray.js'
export function partial(fn, ...args){
const len = fn.length
// If a single array argument is given, those are the args (a la Ramda).
// Otherwise, the variadic arguments are the args.
const argList = args.length === 1 && isArray(args[0]) ? args[0] : args
return (...rest) => {
if (argList.length + rest.length >= len){
return fn(...argList, ...rest)
}
return partial(fn, ...[ ...argList, ...rest ])
}
}
import { partial } from './partial.js'
import { type } from './type.js'
const greet = (
salutation, title, firstName, lastName
) =>
[salutation, title, firstName, lastName]
test('happy', () => {
const canPassAnyNumberOfArguments = partial(
greet, 'Hello', 'Ms.'
)
const fn = canPassAnyNumberOfArguments('foo')
const sayHello = partial(greet, [ 'Hello' ])
const sayHelloRamda = partial(sayHello, [ 'Ms.' ])
expect(type(fn)).toBe('Function')
expect(fn('bar')).toStrictEqual(['Hello', 'Ms.', 'foo', 'bar'])
expect(sayHelloRamda('foo', 'bar')).toStrictEqual(['Hello', 'Ms.', 'foo', 'bar'])
})
test('extra arguments are ignored', () => {
const canPassAnyNumberOfArguments = partial(
greet, 'Hello', 'Ms.'
)
const fn = canPassAnyNumberOfArguments('foo')
expect(type(fn)).toBe('Function')
expect(fn(
'bar', 1, 2
)).toStrictEqual(['Hello', 'Ms.', 'foo', 'bar'])
})
test('when array is input', () => {
const fooFn = (
a, b, c, d
) => ({
a,
b,
c,
d,
})
const barFn = partial(
fooFn, [ 1, 2 ], []
)
expect(barFn(1, 2)).toEqual({
a : [ 1, 2 ],
b : [],
c : 1,
d : 2,
})
})
test('ramda spec', () => {
const sayHello = partial(greet, 'Hello')
const sayHelloToMs = partial(sayHello, 'Ms.')
expect(sayHelloToMs('Jane', 'Jones')).toStrictEqual(['Hello', 'Ms.', 'Jane', 'Jones'])
})
partialCurry<Input, PartialInput, Output>(
fn: (input: Input) => Output,
partialInput: PartialInput,
): (input: Pick<Input, Exclude<keyof Input, keyof PartialInput>>) => Output
Same as R.partialObject
.
When Ramda
introduced R.partialObject
, Rambdax
already had such method, i.e. R.partialCurry
. So this method is kept for backward compatibility.
:boom: Function input can be asynchronous
export { partialObject as partialCurry } from './partialObject.js'
partialObject<Input, PartialInput, Output>(
fn: (input: Input) => Output,
partialInput: PartialInput,
): (input: Pick<Input, Exclude<keyof Input, keyof PartialInput>>) => Output
R.partialObject
is a curry helper designed specifically for functions accepting object as a single argument.
Initially the function knows only a part from the whole input object and then R.partialObject
helps in preparing the function for the second part, when it receives the rest of the input.
:boom: Function input can be asynchronous
const fn = ({ a, b, c }) => a + b + c
const curried = R.partialObject(fn, { a : 1 })
const result = curried({
b : 2,
c : 3,
})
// => 6
Try this R.partialObject example in Rambda REPL
import { mergeDeepRight } from './mergeDeepRight.js'
export function partialObject(fn, input){
return nextInput => fn(mergeDeepRight(nextInput, input))
}
import { delay } from './delay.js'
import { partialObject } from './partialObject.js'
import { type } from './type.js'
test('with plain function', () => {
const fn = ({ a, b, c }) => a + b + c
const curried = partialObject(fn, { a : 1 })
expect(type(curried)).toBe('Function')
expect(curried({
b : 2,
c : 3,
})).toBe(6)
})
test('with function that throws an error', () => {
const fn = ({ a, b, c }) => {
throw new Error('foo')
}
const curried = partialObject(fn, { a : 1 })
expect(type(curried)).toBe('Function')
expect(() =>
curried({
b : 2,
c : 3,
})).toThrowErrorMatchingInlineSnapshot('"foo"')
})
test('with async', async () => {
const fn = async ({ a, b, c }) => {
await delay(100)
return a + b + c
}
const curried = partialObject(fn, { a : 1 })
const result = await curried({
b : 2,
c : 3,
})
expect(result).toBe(6)
})
test('async function throwing an error', async () => {
const fn = async ({ a, b, c }) => {
await delay(100)
throw new Error('foo')
}
const curried = partialObject(fn, { a : 1 })
try {
await curried({
b : 2,
c : 3,
})
expect(true).toBeFalsy()
} catch (e){
expect(e.message).toBe('foo')
}
})
partition<T>(
predicate: Predicate<T>,
input: T[]
): [T[], T[]]
It will return array of two objects/arrays according to predicate
function. The first member holds all instances of input
that pass the predicate
function, while the second member - those who doesn't.
const list = [1, 2, 3]
const obj = {a: 1, b: 2, c: 3}
const predicate = x => x > 2
const result = [
R.partition(predicate, list),
R.partition(predicate, Record<string, unknown>)
]
const expected = [
[[3], [1, 2]],
[{c: 3}, {a: 1, b: 2}],
]
// `result` is equal to `expected`
Try this R.partition example in Rambda REPL
import { isArray } from './_internals/isArray.js'
export function partitionObject(predicate, iterable){
const yes = {}
const no = {}
Object.entries(iterable).forEach(([ prop, value ]) => {
if (predicate(value, prop)){
yes[ prop ] = value
} else {
no[ prop ] = value
}
})
return [ yes, no ]
}
export function partitionArray(
predicate, list, indexed = false
){
const yes = []
const no = []
let counter = -1
while (counter++ < list.length - 1){
if (
indexed ? predicate(list[ counter ], counter) : predicate(list[ counter ])
){
yes.push(list[ counter ])
} else {
no.push(list[ counter ])
}
}
return [ yes, no ]
}
export function partition(predicate, iterable){
if (arguments.length === 1){
return listHolder => partition(predicate, listHolder)
}
if (!isArray(iterable)) return partitionObject(predicate, iterable)
return partitionArray(predicate, iterable)
}
import { partition } from './partition.js'
test('with array', () => {
const predicate = x => x > 2
const list = [ 1, 2, 3, 4 ]
const result = partition(predicate, list)
const expectedResult = [
[ 3, 4 ],
[ 1, 2 ],
]
expect(result).toEqual(expectedResult)
})
test('with object', () => {
const predicate = (value, prop) => {
expect(typeof prop).toBe('string')
return value > 2
}
const hash = {
a : 1,
b : 2,
c : 3,
d : 4,
}
const result = partition(predicate)(hash)
const expectedResult = [
{
c : 3,
d : 4,
},
{
a : 1,
b : 2,
},
]
expect(result).toEqual(expectedResult)
})
test('readme example', () => {
const list = [ 1, 2, 3 ]
const obj = {
a : 1,
b : 2,
c : 3,
}
const predicate = x => x > 2
const result = [ partition(predicate, list), partition(predicate, obj) ]
const expected = [
[ [ 3 ], [ 1, 2 ] ],
[
{ c : 3 },
{
a : 1,
b : 2,
},
],
]
expect(result).toEqual(expected)
})
pass(...inputs: any[]): (...rules: any[]) => boolean
It checks if inputs
are following schemas
specifications according to R.isValid
.
const result = R.pass(
1,
['foo','bar']
)(
Number,
[String]
)
// => true
Try this R.pass example in Rambda REPL
import { any } from './any.js'
import { check } from './ok.js'
export function pass(...inputs){
return (...schemas) =>
any((x, i) => {
const schema = schemas[ i ] === undefined ? schemas[ 0 ] : schemas[ i ]
return !check(x, schema)
}, inputs) === false
}
import { pass } from './pass.js'
test('true on success', () => {
const result = pass(
1, 'foo', {}
)(
'number', 'string', 'object'
)
expect(result).toBeTrue()
})
test('false on failure', () => {
expect(pass(
1, 'foo', {}
)(
'number', 'string', 'string'
)).toBeFalse()
})
test('true when single schema', () => {
expect(pass(
1, 2, 3
)('number')).toBeTrue()
})
test('false when single schema', () => {
expect(pass(
1, 'foo', {}
)('number')).toBeFalse()
})
test('array of schemas', () => {
const result = pass([ { a : 1 }, { a : 2 }, { a : 3 } ])([ { a : Number } ])
expect(result).toBeTruthy()
})
test('reame example', () => {
const result = pass(1, [ 'foo', 'bar' ])(Number, [ String ])
expect(result).toBeTruthy()
})
path<S, K0 extends keyof S = keyof S>(path: [K0], obj: S): S[K0]
If pathToSearch
is 'a.b'
then it will return 1
if obj
is {a:{b:1}}
.
It will return undefined
, if such path is not found.
:boom: String annotation of
pathToSearch
is one of the differences betweenRambda
andRamda
.
const obj = {a: {b: 1}}
const pathToSearch = 'a.b'
const pathToSearchList = ['a', 'b']
const result = [
R.path(pathToSearch, Record<string, unknown>),
R.path(pathToSearchList, Record<string, unknown>),
R.path('a.b.c.d', Record<string, unknown>)
]
// => [1, 1, undefined]
Try this R.path example in Rambda REPL
import { createPath } from './_internals/createPath.js'
export function pathFn(pathInput, obj){
let willReturn = obj
let counter = 0
const pathArrValue = createPath(pathInput)
while (counter < pathArrValue.length){
if (willReturn === null || willReturn === undefined){
return undefined
}
if (willReturn[ pathArrValue[ counter ] ] === null) return undefined
willReturn = willReturn[ pathArrValue[ counter ] ]
counter++
}
return willReturn
}
export function path(pathInput, obj){
if (arguments.length === 1) return _obj => path(pathInput, _obj)
if (obj === null || obj === undefined){
return undefined
}
return pathFn(pathInput, obj)
}
import { path } from './path.js'
test('with array inside object', () => {
const obj = { a : { b : [ 1, { c : 1 } ] } }
expect(path('a.b.1.c', obj)).toBe(1)
})
test('works with undefined', () => {
const obj = { a : { b : { c : 1 } } }
expect(path('a.b.c.d.f', obj)).toBeUndefined()
expect(path('foo.babaz', undefined)).toBeUndefined()
expect(path('foo.babaz')(undefined)).toBeUndefined()
})
test('works with string instead of array', () => {
expect(path('foo.bar.baz')({ foo : { bar : { baz : 'yes' } } })).toBe('yes')
})
test('path', () => {
expect(path([ 'foo', 'bar', 'baz' ])({ foo : { bar : { baz : 'yes' } } })).toBe('yes')
expect(path([ 'foo', 'bar', 'baz' ])(null)).toBeUndefined()
expect(path([ 'foo', 'bar', 'baz' ])({ foo : { bar : 'baz' } })).toBeUndefined()
})
test('null is not a valid path', () => {
expect(path('audio_tracks', {
a : 1,
audio_tracks : null,
})).toBeUndefined()
})
pathEq(pathToSearch: Path, target: any, input: any): boolean
It returns true
if pathToSearch
of input
object is equal to target
value.
pathToSearch
is passed to R.path
, which means that it can be either a string or an array. Also equality between target
and the found value is determined by R.equals
.
const path = 'a.b'
const target = {c: 1}
const input = {a: {b: {c: 1}}}
const result = R.pathEq(
path,
target,
input
)
// => true
Try this R.pathEq example in Rambda REPL
import { curry } from './curry.js'
import { equals } from './equals.js'
import { path } from './path.js'
function pathEqFn(
pathToSearch, target, input
){
return equals(path(pathToSearch, input), target)
}
export const pathEq = curry(pathEqFn)
import { pathEq } from './pathEq.js'
test('when true', () => {
const path = 'a.b'
const obj = { a : { b : { c : 1 } } }
const target = { c : 1 }
expect(pathEq(
path, target, obj
)).toBeTrue()
})
test('when false', () => {
const path = 'a.b'
const obj = { a : { b : 1 } }
const target = 2
expect(pathEq(path, target)(obj)).toBeFalse()
})
test('when wrong path', () => {
const path = 'foo.bar'
const obj = { a : { b : 1 } }
const target = 2
expect(pathEq(
path, target, obj
)).toBeFalse()
})
pathOr<T>(defaultValue: T, pathToSearch: Path, obj: any): T
It reads obj
input and returns either R.path(pathToSearch, Record<string, unknown>)
result or defaultValue
input.
const defaultValue = 'DEFAULT_VALUE'
const pathToSearch = 'a.b'
const pathToSearchList = ['a', 'b']
const obj = {
a : {
b : 1
}
}
const result = [
R.pathOr(DEFAULT_VALUE, pathToSearch, Record<string, unknown>),
R.pathOr(DEFAULT_VALUE, pathToSearchList, Record<string, unknown>),
R.pathOr(DEFAULT_VALUE, 'a.b.c', Record<string, unknown>)
]
// => [1, 1, 'DEFAULT_VALUE']
Try this R.pathOr example in Rambda REPL
import { curry } from './curry.js'
import { defaultTo } from './defaultTo.js'
import { path } from './path.js'
function pathOrFn(
defaultValue, pathInput, obj
){
return defaultTo(defaultValue, path(pathInput, obj))
}
export const pathOr = curry(pathOrFn)
import { pathOr } from './pathOr.js'
test('with undefined', () => {
const result = pathOr(
'foo', 'x.y', { x : { y : 1 } }
)
expect(result).toBe(1)
})
test('with null', () => {
const result = pathOr(
'foo', 'x.y', null
)
expect(result).toBe('foo')
})
test('with NaN', () => {
const result = pathOr(
'foo', 'x.y', NaN
)
expect(result).toBe('foo')
})
test('curry case (x)(y)(z)', () => {
const result = pathOr('foo')('x.y.z')({ x : { y : { a : 1 } } })
expect(result).toBe('foo')
})
test('curry case (x)(y,z)', () => {
const result = pathOr('foo', 'x.y.z')({ x : { y : { a : 1 } } })
expect(result).toBe('foo')
})
test('curry case (x,y)(z)', () => {
const result = pathOr('foo')('x.y.z', { x : { y : { a : 1 } } })
expect(result).toBe('foo')
})
paths<Input, T>(pathsToSearch: Path[], obj: Input): (T | undefined)[]
It loops over members of pathsToSearch
as singlePath
and returns the array produced by R.path(singlePath, Record<string, unknown>)
.
Because it calls R.path
, then singlePath
can be either string or a list.
const obj = {
a : {
b : {
c : 1,
d : 2
}
}
}
const result = R.paths([
'a.b.c',
'a.b.d',
'a.b.c.d.e',
], Record<string, unknown>)
// => [1, 2, undefined]
Try this R.paths example in Rambda REPL
import { path } from './path.js'
export function paths(pathsToSearch, obj){
if (arguments.length === 1){
return _obj => paths(pathsToSearch, _obj)
}
return pathsToSearch.map(singlePath => path(singlePath, obj))
}
import { paths } from './paths.js'
const obj = {
a : {
b : {
c : 1,
d : 2,
},
},
p : [ { q : 3 } ],
x : {
y : 'FOO',
z : [ [ {} ] ],
},
}
test('with string path + curry', () => {
const pathsInput = [ 'a.b.d', 'p.q' ]
const expected = [ 2, undefined ]
const result = paths(pathsInput, obj)
const curriedResult = paths(pathsInput)(obj)
expect(result).toEqual(expected)
expect(curriedResult).toEqual(expected)
})
test('with array path', () => {
const result = paths([
[ 'a', 'b', 'c' ],
[ 'x', 'y' ],
],
obj)
expect(result).toEqual([ 1, 'FOO' ])
})
test('takes a paths that contains indices into arrays', () => {
expect(paths([
[ 'p', 0, 'q' ],
[ 'x', 'z', 0, 0 ],
],
obj)).toEqual([ 3, {} ])
expect(paths([
[ 'p', 0, 'q' ],
[ 'x', 'z', 2, 1 ],
],
obj)).toEqual([ 3, undefined ])
})
test('gets a deep property\'s value from objects', () => {
expect(paths([ [ 'a', 'b' ] ], obj)).toEqual([ obj.a.b ])
expect(paths([ [ 'p', 0 ] ], obj)).toEqual([ obj.p[ 0 ] ])
})
test('returns undefined for items not found', () => {
expect(paths([ [ 'a', 'x', 'y' ] ], obj)).toEqual([ undefined ])
expect(paths([ [ 'p', 2 ] ], obj)).toEqual([ undefined ])
})
Try this R.pathSatisfies example in Rambda REPL
pick<T, K extends string | number | symbol>(propsToPick: K[], input: T): Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>
It returns a partial copy of an input
containing only propsToPick
properties.
input
can be either an object or an array.
String annotation of propsToPick
is one of the differences between Rambda
and Ramda
.
:boom: When using this method with
TypeScript
, it is much easier to passpropsToPick
as an array. If passing a string, you will need to explicitly declare the output type.
const obj = {
a : 1,
b : false,
foo: 'cherry'
}
const list = [1, 2, 3, 4]
const propsToPick = 'a,foo'
const propsToPickList = ['a', 'foo']
const result = [
R.pick(propsToPick, Record<string, unknown>),
R.pick(propsToPickList, Record<string, unknown>),
R.pick('a,bar', Record<string, unknown>),
R.pick('bar', Record<string, unknown>),
R.pick([0, 3, 5], list),
R.pick('0,3,5', list),
]
const expected = [
{a:1, foo: 'cherry'},
{a:1, foo: 'cherry'},
{a:1},
{},
{0: 1, 3: 4},
{0: 1, 3: 4},
]
// => `result` is equal to `expected`
Try this R.pick example in Rambda REPL
import { createPath } from './_internals/createPath.js'
export function pick(propsToPick, input){
if (arguments.length === 1) return _input => pick(propsToPick, _input)
if (input === null || input === undefined){
return undefined
}
const keys = createPath(propsToPick, ',')
const willReturn = {}
let counter = 0
while (counter < keys.length){
if (keys[ counter ] in input){
willReturn[ keys[ counter ] ] = input[ keys[ counter ] ]
}
counter++
}
return willReturn
}
import { pick } from './pick.js'
const obj = {
a : 1,
b : 2,
c : 3,
}
test('props to pick is a string', () => {
const result = pick('a,c', obj)
const resultCurry = pick('a,c')(obj)
const expectedResult = {
a : 1,
c : 3,
}
expect(result).toEqual(expectedResult)
expect(resultCurry).toEqual(expectedResult)
})
test('when prop is missing', () => {
const result = pick('a,d,f', obj)
expect(result).toEqual({ a : 1 })
})
test('with list indexes as props', () => {
const list = [ 1, 2, 3 ]
const expected = {
0 : 1,
2 : 3,
}
expect(pick([ 0, 2, 3 ], list)).toEqual(expected)
expect(pick('0,2,3', list)).toEqual(expected)
})
test('props to pick is an array', () => {
expect(pick([ 'a', 'c' ])({
a : 'foo',
b : 'bar',
c : 'baz',
})).toEqual({
a : 'foo',
c : 'baz',
})
expect(pick([ 'a', 'd', 'e', 'f' ])({
a : 'foo',
b : 'bar',
c : 'baz',
})).toEqual({ a : 'foo' })
expect(pick('a,d,e,f')(null)).toBeUndefined()
})
test('works with list as input and number as props - props to pick is an array', () => {
const result = pick([ 1, 2 ], [ 'a', 'b', 'c', 'd' ])
expect(result).toEqual({
1 : 'b',
2 : 'c',
})
})
test('works with list as input and number as props - props to pick is a string', () => {
const result = pick('1,2', [ 'a', 'b', 'c', 'd' ])
expect(result).toEqual({
1 : 'b',
2 : 'c',
})
})
test('with symbol', () => {
const symbolProp = Symbol('s')
expect(pick([ symbolProp ], { [ symbolProp ] : 'a' })).toMatchInlineSnapshot(`
{
Symbol(s): "a",
}
`)
})
pickAll<T, K extends keyof T>(propsToPicks: K[], input: T): Pick<T, K>
Same as R.pick
but it won't skip the missing props, i.e. it will assign them to undefined
.
:boom: When using this method with
TypeScript
, it is much easier to passpropsToPick
as an array. If passing a string, you will need to explicitly declare the output type.
const obj = {
a : 1,
b : false,
foo: 'cherry'
}
const propsToPick = 'a,foo,bar'
const propsToPickList = ['a', 'foo', 'bar']
const result = [
R.pickAll(propsToPick, Record<string, unknown>),
R.pickAll(propsToPickList, Record<string, unknown>),
R.pickAll('a,bar', Record<string, unknown>),
R.pickAll('bar', Record<string, unknown>),
]
const expected = [
{a:1, foo: 'cherry', bar: undefined},
{a:1, foo: 'cherry', bar: undefined},
{a:1, bar: undefined},
{bar: undefined}
]
// => `result` is equal to `expected`
Try this R.pickAll example in Rambda REPL
import { createPath } from './_internals/createPath.js'
export function pickAll(propsToPick, obj){
if (arguments.length === 1) return _obj => pickAll(propsToPick, _obj)
if (obj === null || obj === undefined){
return undefined
}
const keysValue = createPath(propsToPick, ',')
const willReturn = {}
let counter = 0
while (counter < keysValue.length){
if (keysValue[ counter ] in obj){
willReturn[ keysValue[ counter ] ] = obj[ keysValue[ counter ] ]
} else {
willReturn[ keysValue[ counter ] ] = undefined
}
counter++
}
return willReturn
}
import { pickAll } from './pickAll.js'
test('when input is undefined or null', () => {
expect(pickAll('a', null)).toBeUndefined()
expect(pickAll('a', undefined)).toBeUndefined()
})
test('with string as condition', () => {
const obj = {
a : 1,
b : 2,
c : 3,
}
const result = pickAll('a,c', obj)
const resultCurry = pickAll('a,c')(obj)
const expectedResult = {
a : 1,
b : undefined,
c : 3,
}
expect(result).toEqual(expectedResult)
expect(resultCurry).toEqual(expectedResult)
})
test('with array as condition', () => {
expect(pickAll([ 'a', 'b', 'c' ], {
a : 'foo',
c : 'baz',
})).toEqual({
a : 'foo',
b : undefined,
c : 'baz',
})
})
Try this R.pickBy example in Rambda REPL
It performs left-to-right function composition.
Try this R.pipe example in Rambda REPL
Asynchronous version of R.pipe
. await
s the result of each function before passing it to the next. Returns a Promise
of the result.
Try this R.pipeAsync example in Rambda REPL
piped<A, B>(input: A, fn0: (x: A) => B) : B
It is basically R.pipe
, but instead of passing input
argument as R.pipe(...)(input)
, you pass it as the first argument.
const result = R.piped(
[1, 2, 3],
R.filter(x => x > 1),
R.map(x => x*10),
)
// => [20, 30]
Try this R.piped example in Rambda REPL
import { pipe } from './pipe.js'
export function piped(...inputs){
const [ input, ...fnList ] = inputs
return pipe(...fnList)(input)
}
import { add } from './add.js'
import { filter } from './filter.js'
import { map } from './map.js'
import { piped } from './piped.js'
test('happy', () => {
const result = piped(
[ 1, 2, 3 ],
filter(x => x > 1),
map(x => x * 10),
map(add(1))
)
const expectedResult = [ 21, 31 ]
expect(result).toEqual(expectedResult)
})
It accepts input as first argument and series of functions as next arguments. It is same as R.piped
but with support for asynchronous functions like R.pipeAsync
.
Try this R.pipedAsync example in Rambda REPL
pluck<K extends keyof T, T>(property: K, list: T[]): T[K][]
It returns list of the values of property
taken from the all objects inside list
.
const list = [{a: 1}, {a: 2}, {b: 3}]
const property = 'a'
const result = R.pluck(property, list)
// => [1, 2]
Try this R.pluck example in Rambda REPL
import { map } from './map.js'
export function pluck(property, list){
if (arguments.length === 1) return _list => pluck(property, _list)
const willReturn = []
map(x => {
if (x[ property ] !== undefined){
willReturn.push(x[ property ])
}
}, list)
return willReturn
}
import { pluck } from './pluck.js'
test('happy', () => {
expect(pluck('a')([ { a : 1 }, { a : 2 }, { b : 1 } ])).toEqual([ 1, 2 ])
})
test('with undefined', () => {
expect(pluck(undefined)([ { a : 1 }, { a : 2 }, { b : 1 } ])).toEqual([ ])
})
test('with number', () => {
const input = [
[ 1, 2 ],
[ 3, 4 ],
]
expect(pluck(0, input)).toEqual([ 1, 3 ])
})
prepend<T>(xToPrepend: T, iterable: T[]): T[]
It adds element x
at the beginning of list
.
const result = R.prepend('foo', ['bar', 'baz'])
// => ['foo', 'bar', 'baz']
Try this R.prepend example in Rambda REPL
export function prepend(x, input){
if (arguments.length === 1) return _input => prepend(x, _input)
if (typeof input === 'string') return [ x ].concat(input.split(''))
return [ x ].concat(input)
}
import { prepend } from './prepend.js'
test('happy', () => {
expect(prepend('yes', [ 'foo', 'bar', 'baz' ])).toEqual([
'yes',
'foo',
'bar',
'baz',
])
})
test('with empty list', () => {
expect(prepend('foo')([])).toEqual([ 'foo' ])
})
test('with string instead of array', () => {
expect(prepend('foo')('bar')).toEqual([ 'foo', 'b', 'a', 'r' ])
})
prevIndex(index: number, list: any[]): number
It returns the next index of the list when the order is descending.
If we have reached the beginning of the list, then it will return the last index of the list.
:boom: Unlike
R.nextIndex
, which safeguards against index out of bounds, this method does not.
const list = [1, 2, 3]
const result = [
R.prevIndex(0, list),
R.prevIndex(1, list),
R.prevIndex(2, list),
]
// => [2, 0, 1]
Try this R.prevIndex example in Rambda REPL
export function prevIndex(index, list){
return index === 0 ? list.length - 1 : index - 1
}
import { prevIndex } from './prevIndex.js'
const list = [ 1, 2, 3, 4 ]
test('happy path 1', () => {
expect(prevIndex(2, list)).toBe(1)
})
test('happy path 2', () => {
expect(prevIndex(0, list)).toBe(3)
})
produce<Input extends any, Output>(
rules: ProduceRules<Output, keyof Output, Input>,
input: Input
): Output
It returns an object created by applying each value of rules
to input
argument.
:boom: In Typescript context,
rules
functions can be only 1 level deep. In Javascript context, there is no such restriction.
const rules = {
foo: R.pipe(R.add(1), R.add(2)),
a: {b: R.add(3)}
}
const result = R.produce(rules, 1)
const expected = {
foo: 4,
a: {b: 4}
}
// => `result` is equal to `expected`
Try this R.produce example in Rambda REPL
import { map } from './map.js'
import { type } from './type.js'
export function produce(rules, input){
if (arguments.length === 1){
return _input => produce(rules, _input)
}
return map(singleRule =>
type(singleRule) === 'Object' ?
produce(singleRule, input) :
singleRule(input),
rules)
}
import { add, pipe } from '../rambda.js'
import { produce } from './produce.js'
const rules = {
a : pipe(add(2), add(3)),
b : x => ({ foo : x }),
c : {
d : add(2),
e : add(10),
},
}
const expected = {
a : 6,
b : { foo : 1 },
c : {
d : 3,
e : 11,
},
}
test('happy', () => {
const result = produce(rules, 1)
expect(result).toEqual(expected)
})
test('curried', () => {
const result = produce(rules)(1)
expect(result).toEqual(expected)
})
produceAsync<Input extends any, Output>(
rules: ProduceAsyncRules<Output, keyof Output, Input>,
input: Input
): Promise<Output>
It returns an object created by applying each value of rules
to input
argument.
rules
input is an object with synchronous or asynchronous functions as values.
The return value is wrapped in a promise, even if all rules
are synchronous functions.
const rules = {
foo: async x => {
await R.delay(100)
return x > 1
},
bar: x => ({baz: x})
}
const input = 2
const result = await R.produceAsync(rules, input)
const expected = {
foo: true,
bar: {baz: 2}
}
// => `result` is equal to `expected`
Try this R.produceAsync example in Rambda REPL
import { map } from './map.js'
import { type } from './type.js'
function promisify({ condition, input, prop }){
return new Promise((resolve, reject) => {
if (type(condition) !== 'Promise'){
return resolve({
type : prop,
payload : condition(input),
})
}
condition(input)
.then(result => {
resolve({
type : prop,
payload : result,
})
})
.catch(err => reject(err))
})
}
function produceFn(conditions, input){
let asyncConditionsFlag = false
for (const prop in conditions){
if (
asyncConditionsFlag === false &&
type(conditions[ prop ]) === 'Promise'
){
asyncConditionsFlag = true
}
}
if (asyncConditionsFlag === false){
const willReturn = {}
for (const prop in conditions){
willReturn[ prop ] = conditions[ prop ](input)
}
return Promise.resolve(willReturn)
}
const promised = []
for (const prop in conditions){
const condition = conditions[ prop ]
promised.push(promisify({
input,
condition,
prop,
}))
}
return new Promise((resolve, reject) => {
Promise.all(promised)
.then(results => {
const willReturn = {}
map(result => willReturn[ result.type ] = result.payload, results)
resolve(willReturn)
})
.catch(err => reject(err))
})
}
export function produceAsync(conditions, input){
if (arguments.length === 1){
return async _input => produceFn(conditions, _input)
}
return new Promise((resolve, reject) => {
produceFn(conditions, input).then(resolve)
.catch(reject)
})
}
import { delay } from './delay.js'
import { produceAsync } from './produceAsync.js'
test('happy', async () => {
const result = await produceAsync({
foo : async x => {
await delay(100)
return `${ x }_ZEPPELIN`
},
bar : x => x.length === 3,
},
'LED')
const expected = {
foo : 'LED_ZEPPELIN',
bar : true,
}
expect(result).toEqual(expected)
})
test('when all rules are synchronous', async () => {
const result = await produceAsync({
foo : x => `${ x }_ZEPPELIN`,
bar : x => x.length === 3,
},
'LED')
const expected = {
foo : 'LED_ZEPPELIN',
bar : true,
}
expect(result).toEqual(expected)
})
test('with error', async () => {
const fn = produceAsync({
foo : async x => {
await delay(100)
throw new Error(`${ x }_ZEPPELIN`)
},
bar : inputArgument => inputArgument === 5,
})
await expect(fn('LED')).rejects.toThrow('LED_ZEPPELIN')
})
product(list: number[]): number
R.product([ 2, 3, 4 ])
// => 24)
Try this R.product example in Rambda REPL
import { multiply } from './multiply.js'
import { reduce } from './reduce.js'
export const product = reduce(multiply, 1)
import { product } from './product.js'
test('happy', () => {
expect(product([ 2, 3, 4 ])).toBe(24)
})
test('bad input', () => {
expect(product([ null ])).toBe(0)
expect(product([])).toBe(1)
})
prop<_, P extends keyof never, T>(p: P, value: T): Prop<T, P>
It returns the value of property propToFind
in obj
.
If there is no such property, it returns undefined
.
const result = [
R.prop('x', {x: 100}),
R.prop('x', {a: 1})
]
// => [100, undefined]
Try this R.prop example in Rambda REPL
export function propFn(searchProperty, obj){
if (!obj) return undefined
return obj[ searchProperty ]
}
export function prop(searchProperty, obj){
if (arguments.length === 1) return _obj => prop(searchProperty, _obj)
return propFn(searchProperty, obj)
}
import { prop } from './prop.js'
test('prop', () => {
expect(prop('foo')({ foo : 'baz' })).toBe('baz')
expect(prop('bar')({ foo : 'baz' })).toBeUndefined()
expect(prop('bar')(null)).toBeUndefined()
})
propEq<K extends string | number>(valueToMatch: any, propToFind: K, obj: Record<K, any>): boolean
It returns true if obj
has property propToFind
and its value is equal to valueToMatch
.
const obj = { foo: 'bar' }
const secondObj = { foo: 1 }
const propToFind = 'foo'
const valueToMatch = 'bar'
const result = [
R.propEq(propToFind, valueToMatch, Record<string, unknown>),
R.propEq(propToFind, valueToMatch, secondRecord<string, unknown>)
]
// => [true, false]
Try this R.propEq example in Rambda REPL
import { curry } from './curry.js'
import { equals } from './equals.js'
import { prop } from './prop.js'
function propEqFn(
valueToMatch, propToFind, obj
){
if (!obj) return false
return equals(valueToMatch, prop(propToFind, obj))
}
export const propEq = curry(propEqFn)
import { BAR, FOO } from './_internals/testUtils.js'
import { propEq } from './propEq.js'
test('happy', () => {
const obj = { [ FOO ] : BAR }
expect(propEq(BAR, FOO)(obj)).toBeTrue()
expect(propEq(1, FOO)(obj)).toBeFalse()
expect(propEq(1)(FOO)(obj)).toBeFalse()
expect(propEq(
1, 1, null
)).toBeFalse()
})
test('returns false if called with a null or undefined object', () => {
expect(propEq(
'name', 'Abby', null
)).toBeFalse()
expect(propEq(
'name', 'Abby', undefined
)).toBeFalse()
})
propIs<C extends AnyFunction, K extends keyof any>(type: C, name: K, obj: any): obj is Record<K, ReturnType<C>>
It returns true
if property
of obj
is from target
type.
const obj = {a:1, b: 'foo'}
const result = [
R.propIs(Number, 'a', Record<string, unknown>),
R.propIs(String, 'b', Record<string, unknown>),
R.propIs(Number, 'b', Record<string, unknown>),
]
// => [true, true, false]
Try this R.propIs example in Rambda REPL
import { curry } from './curry.js'
import { is } from './is.js'
function propIsFn(
targetPrototype, property, obj
){
return is(targetPrototype, obj[ property ])
}
export const propIs = curry(propIsFn)
import { propIs } from './propIs.js'
const obj = {
a : 1,
b : 'foo',
}
test('when true', () => {
expect(propIs(
Number, 'a', obj
)).toBeTrue()
expect(propIs(
String, 'b', obj
)).toBeTrue()
})
test('when false', () => {
expect(propIs(
String, 'a', obj
)).toBeFalse()
expect(propIs(
Number, 'b', obj
)).toBeFalse()
})
propOr<T, P extends string>(defaultValue: T, property: P, obj: Partial<Record<P, T>> | undefined): T
It returns either defaultValue
or the value of property
in obj
.
const obj = {a: 1}
const defaultValue = 'DEFAULT_VALUE'
const property = 'a'
const result = [
R.propOr(defaultValue, property, Record<string, unknown>),
R.propOr(defaultValue, 'foo', Record<string, unknown>)
]
// => [1, 'DEFAULT_VALUE']
Try this R.propOr example in Rambda REPL
import { curry } from './curry.js'
import { defaultTo } from './defaultTo.js'
function propOrFn(
defaultValue, property, obj
){
if (!obj) return defaultValue
return defaultTo(defaultValue, obj[ property ])
}
export const propOr = curry(propOrFn)
import { propOr } from './propOr.js'
test('propOr (result)', () => {
const obj = { a : 1 }
expect(propOr(
'default', 'a', obj
)).toBe(1)
expect(propOr(
'default', 'notExist', obj
)).toBe('default')
expect(propOr(
'default', 'notExist', null
)).toBe('default')
})
test('propOr (currying)', () => {
const obj = { a : 1 }
expect(propOr('default')('a', obj)).toBe(1)
expect(propOr('default', 'a')(obj)).toBe(1)
expect(propOr('default')('notExist', obj)).toBe('default')
expect(propOr('default', 'notExist')(obj)).toBe('default')
})
props<P extends string, T>(propsToPick: P[], obj: Record<P, T>): T[]
It takes list with properties propsToPick
and returns a list with property values in obj
.
const result = R.props(
['a', 'b'],
{a:1, c:3}
)
// => [1, undefined]
Try this R.props example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { mapArray } from './map.js'
export function props(propsToPick, obj){
if (arguments.length === 1){
return _obj => props(propsToPick, _obj)
}
if (!isArray(propsToPick)){
throw new Error('propsToPick is not a list')
}
return mapArray(prop => obj[ prop ], propsToPick)
}
import { props } from './props.js'
const obj = {
a : 1,
b : 2,
}
const propsToPick = [ 'a', 'c' ]
test('happy', () => {
const result = props(propsToPick, obj)
expect(result).toEqual([ 1, undefined ])
})
test('curried', () => {
const result = props(propsToPick)(obj)
expect(result).toEqual([ 1, undefined ])
})
test('wrong input', () => {
expect(() => props(null)(obj)).toThrowErrorMatchingInlineSnapshot('"propsToPick is not a list"')
})
propSatisfies<T>(predicate: Predicate<T>, property: string, obj: Record<string, T>): boolean
It returns true
if the object property satisfies a given predicate.
const obj = {a: {b:1}}
const property = 'a'
const predicate = x => x?.b === 1
const result = R.propSatisfies(predicate, property, Record<string, unknown>)
// => true
Try this R.propSatisfies example in Rambda REPL
import { curry } from './curry.js'
import { prop } from './prop.js'
function propSatisfiesFn(
predicate, property, obj
){
return predicate(prop(property, obj))
}
export const propSatisfies = curry(propSatisfiesFn)
import { propSatisfies } from './propSatisfies.js'
const obj = { a : 1 }
test('when true', () => {
expect(propSatisfies(
x => x > 0, 'a', obj
)).toBeTrue()
})
test('when false', () => {
expect(propSatisfies(x => x < 0, 'a')(obj)).toBeFalse()
})
random(minInclusive: number, maxInclusive: number): number
It returns a random number between min
inclusive and max
inclusive.
export function random(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min
}
import { random } from './random.js'
import { range } from './range.js'
test('when returns true', () => {
range(0, 100).map(() => {
const randomResult = random(1, 10)
expect(randomResult).toBeLessThanOrEqual(10)
expect(randomResult).toBeGreaterThanOrEqual(1)
})
})
range(startInclusive: number, endExclusive: number): number[]
It returns list of numbers between startInclusive
to endExclusive
markers.
R.range(0, 5)
// => [0, 1, 2, 3, 4]
Try this R.range example in Rambda REPL
export function range(start, end){
if (arguments.length === 1) return _end => range(start, _end)
if (Number.isNaN(Number(start)) || Number.isNaN(Number(end))){
throw new TypeError('Both arguments to range must be numbers')
}
if (end < start) return []
const len = end - start
const willReturn = Array(len)
for (let i = 0; i < len; i++){
willReturn[ i ] = start + i
}
return willReturn
}
import { range } from './range.js'
test('happy', () => {
expect(range(0, 10)).toEqual([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ])
})
test('end range is bigger than start range', () => {
expect(range(7, 3)).toEqual([])
expect(range(5, 5)).toEqual([])
})
test('with bad input', () => {
const throwMessage = 'Both arguments to range must be numbers'
expect(() => range('a', 6)).toThrowWithMessage(Error, throwMessage)
expect(() => range(6, 'z')).toThrowWithMessage(Error, throwMessage)
})
test('curry', () => {
expect(range(0)(10)).toEqual([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ])
})
:boom: It passes index of the list as third argument to
reducer
function.
Try this R.reduce example in Rambda REPL
Try this R.reduceBy example in Rambda REPL
reject<T>(predicate: Predicate<T>, list: T[]): T[]
It has the opposite effect of R.filter
.
const list = [1, 2, 3, 4]
const obj = {a: 1, b: 2}
const predicate = x => x > 1
const result = [
R.reject(predicate, list),
R.reject(predicate, Record<string, unknown>)
]
// => [[1], {a: 1}]
Try this R.reject example in Rambda REPL
import { filter } from './filter.js'
export function reject(predicate, list){
if (arguments.length === 1) return _list => reject(predicate, _list)
return filter(x => !predicate(x), list)
}
import { reject } from './reject.js'
const isOdd = n => n % 2 === 1
test('with array', () => {
expect(reject(isOdd)([ 1, 2, 3, 4 ])).toEqual([ 2, 4 ])
})
test('with object', () => {
const obj = {
a : 1,
b : 2,
c : 3,
d : 4,
}
expect(reject(isOdd, obj)).toEqual({
b : 2,
d : 4,
})
})
Same as R.reject
, but it passes index/property as second argument to the predicate, when looping over arrays/objects.
Try this R.rejectIndexed example in Rambda REPL
It will remove all toRemove
entries from text
sequentially.
toRemove
argument can be either a list of strings/regular expressions or a single string/regular expression.
:boom: This is the only case where Rambdax exports clashes with Ramda API, as Ramda has
remove
method. IfRambda.remove
is introduced, then this method will be renamed.
Try this R.remove example in Rambda REPL
removeIndex<T>(index: number, list: T[]): T[]
It returns a copy of list
input with removed index
.
const list = [1, 2, 3, 4]
const result = R.removeIndex(1, list)
// => [1, 3, 4]
Try this R.removeIndex example in Rambda REPL
export function removeIndex(index, list){
if (arguments.length === 1) return _list => removeIndex(index, _list)
if (index <= 0) return list.slice(1)
if (index >= list.length - 1) return list.slice(0, list.length - 1)
return [ ...list.slice(0, index), ...list.slice(index + 1) ]
}
import { removeIndex } from './removeIndex.js'
const list = [ 1, 2, 3, 4 ]
test('first or before first index', () => {
expect(removeIndex(-2, list)).toEqual([ 2, 3, 4 ])
expect(removeIndex(-2)(list)).toEqual([ 2, 3, 4 ])
})
test('last or after last index', () => {
expect(removeIndex(4, list)).toEqual([ 1, 2, 3 ])
expect(removeIndex(10, list)).toEqual([ 1, 2, 3 ])
})
test('middle index', () => {
expect(removeIndex(1, list)).toEqual([ 1, 3, 4 ])
expect(removeIndex(2, list)).toEqual([ 1, 2, 4 ])
})
renameProps(rules: object, input: object): object
If property prop
of rules
is also a property in input
, then rename input
property to rules[prop]
.
import { mergeRight } from './mergeRight.js'
import { omit } from './omit.js'
export function renameProps(conditions, inputObject){
if (arguments.length === 1){
return inputObjectHolder => renameProps(conditions, inputObjectHolder)
}
const renamed = {}
Object.keys(conditions).forEach(condition => {
if (Object.keys(inputObject).includes(condition)){
renamed[ conditions[ condition ] ] = inputObject[ condition ]
}
})
return mergeRight(renamed, omit(Object.keys(conditions), inputObject))
}
import { renameProps } from './renameProps.js'
test('renameProps', () => {
const rules = {
f : 'foo',
b : 'bar',
q : 'x',
}
const input = {
f : 1,
b : 2,
a : 3,
}
const result = renameProps(rules, input)
const expectedResult = {
foo : 1,
bar : 2,
a : 3,
}
expect(result).toEqual(expectedResult)
})
test('curry', () => {
const rules = {
f : 'foo',
b : 'bar',
}
const input = {
f : 1,
b : 2,
}
const result = renameProps(rules)(input)
const expectedResult = {
foo : 1,
bar : 2,
}
expect(result).toEqual(expectedResult)
})
repeat<T>(x: T): (timesToRepeat: number) => T[]
R.repeat('foo', 3)
// => ['foo', 'foo', 'foo']
Try this R.repeat example in Rambda REPL
export function repeat(x, timesToRepeat){
if (arguments.length === 1){
return _timesToRepeat => repeat(x, _timesToRepeat)
}
return Array(timesToRepeat).fill(x)
}
import { repeat } from './repeat.js'
test('repeat', () => {
expect(repeat('')(3)).toEqual([ '', '', '' ])
expect(repeat('foo', 3)).toEqual([ 'foo', 'foo', 'foo' ])
const obj = {}
const arr = repeat(obj, 3)
expect(arr).toEqual([ {}, {}, {} ])
expect(arr[ 0 ] === arr[ 1 ]).toBeTrue()
})
replace(strOrRegex: RegExp | string, replacer: RegExpReplacer, str: string): string
It replaces strOrRegex
found in str
with replacer
.
const strOrRegex = /o/g
const result = R.replace(strOrRegex, '|0|', 'foo')
// => 'f|0||0|'
Try this R.replace example in Rambda REPL
import { curry } from './curry.js'
function replaceFn(
pattern, replacer, str
){
return str.replace(pattern, replacer)
}
export const replace = curry(replaceFn)
import { replace } from './replace.js'
test('happy', () => {
expect(replace(
/\s/g, '|', 'foo bar baz'
)).toBe('foo|bar|baz')
})
test('with function as replacer input', () => {
expect(replace(
/\s/g,
(
match, offset, str
) => {
expect(match).toBe(' ')
expect([ 3, 7 ].includes(offset)).toBeTrue()
expect(str).toBe('foo bar baz')
return '|'
},
'foo bar baz'
)).toBe('foo|bar|baz')
})
replaceAll(patterns: (RegExp | string)[], replacer: string, input: string): string
Same as R.replace
but it accepts array of string and regular expressions instead of a single value.
const replacer = '|'
const patterns = [ /foo/g, 'bar' ]
const input = 'foo bar baz foo bar'
const result = R.replaceAll(patterns, replacer, input)
// => '| | baz | bar'
Try this R.replaceAll example in Rambda REPL
import { curry } from './curry.js'
import { ok } from './ok.js'
function replaceAllFn(
patterns, replacer, input
){
ok(
patterns, replacer, input
)(
Array, String, String
)
let text = input
patterns.forEach(singlePattern => {
text = text.replace(singlePattern, replacer)
})
return text
}
export const replaceAll = curry(replaceAllFn)
import {replaceAll} from './replaceAll.js'
const replacer = '|'
const patterns = [/foo/g, 'bar']
const input = 'foo bar baz foo bar'
test('happy', () => {
const result = replaceAll(patterns, replacer, input)
const expected = '| | baz | bar'
expect(result).toEqual(expected)
})
test('throws when wrong patterns', () => {
expect(() => replaceAll({}, replacer, input))
.toThrowErrorMatchingInlineSnapshot(`
"Failed R.ok -
reason: {"input":{},"schema":"array"}
all inputs: [{},"|","foo bar baz foo bar"]
all schemas: ["array","string","string"]"
`)
})
test('throws when wrong input', () => {
expect(() => replaceAll(patterns, replacer, []))
.toThrowErrorMatchingInlineSnapshot(`
"Failed R.ok -
reason: {"input":[],"schema":"string"}
all inputs: [[{},"bar"],"|",[]]
all schemas: ["array","string","string"]"
`)
})
test('throws when wrong replacer', () => {
expect(() => replaceAll(patterns, null, input))
.toThrowErrorMatchingInlineSnapshot(`
"Failed R.ok -
reason: {"input":null,"schema":"string"}
all inputs: [[{},"bar"],null,"foo bar baz foo bar"]
all schemas: ["array","string","string"]"
`)
})
reset(): void
:boom:
R.getter
method contains explanations, tests and source information ofR.reset
,R.setter
andR.getter
methods.
reverse<T>(input: T[]): T[]
It returns a reversed copy of list or string input
.
const result = [
R.reverse('foo'),
R.reverse([1, 2, 3])
]
// => ['oof', [3, 2, 1]
Try this R.reverse example in Rambda REPL
export function reverse(listOrString) {
if (typeof listOrString === 'string') {
return listOrString.split('').reverse().join('')
}
const clone = listOrString.slice()
return clone.reverse()
}
import {reverse} from './reverse.js'
test('happy', () => {
expect(reverse([1, 2, 3])).toEqual([3, 2, 1])
})
test('with string', () => {
expect(reverse('baz')).toBe('zab')
})
test("it doesn't mutate", () => {
const arr = [1, 2, 3]
expect(reverse(arr)).toEqual([3, 2, 1])
expect(arr).toEqual([1, 2, 3])
})
set<S, A>(lens: Lens<S, A>): {
(a: A): (obj: S) => S
(a: A, obj: S): S
}
It returns a copied Object or Array with modified lens
focus set to replacer
value.
const input = {x: 1, y: 2}
const xLens = R.lensProp('x')
const result = [
R.set(xLens, 4, input),
R.set(xLens, 8, input)
]
// => [{x: 4, y: 2}, {x: 8, y: 2}]
Try this R.set example in Rambda REPL
import {always} from './always.js'
import {curry} from './curry.js'
import {over} from './over.js'
function setFn(lens, replacer, x) {
return over(lens, always(replacer), x)
}
export const set = curry(setFn)
import {assoc} from './assoc.js'
import {lens} from './lens.js'
import {lensIndex} from './lensIndex.js'
import {lensPath} from './lensPath.js'
import {prop} from './prop.js'
import {set} from './set.js'
const testObject = {
foo: 'bar',
baz: {
a: 'x',
b: 'y',
},
}
test('assoc lens', () => {
const assocLens = lens(prop('foo'), assoc('foo'))
const result = set(assocLens, 'FOO', testObject)
const expected = {
...testObject,
foo: 'FOO',
}
expect(result).toEqual(expected)
})
test('path lens', () => {
const pathLens = lensPath('baz.a')
const result = set(pathLens, 'z', testObject)
const expected = {
...testObject,
baz: {
a: 'z',
b: 'y',
},
}
expect(result).toEqual(expected)
})
test('index lens', () => {
const indexLens = lensIndex(0)
const result = set(indexLens, 3, [1, 2])
expect(result).toEqual([3, 2])
})
setter(keyOrObject: string | object, value?: any): void
:boom:
R.getter
method contains explanations, tests and source information ofR.reset
,R.setter
andR.getter
methods.
shuffle<T>(list: T[]): T[]
It returns a randomized copy of array.
export function shuffle(arrayRaw){
const array = arrayRaw.concat()
let counter = array.length
while (counter > 0){
const index = Math.floor(Math.random() * counter)
counter--
const temp = array[ counter ]
array[ counter ] = array[ index ]
array[ index ] = temp
}
return array
}
import { range } from './range.js'
import { shuffle } from './shuffle.js'
import { uniq } from './uniq.js'
test('happy', () => {
const list = range(0, 7)
const result = range(0, 300).map(() => shuffle(list))
const allUniq = uniq(result)
expect(allUniq.length > 150).toBeTrue()
})
slice(from: number, to: number, input: string): string
const list = [0, 1, 2, 3, 4, 5]
const str = 'FOO_BAR'
const from = 1
const to = 4
const result = [
R.slice(from, to, str),
R.slice(from, to, list)
]
// => ['OO_', [1, 2, 3]]
Try this R.slice example in Rambda REPL
import { curry } from './curry.js'
function sliceFn(
from, to, list
){
return list.slice(from, to)
}
export const slice = curry(sliceFn)
import { slice } from './slice.js'
test('slice', () => {
expect(slice(
1, 3, [ 'a', 'b', 'c', 'd' ]
)).toEqual([ 'b', 'c' ])
expect(slice(
1, Infinity, [ 'a', 'b', 'c', 'd' ]
)).toEqual([ 'b', 'c', 'd' ])
expect(slice(
0, -1, [ 'a', 'b', 'c', 'd' ]
)).toEqual([ 'a', 'b', 'c' ])
expect(slice(
-3, -1, [ 'a', 'b', 'c', 'd' ]
)).toEqual([ 'b', 'c' ])
expect(slice(
0, 3, 'ramda'
)).toBe('ram')
})
sort<T>(sortFn: (a: T, b: T) => number, list: T[]): T[]
It returns copy of list
sorted by sortFn
function, where sortFn
needs to return only -1
, 0
or 1
.
const list = [
{a: 2},
{a: 3},
{a: 1}
]
const sortFn = (x, y) => {
return x.a > y.a ? 1 : -1
}
const result = R.sort(sortFn, list)
const expected = [
{a: 1},
{a: 2},
{a: 3}
]
// => `result` is equal to `expected`
Try this R.sort example in Rambda REPL
import { cloneList } from './_internals/cloneList.js'
export function sort(sortFn, list){
if (arguments.length === 1) return _list => sort(sortFn, _list)
return cloneList(list).sort(sortFn)
}
import { sort } from './sort.js'
const fn = (a, b) => a > b ? 1 : -1
test('sort', () => {
expect(sort((a, b) => a - b)([ 2, 3, 1 ])).toEqual([ 1, 2, 3 ])
})
test('it doesn\'t mutate', () => {
const list = [ 'foo', 'bar', 'baz' ]
expect(sort(fn, list)).toEqual([ 'bar', 'baz', 'foo' ])
expect(list[ 0 ]).toBe('foo')
expect(list[ 1 ]).toBe('bar')
expect(list[ 2 ]).toBe('baz')
})
sortBy<T>(sortFn: (a: T) => Ord, list: T[]): T[]
It returns copy of list
sorted by sortFn
function, where sortFn
function returns a value to compare, i.e. it doesn't need to return only -1
, 0
or 1
.
const list = [
{a: 2},
{a: 3},
{a: 1}
]
const sortFn = x => x.a
const result = R.sortBy(sortFn, list)
const expected = [
{a: 1},
{a: 2},
{a: 3}
]
// => `result` is equal to `expected`
Try this R.sortBy example in Rambda REPL
import { cloneList } from './_internals/cloneList.js'
export function sortBy(sortFn, list){
if (arguments.length === 1) return _list => sortBy(sortFn, _list)
const clone = cloneList(list)
return clone.sort((a, b) => {
const aSortResult = sortFn(a)
const bSortResult = sortFn(b)
if (aSortResult === bSortResult) return 0
return aSortResult < bSortResult ? -1 : 1
})
}
import { compose } from './compose.js'
import { prop } from './prop.js'
import { sortBy } from './sortBy.js'
import { toLower } from './toLower.js'
test('happy', () => {
const input = [ { a : 2 }, { a : 1 }, { a : 1 }, { a : 3 } ]
const expected = [ { a : 1 }, { a : 1 }, { a : 2 }, { a : 3 } ]
const result = sortBy(x => x.a)(input)
expect(result).toEqual(expected)
})
test('with compose', () => {
const alice = {
name : 'ALICE',
age : 101,
}
const bob = {
name : 'Bob',
age : -10,
}
const clara = {
name : 'clara',
age : 314.159,
}
const people = [ clara, bob, alice ]
const sortByNameCaseInsensitive = sortBy(compose(toLower, prop('name')))
expect(sortByNameCaseInsensitive(people)).toEqual([ alice, bob, clara ])
})
sortByPath<T>(sortPath: Path, list: T[]): T[]
It returns copy of list
sorted by sortPath
value.
As sortPath
is passed to R.path
, it can be either a string or an array of strings.
const list = [
{a: {b: 2}},
{a: {b: 1}},
{a: {b: 3}}
]
const result = R.sortByPath('a.b', list)
const expected = [
{a: {b: 1}},
{a: {b: 2}},
{a: {b: 3}}
]
// => `result` is equal to `expected`
Try this R.sortByPath example in Rambda REPL
import { path } from './path.js'
import { sortBy } from './sortBy.js'
export function sortByPath(sortPath, list){
if (arguments.length === 1) return _list => sortByPath(sortPath, _list)
return sortBy(path(sortPath), list)
}
import { sortByPath } from './sortByPath.js'
const list = [ { a : { b : 3 } }, { a : { b : 1 } }, { a : { b : 2 } } ]
const sorted = [ { a : { b : 1 } }, { a : { b : 2 } }, { a : { b : 3 } } ]
test('with string as path', () => {
expect(sortByPath('a.b', list)).toEqual(sorted)
})
test('with list of strings as path', () => {
expect(sortByPath([ 'a', 'b' ], list)).toEqual(sorted)
})
test('with string as path - curried', () => {
expect(sortByPath('a.b')(list)).toEqual(sorted)
})
test('with list of strings as path - curried', () => {
expect(sortByPath([ 'a', 'b' ])(list)).toEqual(sorted)
})
sortByProps<T>(sortPaths: string[], list: T[]): T[]
It returns sorted copy of list
of objects.
Sorting is done using a list of strings, each representing a path. Two members a
and b
from list
can be sorted if both return a value for a given path. If the value is equal, then the next member of sortPaths
(if there is such) will be used in order to find difference between a
and b
.
const list = [
{a: {b: 2}},
{a: {b: 1}},
{a: {b: 3}}
]
const result = R.sortByProps(['a.b'], list)
const expected = [
{a: {b: 1}},
{a: {b: 2}},
{a: {b: 3}}
]
// => `result` is equal to `expected`
Try this R.sortByProps example in Rambda REPL
import { path } from './path.js'
function singleSort(
a, b, sortPaths
){
let toReturn = 0
sortPaths.forEach(singlePath => {
if (toReturn !== 0) return
const aResult = path(singlePath, a)
const bResult = path(singlePath, b)
if ([ aResult, bResult ].includes(undefined)) return
if (aResult === bResult) return
toReturn = aResult > bResult ? 1 : -1
})
return toReturn
}
export function sortByProps(sortPaths, list){
if (arguments.length === 1) return _list => sortByProps(sortPaths, _list)
const clone = list.slice()
clone.sort((a, b) => singleSort(
a, b, sortPaths
))
return clone
}
import { sortByProps } from './sortByProps.js'
const list = [ { a : { b : 3 } }, { a : { b : 2 } }, { a : { b : 1 } } ]
const sorted = [ { a : { b : 1 } }, { a : { b : 2 } }, { a : { b : 3 } } ]
test('wrong paths are ignored', () => {
expect(sortByProps([ 'foo.bar', 'a.c', 'a.b', 'a.d' ], list)).toEqual(sorted)
})
test('skip sort when path results are equal', () => {
const input = [
{
a : {
b : 0,
c : 2,
},
},
{
a : {
b : 0,
c : 1,
},
},
]
expect(sortByProps([ 'a.b', 'a.d' ], input)).toEqual(input)
})
test('when list is already sorted', () => {
const input = [
{
a : {
b : 0,
c : 1,
},
},
{
a : {
b : 0,
c : 2,
},
},
]
expect(sortByProps([ 'a.b', 'a.c' ])(input)).toEqual(input)
})
sortObject<T>(predicate: SortObjectPredicate<T>, input: { [key: string]: T }): { [keyOutput: string]: T }
It returns a sorted version of input
object.
const predicate = (propA, propB, valueA, valueB) => valueA > valueB ? -1 : 1
const result = R.sortObject(predicate, {a:1, b: 4, c: 2})
// => {b: 4, c: 2, a: 1}
Try this R.sortObject example in Rambda REPL
import { sort } from './sort.js'
export function sortObject(predicate, obj){
if (arguments.length === 1){
return _obj => sortObject(predicate, _obj)
}
const keys = Object.keys(obj)
const sortedKeys = sort((a, b) => predicate(
a, b, obj[ a ], obj[ b ]
), keys)
const toReturn = {}
sortedKeys.forEach(singleKey => {
toReturn[ singleKey ] = obj[ singleKey ]
})
return toReturn
}
import { runTests } from 'helpers-fn'
import { allTrue } from './allTrue.js'
import { equals } from './equals.js'
import { sortObject } from './sortObject.js'
const obj = {
c : 1,
a : 2,
b : 3,
}
const predicateA = (
propA, propB, valueA, valueB
) => propA > propB ? -1 : 1
const expectationA = [ 'c', 'b', 'a' ]
const predicateB = (
propA, propB, valueA, valueB
) => propA < propB ? -1 : 1
const expectationB = [ 'a', 'b', 'c' ]
const predicateC = (
propA, propB, valueA, valueB
) =>
valueA > valueB ? -1 : 1
const expectationC = [ 'b', 'a', 'c' ]
const fn = ([ predicate, expectation ]) => {
const result = sortObject(predicate, obj)
const curriedResult = sortObject(predicate)(obj)
const sortedKeys = Object.keys(result)
const sortedKeysCurried = Object.keys(curriedResult)
const isSameObject = equals(obj, result)
const isSameObjectCurried = equals(obj, curriedResult)
return allTrue(
isSameObject,
isSameObjectCurried,
equals(sortedKeys, expectation),
equals(sortedKeysCurried, expectation)
)
}
const testData = {
label : 'foo',
data : [
{ ok : [ predicateA, expectationA ] },
{ ok : [ predicateB, expectationB ] },
{ ok : [ predicateC, expectationC ] },
],
fn,
}
runTests(testData)
Try this R.sortWith example in Rambda REPL
split(separator: string | RegExp): (str: string) => string[]
Curried version of String.prototype.split
const str = 'foo|bar|baz'
const separator = '|'
const result = R.split(separator, str)
// => [ 'foo', 'bar', 'baz' ]
Try this R.split example in Rambda REPL
export function split(separator, str){
if (arguments.length === 1) return _str => split(separator, _str)
return str.split(separator)
}
import { split } from './split.js'
const str = 'foo|bar|baz'
const splitChar = '|'
const expected = [ 'foo', 'bar', 'baz' ]
test('happy', () => {
expect(split(splitChar, str)).toEqual(expected)
})
test('curried', () => {
expect(split(splitChar)(str)).toEqual(expected)
})
splitAt<T>(index: number, input: T[]): [T[], T[]]
It splits string or array at a given index.
const list = [ 1, 2, 3 ]
const result = R.splitAt(2, list)
// => [[ 1, 2 ], [ 3 ]]
Try this R.splitAt example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { drop } from './drop.js'
import { maybe } from './maybe.js'
import { take } from './take.js'
export function splitAt(index, input){
if (arguments.length === 1){
return _list => splitAt(index, _list)
}
if (!input) throw new TypeError(`Cannot read property 'slice' of ${ input }`)
if (!isArray(input) && typeof input !== 'string') return [ [], [] ]
const correctIndex = maybe(
index < 0,
input.length + index < 0 ? 0 : input.length + index,
index
)
return [ take(correctIndex, input), drop(correctIndex, input) ]
}
import { splitAt as splitAtRamda } from 'ramda'
import { splitAt } from './splitAt.js'
const list = [ 1, 2, 3 ]
const str = 'foo bar'
test('with array', () => {
const result = splitAt(2, list)
expect(result).toEqual([ [ 1, 2 ], [ 3 ] ])
})
test('with array - index is negative number', () => {
const result = splitAt(-6, list)
expect(result).toEqual([ [], list ])
})
test('with array - index is out of scope', () => {
const result = splitAt(4, list)
expect(result).toEqual([ [ 1, 2, 3 ], [] ])
})
test('with string', () => {
const result = splitAt(4, str)
expect(result).toEqual([ 'foo ', 'bar' ])
})
test('with string - index is negative number', () => {
const result = splitAt(-2, str)
expect(result).toEqual([ 'foo b', 'ar' ])
})
test('with string - index is out of scope', () => {
const result = splitAt(10, str)
expect(result).toEqual([ str, '' ])
})
test('with array - index is out of scope', () => {
const result = splitAt(4)(list)
expect(result).toEqual([ [ 1, 2, 3 ], [] ])
})
const badInputs = [ 1, true, /foo/g, {} ]
const throwingBadInputs = [ null, undefined ]
test('with bad inputs', () => {
throwingBadInputs.forEach(badInput => {
expect(() => splitAt(1, badInput)).toThrowWithMessage(TypeError,
`Cannot read property 'slice' of ${ badInput }`)
expect(() => splitAtRamda(1, badInput)).toThrowWithMessage(TypeError,
`Cannot read properties of ${ badInput } (reading 'slice')`)
})
badInputs.forEach(badInput => {
const result = splitAt(1, badInput)
const ramdaResult = splitAtRamda(1, badInput)
expect(result).toEqual(ramdaResult)
})
})
splitEvery<T>(sliceLength: number, input: T[]): (T[])[]
It splits input
into slices of sliceLength
.
const result = [
R.splitEvery(2, [1, 2, 3]),
R.splitEvery(3, 'foobar')
]
const expected = [
[[1, 2], [3]],
['foo', 'bar']
]
// => `result` is equal to `expected`
Try this R.splitEvery example in Rambda REPL
export function splitEvery(sliceLength, listOrString){
if (arguments.length === 1){
return _listOrString => splitEvery(sliceLength, _listOrString)
}
if (sliceLength < 1){
throw new Error('First argument to splitEvery must be a positive integer')
}
const willReturn = []
let counter = 0
while (counter < listOrString.length){
willReturn.push(listOrString.slice(counter, counter += sliceLength))
}
return willReturn
}
import { splitEvery } from './splitEvery.js'
test('happy', () => {
expect(splitEvery(3, [ 1, 2, 3, 4, 5, 6, 7 ])).toEqual([
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7 ],
])
expect(splitEvery(3)('foobarbaz')).toEqual([ 'foo', 'bar', 'baz' ])
})
test('with bad input', () => {
expect(() =>
expect(splitEvery(0)('foo')).toEqual([ 'f', 'o', 'o' ])).toThrowErrorMatchingInlineSnapshot('"First argument to splitEvery must be a positive integer"')
})
splitWhen<T, U>(predicate: Predicate<T>, list: U[]): (U[])[]
It splits list
to two arrays according to a predicate
function.
The first array contains all members of list
before predicate
returns true
.
const list = [1, 2, 1, 2]
const result = R.splitWhen(R.equals(2), list)
// => [[1], [2, 1, 2]]
Try this R.splitWhen example in Rambda REPL
export function splitWhen(predicate, input){
if (arguments.length === 1){
return _input => splitWhen(predicate, _input)
}
if (!input)
throw new TypeError(`Cannot read property 'length' of ${ input }`)
const preFound = []
const postFound = []
let found = false
let counter = -1
while (counter++ < input.length - 1){
if (found){
postFound.push(input[ counter ])
} else if (predicate(input[ counter ])){
postFound.push(input[ counter ])
found = true
} else {
preFound.push(input[ counter ])
}
}
return [ preFound, postFound ]
}
import { splitWhen as splitWhenRamda } from 'ramda'
import { equals } from './equals.js'
import { splitWhen } from './splitWhen.js'
const list = [ 1, 2, 1, 2 ]
test('happy', () => {
const result = splitWhen(equals(2), list)
expect(result).toEqual([ [ 1 ], [ 2, 1, 2 ] ])
})
test('when predicate returns false', () => {
const result = splitWhen(equals(3))(list)
expect(result).toEqual([ list, [] ])
})
const badInputs = [ 1, true, /foo/g, {} ]
const throwingBadInputs = [ null, undefined ]
test('with bad inputs', () => {
throwingBadInputs.forEach(badInput => {
expect(() => splitWhen(equals(2), badInput)).toThrowWithMessage(TypeError,
`Cannot read property 'length' of ${ badInput }`)
expect(() => splitWhenRamda(equals(2), badInput)).toThrowWithMessage(TypeError,
`Cannot read properties of ${ badInput } (reading 'length')`)
})
badInputs.forEach(badInput => {
const result = splitWhen(equals(2), badInput)
const ramdaResult = splitWhenRamda(equals(2), badInput)
expect(result).toEqual(ramdaResult)
})
})
startsWith<T extends string>(question: T, input: string): boolean
When iterable is a string, then it behaves as String.prototype.startsWith
.
When iterable is a list, then it uses R.equals to determine if the target list starts in the same way as the given target.
:boom: It doesn't work with arrays unlike its corresponding Ramda method.
const str = 'foo-bar'
const list = [{a:1}, {a:2}, {a:3}]
const result = [
R.startsWith('foo', str),
R.startsWith([{a:1}, {a:2}], list)
]
// => [true, true]
Try this R.startsWith example in Rambda REPL
import { isArray } from './_internals/isArray.js'
import { equals } from './equals.js'
export function startsWith(question, iterable){
if (arguments.length === 1)
return _iterable => startsWith(question, _iterable)
if (typeof iterable === 'string'){
return iterable.startsWith(question)
}
if (!isArray(question)) return false
let correct = true
const filtered = question.filter((x, index) => {
if (!correct) return false
const result = equals(x, iterable[ index ])
if (!result) correct = false
return result
})
return filtered.length === question.length
}
import { startsWith as startsWithRamda } from 'ramda'
import { compareCombinations } from './_internals/testUtils.js'
import { possibleIterables, possibleTargets } from './endsWith.spec.js'
import { startsWith } from './startsWith.js'
test('with string', () => {
expect(startsWith('foo', 'foo-bar')).toBeTrue()
expect(startsWith('baz')('foo-bar')).toBeFalse()
})
test('use R.equals with array', () => {
const list = [ { a : 1 }, { a : 2 }, { a : 3 } ]
expect(startsWith({ a : 1 }, list)).toBeFalse()
expect(startsWith([ { a : 1 } ], list)).toBeTrue()
expect(startsWith([ { a : 1 }, { a : 2 } ], list)).toBeTrue()
expect(startsWith(list, list)).toBeTrue()
expect(startsWith([ { a : 2 } ], list)).toBeFalse()
})
describe('brute force', () => {
compareCombinations({
fn : startsWith,
fnRamda : startsWithRamda,
firstInput : possibleTargets,
secondInput : possibleIterables,
callback : errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
{
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 0,
"RESULTS_MISMATCH": 0,
"SHOULD_NOT_THROW": 0,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 32,
}
`)
},
})
})
Curried version of x - y
Try this R.subtract example in Rambda REPL
sum(list: number[]): number
R.sum([1, 2, 3, 4, 5])
// => 15
Try this R.sum example in Rambda REPL
export function sum(list){
return list.reduce((prev, current) => prev + current, 0)
}
import { sum } from './sum.js'
test('happy', () => {
expect(sum([ 1, 2, 3, 4, 5 ])).toBe(15)
})
Try this R.swap example in Rambda REPL
switcher<T>(valueToMatch: any): Switchem<T>
Edited fork of Switchem library.
The method return a value if the matched option is a value.
If the matched option is a function, then R.switcher
returns a function which expects input. Tests of the method explain it better than this short description.
R.equals
is used to determine equality.
const valueToMatch = {foo: 1}
const result = R.switcher(valueToMatch)
.is('baz', 'is baz')
.is(x => typeof x === 'boolean', 'is boolean')
.is({foo: 1}, 'Property foo is 1')
.default('is bar')
// => 'Property foo is 1'
Try this R.switcher example in Rambda REPL
import { equals } from './equals.js'
const NO_MATCH_FOUND = Symbol ? Symbol('NO_MATCH_FOUND') : undefined
const getMatchingKeyValuePair = (
cases, testValue, defaultValue
) => {
let iterationValue
for (let index = 0; index < cases.length; index++){
iterationValue = cases[ index ].test(testValue)
if (iterationValue !== NO_MATCH_FOUND){
return iterationValue
}
}
return defaultValue
}
const isEqual = (testValue, matchValue) => {
const willReturn =
typeof testValue === 'function' ?
testValue(matchValue) :
equals(testValue, matchValue)
return willReturn
}
const is = (testValue, matchResult = true) => ({
key : testValue,
test : matchValue =>
isEqual(testValue, matchValue) ? matchResult : NO_MATCH_FOUND,
})
class Switchem{
constructor(
defaultValue, cases, willMatch
){
if (cases === undefined && willMatch === undefined){
this.cases = []
this.defaultValue = undefined
this.willMatch = defaultValue
} else {
this.cases = cases
this.defaultValue = defaultValue
this.willMatch = willMatch
}
return this
}
default(defaultValue){
const holder = new Switchem(
defaultValue, this.cases, this.willMatch
)
return holder.match(this.willMatch)
}
is(testValue, matchResult){
return new Switchem(
this.defaultValue,
[ ...this.cases, is(testValue, matchResult) ],
this.willMatch
)
}
match(matchValue){
return getMatchingKeyValuePair(
this.cases, matchValue, this.defaultValue
)
}
}
export function switcher(input){
return new Switchem(input)
}
import { add } from './add.js'
import { switcher } from './switcher.js'
import { tap } from './tap.js'
import { trim } from './trim.js'
test('with undefined', () => {
const result = switcher(undefined)
.is(x => x === 0, '0')
.is(x => x === undefined, 'UNDEFINED')
.default('3')
expect(result).toBe('UNDEFINED')
})
test('happy', () => {
const a = true
const b = false
const result = switcher([ a, b ])
.is([ false, false ], '0')
.is([ false, true ], '1')
.is([ true, true ], '2')
.default('3')
expect(result).toBe('3')
})
test('can compare objects', () => {
const result = switcher({ a : 1 })
.is({ a : 1 }, 'it is object')
.is('baz', 'it is baz')
.default('it is default')
expect(result).toBe('it is object')
})
test('options are mixture of functions and values - input match function', () => {
const fn = switcher('foo').is('bar', 1)
.is('foo', add(1))
.default(1000)
expect(fn(2)).toBe(3)
})
test('options are mixture of functions and values - input match value', () => {
const result = switcher('bar').is('bar', 1)
.is('foo', add(1))
.default(1000)
expect(result).toBe(1)
})
test('return function if all options are functions', () => {
const fn = switcher('foo').is('bar', tap)
.is('foo', add(1))
.default(trim)
expect(fn(2)).toBe(3)
})
const switchFn = input =>
switcher(input)
.is(x => x.length && x.length === 7, 'has length of 7')
.is('baz', 'it is baz')
.default('it is default')
test('works with function as condition', () => {
expect(switchFn([ 0, 1, 2, 3, 4, 5, 6 ])).toBe('has length of 7')
})
test('works with string as condition', () => {
expect(switchFn('baz')).toBe('it is baz')
})
test('fallback to default input when no matches', () => {
expect(switchFn(1)).toBe('it is default')
})
symmetricDifference<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.symmetricDifference(x, y)
// => [ 1, 2, 5, 6 ]
Try this R.symmetricDifference example in Rambda REPL
import { concat } from './concat.js'
import { filter } from './filter.js'
import { includes } from './includes.js'
export function symmetricDifference(x, y){
if (arguments.length === 1){
return _y => symmetricDifference(x, _y)
}
return concat(filter(value => !includes(value, y), x),
filter(value => !includes(value, x), y))
}
import { symmetricDifference } from './symmetricDifference.js'
test('symmetricDifference', () => {
const list1 = [ 1, 2, 3, 4 ]
const list2 = [ 3, 4, 5, 6 ]
expect(symmetricDifference(list1)(list2)).toEqual([ 1, 2, 5, 6 ])
expect(symmetricDifference([], [])).toEqual([])
})
test('symmetricDifference with objects', () => {
const list1 = [ { id : 1 }, { id : 2 }, { id : 3 }, { id : 4 } ]
const list2 = [ { id : 3 }, { id : 4 }, { id : 5 }, { id : 6 } ]
expect(symmetricDifference(list1)(list2)).toEqual([
{ id : 1 },
{ id : 2 },
{ id : 5 },
{ id : 6 },
])
})
T(): boolean
R.T()
// => true
Try this R.T example in Rambda REPL
export function T(){
return true
}
tail<T extends unknown[]>(input: T): T extends [any, ...infer U] ? U : [...T]
It returns all but the first element of input
.
const result = [
R.tail([1, 2, 3]),
R.tail('foo')
]
// => [[2, 3], 'oo']
Try this R.tail example in Rambda REPL
import { drop } from './drop.js'
export function tail(listOrString){
return drop(1, listOrString)
}
import { tail } from './tail.js'
test('tail', () => {
expect(tail([ 1, 2, 3 ])).toEqual([ 2, 3 ])
expect(tail([ 1, 2 ])).toEqual([ 2 ])
expect(tail([ 1 ])).toEqual([])
expect(tail([])).toEqual([])
expect(tail('abc')).toBe('bc')
expect(tail('ab')).toBe('b')
expect(tail('a')).toBe('')
expect(tail('')).toBe('')
})
take<T>(howMany: number, input: T[]): T[]
It returns the first howMany
elements of input
.
const howMany = 2
const result = [
R.take(howMany, [1, 2, 3]),
R.take(howMany, 'foobar'),
]
// => [[1, 2], 'fo']
Try this R.take example in Rambda REPL
import baseSlice from './_internals/baseSlice.js'
export function take(howMany, listOrString){
if (arguments.length === 1)
return _listOrString => take(howMany, _listOrString)
if (howMany < 0) return listOrString.slice()
if (typeof listOrString === 'string') return listOrString.slice(0, howMany)
return baseSlice(
listOrString, 0, howMany
)
}
import { take } from './take.js'
test('happy', () => {
const arr = [ 'foo', 'bar', 'baz' ]
expect(take(1, arr)).toEqual([ 'foo' ])
expect(arr).toEqual([ 'foo', 'bar', 'baz' ])
expect(take(2)([ 'foo', 'bar', 'baz' ])).toEqual([ 'foo', 'bar' ])
expect(take(3, [ 'foo', 'bar', 'baz' ])).toEqual([ 'foo', 'bar', 'baz' ])
expect(take(4, [ 'foo', 'bar', 'baz' ])).toEqual([ 'foo', 'bar', 'baz' ])
expect(take(3)('rambda')).toBe('ram')
})
test('with negative index', () => {
expect(take(-1, [ 1, 2, 3 ])).toEqual([ 1, 2, 3 ])
expect(take(-Infinity, [ 1, 2, 3 ])).toEqual([ 1, 2, 3 ])
})
test('with zero index', () => {
expect(take(0, [ 1, 2, 3 ])).toEqual([])
})
takeLast<T>(howMany: number, input: T[]): T[]
It returns the last howMany
elements of input
.
const howMany = 2
const result = [
R.takeLast(howMany, [1, 2, 3]),
R.takeLast(howMany, 'foobar'),
]
// => [[2, 3], 'ar']
Try this R.takeLast example in Rambda REPL
import baseSlice from './_internals/baseSlice.js'
export function takeLast(howMany, listOrString){
if (arguments.length === 1)
return _listOrString => takeLast(howMany, _listOrString)
const len = listOrString.length
if (howMany < 0) return listOrString.slice()
let numValue = howMany > len ? len : howMany
if (typeof listOrString === 'string')
return listOrString.slice(len - numValue)
numValue = len - numValue
return baseSlice(
listOrString, numValue, len
)
}
import { takeLast } from './takeLast.js'
test('with arrays', () => {
expect(takeLast(1, [ 'foo', 'bar', 'baz' ])).toEqual([ 'baz' ])
expect(takeLast(2)([ 'foo', 'bar', 'baz' ])).toEqual([ 'bar', 'baz' ])
expect(takeLast(3, [ 'foo', 'bar', 'baz' ])).toEqual([ 'foo', 'bar', 'baz' ])
expect(takeLast(4, [ 'foo', 'bar', 'baz' ])).toEqual([ 'foo', 'bar', 'baz' ])
expect(takeLast(10, [ 'foo', 'bar', 'baz' ])).toEqual([ 'foo', 'bar', 'baz' ])
})
test('with strings', () => {
expect(takeLast(3, 'rambda')).toBe('bda')
expect(takeLast(7, 'rambda')).toBe('rambda')
})
test('with negative index', () => {
expect(takeLast(-1, [ 1, 2, 3 ])).toEqual([ 1, 2, 3 ])
expect(takeLast(-Infinity, [ 1, 2, 3 ])).toEqual([ 1, 2, 3 ])
})
takeLastWhile(predicate: (x: string) => boolean, input: string): string
const result = R.takeLastWhile(
x => x > 2,
[1, 2, 3, 4]
)
// => [3, 4]
Try this R.takeLastWhile example in Rambda REPL
import { isArray } from './_internals/isArray.js'
export function takeLastWhile(predicate, input){
if (arguments.length === 1){
return _input => takeLastWhile(predicate, _input)
}
if (input.length === 0) return input
const toReturn = []
let counter = input.length
while (counter){
const item = input[ --counter ]
if (!predicate(item)){
break
}
toReturn.push(item)
}
return isArray(input) ? toReturn.reverse() : toReturn.reverse().join('')
}
import { takeLastWhile } from './takeLastWhile.js'
const assert = require('assert')
const list = [ 1, 2, 3, 4 ]
test('happy', () => {
const predicate = x => x > 2
const result = takeLastWhile(predicate, list)
expect(result).toEqual([ 3, 4 ])
})
test('predicate is always true', () => {
const predicate = () => true
const result = takeLastWhile(predicate)(list)
expect(result).toEqual(list)
})
test('predicate is always false', () => {
const predicate = () => false
const result = takeLastWhile(predicate, list)
expect(result).toEqual([])
})
test('with string', () => {
const result = takeLastWhile(x => x !== 'F', 'FOOBAR')
expect(result).toBe('OOBAR')
})
takeUntil<T>(predicate: (x: T) => boolean, list: T[]): T[]
const list = [1, 2, 3, 4, 5]
const predicate = x => x > 3
const result = R.takeUntil(predicate, list)
// => [1, 2, 3]
Try this R.takeUntil example in Rambda REPL
export function takeUntil(predicate, list){
const toReturn = []
let stopFlag = false
let counter = -1
while (stopFlag === false && counter++ < list.length - 1){
if (predicate(list[ counter ])){
stopFlag = true
} else {
toReturn.push(list[ counter ])
}
}
return toReturn
}
import { takeUntil } from './takeUntil.js'
const list = [ 1, 2, 3, 4, 5, 6 ]
test('happy', () => {
const result = takeUntil(x => x > 3, list)
expect(result).toEqual([ 1, 2, 3 ])
})
test('predicate always returns true', () => {
const result = takeUntil(x => x < 10, list)
expect(result).toEqual([])
})
test('predicate always returns false', () => {
const result = takeUntil(x => x > 10, list)
expect(result).toEqual(list)
})
Try this R.takeWhile example in Rambda REPL
tap<T>(fn: (x: T) => void, input: T): T
It applies function fn
to input x
and returns x
.
One use case is debugging in the middle of R.compose
.
const list = [1, 2, 3]
R.compose(
R.map(x => x * 2)
R.tap(console.log),
R.filter(x => x > 1)
)(list)
// => `2` and `3` will be logged
Try this R.tap example in Rambda REPL
export function tap(fn, x){
if (arguments.length === 1) return _x => tap(fn, _x)
fn(x)
return x
}
import { tap } from './tap.js'
test('tap', () => {
let a = 1
const sayX = x => a = x
expect(tap(sayX, 100)).toBe(100)
expect(tap(sayX)(100)).toBe(100)
expect(a).toBe(100)
})
tapAsync<T>(fn: Func<any> | Promise<any>, input: T): T
Asynchronous version of R.tap
.
async function tapAsyncFn(fn, input){
await fn(input)
return input
}
export function tapAsync(fn, input){
if (arguments.length === 1){
return async _input => tapAsyncFn(fn, _input)
}
return new Promise((resolve, reject) => {
tapAsyncFn(fn, input).then(resolve)
.catch(reject)
})
}
import { delay } from './delay.js'
import { pipedAsync } from './pipedAsync.js'
import { tapAsync } from './tapAsync.js'
test('happy', async () => {
const result = await tapAsync(delay, 1)
expect(result).toBe(1)
})
test('complex', async () => {
let marker = false
const fn = () => marker = true
const result = await pipedAsync(
1,
async x => {
await delay(100)
return x + 1
},
tapAsync(fn),
x => x + 1
)
expect(marker).toBeTrue()
expect(result).toBe(3)
})
test(regExpression: RegExp): (str: string) => boolean
It determines whether str
matches regExpression
.
R.test(/^f/, 'foo')
// => true
Try this R.test example in Rambda REPL
export function test(pattern, str){
if (arguments.length === 1) return _str => test(pattern, _str)
if (typeof pattern === 'string'){
throw new TypeError(`R.test requires a value of type RegExp as its first argument; received "${ pattern }"`)
}
return str.search(pattern) !== -1
}
import { test as testMethod } from './test.js'
test('happy', () => {
expect(testMethod(/^x/, 'xyz')).toBeTrue()
expect(testMethod(/^y/)('xyz')).toBeFalse()
})
test('throws if first argument is not regex', () => {
expect(() => testMethod('foo', 'bar')).toThrowErrorMatchingInlineSnapshot('"R.test requires a value of type RegExp as its first argument; received "foo""')
})
throttle<T>(fn: () => T, ms: number): () => T
let counter = 0
const inc = () => {
counter++
}
const throttledInc = R.throttle(inc, 800)
const result = async () => {
throttledInc()
await R.delay(500)
throttledInc()
return counter
}
// `result` resolves to `1`
Try this R.throttle example in Rambda REPL
export function throttle(fn, ms){
let wait = false
let result
return function (...input){
if (!wait){
result = fn.apply(null, input)
wait = true
setTimeout(() => {
wait = false
}, ms)
}
return result
}
}
import { delay } from './delay.js'
import { inc } from './inc.js'
import { throttle } from './throttle.js'
test('with side effect', async () => {
let counter = 0
const incFn = a => {
counter += a
return counter
}
const incWrapped = throttle(incFn, 1000)
incWrapped(1)
incWrapped(1)
await delay(1500)
incWrapped(1)
expect(counter).toBe(2)
})
test('return result', async () => {
const incWrapped = throttle(inc, 1000)
const results = []
results.push(incWrapped(1))
results.push(incWrapped(1))
await delay(1500)
results.push(incWrapped(1))
await delay(500)
results.push(incWrapped(1))
expect(results).toEqual([ 2, 2, 2, 2 ])
})
times<T>(fn: (i: number) => T, howMany: number): T[]
It returns the result of applying function fn
over members of range array.
The range array includes numbers between 0
and howMany
(exclusive).
const fn = x => x * 2
const howMany = 5
R.times(fn, howMany)
// => [0, 2, 4, 6, 8]
Try this R.times example in Rambda REPL
import { isInteger } from './_internals/isInteger.js'
import { map } from './map.js'
import { range } from './range.js'
export function times(fn, howMany){
if (arguments.length === 1) return _howMany => times(fn, _howMany)
if (!isInteger(howMany) || howMany < 0){
throw new RangeError('n must be an integer')
}
return map(fn, range(0, howMany))
}
import assert from 'assert'
import { identity } from './identity.js'
import { times } from './times.js'
test('happy', () => {
const result = times(identity, 5)
expect(result).toEqual([ 0, 1, 2, 3, 4 ])
})
test('with bad input', () => {
assert.throws(() => {
times(3)('cheers!')
}, RangeError)
assert.throws(() => {
times(identity, -1)
}, RangeError)
})
test('curry', () => {
const result = times(identity)(5)
expect(result).toEqual([ 0, 1, 2, 3, 4 ])
})
toDecimal(num: number, charsAfterDecimalPoint?: number): number
R.toDecimal(2.45464,2) // => 2.45
Try this R.toDecimal example in Rambda REPL
export function toDecimal(number, charsAfterDecimalPoint = 2){
return Number(parseFloat(String(number)).toFixed(charsAfterDecimalPoint))
}
import { toDecimal } from './toDecimal.js'
test('happy', () => {
expect(toDecimal(2.2789, 1)).toBe(2.3)
expect(toDecimal(2.2789, 3)).toBe(2.279)
expect(toDecimal(2, 3)).toBe(2)
expect(toDecimal(2.2789)).toBe(2.28)
expect(toDecimal(2.45464)).toBe(2.45)
})
toLower<S extends string>(str: S): Lowercase<S>
R.toLower('FOO')
// => 'foo'
Try this R.toLower example in Rambda REPL
export function toLower(str){
return str.toLowerCase()
}
import { toLower } from './toLower.js'
test('toLower', () => {
expect(toLower('FOO|BAR|BAZ')).toBe('foo|bar|baz')
})
toPairs<O extends object, K extends Extract<keyof O, string | number>>(obj: O): Array<{ [key in K]: [`${key}`, O[key]] }[K]>
It transforms an object to a list.
const list = {
a : 1,
b : 2,
c : [ 3, 4 ],
}
const expected = [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', [ 3, 4 ] ] ]
const result = R.toPairs(list)
// => `result` is equal to `expected`
Try this R.toPairs example in Rambda REPL
export function toPairs(obj){
return Object.entries(obj)
}
import { toPairs } from './toPairs.js'
const obj = {
a : 1,
b : 2,
c : [ 3, 4 ],
}
const expected = [
[ 'a', 1 ],
[ 'b', 2 ],
[ 'c', [ 3, 4 ] ],
]
test('happy', () => {
expect(toPairs(obj)).toEqual(expected)
})
toString(x: unknown): string
R.toString([1, 2])
// => '1,2'
Try this R.toString example in Rambda REPL
export function toString(x){
return x.toString()
}
import { toString } from './toString.js'
test('happy', () => {
expect(toString([ 1, 2, 3 ])).toBe('1,2,3')
})
toUpper<S extends string>(str: S): Uppercase<S>
R.toUpper('foo')
// => 'FOO'
Try this R.toUpper example in Rambda REPL
export function toUpper(str){
return str.toUpperCase()
}
import { toUpper } from './toUpper.js'
test('toUpper', () => {
expect(toUpper('foo|bar|baz')).toBe('FOO|BAR|BAZ')
})
transpose<T>(list: (T[])[]): (T[])[]
const list = [[10, 11], [20], [], [30, 31, 32]]
const expected = [[10, 20, 30], [11, 31], [32]]
const result = R.transpose(list)
// => `result` is equal to `expected`
Try this R.transpose example in Rambda REPL
import { isArray } from './_internals/isArray.js'
export function transpose(array){
return array.reduce((acc, el) => {
el.forEach((nestedEl, i) =>
isArray(acc[ i ]) ? acc[ i ].push(nestedEl) : acc.push([ nestedEl ]))
return acc
}, [])
}
import { transpose } from './transpose.js'
test('happy', () => {
const input = [
[ 'a', 1 ],
[ 'b', 2 ],
[ 'c', 3 ],
]
expect(transpose(input)).toEqual([
[ 'a', 'b', 'c' ],
[ 1, 2, 3 ],
])
})
test('when rows are shorter', () => {
const actual = transpose([ [ 10, 11 ], [ 20 ], [], [ 30, 31, 32 ] ])
const expected = [ [ 10, 20, 30 ], [ 11, 31 ], [ 32 ] ]
expect(actual).toEqual(expected)
})
test('with empty array', () => {
expect(transpose([])).toEqual([])
})
test('array with falsy values', () => {
const actual = transpose([
[ true, false, undefined, null ],
[ null, undefined, false, true ],
])
const expected = [
[ true, null ],
[ false, undefined ],
[ undefined, false ],
[ null, true ],
]
expect(actual).toEqual(expected)
})
trim(str: string): string
R.trim(' foo ')
// => 'foo'
Try this R.trim example in Rambda REPL
export function trim(str){
return str.trim()
}
import { trim } from './trim.js'
test('trim', () => {
expect(trim(' foo ')).toBe('foo')
})
It returns function that runs fn
in try/catch
block. If there was an error, then fallback
is used to return the result. Note that fn
can be value or asynchronous/synchronous function(unlike Ramda
where fallback can only be a synchronous function).
:boom: Please check the tests of
R.tryCatch
to fully understand how this method works.
Try this R.tryCatch example in Rambda REPL
It returns function that runs fn
in try/catch
block. If there was an error, then fallback
is used to return the result.
Try this R.tryCatchAsync example in Rambda REPL
It accepts any input and it returns its type.
:boom:
NaN
,Promise
andAsync
are types specific for Rambda.
Try this R.type example in Rambda REPL
unapply<T = any>(fn: (args: any[]) => T): (...args: any[]) => T
It calls a function fn
with the list of values of the returned function.
R.unapply
is the opposite of R.apply
method.
R.unapply(JSON.stringify)(1, 2, 3)
//=> '[1,2,3]'
Try this R.unapply example in Rambda REPL
export function unapply(fn){
return function (...args){
return fn.call(this, args)
}
}
import { apply } from './apply.js'
import { converge } from './converge.js'
import { identity } from './identity.js'
import { prop } from './prop.js'
import { sum } from './sum.js'
import { unapply } from './unapply.js'
test('happy', () => {
const fn = unapply(identity)
expect(fn(
1, 2, 3
)).toEqual([ 1, 2, 3 ])
expect(fn()).toEqual([])
})
test('returns a function which is always passed one argument', () => {
const fn = unapply(function (){
return arguments.length
})
expect(fn('x')).toBe(1)
expect(fn('x', 'y')).toBe(1)
expect(fn(
'x', 'y', 'z'
)).toBe(1)
})
test('forwards arguments to decorated function as an array', () => {
const fn = unapply(xs => '[' + xs + ']')
expect(fn(2)).toBe('[2]')
expect(fn(2, 4)).toBe('[2,4]')
expect(fn(
2, 4, 6
)).toBe('[2,4,6]')
})
test('returns a function with length 0', () => {
const fn = unapply(identity)
expect(fn).toHaveLength(0)
})
test('is the inverse of R.apply', () => {
let a, b, c, d, e, f, g, n
const rand = function (){
return Math.floor(200 * Math.random()) - 100
}
f = Math.max
g = unapply(apply(f))
n = 1
while (n <= 100){
a = rand()
b = rand()
c = rand()
d = rand()
e = rand()
expect(f(
a, b, c, d, e
)).toEqual(g(
a, b, c, d, e
))
n += 1
}
f = function (xs){
return '[' + xs + ']'
}
g = apply(unapply(f))
n = 1
while (n <= 100){
a = rand()
b = rand()
c = rand()
d = rand()
e = rand()
expect(f([ a, b, c, d, e ])).toEqual(g([ a, b, c, d, e ]))
n += 1
}
})
test('it works with converge', () => {
const fn = unapply(sum)
const convergeFn = converge(fn, [ prop('a'), prop('b'), prop('c') ])
const obj = {
a : 1337,
b : 42,
c : 1,
}
const expected = 1337 + 42 + 1
expect(convergeFn(obj)).toEqual(expected)
})
union<T>(x: T[], y: T[]): T[]
It takes two lists and return a new list containing a merger of both list with removed duplicates.
R.equals
is used to compare for duplication.
const result = R.union([1,2,3], [3,4,5]);
// => [1, 2, 3, 4, 5]
Try this R.union example in Rambda REPL
import { cloneList } from './_internals/cloneList.js'
import { includes } from './includes.js'
export function union(x, y){
if (arguments.length === 1) return _y => union(x, _y)
const toReturn = cloneList(x)
y.forEach(yInstance => {
if (!includes(yInstance, x)) toReturn.push(yInstance)
})
return toReturn
}
import { union } from './union.js'
test('happy', () => {
expect(union([ 1, 2 ], [ 2, 3 ])).toEqual([ 1, 2, 3 ])
})
test('with list of objects', () => {
const list1 = [ { a : 1 }, { a : 2 } ]
const list2 = [ { a : 2 }, { a : 3 } ]
const result = union(list1)(list2)
})
uniq<T>(list: T[]): T[]
It returns a new array containing only one copy of each element of list
.
R.equals
is used to determine equality.
const list = [1, 1, {a: 1}, {a: 2}, {a:1}]
R.uniq(list)
// => [1, {a: 1}, {a: 2}]
Try this R.uniq example in Rambda REPL
import { _Set } from './_internals/set.js'
export function uniq(list){
const set = new _Set()
const willReturn = []
list.forEach(item => {
if (set.checkUniqueness(item)){
willReturn.push(item)
}
})
return willReturn
}
import { uniq } from './uniq.js'
test('happy', () => {
const list = [ 1, 2, 3, 3, 3, 1, 2, 0 ]
expect(uniq(list)).toEqual([ 1, 2, 3, 0 ])
})
test('with object', () => {
const list = [ { a : 1 }, { a : 2 }, { a : 1 }, { a : 2 } ]
expect(uniq(list)).toEqual([ { a : 1 }, { a : 2 } ])
})
test('with nested array', () => {
expect(uniq([ [ 42 ], [ 42 ] ])).toEqual([ [ 42 ] ])
})
test('with booleans', () => {
expect(uniq([ [ false ], [ false ], [ true ] ])).toEqual([ [ false ], [ true ] ])
})
test('with falsy values', () => {
expect(uniq([ undefined, null ])).toEqual([ undefined, null ])
})
test('can distinct between string and number', () => {
expect(uniq([ 1, '1' ])).toEqual([ 1, '1' ])
})
It applies uniqueness to input list based on function that defines what to be used for comparison between elements.
R.equals
is used to determine equality.
Try this R.uniqBy example in Rambda REPL
uniqWith<T, U>(predicate: (x: T, y: T) => boolean, list: T[]): T[]
It returns a new array containing only one copy of each element in list
according to predicate
function.
This predicate should return true, if two elements are equal.
const list = [
{id: 0, title:'foo'},
{id: 1, title:'bar'},
{id: 2, title:'baz'},
{id: 3, title:'foo'},
{id: 4, title:'bar'},
]
const expected = [
{id: 0, title:'foo'},
{id: 1, title:'bar'},
{id: 2, title:'baz'},
]
const predicate = (x,y) => x.title === y.title
const result = R.uniqWith(predicate, list)
// => `result` is equal to `expected`
Try this R.uniqWith example in Rambda REPL
function includesWith(
predicate, target, list
){
let willReturn = false
let index = -1
while (++index < list.length && !willReturn){
const value = list[ index ]
if (predicate(target, value)){
willReturn = true
}
}
return willReturn
}
export function uniqWith(predicate, list){
if (arguments.length === 1) return _list => uniqWith(predicate, _list)
let index = -1
const willReturn = []
while (++index < list.length){
const value = list[ index ]
if (!includesWith(
predicate, value, willReturn
)){
willReturn.push(value)
}
}
return willReturn
}
import { uniqWith as uniqWithRamda } from 'ramda'
import { uniqWith } from './uniqWith.js'
const list = [ { a : 1 }, { a : 1 } ]
test('happy', () => {
const fn = (x, y) => x.a === y.a
const result = uniqWith(fn, list)
expect(result).toEqual([ { a : 1 } ])
})
test('with list of strings', () => {
const fn = (x, y) => x.length === y.length
const list = [ '0', '11', '222', '33', '4', '55' ]
const result = uniqWith(fn)(list)
const resultRamda = uniqWithRamda(fn, list)
expect(result).toEqual([ '0', '11', '222' ])
expect(resultRamda).toEqual([ '0', '11', '222' ])
})
unless<T, U>(predicate: (x: T) => boolean, whenFalseFn: (x: T) => U, x: T): T | U
The method returns function that will be called with argument input
.
If predicate(input)
returns false
, then the end result will be the outcome of whenFalse(input)
.
In the other case, the final output will be the input
itself.
const fn = R.unless(
x => x > 2,
x => x + 10
)
const result = [
fn(1),
fn(5)
]
// => [11, 5]
Try this R.unless example in Rambda REPL
import { curry } from './curry.js'
function unlessFn(
predicate, whenFalseFn, input
){
if (predicate(input)) return input
return whenFalseFn(input)
}
export const unless = curry(unlessFn)
import { inc } from './inc.js'
import { isNil } from './isNil.js'
import { unless } from './unless.js'
test('happy', () => {
const safeInc = unless(isNil, inc)
expect(safeInc(null)).toBeNull()
expect(safeInc(1)).toBe(2)
})
test('curried', () => {
const safeIncCurried = unless(isNil)(inc)
expect(safeIncCurried(null)).toBeNull()
})
test('with 3 inputs', () => {
let result = unless(x => x.startsWith('/'), x=> x.concat('/'), '/api')
expect(result).toBe('/api')
})
Try this R.unnest example in Rambda REPL
Try this R.unwind example in Rambda REPL
update<T>(index: number, newValue: T, list: T[]): T[]
It returns a copy of list
with updated element at index
with newValue
.
const index = 2
const newValue = 88
const list = [1, 2, 3, 4, 5]
const result = R.update(index, newValue, list)
// => [1, 2, 88, 4, 5]
Try this R.update example in Rambda REPL
import { cloneList } from './_internals/cloneList.js'
import { curry } from './curry.js'
export function updateFn(
index, newValue, list
){
const clone = cloneList(list)
if (index === -1) return clone.fill(newValue, index)
return clone.fill(
newValue, index, index + 1
)
}
export const update = curry(updateFn)
import { update } from './update.js'
const list = [ 1, 2, 3 ]
test('happy', () => {
const newValue = 8
const index = 1
const result = update(
index, newValue, list
)
const curriedResult = update(index, newValue)(list)
const tripleCurriedResult = update(index)(newValue)(list)
const expected = [ 1, 8, 3 ]
expect(result).toEqual(expected)
expect(curriedResult).toEqual(expected)
expect(tripleCurriedResult).toEqual(expected)
})
test('list has no such index', () => {
const newValue = 8
const index = 10
const result = update(
index, newValue, list
)
expect(result).toEqual(list)
})
test('with negative index', () => {
expect(update(
-1, 10, [ 1 ]
)).toEqual([ 10 ])
expect(update(
-1, 10, []
)).toEqual([])
expect(update(
-1, 10, list
)).toEqual([ 1, 2, 10 ])
expect(update(
-2, 10, list
)).toEqual([ 1, 10, 3 ])
expect(update(
-3, 10, list
)).toEqual([ 10, 2, 3 ])
})
updateObject<Output>(rules: ([string, any])[], input: object): Output
Very similar to R.assocPath
but it applies list of updates instead of only a single update.
It returns a copy of obj
input with changed properties according to rules
input.
Each instance of rules
is a tuple of object path and the new value for this path. If such object path does not exist, then such object path is created.
As it uses R.path
underneath, object path can be either string or array of strings(in Typescript object path can be only a string).
const obj = {
a: {b: 1},
foo: {bar: 10},
}
const rules = [
['a.b', 2],
['foo.bar', 20],
['q.z', 300],
]
const result = R.updateObject(rules, Record<string, unknown>)
const expected = {
a: {b: 2},
foo: {bar: 20},
q: {z: 300},
}
// => `result` is equal to `expected`
Try this R.updateObject example in Rambda REPL
import { assocPath } from './assocPath.js'
export function updateObject(rules, obj){
if (arguments.length === 1) return _obj => updateObject(rules, _obj)
let clone = { ...obj } /*?.*/
rules.forEach(([ objectPath, newValue ]) => {
clone = assocPath(
objectPath, newValue, clone
)
})
return clone
}
import { updateObject } from './updateObject.js'
const obj = {
a : { b : 1 },
foo : { bar : 10 },
}
const rules = [
[ 'a.b', 2 ],
[ 'foo.bar', 20 ],
[ 'q.z', 300 ],
]
const expected = {
a : { b : 2 },
foo : { bar : 20 },
q : { z : 300 },
}
test('happy', () => {
const result = updateObject(rules, obj)
expect(result).toEqual(expected)
})
test('curried', () => {
const result = updateObject(rules)(obj)
expect(result).toEqual(expected)
})
values<T extends object, K extends keyof T>(obj: T): T[K][]
With correct input, this is nothing more than Object.values(Record<string, unknown>)
. If obj
is not an object, then it returns an empty array.
const obj = {a:1, b:2}
R.values(Record<string, unknown>)
// => [1, 2]
Try this R.values example in Rambda REPL
import { type } from './type.js'
export function values(obj){
if (type(obj) !== 'Object') return []
return Object.values(obj)
}
import { values } from './values.js'
test('happy', () => {
expect(values({
a : 1,
b : 2,
c : 3,
})).toEqual([ 1, 2, 3 ])
})
test('with bad input', () => {
expect(values(null)).toEqual([])
expect(values(undefined)).toEqual([])
expect(values(55)).toEqual([])
expect(values('foo')).toEqual([])
expect(values(true)).toEqual([])
expect(values(false)).toEqual([])
expect(values(NaN)).toEqual([])
expect(values(Infinity)).toEqual([])
expect(values([])).toEqual([])
})
view<S, A>(lens: Lens<S, A>): (obj: S) => A
It returns the value of lens
focus over target
object.
const lens = R.lensProp('x')
R.view(lens, {x: 1, y: 2}) // => 1
R.view(lens, {x: 4, y: 2}) // => 4
Try this R.view example in Rambda REPL
const Const = x => ({
x,
map : fn => Const(x),
})
export function view(lens, target){
if (arguments.length === 1) return _target => view(lens, _target)
return lens(Const)(target).x
}
import { assoc } from './assoc.js'
import { lens } from './lens.js'
import { prop } from './prop.js'
import { view } from './view.js'
const testObject = { foo : 'Led Zeppelin' }
const assocLens = lens(prop('foo'), assoc('foo'))
test('happy', () => {
expect(view(assocLens, testObject)).toBe('Led Zeppelin')
})
viewOr<Input, Output>(fallback: Output, lens: Lens<Input, Output>, input: Input): Output
A combination between R.defaultTo
and `R.view.
const lens = R.lensProp('a');
const input = {a: 'foo'}
const fallbackInput = {b: 'bar'}
const fallback = 'FALLBACK'
const result = [
R.viewOr(fallback, lens, input),
R.viewOr(fallback, lens, fallbackInput)
]
// => ['foo', 'FALLBACK']
Try this R.viewOr example in Rambda REPL
import { curry } from './curry.js'
import { defaultTo } from './defaultTo.js'
import { view } from './view.js'
function viewOrFn(
fallback, lens, input
){
return defaultTo(fallback, view(lens, input))
}
export const viewOr = curry(viewOrFn)
import { lensProp } from './lensProp.js'
import { viewOr } from './viewOr.js'
const lens = lensProp('a')
const input = { a : 'foo' }
const fallbackInput = { b : 'bar' }
const fallback = 'FALLBACK'
test('happy', () => {
const result = viewOr(
fallback, lens, fallbackInput
)
expect(result).toBe(fallback)
})
test('curried', () => {
const result = viewOr(fallback, lens)(input)
expect(result).toBe('foo')
})
wait<T>(fn: Promise<T>): Promise<[T, Error|undefined]>
It provides Golang
-like interface for handling promises.
const [result, err] = await R.wait(R.delay(1000))
// => err is undefined
// => result is `RAMBDAX_DELAY`
Try this R.wait example in Rambda REPL
export function wait(fn){
return new Promise(resolve => {
fn.then(result => resolve([ result, undefined ])).catch(e =>
resolve([ undefined, e ]))
})
}
import { wait } from './wait.js'
test('happy path', async () => {
const fn = x => Promise.resolve(x + 1)
const [ result, err ] = await wait(fn(1))
expect(result).toBe(2)
expect(err).toBeUndefined()
})
test('when promise is rejected', async () => {
const fn = x => Promise.reject(Error('foo'))
const [ result, err ] = await wait(fn(1))
expect(result).toBeUndefined()
expect(err).toEqual(Error('foo'))
})
waitFor(
waitForTrueCondition: () => boolean,
howLong: number,
loops?: number
): () => Promise<boolean>
It returns true
, if condition
returns true
within howLong
milliseconds time period.
The method accepts an optional third argument loops
(default to 10), which is the number of times waitForTrueCondition
will be evaluated for howLong
period. Once this function returns a value different from false
, this value will be the final result.
Otherwise, R.waitFor
will return false
.
const howLong = 1000
let counter = 0
const waitForTrueCondition = async x => {
await R.delay(100)
counter = counter + x
return counter > 10
}
const result = await R.waitFor(waitForTrueCondition, howLong)(2)
// => true
Try this R.waitFor example in Rambda REPL
import { delay } from './delay.js'
import { range } from './range.js'
import { type } from './type.js'
export function waitFor(
condition, howLong, loops = 10
){
const typeCondition = type(condition)
const passPromise = typeCondition === 'Promise'
const passFunction = typeCondition === 'Function'
const interval = Math.floor(howLong / loops)
if (!(passPromise || passFunction)){
throw new Error('R.waitFor')
}
return async (...inputs) => {
for (const _ of range(0, loops)){
const resultCondition = await condition(...inputs)
if (resultCondition === false){
await delay(interval)
} else {
return resultCondition
}
}
return false
}
}
import { delay } from './delay.js'
import { waitFor } from './waitFor.js'
const howLong = 1000
test('true', async () => {
let counter = 0
const condition = x => {
counter++
return counter > x
}
const result = await waitFor(condition, howLong)(6)
expect(result).toBeTrue()
})
test('false', async () => {
let counter = 0
const condition = x => {
counter++
return counter > x
}
const result = await waitFor(condition, howLong)(12)
expect(result).toBeFalse()
})
test('async condition | true', async () => {
let counter = 0
const condition = async x => {
counter++
await delay(10)
return counter > x
}
const result = await waitFor(condition, howLong)(6)
expect(result).toBeTrue()
})
test('async condition | false', async () => {
let counter = 0
const condition = async x => {
counter++
await delay(10)
return counter > x
}
const result = await waitFor(condition, howLong)(12)
expect(result).toBeFalse()
})
test('throws when fn is not function', () => {
const fn = 'foo'
expect(() => waitFor(fn, howLong)()).toThrowErrorMatchingInlineSnapshot('"R.waitFor"')
})
when<T, U>(predicate: (x: T) => boolean, whenTrueFn: (a: T) => U, input: T): T | U
It pass input
to predicate
function and if the result is true
, it will return the result of whenTrueFn(input)
.
If the predicate
returns false
, then it will simply return input
.
const predicate = x => typeof x === 'number'
const whenTrueFn = R.add(11)
const fn = when(predicate, whenTrueResult)
const positiveInput = 88
const negativeInput = 'foo'
const result = [
fn(positiveInput),
fn(positiveInput),
]
const expected = [
99,
'foo',
]
// => `result` is equal to `expected`
Try this R.when example in Rambda REPL
import { curry } from './curry.js'
function whenFn(
predicate, whenTrueFn, input
){
if (!predicate(input)) return input
return whenTrueFn(input)
}
export const when = curry(whenFn)
import { add } from './add.js'
import { when } from './when.js'
const predicate = x => typeof x === 'number'
test('happy', () => {
const fn = when(predicate, add(11))
expect(fn(11)).toBe(22)
expect(fn('foo')).toBe('foo')
})
where<T, U>(conditions: T, input: U): boolean
It returns true
if all each property in conditions
returns true
when applied to corresponding property in input
object.
const condition = R.where({
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.where example in Rambda REPL
export function where(conditions, input){
if (input === undefined){
return _input => where(conditions, _input)
}
let flag = true
for (const prop in conditions){
if (!flag) continue
const result = conditions[ prop ](input[ prop ])
if (flag && result === false){
flag = false
}
}
return flag
}
import { equals } from './equals.js'
import { where } from './where.js'
test('when true', () => {
const result = where({
a : equals('foo'),
b : equals('bar'),
},
{
a : 'foo',
b : 'bar',
x : 11,
y : 19,
})
expect(result).toBeTrue()
})
test('when false | early exit', () => {
let counter = 0
const equalsFn = expected => input => {
console.log(expected, 'expected')
counter++
return input === expected
}
const predicate = where({
a : equalsFn('foo'),
b : equalsFn('baz'),
})
expect(predicate({
a : 'notfoo',
b : 'notbar',
})).toBeFalse()
expect(counter).toBe(1)
})
Same as R.where
, but it will return true
if at least one condition check returns true
.
Try this R.whereAny example in Rambda REPL
whereEq<T, U>(condition: T, input: U): boolean
It will return true
if all of input
object fully or partially include rule
object.
R.equals
is used to determine equality.
const condition = { a : { b : 1 } }
const input = {
a : { b : 1 },
c : 2
}
const result = whereEq(condition, input)
// => true
Try this R.whereEq example in Rambda REPL
import { equals } from './equals.js'
import { filter } from './filter.js'
export function whereEq(condition, input){
if (arguments.length === 1){
return _input => whereEq(condition, _input)
}
const result = filter((conditionValue, conditionProp) =>
equals(conditionValue, input[ conditionProp ]),
condition)
return Object.keys(result).length === Object.keys(condition).length
}
import { whereEq } from './whereEq.js'
test('when true', () => {
const condition = { a : 1 }
const input = {
a : 1,
b : 2,
}
const result = whereEq(condition, input)
const expectedResult = true
expect(result).toEqual(expectedResult)
})
test('when false', () => {
const condition = { a : 1 }
const input = { b : 2 }
const result = whereEq(condition, input)
const expectedResult = false
expect(result).toEqual(expectedResult)
})
test('with nested object', () => {
const condition = { a : { b : 1 } }
const input = {
a : { b : 1 },
c : 2,
}
const result = whereEq(condition)(input)
const expectedResult = true
expect(result).toEqual(expectedResult)
})
test('with wrong input', () => {
const condition = { a : { b : 1 } }
expect(() => whereEq(condition, null)).toThrowErrorMatchingInlineSnapshot('"Cannot read properties of null (reading \'a\')"')
})
without<T>(matchAgainst: T[], source: T[]): T[]
It will return a new array, based on all members of source
list that are not part of matchAgainst
list.
R.equals
is used to determine equality.
const source = [1, 2, 3, 4]
const matchAgainst = [2, 3]
const result = R.without(matchAgainst, source)
// => [1, 4]
Try this R.without example in Rambda REPL
import { _indexOf } from './equals.js'
import { reduce } from './reduce.js'
export function without(matchAgainst, source){
if (source === undefined){
return _source => without(matchAgainst, _source)
}
return reduce(
(prev, current) =>
_indexOf(current, matchAgainst) > -1 ? prev : prev.concat(current),
[],
source
)
}
import { without as withoutRamda } from 'ramda'
import { without } from './without.js'
test('should return a new list without values in the first argument', () => {
const itemsToOmit = [ 'A', 'B', 'C' ]
const collection = [ 'A', 'B', 'C', 'D', 'E', 'F' ]
expect(without(itemsToOmit, collection)).toEqual([ 'D', 'E', 'F' ])
expect(without(itemsToOmit)(collection)).toEqual([ 'D', 'E', 'F' ])
})
test('with list of objects', () => {
const itemsToOmit = [ { a : 1 }, { c : 3 } ]
const collection = [ { a : 1 }, { b : 2 }, { c : 3 }, { d : 4 } ]
const expected = [ { b : 2 }, { d : 4 } ]
expect(without(itemsToOmit, collection)).toEqual(expected)
expect(withoutRamda(itemsToOmit, collection)).toEqual(expected)
})
test('ramda accepts string as target input while rambda throws', () => {
expect(withoutRamda('0:1', [ '0', '0:1' ])).toEqual([ '0:1' ])
expect(() =>
without('0:1', [ '0', '0:1' ])).toThrowErrorMatchingInlineSnapshot('"Cannot read property \'indexOf\' of 0:1"')
expect(without([ '0:1' ], [ '0', '0:1' ])).toEqual([ '0' ])
})
test('ramda test', () => {
expect(without([ 1, 2 ])([ 1, 2, 1, 3, 4 ])).toEqual([ 3, 4 ])
})
xnor(x: boolean, y: boolean): boolean
Logical XNOR
const result = [
R.xnor(1, 0),
R.xnor(0, 1),
R.xnor(0, 0),
R.xnor(1, 1),
]
// => [true, false, false, true]
Try this R.xnor example in Rambda REPL
export function xnor(x, y){
if (arguments.length === 1){
return _y => xnor(x, _y)
}
return Boolean(x && y || !x && !y)
}
import { xnor } from './xnor.js'
test('when true', () => {
expect(xnor(1, 1)).toBeTrue()
expect(xnor(0)(0)).toBeTrue()
})
test('when false', () => {
expect(xnor(0, 1)).toBeFalse()
})
xor(x: boolean, y: boolean): boolean
Logical XOR
const result = [
xor(true, true),
xor(false, false),
xor(false, true),
]
// => [false, false, true]
Try this R.xor example in Rambda REPL
export function xor(a, b){
if (arguments.length === 1) return _b => xor(a, _b)
return Boolean(a) && !b || Boolean(b) && !a
}
import { xor } from './xor.js'
test('compares two values with exclusive or', () => {
expect(xor(true, true)).toBeFalse()
expect(xor(true, false)).toBeTrue()
expect(xor(false, true)).toBeTrue()
expect(xor(false, false)).toBeFalse()
})
test('when both values are truthy, it should return false', () => {
expect(xor(true, 'foo')).toBeFalse()
expect(xor(42, true)).toBeFalse()
expect(xor('foo', 42)).toBeFalse()
expect(xor({}, true)).toBeFalse()
expect(xor(true, [])).toBeFalse()
expect(xor([], {})).toBeFalse()
expect(xor(new Date(), true)).toBeFalse()
expect(xor(true, Infinity)).toBeFalse()
expect(xor(Infinity, new Date())).toBeFalse()
})
test('when both values are falsy, it should return false', () => {
expect(xor(null, false)).toBeFalse()
expect(xor(false, undefined)).toBeFalse()
expect(xor(undefined, null)).toBeFalse()
expect(xor(0, false)).toBeFalse()
expect(xor(false, NaN)).toBeFalse()
expect(xor(NaN, 0)).toBeFalse()
expect(xor('', false)).toBeFalse()
})
test('when one argument is truthy and the other is falsy, it should return true', () => {
expect(xor('foo', null)).toBeTrue()
expect(xor(null, 'foo')).toBeTrue()
expect(xor(undefined, 42)).toBeTrue()
expect(xor(42, undefined)).toBeTrue()
expect(xor(Infinity, NaN)).toBeTrue()
expect(xor(NaN, Infinity)).toBeTrue()
expect(xor({}, '')).toBeTrue()
expect(xor('', {})).toBeTrue()
expect(xor(new Date(), 0)).toBeTrue()
expect(xor(0, new Date())).toBeTrue()
expect(xor([], null)).toBeTrue()
expect(xor(undefined, [])).toBeTrue()
})
zip<K, V>(x: K[], y: V[]): KeyValuePair<K, V>[]
It will return a new array containing tuples of equally positions items from both x
and y
lists.
The returned list will be truncated to match the length of the shortest supplied list.
const x = [1, 2]
const y = ['A', 'B']
R.zip(x, y)
// => [[1, 'A'], [2, 'B']]
// truncates to shortest list
R.zip([...x, 3], ['A', 'B'])
// => [[1, 'A'], [2, 'B']]
Try this R.zip example in Rambda REPL
export function zip(left, right){
if (arguments.length === 1) return _right => zip(left, _right)
const result = []
const length = Math.min(left.length, right.length)
for (let i = 0; i < length; i++){
result[ i ] = [ left[ i ], right[ i ] ]
}
return result
}
import { zip } from './zip.js'
const array1 = [ 1, 2, 3 ]
const array2 = [ 'A', 'B', 'C' ]
test('should return an array', () => {
const actual = zip(array1)(array2)
expect(actual).toBeInstanceOf(Array)
})
test('should return and array or tuples', () => {
const expected = [
[ 1, 'A' ],
[ 2, 'B' ],
[ 3, 'C' ],
]
const actual = zip(array1, array2)
expect(actual).toEqual(expected)
})
test('should truncate result to length of shorted input list', () => {
const expectedA = [
[ 1, 'A' ],
[ 2, 'B' ],
]
const actualA = zip([ 1, 2 ], array2)
expect(actualA).toEqual(expectedA)
const expectedB = [
[ 1, 'A' ],
[ 2, 'B' ],
]
const actualB = zip(array1, [ 'A', 'B' ])
expect(actualB).toEqual(expectedB)
})
zipObj<T, K extends string>(keys: K[], values: T[]): { [P in K]: T }
It will return a new object with keys of keys
array and values of values
array.
const keys = ['a', 'b', 'c']
R.zipObj(keys, [1, 2, 3])
// => {a: 1, b: 2, c: 3}
// truncates to shortest list
R.zipObj(keys, [1, 2])
// => {a: 1, b: 2}
Try this R.zipObj example in Rambda REPL
import { take } from './take.js'
export function zipObj(keys, values){
if (arguments.length === 1) return yHolder => zipObj(keys, yHolder)
return take(values.length, keys).reduce((
prev, xInstance, i
) => {
prev[ xInstance ] = values[ i ]
return prev
}, {})
}
import { equals } from './equals.js'
import { zipObj } from './zipObj.js'
test('zipObj', () => {
expect(zipObj([ 'a', 'b', 'c' ], [ 1, 2, 3 ])).toEqual({
a : 1,
b : 2,
c : 3,
})
})
test('0', () => {
expect(zipObj([ 'a', 'b' ])([ 1, 2, 3 ])).toEqual({
a : 1,
b : 2,
})
})
test('1', () => {
expect(zipObj([ 'a', 'b', 'c' ])([ 1, 2 ])).toEqual({
a : 1,
b : 2,
})
})
test('ignore extra keys', () => {
const result = zipObj([ 'a', 'b', 'c', 'd', 'e', 'f' ], [ 1, 2, 3 ])
const expected = {
a : 1,
b : 2,
c : 3,
}
expect(equals(result, expected)).toBeTrue()
})
zipWith<T, U, TResult>(fn: (x: T, y: U) => TResult, list1: T[], list2: U[]): TResult[]
const list1 = [ 10, 20, 30, 40 ]
const list2 = [ 100, 200 ]
const result = R.zipWith(
R.add, list1, list2
)
// => [110, 220]
Try this R.zipWith example in Rambda REPL
import { curry } from './curry.js'
import { take } from './take.js'
function zipWithFn(
fn, x, y
){
return take(x.length > y.length ? y.length : x.length, x).map((xInstance, i) => fn(xInstance, y[ i ]))
}
export const zipWith = curry(zipWithFn)
import { add } from './add.js'
import { zipWith } from './zipWith.js'
const list1 = [ 1, 2, 3 ]
const list2 = [ 10, 20, 30, 40 ]
const list3 = [ 100, 200 ]
test('when second list is shorter', () => {
const result = zipWith(
add, list1, list3
)
expect(result).toEqual([ 101, 202 ])
})
test('when second list is longer', () => {
const result = zipWith(
add, list1, list2
)
expect(result).toEqual([ 11, 22, 33 ])
})
11.2.0
R.throttle
TS typings now support no argument case for function input.
Sync with Rambda
version 9.3.0
11.1.1
Fix broken build due to changes to TypeScript definitions for lenses.
11.1.0
Improve R.mapToObject
types - Issue #96
Sync with Rambda
version 9.2.0
11.0.0
Sync with Rambda
version 9.1.0
Change typings of R.lensEq
to match Rambda-adjust
typings
10.1.0
Simplify TypeScript logic of R.pipeAsync/R.composeAsync/R.pipedAsync
- MR #698
Sync with Rambda
version 8.6.0
10.0.0
Sync with Rambda
version 8.0.0
Add R.omitPaths
- Issue #681
Add R.noop
9.1.1
Add missing fix for type: module
imports.
9.1.0
Rambda
version 7.5.0
9.0.0
From this release, CHANGELOG will simply refer to the Rambda
version linked to the release, instead of listing Rambda
changes here as well. In this case, the version referring to this release is `
This is only part of the changelog. You can read the full text in CHANGELOG.md file.
Most influential contributors(in alphabetical order)
@farwayer - improving performance in R.find, R.filter; give the idea how to make benchmarks more reliable;
@thejohnfreeman - add R.assoc, R.chain;
@peeja - add several methods and fix mutiple issues; provides great MR documentation
@helmuthdu - add R.clone; help improve code style;
@jpgorman - add R.zip, R.reject, R.without, R.addIndex;
@ku8ar - add R.slice, R.propOr, R.identical, R.propIs and several math related methods; introduce the idea to display missing Ramda methods;
@romgrk - add R.groupBy, R.indexBy, R.findLast, R.findLastIndex;
@squidfunk - add R.assocPath, R.symmetricDifference, R.difference, R.intersperse;
@synthet1c - add all lenses methods; add R.applySpec, R.converge;
@vlad-zhukov - help with configuring Rollup, Babel; change export file to use ES module exports;
Rambda references
Links to Rambda
Deprecated from
Used by
section
Rambda
since October/2020 commit that removes Rambda
Niketa themeCollection of 9 light VSCode themes |
Niketa dark themeCollection of 9 dark VSCode themes |
String-fnString utility library |
Useful Javascript librariesLarge collection of JavaScript,TypeScript and Angular related repos links |
Run-fnCLI commands for lint JS/TS files, commit git changes and upgrade of dependencies |
FAQs
Extended version of Rambda - a lightweight, faster alternative to Ramda
We found that rambdax demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.