

Discover more from montoulieu.dev
Introduction
Rambdax is an extended version of the Rambda library, a collection of utility functions for JavaScript inspired by the popular Ramda library. This library is filled to the brim with lightweight, fast, and easy to use utilities, making it a valuable tool for developers. In this article, we'll explore 10 essential Rambdax utility functions that can simplify and enhance your code.
Compose
compose
is a powerful function that allows you to create a new function by combining multiple functions from right to left. This is particularly useful for creating a pipeline of transformations.
const toUpperCase = x => x.toUpperCase();
const addExclamation = x => x + '!';
const excitedGreeting = Rambdax.compose(addExclamation, toUpperCase);
console.log(excitedGreeting('hello')); // "HELLO!"
Map
map
applies a given function to each item in a list, creating a new list with the transformed items. This function is ideal for transforming data in a consistent way.
const celsiusToFahrenheit = c => (c * 9) / 5 + 32;
const temperaturesInCelsius = [0, 20, 30, 40];
const temperaturesInFahrenheit = Rambdax.map(celsiusToFahrenheit, temperaturesInCelsius);
console.log(temperaturesInFahrenheit); // [32, 68, 86, 104]
Filter
filter
helps you create a new list by filtering out items from an original list based on a predicate function. This is great for refining data based on specific conditions.
const isEven = x => x % 2 === 0;
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const evenNumbers = Rambdax.filter(isEven, numbers);
console.log(evenNumbers); // [2, 4, 6, 8]
Reduce
reduce
takes a list of items and reduces them to a single value using an accumulator function. This function is perfect for aggregating data or computing totals.
const sum = (accumulator, currentValue) => accumulator + currentValue;
const numbers = [1, 2, 3, 4, 5];
const total = Rambdax.reduce(sum, 0, numbers);
console.log(total); // 15
Pluck
pluck
extracts a list of property values from an array of objects. Use this function to quickly retrieve specific properties from a list of objects.
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
];
const names = Rambdax.pluck('name', users);
console.log(names); // ['Alice', 'Bob', 'Charlie']
GroupBy
groupBy
groups a list of items based on a function that returns a key for each item. This function is excellent for organizing data based on common attributes.
const students = [
{ name: 'Alice', grade: 'A' },
{ name: 'Bob', grade: 'B' },
{ name: 'Charlie', grade: 'A' },
{ name: 'David', grade: 'C' },
{ name: 'Eva', grade: 'B' }
];
const studentsByGrade = Rambdax.groupBy(student => student.grade, students);
console.log(studentsByGrade);
// {
// A: [{ name: 'Alice', grade: 'A' }, { name: 'Charlie', grade: 'A' }],
// B: [{ name: 'Bob', grade: 'B' }, { name: 'Eva', grade: 'B' }],
// C: [{ name: 'David', grade: 'C' }]
// }
7. SortBy
sortBy
sorts a list of items based on a function that returns a value for each item. Use this function to order data according to specific criteria.
const products = [
{ name: 'Laptop', price: 1200 },
{ name: 'Phone', price: 800 },
{ name: 'Monitor', price: 300 }
];
const productsSortedByPrice = Rambdax.sortBy(product => product.price, products);
console.log(productsSortedByPrice);
// [
// { name: 'Monitor', price: 300 },
// { name: 'Phone', price: 800 },
// { name: 'Laptop', price: 1200 }
// ]
Path
path
retrieves the value at a given path of an object. This function is useful for accessing deeply nested properties in objects.
const user = {
name: 'Alice',
address: {
street: '123 Main St',
city: 'New York',
state: 'NY',
zip: '10001'
}
};
const city = Rambdax.path(['address', 'city'], user);
console.log(city); // 'New York'
Pick
pick
creates a new object with only the specified properties from an original object. Use this function to extract specific properties from objects, especially when dealing with large data sets.
const user = {
id: 1,
name: 'Alice',
age: 30,
email: 'alice@example.com'
};
const userData = Rambdax.pick(['id', 'name', 'email'], user);
console.log(userData); // { id: 1, name: 'Alice', email: 'alice@example.com' }
Omit
omit
creates a new object without the specified properties from an original object. This function is particularly useful for removing sensitive or unnecessary information from objects before processing or sending data.
const user = {
id: 1,
name: 'Alice',
age: 30,
password: 'secret123'
};
const safeUser = Rambdax.omit(['password'], user);
console.log(safeUser); // { id: 1, name: 'Alice', age: 30 }
Conclusion
Rambdax offers an extensive collection of utility functions that can help simplify and streamline your JavaScript development. The 10 functions discussed in this article are just a small sampling of the power and versatility of Rambdax. By leveraging these functions, you can create cleaner, more efficient, and more maintainable code in your projects. To learn more about Rambdax and explore additional utility functions, visit the official GitHub repo: https://github.com/selfrefactor/rambda.