Rambda
Faster alternative to Ramda in just 7kB
Argumentation
I admire Ramda as it is great library in what it does, but I used only small part of what it offers.
I wanted to optimize the size of my bundle, but already developed Ramda habits.
This lead me to the idea to recreate the funtionality of some Ramda methods and export that as library.
Example use
const R = require("rambda")
const result = R.compose(
R.filter(val => val>2),
R.flatten,
)([ [1], [2], [3], 4])
console.log(result) // => [3,4]
Install
https://cdnjs.cloudflare.com/ajax/libs/rambda/0.5.9/webVersion.js
Differences between Rambda and Ramda
Rambda shadows only small part of the Ramda's API.
A few things to note:
-
Rambda's methods should be compatible with most of the basic Ramda's methods.
For more complex and Ramda specific methods(such as R.__), you should expect a mismatch.
-
Rambda's type detect async functions. The returned value is "Async"
-
Rambda's curry works in different way than Ramda's curry
. Please chech the documentation of the method.
-
Rambda's map/filter work only for arrays, while Ramda's map/filter accept also objects.
-
Rambda's equals doesn't protect against circular structures as Ramda.equals does.
-
Rambda is tested for compatability with Ramda.flip, as this method could be useful in some cases.
Benchmark
Scroll to API
Flowtype
I haven't tested it fully, but the partial test shows that Ramda
definitions can be used.
You need to replace declare module ramda
with declare module rambda
on line
10 and store the file as rambda.js in your flow-typed folder
API
api-list
add
add(a: Number, b: Number): Number
R.add(2, 3)
adjust
adjust(replaceFn: Function, i:Number, arr:Array): Array
- Replaces
i
index in arr
with the result of replaceFn(arr[i])
R.adjust(a => a + 1, 0, [0, 100])
any
any(condition: Function, arr: Array): Boolean
- Returns true if at least one member of
arr
returns true, when passed to the condition
function
R.any(a => a * a > 8)([1, 2, 3])
R.any(a => a * a > 10)([1, 2, 3])
append
append(valueToAppend: any, arr: Array): Array
R.append('foo', ['bar', 'baz'])
contains
contains(valueToFind: any, arr: Array): Boolean
Returns true if valueToFind
is part of arr
R.contains(2, [1, 2])
R.contains(3, [1, 2])
curry
curry(fn: Function, a: Object, b: Object): Function
Returns function called with merged object of a
over b
.
It supports async functions.
Works only when as arguments are used on first call fn
and a
and on second call b
.
See the example below:
const fn = ({a, b, c}) => {
return (a * b) + c
}
const curried = R.curry(fn, {a: 2})
curried({b: 3, c: 10})
defaultTo
defaultTo(defaultArgument: T, inputArgument: any): T
Returns defaultArgument
if inputArgument
is undefined
or the type of inputArgument
is different of the type of defaultArgument
.
Returns inputArgument
in any other case.
R.defaultTo('foo', undefined)
R.defaultTo('foo')('bar')
R.defaultTo('foo')(1)
drop
drop(howManyToDrop: Number, arrOrStr: Array|String): Array|String
Returns arrOrStr
with howManyToDrop
items dropped from the left
R.drop(1, ['foo', 'bar', 'baz'])
R.drop(1, 'foo')
dropLast
dropLast(howManyToDrop: Number, arrOrStr: Array|String): Array|String
Returns arrOrStr
with howManyToDrop
items dropped from the right
R.dropLast(1, ['foo', 'bar', 'baz'])
R.dropLast(1, 'foo')
equals
equals(a: any, b: any): Boolean
- Returns equality match between
a
and b
Doesn't handles cyclical data structures
R.equals(1, 1)
R.equals({}, {})
R.equals([1, 2, 3], [1, 2, 3])
filter
filter(filterFn: Function, arr: Array): Array
Filters arr
throw boolean returning filterFn
const filterFn = a => a % 2 === 0
R.filter(filterFn, [1, 2, 3, 4])
find
find(findFn: Function, arr: Array): T|undefined
Returns undefined
or the first element of arr
satisfying findFn
const findFn = a => R.type(a.foo) === "Number"
const arr = [{foo: "bar"}, {foo: 1}]
R.find(findFn, arr)
findIndex
findIndex(findFn: Function, arr: Array): Number
Returns -1
or the index of the first element of arr
satisfying findFn
const findFn = a => R.type(a.foo) === "Number"
const arr = [{foo: "bar"}, {foo: 1}]
R.find(findFn, arr)
flatten
flatten(arr: Array): Array
R.flatten([ 1, [ 2, [ 3 ] ] ]
head
head(arrOrStr: Array|String): any
- Returns the first element of
arrOrStr
R.head([1, 2, 3])
R.head('foo')
indexOf
indexOf(valueToFind: any, arr: Array): Number
Returns -1
or the index of the first element of arr
equal of valueToFind
R.indexOf(1, [1, 2])
init
init(arrOrStr: Array|String): Array|String
- Returns all but the last element of
arrOrStr
R.init([1, 2, 3])
R.init('foo')
join
join(separator: String, arr: Array): String
R.join('-', [1, 2, 3])
last
last(arrOrStr: Array|String): any
- Returns the last element of
arrOrStr
R.last(['foo', 'bar', 'baz'])
R.last('foo')
length
length(arrOrStr: Array|String): Number
R.length([1, 2, 3])
map
map(mapFn: Function, arr: Array): Array
Returns the result of looping through arr
with mapFn
const mapFn = x => x * 2;
R.map(mapFn, [1, 2, 3])
match
map(regExpression: Regex, str: String): Array
R.match(/([a-z]a)/g, 'bananas')
merge
merge(a: Object, b: Object)
Returns result of Object.assign({}, a, b)
R.merge({ 'foo': 0, 'bar': 1 }, { 'foo': 7 })
omit
omit(propsToOmit: Array, obj: Object): Object
- Returns a partial copy of an
obj
with omitting propsToOmit
R.omit(['a', 'd'], {a: 1, b: 2, c: 3})
path
path(pathToSearch: Array, obj: Object): any
- Retrieve the value at
pathToSearch
in object obj
R.path(['a', 'b'], {a: {b: 2}})
R.path(['a', 'c'], {a: {b: 2}})
pick
pick(propsToPick: Array, obj: Object): Object
- Returns a partial copy of an
obj
containing only propsToPick
properties
R.pick(['a', 'c'], {a: 1, b: 2})
pluck
pluck(prop: String, arr: Array): Array
- Returns list of the values of property
prop
taken from the objects in arr
R.pluck('a')([{a: 1}, {a: 2}])
prepend
prepend(valueToPrepend: any, arr: Array): Array
R.prepend('foo', ['bar', 'baz'])
prop(propToFind: String, obj: Object): any
Returns undefined
or the value of property propToFind
in obj
R.prop('x', {x: 100})
R.prop('x', {})
propEq
propEq(propToFind: String, valueToMatch: any, obj: Object): Boolean
Returns true if obj
has property propToFind
and its value is equal to valueToMatch
const propToFind = "foo"
const valueToMatch = 0
R.propEq(propToFind, valueToMatch)({foo: 0})
R.propEq(propToFind, valueToMatch)({foo: 1})
range
range(start: Number, end: Number): Array
- Returns a array of numbers from
start
(inclusive) to end
(exclusive)
R.range(0, 2)
repeat
repeat(valueToRepeat: T, num: Number): Array
R.repeat('foo', 2)
replace
replace(strOrRegex: String|Regex, replacer: String, str: String): String
Replace strOrRegex
found in str
with replacer
R.replace('foo', 'bar', 'foo foo')
R.replace(/foo/, 'bar', 'foo foo')
R.replace(/foo/g, 'bar', 'foo foo')
sort
sort(sortFn: Function, arr: Array): Array
Returns copy of arr
sorted by sortFn
sortFn
must return Number
const sortFn = (a, b) => a - b
R.sort(sortFn, [3, 1, 2])
sortBy
Returns copy of arr
sorted by sortFn
sortFn
must return value for comparison
const sortFn = obj => obj.foo
R.sortBy(sortFn, [
{foo: 1},
{foo: 0}
])
split
split(separator: String, str: String): Array
R.split('-', 'a-b-c')
splitEvery
splitEvery(sliceLength: Number, arrOrString: Array|String): Array
- Splits
arrOrStr
into slices of sliceLength
R.splitEvery(2, [1, 2, 3])
R.splitEvery(3, 'foobar')
subtract
subtract(a: Number, b: Number): Number
Returns a
minus b
R.subtract(3, 1)
tail
tail(arrOrStr: Array|String): Array|String
- Returns all but the first element of
arrOrStr
R.tail([1, 2, 3])
R.tail('foo')
take
take(num: Number, arrOrStr: Array|String): Array|String
- Returns the first
num
elements of arrOrStr
R.take(1, ['foo', 'bar'])
R.take(2, ['foo'])
takeLast
takeLast(num: Number, arrOrStr: Array|String): Array|String
- Returns the last
num
elements of arrOrStr
R.takeLast(1, ['foo', 'bar'])
R.takeLast(2, ['foo'])
test
test(regExpression: Regex, str: String): Boolean
- Determines whether
str
matches regExpression
R.test(/^f/, 'foo')
R.test(/^f/, 'bar')
toLower
toLower(str: String): String
R.toLower('FOO')
toUpper
toUpper(str: String): String
R.toUpper('foo')
trim
trim(str: String): String
R.trim(' foo ')
type
type(a: any): String
R.type(() => {})
R.type(async () => {})
R.type([])
R.type({})
R.type('s')
R.type(1)
R.type(false)
R.type(null)
R.type(/[A-z]/)
uniq
uniq(arr: Array): Array
- Returns a new array containing only one copy of each element in
arr
R.uniq([1, 1, 2, 1])
R.uniq([1, '1'])
update
update(i: Number, replaceValue: any, arr: Array): Array
- Returns a new copy of the
arr
with the element at i
index
replaced with replaceValue
R.update(0, "foo", ['bar', 'baz'])
values
values(obj: Object): Array
- Returns array with of all values in
obj
R.values({a: 1, b: 2})
Projects using Rambda
Articles about Rambda