or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--invert

The lodash methods invert and invertBy exported as a module for creating objects with inverted keys and values.

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

To install, run

npx @tessl/cli install tessl/npm-lodash--invert@4.3.0

index.mddocs/

Lodash Invert

Lodash Invert provides the lodash invert and invertBy methods as standalone, modular utilities for creating objects composed of inverted keys and values. It includes both basic inversion (invert) and advanced grouping inversion (invertBy) functionality. This is a lightweight alternative to importing the entire lodash library when only inversion capabilities are needed.

Package Information

  • Package Name: lodash.invert
  • Package Type: npm
  • Language: JavaScript (ES5)
  • Installation: npm install lodash.invert
  • License: MIT

Core Imports

var invert = require('lodash.invert');
var invertBy = require('lodash.invert').invertBy;

For ES6/ESM environments:

import invert, { invertBy } from 'lodash.invert';

Basic Usage

var invert = require('lodash.invert');
var invertBy = require('lodash.invert').invertBy;

// Basic inversion
var object = { 'a': 1, 'b': 2, 'c': 1 };
var result = invert(object);
console.log(result);
// => { '1': 'c', '2': 'b' }

// Inversion with grouping
var resultBy = invertBy(object);
console.log(resultBy);
// => { '1': ['a', 'c'], '2': ['b'] }

// Real-world example: Creating lookup tables
var statusCodes = { 'ok': 200, 'error': 500, 'notFound': 404 };
var codeToStatus = invert(statusCodes);
console.log(codeToStatus);
// => { '200': 'ok', '500': 'error', '404': 'notFound' }

Capabilities

Object Inversion

Creates an object composed of the inverted keys and values of the input object. When duplicate values exist in the original object, subsequent values overwrite property assignments of previous values.

/**
 * Creates an object composed of the inverted keys and values of object.
 * If object contains duplicate values, subsequent values overwrite property 
 * assignments of previous values.
 * 
 * @param {Object} object The object to invert.
 * @returns {Object} Returns the new inverted object.
 */
function invert(object);

Object Inversion By Iteratee

Creates an object composed of inverted keys and values, but uses an iteratee function to transform values before inversion. The inverted value of each inverted key is an array of keys responsible for generating that inverted value.

/**
 * This method is like invert except that the inverted object is generated
 * from the results of running each element of object through iteratee.
 * The corresponding inverted value of each inverted key is an array of keys
 * responsible for generating the inverted value.
 * 
 * @param {Object} object The object to invert.
 * @param {Function|Object|string} [iteratee=_.identity] The iteratee invoked per element.
 * @returns {Object} Returns the new inverted object.
 */
function invertBy(object, iteratee);

Parameters for invert:

  • object (Object): The object to invert

Returns:

  • (Object): Returns the new inverted object

Parameters for invertBy:

  • object (Object): The object to invert
  • [iteratee=_.identity] (Function|Object|string): The iteratee invoked per element

Returns:

  • (Object): Returns the new inverted object with arrays as values

Behavior:

  • invert: Original object keys become values, original values become keys
  • invert: When duplicate values exist, the last key with that value overwrites previous assignments
  • invertBy: Groups keys by the transformed values, resulting in arrays of keys
  • invertBy: Uses iteratee function to transform values before grouping
  • Values are converted to strings when used as keys
  • Returns a new object (does not mutate the original)
  • Handles non-object inputs gracefully following lodash conventions

Usage Examples:

// Basic inversion
var colors = { red: '#ff0000', blue: '#0000ff', green: '#00ff00' };
invert(colors);
// => { '#ff0000': 'red', '#0000ff': 'blue', '#00ff00': 'green' }

// Duplicate values - last key wins
var ratings = { good: 5, excellent: 5, poor: 1, bad: 1 };
invert(ratings);
// => { '5': 'excellent', '1': 'bad' }

// invertBy with duplicate values - keys grouped in arrays
invertBy(ratings);
// => { '5': ['good', 'excellent'], '1': ['poor', 'bad'] }

// invertBy with iteratee function
var scores = { alice: 95, bob: 87, charlie: 95, dave: 87 };
invertBy(scores, function(value) {
  return 'grade-' + (value >= 90 ? 'A' : 'B');
});
// => { 'grade-A': ['alice', 'charlie'], 'grade-B': ['bob', 'dave'] }

// Mixed value types
var mixed = { a: 1, b: true, c: 'hello' };
invert(mixed);
// => { '1': 'a', 'true': 'b', 'hello': 'c' }

// Empty object
invert({});
// => {}

// Common use case: Creating reverse lookup maps
var userRoles = { admin: 1, editor: 2, viewer: 3 };
var roleIds = invert(userRoles);
// Use roleIds['1'] to get 'admin', etc.