or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--uniq

Creates a duplicate-free version of arrays using SameValueZero for equality comparisons, with optimization support for sorted arrays and custom iteratee functions.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.uniq@3.2.x

To install, run

npx @tessl/cli install tessl/npm-lodash--uniq@3.2.0

index.mddocs/

Lodash Uniq

Lodash Uniq provides array deduplication functionality, creating duplicate-free versions of arrays using SameValueZero for equality comparisons. It retains only the first occurrence of each element and offers optimization for sorted arrays with custom comparison logic through iteratee functions. The function is also available under the alias unique.

Package Information

  • Package Name: lodash.uniq
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.uniq

Core Imports

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

For ES modules (if supported):

import uniq from 'lodash.uniq';

Basic Usage

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

// Basic deduplication
uniq([2, 1, 2]);
// => [2, 1]

// Sorted array optimization
uniq([1, 1, 2], true);
// => [1, 2]

// Custom comparison with iteratee
uniq([1, 2.5, 1.5, 2], function(n) {
  return Math.floor(n);
});
// => [1, 2.5]

// Property-based deduplication
uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]

Capabilities

Array Deduplication

Creates a duplicate-free version of an array, using SameValueZero for equality comparisons, keeping only the first occurrence of each element.

/**
 * Creates a duplicate-free version of an array, using SameValueZero
 * for equality comparisons, in which only the first occurence of each element
 * is kept. Providing `true` for `isSorted` performs a faster search algorithm
 * for sorted arrays. If an iteratee function is provided it's invoked for
 * each element in the array to generate the criterion by which uniqueness
 * is computed. The `iteratee` is bound to `thisArg` and invoked with three
 * arguments: (value, index, array).
 *
 * If a property name is provided for `iteratee` the created `_.property`
 * style callback returns the property value of the given element.
 *
 * If a value is also provided for `thisArg` the created `_.matchesProperty`
 * style callback returns `true` for elements that have a matching property
 * value, else `false`.
 *
 * If an object is provided for `iteratee` the created `_.matches` style
 * callback returns `true` for elements that have the properties of the given
 * object, else `false`.
 *
 * @alias unique
 * @param {Array} array The array to inspect.
 * @param {boolean} [isSorted] Specify the array is sorted.
 * @param {Function|Object|string} [iteratee] The function invoked per iteration.
 * @param {*} [thisArg] The `this` binding of `iteratee`.
 * @returns {Array} Returns the new duplicate-value-free array.
 */
function uniq(array, isSorted, iteratee, thisArg);

// Available as alias
var unique = uniq;

Usage Examples:

Basic Deduplication:

// Remove duplicates from unsorted array
uniq([2, 1, 2]);
// => [2, 1]

uniq([1, 3, 2, 1, 3]);
// => [1, 3, 2]

Sorted Array Optimization:

// Use isSorted flag for performance optimization
uniq([1, 1, 2], true);
// => [1, 2]

uniq([1, 2, 2, 3, 3, 3], true);
// => [1, 2, 3]

Custom Iteratee Functions:

// Custom comparison logic
uniq([1, 2.5, 1.5, 2], function(n) {
  return Math.floor(n);
});
// => [1, 2.5]

// With thisArg binding
uniq([1, 2.5, 1.5, 2], function(n) {
  return this.floor(n);
}, Math);
// => [1, 2.5]

Property Name Shorthand:

// Deduplicate by object property
uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]

uniq([{ name: 'Alice' }, { name: 'Bob' }, { name: 'Alice' }], 'name');
// => [{ name: 'Alice' }, { name: 'Bob' }]

Object Pattern Matching:

// Deduplicate by object pattern match
uniq([
  { 'user': 'barney', 'active': false },
  { 'user': 'fred', 'active': true },
  { 'user': 'pebbles', 'active': false }
], { 'active': false });
// => [{ 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': true }]

Property-Value Matching:

// Deduplicate using property-value pair matching
var users = [
  { 'user': 'barney', 'active': false },
  { 'user': 'fred', 'active': true },
  { 'user': 'pebbles', 'active': false }
];

// Uses _.matchesProperty style callback
uniq(users, 'active', false);
// => [{ 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': true }]

Function Behavior

Parameter Handling

  • Empty/Invalid Arrays: Returns empty array [] when input is null, undefined, or has no length
  • Parameter Normalization: Automatically handles different parameter combinations and types
  • Argument Detection: Uses isIterateeCall to detect when parameters are passed in shorthand form

Equality Comparison

  • SameValueZero: Uses SameValueZero algorithm for consistent equality comparisons
  • First Occurrence: Always retains the first occurrence of duplicate elements
  • Custom Criteria: Supports custom uniqueness criteria through iteratee functions

Performance Optimization

  • Sorted Arrays: Provides sortedUniq optimized algorithm when isSorted is true
  • Iteratee Caching: Caches iteratee results for efficient comparison
  • Callback Styles: Supports multiple callback shorthand styles for flexible usage

Error Handling

  • Graceful Degradation: Returns empty array for invalid inputs rather than throwing errors
  • Type Safety: Handles various parameter types and combinations without breaking
  • Null Safety: Safe handling of null/undefined values in arrays and parameters