or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md
tile.json

index.mddocs/

0

# Lodash Invert

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: lodash.invert

7

- **Package Type**: npm

8

- **Language**: JavaScript (ES5)

9

- **Installation**: `npm install lodash.invert`

10

- **License**: MIT

11

12

## Core Imports

13

14

```javascript

15

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

16

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

17

```

18

19

For ES6/ESM environments:

20

21

```javascript

22

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

23

```

24

25

## Basic Usage

26

27

```javascript

28

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

29

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

30

31

// Basic inversion

32

var object = { 'a': 1, 'b': 2, 'c': 1 };

33

var result = invert(object);

34

console.log(result);

35

// => { '1': 'c', '2': 'b' }

36

37

// Inversion with grouping

38

var resultBy = invertBy(object);

39

console.log(resultBy);

40

// => { '1': ['a', 'c'], '2': ['b'] }

41

42

// Real-world example: Creating lookup tables

43

var statusCodes = { 'ok': 200, 'error': 500, 'notFound': 404 };

44

var codeToStatus = invert(statusCodes);

45

console.log(codeToStatus);

46

// => { '200': 'ok', '500': 'error', '404': 'notFound' }

47

```

48

49

## Capabilities

50

51

### Object Inversion

52

53

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.

54

55

```javascript { .api }

56

/**

57

* Creates an object composed of the inverted keys and values of object.

58

* If object contains duplicate values, subsequent values overwrite property

59

* assignments of previous values.

60

*

61

* @param {Object} object The object to invert.

62

* @returns {Object} Returns the new inverted object.

63

*/

64

function invert(object);

65

```

66

67

### Object Inversion By Iteratee

68

69

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.

70

71

```javascript { .api }

72

/**

73

* This method is like invert except that the inverted object is generated

74

* from the results of running each element of object through iteratee.

75

* The corresponding inverted value of each inverted key is an array of keys

76

* responsible for generating the inverted value.

77

*

78

* @param {Object} object The object to invert.

79

* @param {Function|Object|string} [iteratee=_.identity] The iteratee invoked per element.

80

* @returns {Object} Returns the new inverted object.

81

*/

82

function invertBy(object, iteratee);

83

```

84

85

**Parameters for invert:**

86

- `object` (Object): The object to invert

87

88

**Returns:**

89

- (Object): Returns the new inverted object

90

91

**Parameters for invertBy:**

92

- `object` (Object): The object to invert

93

- `[iteratee=_.identity]` (Function|Object|string): The iteratee invoked per element

94

95

**Returns:**

96

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

97

98

**Behavior:**

99

- **invert**: Original object keys become values, original values become keys

100

- **invert**: When duplicate values exist, the last key with that value overwrites previous assignments

101

- **invertBy**: Groups keys by the transformed values, resulting in arrays of keys

102

- **invertBy**: Uses iteratee function to transform values before grouping

103

- Values are converted to strings when used as keys

104

- Returns a new object (does not mutate the original)

105

- Handles non-object inputs gracefully following lodash conventions

106

107

**Usage Examples:**

108

109

```javascript

110

// Basic inversion

111

var colors = { red: '#ff0000', blue: '#0000ff', green: '#00ff00' };

112

invert(colors);

113

// => { '#ff0000': 'red', '#0000ff': 'blue', '#00ff00': 'green' }

114

115

// Duplicate values - last key wins

116

var ratings = { good: 5, excellent: 5, poor: 1, bad: 1 };

117

invert(ratings);

118

// => { '5': 'excellent', '1': 'bad' }

119

120

// invertBy with duplicate values - keys grouped in arrays

121

invertBy(ratings);

122

// => { '5': ['good', 'excellent'], '1': ['poor', 'bad'] }

123

124

// invertBy with iteratee function

125

var scores = { alice: 95, bob: 87, charlie: 95, dave: 87 };

126

invertBy(scores, function(value) {

127

return 'grade-' + (value >= 90 ? 'A' : 'B');

128

});

129

// => { 'grade-A': ['alice', 'charlie'], 'grade-B': ['bob', 'dave'] }

130

131

// Mixed value types

132

var mixed = { a: 1, b: true, c: 'hello' };

133

invert(mixed);

134

// => { '1': 'a', 'true': 'b', 'hello': 'c' }

135

136

// Empty object

137

invert({});

138

// => {}

139

140

// Common use case: Creating reverse lookup maps

141

var userRoles = { admin: 1, editor: 2, viewer: 3 };

142

var roleIds = invert(userRoles);

143

// Use roleIds['1'] to get 'admin', etc.

144

```