From e758b76373cf2847f6b670d38acc07bf8f70a732 Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sun, 28 Dec 2025 13:39:15 +0530 Subject: [PATCH 1/4] feat: add `object/inverse-by` Ref: https://github.com/stdlib-js/stdlib/issues/8755 --- .../@stdlib/object/inverse-by/README.md | 205 ++++++++++ .../object/inverse-by/benchmark/benchmark.js | 130 +++++++ .../@stdlib/object/inverse-by/docs/repl.txt | 67 ++++ .../object/inverse-by/docs/types/index.d.ts | 160 ++++++++ .../object/inverse-by/docs/types/test.ts | 81 ++++ .../object/inverse-by/examples/index.js | 46 +++ .../@stdlib/object/inverse-by/lib/index.js | 78 ++++ .../@stdlib/object/inverse-by/lib/main.js | 148 ++++++++ .../@stdlib/object/inverse-by/package.json | 75 ++++ .../@stdlib/object/inverse-by/test/test.js | 359 ++++++++++++++++++ 10 files changed, 1349 insertions(+) create mode 100644 lib/node_modules/@stdlib/object/inverse-by/README.md create mode 100644 lib/node_modules/@stdlib/object/inverse-by/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/object/inverse-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/object/inverse-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/object/inverse-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/object/inverse-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/object/inverse-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/object/inverse-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/object/inverse-by/package.json create mode 100644 lib/node_modules/@stdlib/object/inverse-by/test/test.js diff --git a/lib/node_modules/@stdlib/object/inverse-by/README.md b/lib/node_modules/@stdlib/object/inverse-by/README.md new file mode 100644 index 000000000000..0f4c3b91bd37 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/README.md @@ -0,0 +1,205 @@ + + +# Object Inverse + +> Invert an object, such that keys become values and values become keys, according to a transform function. + +
+ +## Usage + +```javascript +var invertBy = require( '@stdlib/object/inverse-by' ); +``` + +#### invertBy( obj, \[options,] transform ) + +Inverts an object, such that keys become values and values become keys, according to a transform function. + +```javascript +function transform( key, value ) { + return value; +} +var obj = { + 'a': 'beep', + 'b': 'boop' +}; +var out = invertBy( obj, transform ); +// returns { 'beep': 'a', 'boop': 'b' } +``` + +The function accepts the following options: + +- **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. + +```javascript +function transform( key, value ) { + return value; +} +var obj = { + 'a': 'beep', + 'b': 'beep' +}; +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. + +```javascript +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' } +``` + +The transform function is provided three arguments: + +- **key**: object key. +- **value**: object value corresponding to `key`. +- **obj**: input object. + +```javascript +function transform( key, value, o ) { + if ( key === 'name' ) { + return value; + } + return o.name + ':' + value; +} +var obj = { + 'name': 'foo', + 'a': 'beep', + 'b': 'boop' +}; +var out = invertBy( obj, transform ); +// returns { 'foo': 'name', 'foo:beep': 'a', 'foo:boop': 'b' } +``` + +
+ + + +
+ +## 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. + + ```javascript + function transform( key, value ) { + return value; + } + var obj = { + 'a': [ 1, 2, 3 ], + 'b': { + 'c': 'd' + } + }; + + var out = invertBy( obj, transform ); + // returns { '1,2,3': 'a', '[object Object]': 'b' } + ``` + +- 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. + +
+ + + +
+ +## Examples + + + +```javascript +var objectKeys = require( '@stdlib/utils/keys' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var invertBy = require( '@stdlib/object/inverse-by' ); + +function transform( key, value ) { + return value; +} + +// Create an array of random integers: +var arr = discreteUniform( 1000, 0, 100, { + 'dtype': 'generic' +}); + +// Invert the array to determine value frequency... +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; + } else { + out[ i ] = 0; + } +} +console.dir( out ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/object/inverse-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/object/inverse-by/benchmark/benchmark.js new file mode 100644 index 000000000000..dc41e2a23221 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/benchmark/benchmark.js @@ -0,0 +1,130 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var invertBy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var obj; + var out; + var i; + + function transform( key, value ) { + return key + ':' + value; + } + obj = { + 'a': 'beep', + 'b': 'boop', + 'c': 'foo', + 'd': 'bar', + 'e': randu() + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.e = randu(); + out = invertBy( obj, transform ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':duplicates=true', function benchmark( b ) { + var opts; + var obj; + var out; + var i; + + function transform( key, value ) { + return value + 'boop'; + } + obj = { + 'a': 'beep', + 'b': 'beep', + 'c': 'beep', + 'd': 'beep', + 'e': randu() + }; + opts = { + 'duplicates': true + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.e = randu(); + out = invertBy( obj, opts, transform ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':duplicates=false', function benchmark( b ) { + var opts; + var obj; + var out; + var i; + + function transform( key, value ) { + return value + 'boop'; + } + obj = { + 'a': 'beep', + 'b': 'beep', + 'c': 'beep', + 'd': 'beep', + 'e': randu() + }; + opts = { + 'duplicates': false + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.e = randu(); + out = invertBy( obj, opts, transform ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/object/inverse-by/docs/repl.txt b/lib/node_modules/@stdlib/object/inverse-by/docs/repl.txt new file mode 100644 index 000000000000..0cf84af38505 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/docs/repl.txt @@ -0,0 +1,67 @@ + +{{alias}}( obj, [options,] transform ) + Inverts an object, such that keys become values and values become keys, + according to a transform function. + + 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. + + 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 + ---------- + obj: ObjectLike + Input object. + + options: Object (optional) + Options. + + options.duplicates: boolean (optional) + Boolean indicating whether to store keys mapped to duplicate values in + arrays. Default: `true`. + + transform: Function + Transform function. + + Returns + ------- + out: Object + Inverted object. + + Examples + -------- + // Basic usage: + > function transform( key, value ) { return key + value; }; + > var obj = { 'a': 'beep', 'b': 'boop' }; + > var out = {{alias}}( obj, transform ) + { 'abeep': 'a', 'bboop': 'b' } + + // Duplicate values: + > function transform( key, value ) { return value; }; + > obj = { 'a': 'beep', 'b': 'beep' }; + > out = {{alias}}( obj, transform ) + { 'beep': [ 'a', 'b' ] } + + // Override duplicate values: + > obj = {}; + > obj.a = 'beep'; + > obj.b = 'boop'; + > obj.c = 'beep'; + > out = {{alias}}( obj, { 'duplicates': false }, transform ) + { 'beep': 'c', 'boop': 'b' } + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/object/inverse-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/object/inverse-by/docs/types/index.d.ts new file mode 100644 index 000000000000..942ca87be454 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/docs/types/index.d.ts @@ -0,0 +1,160 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Boolean indicating whether to store duplicate keys. + */ + duplicates?: boolean; +} + +/** +* Returns a value for an object element that can be serialized as an object key. +* +* @returns key in inverted object +*/ +type Nullary = () => string; + +/** +* Returns a value for an object element that can be serialized as an object key. +* +* @param key - object key +* @returns key in inverted object +*/ +type Unary = ( key: string ) => string; + +/** +* Returns a value for an object element that can be serialized as an object key. +* +* @param key - object key +* @param value - object value corresponding to `key` +* @returns key in inverted object +*/ +type Binary = ( key: string, value: any ) => string; + +/** +* Returns a value for an object element that can be serialized as an object key. +* +* @param key - object key +* @param value - object value corresponding to `key` +* @param obj - the input object +* @returns key in inverted object +*/ +type Ternary = ( key: string, value: any, obj: any ) => string; + +/** +* Returns a value for an object element that can be serialized as an object key. +* +* @param key - object key +* @param value - object value corresponding to `key` +* @param obj - the input object +* @returns key in inverted object +*/ +type Transform = Nullary | Unary | Binary | Ternary; + +/** +* 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. +* +* - 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 +* @returns 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' ] } +*/ +declare function invertBy( obj: any, transform: Transform ): any; + +/** +* 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. +* +* - 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 +* @param opts.duplicates - boolean indicating whether to store duplicate keys (default: true) +* @param transform - transform function +* @returns 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' } +*/ +declare function invertBy( obj: any, opts: Options, transform: Transform ): any; + + +// EXPORTS // + +export = invertBy; diff --git a/lib/node_modules/@stdlib/object/inverse-by/docs/types/test.ts b/lib/node_modules/@stdlib/object/inverse-by/docs/types/test.ts new file mode 100644 index 000000000000..e3c9bdf361cf --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/docs/types/test.ts @@ -0,0 +1,81 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import invertBy = require( './index' ); + +const transform = ( key: string, value: string ): string => key + value; + + +// TESTS // + +// The function returns an object... +{ + const obj = { + 'a': 'beep', + 'b': 'boop' + }; + const opts = { + 'duplicates': false + }; + invertBy( obj, ( key: string, value: string ) => key + value ); // $ExpectType any + invertBy( obj, opts, ( key: string, value: string ) => key + value ); // $ExpectType any +} + +// The compiler throws an error if the function is provided a last argument which is not a function... +{ + const obj = { + 'a': 'beep', + 'b': 'boop' + }; + invertBy( obj, false ); // $ExpectError + invertBy( obj, true ); // $ExpectError + invertBy( obj, 32 ); // $ExpectError + invertBy( obj, 'abc' ); // $ExpectError + invertBy( obj, [] ); // $ExpectError + invertBy( obj, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const obj = { + 'a': 'beep', + 'b': 'boop' + }; + invertBy( obj, null, transform ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `duplicates` option which is not a boolean... +{ + const obj = { + 'a': 'beep', + 'b': 'boop' + }; + invertBy( obj, { 'duplicates': '5' }, transform ); // $ExpectError + invertBy( obj, { 'duplicates': 123 }, transform ); // $ExpectError + invertBy( obj, { 'duplicates': null }, transform ); // $ExpectError + invertBy( obj, { 'duplicates': [] }, transform ); // $ExpectError + invertBy( obj, { 'duplicates': {} }, transform ); // $ExpectError + invertBy( obj, { 'duplicates': ( x: number ): number => x }, transform ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + invertBy(); // $ExpectError + invertBy( {} ); // $ExpectError + invertBy( {}, {}, transform, 16 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/object/inverse-by/examples/index.js b/lib/node_modules/@stdlib/object/inverse-by/examples/index.js new file mode 100644 index 000000000000..b71ddc803884 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/examples/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var objectKeys = require( '@stdlib/utils/keys' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var invertBy = require( './../lib' ); + +function transform( key, value ) { + return value; +} + +// Create an array of random integers: +var arr = discreteUniform( 1000, 0, 100, { + 'dtype': 'generic' +}); + +// Invert the array to determine value frequency... +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; + } else { + out[ i ] = 0; + } +} +console.dir( out ); diff --git a/lib/node_modules/@stdlib/object/inverse-by/lib/index.js b/lib/node_modules/@stdlib/object/inverse-by/lib/index.js new file mode 100644 index 000000000000..d6130fbdec32 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/lib/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Invert an object, such that keys become values and values become keys, according to a transform function. +* +* @module @stdlib/object/inverse-by +* +* @example +* var invertBy = require( '@stdlib/object/inverse-by' ); +* +* function transform( key, value ) { +* return key + value; +* } +* var obj = { +* 'a': 'beep', +* 'b': 'boop' +* }; +* var out = invertBy( obj, transform ); +* // returns { 'abeep': 'a', 'bboop': 'b' } +* +* @example +* var invertBy = require( '@stdlib/object/inverse-by' ); +* +* function transform( key, value ) { +* return value; +* } +* var obj = { +* 'a': 'beep', +* 'b': 'beep' +* }; +* var out = invertBy( obj, transform ); +* // returns { 'beep': [ 'a', 'b' ] } +* +* @example +* var invertBy = require( '@stdlib/object/inverse-by' ); +* +* 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' } +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/object/inverse-by/lib/main.js b/lib/node_modules/@stdlib/object/inverse-by/lib/main.js new file mode 100644 index 000000000000..a088454db521 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/lib/main.js @@ -0,0 +1,148 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var objectKeys = require( '@stdlib/utils/keys' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var isObjectLike = require( '@stdlib/assert/is-object-like' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isFunction = require( '@stdlib/assert/is-function' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Inverts an object, such that keys become values and values become keys, according to a transform function. +* +* @param {ObjectLike} obj - input object +* @param {Options} [opts] - function options +* @param {boolean} [opts.duplicates=true] - boolean indicating whether to store duplicate keys +* @param {Function} transform - transform function +* @throws {TypeError} first argument must be object-like +* @throws {TypeError} options argument must an an object +* @throws {TypeError} last argument must be a function +* @throws {TypeError} must provide valid options +* @returns {Object} 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' ] } +* +* @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' } +*/ +function invertBy( obj, opts, transform ) { + var allowDupes; + var keys; + var len; + var key; + var val; + var out; + var cb; + var v; + var i; + if ( !isObjectLike( obj ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an object (except null). Value: `%s`.', obj ) ); + } + allowDupes = true; + if ( arguments.length === 2 ) { + cb = opts; + } else { + if ( !isObject( opts ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) ); + } + if ( hasOwnProp( opts, 'duplicates' ) ) { + allowDupes = opts.duplicates; + if ( !isBoolean( allowDupes ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'duplicates', allowDupes ) ); + } + } + cb = transform; + } + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', cb ) ); + } + keys = objectKeys( obj ); + len = keys.length; + out = {}; + if ( allowDupes ) { + for ( i = 0; i < len; i++ ) { + key = keys[ i ]; + val = cb( key, obj[ key ], obj ); + if ( !hasOwnProp( out, val ) ) { + out[ val ] = key; + continue; + } + v = out[ val ]; + if ( isArray( v ) ) { + out[ val ].push( key ); + } else { + out[ val ] = [ v, key ]; + } + } + } else { + for ( i = 0; i < len; i++ ) { + key = keys[ i ]; + val = cb( key, obj[ key ], obj ); + out[ val ] = key; + } + } + return out; +} + + +// EXPORTS // + +module.exports = invertBy; diff --git a/lib/node_modules/@stdlib/object/inverse-by/package.json b/lib/node_modules/@stdlib/object/inverse-by/package.json new file mode 100644 index 000000000000..1ad27bd7282c --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/object/inverse-by", + "version": "0.0.0", + "description": "Invert an object, such that keys become values and values become keys, according to a transform function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "inverse", + "invert", + "invertby", + "inverseby", + "transform", + "convert", + "object", + "obj", + "object-like", + "keys", + "values", + "bijection", + "mapping", + "hash", + "array" + ] +} diff --git a/lib/node_modules/@stdlib/object/inverse-by/test/test.js b/lib/node_modules/@stdlib/object/inverse-by/test/test.js new file mode 100644 index 000000000000..dd2ef847d11c --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse-by/test/test.js @@ -0,0 +1,359 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var noop = require( '@stdlib/utils/noop' ); +var invertBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof invertBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided an object-like value', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + invertBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if provided a last argument which is not a function (no options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + invertBy( {}, value ); + }; + } +}); + +tape( 'the function throws an error if provided a last argument which is not a function (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + invertBy( {}, {}, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + invertBy( {}, value, noop ); + }; + } +}); + +tape( 'the function throws an error if provided a duplicates option which is not a boolean primitive', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'duplicates': value + }; + invertBy( {}, opts, noop ); + }; + } +}); + +tape( 'the function returns an empty object if provided an empty object', function test( t ) { + var out; + + function transform( key, value ) { + return value; + } + out = invertBy( {}, transform ); + t.deepEqual( out, {}, 'returns empty object' ); + t.end(); +}); + +tape( 'the function inverts an object according to a transform function', function test( t ) { + var expected; + var actual; + var obj; + + function transform( key, value ) { + return value; + } + + obj = { + 'a': 'beep', + 'b': 'boop', + 'c': true, + 'd': null, + 'e': [ 1, 2, 3 ], + 'f': { + 'a': 'b' + }, + 'g': 1 + }; + expected = { + 'beep': 'a', + 'boop': 'b', + 'true': 'c', + 'null': 'd', + '1,2,3': 'e', + '[object Object]': 'f', + '1': 'g' + }; + + actual = invertBy( obj, transform ); + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); + +tape( 'the function handles duplicate values', function test( t ) { + var expected; + var actual; + var obj; + + function transform( key, value ) { + return value; + } + + obj = { + 'a': 'beep', + 'b': 'beep', + 'c': 'boop', + 'd': 'boop', + 'e': 'boop' + }; + expected = { + 'beep': [ 'a', 'b' ], + 'boop': [ 'c', 'd', 'e' ] + }; + actual = invertBy( obj, transform ); + + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); + +tape( 'the function supports overriding duplicate values', function test( t ) { + var expected; + var actual; + var opts; + var obj; + + function transform( key, value ) { + return value; + } + + obj = { + 'a': 'beep', + 'b': 'beep' + }; + opts = { + 'duplicates': false + }; + expected = { + 'beep': 'b' + }; + actual = invertBy( obj, opts, transform ); + + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); + +tape( 'the function ignores unknown options', function test( t ) { + var expected; + var actual; + var opts; + var obj; + + function transform( key, value ) { + return value; + } + + obj = { + 'a': 'beep', + 'b': 'beep' + }; + opts = { + 'bee': 'bop' + }; + expected = { + 'beep': [ 'a', 'b' ] + }; + actual = invertBy( obj, opts, transform ); + + t.deepEqual( actual, expected, 'ignores unknown options' ); + t.end(); +}); + +tape( 'the function supports custom transformations', function test( t ) { + var expected; + var actual; + var obj; + + function transform( key, value ) { + return key + value; + } + + obj = { + 'a': 'beep', + 'b': 'beep', + 'c': 'boop', + 'd': 'boop', + 'e': 'boop' + }; + expected = { + 'abeep': 'a', + 'bbeep': 'b', + 'cboop': 'c', + 'dboop': 'd', + 'eboop': 'e' + }; + actual = invertBy( obj, transform ); + + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); + +tape( 'the function supports custom transformations (object)', function test( t ) { + var expected; + var actual; + var obj; + + function transform( key, value, obj ) { + if ( key === 'name' ) { + return value; + } + return obj.name + ':' + value; + } + + obj = { + 'name': 'foo', + 'a': 'beep', + 'b': 'beep', + 'c': 'boop', + 'd': 'boop', + 'e': 'boop' + }; + expected = { + 'foo': 'name', + 'foo:beep': [ 'a', 'b' ], + 'foo:boop': [ 'c', 'd', 'e' ] + }; + actual = invertBy( obj, transform ); + + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); From 681ec43ce15a2d36b8f7bf50addddd150cb2b3c1 Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sun, 28 Dec 2025 13:41:00 +0530 Subject: [PATCH 2/4] remove: remove `objectInverseBy` from namespace This commit removes the `objectInverseBy` symbol from the `@stdlib/utils` namespace due to a package migration. BREAKING CHANGE: remove `objectInverseBy` To migrate, users should access the same symbol via the `@stdlib/object` namespace. Ref: https://github.com/stdlib-js/stdlib/issues/8755 --- .../@stdlib/utils/docs/types/index.d.ts | 40 ------------------- lib/node_modules/@stdlib/utils/lib/index.js | 9 ----- 2 files changed, 49 deletions(-) diff --git a/lib/node_modules/@stdlib/utils/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/docs/types/index.d.ts index 6af6c2aa5bf0..76973758459d 100644 --- a/lib/node_modules/@stdlib/utils/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/utils/docs/types/index.d.ts @@ -152,7 +152,6 @@ import nonEnumerablePropertySymbolsIn = require( '@stdlib/utils/nonenumerable-pr import nonIndexKeys = require( '@stdlib/utils/nonindex-keys' ); import noop = require( '@stdlib/utils/noop' ); import objectInverse = require( '@stdlib/utils/object-inverse' ); -import objectInverseBy = require( '@stdlib/utils/object-inverse-by' ); import omit = require( '@stdlib/utils/omit' ); import omitBy = require( '@stdlib/utils/omit-by' ); import openURL = require( '@stdlib/utils/open-url' ); @@ -4051,45 +4050,6 @@ interface Namespace { */ objectInverse: typeof objectInverse; - /** - * 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. - * - * - 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 - * @param opts.duplicates - boolean indicating whether to store duplicate keys (default: true) - * @param transform - transform function - * @returns 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 = ns.objectInverseBy( obj, opts, transform ); - * // returns { 'beep': 'c', 'boop': 'b' } - */ - objectInverseBy: typeof objectInverseBy; - /** * Returns a partial object copy excluding specified keys. * diff --git a/lib/node_modules/@stdlib/utils/lib/index.js b/lib/node_modules/@stdlib/utils/lib/index.js index badc25367154..7afdda6dab29 100644 --- a/lib/node_modules/@stdlib/utils/lib/index.js +++ b/lib/node_modules/@stdlib/utils/lib/index.js @@ -1228,15 +1228,6 @@ setReadOnly( utils, 'noop', require( '@stdlib/utils/noop' ) ); */ setReadOnly( utils, 'objectInverse', require( '@stdlib/utils/object-inverse' ) ); -/** -* @name objectInverseBy -* @memberof utils -* @readonly -* @type {Function} -* @see {@link module:@stdlib/utils/object-inverse-by} -*/ -setReadOnly( utils, 'objectInverseBy', require( '@stdlib/utils/object-inverse-by' ) ); - /** * @name omit * @memberof utils From 2c016b3b748c2f09dea50295b8fe7e7c7d61b3ca Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sun, 28 Dec 2025 17:15:58 +0530 Subject: [PATCH 3/4] refactor: update paths Ref: https://github.com/stdlib-js/stdlib/issues/8755 --- lib/node_modules/@stdlib/namespace/alias2pkg/data/data.csv | 2 +- lib/node_modules/@stdlib/namespace/lib/namespace/o.js | 6 +++--- lib/node_modules/@stdlib/namespace/pkg2alias/data/data.csv | 2 +- .../@stdlib/namespace/pkg2related/data/data.csv | 4 ++-- .../@stdlib/namespace/pkg2standalone/data/data.csv | 2 +- .../@stdlib/namespace/standalone2pkg/data/data.csv | 2 +- lib/node_modules/@stdlib/utils/object-inverse/README.md | 4 ++-- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/node_modules/@stdlib/namespace/alias2pkg/data/data.csv b/lib/node_modules/@stdlib/namespace/alias2pkg/data/data.csv index 0beda5c23b05..8bde07d7960d 100644 --- a/lib/node_modules/@stdlib/namespace/alias2pkg/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/alias2pkg/data/data.csv @@ -2663,7 +2663,7 @@ objectEntries,"@stdlib/utils/entries" objectEntriesIn,"@stdlib/utils/entries-in" objectFromEntries,"@stdlib/utils/from-entries" objectInverse,"@stdlib/utils/object-inverse" -objectInverseBy,"@stdlib/utils/object-inverse-by" +objectInverseBy,"@stdlib/object/inverse-by" objectKeys,"@stdlib/utils/keys" objectValues,"@stdlib/utils/values" objectValuesIn,"@stdlib/utils/values-in" diff --git a/lib/node_modules/@stdlib/namespace/lib/namespace/o.js b/lib/node_modules/@stdlib/namespace/lib/namespace/o.js index ff68023204bd..1850fbe6026a 100644 --- a/lib/node_modules/@stdlib/namespace/lib/namespace/o.js +++ b/lib/node_modules/@stdlib/namespace/lib/namespace/o.js @@ -76,14 +76,14 @@ ns.push({ 'value': require( '@stdlib/utils/object-inverse' ), 'type': 'Function', 'related': [ - '@stdlib/utils/object-inverse-by' + '@stdlib/object/inverse-by' ] }); ns.push({ 'alias': 'objectInverseBy', - 'path': '@stdlib/utils/object-inverse-by', - 'value': require( '@stdlib/utils/object-inverse-by' ), + 'path': '@stdlib/object/inverse-by', + 'value': require( '@stdlib/object/inverse-by' ), 'type': 'Function', 'related': [ '@stdlib/utils/object-inverse' diff --git a/lib/node_modules/@stdlib/namespace/pkg2alias/data/data.csv b/lib/node_modules/@stdlib/namespace/pkg2alias/data/data.csv index 129167495b6f..a839763c0e79 100644 --- a/lib/node_modules/@stdlib/namespace/pkg2alias/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/pkg2alias/data/data.csv @@ -2663,7 +2663,7 @@ "@stdlib/utils/entries-in",objectEntriesIn "@stdlib/utils/from-entries",objectFromEntries "@stdlib/utils/object-inverse",objectInverse -"@stdlib/utils/object-inverse-by",objectInverseBy +"@stdlib/object/inverse-by",objectInverseBy "@stdlib/utils/keys",objectKeys "@stdlib/utils/values",objectValues "@stdlib/utils/values-in",objectValuesIn diff --git a/lib/node_modules/@stdlib/namespace/pkg2related/data/data.csv b/lib/node_modules/@stdlib/namespace/pkg2related/data/data.csv index 522dc6186513..a775b8f46d1d 100644 --- a/lib/node_modules/@stdlib/namespace/pkg2related/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/pkg2related/data/data.csv @@ -2662,8 +2662,8 @@ "@stdlib/utils/entries","@stdlib/utils/entries-in,@stdlib/utils/from-entries,@stdlib/utils/keys,@stdlib/utils/values" "@stdlib/utils/entries-in","@stdlib/utils/entries,@stdlib/utils/from-entries,@stdlib/utils/keys-in,@stdlib/utils/values-in" "@stdlib/utils/from-entries","@stdlib/utils/entries" -"@stdlib/utils/object-inverse","@stdlib/utils/object-inverse-by" -"@stdlib/utils/object-inverse-by","@stdlib/utils/object-inverse" +"@stdlib/utils/object-inverse","@stdlib/object/inverse-by" +"@stdlib/object/inverse-by","@stdlib/utils/object-inverse" "@stdlib/utils/keys","@stdlib/utils/entries,@stdlib/utils/keys-in,@stdlib/utils/nonindex-keys,@stdlib/utils/values" "@stdlib/utils/values","@stdlib/utils/entries,@stdlib/utils/keys" "@stdlib/utils/values-in","@stdlib/utils/entries-in,@stdlib/utils/keys-in,@stdlib/utils/values" diff --git a/lib/node_modules/@stdlib/namespace/pkg2standalone/data/data.csv b/lib/node_modules/@stdlib/namespace/pkg2standalone/data/data.csv index 60bdd7af0bc3..3c8467739eea 100644 --- a/lib/node_modules/@stdlib/namespace/pkg2standalone/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/pkg2standalone/data/data.csv @@ -2663,7 +2663,7 @@ "@stdlib/utils/entries-in","@stdlib/utils-entries-in" "@stdlib/utils/from-entries","@stdlib/utils-from-entries" "@stdlib/utils/object-inverse","@stdlib/utils-object-inverse" -"@stdlib/utils/object-inverse-by","@stdlib/utils-object-inverse-by" +"@stdlib/object/inverse-by","@stdlib/object-inverse-by" "@stdlib/utils/keys","@stdlib/utils-keys" "@stdlib/utils/values","@stdlib/utils-values" "@stdlib/utils/values-in","@stdlib/utils-values-in" diff --git a/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv b/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv index 1571d9805f5e..220a45f5015c 100644 --- a/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv @@ -2663,7 +2663,7 @@ "@stdlib/utils-entries-in","@stdlib/utils/entries-in" "@stdlib/utils-from-entries","@stdlib/utils/from-entries" "@stdlib/utils-object-inverse","@stdlib/utils/object-inverse" -"@stdlib/utils-object-inverse-by","@stdlib/utils/object-inverse-by" +"@stdlib/object-inverse-by","@stdlib/object/inverse-by" "@stdlib/utils-keys","@stdlib/utils/keys" "@stdlib/utils-values","@stdlib/utils/values" "@stdlib/utils-values-in","@stdlib/utils/values-in" diff --git a/lib/node_modules/@stdlib/utils/object-inverse/README.md b/lib/node_modules/@stdlib/utils/object-inverse/README.md index a33fe8d63fd8..26e4d01bda67 100644 --- a/lib/node_modules/@stdlib/utils/object-inverse/README.md +++ b/lib/node_modules/@stdlib/utils/object-inverse/README.md @@ -144,7 +144,7 @@ console.dir( out ); ## See Also -- [`@stdlib/utils/object-inverse-by`][@stdlib/utils/object-inverse-by]: invert an object, such that keys become values and values become keys, according to a transform function. +- [`@stdlib/object/inverse-by`][@stdlib/object/inverse-by]: invert an object, such that keys become values and values become keys, according to a transform function. @@ -158,7 +158,7 @@ console.dir( out ); -[@stdlib/utils/object-inverse-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/object-inverse-by +[@stdlib/object/inverse-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/object/inverse-by From 4629996c2de9a1c702407cbae32db08eb67bb1e0 Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sun, 28 Dec 2025 17:18:18 +0530 Subject: [PATCH 4/4] remove: remove `utils/object-inverse-by` This commit removes `@stdlib/utils/object-inverse-by` in favor of `@stdlib/object/inverse-by`. BREAKING CHANGE: remove `utils/object-inverse-by` To migrate, users should update their require/import paths to use `@stdlib/object/inverse-by` which provides the same API and implementation. Ref: https://github.com/stdlib-js/stdlib/issues/4797 --- .../@stdlib/utils/object-inverse-by/README.md | 205 ---------- .../object-inverse-by/benchmark/benchmark.js | 130 ------- .../utils/object-inverse-by/docs/repl.txt | 67 ---- .../object-inverse-by/docs/types/index.d.ts | 160 -------- .../object-inverse-by/docs/types/test.ts | 81 ---- .../utils/object-inverse-by/examples/index.js | 46 --- .../utils/object-inverse-by/lib/index.js | 78 ---- .../utils/object-inverse-by/lib/main.js | 148 -------- .../utils/object-inverse-by/package.json | 75 ---- .../utils/object-inverse-by/test/test.js | 359 ------------------ 10 files changed, 1349 deletions(-) delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/README.md delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/benchmark/benchmark.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/docs/repl.txt delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/test.ts delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/examples/index.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/lib/index.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/lib/main.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/package.json delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse-by/test/test.js diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/README.md b/lib/node_modules/@stdlib/utils/object-inverse-by/README.md deleted file mode 100644 index 8c1651d1a57a..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/README.md +++ /dev/null @@ -1,205 +0,0 @@ - - -# Object Inverse - -> Invert an object, such that keys become values and values become keys, according to a transform function. - -
- -## Usage - -```javascript -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. - -```javascript -function transform( key, value ) { - return value; -} -var obj = { - 'a': 'beep', - 'b': 'boop' -}; -var out = invertBy( obj, transform ); -// returns { 'beep': 'a', 'boop': 'b' } -``` - -The function accepts the following options: - -- **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. - -```javascript -function transform( key, value ) { - return value; -} -var obj = { - 'a': 'beep', - 'b': 'beep' -}; -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. - -```javascript -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' } -``` - -The transform function is provided three arguments: - -- **key**: object key. -- **value**: object value corresponding to `key`. -- **obj**: input object. - -```javascript -function transform( key, value, o ) { - if ( key === 'name' ) { - return value; - } - return o.name + ':' + value; -} -var obj = { - 'name': 'foo', - 'a': 'beep', - 'b': 'boop' -}; -var out = invertBy( obj, transform ); -// returns { 'foo': 'name', 'foo:beep': 'a', 'foo:boop': 'b' } -``` - -
- - - -
- -## 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. - - ```javascript - function transform( key, value ) { - return value; - } - var obj = { - 'a': [ 1, 2, 3 ], - 'b': { - 'c': 'd' - } - }; - - var out = invertBy( obj, transform ); - // returns { '1,2,3': 'a', '[object Object]': 'b' } - ``` - -- 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. - -
- - - -
- -## Examples - - - -```javascript -var objectKeys = require( '@stdlib/utils/keys' ); -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var invertBy = require( '@stdlib/utils/object-inverse-by' ); - -function transform( key, value ) { - return value; -} - -// Create an array of random integers: -var arr = discreteUniform( 1000, 0, 100, { - 'dtype': 'generic' -}); - -// Invert the array to determine value frequency... -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; - } else { - out[ i ] = 0; - } -} -console.dir( out ); -``` - -
- - - - - - - - - - - - - - diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/object-inverse-by/benchmark/benchmark.js deleted file mode 100644 index dc41e2a23221..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/benchmark/benchmark.js +++ /dev/null @@ -1,130 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); -var pkg = require( './../package.json' ).name; -var invertBy = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var obj; - var out; - var i; - - function transform( key, value ) { - return key + ':' + value; - } - obj = { - 'a': 'beep', - 'b': 'boop', - 'c': 'foo', - 'd': 'bar', - 'e': randu() - }; - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - obj.e = randu(); - out = invertBy( obj, transform ); - if ( typeof out !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( typeof out !== 'object' ) { - b.fail( 'should return an object' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':duplicates=true', function benchmark( b ) { - var opts; - var obj; - var out; - var i; - - function transform( key, value ) { - return value + 'boop'; - } - obj = { - 'a': 'beep', - 'b': 'beep', - 'c': 'beep', - 'd': 'beep', - 'e': randu() - }; - opts = { - 'duplicates': true - }; - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - obj.e = randu(); - out = invertBy( obj, opts, transform ); - if ( typeof out !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( typeof out !== 'object' ) { - b.fail( 'should return an object' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':duplicates=false', function benchmark( b ) { - var opts; - var obj; - var out; - var i; - - function transform( key, value ) { - return value + 'boop'; - } - obj = { - 'a': 'beep', - 'b': 'beep', - 'c': 'beep', - 'd': 'beep', - 'e': randu() - }; - opts = { - 'duplicates': false - }; - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - obj.e = randu(); - out = invertBy( obj, opts, transform ); - if ( typeof out !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( typeof out !== 'object' ) { - b.fail( 'should return an object' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/docs/repl.txt b/lib/node_modules/@stdlib/utils/object-inverse-by/docs/repl.txt deleted file mode 100644 index 0cf84af38505..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/docs/repl.txt +++ /dev/null @@ -1,67 +0,0 @@ - -{{alias}}( obj, [options,] transform ) - Inverts an object, such that keys become values and values become keys, - according to a transform function. - - 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. - - 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 - ---------- - obj: ObjectLike - Input object. - - options: Object (optional) - Options. - - options.duplicates: boolean (optional) - Boolean indicating whether to store keys mapped to duplicate values in - arrays. Default: `true`. - - transform: Function - Transform function. - - Returns - ------- - out: Object - Inverted object. - - Examples - -------- - // Basic usage: - > function transform( key, value ) { return key + value; }; - > var obj = { 'a': 'beep', 'b': 'boop' }; - > var out = {{alias}}( obj, transform ) - { 'abeep': 'a', 'bboop': 'b' } - - // Duplicate values: - > function transform( key, value ) { return value; }; - > obj = { 'a': 'beep', 'b': 'beep' }; - > out = {{alias}}( obj, transform ) - { 'beep': [ 'a', 'b' ] } - - // Override duplicate values: - > obj = {}; - > obj.a = 'beep'; - > obj.b = 'boop'; - > obj.c = 'beep'; - > out = {{alias}}( obj, { 'duplicates': false }, transform ) - { 'beep': 'c', 'boop': 'b' } - - See Also - -------- - diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts deleted file mode 100644 index 942ca87be454..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/index.d.ts +++ /dev/null @@ -1,160 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2019 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Boolean indicating whether to store duplicate keys. - */ - duplicates?: boolean; -} - -/** -* Returns a value for an object element that can be serialized as an object key. -* -* @returns key in inverted object -*/ -type Nullary = () => string; - -/** -* Returns a value for an object element that can be serialized as an object key. -* -* @param key - object key -* @returns key in inverted object -*/ -type Unary = ( key: string ) => string; - -/** -* Returns a value for an object element that can be serialized as an object key. -* -* @param key - object key -* @param value - object value corresponding to `key` -* @returns key in inverted object -*/ -type Binary = ( key: string, value: any ) => string; - -/** -* Returns a value for an object element that can be serialized as an object key. -* -* @param key - object key -* @param value - object value corresponding to `key` -* @param obj - the input object -* @returns key in inverted object -*/ -type Ternary = ( key: string, value: any, obj: any ) => string; - -/** -* Returns a value for an object element that can be serialized as an object key. -* -* @param key - object key -* @param value - object value corresponding to `key` -* @param obj - the input object -* @returns key in inverted object -*/ -type Transform = Nullary | Unary | Binary | Ternary; - -/** -* 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. -* -* - 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 -* @returns 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' ] } -*/ -declare function invertBy( obj: any, transform: Transform ): any; - -/** -* 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. -* -* - 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 -* @param opts.duplicates - boolean indicating whether to store duplicate keys (default: true) -* @param transform - transform function -* @returns 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' } -*/ -declare function invertBy( obj: any, opts: Options, transform: Transform ): any; - - -// EXPORTS // - -export = invertBy; diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/test.ts b/lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/test.ts deleted file mode 100644 index e3c9bdf361cf..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/docs/types/test.ts +++ /dev/null @@ -1,81 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2019 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import invertBy = require( './index' ); - -const transform = ( key: string, value: string ): string => key + value; - - -// TESTS // - -// The function returns an object... -{ - const obj = { - 'a': 'beep', - 'b': 'boop' - }; - const opts = { - 'duplicates': false - }; - invertBy( obj, ( key: string, value: string ) => key + value ); // $ExpectType any - invertBy( obj, opts, ( key: string, value: string ) => key + value ); // $ExpectType any -} - -// The compiler throws an error if the function is provided a last argument which is not a function... -{ - const obj = { - 'a': 'beep', - 'b': 'boop' - }; - invertBy( obj, false ); // $ExpectError - invertBy( obj, true ); // $ExpectError - invertBy( obj, 32 ); // $ExpectError - invertBy( obj, 'abc' ); // $ExpectError - invertBy( obj, [] ); // $ExpectError - invertBy( obj, {} ); // $ExpectError -} - -// The compiler throws an error if the function is provided an options argument which is not an object... -{ - const obj = { - 'a': 'beep', - 'b': 'boop' - }; - invertBy( obj, null, transform ); // $ExpectError -} - -// The compiler throws an error if the function is provided a `duplicates` option which is not a boolean... -{ - const obj = { - 'a': 'beep', - 'b': 'boop' - }; - invertBy( obj, { 'duplicates': '5' }, transform ); // $ExpectError - invertBy( obj, { 'duplicates': 123 }, transform ); // $ExpectError - invertBy( obj, { 'duplicates': null }, transform ); // $ExpectError - invertBy( obj, { 'duplicates': [] }, transform ); // $ExpectError - invertBy( obj, { 'duplicates': {} }, transform ); // $ExpectError - invertBy( obj, { 'duplicates': ( x: number ): number => x }, transform ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - invertBy(); // $ExpectError - invertBy( {} ); // $ExpectError - invertBy( {}, {}, transform, 16 ); // $ExpectError -} diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/examples/index.js b/lib/node_modules/@stdlib/utils/object-inverse-by/examples/index.js deleted file mode 100644 index b71ddc803884..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/examples/index.js +++ /dev/null @@ -1,46 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var objectKeys = require( '@stdlib/utils/keys' ); -var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var invertBy = require( './../lib' ); - -function transform( key, value ) { - return value; -} - -// Create an array of random integers: -var arr = discreteUniform( 1000, 0, 100, { - 'dtype': 'generic' -}); - -// Invert the array to determine value frequency... -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; - } else { - out[ i ] = 0; - } -} -console.dir( out ); diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/lib/index.js b/lib/node_modules/@stdlib/utils/object-inverse-by/lib/index.js deleted file mode 100644 index e58eda751182..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/lib/index.js +++ /dev/null @@ -1,78 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Invert an object, such that keys become values and values become keys, according to a transform function. -* -* @module @stdlib/utils/object-inverse-by -* -* @example -* var invertBy = require( '@stdlib/utils/object-inverse-by' ); -* -* function transform( key, value ) { -* return key + value; -* } -* var obj = { -* 'a': 'beep', -* 'b': 'boop' -* }; -* var out = invertBy( obj, transform ); -* // returns { 'abeep': 'a', 'bboop': 'b' } -* -* @example -* var invertBy = require( '@stdlib/utils/object-inverse-by' ); -* -* function transform( key, value ) { -* return value; -* } -* var obj = { -* 'a': 'beep', -* 'b': 'beep' -* }; -* var out = invertBy( obj, transform ); -* // returns { 'beep': [ 'a', 'b' ] } -* -* @example -* var invertBy = require( '@stdlib/utils/object-inverse-by' ); -* -* 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' } -*/ - -// MODULES // - -var main = require( './main.js' ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/lib/main.js b/lib/node_modules/@stdlib/utils/object-inverse-by/lib/main.js deleted file mode 100644 index a088454db521..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/lib/main.js +++ /dev/null @@ -1,148 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var objectKeys = require( '@stdlib/utils/keys' ); -var isArray = require( '@stdlib/assert/is-array' ); -var isObject = require( '@stdlib/assert/is-plain-object' ); -var isObjectLike = require( '@stdlib/assert/is-object-like' ); -var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; -var isFunction = require( '@stdlib/assert/is-function' ); -var hasOwnProp = require( '@stdlib/assert/has-own-property' ); -var format = require( '@stdlib/string/format' ); - - -// MAIN // - -/** -* Inverts an object, such that keys become values and values become keys, according to a transform function. -* -* @param {ObjectLike} obj - input object -* @param {Options} [opts] - function options -* @param {boolean} [opts.duplicates=true] - boolean indicating whether to store duplicate keys -* @param {Function} transform - transform function -* @throws {TypeError} first argument must be object-like -* @throws {TypeError} options argument must an an object -* @throws {TypeError} last argument must be a function -* @throws {TypeError} must provide valid options -* @returns {Object} 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' ] } -* -* @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' } -*/ -function invertBy( obj, opts, transform ) { - var allowDupes; - var keys; - var len; - var key; - var val; - var out; - var cb; - var v; - var i; - if ( !isObjectLike( obj ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an object (except null). Value: `%s`.', obj ) ); - } - allowDupes = true; - if ( arguments.length === 2 ) { - cb = opts; - } else { - if ( !isObject( opts ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) ); - } - if ( hasOwnProp( opts, 'duplicates' ) ) { - allowDupes = opts.duplicates; - if ( !isBoolean( allowDupes ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'duplicates', allowDupes ) ); - } - } - cb = transform; - } - if ( !isFunction( cb ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', cb ) ); - } - keys = objectKeys( obj ); - len = keys.length; - out = {}; - if ( allowDupes ) { - for ( i = 0; i < len; i++ ) { - key = keys[ i ]; - val = cb( key, obj[ key ], obj ); - if ( !hasOwnProp( out, val ) ) { - out[ val ] = key; - continue; - } - v = out[ val ]; - if ( isArray( v ) ) { - out[ val ].push( key ); - } else { - out[ val ] = [ v, key ]; - } - } - } else { - for ( i = 0; i < len; i++ ) { - key = keys[ i ]; - val = cb( key, obj[ key ], obj ); - out[ val ] = key; - } - } - return out; -} - - -// EXPORTS // - -module.exports = invertBy; diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/package.json b/lib/node_modules/@stdlib/utils/object-inverse-by/package.json deleted file mode 100644 index 4d2eae33aa47..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/package.json +++ /dev/null @@ -1,75 +0,0 @@ -{ - "name": "@stdlib/utils/object-inverse-by", - "version": "0.0.0", - "description": "Invert an object, such that keys become values and values become keys, according to a transform function.", - "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": {}, - "homepage": "https://github.com/stdlib-js/stdlib", - "repository": { - "type": "git", - "url": "git://github.com/stdlib-js/stdlib.git" - }, - "bugs": { - "url": "https://github.com/stdlib-js/stdlib/issues" - }, - "dependencies": {}, - "devDependencies": {}, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], - "keywords": [ - "stdlib", - "stdutils", - "stdutil", - "utilities", - "utility", - "utils", - "util", - "inverse", - "invert", - "invertby", - "inverseby", - "transform", - "convert", - "object", - "obj", - "object-like", - "keys", - "values", - "bijection", - "mapping", - "hash", - "array" - ] -} diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/test/test.js b/lib/node_modules/@stdlib/utils/object-inverse-by/test/test.js deleted file mode 100644 index dd2ef847d11c..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/test/test.js +++ /dev/null @@ -1,359 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils/noop' ); -var invertBy = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof invertBy, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided an object-like value', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - invertBy( value, noop ); - }; - } -}); - -tape( 'the function throws an error if provided a last argument which is not a function (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - invertBy( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided a last argument which is not a function (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - invertBy( {}, {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - invertBy( {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if provided a duplicates option which is not a boolean primitive', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'duplicates': value - }; - invertBy( {}, opts, noop ); - }; - } -}); - -tape( 'the function returns an empty object if provided an empty object', function test( t ) { - var out; - - function transform( key, value ) { - return value; - } - out = invertBy( {}, transform ); - t.deepEqual( out, {}, 'returns empty object' ); - t.end(); -}); - -tape( 'the function inverts an object according to a transform function', function test( t ) { - var expected; - var actual; - var obj; - - function transform( key, value ) { - return value; - } - - obj = { - 'a': 'beep', - 'b': 'boop', - 'c': true, - 'd': null, - 'e': [ 1, 2, 3 ], - 'f': { - 'a': 'b' - }, - 'g': 1 - }; - expected = { - 'beep': 'a', - 'boop': 'b', - 'true': 'c', - 'null': 'd', - '1,2,3': 'e', - '[object Object]': 'f', - '1': 'g' - }; - - actual = invertBy( obj, transform ); - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -}); - -tape( 'the function handles duplicate values', function test( t ) { - var expected; - var actual; - var obj; - - function transform( key, value ) { - return value; - } - - obj = { - 'a': 'beep', - 'b': 'beep', - 'c': 'boop', - 'd': 'boop', - 'e': 'boop' - }; - expected = { - 'beep': [ 'a', 'b' ], - 'boop': [ 'c', 'd', 'e' ] - }; - actual = invertBy( obj, transform ); - - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -}); - -tape( 'the function supports overriding duplicate values', function test( t ) { - var expected; - var actual; - var opts; - var obj; - - function transform( key, value ) { - return value; - } - - obj = { - 'a': 'beep', - 'b': 'beep' - }; - opts = { - 'duplicates': false - }; - expected = { - 'beep': 'b' - }; - actual = invertBy( obj, opts, transform ); - - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -}); - -tape( 'the function ignores unknown options', function test( t ) { - var expected; - var actual; - var opts; - var obj; - - function transform( key, value ) { - return value; - } - - obj = { - 'a': 'beep', - 'b': 'beep' - }; - opts = { - 'bee': 'bop' - }; - expected = { - 'beep': [ 'a', 'b' ] - }; - actual = invertBy( obj, opts, transform ); - - t.deepEqual( actual, expected, 'ignores unknown options' ); - t.end(); -}); - -tape( 'the function supports custom transformations', function test( t ) { - var expected; - var actual; - var obj; - - function transform( key, value ) { - return key + value; - } - - obj = { - 'a': 'beep', - 'b': 'beep', - 'c': 'boop', - 'd': 'boop', - 'e': 'boop' - }; - expected = { - 'abeep': 'a', - 'bbeep': 'b', - 'cboop': 'c', - 'dboop': 'd', - 'eboop': 'e' - }; - actual = invertBy( obj, transform ); - - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -}); - -tape( 'the function supports custom transformations (object)', function test( t ) { - var expected; - var actual; - var obj; - - function transform( key, value, obj ) { - if ( key === 'name' ) { - return value; - } - return obj.name + ':' + value; - } - - obj = { - 'name': 'foo', - 'a': 'beep', - 'b': 'beep', - 'c': 'boop', - 'd': 'boop', - 'e': 'boop' - }; - expected = { - 'foo': 'name', - 'foo:beep': [ 'a', 'b' ], - 'foo:boop': [ 'c', 'd', 'e' ] - }; - actual = invertBy( obj, transform ); - - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -});