diff --git a/lib/node_modules/@stdlib/utils/any-in-by/README.md b/lib/node_modules/@stdlib/utils/any-in-by/README.md new file mode 100644 index 000000000000..dfc930a52c3a --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/README.md @@ -0,0 +1,252 @@ + + +# anyInBy + +> Test whether at least one property in an object passes a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var anyInBy = require( '@stdlib/utils/any-in-by' ); +``` + +#### anyInBy( collection, predicate\[, thisArg ] ) + +Tests whether at least one property in an `object` passes a test implemented by a `predicate` function. + +```javascript +function isNegative( value ) { + return ( value < 0 ); +} + +var obj = { + 'a': 1, + 'b': 2, + 'c': -1 +}; + +var bool = anyInBy( 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 +}; + +var bool = anyInBy( obj, isPositive ); +// returns true +``` + +The invoked `function` is provided three arguments: + +- `value`: collection element +- `index`: collection index +- `collection`: input collection + +To set the function execution context, provide a `thisArg`. + +```javascript +function sum( value ) { + this.sum += value; + this.count += 1; + return ( value < 0 ); +} + +var obj = { + 'a': 1, + 'b': 2, + 'c': 3, + 'd': -4 +}; + +var context = { + 'sum': 0, + 'count': 0 +}; + +var bool = anyInBy( obj, sum, context ); +// returns true + +var mean = context.sum / context.count; +// returns 0.5 +``` + +
+ + + + + +
+ +## Notes + +- The `obj` object may contain own properties as well as inherited properties from its prototype chain. + +- If provided an empty collection, the function returns `false`. + + ```javascript + function alwaysTrue() { + return true; + } + var bool = anyInBy( {}, alwaysTrue ); + // returns false + ``` + +- The function differs from [`Array.prototype.some`][mdn-array-some] in the following ways: + + - The function does **not** skip `undefined` elements. + + + + ```javascript + function log( value, index ) { + console.log( '%s: %s', index, value ); + return ( value < 0 ); + } + + var arr = [ 1, , , 4, -1 ]; + + var bool = anyInBy( arr, log ); + /* => + 0: 1 + 1: undefined + 2: undefined + 3: 4 + 4: -1 + */ + ``` + + - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution). + +
+ + + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var anyInBy = require( '@stdlib/utils/any-in-by' ); + +function threshold( value ) { + return ( value > 0.95 ); +} + +var bool; +var obj = {}; +var i; + +for ( i = 0; i < 100; i++ ) { + obj[i] = randu(); +} + +bool = anyInBy( obj, threshold ); +// returns +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/any-in-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/any-in-by/benchmark/benchmark.js new file mode 100644 index 000000000000..b6a24896daf0 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/benchmark/benchmark.js @@ -0,0 +1,136 @@ +/** +* @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 pkg = require( './../package.json' ).name; +var anyInBy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var i; + + function predicate( v ) { + return isnan( v ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'a': i, + 'b': i+1, + 'c': i+2, + 'd': i+3, + 'e': i+4, + 'f': NaN + }; + bool = anyInBy( 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(); +}); + +bench( pkg+'::built-in', function benchmark( b ) { + var bool; + var keys; + var obj; + var i; + + function predicate( v ) { + return isnan( v ); + } + function testPredicate(key) { + return predicate(obj[key]); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'a': i, + 'b': i+1, + 'c': i+2, + 'd': i+3, + 'e': i+4, + 'f': NaN + }; + keys = Object.keys( obj ); + bool = keys.some(testPredicate); + 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(); +}); + +bench( pkg+'::loop', function benchmark( b ) { + var bool; + var keys; + var obj; + var i; + var j; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'a': i, + 'b': i+1, + 'c': i+2, + 'd': i+3, + 'e': i+4, + 'f': NaN + }; + keys = Object.keys( obj ); + bool = false; + for ( j = 0; j < keys.length; j++ ) { + if ( isnan( obj[ keys[j] ] ) ) { + bool = true; + break; + } + } + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should be a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/utils/any-in-by/benchmark/julia/REQUIRE b/lib/node_modules/@stdlib/utils/any-in-by/benchmark/julia/REQUIRE new file mode 100644 index 000000000000..98645e192e41 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/benchmark/julia/REQUIRE @@ -0,0 +1,2 @@ +julia 1.5 +BenchmarkTools 0.5.0 diff --git a/lib/node_modules/@stdlib/utils/any-in-by/benchmark/julia/benchmark.jl b/lib/node_modules/@stdlib/utils/any-in-by/benchmark/julia/benchmark.jl new file mode 100644 index 000000000000..22297511618d --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/benchmark/julia/benchmark.jl @@ -0,0 +1,144 @@ +#!/usr/bin/env julia + +# @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 BenchmarkTools +using Printf + +# Benchmark variables: +name = "any-in-by"; +repeats = 3; + +""" + print_version() + + Prints the TAP version. + + # Examples + + ``` julia + julia> print_version() + ``` +""" +function print_version() + @printf( "TAP version 13\n" ); +end + +""" + print_summary( total, passing ) + + Print the benchmark summary. + + # Arguments + + - `total`: total number of tests + - `passing`: number of passing tests + + # Examples + + ``` julia + julia> print_summary( 3, 3 ) + ``` +""" +function print_summary( total, passing ) + @printf( "#\n" ); + @printf( "1..%d\n", total ); # TAP plan + @printf( "# total %d\n", total ); + @printf( "# pass %d\n", passing ); + @printf( "#\n" ); + @printf( "# ok\n" ); +end + +""" + print_results( iterations, elapsed ) + + Print benchmark results. + + # Arguments + + - `iterations`: number of iterations + - `elapsed`: elapsed time (in seconds) + + # Examples + + ``` julia + julia> print_results( 1000000, 0.131009101868 ) + ``` +""" +function print_results( iterations, elapsed ) + rate = iterations / elapsed + + @printf( " ---\n" ); + @printf( " iterations: %d\n", iterations ); + @printf( " elapsed: %0.9f\n", elapsed ); + @printf( " rate: %0.9f\n", rate ); + @printf( " ...\n" ); +end + +""" + benchmark() + + Run a benchmark. + + # Notes + + - Benchmark results are returned as a two-element array: [ iterations, elapsed ]. + - The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. + - The elapsed time is in seconds. + + # Examples + + ``` julia + julia> out = benchmark(); + ``` +""" +function benchmark() + t = BenchmarkTools.@benchmark any( v -> ( isnan(v) ), [ 1, 2, 3, 4, 5, NaN ] ) samples=1e6 + + # Compute the total "elapsed" time and convert from nanoseconds to seconds: + s = sum( t.times ) / 1.0e9; + + # Determine the number of "iterations": + iter = length( t.times ); + + # Return the results: + [ iter, s ]; +end + +""" + main() + + Run benchmarks. + + # Examples + + ``` julia + julia> main(); + ``` +""" +function main() + print_version(); + for i in 1:repeats + @printf( "# julia::%s\n", name ); + results = benchmark(); + print_results( results[ 1 ], results[ 2 ] ); + @printf( "ok %d benchmark finished\n", i ); + end + print_summary( repeats, repeats ); +end + +main(); diff --git a/lib/node_modules/@stdlib/utils/any-in-by/docs/repl.txt b/lib/node_modules/@stdlib/utils/any-in-by/docs/repl.txt new file mode 100644 index 000000000000..8caa697c9354 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/docs/repl.txt @@ -0,0 +1,41 @@ +{{alias}}( collection, predicate[, thisArg ] ) + Tests whether at least one element in a collection passes a test implemented + by a predicate function. + + The predicate function is provided three arguments: + + - `value`: collection value + - `index`: collection index + - `collection`: the input collection + + The function immediately returns upon encountering a truthy return value. + + If provided an empty collection, the function returns `false`. + + Parameters + ---------- + collection: Array|TypedArray|Object + Input collection over which to iterate. If provided an object, the + object must be array-like (excluding strings and functions). + + predicate: Function + The test function. + + thisArg: any (optional) + Execution context. + + Returns + ------- + bool: boolean + The function returns `true` if the predicate function returns `true` for + any element; otherwise, the function returns `false`. + + Examples + -------- + > function negative( v ) { return ( v < 0 ); }; + > var obj = { a: 1, b: -2, c: 3, d: 4 }; + > var bool = {{alias}}( obj, negative ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/utils/any-in-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/any-in-by/docs/types/index.d.ts new file mode 100644 index 000000000000..ebda224d8e44 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/docs/types/index.d.ts @@ -0,0 +1,103 @@ +/* +* @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 + +/// + +import { Collection } from '@stdlib/types/array'; + +/** +* 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; + +/** +* Tests whether at least one property in an object passes a test implemented by a predicate function. +* +* ## Notes +* +* - The predicate function is provided three arguments: +* +* - `value`: property value +* - `key`: property key +* - `obj`: the input object +* +* - The function immediately returns upon encountering a truthy return value. +* +* - If provided an empty object, the function returns `false`. +* +* @param obj - input object +* @param predicate - test function +* @param thisArg - execution context +* @returns boolean indicating whether at least one property passes a test +* +* @example +* function isNegative( v ) { +* return ( v < 0 ); +* } +* +* var obj = { 'a': 1, 'b': -2, 'c': 3 }; +* +* var bool = anyInBy( obj, isNegative ); +* // returns true +*/ +declare function anyInBy( obj: { [key: string]: T }, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + +// EXPORTS // + +export = anyInBy; diff --git a/lib/node_modules/@stdlib/utils/any-in-by/docs/types/test.ts b/lib/node_modules/@stdlib/utils/any-in-by/docs/types/test.ts new file mode 100644 index 000000000000..d02cb0e8eb14 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/docs/types/test.ts @@ -0,0 +1,53 @@ +/* +* @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 anyInBy = require( './index' ); + +const obj = { + 'a': 1, + 'b': 2, + 'c': 3 +}; + +const isPositive = ( v: number ): boolean => { + return ( v > 0 ); +}; + +// TESTS // + +// The function returns a boolean... +{ + anyInBy( obj, isPositive ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + anyInBy( obj, 2 ); // $ExpectError + anyInBy( obj, false ); // $ExpectError + anyInBy( obj, true ); // $ExpectError + anyInBy( obj, 'abc' ); // $ExpectError + anyInBy( obj, {} ); // $ExpectError + anyInBy( obj, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + anyInBy(); // $ExpectError + anyInBy( obj ); // $ExpectError + anyInBy( obj, isPositive, {}, 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/any-in-by/examples/index.js b/lib/node_modules/@stdlib/utils/any-in-by/examples/index.js new file mode 100644 index 000000000000..b32cfcda087a --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-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 anyInBy = require( './../lib' ); + +function threshold( value ) { + return ( value > 0.95 ); +} + +var bool; +var obj = {}; +var i; + +for ( i = 0; i < 100; i++ ) { + obj[i] = randu(); +} + +bool = anyInBy( obj, threshold ); +console.log( bool ); diff --git a/lib/node_modules/@stdlib/utils/any-in-by/lib/index.js b/lib/node_modules/@stdlib/utils/any-in-by/lib/index.js new file mode 100644 index 000000000000..b2c13d828597 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/lib/index.js @@ -0,0 +1,50 @@ +/** +* @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 at least one property in an object passes a test implemented by a predicate function. +* +* @module @stdlib/utils/any-in-by +* +* @example +* var anyInBy = require( '@stdlib/utils/any-in-by' ); +* +* function isNegative( v ) { +* return ( v < 0 ); +* } +* +* var obj = { +* a: 1, +* b: 2, +* c: -1 +* }; +* +* var bool = anyInBy( obj, isNegative ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/any-in-by/lib/main.js b/lib/node_modules/@stdlib/utils/any-in-by/lib/main.js new file mode 100644 index 000000000000..a3f4234279a6 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/lib/main.js @@ -0,0 +1,76 @@ +/** +* @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 at least one own or inherited property of an object passes 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 at least one property passes a test +* +* @example +* function isNegative( v ) { +* return ( v < 0 ); +* } +* +* var obj = { +* 'a': 1, +* 'b': -1, +* 'c': 3 +* }; +* +* var bool = anyInBy( obj, isNegative ); +* // returns true +*/ +function anyInBy( obj, predicate, thisArg ) { + 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 ) && predicate.call( thisArg, obj[key], key, obj ) ) { + return true; + } + } + return false; +} + + +// EXPORTS // + +module.exports = anyInBy; diff --git a/lib/node_modules/@stdlib/utils/any-in-by/package.json b/lib/node_modules/@stdlib/utils/any-in-by/package.json new file mode 100644 index 000000000000..e330ccab7d50 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/utils/any-in-by", + "version": "0.0.0", + "description": "Test whether at least one property in an object passes 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", + "any", + "array.some", + "iterate", + "collection", + "array-like", + "validate" + ] + } + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/utils/any-in-by/test/test.js b/lib/node_modules/@stdlib/utils/any-in-by/test/test.js new file mode 100644 index 000000000000..ca25f96d4a24 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/any-in-by/test/test.js @@ -0,0 +1,132 @@ +/* +* @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 anyInBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof anyInBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a 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() { + anyInBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if not provided a 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() { + anyInBy( value, noop ); + }; + } +}); + +tape( 'the function returns `true` if at least one own property passes a test', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 20, + 'b': 22, + 'c': 25 + }; + + function overAge( value ) { + return ( value > 18 ); + } + + bool = anyInBy( obj, overAge ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if one or more own properties pass a test', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 10, + 'b': 12, + 'c': 15 + }; + + function underAge( value ) { + return ( value < 18 ); + } + + bool = anyInBy( obj, underAge ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +});