101

101 is a JavaScript library for dealing with immutable data in a functional manner.

A cheatsheet by @rstacruz|Refreshed 7 months ago.Refresh|View source on Github

Usage

Usage

const isObject = require('101/isObject')
isObject({}) // → true

Every function is exposed as a module.

See: 101

Type checking

Type checking

isObject({})
isString('str')
isRegExp(/regexp/)
isBoolean(true)
isEmpty({})
isfunction(x => x)
isInteger(10)
isNumber(10.1)
instanceOf(obj, 'string')

Example

Example

let obj = {}

Update

obj = put(obj, 'user.name', 'John')
// → { user: { name: 'John' } }

Read

pluck(name, 'user.name')
// → 'John'

Delete

obj = del(obj, 'user')
// → { }

Getting

Getting

pluck(state, 'user.profile.name')
pick(state, ['user', 'ui'])
pick(state, /^_/)

pluck returns values, pick returns subsets of objects.

See:
pluck,
pick

Setting

Setting

put(state, 'user.profile.name', 'john')

See:
put

Deleting

Deleting

del(state, 'user.profile')
omit(state, ['user', 'data'])

omit is like del, but supports multiple keys to be deleted.

See:
omit,
del

Keypath check

Keypath check

hasKeypaths(state, ['user'])
hasKeypaths(state, { 'user.profile.name': 'john' })

See:
hasKeypaths

Get values

Get values

values(state)

Simple functions

Simple functions

and(x, y) x && y
or(x, y) x || y
xor(x, y) !(!x && !y) && !(x && y)
equals(x, y) x === y
exists(x) !!x
not(x) !x

Useful for function composition.

See:
and,
equals,
exists

Composition

Composition

compose(f, g)       // x => f(g(x))
curry(f)            // x => y => f(x, y)
flip(f)             // f(x, y) --> f(y, x)

See:
compose,
curry,
flip

And/or

And/or

passAll(f, g)       // x => f(x) && g(x)
passAny(f, g)       // x => f(x) || g(x)

See:
passAll,
passAny

Converge

Converge

converge(and, [pluck('a'), pluck('b')])(x)
// → and(pluck(x, 'a'), pluck(x, 'b'))

See:
converge

Finding

Finding

find(list, x => x.y === 2)
findIndex(list, x => ...)
includes(list, 'item')
last(list)
find(list, hasProps('id'))

Grouping

Grouping

groupBy(list, 'id')
indexBy(list, 'id')

Function composition

Function composition

isFloat = passAll(isNumber, compose(isInteger, not))
// n => isNumber(n) && not(isInteger(n))
function doStuff (object, options) { ... }

doStuffForce = curry(flip(doStuff))({ force: true })