Comprehensive functional programming utilities for arrays, objects, and sets with map, filter, reduce, and transformation operations.
Functional programming utilities specifically designed for array operations.
const compactArray = require('fbjs/lib/compactArray');
const concatAllArray = require('fbjs/lib/concatAllArray');
const distinctArray = require('fbjs/lib/distinctArray');
const flatMapArray = require('fbjs/lib/flatMapArray');
const groupArray = require('fbjs/lib/groupArray');
const partitionArray = require('fbjs/lib/partitionArray');
/**
* Removes null and undefined elements from array
* @param array - Array potentially containing null/undefined values
* @returns Array with null/undefined elements removed
*/
function compactArray<T>(array: Array<?T>): Array<T>;
/**
* Concatenates an array of arrays into a single array
* @param array - Array containing arrays to concatenate
* @returns Single flattened array
*/
function concatAllArray<T>(array: Array<Array<T>>): Array<T>;
/**
* Returns array with unique elements (removes duplicates)
* @param xs - Array with potential duplicates
* @returns Array with unique elements only
*/
function distinctArray<T>(xs: Array<T>): Array<T>;
/**
* Maps array elements then flattens the result
* @param array - Input array
* @param fn - Mapping function that returns array
* @returns Flattened array of mapped results
*/
function flatMapArray<T, U>(array: Array<T>, fn: (item: T) => Array<U>): Array<U>;
/**
* Groups array elements by key function result
* @param array - Input array to group
* @param fn - Function that returns grouping key
* @returns Object with keys mapping to arrays of grouped elements
*/
function groupArray<T>(array: Array<T>, fn: (item: T) => string): {[key: string]: Array<T>};
/**
* Splits array into two arrays based on predicate
* @param array - Input array to partition
* @param predicate - Function that returns boolean for partitioning
* @returns Tuple of [passing elements, failing elements]
*/
function partitionArray<T>(array: Array<T>, predicate: (item: T) => boolean): [Array<T>, Array<T>];Usage Examples:
const compactArray = require('fbjs/lib/compactArray');
const groupArray = require('fbjs/lib/groupArray');
const partitionArray = require('fbjs/lib/partitionArray');
// Remove null/undefined values
const sparse = [1, null, 2, undefined, 3, null];
const compact = compactArray(sparse); // [1, 2, 3]
// Group by property
const users = [
{ name: 'Alice', role: 'admin' },
{ name: 'Bob', role: 'user' },
{ name: 'Charlie', role: 'admin' }
];
const byRole = groupArray(users, user => user.role);
// { admin: [{name: 'Alice', role: 'admin'}, {name: 'Charlie', role: 'admin'}], user: [{name: 'Bob', role: 'user'}] }
// Partition by condition
const numbers = [1, 2, 3, 4, 5, 6];
const [evens, odds] = partitionArray(numbers, n => n % 2 === 0);
// evens: [2, 4, 6], odds: [1, 3, 5]Functional programming utilities for object manipulation and transformation.
const everyObject = require('fbjs/lib/everyObject');
const filterObject = require('fbjs/lib/filterObject');
const forEachObject = require('fbjs/lib/forEachObject');
const mapObject = require('fbjs/lib/mapObject');
const partitionObject = require('fbjs/lib/partitionObject');
const partitionObjectByKey = require('fbjs/lib/partitionObjectByKey');
const someObject = require('fbjs/lib/someObject');
/**
* Tests whether all object values pass the predicate test
* @param object - Object to test
* @param predicate - Test function for values
* @param context - Optional context for predicate function
* @returns True if all values pass the test
*/
function everyObject<T>(
object: {[key: string]: T},
predicate: (value: T, key: string) => boolean,
context?: any
): boolean;
/**
* Filters object properties based on predicate
* @param object - Object to filter
* @param predicate - Test function for values
* @param context - Optional context for predicate function
* @returns New object with filtered properties
*/
function filterObject<T>(
object: {[key: string]: T},
predicate: (value: T, key: string) => boolean,
context?: any
): {[key: string]: T};
/**
* Iterates over object properties
* @param object - Object to iterate over
* @param callback - Function called for each property
* @param context - Optional context for callback function
*/
function forEachObject<T>(
object: {[key: string]: T},
callback: (value: T, key: string) => void,
context?: any
): void;
/**
* Creates new object with transformed values
* Executes callback once for each enumerable own property and constructs a new object from the results
* Returns null if input object is null or undefined
* @param object - Object to transform (can be null)
* @param callback - Transformation function called with (value, key, object)
* @param context - Context for callback function (this binding)
* @returns New object with transformed values, or null if input is null
*/
function mapObject(object: ?Object, callback: Function, context: any): ?Object;
/**
* Splits object into two objects based on value predicate
* @param object - Object to partition
* @param predicate - Test function for values
* @param context - Optional context for predicate function
* @returns Tuple of [passing properties, failing properties]
*/
function partitionObject<T>(
object: {[key: string]: T},
predicate: (value: T, key: string) => boolean,
context?: any
): [{[key: string]: T}, {[key: string]: T}];
/**
* Splits object into two objects based on key predicate
* @param object - Object to partition
* @param predicate - Test function for keys
* @param context - Optional context for predicate function
* @returns Tuple of [passing properties, failing properties]
*/
function partitionObjectByKey<T>(
object: {[key: string]: T},
predicate: (key: string, value: T) => boolean,
context?: any
): [{[key: string]: T}, {[key: string]: T}];
/**
* Tests whether any object value passes the predicate test
* @param object - Object to test
* @param predicate - Test function for values
* @param context - Optional context for predicate function
* @returns True if any value passes the test
*/
function someObject<T>(
object: {[key: string]: T},
predicate: (value: T, key: string) => boolean,
context?: any
): boolean;Usage Examples:
const mapObject = require('fbjs/lib/mapObject');
const filterObject = require('fbjs/lib/filterObject');
const everyObject = require('fbjs/lib/everyObject');
// Transform object values
const numbers = { a: 1, b: 2, c: 3 };
const doubled = mapObject(numbers, x => x * 2); // { a: 2, b: 4, c: 6 }
// Handle null inputs gracefully - returns null
const nullInput = mapObject(null, x => x * 2); // null
const undefinedInput = mapObject(undefined, x => x * 2); // null
// Safe transformation with error handling
function safeMapObject(obj, transform, defaultValue = {}) {
try {
const result = mapObject(obj, transform);
return result || defaultValue;
} catch (error) {
console.warn('mapObject failed:', error.message);
return defaultValue;
}
}
// Context binding example
const context = { multiplier: 3 };
const contextNumbers = { x: 5, y: 10 };
const tripled = mapObject(contextNumbers, function(value, key, obj) {
// 'this' refers to context object
return value * this.multiplier;
}, context); // { x: 15, y: 30 }
// Filter object properties
const users = {
alice: { active: true, role: 'admin' },
bob: { active: false, role: 'user' },
charlie: { active: true, role: 'user' }
};
const activeUsers = filterObject(users, user => user.active);
// { alice: { active: true, role: 'admin' }, charlie: { active: true, role: 'user' } }
// Test all values
const allActive = everyObject(activeUsers, user => user.active); // trueFunctional programming utilities for Set operations.
const equalsSet = require('fbjs/lib/equalsSet');
const everySet = require('fbjs/lib/everySet');
const someSet = require('fbjs/lib/someSet');
/**
* Compares two sets for equality
* @param set1 - First set to compare
* @param set2 - Second set to compare
* @returns True if sets contain the same elements
*/
function equalsSet<T>(set1: Set<T>, set2: Set<T>): boolean;
/**
* Tests whether all set elements pass the predicate test
* @param set - Set to test
* @param predicate - Test function for elements
* @param context - Optional context for predicate function
* @returns True if all elements pass the test
*/
function everySet<T>(
set: Set<T>,
predicate: (value: T) => boolean,
context?: any
): boolean;
/**
* Tests whether any set element passes the predicate test
* @param set - Set to test
* @param predicate - Test function for elements
* @param context - Optional context for predicate function
* @returns True if any element passes the test
*/
function someSet<T>(
set: Set<T>,
predicate: (value: T) => boolean,
context?: any
): boolean;Usage Examples:
const equalsSet = require('fbjs/lib/equalsSet');
const everySet = require('fbjs/lib/everySet');
// Compare sets
const set1 = new Set([1, 2, 3]);
const set2 = new Set([3, 2, 1]);
const areEqual = equalsSet(set1, set2); // true
// Test all elements
const numbers = new Set([2, 4, 6, 8]);
const allEven = everySet(numbers, n => n % 2 === 0); // trueUtilities for comparing iterables and finding extremes.
const equalsIterable = require('fbjs/lib/equalsIterable');
const countDistinct = require('fbjs/lib/countDistinct');
const maxBy = require('fbjs/lib/maxBy');
const minBy = require('fbjs/lib/minBy');
/**
* Compares two iterables for equality
* @param iter1 - First iterable to compare
* @param iter2 - Second iterable to compare
* @param areEqual - Optional equality function (defaults to ===)
* @returns True if iterables contain equal elements in same order
*/
function equalsIterable<T>(
iter1: Iterable<T>,
iter2: Iterable<T>,
areEqual?: (a: T, b: T) => boolean
): boolean;
/**
* Counts unique elements in iterable
* @param iterable - Iterable to count unique elements in
* @returns Number of unique elements
*/
function countDistinct<T>(iterable: Iterable<T>): number;
/**
* Finds element with maximum score from scoring function
* @param iterable - Iterable to search
* @param fn - Scoring function
* @returns Element with highest score or undefined if empty
*/
function maxBy<T>(iterable: Iterable<T>, fn: (item: T) => number): ?T;
/**
* Finds element with minimum score from scoring function
* @param iterable - Iterable to search
* @param fn - Scoring function
* @returns Element with lowest score or undefined if empty
*/
function minBy<T>(iterable: Iterable<T>, fn: (item: T) => number): ?T;Usage Examples:
const maxBy = require('fbjs/lib/maxBy');
const minBy = require('fbjs/lib/minBy');
const countDistinct = require('fbjs/lib/countDistinct');
// Find extremes by property
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 22 }
];
const oldest = maxBy(users, user => user.age); // { name: 'Bob', age: 30 }
const youngest = minBy(users, user => user.age); // { name: 'Charlie', age: 22 }
// Count unique elements
const numbers = [1, 2, 2, 3, 3, 3, 4];
const unique = countDistinct(numbers); // 4