입력:23/02/06수정:24/01/05

ramdajs 유용한 7가지 예시

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

(=condition)
switch처럼 원하는 결과값에 매칭이 가능함

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

sort 혹은 filter 할때 사용하기 유용함

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)
);

pathOr

When you not sure about your path of the object

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

토픽: ramdajs
영역: 영역/개발
타입: 타입/스니펫
소스: 링크