diff --git a/none-own-by/README.md b/none-own-by/README.md new file mode 100644 index 00000000..7c77c686 --- /dev/null +++ b/none-own-by/README.md @@ -0,0 +1,164 @@ + + +# noneOwnBy + +> Tests whether every own property of an object fails a test implemented by a predicate function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var noneOwnBy = require( '@stdlib/utils/none-own-by' ); +``` + +#### noneOwnBy( object, predicate\[, thisArg ] ) + +Tests whether every `own` property of an object fails a test implemented by a `predicate` function. + +```javascript +function isUnderage( age ) { + return ( age < 18 ); +} + +var obj = { + 'a': 28, + 'b': 22, + 'c': 25 +}; + +var bool = noneOwnBy( obj, isUnderage ); +// returns true +``` + +If a `predicate` function returns a truthy value, the function **immediately** returns `false`. + +```javascript +function isUnderage( age ) { + return ( age < 18 ); +} + +var obj = { + 'a': 12, + 'b': 22, + 'c': 25 +}; + +var bool = noneOwnBy( obj, isUnderage ); +// returns false +``` + +
+ + + + + +
+ +## Notes + +- If the 1st argument is not an 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 truthy() { + return true; + } + var bool = noneOwnBy( {}, truthy ); + // returns true + ``` + +
+ + + + + +
+ +## Examples + + + +```javascript +var noneOwnBy = require( '@stdlib/utils/none-own-by' ); + +function isUnderage( age ) { + return age < 18; +} + +var obj = { + 'a': 26, + 'b': 20, + 'c': 25 +}; + +var bool = noneOwnBy( obj, isUnderage ); +// returns true +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/none-own-by/benchmark/index.js b/none-own-by/benchmark/index.js new file mode 100644 index 00000000..a48d76de --- /dev/null +++ b/none-own-by/benchmark/index.js @@ -0,0 +1,60 @@ +/** +* @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 noneOwnBy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var i; + + function predicate( v ) { + return isnan( v ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'a': i, + 'b': i+1, + 'c': i+2, + 'd': i+3 + }; + bool = noneOwnBy( 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(); +}); diff --git a/none-own-by/docs/repl.txt b/none-own-by/docs/repl.txt new file mode 100644 index 00000000..a942d607 --- /dev/null +++ b/none-own-by/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( object, predicate[, thisArg ] ) + Tests whether every own property of an object fails 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 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 falsy + value for all own properties; otherwise, the function returns `false`. + + Examples + -------- + > function isUnderage( v ) { return ( v < 18 ); }; + > var obj = { 'a': 11, 'b': 12, 'c': 22 }; + > var bool = {{alias}}( obj, isUnderage ) + false + + See Also + -------- + diff --git a/none-own-by/docs/types/index.d.ts b/none-own-by/docs/types/index.d.ts new file mode 100644 index 00000000..7ba3dda5 --- /dev/null +++ b/none-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 - property 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 - object value +* @param key - object key +* @param object - input object +* @returns boolean indicating whether an own property of the object passes the test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* Tests whether every property of an object fails a test implemented by a predicate function. +* +* ## Notes +* +* - The predicate function is provided three arguments: +* +* - `value`: property value +* - `key`: property 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 every property fails a test +* +* @example +* function isUnderage( v ) { +* return ( v < 18 ); +* } +* +* var obj = { 'a': 20, 'b': 22, 'c': 25 }; +* +* var bool = noneOwnBy( obj, isUnderage ); +* // returns true +*/ +declare function noneOwnBy( object: Record, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + + +// EXPORTS // + +export = noneOwnBy; diff --git a/none-own-by/docs/types/test.ts b/none-own-by/docs/types/test.ts new file mode 100644 index 00000000..c095398c --- /dev/null +++ b/none-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 noneOwnBy = require( './index' ); + +const isUnderAge = ( v: number ): boolean => { + return ( v < 18 ); +}; + +// TESTS // + +const obj = { + 'a': 10, + 'b': 12, + 'c': 22 +}; + +// The function returns a boolean... +{ + noneOwnBy( obj, isUnderAge ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a first argument which is not an object... +{ + noneOwnBy( 2, isUnderAge ); // $ExpectError + noneOwnBy( false, isUnderAge ); // $ExpectError + noneOwnBy( true, isUnderAge ); // $ExpectError + noneOwnBy( [ 1, 2 ], isUnderAge ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + noneOwnBy( obj , 2 ); // $ExpectError + noneOwnBy( obj , false ); // $ExpectError + noneOwnBy( obj , true ); // $ExpectError + noneOwnBy( obj , 'abc' ); // $ExpectError + noneOwnBy( obj , {} ); // $ExpectError + noneOwnBy( obj , [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + noneOwnBy(); // $ExpectError + noneOwnBy( [ 1, 2, 3 ] ); // $ExpectError + noneOwnBy( [ 1, 2, 3 ], isUnderAge, {}, 3 ); // $ExpectError +} diff --git a/none-own-by/examples/index.js b/none-own-by/examples/index.js new file mode 100644 index 00000000..bf73e63c --- /dev/null +++ b/none-own-by/examples/index.js @@ -0,0 +1,35 @@ +/** +* @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 noneOwnBy = require( './../lib' ); + +function isUnderage( age ) { + return age < 18; +} + +var obj = { + 'a': 26, + 'b': 20, + 'c': 25 +}; + +var bool = noneOwnBy( obj, isUnderage ); +console.log( bool ); +// => true diff --git a/none-own-by/lib/index.js b/none-own-by/lib/index.js new file mode 100644 index 00000000..903688c2 --- /dev/null +++ b/none-own-by/lib/index.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether every "own" property of a provided object fails a test implemented by a predicate function. +* +* @module @stdlib/utils/none-own-by +* +* @example +* var noneOwnBy = require( '@stdlib/utils/none-own-by' ); +* +* function isUnderage( age ) { +* return age < 18; +* } +* +* var obj = { +* a : 10, +* b : 12, +* c : 15 +* }; +* +* var bool = noneOwnBy( obj, isUnderage ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/none-own-by/lib/main.js b/none-own-by/lib/main.js new file mode 100644 index 00000000..6af9ad13 --- /dev/null +++ b/none-own-by/lib/main.js @@ -0,0 +1,75 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isObject = require( '@stdlib/assert/is-object' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Tests whether every own property of an object fails 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 elements fail a test +* +* @example +* function isUnderage(age) { +* return ( age < 18 ); +* }; +* +* var obj = { +* 'a': 10, +* 'b': 12, +* 'c': 15 +* }; +* +* var bool = noneOwnBy( obj, isUnderage ); +* // returns false +*/ +function noneOwnBy( obj, predicate, thisArg ) { + var key; + + if ( !isObject( obj ) ) { + throw new TypeError( format(' invalid argument. First argument must be an object. Value: `%s`.', obj ) ); + } + if ( !isFunction( predicate ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', predicate ) ); + } + for ( key in obj ) { + if (hasOwnProp( obj, key ) && predicate.call( thisArg, obj[key], key, obj)) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = noneOwnBy; diff --git a/none-own-by/package.json b/none-own-by/package.json new file mode 100644 index 00000000..8cc325d4 --- /dev/null +++ b/none-own-by/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/utils/none-own-by", + "version": "0.0.0", + "description": "Tests whether every own property of an object fails 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", + "none", + "all", + "every", + "object", + "iterate", + "validate" + ] +} + \ No newline at end of file diff --git a/none-own-by/test/test.js b/none-own-by/test/test.js new file mode 100644 index 00000000..750d04ff --- /dev/null +++ b/none-own-by/test/test.js @@ -0,0 +1,122 @@ +/** +* @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( './../../noop' ); +var noneOwnBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof noneOwnBy, '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() { + noneOwnBy( value, noop ); + }; + } +}); + +tape( 'the function throws an error if not provided a predicate function', function test( t ) { + var values; + + values = { + 'a': 10, + 'b': 12, + 'c': 15 + }; + + t.throws( badValue( values ), TypeError, 'throws a type error when provided '+values ); + t.end(); + + function badValue( value ) { + return function badValue() { + noneOwnBy( value, value ); + }; + } +}); + +tape( 'the function returns `true` if all own properties fail a test ', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 20, + 'b': 22, + 'c': 25 + }; + + function underAge( value ) { + return ( value < 18 ); + } + + bool = noneOwnBy( obj, underAge ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if one or more own properties pass a test', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 10, + 'b': 12, + 'c': 15 + }; + + function underAge( value ) { + return ( value < 18 ); + } + + bool = noneOwnBy( obj, underAge ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +});