Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions lib/node_modules/@stdlib/utils/object-inverse-by/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var invertBy = require( '@stdlib/utils/object-inverse-by' );

#### invertBy( obj, \[options,] transform )

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

```javascript
function transform( key, value ) {
Expand All @@ -46,11 +46,11 @@ var out = invertBy( obj, transform );
// returns { 'beep': 'a', 'boop': 'b' }
```

The function accepts the following `options`:
The function accepts the following options:

- **duplicates**: `boolean` indicating whether to store keys mapped to duplicate values in `arrays`. Default: `true`.
- **duplicates**: boolean indicating whether to store keys mapped to duplicate values in arrays. Default: `true`.

By default, keys mapped to duplicate values are stored in `arrays`.
By default, keys mapped to duplicate values are stored in arrays.

```javascript
function transform( key, value ) {
Expand All @@ -64,7 +64,7 @@ var out = invertBy( obj, transform );
// returns { 'beep': [ 'a', 'b' ] }
```

To **not** allow duplicates, set the `duplicates` option to `false`. The output `key-value` pair will be the `key` most recently inserted into the input `object`.
To **not** allow duplicates, set the `duplicates` option to `false`. The output key-value pair will be the key most recently inserted into the input object.

```javascript
function transform( key, value ) {
Expand All @@ -82,7 +82,7 @@ var out = invertBy( obj, opts, transform );
// returns { 'beep': 'c', 'boop': 'b' }
```

The `transform` function is provided three arguments:
The transform function is provided three arguments:

- **key**: object key.
- **value**: object value corresponding to `key`.
Expand Down Expand Up @@ -112,7 +112,7 @@ var out = invertBy( obj, transform );

## Notes

- Beware when providing `objects` having values which are themselves `objects`. This function relies on native `object` serialization (`#toString`) when converting `transform` function return values to keys.
- Beware when providing objects having values which are themselves objects. This function relies on native object serialization (`#toString`) when converting transform function return values to keys.

```javascript
function transform( key, value ) {
Expand All @@ -129,7 +129,7 @@ var out = invertBy( obj, transform );
// returns { '1,2,3': 'a', '[object Object]': 'b' }
```

- Insertion order is not guaranteed, as `object` key enumeration is not specified according to the [ECMAScript specification][ecma-262-for-in]. In practice, however, most engines use insertion order to sort an `object`'s keys, thus allowing for deterministic inversion.
- In older JavaScript engines, insertion order is not guaranteed, as object key enumeration was not specified according to the [ECMAScript specification][ecma-262-for-in] in earlier editions. In practice, however, most older engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.

</section>

Expand All @@ -142,27 +142,24 @@ var out = invertBy( obj, transform );
<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var objectKeys = require( '@stdlib/utils/keys' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var invertBy = require( '@stdlib/utils/object-inverse-by' );

var keys;
var arr;
var out;
var i;

function transform( key, value ) {
return value;
}

// Create an array of random integers...
arr = new Array( 1000 );
for ( i = 0; i < arr.length; i++ ) {
arr[ i ] = round( randu()*100.0 );
}
// Create an array of random integers:
var arr = discreteUniform( 1000, 0, 100, {
'dtype': 'generic'
});

// Invert the array to determine value frequency...
out = invertBy( arr, transform );
keys = Object.keys( out );
var out = invertBy( arr, transform );
var keys = objectKeys( out );

var i;
for ( i = 0; i < keys.length; i++ ) {
if ( out[ i ] ) {
out[ i ] = out[ i ].length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
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.
In older JavaScript engines, insertion order is not guaranteed, as object
key enumeration was not specified according to the ECMAScript specification
in earlier editions. In practice, however, most older engines use insertion
order to sort an object's keys, thus allowing for deterministic inversion.

Parameters
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* @param value - object value corresponding to `key`
* @returns key in inverted object
*/
type Binary = ( key: string, value: any ) => string;

Check warning on line 53 in lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Returns a value for an object element that can be serialized as an object key.
Expand All @@ -60,7 +60,7 @@
* @param obj - the input object
* @returns key in inverted object
*/
type Ternary = ( key: string, value: any, obj: any ) => string;

Check warning on line 63 in lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 63 in lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Returns a value for an object element that can be serialized as an object key.
Expand All @@ -79,13 +79,13 @@
*
* - The transform function is provided three arguments:
*
* - `key`: object key
* - `value`: object value corresponding to `key`
* - `obj`: the input object
* - `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.
* - In older JavaScript engines, insertion order is not guaranteed, as object key enumeration was not specified according to the ECMAScript specification in earlier editions. In practice, however, most older engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.
*
* @param obj - input object
* @param transform - transform function
Expand Down Expand Up @@ -113,7 +113,7 @@
* var out = invertBy( obj, transform );
* // returns { 'beep': [ 'a', 'b' ] }
*/
declare function invertBy( obj: any, transform: Transform ): any;

Check warning on line 116 in lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 116 in lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Inverts an object, such that keys become values and values become keys, according to a transform function.
Expand All @@ -122,13 +122,13 @@
*
* - The transform function is provided three arguments:
*
* - `key`: object key
* - `value`: object value corresponding to `key`
* - `obj`: the input object
* - `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.
* - In older JavaScript engines, insertion order is not guaranteed, as object key enumeration was not specified according to the ECMAScript specification in earlier editions. In practice, however, most older engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.
*
* @param obj - input object
* @param opts - function options
Expand All @@ -152,7 +152,7 @@
* var out = invertBy( obj, opts, transform );
* // returns { 'beep': 'c', 'boop': 'b' }
*/
declare function invertBy( obj: any, opts: Options, transform: Transform ): any;

Check warning on line 155 in lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 155 in lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
24 changes: 10 additions & 14 deletions lib/node_modules/@stdlib/utils/object-inverse-by/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,23 @@
'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var invertBy = require( './../lib' );

var keys;
var arr;
var out;
var i;

function transform( key, value ) {
return value;
}

// Create an array of random integers...
arr = new Array( 1000 );
for ( i = 0; i < arr.length; i++ ) {
arr[ i ] = round( randu()*100.0 );
}
// Create an array of random integers:
var arr = discreteUniform( 1000, 0, 100, {
'dtype': 'generic'
});

// Invert the array to determine value frequency...
out = invertBy( arr, transform );
keys = objectKeys( out );
var out = invertBy( arr, transform );
var keys = objectKeys( out );

var i;
for ( i = 0; i < keys.length; i++ ) {
if ( out[ i ] ) {
out[ i ] = out[ i ].length;
Expand Down