bambi
Like a ram, but softer
API
- adjust
- always
- assoc
- both
- clone
- compose
- composeP
- curry
- dec
- deepClone
- deepMerge
- defaultTo
- defer
- dissoc
- equals
- F
- filter
- gt
- gte
- head
- identity
- ifElse
- inc
- insert
- is
- isNil
- lens
- lensPath
- lensProp
- lt
- lte
- map
- not
- omit
- once
- path
- pathEq
- pathOr
- pipe
- prop
- propOr
- random
- reduce
- reverse
- set
- T
- tail
- uniq
- uppercase
- view
- zip
adjust
adjust: (a -> b) -> Int -> [a] -> [b]
const fn = const index = 1const list = 0 0 0 // [0, 1, 0]
always
always: a -> () -> a
const obj = {} === obj
Returns a function that always returns the passed in value.
assoc
assoc: a -> string -> Obj -> Obj
const obj = name: 'Tim' age: 28 const birthdayAge = 29const newObj = newObjage // 29newObjname // 'Tim'
Returns a shallow copy of the object, with the key associated with the value
both
both: (a -> Boolean) -> (a -> Boolean) -> a -> Boolean
const isTim = name === 'Tim'const isAdmin = level === 'admin' const person = name: 'Tim' level: 'admin' const canView = // true // false
clone
clone: a -> a
const arr = 1 2 3const cloned = // [1, 2, 3] cloned === arr // falsecloned0 === arr0 // true const obj = k: v const clonedObj = clonedObj === obj // falseclonedObjk === objk // true
Performs a shallow clone of an array or object.
compose
compose: (b -> c) -> (a -> b) -> (a -> c)
const getName = datanameconst uppercase = str const getUpperCaseName = const data = name: 'Tim' // 'TIM'
Composes functions together into a single unary function, calling the passed in functions right-to-left order.
composeP
composeP: (b -> Promise<c> | c) -> (a -> Promise<b> | b) -> (Promise<a> | a) -> Promise<c>
const apiCall = Promiseconst getFriends = Promise const upperCaseNames = const composed = // 'JOHN'
Composes functions that may or may not return a promise into a function that returns a promise, given a value
cond
cond: ([[(a -> boolean), (a -> b)]]) -> a -> b
const conditions = const tim = name: 'tim' const john = name: 'john' // 'TIM' // 'NHOJ'
Applies the transformation function given at the first predicate that returns truthy
curry
curry: ((a, b) -> c) -> (a -> b -> c)
const fn = a + bconst curried = 12 // 3
Returns a wrapped version of the passed in function, returning functions until all of the arguments are given.
dec
dec: number -> number
// 0
Returns the number passed in minus 1
deepClone
deepClone: a -> a
const arr = k: vconst cloned = cloned === arr // falsecloned0 === arr0 // false const obj = k: k: v const clonedObj = clonedObj === obj // falseclonedObjk === objk // false
Performs a deep cloning of an object, recursively until getting to primitives.
deepMerge
deepMerge: a -> a -> a
const a = data: name: first: 't' const b = data: name: last: 'r' // { data: { name: { first: 't', last: 'r' } } } // { data: [1, 2] }
Recursively merges keys on the objects.
defaultTo
defaultTo: a -> b -> a | b
const value = trueconst other = undefinedconst defaulted = 'tim' // true // 'tim'
Returns the passed in value if that value is not null
or undefined
. Returns the default value is not
defer
defer: Array<a> -> (a -> b) -> () -> b
const fn = consoleconst defered = const value = value // 1
Returns a function that will apply a function with the given arguments
dissoc
dissoc: string -> Obj -> Obj
const obj = name: 'Tim' age: 28 const newObj = newObjage // 28newObjname // undefined objage // 28objname // 'Tim'
Returns a shallow copy, with the given key deleted
equals
equals: a -> b -> Boolean
const a = 1const b = 1const c = {}const d = {} // true // false// false because {} !== {}
Returns whethere both values are strictly equal or not
F
F: () -> boolean
// false
Returns false
gt
gt: number -> number -> boolean
const a = 2const b = 1 // true // false
Returns whether the first value is greater than the second
gte
gte: number -> number -> boolean
const a = 2const b = 1 // true // true // false
Returns whether the first value is greater or equal to the second
head
head: Array<a> -> a
const list = 12 3 // 1
Returns the first item in the list or undefined if empty
identity
identity: a -> a
const obj = {}const id = id === obj
Returns the passed in value
ifElse
ifElse: (a -> boolean) -> (a -> *) -> (a -> *) -> a -> *
const hasAge = 'age' in dataconst double = num * 2const defaultAge = 30 const getAge = // 30 // 1
Returns the value of the second argument if the first function returns truthy, returns the third argument if not
inc
inc: number -> number
// 2
Returns the number plus one
inserts
insert: a -> number -> Array<a> -> Array<a>
const list = 1 2 3const value = 4const index = 1 // [1, 4, 2, 3]
is
is: (Constructor | string) -> a -> Boolean
const isString = const isFunction = {}const isClass = // true // true {}const b = // true
Returns if the passed in value is of the same type as the passed in constructor or if the typeof
of the passed in value is the same as the passed in string type.
isNil
isNil: a -> Boolean
// true // true // true // false // false // false
Returns true if passed undefined
or null
. Returns false otherwise
lens
lens: (a -> *) -> (* -> a) -> Lens
const data = name: 'Tim' age: 29 const nameLens = // { get: prop('name'), set: assoc('name') }
Returns a Bambi
-style lens
object for using in set
, over
, and view
.
lensPath
lensPath: [string | number] -> Lens
const data = name: 'Tim' location: state: 'TN' const stateLens = const value = // 'TN'
Returns a Babmi
-style lens
at the given path
lensProp
lensProp: string | number -> Lens
const data = name: 'Tim' const nameLens = const value = // 'Tim'
lt
lt: number -> number -> boolean
const a = 1const b = 2 // true // false
Returns true if the first value is less than the second, false if otherwise
lte
lte: number -> number -> boolean
const a = 1const b = 2 // true // true // false
map
map: (a -> b) -> Iterator -> Iterator
const list = 1 2 3const fn = a * 2 // [2, 4, 6] const obj = a: 1 b: 2 c: 3 // { a: 2, b: 4, c: 6 }
Iterates over a data structures items and applies the passed function to each
not
not: (a -> b) -> args -> Boolean
const fn = falseconst notFn = // true const otherFn = trueconst notOther = // false
Returns a function that returns the opposite Boolean value as the passed in function
over
over: Lens -> (a -> b) -> * -> *
const data = name: 'Tim' age: 29 const nameLens = const updated = // { name: 'John', age: 29 }
Returns the data structure passed in, with the value at the lens
updated
omit
omit: (Array<string>) -> a -> a
const keys = 'name' 'age'const obj = name : 'tim' age: 29 location: 'USA' // { location: 'USA' }
Returns a shallow copy of the passed in object, with the given keys removed
once
once: (* -> *) -> * -> *
let count = 0const fn = count++const oner = // 1 // 1count // 2
Returns a function that will call the passed in function only once, always returning the given value
path
path: Array<string|number> -> DataStructure -> a
const obj = name: 'Tim' location: city: 'SF' area: 'CA' const area = area === 'CA'
Returns the value at the path, walking the data structure
pathEq
pathEq: Array<string | number> -> a -> b -> Boolean
const data = location: city: 'Cookeville' const path = 'location' 'city'const value = 'Cookeville' // true
Returns whether or not a path is equal to a value inside of an object or not
pathOr
pathOr: a -> Array<string|number> -> DatStructure -> a
const obj = {}const value = value === 1
Returns the value at the path or the passed in default
pipe
pipe: (a -> b) -> (b -> c)
const add1 = v + 1const add2 = v + 2 const adder = // 3
Returns a function that is the functions passed in, called in right-to-left order.
pipeP
pipe: (a -> Promise<b> | b) -> (b -> Promise<c> | c) -> (Promise<a> | a) -> Promise<c>
const apiCall = Promiseconst getFriends = Promiseconst toUpperCase = str const piped = pipe // ['JOHN']
prop
prop: (string|number) -> DataStructure -> a
const obj = name: 'Tim' const value = value === 'Tim'
Returns the value at the given key
propEq
propEq: (string | number) -> a -> b -> Boolean
const data = name: 'Tim' const prop = 'name'const value = 'John' // false
Returns whether or not a prop is equal to a value inside of an object
propOr
propOr: a -> (string|number) -> DataStructure -> a
const obj = name: 'Tim' const name = const age = name // 'Tim'age // 'default'
Returns the value at the prop or the default
random
random: number -> number -> Integer
const min = 1const max = 10 // random integer between 1 - 10
Returns a random number between the min/max inclusively.
reduce
reduce: (a -> b -> c) -> a -> a[] -> c
const reducer = acc + currconst initialState = 0const list = 1 2 3 // 6
Reduce a list into a single value given a reducer function.
reverse
reverse: (Array<a> | string) -> Array<a> | string
const list = 1 2 3 // [3, 2, 1] const str = 'abc' // 'cba'
Returns a shallow copy of the passed in list or string but in reverse order
T
T: () -> boolean
// true
Returns true
tail
tail: Array<a> -> Array<a>
const list = 1 2 3 // [2, 3]
Returns all but the first item in the list
set
set: Lens -> * -> a -> a
const data = { name: 'Tim', age: 29 }
const nameLens = lensProp('name')
const updated = set(nameLens, 'Joh', data) // { name: 'John', age: 29 }
Sets the value at the location pointed to by the lens.
uniq
uniq: Array<T> -> Array<T>
const arr = 1 2 3 1 // [1, 2, 3]
Returns a copy of the passed in array, with only unique values.
uppercase
uppercase: string -> string
const name = 'tim' // 'TIM'
Returns the uppercased version of the passed string
view
view: Lens -> a -> *
const data = name: 'Tim' const nameLens = const value = // 'Tim'
Allows for reading or viewing of a Bambi-style lens
zip
zip: (a -> b -> c) -> [a] -> [b] -> [c]
const list1 = 1 2 3const list2 = 1 1 1const zipped = // [2, 3, 4]
Mixes two arrays by a given function