How to use the over function from ramda
Find comprehensive JavaScript ramda.over code examples handpicked from public code repositorys.
ramda.over is a function that returns a new function which applies a transformation to a specific path in a given object and returns a new copy of the object.
43 44 45 46 47 48 49 50 51 52
*/ const coerceUserOption = R.when( R.propSatisfies(R.complement(R.isNil), 'user'), R.compose( R.dissoc('user'), R.over( R.lens(updateHeaderWithUser, R.assoc('header')), R.identity ) )
+ 7 other calls in file
GitHub: DFEAGILEDEVOPS/MTC
74 75 76 77 78 79 80 81 82 83 84
} } function tryParseErrorObjectToString (error) { try { // R.over(R.lensProp('audience_sizes'), R.omit(['fb']), R.omit(['id'], obj)) // const reducedError = R.omit(['stack', 'originalError'], error) const reducedError = R.over(R.lensProp('originalError'), R.omit(['stack']), R.omit(['stack'], error)) const errorAsString = util.inspect(reducedError, true) return `error object:\n${errorAsString}`
+ 3 other calls in file
How does ramda.over work?
ramda.over
is a higher-order function provided by the Ramda library that allows you to create a new function that applies a given transformation to a specific part of an object or a nested object structure.
When you call ramda.over
with a lens and a transformer function, it returns a new function that expects an object as its argument. When this new function is called with an object, it applies the transformer function to the value that the lens focuses on and returns a new object with the result. The original object remains unchanged.
The lens can be a path to a nested property of an object, or a function that takes an object and returns the value that should be transformed. The transformer function is a function that takes a value and returns the transformed value.
GitHub: bahmutov/focha
23 24 25 26 27 28 29 30 31 32 33 34
log('shuffling %d describe blocks in "%s"', s.suites.length, s.title) const suitesLens = R.lensProp('suites') const shuffleSuites = R.over(suitesLens, _.shuffle) const shuffleNestedSuites = R.over(suitesLens, R.map(shuffleDescribes)) function shuffleTests (suite) { if (suite && Array.isArray(suite.tests) && suite.tests.length) { log('shuffling %d unit tests in "%s"',
32 33 34 35 36 37 38 39 40 41 42
injectPluginIndex( constructorName, i => { const pluginLens = lensPath(['plugins', i]) return over(pluginLens, transform, config) }, config ) )
+ 7 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11
const R = require("ramda"); const lensPath = R.lensPath(["x", "y"]); const incY = R.over(lensPath, R.inc); const obj = { x: { y: 5 } }; const newObj = incY(obj); console.log(newObj); // { x: { y: 6 } }
In this example, we define a lens path ['x', 'y'] and a transformer function R.inc (which increments its input by 1). We then use R.over to create a new transformer function incY that will increment the value at the lens path ['x', 'y']. We apply incY to an object { x: { y: 5 } } to create a new object with the value at the lens path incremented by 1. The resulting object { x: { y: 6 } } is logged to the console.
70 71 72 73 74 75 76 77 78 79 80
}; var convertPricesListToMap = function convertPricesListToMap(prices) { return R.reduce(function (acc, price) { return R.over(R.lensProp(price.hotelID), function (prevPrice) { return prevPrice ? R.over(R.lensProp('offers'), function (offers) { return R.concat(offers, price.offers); }, prevPrice) : price; }, acc); }, {}, prices);
+ 2 other calls in file
GitHub: corporatepiyush/es6
245 246 247 248 249 250 251 252 253
}) //=> 1 _.set(xLens, 4, { x: 1, y: 2 }) //=> {x: 4, y: 2} _.over(xLens, _.negate, { x: 1, y: 2 }) //=> {x: -1, y: 2}
+ 5 other calls in file
GitHub: mg901/webpack-with-ramda
5 6 7 8 9 10 11 12 13 14 15 16
...options, ...obj, }, }); exports.useLensWith = (rules) => R.over(R.lensProp('use'), R.concat(rules)); exports.rulesLensWith = (fn) => R.over(R.lensPath(['module', 'rules']), R.map(fn));
+ 2 other calls in file
GitHub: areca/misc
7273 7274 7275 7276 7277 7278 7279 7280 7281 7282
* * var xyLens = R.lensPath(['x', 'y']); * * R.view(xyLens, {x: {y: 2, z: 3}}); //=> 2 * R.set(xyLens, 4, {x: {y: 2, z: 3}}); //=> {x: {y: 4, z: 3}} * R.over(xyLens, R.negate, {x: {y: 2, z: 3}}); //=> {x: {y: -2, z: 3}} */ var lensPath = _curry1(function lensPath(p) { return lens(path(p), assocPath(p)); });
+ 113 other calls in file
GitHub: octachrome/t2
134 135 136 137 138 139 140 141 142 143
applySteal: R.curry((amount, context) => { const toSteal = Math.min(amount, R.view(Player.lens(context.target), context).cash); return R.pipe( R.over(Player.lens(context.target), Player.adjustCash(-toSteal)), R.over(activePlayerLens, Player.adjustCash(toSteal)) )(context); }), playerHasCash: (idx, cash) => R.pipe(
+ 55 other calls in file
GitHub: octachrome/t2
129 130 131 132 133 134 135 136 137 138
[Action.typeEq(Action.tax()), action => ( Executor.claimAction(Action.tax()) )], [Action.typeEq(Action.income()), action => ( R.pipe( R.over(activePlayerLens, Player.adjustCash(1)), Game.nextTurn ) )], [Action.typeEq(Action.challenge()), action => (
+ 11 other calls in file
GitHub: VesaLahd/aoc2022
34 35 36 37 38 39 40 41 42 43 44 45 46
const cave = Array(R.apply(R.multiply)(R.map(R.inc, normalizeCoordinates(maxDimensions)))).fill('.') const lineComponents = R.compose( R.apply(R.range), R.over(R.lensIndex(1), R.inc), R.sort(R.subtract), ) const createWall = R.compose(
+ 2 other calls in file
GitHub: jcla1/AdventOfCode
76 77 78 79 80 81 82 83 84 85 86 87
if (!R.equals(leftNeighbourNum, [])) { n = R.over(R.lensPath(leftNeighbourNum), R.add(ep[0]), n); } if (!R.equals(rightNeighbourNum, [])) { n = R.over(R.lensPath(rightNeighbourNum), R.add(ep[1]), n); } return n; };
+ 7 other calls in file
GitHub: akx/advent-of-code-2017
6 7 8 9 10 11 12 13 14 15
pair = fn(pair[1], result); } return result; }); const setIndex = (index, value) => R.set(R.lensIndex(index), value); const incrIndex = (index) => R.over(R.lensIndex(index), R.inc); const findMaxIndex = (arr) => R.findIndex(R.equals(R.reduce(R.max, 0, arr)))(arr); const nextStep = (banks, index) => (index + 1) % banks.length; const distStep = (banks, index, remaining) => (remaining <= 0 ? banks : distStep(incrIndex(index)(banks), nextStep(banks, index), remaining - 1)); const dist = (banks, index) => distStep(setIndex(index, 0)(banks), nextStep(banks, index), banks[index]);
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
* @see R.unless, R.when * @example * * var incCount = R.ifElse( * R.has('count'), * R.over(R.lensProp('count'), R.inc), * R.assoc('count', 1) * ); * incCount({}); //=> { count: 1 } * incCount({ count: 1 }); //=> { count: 2 }
+ 107 other calls in file
46 47 48 49 50 51 52 53 54 55
.then(res => { expect(commonMocks.recursivelyOmitProps(['timestamp', 'created'], res)) .toEqual(R.compose( R.omit(privateFields), mergeInsertId, R.over(R.lensProp('metaJson'), JSON.parse) )(FAKE_TENANT_CONNECTION_DATA)); done(); }); });
49 50 51 52 53 54 55 56 57 58
.then(res => { expect(commonMocks.recursivelyOmitProps(['timestamp', 'created'], res)) .toEqual(R.compose( R.omit(privateFields), mergeInsertId, R.over(R.lensProp('metaJson'), JSON.parse))(FAKE_TENANT_ORGANIZATION_DATA) ); done(); }); });
38 39 40 41 42 43 44 45 46 47
it('returns all cloudUsers', done => { getAllCloudUsers() .then(res => { res = R.map(R.compose( R.over(birthdayLens, d => d === '0000-00-00' ? d : moment(d).format(CALENDAR_DATE_FORMAT)), R.omit(privateFields) ))(res); expect(commonMocks.recursivelyOmitProps(['timestamp', 'created'], res)) .toEqual(KNOWN_TEST_CLOUD_USER_DATA);
+ 2 other calls in file
GitHub: VesaLahd/aoc2022
65 66 67 68 69 70 71 72 73 74
return R.compose( R.reduce((acc, [item, target]) => R.over( R.compose(R.lensIndex(target), R.lensProp('items')), R.append(item) )(acc), R.__, R.zip(worries, targetMonkeys)), R.over(currentMonkey, R.assoc('items', [])), R.over(R.compose(currentMonkey, R.lensProp('inspects')), R.add(R.length(worries))) )(monkeys) })
+ 7 other calls in file
8828 8829 8830 8831 8832 8833 8834 8835 8836
* * R.view(xHeadYLens, {x: [{y: 2, z: 3}, {y: 4, z: 5}]}); * //=> 2 * R.set(xHeadYLens, 1, {x: [{y: 2, z: 3}, {y: 4, z: 5}]}); * //=> {x: [{y: 1, z: 3}, {y: 4, z: 5}]} * R.over(xHeadYLens, R.negate, {x: [{y: 2, z: 3}, {y: 4, z: 5}]}); * //=> {x: [{y: -2, z: 3}, {y: 4, z: 5}]} */
+ 23 other calls in file
GitHub: rolfst/strapi-fp
61 62 63 64 65 66 67 68 69 70
likedByUser: { $toBool: { $size: '$likedByUser' } } } /* basic */ const matchLens = R.lensPath([0, '$match', '$and']) /** addMatcher : SemiGroupoid a -> MatchTarget -> MatchTarget */ const addMatcher = (setter, lens = matchLens) => R.over(lens, setter) const _arrOnCondition = (fn) => compose( alt(Maybe.of([])), map(Array.of),
ramda.clone is the most popular function in ramda (30311 examples)