Skip to content

Instantly share code, notes, and snippets.

@u840903
Created January 3, 2019 15:04
Show Gist options
  • Save u840903/59ac4a5d8b2ca178452996be2327152d to your computer and use it in GitHub Desktop.
Save u840903/59ac4a5d8b2ca178452996be2327152d to your computer and use it in GitHub Desktop.
Lodash --> Ramda
const user = {
name: {
forename: 'Jan',
surname: 'Jarfalk'
},
age: 34,
friends: ['Peter','Bjorn','John', '']
};
// Get --> Prop, Path, Lens*
const name = R.prop('name', user);
const forename = R.path(['name','forename'], user);
const getSurname = R.path(['name','surname']);
const surname = getSurname(user);
// GetOr --> PropOr, PathOr
const getNickname = R.pathOr('N/A', ['name','nickename']);
const nickname = getNickname(user);
// Set --> Assoc, AssocPath
const user2 = R.assocPath(['name','forename'], 'Kenneth', user);
const user3 = R.assoc('age', 23, user);
// Filter --> Filter
const isEven = n => n % 2 === 0;
R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
// Entries --> Pairs
R.toPairs({a: 1, b: 2, c: 3}); //=> [['a', 1], ['b', 2], ['c', 3]]
// FlowRight, Compose --> Compose
const classyGreeting = (firstName, lastName) => "The name's " + lastName + ", " + firstName + " " + lastName
const yellGreeting = R.compose(R.toUpper, classyGreeting);
yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"
// Find --> Find
const xs = [{a: 1}, {a: 2}, {a: 3}];
R.find(R.propEq('a', 2))(xs); //=> {a: 2}
R.find(R.propEq('a', 4))(xs); //=> undefined
// FlattenDeep --> Flatten.
R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// GroupBy --> GroupBy
const byGrade = R.groupBy(function(student) {
const score = student.score;
return score < 65 ? 'F' :
score < 70 ? 'D' :
score < 80 ? 'C' :
score < 90 ? 'B' : 'A';
});
const students = [{name: 'Abby', score: 84},
{name: 'Eddy', score: 58},
// ...
{name: 'Jack', score: 69}];
byGrade(students);
// IsEmpty --> isEmpty
R.isEmpty([1, 2, 3]); //=> false
R.isEmpty([]); //=> true
R.isEmpty(''); //=> true
R.isEmpty(null); //=> false
R.isEmpty({}); //=> true
R.isEmpty({length: 0}); //=> false
// IsEqual --> eqProps
const o1 = { a: 1, b: 2, c: 3, d: 4 };
const o2 = { a: 10, b: 20, c: 3, d: 40 };
R.eqProps('a', o1, o2); //=> false
R.eqProps('c', o1, o2); //=> true
// Map --> Map
const double = x => x * 2;
R.map(double, [1, 2, 3]); //=> [2, 4, 6]
R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
// Merge --> Merge
// Partition --> Partition
// pickBy --> pickBy
// reduce --> reduce
// Remove --> Filter
// Reverse --> Reverse
// Sortby --> Sortby
// toArray --> values (depends)
// uniqBy --> uniqBy
// Throttle --> lodash.throttle
// Debounce --> lodash.debounce
// Unset --> dissoc
R.dissoc('b', {a: 1, b: 2, c: 3}); //=> {a: 1, c: 3}
// Update --> set
// Flow --> Pipe
// Flatten
// Size
// *Lens
const getFriends = R.path(['friends']);
const setFriends = R.assocPath(['friends']);
const friendsLens = R.lens(getFriends, setFriends);
const friends = R.view(friendsLens, user);
const noFriends = R.set(friendsLens, [], user);
const reverseFriends = R.over(friendsLens, reverse, user);
const isNotEmpty = R.filter(x => x);
const noEmptyFriends = R.over(friendsLens, isNotEmpty, user);
// What Function Should I Use?
// https://github.com/ramda/ramda/wiki/What-Function-Should-I-Use%3F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment