diff --git a/lib/node_modules/@stdlib/utils/any-own-by/README.md b/lib/node_modules/@stdlib/utils/any-own-by/README.md new file mode 100644 index 00000000000..e5a9a250e6e --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/README.md @@ -0,0 +1,206 @@ + + +# anyOwnBy + +> Test whether at least one own property of a provided object passes a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var anyOwnBy = require( '@stdlib/utils/any-own-by' ); +``` + +#### anyBy( collection, predicate\[, thisArg ] ) + +Tests whether at least one own property of a provided [`object`][mdn-object] passes a test implemented by a `predicate` function. + +```javascript +function isNegative( value ) { + return ( value < 0 ); +} + +var obj = { + 'a': 1, + 'b': 2, + 'c': 3, + 'd': -24, + 'e': 12 +}; + +var bool = anyOwnBy( obj, isNegative ); +// returns true +``` + +If a `predicate` function returns a truthy value, the function **immediately** returns `true`. + +```javascript +function isPositive( value ) { + if ( value < 0 ) { + throw new Error( 'should never reach this line' ); + } + return ( value > 0 ); +} + +var obj = { + 'a': 1, + 'b': 2, + 'c': 3, + 'd': -24, + 'e': 12 +}; + +var bool = anyOwnBy( obj, isPositive ); +// returns true +``` + +The invoked `function` is provided three arguments: + +- `value`: property value +- `key`: property key +- `obj`: input object + +To set the function execution context, provide a `thisArg`. + +```javascript +function verify( value ) { + this.sum += value; + this.count += 1; + return ( value > 0 ); +} + +var obj = { + 'a': -1, + 'b': -2, + 'c': 3, + 'd': -14 +}; + +var context = { + 'sum': 0, + 'count': 0 +}; + +var bool = anyOwnBy( obj, verify, context ); +// returns true + +var mean = context.sum / context.count; +// returns 0 +``` + +
+ + + + + +
+ +## Notes + +- If provided an empty object, the function returns `false`. + + ```javascript + function verify() { + return true; + } + var bool = anyOwnBy( {}, verify ); + // returns false + ``` + +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var anyOwnBy = require( '@stdlib/utils/any-own-by' ); + +function threshold( value ) { + return ( value > 0.94 ); +} + +var bool; +var obj = {}; +var keys = [ 'a', 'b', 'c', 'd', 'e' ]; +var i; +for ( i = 0; i < keys.length; i++ ) { + obj[ keys[ i ] ] = randu(); +} + +bool = anyOwnBy( obj, threshold ); +// returns +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/any-own-by/docs/repl.txt b/lib/node_modules/@stdlib/utils/any-own-by/docs/repl.txt new file mode 100644 index 00000000000..af11067f69d --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/docs/repl.txt @@ -0,0 +1,42 @@ +{{alias}}( object, predicate[, thisArg ] ) + Tests whether at least one own property of an object passes a + test implemented by a predicate function. + + The predicate function is provided three arguments: + + - `value`: property value + - `index`: property key + - `object`: the input object + + The function immediately returns upon encountering a truthy return + value. + + If provided an empty object, the function returns `false`. + + Parameters + ---------- + object: Object + Input object. + + 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 one own property; otherwise, the function returns `false`. + + Examples + -------- + > function positive( v ) { return ( v > 0 ); }; + > var obj = { 'a': -1, 'b': 2, 'c': -3 }; + > var bool = {{alias}}( obj, positive ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/utils/any-own-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/any-own-by/docs/types/index.d.ts new file mode 100644 index 00000000000..fe0dac85b4e --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* Checks whether an own property of the object passes the test. +* +* @returns boolean indicating whether an own property of the object passes the test +*/ +type Nullary = ( this: U ) => boolean; + +/** +* Checks whether an own property of the object passes the test. +* +* @param value - collection value +* @returns boolean indicating whether an own property of the object passes the test +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Checks whether an own property of the object passes the test. +* +* @param value - property value +* @param key - property key +* @returns boolean indicating whether an own property of the object passes the test +*/ +type Binary = ( this: U, value: T, key: number ) => boolean; + +/** +* Checks whether an own property of the object passes the test. +* +* @param value - property value +* @param key - property key +* @param object - input object +* @returns boolean indicating whether an own property of the object passes the test +*/ +type Ternary = ( this: U, value: T, key: number, object: Object ) => boolean; + +/** +* Checks whether an own property of the object passes the test. +* +* @param value - property value +* @param key - property key +* @param object - input object +* @returns boolean indicating whether an own property of the object passes the tests +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* Tests whether any property of an object passes a test implemented by a predicate function. +* +* ## Notes +* +* - The predicate function is provided three arguments: +* +* - `value`: property value +* - `key`: property key +* - `object`: the input object +* +* - The function immediately returns upon encountering a truthy return value. +* - If provided an empty object, the function returns `false`. +* +* @param object - input object +* @param predicate - test function +* @param thisArg - execution context +* @returns boolean indicating whether any own property pass a test +* +* @example +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var obj = { 'a': -1, 'b': 2, 'c': -3 }; +* +* var bool = anyOwnBy( obj, isPositive ); +* // returns true +*/ +declare function anyOwnBy( object: Record, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + + +// EXPORTS // + +export = anyOwnBy; diff --git a/lib/node_modules/@stdlib/utils/any-own-by/docs/types/test.ts b/lib/node_modules/@stdlib/utils/any-own-by/docs/types/test.ts new file mode 100644 index 00000000000..bb87632c542 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/docs/types/test.ts @@ -0,0 +1,63 @@ +/* +* @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 anyOwnBy = require( './index' ); + +const isPositive = ( v: number ): boolean => { + return ( v > 0 ); +}; + +const obj = { + 'a':-1, + 'b':2, + 'c':-3 +}; + +// TESTS // + +// The function returns a boolean... +{ + anyOwnBy( obj, isPositive ); // $ExpectType boolean + anyOwnBy( obj, isPositive ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a first argument which is not an object... +{ + anyOwnBy( 2, isPositive ); // $ExpectError + anyOwnBy( false, isPositive ); // $ExpectError + anyOwnBy( true, isPositive ); // $ExpectError + anyOwnBy( [ 1, 2 ], isPositive ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + anyOwnBy( obj , 2 ); // $ExpectError + anyOwnBy( obj , false ); // $ExpectError + anyOwnBy( obj , true ); // $ExpectError + anyOwnBy( obj , 'abc' ); // $ExpectError + anyOwnBy( obj , {} ); // $ExpectError + anyOwnBy( obj , [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + anyOwnBy(); // $ExpectError + anyOwnBy( [ 1, 2, 3 ] ); // $ExpectError + anyOwnBy( [ 1, 2, 3 ], isPositive, {}, 3 ); // $ExpectError +} + diff --git a/lib/node_modules/@stdlib/utils/any-own-by/examples/index.js b/lib/node_modules/@stdlib/utils/any-own-by/examples/index.js new file mode 100644 index 00000000000..d95d8d725a9 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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 anyOwnBy = require( './../lib' ); + +function threshold( value ) { + return ( value > 0.94 ); +} + +var bool; +var obj = {}; +var keys = [ 'a', 'b', 'c', 'd', 'e' ]; +var i; +for ( i = 0; i < keys.length; i++ ) { + obj[ keys[ i ] ] = randu(); +} + +bool = anyOwnBy( obj, threshold ); +console.log( bool ); diff --git a/lib/node_modules/@stdlib/utils/any-own-by/lib/index.js b/lib/node_modules/@stdlib/utils/any-own-by/lib/index.js new file mode 100644 index 00000000000..9da6bcd2dca --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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 any 'own' property of a provided object satisfies a predicate function. +* +* @module @stdlib/utils/any-own-by +* +* @example +* var anyOwnBy = require( '@stdlib/utils/any-own-by' ); +* +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var obj = { 'a': -1, 'b': 2, 'c': -3 }; +* +* var bool = anyOwnBy( obj, isPositive ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/any-own-by/lib/main.js b/lib/node_modules/@stdlib/utils/any-own-by/lib/main.js new file mode 100644 index 00000000000..1c3b30a1cb6 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/lib/main.js @@ -0,0 +1,77 @@ +/** +* @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 isObject = require( '@stdlib/assert/is-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Tests whether any 'own' property of a provided object satisfies 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 returned indicating whether any own property of an object pass a test +* +* @example +* var anyOwnBy = require( '@stdlib/utils/any-own-by' ); +* +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var obj = { 'a': -1, 'b': 2, 'c': -3 }; +* +* var bool = anyOwnBy( obj, isPositive ); +* // returns true +*/ +function anyOwnBy( obj, predicate, thisArg ) { + var result; + var key; + if ( !isObject( obj ) ) { + 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 ( hasOwnProp( obj, key ) ) { + result = predicate.call( thisArg, obj[ key ], key, obj ); + if ( result ) { + return true; + } + } + } + return false; +} + + +// EXPORTS // + +module.exports = anyOwnBy; diff --git a/lib/node_modules/@stdlib/utils/any-own-by/package.json b/lib/node_modules/@stdlib/utils/any-own-by/package.json new file mode 100644 index 00000000000..8fdc7bf41d9 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/utils/any-own-by", + "version": "0.0.0", + "description": "Test whether whether any 'own' property of a provided object satisfies 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", + "any", + "iterate", + "object", + "property", + "properties", + "props", + "keys", + "obj", + "validate" + ] + } \ No newline at end of file diff --git a/lib/node_modules/@stdlib/utils/any-own-by/test/test.js b/lib/node_modules/@stdlib/utils/any-own-by/test/test.js new file mode 100644 index 00000000000..8ad76464f0b --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-own-by/test/test.js @@ -0,0 +1,180 @@ +/** +* @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 anyOwnBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof anyOwnBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {}, + /.*/, + new Date() + ]; + for ( i =0; i < values.length; i++ ) { + t.throws( badValue( values ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + anyOwnBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not 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() { + anyOwnBy( {}, value ); + }; + } +}); + +tape( 'if provided an empty object, the function returns `false`', function test( t ) { + var bool; + var obj; + + function foo() { + t.fail( 'should not be invoked' ); + } + obj = {}; + bool = anyOwnBy( obj, foo ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if any one property pass a test', function test( t ) { + var bool; + var obj; + + obj = { + 'a': -1, + 'b': 2, + 'c': -3 + }; + + function isPositive( v ) { + return ( v > 0 ); + } + + bool = anyOwnBy( obj, isPositive ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if no properties pass a test', function test( t ) { + var bool; + var obj; + + obj = { + 'a': -1, + 'b': -2, + 'c': -3, + 'd': -34 + }; + + function isPositive( v ) { + return ( v > 0 ); + } + + bool = anyOwnBy( obj, isPositive ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var bool; + var ctx; + var obj; + + function verify( value ) { + /* eslint-disable no-invalid-this */ + this.sum += value; + this.count += 1; + return ( value > 0 ); + } + + ctx = { + 'sum': 0, + 'count': 0 + }; + + obj = { + 'a': -1, + 'b': -2, + 'c': 3, + 'd': -14 + }; + + bool = anyOwnBy( obj, verify, ctx ); + + t.strictEqual( bool, true, 'returns true' ); + t.strictEqual( ctx.sum/ctx.count, 0, 'expected result' ); + + t.end(); +});