From 10befd880d9e0146b87e5dc2e4c65c1b246f2a0e Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sat, 27 Dec 2025 17:39:40 +0530 Subject: [PATCH 1/4] feat: add `object/inverse` Ref: https://github.com/stdlib-js/stdlib/issues/8755 --- .../@stdlib/object/inverse/README.md | 167 ++++++++++++++ .../object/inverse/benchmark/benchmark.js | 121 ++++++++++ .../@stdlib/object/inverse/docs/repl.txt | 53 +++++ .../object/inverse/docs/types/index.d.ts | 66 ++++++ .../@stdlib/object/inverse/docs/types/test.ts | 60 +++++ .../@stdlib/object/inverse/examples/index.js | 46 ++++ .../@stdlib/object/inverse/lib/index.js | 59 +++++ .../@stdlib/object/inverse/lib/main.js | 123 ++++++++++ .../@stdlib/object/inverse/package.json | 69 ++++++ .../@stdlib/object/inverse/test/test.js | 215 ++++++++++++++++++ 10 files changed, 979 insertions(+) create mode 100644 lib/node_modules/@stdlib/object/inverse/README.md create mode 100644 lib/node_modules/@stdlib/object/inverse/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/object/inverse/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/object/inverse/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/object/inverse/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/object/inverse/examples/index.js create mode 100644 lib/node_modules/@stdlib/object/inverse/lib/index.js create mode 100644 lib/node_modules/@stdlib/object/inverse/lib/main.js create mode 100644 lib/node_modules/@stdlib/object/inverse/package.json create mode 100644 lib/node_modules/@stdlib/object/inverse/test/test.js diff --git a/lib/node_modules/@stdlib/object/inverse/README.md b/lib/node_modules/@stdlib/object/inverse/README.md new file mode 100644 index 000000000000..af495c45b03e --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/README.md @@ -0,0 +1,167 @@ + + +# Object Inverse + +> Invert an object, such that keys become values and values become keys. + +
+ +## Usage + +```javascript +var invert = require( '@stdlib/object/inverse' ); +``` + +#### invert( obj\[, options] ) + +Inverts an `object`, such that keys become values and values become keys. + +```javascript +var out = invert({ + 'a': 'beep', + 'b': 'boop' +}); +// 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 +var out = invert({ + 'a': 'beep', + 'b': 'beep' +}); +// 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 +var obj = {}; +obj.a = 'beep'; +obj.b = 'boop'; +obj.c = 'beep'; // inserted after `a` + +var out = invert( obj, { + 'duplicates': false +}); +// returns { 'beep': 'c', 'boop': 'b' } +``` + +
+ + + +
+ +## Notes + +- Beware when providing `objects` having values which are themselves `objects`. This implementation relies on native `object` serialization (`#toString`) when converting values to keys. + + ```javascript + var obj = { + 'a': [ 1, 2, 3 ], + 'b': { + 'c': 'd' + } + }; + + var out = invert( obj ); + // 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. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var invert = require( '@stdlib/object/inverse' ); + +var keys; +var arr; +var out; +var i; + +// Create an array of random integers... +arr = new Array( 1000 ); +for ( i = 0; i < arr.length; i++ ) { + arr[ i ] = round( randu()*100.0 ); +} +// Invert the array to determine value frequency... +out = invert( arr ); +keys = Object.keys( out ); +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/benchmark/benchmark.js b/lib/node_modules/@stdlib/object/inverse/benchmark/benchmark.js new file mode 100644 index 000000000000..26abd73e0899 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/benchmark/benchmark.js @@ -0,0 +1,121 @@ +/** +* @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 invert = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var obj; + var out; + var i; + + obj = { + 'a': 'beep', + 'b': 'boop', + 'c': 'foo', + 'd': 'bar', + 'e': randu() + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.e = randu(); + out = invert( obj ); + 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; + + 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 = invert( obj, opts ); + 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; + + 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 = invert( obj, opts ); + 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/docs/repl.txt b/lib/node_modules/@stdlib/object/inverse/docs/repl.txt new file mode 100644 index 000000000000..60ca7ca3602e --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/docs/repl.txt @@ -0,0 +1,53 @@ + +{{alias}}( obj[, options] ) + Inverts an object, such that keys become values and values become keys. + + Beware when providing objects having values which are themselves objects. + The function relies on native object serialization (`#toString`) when + converting values to keys. + + Insertion order is not guaranteed, as object key enumeration is not + specified according to the ECMAScript specification. In practice, however, + most engines use insertion order to sort an object's keys, thus allowing for + deterministic inversion. + + Parameters + ---------- + obj: 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`. + + Returns + ------- + out: Object + Inverted object. + + Examples + -------- + // Basic usage: + > var obj = { 'a': 'beep', 'b': 'boop' }; + > var out = {{alias}}( obj ) + { 'beep': 'a', 'boop': 'b' } + + // Duplicate values: + > obj = { 'a': 'beep', 'b': 'beep' }; + > out = {{alias}}( obj ) + { 'beep': [ 'a', 'b' ] } + + // Override duplicate values: + > obj = {}; + > obj.a = 'beep'; + > obj.b = 'boop'; + > obj.c = 'beep'; + > out = {{alias}}( obj, { 'duplicates': false } ) + { 'beep': 'c', 'boop': 'b' } + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/object/inverse/docs/types/index.d.ts b/lib/node_modules/@stdlib/object/inverse/docs/types/index.d.ts new file mode 100644 index 000000000000..0a4a90679b0d --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/docs/types/index.d.ts @@ -0,0 +1,66 @@ +/* +* @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 Options { + /** + * Boolean indicating whether to store duplicate keys. + */ + duplicates?: boolean; +} + +/** +* Inverts an object, such that keys become values and values become keys. +* +* @param obj - input object +* @param options - function options +* @param options.duplicates - boolean indicating whether to store duplicate keys (default: true) +* @returns inverted object +* +* @example +* var out = invert({ +* 'a': 'beep', +* 'b': 'boop' +* }); +* // returns { 'beep': 'a', 'boop': 'b' } +* +* @example +* var out = invert({ +* 'a': 'beep', +* 'b': 'beep' +* }); +* // returns { 'beep': [ 'a', 'b' ] } +* +* @example +* var obj = {}; +* obj.a = 'beep'; +* obj.b = 'boop'; +* obj.c = 'beep'; // inserted after `a` +* +* var out = invert( obj, { +* 'duplicates': false +* }); +* // returns { 'beep': 'c', 'boop': 'b' } +*/ +declare function invert( obj: any, options?: Options ): any; + + +// EXPORTS // + +export = invert; diff --git a/lib/node_modules/@stdlib/object/inverse/docs/types/test.ts b/lib/node_modules/@stdlib/object/inverse/docs/types/test.ts new file mode 100644 index 000000000000..32f5d363b272 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @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 invert = require( './index' ); + + +// TESTS // + +// The function returns an object... +{ + const obj = { + 'a': 'beep', + 'b': 'boop' + }; + invert( obj ); // $ExpectType any +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const obj = { + 'a': 'beep', + 'b': 'boop' + }; + invert( obj, null ); // $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' + }; + invert( obj, { 'duplicates': '5' } ); // $ExpectError + invert( obj, { 'duplicates': 123 } ); // $ExpectError + invert( obj, { 'duplicates': null } ); // $ExpectError + invert( obj, { 'duplicates': [] } ); // $ExpectError + invert( obj, { 'duplicates': {} } ); // $ExpectError + invert( obj, { 'duplicates': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + invert(); // $ExpectError + invert( {}, 4, 16 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/object/inverse/examples/index.js b/lib/node_modules/@stdlib/object/inverse/examples/index.js new file mode 100644 index 000000000000..0db3a85eb318 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/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 randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var invert = require( './../lib' ); + +var keys; +var arr; +var out; +var i; + +// Create an array of random integers... +arr = new Array( 1000 ); +for ( i = 0; i < arr.length; i++ ) { + arr[ i ] = round( randu()*100.0 ); +} +// Invert the array to determine value frequency... +out = invert( arr ); +keys = objectKeys( out ); +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/lib/index.js b/lib/node_modules/@stdlib/object/inverse/lib/index.js new file mode 100644 index 000000000000..835c30fecf56 --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/lib/index.js @@ -0,0 +1,59 @@ +/** +* @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. +* +* @module @stdlib/object/inverse +* +* @example +* var invert = require( '@stdlib/object/inverse' ); +* +* var out = invert({ +* 'a': 'beep', +* 'b': 'boop' +* }); +* // returns { 'beep': 'a', 'boop': 'b' } +* +* out = invert({ +* 'a': 'beep', +* 'b': 'beep' +* }); +* // returns { 'beep': [ 'a', 'b' ] } +* +* var obj = {}; +* obj.a = 'beep'; +* obj.b = 'boop'; +* obj.c = 'beep'; // inserted after `a` +* +* out = invert( obj, { +* 'duplicates': false +* }); +* // returns { 'beep': 'c', 'boop': 'b' } +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/object/inverse/lib/main.js b/lib/node_modules/@stdlib/object/inverse/lib/main.js new file mode 100644 index 000000000000..d9b0a9b969bc --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/lib/main.js @@ -0,0 +1,123 @@ +/** +* @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 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. +* +* @param {ObjectLike} obj - input object +* @param {Options} [opts] - function options +* @param {boolean} [opts.duplicates=true] - boolean indicating whether to store duplicate keys +* @throws {TypeError} first argument must be object-like +* @throws {TypeError} second argument must an an object +* @throws {TypeError} must provide valid options +* @returns {Object} inverted object +* +* @example +* var out = invert({ +* 'a': 'beep', +* 'b': 'boop' +* }); +* // returns { 'beep': 'a', 'boop': 'b' } +* +* @example +* var out = invert({ +* 'a': 'beep', +* 'b': 'beep' +* }); +* // returns { 'beep': [ 'a', 'b' ] } +* +* @example +* var obj = {}; +* obj.a = 'beep'; +* obj.b = 'boop'; +* obj.c = 'beep'; // inserted after `a` +* +* var out = invert( obj, { +* 'duplicates': false +* }); +* // returns { 'beep': 'c', 'boop': 'b' } +*/ +function invert( obj, opts ) { + var allowDupes = true; + var keys; + var len; + var key; + var val; + var out; + var v; + var i; + if ( !isObjectLike( obj ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an object (except null). Value: `%s`.', obj ) ); + } + if ( arguments.length > 1 ) { + 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 ) ); + } + } + } + keys = objectKeys( obj ); + len = keys.length; + out = {}; + if ( allowDupes ) { + for ( i = 0; i < len; i++ ) { + key = keys[ i ]; + val = obj[ key ]; + 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 ]; + out[ obj[ key ] ] = key; + } + } + return out; +} + + +// EXPORTS // + +module.exports = invert; diff --git a/lib/node_modules/@stdlib/object/inverse/package.json b/lib/node_modules/@stdlib/object/inverse/package.json new file mode 100644 index 000000000000..cd52ddfea37b --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/object/inverse", + "version": "0.0.0", + "description": "Invert an object, such that keys become values and values become keys.", + "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", + "object", + "keys", + "values", + "bijection", + "mapping", + "hash", + "array" + ] +} diff --git a/lib/node_modules/@stdlib/object/inverse/test/test.js b/lib/node_modules/@stdlib/object/inverse/test/test.js new file mode 100644 index 000000000000..abac4a13439b --- /dev/null +++ b/lib/node_modules/@stdlib/object/inverse/test/test.js @@ -0,0 +1,215 @@ +/** +* @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 invert = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof invert, '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() { + invert( 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() { + invert( {}, value ); + }; + } +}); + +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() { + invert( {}, { + 'duplicates': value + }); + }; + } +}); + +tape( 'the function returns an empty object if provided an empty object', function test( t ) { + t.deepEqual( invert( {} ), {}, 'returns empty object' ); + t.end(); +}); + +tape( 'the function inverts an object', function test( t ) { + var expected; + var actual; + var obj; + + 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 = invert( obj ); + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); + +tape( 'the function handles duplicate values', function test( t ) { + var expected; + var actual; + var obj; + + obj = { + 'a': 'beep', + 'b': 'beep', + 'c': 'boop', + 'd': 'boop', + 'e': 'boop' + }; + expected = { + 'beep': [ 'a', 'b' ], + 'boop': [ 'c', 'd', 'e' ] + }; + actual = invert( obj ); + + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); + +tape( 'the function supports overriding duplicate values', function test( t ) { + var expected; + var actual; + var obj; + + obj = { + 'a': 'beep', + 'b': 'beep' + }; + expected = { + 'beep': 'b' + }; + actual = invert( obj, { + 'duplicates': false + }); + + t.deepEqual( actual, expected, 'returns inverted object' ); + t.end(); +}); + +tape( 'the function ignores unknown options', function test( t ) { + var expected; + var actual; + var obj; + + obj = { + 'a': 'beep', + 'b': 'beep' + }; + expected = { + 'beep': [ 'a', 'b' ] + }; + actual = invert( obj, { + 'bee': 'bop' + }); + + t.deepEqual( actual, expected, 'ignores unknown options' ); + t.end(); +}); From 1a0ede80107207a8639f2762a357828b388b6ed6 Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sat, 27 Dec 2025 17:41:37 +0530 Subject: [PATCH 2/4] remove: remove `objectInverse` from namespace This commit removes the `objectInverse` symbol from the `@stdlib/utils` namespace due to a package migration. BREAKING CHANGE: remove `objectInverse` 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 | 36 ------------------- lib/node_modules/@stdlib/utils/lib/index.js | 9 ----- 2 files changed, 45 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..2a16957f937e 100644 --- a/lib/node_modules/@stdlib/utils/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/utils/docs/types/index.d.ts @@ -151,7 +151,6 @@ import nonEnumerablePropertySymbols = require( '@stdlib/utils/nonenumerable-prop import nonEnumerablePropertySymbolsIn = require( '@stdlib/utils/nonenumerable-property-symbols-in' ); 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' ); @@ -4016,41 +4015,6 @@ interface Namespace { */ noop: typeof noop; - /** - * Inverts an object, such that keys become values and values become keys. - * - * @param obj - input object - * @param options - function options - * @param options.duplicates - boolean indicating whether to store duplicate keys (default: true) - * @returns inverted object - * - * @example - * var out = ns.objectInverse({ - * 'a': 'beep', - * 'b': 'boop' - * }); - * // returns { 'beep': 'a', 'boop': 'b' } - * - * @example - * var out = ns.objectInverse({ - * 'a': 'beep', - * 'b': 'beep' - * }); - * // returns { 'beep': [ 'a', 'b' ] } - * - * @example - * var obj = {}; - * obj.a = 'beep'; - * obj.b = 'boop'; - * obj.c = 'beep'; // inserted after `a` - * - * var out = ns.objectInverse( obj, { - * 'duplicates': false - * }); - * // returns { 'beep': 'c', 'boop': 'b' } - */ - objectInverse: typeof objectInverse; - /** * Inverts an object, such that keys become values and values become keys, according to a transform function. * diff --git a/lib/node_modules/@stdlib/utils/lib/index.js b/lib/node_modules/@stdlib/utils/lib/index.js index badc25367154..42513e32a6c7 100644 --- a/lib/node_modules/@stdlib/utils/lib/index.js +++ b/lib/node_modules/@stdlib/utils/lib/index.js @@ -1219,15 +1219,6 @@ setReadOnly( utils, 'nonIndexKeys', require( '@stdlib/utils/nonindex-keys' ) ); */ setReadOnly( utils, 'noop', require( '@stdlib/utils/noop' ) ); -/** -* @name objectInverse -* @memberof utils -* @readonly -* @type {Function} -* @see {@link module:@stdlib/utils/object-inverse} -*/ -setReadOnly( utils, 'objectInverse', require( '@stdlib/utils/object-inverse' ) ); - /** * @name objectInverseBy * @memberof utils From 94c3942fa7feb8ba05e5a6aa5a3639bda31db613 Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sat, 27 Dec 2025 17:59:05 +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 +- .../@stdlib/ndarray/base/dtype-enum2str/lib/main.js | 2 +- lib/node_modules/@stdlib/utils/object-inverse-by/README.md | 4 ++-- 8 files changed, 12 insertions(+), 12 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..7c7a901a5502 100644 --- a/lib/node_modules/@stdlib/namespace/alias2pkg/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/alias2pkg/data/data.csv @@ -2662,7 +2662,7 @@ Object,"@stdlib/object/ctor" objectEntries,"@stdlib/utils/entries" objectEntriesIn,"@stdlib/utils/entries-in" objectFromEntries,"@stdlib/utils/from-entries" -objectInverse,"@stdlib/utils/object-inverse" +objectInverse,"@stdlib/object/inverse" objectInverseBy,"@stdlib/utils/object-inverse-by" objectKeys,"@stdlib/utils/keys" objectValues,"@stdlib/utils/values" diff --git a/lib/node_modules/@stdlib/namespace/lib/namespace/o.js b/lib/node_modules/@stdlib/namespace/lib/namespace/o.js index ff68023204bd..262be8c2ea43 100644 --- a/lib/node_modules/@stdlib/namespace/lib/namespace/o.js +++ b/lib/node_modules/@stdlib/namespace/lib/namespace/o.js @@ -72,8 +72,8 @@ ns.push({ ns.push({ 'alias': 'objectInverse', - 'path': '@stdlib/utils/object-inverse', - 'value': require( '@stdlib/utils/object-inverse' ), + 'path': '@stdlib/object/inverse', + 'value': require( '@stdlib/object/inverse' ), 'type': 'Function', 'related': [ '@stdlib/utils/object-inverse-by' @@ -86,7 +86,7 @@ ns.push({ 'value': require( '@stdlib/utils/object-inverse-by' ), 'type': 'Function', 'related': [ - '@stdlib/utils/object-inverse' + '@stdlib/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..11fbf9008bef 100644 --- a/lib/node_modules/@stdlib/namespace/pkg2alias/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/pkg2alias/data/data.csv @@ -2662,7 +2662,7 @@ "@stdlib/utils/entries",objectEntries "@stdlib/utils/entries-in",objectEntriesIn "@stdlib/utils/from-entries",objectFromEntries -"@stdlib/utils/object-inverse",objectInverse +"@stdlib/object/inverse",objectInverse "@stdlib/utils/object-inverse-by",objectInverseBy "@stdlib/utils/keys",objectKeys "@stdlib/utils/values",objectValues diff --git a/lib/node_modules/@stdlib/namespace/pkg2related/data/data.csv b/lib/node_modules/@stdlib/namespace/pkg2related/data/data.csv index 522dc6186513..5482bfbcb7ff 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/object/inverse","@stdlib/utils/object-inverse-by" +"@stdlib/utils/object-inverse-by","@stdlib/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..c459c7534c7a 100644 --- a/lib/node_modules/@stdlib/namespace/pkg2standalone/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/pkg2standalone/data/data.csv @@ -2662,7 +2662,7 @@ "@stdlib/utils/entries","@stdlib/utils-entries" "@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/object/inverse","@stdlib/object-inverse" "@stdlib/utils/object-inverse-by","@stdlib/utils-object-inverse-by" "@stdlib/utils/keys","@stdlib/utils-keys" "@stdlib/utils/values","@stdlib/utils-values" diff --git a/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv b/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv index 1571d9805f5e..caa0114203d4 100644 --- a/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv +++ b/lib/node_modules/@stdlib/namespace/standalone2pkg/data/data.csv @@ -2662,7 +2662,7 @@ "@stdlib/utils-entries","@stdlib/utils/entries" "@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/object-inverse","@stdlib/object/inverse" "@stdlib/utils-object-inverse-by","@stdlib/utils/object-inverse-by" "@stdlib/utils-keys","@stdlib/utils/keys" "@stdlib/utils-values","@stdlib/utils/values" diff --git a/lib/node_modules/@stdlib/ndarray/base/dtype-enum2str/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/dtype-enum2str/lib/main.js index 5dd034975c98..8a2ad428453d 100644 --- a/lib/node_modules/@stdlib/ndarray/base/dtype-enum2str/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/dtype-enum2str/lib/main.js @@ -21,7 +21,7 @@ // MODULES // var isString = require( '@stdlib/assert/is-string' ).isPrimitive; -var objectInverse = require( '@stdlib/utils/object-inverse' ); +var objectInverse = require( '@stdlib/object/inverse' ); var dtypeEnums = require( '@stdlib/ndarray/base/dtype-enums' ); diff --git a/lib/node_modules/@stdlib/utils/object-inverse-by/README.md b/lib/node_modules/@stdlib/utils/object-inverse-by/README.md index 8c1651d1a57a..90db015652bd 100644 --- a/lib/node_modules/@stdlib/utils/object-inverse-by/README.md +++ b/lib/node_modules/@stdlib/utils/object-inverse-by/README.md @@ -182,7 +182,7 @@ console.dir( out ); ## See Also -- [`@stdlib/utils/object-inverse`][@stdlib/utils/object-inverse]: invert an object, such that keys become values and values become keys. +- [`@stdlib/object/inverse`][@stdlib/object/inverse]: invert an object, such that keys become values and values become keys. @@ -196,7 +196,7 @@ console.dir( out ); -[@stdlib/utils/object-inverse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/object-inverse +[@stdlib/object/inverse]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/object/inverse From 3af3ecb94207a5bb3c71cecbd46674e34efaf062 Mon Sep 17 00:00:00 2001 From: NEERAJ Date: Sat, 27 Dec 2025 18:01:59 +0530 Subject: [PATCH 4/4] remove: remove `utils/object-inverse` This commit removes `@stdlib/utils/object-inverse` in favor of `@stdlib/object/inverse`. BREAKING CHANGE: remove `utils/object-inverse` To migrate, users should update their require/import paths to use `@stdlib/object/inverse` which provides the same API and implementation. Ref: https://github.com/stdlib-js/stdlib/issues/8755 --- .../@stdlib/utils/object-inverse/README.md | 167 -------------- .../object-inverse/benchmark/benchmark.js | 121 ---------- .../utils/object-inverse/docs/repl.txt | 53 ----- .../object-inverse/docs/types/index.d.ts | 66 ------ .../utils/object-inverse/docs/types/test.ts | 60 ----- .../utils/object-inverse/examples/index.js | 46 ---- .../@stdlib/utils/object-inverse/lib/index.js | 59 ----- .../@stdlib/utils/object-inverse/lib/main.js | 123 ---------- .../@stdlib/utils/object-inverse/package.json | 69 ------ .../@stdlib/utils/object-inverse/test/test.js | 215 ------------------ 10 files changed, 979 deletions(-) delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/README.md delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/benchmark/benchmark.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/docs/repl.txt delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/docs/types/index.d.ts delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/docs/types/test.ts delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/examples/index.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/lib/index.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/lib/main.js delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/package.json delete mode 100644 lib/node_modules/@stdlib/utils/object-inverse/test/test.js diff --git a/lib/node_modules/@stdlib/utils/object-inverse/README.md b/lib/node_modules/@stdlib/utils/object-inverse/README.md deleted file mode 100644 index a33fe8d63fd8..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/README.md +++ /dev/null @@ -1,167 +0,0 @@ - - -# Object Inverse - -> Invert an object, such that keys become values and values become keys. - -
- -## Usage - -```javascript -var invert = require( '@stdlib/utils/object-inverse' ); -``` - -#### invert( obj\[, options] ) - -Inverts an `object`, such that keys become values and values become keys. - -```javascript -var out = invert({ - 'a': 'beep', - 'b': 'boop' -}); -// 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 -var out = invert({ - 'a': 'beep', - 'b': 'beep' -}); -// 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 -var obj = {}; -obj.a = 'beep'; -obj.b = 'boop'; -obj.c = 'beep'; // inserted after `a` - -var out = invert( obj, { - 'duplicates': false -}); -// returns { 'beep': 'c', 'boop': 'b' } -``` - -
- - - -
- -## Notes - -- Beware when providing `objects` having values which are themselves `objects`. This implementation relies on native `object` serialization (`#toString`) when converting values to keys. - - ```javascript - var obj = { - 'a': [ 1, 2, 3 ], - 'b': { - 'c': 'd' - } - }; - - var out = invert( obj ); - // 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. - -
- - - -
- -## Examples - - - -```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var invert = require( '@stdlib/utils/object-inverse' ); - -var keys; -var arr; -var out; -var i; - -// Create an array of random integers... -arr = new Array( 1000 ); -for ( i = 0; i < arr.length; i++ ) { - arr[ i ] = round( randu()*100.0 ); -} -// Invert the array to determine value frequency... -out = invert( arr ); -keys = Object.keys( out ); -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/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/object-inverse/benchmark/benchmark.js deleted file mode 100644 index 26abd73e0899..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/benchmark/benchmark.js +++ /dev/null @@ -1,121 +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 invert = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var obj; - var out; - var i; - - obj = { - 'a': 'beep', - 'b': 'boop', - 'c': 'foo', - 'd': 'bar', - 'e': randu() - }; - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - obj.e = randu(); - out = invert( obj ); - 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; - - 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 = invert( obj, opts ); - 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; - - 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 = invert( obj, opts ); - 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/docs/repl.txt b/lib/node_modules/@stdlib/utils/object-inverse/docs/repl.txt deleted file mode 100644 index 60ca7ca3602e..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/docs/repl.txt +++ /dev/null @@ -1,53 +0,0 @@ - -{{alias}}( obj[, options] ) - Inverts an object, such that keys become values and values become keys. - - Beware when providing objects having values which are themselves objects. - The function relies on native object serialization (`#toString`) when - converting values to keys. - - Insertion order is not guaranteed, as object key enumeration is not - specified according to the ECMAScript specification. In practice, however, - most engines use insertion order to sort an object's keys, thus allowing for - deterministic inversion. - - Parameters - ---------- - obj: 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`. - - Returns - ------- - out: Object - Inverted object. - - Examples - -------- - // Basic usage: - > var obj = { 'a': 'beep', 'b': 'boop' }; - > var out = {{alias}}( obj ) - { 'beep': 'a', 'boop': 'b' } - - // Duplicate values: - > obj = { 'a': 'beep', 'b': 'beep' }; - > out = {{alias}}( obj ) - { 'beep': [ 'a', 'b' ] } - - // Override duplicate values: - > obj = {}; - > obj.a = 'beep'; - > obj.b = 'boop'; - > obj.c = 'beep'; - > out = {{alias}}( obj, { 'duplicates': false } ) - { 'beep': 'c', 'boop': 'b' } - - See Also - -------- - diff --git a/lib/node_modules/@stdlib/utils/object-inverse/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/object-inverse/docs/types/index.d.ts deleted file mode 100644 index 0a4a90679b0d..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/docs/types/index.d.ts +++ /dev/null @@ -1,66 +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 Options { - /** - * Boolean indicating whether to store duplicate keys. - */ - duplicates?: boolean; -} - -/** -* Inverts an object, such that keys become values and values become keys. -* -* @param obj - input object -* @param options - function options -* @param options.duplicates - boolean indicating whether to store duplicate keys (default: true) -* @returns inverted object -* -* @example -* var out = invert({ -* 'a': 'beep', -* 'b': 'boop' -* }); -* // returns { 'beep': 'a', 'boop': 'b' } -* -* @example -* var out = invert({ -* 'a': 'beep', -* 'b': 'beep' -* }); -* // returns { 'beep': [ 'a', 'b' ] } -* -* @example -* var obj = {}; -* obj.a = 'beep'; -* obj.b = 'boop'; -* obj.c = 'beep'; // inserted after `a` -* -* var out = invert( obj, { -* 'duplicates': false -* }); -* // returns { 'beep': 'c', 'boop': 'b' } -*/ -declare function invert( obj: any, options?: Options ): any; - - -// EXPORTS // - -export = invert; diff --git a/lib/node_modules/@stdlib/utils/object-inverse/docs/types/test.ts b/lib/node_modules/@stdlib/utils/object-inverse/docs/types/test.ts deleted file mode 100644 index 32f5d363b272..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/docs/types/test.ts +++ /dev/null @@ -1,60 +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 invert = require( './index' ); - - -// TESTS // - -// The function returns an object... -{ - const obj = { - 'a': 'beep', - 'b': 'boop' - }; - invert( obj ); // $ExpectType any -} - -// The compiler throws an error if the function is provided a second argument which is not an object... -{ - const obj = { - 'a': 'beep', - 'b': 'boop' - }; - invert( obj, null ); // $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' - }; - invert( obj, { 'duplicates': '5' } ); // $ExpectError - invert( obj, { 'duplicates': 123 } ); // $ExpectError - invert( obj, { 'duplicates': null } ); // $ExpectError - invert( obj, { 'duplicates': [] } ); // $ExpectError - invert( obj, { 'duplicates': {} } ); // $ExpectError - invert( obj, { 'duplicates': ( x: number ): number => x } ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - invert(); // $ExpectError - invert( {}, 4, 16 ); // $ExpectError -} diff --git a/lib/node_modules/@stdlib/utils/object-inverse/examples/index.js b/lib/node_modules/@stdlib/utils/object-inverse/examples/index.js deleted file mode 100644 index 0db3a85eb318..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/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 randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var invert = require( './../lib' ); - -var keys; -var arr; -var out; -var i; - -// Create an array of random integers... -arr = new Array( 1000 ); -for ( i = 0; i < arr.length; i++ ) { - arr[ i ] = round( randu()*100.0 ); -} -// Invert the array to determine value frequency... -out = invert( arr ); -keys = objectKeys( out ); -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/lib/index.js b/lib/node_modules/@stdlib/utils/object-inverse/lib/index.js deleted file mode 100644 index 313f1705ad48..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/lib/index.js +++ /dev/null @@ -1,59 +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. -* -* @module @stdlib/utils/object-inverse -* -* @example -* var invert = require( '@stdlib/utils/object-inverse' ); -* -* var out = invert({ -* 'a': 'beep', -* 'b': 'boop' -* }); -* // returns { 'beep': 'a', 'boop': 'b' } -* -* out = invert({ -* 'a': 'beep', -* 'b': 'beep' -* }); -* // returns { 'beep': [ 'a', 'b' ] } -* -* var obj = {}; -* obj.a = 'beep'; -* obj.b = 'boop'; -* obj.c = 'beep'; // inserted after `a` -* -* out = invert( obj, { -* 'duplicates': false -* }); -* // 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/lib/main.js b/lib/node_modules/@stdlib/utils/object-inverse/lib/main.js deleted file mode 100644 index d9b0a9b969bc..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/lib/main.js +++ /dev/null @@ -1,123 +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 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. -* -* @param {ObjectLike} obj - input object -* @param {Options} [opts] - function options -* @param {boolean} [opts.duplicates=true] - boolean indicating whether to store duplicate keys -* @throws {TypeError} first argument must be object-like -* @throws {TypeError} second argument must an an object -* @throws {TypeError} must provide valid options -* @returns {Object} inverted object -* -* @example -* var out = invert({ -* 'a': 'beep', -* 'b': 'boop' -* }); -* // returns { 'beep': 'a', 'boop': 'b' } -* -* @example -* var out = invert({ -* 'a': 'beep', -* 'b': 'beep' -* }); -* // returns { 'beep': [ 'a', 'b' ] } -* -* @example -* var obj = {}; -* obj.a = 'beep'; -* obj.b = 'boop'; -* obj.c = 'beep'; // inserted after `a` -* -* var out = invert( obj, { -* 'duplicates': false -* }); -* // returns { 'beep': 'c', 'boop': 'b' } -*/ -function invert( obj, opts ) { - var allowDupes = true; - var keys; - var len; - var key; - var val; - var out; - var v; - var i; - if ( !isObjectLike( obj ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an object (except null). Value: `%s`.', obj ) ); - } - if ( arguments.length > 1 ) { - 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 ) ); - } - } - } - keys = objectKeys( obj ); - len = keys.length; - out = {}; - if ( allowDupes ) { - for ( i = 0; i < len; i++ ) { - key = keys[ i ]; - val = obj[ key ]; - 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 ]; - out[ obj[ key ] ] = key; - } - } - return out; -} - - -// EXPORTS // - -module.exports = invert; diff --git a/lib/node_modules/@stdlib/utils/object-inverse/package.json b/lib/node_modules/@stdlib/utils/object-inverse/package.json deleted file mode 100644 index fcca361da5cf..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/package.json +++ /dev/null @@ -1,69 +0,0 @@ -{ - "name": "@stdlib/utils/object-inverse", - "version": "0.0.0", - "description": "Invert an object, such that keys become values and values become keys.", - "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", - "object", - "keys", - "values", - "bijection", - "mapping", - "hash", - "array" - ] -} diff --git a/lib/node_modules/@stdlib/utils/object-inverse/test/test.js b/lib/node_modules/@stdlib/utils/object-inverse/test/test.js deleted file mode 100644 index abac4a13439b..000000000000 --- a/lib/node_modules/@stdlib/utils/object-inverse/test/test.js +++ /dev/null @@ -1,215 +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 invert = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof invert, '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() { - invert( 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() { - invert( {}, value ); - }; - } -}); - -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() { - invert( {}, { - 'duplicates': value - }); - }; - } -}); - -tape( 'the function returns an empty object if provided an empty object', function test( t ) { - t.deepEqual( invert( {} ), {}, 'returns empty object' ); - t.end(); -}); - -tape( 'the function inverts an object', function test( t ) { - var expected; - var actual; - var obj; - - 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 = invert( obj ); - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -}); - -tape( 'the function handles duplicate values', function test( t ) { - var expected; - var actual; - var obj; - - obj = { - 'a': 'beep', - 'b': 'beep', - 'c': 'boop', - 'd': 'boop', - 'e': 'boop' - }; - expected = { - 'beep': [ 'a', 'b' ], - 'boop': [ 'c', 'd', 'e' ] - }; - actual = invert( obj ); - - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -}); - -tape( 'the function supports overriding duplicate values', function test( t ) { - var expected; - var actual; - var obj; - - obj = { - 'a': 'beep', - 'b': 'beep' - }; - expected = { - 'beep': 'b' - }; - actual = invert( obj, { - 'duplicates': false - }); - - t.deepEqual( actual, expected, 'returns inverted object' ); - t.end(); -}); - -tape( 'the function ignores unknown options', function test( t ) { - var expected; - var actual; - var obj; - - obj = { - 'a': 'beep', - 'b': 'beep' - }; - expected = { - 'beep': [ 'a', 'b' ] - }; - actual = invert( obj, { - 'bee': 'bop' - }); - - t.deepEqual( actual, expected, 'ignores unknown options' ); - t.end(); -});