From 365ae8ed8e22407ce8c777f367cf1a08c704656f Mon Sep 17 00:00:00 2001 From: Praneki <97080887+PraneGIT@users.noreply.github.com> Date: Mon, 4 Mar 2024 04:41:54 +0530 Subject: [PATCH] feat: add `utils/every-own-by` PR-URL: #1413 Closes: #818 --------- Signed-off-by: Philipp Burckhardt Co-authored-by: Philipp Burckhardt Reviewed-by: Aditya Sapra <110766802+adityacodes30@users.noreply.github.com> Reviewed-by: Philipp Burckhardt --- .../@stdlib/utils/every-own-by/README.md | 207 ++++++++++++++++++ .../utils/every-own-by/benchmark/benchmark.js | 97 ++++++++ .../every-own-by/benchmark/julia/REQUIRE | 2 + .../every-own-by/benchmark/julia/benchmark.jl | 144 ++++++++++++ .../@stdlib/utils/every-own-by/docs/repl.txt | 43 ++++ .../utils/every-own-by/docs/types/index.d.ts | 102 +++++++++ .../utils/every-own-by/docs/types/test.ts | 62 ++++++ .../utils/every-own-by/examples/index.js | 37 ++++ .../@stdlib/utils/every-own-by/lib/index.js | 46 ++++ .../@stdlib/utils/every-own-by/lib/main.js | 77 +++++++ .../@stdlib/utils/every-own-by/package.json | 69 ++++++ .../@stdlib/utils/every-own-by/test/test.js | 180 +++++++++++++++ 12 files changed, 1066 insertions(+) create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/README.md create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/benchmark/julia/REQUIRE create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/benchmark/julia/benchmark.jl create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/package.json create mode 100644 lib/node_modules/@stdlib/utils/every-own-by/test/test.js diff --git a/lib/node_modules/@stdlib/utils/every-own-by/README.md b/lib/node_modules/@stdlib/utils/every-own-by/README.md new file mode 100644 index 00000000000..d3eb43ddfcf --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/README.md @@ -0,0 +1,207 @@ + + +# everyOwnBy + +> Test whether all own propertes of an object pass a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var everyOwnBy = require( '@stdlib/utils/every-own-by' ); +``` + +#### everyOwnBy( object, predicate\[, thisArg ] ) + +Tests whether all `own` properties of an object pass a test implemented by a `predicate` function. + +```javascript +function isPositive( value ) { + return ( value > 0 ); +} + +var obj = { + 'a': 1, + 'b': 2, + 'c': 3, + 'd': 4 +}; + +var bool = everyOwnBy( obj, isPositive ); +// returns true +``` + +If a `predicate` function returns a non-truthy value, the function **immediately** returns `false`. + +```javascript +function isPositive( value ) { + return ( value > 0 ); +} + +var obj = { + 'a': 1, + 'b': -2, + 'c': 3, + 'd': 4 +}; + +var bool = everyOwnBy( obj, isPositive ); +// returns false +``` + +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 sum( value ) { + if ( value < 0 ) { + return false; + } + this.sum += value; + this.count += 1; + return true; +} + +var obj = { + 'a': 1, + 'b': 2, + 'c': 3 +}; + +var context = { + 'sum': 0, + 'count': 0 +}; + +var bool = everyOwnBy( obj, sum, context ); +// returns true + +var mean = context.sum / context.count; +// returns 2 +``` + +
+ + + + + +
+ +## Notes + +- If the 1st argument is not an [`object`][mdn-object] or the second argument is not a fuction , the + function throws a Type Error. + +- If provided an empty object, the function returns `true`. + + ```javascript + function untrue() { + return false; + } + var bool = everyOwnBy( {}, untrue ); + // returns true + ``` + + +
+ + + + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var everyOwnBy = require( '@stdlib/utils/every-own-by' ); + +function isPositive( value ) { + return ( value > 0 ); +} + +var obj = {}; +var i; + +// Populate object with random values +for ( i = 0; i < 100; i++ ) { + obj[ 'prop_' + i ] = randu(); +} + +var bool = everyOwnBy( obj, isPositive ); +// returns +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/utils/every-own-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/utils/every-own-by/benchmark/benchmark.js new file mode 100644 index 00000000000..7536111af4c --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/benchmark/benchmark.js @@ -0,0 +1,97 @@ +/** +* @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 everyOwnBy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'a': i, + 'b': i+1, + 'c': i+2, + 'd': i+3, + 'e': i+4 + }; + bool = everyOwnBy( 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(); + + function predicate( v ) { + return !isnan( v ); + } +}); + +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 + }; + keys = Object.keys( obj ); + bool = true; + for ( j = 0; j < keys.length; j++ ) { + if ( isnan( obj[ keys[j] ] ) ) { + bool = false; + break; + } + } + 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-own-by/benchmark/julia/REQUIRE b/lib/node_modules/@stdlib/utils/every-own-by/benchmark/julia/REQUIRE new file mode 100644 index 00000000000..98645e192e4 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/benchmark/julia/REQUIRE @@ -0,0 +1,2 @@ +julia 1.5 +BenchmarkTools 0.5.0 diff --git a/lib/node_modules/@stdlib/utils/every-own-by/benchmark/julia/benchmark.jl b/lib/node_modules/@stdlib/utils/every-own-by/benchmark/julia/benchmark.jl new file mode 100644 index 00000000000..4fd714d3c17 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-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 = "every-own-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 all( v -> ( !isnan(v) ), [ 1, 2, 3, 4, 5, 6 ] ) 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/every-own-by/docs/repl.txt b/lib/node_modules/@stdlib/utils/every-own-by/docs/repl.txt new file mode 100644 index 00000000000..8465c251e47 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/docs/repl.txt @@ -0,0 +1,43 @@ + +{{alias}}( object, predicate[, thisArg ] ) + Tests whether every own property of an object pass 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 non-truthy return + value. + + If provided an empty object, the function returns `true`. + + 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 all elements; 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/every-own-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/utils/every-own-by/docs/types/index.d.ts new file mode 100644 index 00000000000..b133bceac8e --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/docs/types/index.d.ts @@ -0,0 +1,102 @@ +/* +* @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 every property of an object passes a test implemented by a predicate function. +* +* ## Notes +* +* - The predicate function is provided three arguments: +* +* - `value`: collection value +* - `key`: collection key +* - `object`: the input object +* +* - The function immediately returns upon encountering a truthy return value. +* - If provided an empty object, the function returns `true`. +* +* @param object - input object +* @param predicate - test function +* @param thisArg - execution context +* @returns boolean indicating whether all own elements pass a test +* +* @example +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var obj = { 'a': 1, 'b': 2, 'c': 3 }; +* +* var bool = everyOwnBy( obj, isPositive ); +* // returns true +*/ +declare function everyOwnBy( object: Record, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + + +// EXPORTS // + +export = everyOwnBy; diff --git a/lib/node_modules/@stdlib/utils/every-own-by/docs/types/test.ts b/lib/node_modules/@stdlib/utils/every-own-by/docs/types/test.ts new file mode 100644 index 00000000000..a839c1192f3 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/docs/types/test.ts @@ -0,0 +1,62 @@ +/* +* @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 everyOwnBy = require( './index' ); + +const isPositive = ( v: number ): boolean => { + return ( v > 0 ); +}; + +// TESTS // + +var obj = { + 'a':1, + 'b':2, + 'c':3 +}; + +// The function returns a boolean... +{ + everyOwnBy( obj, isPositive ); // $ExpectType boolean + everyOwnBy( obj, isPositive ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a first argument which is not an object... +{ + everyOwnBy( 2, isPositive ); // $ExpectError + everyOwnBy( false, isPositive ); // $ExpectError + everyOwnBy( true, isPositive ); // $ExpectError + everyOwnBy( [ 1, 2 ], isPositive ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + everyOwnBy( obj , 2 ); // $ExpectError + everyOwnBy( obj , false ); // $ExpectError + everyOwnBy( obj , true ); // $ExpectError + everyOwnBy( obj , 'abc' ); // $ExpectError + everyOwnBy( obj , {} ); // $ExpectError + everyOwnBy( obj , [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + everyOwnBy(); // $ExpectError + everyOwnBy( [ 1, 2, 3 ] ); // $ExpectError + everyOwnBy( [ 1, 2, 3 ], isPositive, {}, 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/utils/every-own-by/examples/index.js b/lib/node_modules/@stdlib/utils/every-own-by/examples/index.js new file mode 100644 index 00000000000..421313d0ffc --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-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 everyOwnBy = require( './../lib' ); + +function isPositive( value ) { + return ( value > 0 ); +} + +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 = everyOwnBy( obj, isPositive ); +console.log( bool ); diff --git a/lib/node_modules/@stdlib/utils/every-own-by/lib/index.js b/lib/node_modules/@stdlib/utils/every-own-by/lib/index.js new file mode 100644 index 00000000000..c969974ef4b --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-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 every "own" property of a provided object passes a test implemented by a predicate function. +* +* @module @stdlib/utils/every-own-by +* +* @example +* var every = require( '@stdlib/utils/every-own-by' ); +* +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var obj = { 'a': 1, 'b': 2, 'c': 3 }; +* +* var bool = everyOwnBy( obj, isPositive ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/utils/every-own-by/lib/main.js b/lib/node_modules/@stdlib/utils/every-own-by/lib/main.js new file mode 100644 index 00000000000..ae590a40a65 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-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 all own elements in a collection 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 own elements pass a test +* +* @example +* function isPositive( v ) { +* return ( v > 0 ); +* } +* +* var obj = { +* 'a': 1, +* 'b': 2, +* 'c': 3 +* }; +* +* var bool = everyOwnBy( obj, isPositive ); +* // returns true +*/ +function everyOwnBy( 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 ) ) { + if ( !predicate.call( thisArg, obj[ key ], key, obj ) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = everyOwnBy; diff --git a/lib/node_modules/@stdlib/utils/every-own-by/package.json b/lib/node_modules/@stdlib/utils/every-own-by/package.json new file mode 100644 index 00000000000..db1881631f2 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/utils/every-own-by", + "version": "0.0.0", + "description": "Test whether all own properties 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.every", + "everyownby", + "iterate", + "validate" + ] + } + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/utils/every-own-by/test/test.js b/lib/node_modules/@stdlib/utils/every-own-by/test/test.js new file mode 100644 index 00000000000..f4e126401ff --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-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 everyOwnBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof everyOwnBy, '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() {}, + /.*/, + 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() { + everyOwnBy( 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() { + everyOwnBy( {}, value ); + }; + } +}); + +tape( 'if provided an empty object, the function returns `true`', function test( t ) { + var bool; + var obj; + + function foo() { + t.fail( 'should not be invoked' ); + } + obj = {}; + bool = everyOwnBy( obj, foo ); + + t.strictEqual( bool, true, 'returns true' ); + 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 = everyOwnBy( obj, isPositive ); + + t.strictEqual( bool, true, 'returns true' ); + 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 = everyOwnBy( 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 sum( value ) { + /* eslint-disable no-invalid-this */ + if ( value < 0 ) { + return false; + } + this.sum += value; + this.count += 1; + return true; + } + + ctx = { + 'sum': 0, + 'count': 0 + }; + obj = { + 'a': 1, + 'b': 2, + 'c': 3 + }; + + bool = everyOwnBy( obj, sum, ctx ); + + t.strictEqual( bool, true, 'returns true' ); + t.strictEqual( ctx.sum/ctx.count, 2.0, 'expected result' ); + + t.end(); +});