101 Cheat Sheet

From WikiOD

Here is the cheatsheat of 101 and basic guide on how to use 101

Usage[edit | edit source]

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

Every function is exposed as a module.

See: 101

Type checking[edit | edit source]

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

Objects[edit | edit source]

{: .-three-column}

Example[edit | edit source]

{: .-prime}

let obj = {}

Update[edit | edit source]

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

Read[edit | edit source]

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

Delete[edit | edit source]

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

Getting[edit | edit source]

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

pluck returns values, pick returns subsets of objects.

See: pluck, pick

Setting[edit | edit source]

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

See: put

Deleting[edit | edit source]

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

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

See: omit, del

Keypath check[edit | edit source]

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

See: hasKeypaths

Get values[edit | edit source]

values(state)

Functions[edit | edit source]

Simple functions[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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

See: passAll, passAny

Converge[edit | edit source]

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

See: converge

Arrays[edit | edit source]

Finding[edit | edit source]

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

Grouping[edit | edit source]

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

Examples[edit | edit source]

Function composition[edit | edit source]

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

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

Reference[edit | edit source]

Credit:rstacruz