From 7bea15cb059ec7f2906589bc1b28a938cdadfa08 Mon Sep 17 00:00:00 2001 From: praneki Date: Thu, 29 Feb 2024 04:41:53 +0530 Subject: [PATCH 1/6] feat: add @stdlib/utils/every-own-by --- .../@stdlib/utils/every-own-by/README.md | 252 ++++++++++++++++++ .../utils/every-own-by/benchmark/benchmark.js | 97 +++++++ .../every-own-by/benchmark/julia/REQUIRE | 2 + .../every-own-by/benchmark/julia/benchmark.jl | 40 +++ .../@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 | 61 +++++ .../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 | 83 ++++++ .../@stdlib/utils/every-own-by/package.json | 71 +++++ .../@stdlib/utils/every-own-by/test/test.js | 180 +++++++++++++ 12 files changed, 1014 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 000000000000..33e39978c364 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/README.md @@ -0,0 +1,252 @@ + + +# everyOwnBy + +> Test whether all own propertes of an object in a collection pass a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var everyOwnBy = require( '@stdlib/utils/every-own-by' ); +``` + +#### everyOwnBy( collection, 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 + ``` + +- The function differs from [`Array.prototype.every`][mdn-array-every] in the following ways: + + - The function does **not** skip `undefined` elements. + + + + ```javascript + function log( value, index ) { + console.log( '%s: %s', index, value ); + return true; + } + + var obj = { + 'a': 1, + 'b': NaN, + 'c': 3 + }; + + var bool = everyOwnBy( obj, log ); + /* => + a: 1 + b: undefined + c: 3 + */ + ``` + +
+ + + + + +
+ +## 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 000000000000..7536111af4c6 --- /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 000000000000..98645e192e41 --- /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 000000000000..b1fe869b9497 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/benchmark/julia/benchmark.jl @@ -0,0 +1,40 @@ +#!/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. + +using Statistics +using BenchmarkTools + +import .everyOwnBy + +# Benchmark function +function bench_everyOwnBy(n) + function predicate(v) + return !isnan(v) + end + + # Benchmark + @benchmark begin + obj = Dict(:a => 1, :b => 2, :c => 3, :d => 4, :e => 5) + + everyOwnBy(obj, predicate) + end +end + +# Benchmark everyOwnBy function +n = 100 +@time bench_everyOwnBy(n) 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 000000000000..58d7fcd012a7 --- /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 + The 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 000000000000..88f049feff0f --- /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 teste +*/ +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 teste +*/ +type Unary = ( this: U, value: T ) => boolean; + +/** +* Checks whether an own property of the object passes the test. +* +* @param value - collection value +* @param key - collection key +* @returns boolean indicating whether an own property of the object passes the teste +*/ +type Binary = ( this: U, value: T, key: number ) => boolean; + +/** +* Checks whether an own property of the object passes the test. +* +* @param value - collection value +* @param key - collection key +* @param object - input object +* @returns boolean indicating whether an own property of the object passes the tests +*/ +type Ternary = ( this: U, value: T, key: number, object: Object ) => boolean; + +/** +* Checks whether an own property of the object passes the test. +* +* @param value - collection value +* @param key - collection 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 000000000000..0ad1c297d4f1 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-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 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 a 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 000000000000..007228009265 --- /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 000000000000..eed5b984975b --- /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 pass 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 000000000000..aed9717fde36 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/lib/main.js @@ -0,0 +1,83 @@ +/** +* @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 keys; + var key; + var len; + var i; + 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 ) ); + } + + keys = Object.keys( obj ); + len = keys.length; + for ( i = 0; i < len; i++ ) { + key = keys[ i ]; + 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 000000000000..2a8d5d2314a6 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/every-own-by/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/utils/every-own-by", + "version": "0.0.0", + "description": "Test whether all own property 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", + "collection", + "array-like", + "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 000000000000..f40c04387ffb --- /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 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() { + 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(); +}); From 3d9466e7ff0d3110f948b482d89ca67adfc162ee Mon Sep 17 00:00:00 2001 From: praneki Date: Sun, 3 Mar 2024 23:02:23 +0530 Subject: [PATCH 2/6] feat: added @stdlib/utils/every-own-by spaces --- .../every-own-by/benchmark/julia/benchmark.jl | 12 ++++++------ .../utils/every-own-by/examples/index.js | 18 +++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) 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 index b1fe869b9497..4011544d43eb 100644 --- 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 @@ -22,19 +22,19 @@ using BenchmarkTools import .everyOwnBy # Benchmark function -function bench_everyOwnBy(n) - function predicate(v) - return !isnan(v) +function bench_everyOwnBy( n ) + function predicate( v ) + return !isnan( v ) end # Benchmark @benchmark begin - obj = Dict(:a => 1, :b => 2, :c => 3, :d => 4, :e => 5) + obj = Dict( :a => 1, :b => 2, :c => 3, :d => 4, :e => 5 ) - everyOwnBy(obj, predicate) + everyOwnBy( obj, predicate ) end end # Benchmark everyOwnBy function n = 100 -@time bench_everyOwnBy(n) +@time bench_everyOwnBy( n ) 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 index 007228009265..421313d0ffc5 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/examples/index.js +++ b/lib/node_modules/@stdlib/utils/every-own-by/examples/index.js @@ -18,20 +18,20 @@ 'use strict'; -var randu = require('@stdlib/random/base/randu'); -var everyOwnBy = require('./../lib'); +var randu = require( '@stdlib/random/base/randu' ); +var everyOwnBy = require( './../lib' ); -function isPositive(value) { - return (value > 0); +function isPositive( value ) { + return ( value > 0 ); } var bool; var obj = {}; -var keys = ['a', 'b', 'c', 'd', 'e']; +var keys = [ 'a', 'b', 'c', 'd', 'e' ]; var i; -for ( i = 0; i < keys.length; i++) { - obj[keys[i]] = randu(); +for ( i = 0; i < keys.length; i++ ) { + obj[ keys[ i ] ] = randu(); } -bool = everyOwnBy(obj, isPositive); -console.log(bool); +bool = everyOwnBy( obj, isPositive ); +console.log( bool ); From 3b7f3d7fef10dc682499b03648a74b994be3f21d Mon Sep 17 00:00:00 2001 From: praneki Date: Mon, 4 Mar 2024 00:02:25 +0530 Subject: [PATCH 3/6] feat: added @stdlib/utils/every-own-by spaces --- .../every-own-by/benchmark/julia/benchmark.jl | 144 +++++++++++++++--- 1 file changed, 124 insertions(+), 20 deletions(-) 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 index 4011544d43eb..4fd714d3c177 100644 --- 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 @@ -16,25 +16,129 @@ # See the License for the specific language governing permissions and # limitations under the License. -using Statistics -using BenchmarkTools - -import .everyOwnBy - -# Benchmark function -function bench_everyOwnBy( n ) - function predicate( v ) - return !isnan( v ) - end - - # Benchmark - @benchmark begin - obj = Dict( :a => 1, :b => 2, :c => 3, :d => 4, :e => 5 ) - - everyOwnBy( obj, predicate ) - end +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 -# Benchmark everyOwnBy function -n = 100 -@time bench_everyOwnBy( n ) +main(); From acde5219d24a72080e6868f654305f6fa66f25f1 Mon Sep 17 00:00:00 2001 From: praneki Date: Mon, 4 Mar 2024 00:18:49 +0530 Subject: [PATCH 4/6] feat: added @stdlib/utils/every-own-by spaces --- lib/node_modules/@stdlib/utils/every-own-by/lib/main.js | 2 +- lib/node_modules/@stdlib/utils/every-own-by/test/test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 index aed9717fde36..49f549d3d2cc 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/lib/main.js +++ b/lib/node_modules/@stdlib/utils/every-own-by/lib/main.js @@ -57,7 +57,7 @@ function everyOwnBy( obj, predicate, thisArg ) { var key; var len; var i; - if ( !isObject( obj )) { + if ( !isObject( obj ) ) { throw new TypeError( format( 'invalid argument. First argument must be an object. Value: `%s`.', obj ) ); } if ( !isFunction( predicate ) ) { 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 index f40c04387ffb..25c0408b2f0d 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/test/test.js +++ b/lib/node_modules/@stdlib/utils/every-own-by/test/test.js @@ -50,7 +50,7 @@ tape( 'the function throws an error if not provided a object', function test( t /.*/, new Date() ]; - for (i =0; i < values.length; i++) { + for ( i =0; i < values.length; i++ ) { t.throws( badValue( values ), TypeError, 'throws a type error when provided '+values[i] ); } t.end(); From 7e201a9223e99358a663023394b7b17ab4c5cd9b Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sun, 3 Mar 2024 18:02:56 -0500 Subject: [PATCH 5/6] Apply suggestions from code review Signed-off-by: Philipp Burckhardt --- .../@stdlib/utils/every-own-by/README.md | 47 +------------------ .../@stdlib/utils/every-own-by/docs/repl.txt | 2 +- .../utils/every-own-by/docs/types/index.d.ts | 22 ++++----- .../utils/every-own-by/docs/types/test.ts | 3 +- .../@stdlib/utils/every-own-by/lib/index.js | 2 +- .../@stdlib/utils/every-own-by/lib/main.js | 8 +--- .../@stdlib/utils/every-own-by/package.json | 4 +- .../@stdlib/utils/every-own-by/test/test.js | 2 +- 8 files changed, 20 insertions(+), 70 deletions(-) diff --git a/lib/node_modules/@stdlib/utils/every-own-by/README.md b/lib/node_modules/@stdlib/utils/every-own-by/README.md index 33e39978c364..f0670807436b 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/README.md +++ b/lib/node_modules/@stdlib/utils/every-own-by/README.md @@ -20,7 +20,7 @@ limitations under the License. # everyOwnBy -> Test whether all own propertes of an object in a collection pass a test implemented by a predicate function. +> Test whether all own propertes of an object pass a test implemented by a predicate function. @@ -40,7 +40,7 @@ limitations under the License. var everyOwnBy = require( '@stdlib/utils/every-own-by' ); ``` -#### everyOwnBy( collection, predicate\[, thisArg ] ) +#### everyOwnBy( object, predicate\[, thisArg ] ) Tests whether all `own` properties of an object pass a test implemented by a `predicate` function. @@ -137,31 +137,6 @@ var mean = context.sum / context.count; // returns true ``` -- The function differs from [`Array.prototype.every`][mdn-array-every] in the following ways: - - - The function does **not** skip `undefined` elements. - - - - ```javascript - function log( value, index ) { - console.log( '%s: %s', index, value ); - return true; - } - - var obj = { - 'a': 1, - 'b': NaN, - 'c': 3 - }; - - var bool = everyOwnBy( obj, log ); - /* => - a: 1 - b: undefined - c: 3 - */ - ``` @@ -211,15 +186,6 @@ var bool = everyOwnBy( obj, isPositive ); @@ -235,15 +201,6 @@ var bool = everyOwnBy( obj, isPositive ); -[@stdlib/utils/any-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/any-by - -[@stdlib/utils/every-by-right]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/every-by-right - -[@stdlib/utils/for-each]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/for-each - -[@stdlib/utils/none-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/none-by - -[@stdlib/utils/some-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/some-by 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 index 58d7fcd012a7..8465c251e474 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/utils/every-own-by/docs/repl.txt @@ -20,7 +20,7 @@ Input object. predicate: Function - The test function. + Test function. thisArg: any (optional) Execution context. 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 index 88f049feff0f..b133bceac8e6 100644 --- 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 @@ -24,7 +24,7 @@ /** * Checks whether an own property of the object passes the test. * -* @returns boolean indicating whether an own property of the object passes the teste +* @returns boolean indicating whether an own property of the object passes the test */ type Nullary = ( this: U ) => boolean; @@ -32,34 +32,34 @@ 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 teste +* @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 - collection value -* @param key - collection key -* @returns boolean indicating whether an own property of the object passes the teste +* @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 - collection value -* @param key - collection key +* @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 +* @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 - collection value -* @param key - collection key +* @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 */ @@ -89,7 +89,7 @@ type Predicate = Nullary | Unary | Binary | Ternary; * return ( v > 0 ); * } * -* var obj = { 'a':1, 'b':2, 'c':3 }; +* var obj = { 'a': 1, 'b': 2, 'c': 3 }; * * var bool = everyOwnBy( obj, isPositive ); * // returns true 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 index 0ad1c297d4f1..a839c1192f3b 100644 --- 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 @@ -29,13 +29,14 @@ var obj = { '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 a object... +// 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 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 index eed5b984975b..c969974ef4b3 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/lib/index.js +++ b/lib/node_modules/@stdlib/utils/every-own-by/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Test whether every "own" property of a provided object pass a test implemented by a predicate function. +* Test whether every "own" property of a provided object passes a test implemented by a predicate function. * * @module @stdlib/utils/every-own-by * 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 index 49f549d3d2cc..ae590a40a651 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/lib/main.js +++ b/lib/node_modules/@stdlib/utils/every-own-by/lib/main.js @@ -53,10 +53,7 @@ var format = require( '@stdlib/string/format' ); * // returns true */ function everyOwnBy( obj, predicate, thisArg ) { - var keys; var key; - var len; - var i; if ( !isObject( obj ) ) { throw new TypeError( format( 'invalid argument. First argument must be an object. Value: `%s`.', obj ) ); } @@ -64,10 +61,7 @@ function everyOwnBy( obj, predicate, thisArg ) { throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', predicate ) ); } - keys = Object.keys( obj ); - len = keys.length; - for ( i = 0; i < len; i++ ) { - key = keys[ i ]; + for ( key in obj ) { if ( hasOwnProp( obj, key ) ) { if ( !predicate.call( thisArg, obj[ key ], key, obj ) ) { return false; diff --git a/lib/node_modules/@stdlib/utils/every-own-by/package.json b/lib/node_modules/@stdlib/utils/every-own-by/package.json index 2a8d5d2314a6..db1881631f2d 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/package.json +++ b/lib/node_modules/@stdlib/utils/every-own-by/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/utils/every-own-by", "version": "0.0.0", - "description": "Test whether all own property of an object pass a test implemented by a predicate function.", + "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", @@ -63,8 +63,6 @@ "object.every", "everyownby", "iterate", - "collection", - "array-like", "validate" ] } 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 index 25c0408b2f0d..f4e126401fff 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/test/test.js +++ b/lib/node_modules/@stdlib/utils/every-own-by/test/test.js @@ -33,7 +33,7 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function throws an error if not provided a object', function test( t ) { +tape( 'the function throws an error if not provided an object', function test( t ) { var values; var i; From 44b5995028527af31a92eff5455167bdbe482fd2 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sun, 3 Mar 2024 18:05:27 -0500 Subject: [PATCH 6/6] Update lib/node_modules/@stdlib/utils/every-own-by/README.md Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/utils/every-own-by/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/utils/every-own-by/README.md b/lib/node_modules/@stdlib/utils/every-own-by/README.md index f0670807436b..d3eb43ddfcf1 100644 --- a/lib/node_modules/@stdlib/utils/every-own-by/README.md +++ b/lib/node_modules/@stdlib/utils/every-own-by/README.md @@ -197,8 +197,6 @@ var bool = everyOwnBy( obj, isPositive ); [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object -[mdn-array-every]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every -