Skip to content

feat: added utils/every-in-by #1409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions lib/node_modules/@stdlib/utils/every-in-by/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!--

@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.

-->

# everyInBy

> Test whether all properties (own and inherited) of an object pass a test implemented by a predicate function.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var everyInBy = require( '@stdlib/utils/every-in-by' );
```

#### everyInBy( object, predicate\[, thisArg ] )

Tests whether all properties (own and inherited) of an `object` pass a test implemented by a `predicate` function.

```javascript
var o;
var bool;

function isPositive( v ) {
return ( v > 0 );
}

o = {
'a': 1,
'b': 2,
'c': 3
};

bool = everyInBy( o, isPositive );
// returns true
```

If provided an empty `object`, the function returns `true`.

```javascript
function isPositive(v) {
return ( v > 0 );
}

var bool = everyInBy( {}, isPositive );
// returns true
```

</section>

<!-- /.usage -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var everyInBy = require( '@stdlib/utils/every-in-by' );

var bool;
var o;
var i;

function isPositive(v) {
return ( v > 0 );
}

o = {};
for ( i = 0; i < 100; i++ ) {
o[ i ] = ( randu() < 0.95 );
}

bool = everyInBy( o, isPositive );
// returns <boolean>
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.links -->
64 changes: 64 additions & 0 deletions lib/node_modules/@stdlib/utils/every-in-by/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var randu = require( '@stdlib/random/base/randu' );
var pkg = require( './../package.json' ).name;
var everyInBy = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var bool;
var obj;
var i;

function predicate( v ) {
return !isnan( v );
}

obj = {
'a': 'beep',
'b': 'boop',
'c': 'foo',
'd': 'bar',
'e': randu()
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
obj.e = randu();
bool = everyInBy( obj, predicate );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/utils/every-in-by/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

{{alias}}( object, predicate[, thisArg ] )
Test whether all properties (own and inherited) of an object pass a
test implemented by a predicate function.

The predicate function is provided three arguments:

- `value`: object value
- `key`: object key
- `object`: the input object

The function immediately returns upon encountering a non-truthy return
value.

If provided an empty object, the function returns `true`.

Parameters
----------
object: Object
Input object over which to iterate.

predicate: Function
Test function.

thisArg: any (optional)
Execution context.

Returns
-------
bool: boolean
The function returns `true` if the predicate function returns a truthy
value for all elements; otherwise, the function returns `false`.

Examples
--------
> function positive( v ) { return ( v > 0 ); };
> var o = {a: 1, b: 2, c: 3};
> var bool = {{alias}}( o, positive )
true

See Also
--------

84 changes: 84 additions & 0 deletions lib/node_modules/@stdlib/utils/every-in-by/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Collection } from '@stdlib/types/object';

/**
* Checks whether an element in a collection passes a test.
*
* @returns boolean indicating whether an element in a collection passes a test
*/
type Nullary<U> = ( 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<T, U> = ( 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<T, U> = ( 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<T, U> = ( this: U, value: T, index: number, collection: Collection<T> ) => 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<T, U> = Nullary<U> | Unary<T, U> | Binary<T, U> | Ternary<T, U>;

/**
* Checks whether all own and inherited properties in an object pass a test implemented by a predicate function.
*
* @param obj - The object to iterate over.
* @param predicate - The test function to apply to each property.
* @param thisArg - Optional execution context for the predicate function.
* @returns boolean indicating whether all properties pass the test.
*
* @throws TypeError if `obj` is not an object or if `predicate` is not a function.
*/
declare function everyInBy<T, U>(
obj: object,
predicate: Predicate<T, U>,
thisArg?: ThisParameterType<Predicate<T, U>>
): boolean;


// EXPORTS //

export = everyInBy;
61 changes: 61 additions & 0 deletions lib/node_modules/@stdlib/utils/every-in-by/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import everyInBy = require( './index' );


// TESTS //

function isPositive( v: number ): boolean {
if ( typeof v !== 'object' || v === null ) {
throw new TypeError( 'Expected an object' );
}

return v > 0;
}


// The function returns a boolean...
{
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, isPositive ); // $ExpectType boolean
}

// The compiler throws an error if the function is provided a first argument which is not a object...
{
everyInBy( 2 ); // $ExpectError
everyInBy( false ); // $ExpectError
everyInBy( true ); // $ExpectError
everyInBy( [1, 2, 3, 4] ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a function...
{
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, 2 ); // $ExpectError
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, false ); // $ExpectError
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, true ); // $ExpectError
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, 'abc' ); // $ExpectError
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, {} ); // $ExpectError
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, [] ); // $ExpectError
}

// The compiler throws an error if the function is provided an invalid number of arguments...
{
everyInBy(); // $ExpectError
everyInBy( { 'a': 1, 'b': 2, 'c': 3} ); // $ExpectError
everyInBy( { 'a': 1, 'b': 2, 'c': 3 }, {}, 3 ); // $ExpectError
}
Loading