Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "utils/object-inverse-by/docs/types/index.d"

Index

Interfaces

Type aliases

Functions

Type aliases

Binary

Binary: (key: string, value: any) => string

Returns a value for an object element that can be serialized as an object key.

param

object key

param

object value corresponding to key

returns

key in inverted object

Type declaration

    • (key: string, value: any): string
    • Parameters

      • key: string
      • value: any

      Returns string

Nullary

Nullary: () => string

Returns a value for an object element that can be serialized as an object key.

returns

key in inverted object

Type declaration

    • (): string
    • Returns string

Ternary

Ternary: (key: string, value: any, obj: any) => string

Returns a value for an object element that can be serialized as an object key.

param

object key

param

object value corresponding to key

param

the input object

returns

key in inverted object

Type declaration

    • (key: string, value: any, obj: any): string
    • Parameters

      • key: string
      • value: any
      • obj: any

      Returns string

Transform

Transform: Nullary | Unary | Binary | Ternary

Returns a value for an object element that can be serialized as an object key.

param

object key

param

object value corresponding to key

param

the input object

returns

key in inverted object

Unary

Unary: (key: string) => string

Returns a value for an object element that can be serialized as an object key.

param

object key

returns

key in inverted object

Type declaration

    • (key: string): string
    • Parameters

      • key: string

      Returns string

Functions

Export assignment invertBy

  • Inverts an object, such that keys become values and values become keys, according to a transform function.

    Notes

    • The transform function is provided three arguments:

      • key: object key
      • value: object value corresponding to key
      • obj: the input object
    • The value returned by a transform function should be a value which can be serialized as an object key. Hence, beware when providing objects having values which are themselves objects. The function relies on native object serialization (#toString) when converting transform function return values to keys.

    • Insertion order is not guaranteed, as object key enumeration is not specified according to the ECMAScript specification. In practice, however, most engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.

    Parameters

    • obj: any

      input object

    • transform: Transform

      transform function

    Returns any

    inverted object

    Example

    function transform( key, value ) {
        return key + value;
    }
    var obj = {
        'a': 'beep',
        'b': 'boop'
    };
    var out = invertBy( obj, transform );
    // returns { 'abeep': 'a', 'bboop': 'b' }

    Example

    function transform( key, value ) {
        return value;
    }
    var obj = {
        'a': 'beep',
        'b': 'beep'
    };
    var out = invertBy( obj, transform );
    // returns { 'beep': [ 'a', 'b' ] }
  • Inverts an object, such that keys become values and values become keys, according to a transform function.

    Notes

    • The transform function is provided three arguments:

      • key: object key
      • value: object value corresponding to key
      • obj: the input object
    • The value returned by a transform function should be a value which can be serialized as an object key. Hence, beware when providing objects having values which are themselves objects. The function relies on native object serialization (#toString) when converting transform function return values to keys.

    • Insertion order is not guaranteed, as object key enumeration is not specified according to the ECMAScript specification. In practice, however, most engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.

    Parameters

    • obj: any

      input object

    • options: Options
    • transform: Transform

      transform function

    Returns any

    inverted object

    Example

    function transform( key, value ) {
        return value;
    }
    
    var obj = {};
    obj.a = 'beep';
    obj.b = 'boop';
    obj.c = 'beep'; // inserted after `a`
    
    var opts = {
        'duplicates': false
    };
    var out = invertBy( obj, opts, transform );
    // returns { 'beep': 'c', 'boop': 'b' }