-
-
Notifications
You must be signed in to change notification settings - Fork 836
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
009160a
feat: added utils/every-in-by
saisrikardumpeti b355421
feat: added utils/every-in-by added README.md
saisrikardumpeti 5000e30
feat: added utils/every-in-by feedback
saisrikardumpeti 58c1166
feat: added utils/every-in-by updated main.js
saisrikardumpeti f3ece15
feat: added utils/every-in-by updated main.js
saisrikardumpeti 1e06583
feat: added utils/every-in-by updated test.js
saisrikardumpeti 24641e5
style: add missing spaces
Planeshifter 6e76aa5
Update lib/node_modules/@stdlib/utils/every-by/docs/types/index.d.ts
Planeshifter 30a0211
Update lib/node_modules/@stdlib/utils/every-in-by/docs/repl.txt
Planeshifter 6ccb0b9
Update lib/node_modules/@stdlib/utils/every-in-by/docs/types/index.d.ts
Planeshifter 818222e
Update lib/node_modules/@stdlib/utils/every-in-by/docs/types/index.d.ts
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
lib/node_modules/@stdlib/utils/every-in-by/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
84
lib/node_modules/@stdlib/utils/every-in-by/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
lib/node_modules/@stdlib/utils/every-in-by/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.