[Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve

We don't always control the data we need in our applications, and that means we often find ourselves massaging and transforming our data. In this lesson, we'll learn how to transform objects in a declarative way using ramda's evolve function.

 

Assume we have this object:

const product = {
    name: 'cog',
    price: 100,
    details: {
      shippingInfo: {
        weight: 7,
        method: 'ups'
      }
    }
  }

 

Let's say we have to do 

  • Uppcase for name
  • Double price
  • Change weight to 8

Using Ramda's evolve:

const product = {
    name: 'cog',
    price: 100,
    details: {
      shippingInfo: {
        weight: 7,
        method: 'ups'
      }
    }
  }

const transformation = {
    name: R.toUpper,
    price: R.multiply(2),
    details: {
      shippingInfo: {
        weight: R.inc
      }
    }
};
const adjustProduct = R.evolve(transformation);
const result = adjustProduct(product)

 

posted @ 2017-01-19 16:36  Zhentiw  阅读(190)  评论(0编辑  收藏  举报