CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-partition

The lodash method partition exported as a module for splitting collections into two groups based on a predicate function

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Lodash Partition

Lodash Partition provides the partition method from the lodash library as a standalone module. It creates an array of elements split into two groups based on the result of running each element through a predicate function. The first group contains elements for which the predicate returns truthy, and the second group contains elements for which the predicate returns falsey.

Package Information

  • Package Name: lodash.partition
  • Package Type: npm
  • Language: JavaScript (CommonJS)
  • Installation: npm install lodash.partition

Core Imports

var partition = require('lodash.partition');

For ES modules (if using a bundler):

import partition from 'lodash.partition';

Basic Usage

var partition = require('lodash.partition');

var users = [
  { 'user': 'barney',  'age': 36, 'active': false },
  { 'user': 'fred',    'age': 40, 'active': true },
  { 'user': 'pebbles', 'age': 1,  'active': false }
];

// Function predicate
partition(users, function(o) { return o.active; });
// => [
//   [{ 'user': 'fred', 'age': 40, 'active': true }],
//   [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]

// Property path shorthand
partition(users, 'active');
// => [
//   [{ 'user': 'fred', 'age': 40, 'active': true }],
//   [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]

Capabilities

Partition Function

Creates an array of elements split into two groups based on the result of running each element through a predicate function.

/**
 * Creates an array of elements split into two groups, the first of which
 * contains elements predicate returns truthy for, the second of which
 * contains elements predicate returns falsey for. The predicate is invoked
 * with one argument: (value).
 *
 * @param {Array|Object} collection The collection to iterate over.
 * @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
 * @returns {Array} Returns the array of grouped elements.
 */
function partition(collection, predicate);

Parameters:

  • collection (Array|Object): The collection to iterate over
  • [predicate=_.identity] (Function|Object|string): The function invoked per iteration

Returns:

  • (Array): Returns the array of grouped elements - first group contains elements for which predicate returns truthy, second group contains elements for which predicate returns falsey

Predicate Formats:

The predicate parameter supports multiple formats for maximum flexibility:

  1. Function: Custom predicate function

    partition(users, function(o) { return o.active; });
  2. Object: The _.matches iteratee shorthand

    partition(users, { 'age': 1, 'active': false });
  3. Array: The _.matchesProperty iteratee shorthand

    partition(users, ['active', false]);
  4. String: The _.property iteratee shorthand

    partition(users, 'active');

Usage Examples:

var partition = require('lodash.partition');

var users = [
  { 'user': 'barney',  'age': 36, 'active': false },
  { 'user': 'fred',    'age': 40, 'active': true },
  { 'user': 'pebbles', 'age': 1,  'active': false }
];

// Using function predicate
var result1 = partition(users, function(o) { return o.active; });
// => [
//   [{ 'user': 'fred', 'age': 40, 'active': true }],
//   [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]

// Using object predicate (matches properties)
var result2 = partition(users, { 'age': 1, 'active': false });
// => [
//   [{ 'user': 'pebbles', 'age': 1, 'active': false }],
//   [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'fred', 'age': 40, 'active': true }]
// ]

// Using array predicate (property-value pair)
var result3 = partition(users, ['active', false]);
// => [
//   [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }],
//   [{ 'user': 'fred', 'age': 40, 'active': true }]
// ]

// Using string predicate (property path)
var result4 = partition(users, 'active');
// => [
//   [{ 'user': 'fred', 'age': 40, 'active': true }],
//   [{ 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': false }]
// ]

// Working with arrays
var numbers = [1, 2, 3, 4, 5, 6];
var evenOdd = partition(numbers, function(n) { return n % 2 === 0; });
// => [[2, 4, 6], [1, 3, 5]]

// Working with objects
var obj = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 };
var objPartition = partition(obj, function(value) { return value > 2; });
// => [[3, 4], [1, 2]]

Implementation Notes

  • Zero Dependencies: The package is completely self-contained with no external dependencies
  • Collection Support: Works with both arrays and objects
  • Immutable: Does not modify the original collection
  • Performance: Uses optimized internal utilities for efficient iteration
  • Compatibility: Follows lodash v4.6.0 conventions and behavior
  • Module Format: CommonJS export (module.exports = partition)

docs

index.md

tile.json