How to use the evolve function from ramda

Find comprehensive JavaScript ramda.evolve code examples handpicked from public code repositorys.

ramda.evolve is a function in the Ramda library that applies a set of functions to the values of a given object and returns a new object with the updated values.

92
93
94
95
96
97
98
99
100
101
);

const coerceDataToApiDescriptions = R.compose(
  R.unnest,
  R.values,
  R.evolve({
    data: R.compose(
      R.map(([location, content]) => {
        const apiDescription = (typeof content === 'string')
          ? { location, content }
fork icon303
star icon0
watch icon2

+ 3 other calls in file

19
20
21
22
23
24
25
26
27
28
  Modify: [Number, Component.Action],
})

// Update
const update = Action.caseOn({ // action -> model -> model
  Add: (model) => R.evolve({items: R.append(Component.init(0))}, model),
  Reverse: R.evolve({items: R.reverse}),
  Modify: (idx, counterAction, model) => R.evolve({
    items: R.adjust(Component.update(counterAction), idx)
  }, model)
fork icon92
star icon0
watch icon0

+ 5 other calls in file

How does ramda.evolve work?

When you use ramda.evolve in your JavaScript code, you are using a function that applies a set of functions to the values of a given object and returns a new object with the updated values. To use ramda.evolve, you pass an object representing the transformations you want to apply as the first argument, and the object you want to transform as the second argument. The transformation object is an object with keys that correspond to the keys in the object you want to transform, and values that are functions that will be applied to the values in the original object. Each function takes the original value as an argument and returns the transformed value. Here is an example of using ramda.evolve to transform an object: javascript Copy code {{{{{{{ const R = require('ramda'); const person = { name: 'Alice', age: 30, address: { street: '123 Main St', city: 'Anytown', state: 'CA', zip: '12345' } }; const transformations = { age: R.add(1), address: { city: R.toUpper, state: R.toUpper } }; const updatedPerson = R.evolve(transformations, person); console.log(updatedPerson); In this example, we are using ramda.evolve to transform the person object by incrementing the age property by 1, and converting the city and state properties in the address object to uppercase. The resulting updatedPerson object is: css Copy code {{{{{{{ class="!whitespace-pre hljs language-css">{ name: 'Alice', age: 31, address: { street: '123 Main St', city: 'ANYTOWN', state: 'CA', zip: '12345' } } Overall, ramda.evolve provides a powerful and flexible way to transform objects in JavaScript, allowing you to write more concise and readable code.

27
28
29
30
31
32
33
34
35
36
});

const update = Action.caseOn({
    Insert: (model) =>
      R.evolve({nextId: R.inc, counters: R.append([model.nextId, counter.init(0)])}, model),
    Remove: (id, model) => R.evolve({counters: R.reject((c) => c[0] === id)}, model),
    Modify: (id, counterAction, model) =>
      R.evolve({counters: R.map((c) => {
                  const [counterId, counterModel] = c;
                  return counterId === id ? [counterId, counter.update(counterAction, counterModel)] : c;
fork icon92
star icon0
watch icon72

+ 5 other calls in file

48
49
50
51
52
53
54
55
56
57
                             nextId: R.inc,
                             newTitle: R.always('')}, model),
Remove: (todo, model) => R.evolve({todos: R.reject(R.eq(todo))}, model),
Modify: (todo, action, model) => {
  const idx = R.indexOf(todo, model.todos)
  return R.evolve({todos: R.adjust(Todo.update(action), idx)}, model)
},
ToggleAll: (model) => {
  const left = R.length(R.reject(R.prop('done'), model.todos)),
        todoAction = left === 0 ? Todo.Action.UnsetDone() : Todo.Action.SetDone();
fork icon92
star icon0
watch icon2

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const R = require("ramda");

const person = {
  name: "Alice",
  age: 30,
  address: {
    street: "123 Main St",
    city: "Anytown",
    state: "CA",
    zip: "12345",
  },
};

const transformations = {
  age: R.add(1),
  address: {
    city: R.toUpper,
    state: R.toUpper,
  },
};

const updatedPerson = R.evolve(transformations, person);
console.log(updatedPerson);

In this example, we are using ramda.evolve to transform the person object by incrementing the age property by 1, and converting the city and state properties in the address object to uppercase. The resulting updatedPerson object is: css Copy code

26
27
28
29
30
31
32
33
34
35

// View

const update = (action, model) =>
  Action.case({
    Insert: () => R.evolve({nextId: R.add(1),
                           counters: R.append([model.nextId, counter.init(0)])}, model),
    Remove: () => R.evolve({counters: R.tail}, model),
    Modify: (id, counterAction) =>
      R.evolve({counters: R.map((c) => {
fork icon92
star icon0
watch icon72

+ 5 other calls in file

19
20
21
22
23
24
25
26
27
28
  Modify: [Number, counter.Action],
})

const update = Action.caseOn({
    Insert: (model) =>
      R.evolve({nextId: R.inc, counters: R.append([model.nextId, counter.init(4)])}, model),
    Remove: (id, model) =>
      R.evolve({counters: R.reject((c) => c[0] === id)}, model),
    Modify: (id, counterAction, model) =>
      R.evolve({counters: R.map((c) => {
fork icon92
star icon0
watch icon72

+ 5 other calls in file

16
17
18
19
20
21
22
23
24
25
  const setDefaultProps = {
    status: R.defaultTo(eventStatuses.NOT_STARTED),
    archiveEvent: R.defaultTo(false),
    uncomposed: R.defaultTo(false),
  };
  return R.evolve(setDefaultProps, eventData);
};
const buildEvent = (props, eventData) => setDefaults(R.pick(props, eventData));
const buildOtData = userType => JSON.stringify({
  userType
fork icon12
star icon5
watch icon0

226
227
228
229
230
231
232
233
234
235
236
237
        data: {
            elapsed: _.add(1),
            remaining: _.add(-1)
        }
    }
    console.log("evolve=", _.evolve(transformations, tomato)) //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}


}



fork icon1
star icon2
watch icon0

17
18
19
20
21
22
23
24
25
26
const dcr = R.evolve({
	[cur_name]: {
		retain: R.dec,
	}
})
const refreshContext = R.evolve({
	[cur_name]: {
		retain: R.always(max_retain),
	}
})
fork icon1
star icon0
watch icon0

+ 3 other calls in file

47
48
49
50
51
52
53
54
55
56
return octokit.request("GET /repos/" + pkg.name).then((res) => {
    return R.mergeLeft(pkg, { forkOf: R.defaultTo(null, R.path(["data", "parent", "full_name"], res)) })
}).catch((error) => {
    console.error(`Error while getting repo info for ${pkg.name}: `, error.message)
    if (error.message == "Not Found")
        return R.evolve({ tags: R.append("uncat/deleted") }, pkg)
    else
        return pkg  // the package object shouldn't have a forkOf property 
    // if we failed to retrieve repo info
})
fork icon1
star icon0
watch icon0

+ 3 other calls in file

228
229
230
231
232
233
234
235
236
237
// remove double quotes from certain options
const removeQuotes = {
  group: dequote,
  ciBuildId: dequote,
}
const cleanOpts = R.evolve(removeQuotes, opts)

debug('parsed cli options %o', cleanOpts)

return cleanOpts
fork icon0
star icon0
watch icon1

1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
 *      var transformations = {
 *        firstName: R.trim,
 *        lastName: R.trim, // Will not get invoked.
 *        data: {elapsed: R.add(1), remaining: R.add(-1)}
 *      };
 *      R.evolve(transformations, tomato); //=> {firstName: 'Tomato', data: {elapsed: 101, remaining: 1399}, id:123}
 */
var evolve = _curry2(function evolve(transformations, object) {
    var result = {};
    var transformation, key, type;
fork icon0
star icon0
watch icon0

+ 17 other calls in file

36
37
38
39
40
41
42
43
44
45
46
47


const activePlayerLens = (() => {
  // Context -> Player
  const getter = context => context.players[context.whoseTurn];
  // Context -> Player -> Context
  const setter = (player, context) => R.evolve({players: R.update(context.whoseTurn, player)}, context);


  return R.lens(getter, setter);
})();

fork icon0
star icon0
watch icon0

+ 41 other calls in file

25
26
27
28
29
30
31
32
33
34
35
36


const activePlayerLens = (() => {
  // Game -> Player
  const getActivePlayer = game => game.players[game.whoseTurn];
  // Game -> Player -> Game
  const setActivePlayer = (player, game) => R.evolve({players: R.update(game.whoseTurn, player)}, game);


  return R.lens(getActivePlayer, setActivePlayer);
})();

fork icon0
star icon0
watch icon0

+ 19 other calls in file

Other functions in ramda

Sorted by popularity

function icon

ramda.clone is the most popular function in ramda (30311 examples)