Most useful methods of ‘ramda.js’

List of methods:

  1. Pluck
  2. ApplySpec
  3. Cond
  4. PropEq
  5. Pipe
  6. FromPairs
  7. PathOr

Pluck

Using all the time. Go throw the array and take the value which we put as the key

const getAges = R.pluck(‘age’);getAges([{name: ‘fred’, age: 29}, {name: ‘wilma’, age: 27}]); //=> [29, 27]

ApplySpec

The gold key when we would like to format or translate our values of the object

const getTranslated = R.applySpec({  header: i18n.translate,  content: i18n.translate});getTranslated(article); //=> {header: ‘Translated header’, content: ‘Translated content’}

Cond

The devilish replacement for all the mad “switch” and all the awful if / else cases.

const fn = R.cond([  [R.equals(0), R.always(‘water freezes at 0°C’)],  [R.equals(100), R.always(‘water boils at 100°C’)],  [R.T, temp => ‘nothing special happens at ‘ + temp + ‘°C’]]);fn(0); //=> ‘water freezes at 0°C’fn(50); //=> ‘nothing special happens at 50°C’fn(100); //=> ‘water boils at 100°C’

PropEq

Helps to sort and filter the list of data

const abby = {name: ‘Abby’, age: 7, hair: ‘blond’};const fred = {name: ‘Fred’, age: 12, hair: ‘brown’};const rusty = {name: ‘Rusty’, age: 10, hair: ‘brown’};const alois = {name: ‘Alois’, age: 15, disposition: ‘surly’};const kids = [abby, fred, rusty, alois];const hasBrownHair = R.propEq(‘hair’, ‘brown’);R.filter(hasBrownHair, kids); //=> [fred, rusty]

Pipe

Helper for implementation graceful functions

const getFirstFiveBooks = R.pipe(   R.filter(propEq(‘type’, ‘book’),   R.path(‘bookInfo’),   R.take(5));

FromPairs

From array to object

R.fromPairs([[‘a’, 1], [‘b’, 2], [‘c’, 3]]); //=> {a: 1, b: 2, c: 3}

PathOr

When you not sure about your path of the object

R.pathOr(‘N/A’, [‘a’, ‘b’], {a: {b: 2}}); //=> 2R.pathOr(‘N/A’, [‘a’, ‘b’], {c: {b: 2}}); //=> “N/A”

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store