-
-
Notifications
You must be signed in to change notification settings - Fork 564
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
feat: add assert/has-same-constructor
#1357
Changes from 12 commits
40f15f4
45fe0f9
e9646fa
549425d
c9018dd
a30b853
d881d61
d4b9105
6dba796
a377318
5487a81
81c2e81
d2308a4
d97cacb
10d857c
3515c5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,93 @@ | ||||||
<!-- | ||||||
|
||||||
@license Apache-2.0 | ||||||
|
||||||
Copyright (c) 2018 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. | ||||||
|
||||||
--> | ||||||
|
||||||
# hasSameConstructor | ||||||
|
||||||
> Test if two values have same constructor. | ||||||
|
||||||
<section class="usage"> | ||||||
|
||||||
## Usage | ||||||
|
||||||
```javascript | ||||||
var isArrayLikeObject = require( '@stdlib/assert/has-same-constructor' ); | ||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
``` | ||||||
|
||||||
#### hasSameConstructor( value , value ) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Tests if two values have same `constructor`. | ||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
<!-- eslint-disable object-curly-newline --> | ||||||
|
||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
```javascript | ||||||
> var obj1 = new Date(); | ||||||
var obj2 = new Date(); | ||||||
console.log(hasSameConstructor(obj1, obj2)); | ||||||
true | ||||||
|
||||||
> var obj1 = new Date(); | ||||||
var obj3 = new String("Hello"); | ||||||
console.log(hasSameConstructor(obj1, obj3)); | ||||||
false | ||||||
|
||||||
> var obj3 = new String("Hello"); | ||||||
var obj4 = "World"; | ||||||
console.log(hasSameConstructor(obj3, obj4)); | ||||||
true | ||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
``` | ||||||
|
||||||
If provided with two values with different `constructors`, the function returns `false`. | ||||||
|
||||||
</section> | ||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
<!-- /.usage --> | ||||||
|
||||||
<section class="examples"> | ||||||
|
||||||
## Examples | ||||||
|
||||||
<!-- eslint-disable object-curly-newline, object-curly-spacing, no-empty-function, no-restricted-syntax --> | ||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
<!-- eslint no-undef: "error" --> | ||||||
|
||||||
```javascript | ||||||
var hasSameConstructor = require( './../lib' ); | ||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
var DateObject1 = new Date(); | ||||||
var DateObject2 = new Date(); | ||||||
var StringObject = new String("Hello"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using the |
||||||
var StringPrimitive = "World"; | ||||||
|
||||||
console.log( hasSameConstructor( DateObject1, DateObject2 ) ); | ||||||
// => true | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not how we do doctesting. See our doctesting guide: https://github.com/stdlib-js/stdlib/blob/develop/docs/doctest.md |
||||||
|
||||||
console.log( hasSameConstructor( DateObject1, StringObject ) ); | ||||||
// => false | ||||||
|
||||||
console.log( hasSameConstructor( StringObject, StringPrimitive ) ); | ||||||
// => true | ||||||
|
||||||
console.log( (function test() { | ||||||
return hasSameConstructor( arguments, StringObject ); | ||||||
})() ); | ||||||
// => false | ||||||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
``` | ||||||
|
||||||
</section> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You removed trailing sections for links, related packages, etc. Please append those here. We use them in various build processes, even if they are empty. |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,92 @@ | ||||||||
/** | ||||||||
* @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 pkg = require( './../package.json' ).name; | ||||||||
var hasSameConstructor = require( './../lib' ); | ||||||||
|
||||||||
|
||||||||
// MAIN // | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
/** | ||||||||
* Benchmark the hasSameConstructor function when provided with same value types. | ||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for JSDoc comments here and below. |
||||||||
bench( pkg, function benchmark( b ) { | ||||||||
var obj1; | ||||||||
var obj2; | ||||||||
var bool; | ||||||||
var i; | ||||||||
|
||||||||
b.tic(); | ||||||||
for ( i = 0; i < b.iterations; i++ ) { | ||||||||
obj1 = new Date(); | ||||||||
obj2 = new Date(); | ||||||||
bool = hasSameConstructor( obj1, obj2 ); | ||||||||
if ( typeof bool !== 'boolean' ) { | ||||||||
b.fail( 'should return a boolean' ); | ||||||||
} | ||||||||
} | ||||||||
b.toc(); | ||||||||
if ( typeof bool !== 'boolean' ) { | ||||||||
b.fail( 'should return a boolean' ); | ||||||||
} | ||||||||
b.pass( 'benchmark finished' ); | ||||||||
b.end(); | ||||||||
}); | ||||||||
|
||||||||
/** | ||||||||
* Benchmark the hasSameConstructor function when provided with other value types. | ||||||||
*/ | ||||||||
bench( pkg+':other_types', function benchmark( b ) { | ||||||||
var obj1; | ||||||||
var obj2; | ||||||||
var obj3; | ||||||||
var obj4; | ||||||||
var bool; | ||||||||
var i; | ||||||||
|
||||||||
b.tic(); | ||||||||
for ( i = 0; i < b.iterations; i++ ) { | ||||||||
obj1 = new Date(); | ||||||||
obj2 = new Date(); | ||||||||
obj3 = new String( "Hello" ); | ||||||||
obj4 = "World"; | ||||||||
bool = hasSameConstructor( obj1, obj2 ); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not what we want. Please see other benchmarks for how we measure things and read the benchmark harness README to get a better understanding of how benchmarks should be written: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/bench/harness. |
||||||||
if ( typeof bool !== 'boolean' ) { | ||||||||
b.fail( 'should return a boolean' ); | ||||||||
} | ||||||||
bool = hasSameConstructor( obj1, obj3 ); | ||||||||
if ( typeof bool !== 'boolean' ) { | ||||||||
b.fail( 'should return a boolean' ); | ||||||||
} | ||||||||
bool = hasSameConstructor( obj3, obj4 ); | ||||||||
if ( typeof bool !== 'boolean' ) { | ||||||||
b.fail( 'should return a boolean' ); | ||||||||
} | ||||||||
} | ||||||||
b.toc(); | ||||||||
if ( typeof bool !== 'boolean' ) { | ||||||||
b.fail( 'should return a boolean' ); | ||||||||
} | ||||||||
b.pass( 'benchmark finished' ); | ||||||||
b.end(); | ||||||||
}); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
{{alias}}( value ) | ||||||
Tests if two values have same constructor. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Here and elsewhere. |
||||||
|
||||||
If provided with two values with different constructors, the function returns `false`. | ||||||
|
||||||
Comment on lines
+4
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Parameters | ||||||
---------- | ||||||
value: any | ||||||
Value to test. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
bool: boolean | ||||||
Boolean indicating whether two values have same constructor. | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
> var obj1 = new Date(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not how we do examples. See https://github.com/stdlib-js/stdlib/blob/develop/docs/repl_text.md. |
||||||
var obj2 = new Date(); | ||||||
console.log({{alias}}(obj1, obj2)); | ||||||
true | ||||||
|
||||||
> var obj1 = new Date(); | ||||||
var obj3 = new String("Hello"); | ||||||
console.log({{alias}}tor(obj1, obj3)); | ||||||
false | ||||||
|
||||||
> var obj3 = new String("Hello"); | ||||||
var obj4 = "World"; | ||||||
console.log({{alias}}(obj3, obj4)); | ||||||
true | ||||||
|
||||||
See Also | ||||||
-------- | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||
/* | ||||||
* @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 | ||||||
|
||||||
/** | ||||||
* Test whether two provided values have the same constructor. | ||||||
* | ||||||
* @param {*} a - The first value to test. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't include type annotations in our TypeScript JSDoc comments. And also, this is not the format we use for parameter descriptions. Please refer to other packages for examples. |
||||||
* @param {*} b - The second value to test. | ||||||
* @returns {boolean} Returns `true` if both values have the same constructor, else `false`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @example | ||||||
* var obj1 = new Date(); | ||||||
* var obj2 = new Date(); | ||||||
* console.log(hasSameConstructor(obj1, obj2)); | ||||||
* // returns true, both are instances of Date | ||||||
* | ||||||
* @example | ||||||
* var obj1 = new Date(); | ||||||
* var obj3 = new String("Hello"); | ||||||
* console.log(hasSameConstructor(obj1, obj3)); | ||||||
* // returns false | ||||||
* | ||||||
* @example | ||||||
* var obj3 = new String("Hello"); | ||||||
* var obj4 = "World"; | ||||||
* console.log(hasSameConstructor(obj3, obj4)); | ||||||
* // returns true, one is String, the other is primitive string; but has same String constructor | ||||||
*/ | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
declare function hasSameConstructor(a: any, b: any): boolean; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// EXPORTS // | ||||||
|
||||||
export = hasSameConstructor; |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||
/* | ||||||||
* @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 hasSameConstructor from './index'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please follow practices observed in other packages. |
||||||||
|
||||||||
// TESTS // | ||||||||
|
||||||||
// The function returns a boolean... | ||||||||
{ | ||||||||
hasSameConstructor(new Date(), new String("Hello")); // $ExpectType boolean | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces. Quotes. |
||||||||
hasSameConstructor(new String("Hello"), "World"); // $ExpectType boolean | ||||||||
} | ||||||||
|
||||||||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||||||||
{ | ||||||||
hasSameConstructor(); // $ExpectError | ||||||||
hasSameConstructor(new Date(), new Date(), new Date()); // $ExpectError | ||||||||
} | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @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. | ||
*/ | ||
|
||
/* eslint-disable object-curly-newline, no-empty-function, no-restricted-syntax */ | ||
|
||
'use strict'; | ||
|
||
var hasSameConstructor = require( './../lib' ); | ||
|
||
var DateObject1 = new Date(); | ||
var DateObject2 = new Date(); | ||
var StringObject = new String("Hello"); | ||
var StringPrimitive = "World"; | ||
|
||
console.log( hasSameConstructor( DateObject1, DateObject2 ) ); | ||
// => true | ||
|
||
console.log( hasSameConstructor( DateObject1, StringObject ) ); | ||
// => false | ||
|
||
console.log( hasSameConstructor( StringObject, StringPrimitive ) ); | ||
// => true | ||
|
||
console.log( (function test() { | ||
return hasSameConstructor( arguments, StringObject ); | ||
})() ); | ||
// => false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.