diff --git a/lib/node_modules/@stdlib/utils/every-in-by/README.md b/lib/node_modules/@stdlib/utils/every-in-by/README.md new file mode 100644 index 000000000000..706362cafd15 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/README.md @@ -0,0 +1,127 @@ + + +# everyInBy + +> Test whether all properties (own and inherited) of an object pass a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var everyInBy = require( '@stdlib/utils/every-in-by' ); +``` + +#### everyInBy( object, predicate\[, thisArg ] ) + +Tests whether all properties (own and inherited) of an `object` pass a test implemented by a `predicate` function. + +```javascript +var o; +var bool; + +function isPositive( v ) { + return ( v > 0 ); +} + +o = { + 'a': 1, + 'b': 2, + 'c': 3 +}; + +bool = everyInBy( o, isPositive ); +// returns true +``` + +If provided an empty `object`, the function returns `true`. + +```javascript +function isPositive(v) { + return ( v > 0 ); +} + +var bool = everyInBy( {}, isPositive ); +// returns true +``` + +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var everyInBy = require( '@stdlib/utils/every-in-by' ); + +var bool; +var o; +var i; + +function isPositive(v) { + return ( v > 0 ); +} + +o = {}; +for ( i = 0; i < 100; i++ ) { + o[ i ] = ( randu() < 0.95 ); +} + +bool = everyInBy( o, isPositive ); +// returns +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/every-in-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/every-in-by/benchmark/benchmark.js new file mode 100644 index 000000000000..bb88a5b4f78e --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/benchmark/benchmark.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var everyInBy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var i; + + function predicate( v ) { + return !isnan( v ); + } + + obj = { + 'a': 'beep', + 'b': 'boop', + 'c': 'foo', + 'd': 'bar', + 'e': randu() + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.e = randu(); + bool = everyInBy( obj, predicate ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/utils/every-in-by/docs/repl.txt b/lib/node_modules/@stdlib/utils/every-in-by/docs/repl.txt new file mode 100644 index 000000000000..3eb1df9cd9a5 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/docs/repl.txt @@ -0,0 +1,43 @@ + +{{alias}}( object, predicate[, thisArg ] ) + Test whether all properties (own and inherited) of an object pass a + test implemented by a predicate function. + + The predicate function is provided three arguments: + + - `value`: object value + - `key`: object key + - `object`: the input object + + The function immediately returns upon encountering a non-truthy return + value. + + If provided an empty object, the function returns `true`. + + Parameters + ---------- + object: Object + Input object over which to iterate. + + predicate: Function + Test function. + + thisArg: any (optional) + Execution context. + + Returns + ------- + bool: boolean + The function returns `true` if the predicate function returns a truthy + value for all elements; otherwise, the function returns `false`. + + Examples + -------- + > function positive( v ) { return ( v > 0 ); }; + > var o = {a: 1, b: 2, c: 3}; + > var bool = {{alias}}( o, positive ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/utils/every-in-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/every-in-by/docs/types/index.d.ts new file mode 100644 index 000000000000..10bf34b8c027 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/docs/types/index.d.ts @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 { Collection } from '@stdlib/types/object'; + +/** +* Checks whether an element in a collection passes a test. +* +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Nullary = ( this: U ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @param index - collection index +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Binary = ( this: U, value: T, index: number ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @param index - collection index +* @param collection - input collection +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Ternary = ( this: U, value: T, index: number, collection: Collection ) => boolean; + +/** +* Checks whether an element in a collection passes a test. +* +* @param value - collection value +* @param index - collection index +* @param collection - input collection +* @returns boolean indicating whether an element in a collection passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** + * Checks whether all own and inherited properties in an object pass a test implemented by a predicate function. + * + * @param obj - The object to iterate over. + * @param predicate - The test function to apply to each property. + * @param thisArg - Optional execution context for the predicate function. + * @returns boolean indicating whether all properties pass the test. + * + * @throws TypeError if `obj` is not an object or if `predicate` is not a function. + */ +declare function everyInBy( + obj: object, + predicate: Predicate, + thisArg?: ThisParameterType> +): boolean; + + +// EXPORTS // + +export = everyInBy; diff --git a/lib/node_modules/@stdlib/utils/every-in-by/docs/types/test.ts b/lib/node_modules/@stdlib/utils/every-in-by/docs/types/test.ts new file mode 100644 index 000000000000..5a757d9aba32 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/docs/types/test.ts @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 everyInBy = require( './index' ); + + +// TESTS // + +function isPositive( v: number ): boolean { + if ( typeof v !== 'object' || v === null ) { + throw new TypeError( 'Expected an object' ); + } + + return v > 0; +} + + +// The function returns a boolean... +{ + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, isPositive ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a first argument which is not a object... +{ + everyInBy( 2 ); // $ExpectError + everyInBy( false ); // $ExpectError + everyInBy( true ); // $ExpectError + everyInBy( [1, 2, 3, 4] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, 2 ); // $ExpectError + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, false ); // $ExpectError + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, true ); // $ExpectError + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, 'abc' ); // $ExpectError + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, {} ); // $ExpectError + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + everyInBy(); // $ExpectError + everyInBy( { 'a': 1, 'b': 2, 'c': 3} ); // $ExpectError + everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, {}, 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/every-in-by/examples/index.js b/lib/node_modules/@stdlib/utils/every-in-by/examples/index.js new file mode 100644 index 000000000000..9cc41d83f966 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 randu = require( '@stdlib/random/base/randu' ); +var everyInBy = require( './../lib' ); + +function isPositive( value ) { + return ( value > 0 ); +} + +var o; +var i; +var bool; + +o = {}; +for ( i = 0; i < 100; i++ ) { + o[ i ] = randu(); +} + +bool = everyInBy( o, isPositive ); +console.log( bool ); diff --git a/lib/node_modules/@stdlib/utils/every-in-by/lib/index.js b/lib/node_modules/@stdlib/utils/every-in-by/lib/index.js new file mode 100644 index 000000000000..1231584d24f2 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Test whether all elements in an object pass a test implemented by a predicate function. +* +* @module @stdlib/utils/every-in-by +* +* @example +* var everyInBy = require( '@stdlib/utils/every-in-by' ); +* +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var o = { a: 1, b: 2, c: 3 } +* +* var bool = everyInBy( o, isPositive ) +* // returns true +* +* o.a = -1 +* +* bool = everyInBy( o, isPositive ) +* // returns false +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/every-in-by/lib/main.js b/lib/node_modules/@stdlib/utils/every-in-by/lib/main.js new file mode 100644 index 000000000000..71f3818cb10f --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isFunction = require( '@stdlib/assert/is-function' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Tests whether all properties (own and inherited) of an object pass a test implemented by a predicate function. +* +* @param {Object} obj - input object +* @param {Function} predicate - test function +* @param {*} [thisArg] - execution context +* @throws {TypeError} first argument must be an object +* @throws {TypeError} second argument must be a function +* @returns {boolean} boolean indicating whether all elements pass a test +* +* @example +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var o = { a: 1, b: 2, c: 3 }; +* +* var bool = everyInBy( o, isPositive ); +* // returns true +*/ +function everyInBy( obj, predicate, thisArg ) { + var key; + if (typeof obj !== 'object' || obj === null) { + throw new TypeError( format( 'invalid argument. First argument must be an object. Value: `%s`.', obj ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', predicate ) ); + } + + for ( key in obj ) { + if ( !predicate.call( thisArg, obj[ key ], key, obj ) ) { + return false; + } + } + + return true; +} + + +// EXPORTS // + +module.exports = everyInBy; diff --git a/lib/node_modules/@stdlib/utils/every-in-by/package.json b/lib/node_modules/@stdlib/utils/every-in-by/package.json new file mode 100644 index 000000000000..78946ae8449b --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/utils/every-in-by", + "version": "0.0.0", + "description": "Test whether all properties (own and inherited) of an object pass a test implemented by a predicate 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", + "test", + "predicate", + "every", + "all", + "object", + "iterate", + "validate" + ] +} diff --git a/lib/node_modules/@stdlib/utils/every-in-by/test/test.js b/lib/node_modules/@stdlib/utils/every-in-by/test/test.js new file mode 100644 index 000000000000..5cd9af8113ff --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-in-by/test/test.js @@ -0,0 +1,178 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 everyInBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof everyInBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided 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 a type error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + everyInBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if not provided a predicate function', function test( t ) { + var values; + var i; + + values = [ '5', 5, NaN, true, false, null, void 0, {}, [], /.*/, new Date() ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + everyInBy( {}, value ); + }; + } +}); + +tape( 'if provided an empty object, the function returns `true`', function test( t ) { + var bool; + var obj; + + obj = {}; + bool = everyInBy( obj, noop ); + + t.strictEqual( bool, true, 'returns true for empty object' ); + t.end(); +}); + +tape( 'the function returns `true` if all properties pass a test', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 1, + 'b': 2, + 'c': 3 + }; + + function isPositive( value ) { + return value > 0; + } + + bool = everyInBy( obj, isPositive ); + + t.strictEqual( bool, true, 'returns true for all positive values' ); + t.end(); +}); + +tape( 'the function returns `false` if one or more properties fail a test', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 1, + 'b': -2, + 'c': 3 + }; + + function isPositive( value ) { + return value > 0; + } + + bool = everyInBy( obj, isPositive ); + + t.strictEqual( bool, false, 'returns false for negative value' ); + t.end(); +}); + +tape( 'the function considers inherited properties', function test( t ) { + var bool; + var obj; + + function Parent() { + this.inherited = 'property'; + } + + obj = new Parent(); + obj.own = 'value'; + + function hasBoth( value, key ) { + return key === 'inherited' || key === 'own'; + } + + bool = everyInBy( obj, hasBoth ); + + t.strictEqual( bool, true, 'returns true for inherited and own properties' ); + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var bool; + var ctx; + var obj; + + function sum( value ) { + // eslint-disable-next-line no-invalid-this + this.sum += value; + return true; + } + + ctx = { + 'sum': 0 + }; + + obj = { + 'a': 1, + 'b': 2, + 'c': 3 + }; + + bool = everyInBy( obj, sum, ctx ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +});