Essential stream transformation operations for mapping, filtering, and basic data manipulation.
Transforms each value in the stream using a function.
/**
* Transform each value using a function
* @param {Function} fn - Transformation function
* @returns {Stream} Stream with transformed values
*/
Stream.prototype.map(fn);Usage Examples:
// Basic mapping
_([1, 2, 3]).map(x => x * 2).toArray(console.log); // [2, 4, 6]
// Object transformation
_([{name: 'Alice'}, {name: 'Bob'}])
.map(user => ({...user, greeting: `Hello ${user.name}`}))
.toArray(console.log);Filters values based on a predicate function.
/**
* Filter values using a predicate function
* @param {Function} predicate - Function that returns truthy for included values
* @returns {Stream} Stream with filtered values
*/
Stream.prototype.filter(predicate);Usage Examples:
// Filter numbers
_([1, 2, 3, 4, 5]).filter(x => x % 2 === 0).toArray(console.log); // [2, 4]
// Filter objects
const users = [{name: 'Alice', active: true}, {name: 'Bob', active: false}];
_(users).filter(user => user.active).toArray(console.log); // [{name: 'Alice', active: true}]The inverse of filter - excludes values that match the predicate.
/**
* Exclude values that match the predicate (inverse of filter)
* @param {Function} predicate - Function that returns truthy for excluded values
* @returns {Stream} Stream with values that don't match predicate
*/
Stream.prototype.reject(predicate);Usage Examples:
// Reject even numbers
_([1, 2, 3, 4, 5]).reject(x => x % 2 === 0).toArray(console.log); // [1, 3, 5]Operations for extracting and manipulating object properties.
/**
* Extract a property from each object in the stream
* @param {string} property - Property name to extract
* @returns {Stream} Stream of property values
*/
Stream.prototype.pluck(property);
/**
* Create new objects with only specified properties
* @param {string[]} properties - Array of property names to keep
* @returns {Stream} Stream of objects with only specified properties
*/
Stream.prototype.pick(properties);
/**
* Create new objects with properties selected by predicate
* @param {Function} predicate - Function to test each property
* @returns {Stream} Stream of objects with selected properties
*/
Stream.prototype.pickBy(predicate);Usage Examples:
const users = [
{name: 'Alice', age: 25, city: 'NYC'},
{name: 'Bob', age: 30, city: 'LA'}
];
// Extract property
_(users).pluck('name').toArray(console.log); // ['Alice', 'Bob']
// Pick specific properties
_(users).pick(['name', 'age']).toArray(console.log);
// [{name: 'Alice', age: 25}, {name: 'Bob', age: 30}]
// Pick by predicate
_(users).pickBy((key, value) => typeof value === 'string').toArray(console.log);
// [{name: 'Alice', city: 'NYC'}, {name: 'Bob', city: 'LA'}]Filter objects based on property matching.
/**
* Filter objects that match specified property values
* @param {Object} properties - Object with property-value pairs to match
* @returns {Stream} Stream of matching objects
*/
Stream.prototype.where(properties);
/**
* Find first object that matches specified property values
* @param {Object} properties - Object with property-value pairs to match
* @returns {Stream} Stream containing first matching object
*/
Stream.prototype.findWhere(properties);Usage Examples:
const products = [
{name: 'Laptop', category: 'electronics', price: 999},
{name: 'Book', category: 'books', price: 29},
{name: 'Phone', category: 'electronics', price: 599}
];
// Find all electronics
_(products).where({category: 'electronics'}).toArray(console.log);
// [{name: 'Laptop', ...}, {name: 'Phone', ...}]
// Find first electronics item
_(products).findWhere({category: 'electronics'}).toArray(console.log);
// [{name: 'Laptop', category: 'electronics', price: 999}]Finding specific values in streams.
/**
* Find first value that matches predicate
* @param {Function} predicate - Test function
* @returns {Stream} Stream containing first matching value
*/
Stream.prototype.find(predicate);Usage Examples:
// Find first even number
_([1, 3, 4, 5, 6]).find(x => x % 2 === 0).toArray(console.log); // [4]Remove falsy values from the stream.
/**
* Remove all falsy values from the stream
* @returns {Stream} Stream with only truthy values
*/
Stream.prototype.compact();
// Top-level version
const _.compact = (stream) => stream.compact();Usage Examples:
// Remove falsy values
_([0, 1, false, 2, '', 3, null, 4, undefined]).compact().toArray(console.log);
// [1, 2, 3, 4]Perform operations without changing the stream values.
/**
* Apply function for side effects, return original value
* @param {Function} fn - Function to call with each value
* @returns {Stream} Stream with original values
*/
Stream.prototype.doto(fn);
/**
* Alias for doto
* @param {Function} fn - Function to call with each value
* @returns {Stream} Stream with original values
*/
Stream.prototype.tap(fn);
// Note: _.tap is available as alias for _.dotoUsage Examples:
// Debug stream values
_([1, 2, 3])
.tap(console.log) // Logs: 1, 2, 3
.map(x => x * 2)
.toArray(console.log); // [2, 4, 6]
// Mutate objects (be careful!)
_([{count: 0}, {count: 1}])
.doto(obj => obj.count++)
.toArray(console.log); // [{count: 1}, {count: 2}]