-
-
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 all 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,121 @@ | ||||||||||||||
<!-- | ||||||||||||||
|
||||||||||||||
@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 hasSameConstructor = require( '@stdlib/assert/has-same-constructor' ); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
#### hasSameConstructor( v1, v2 ) | ||||||||||||||
|
||||||||||||||
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
|
||||||||||||||
|
||||||||||||||
<!-- eslint-disable no-new-wrappers --> | ||||||||||||||
|
||||||||||||||
```javascript | ||||||||||||||
var obj1 = new Date(); | ||||||||||||||
var obj2 = new Date(); | ||||||||||||||
var bool = hasSameConstructor(obj1, obj2); | ||||||||||||||
// returns true | ||||||||||||||
|
||||||||||||||
var obj1 = new Date(); | ||||||||||||||
var obj3 = 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.
Suggested change
As elsewhere in this project, spaces and we use single quotes. Here and everywhere else. |
||||||||||||||
bool = hasSameConstructor(obj1, obj3); | ||||||||||||||
// returns false | ||||||||||||||
|
||||||||||||||
var obj3 = new String("Hello"); | ||||||||||||||
var obj4 = "World"; | ||||||||||||||
bool = hasSameConstructor(obj3, obj4); | ||||||||||||||
// returns true | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
</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.
Suggested change
|
||||||||||||||
<!-- /.usage --> | ||||||||||||||
|
||||||||||||||
<section class="examples"> | ||||||||||||||
|
||||||||||||||
## Examples | ||||||||||||||
|
||||||||||||||
<!-- eslint-disable no-new-wrappers --> | ||||||||||||||
|
||||||||||||||
Comment on lines
+65
to
+66
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
|
||||||||||||||
<!-- eslint no-undef: "error" --> | ||||||||||||||
|
||||||||||||||
```javascript | ||||||||||||||
var DateObj1 = new Date(); | ||||||||||||||
var DateObj2 = new Date(); | ||||||||||||||
var FloatArrObj = new Float64Array( 10 ); | ||||||||||||||
var IntArrObj = new Int32Array( [ 1, 2, 3 ] ); | ||||||||||||||
var ComplexObj = new Complex64( 5.0, 3.0 ); | ||||||||||||||
|
||||||||||||||
var bool = hasSameConstructor( DateObj1, DateObj2 ); | ||||||||||||||
// => 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 |
||||||||||||||
|
||||||||||||||
bool = hasSameConstructor( DateObj1, FloatArrObj ); | ||||||||||||||
// => false | ||||||||||||||
|
||||||||||||||
bool = hasSameConstructor( FloatArrObj , IntArrObj ); | ||||||||||||||
// => false | ||||||||||||||
|
||||||||||||||
bool = hasSameConstructor( ComplexObj , IntArrObj ); | ||||||||||||||
// => false | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
</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. |
||||||||||||||
|
||||||||||||||
<!-- /.examples --> | ||||||||||||||
|
||||||||||||||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||||||||||||||
|
||||||||||||||
<section class="related"> | ||||||||||||||
|
||||||||||||||
* * * | ||||||||||||||
|
||||||||||||||
## See Also | ||||||||||||||
|
||||||||||||||
- <span class="package-name">[`@stdlib/assert/has-iterator-symbol-support`][@stdlib/assert/has-iterator-symbol-support]</span><span class="delimiter">: </span><span class="description">detect native Symbol.iterator support.</span> | ||||||||||||||
Comment on lines
+96
to
+101
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
|
||||||||||||||
|
||||||||||||||
</section> | ||||||||||||||
|
||||||||||||||
<!-- /.related --> | ||||||||||||||
|
||||||||||||||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||||||||||||||
|
||||||||||||||
<section class="links"> | ||||||||||||||
|
||||||||||||||
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol | ||||||||||||||
|
||||||||||||||
<!-- <related-links> --> | ||||||||||||||
|
||||||||||||||
[@stdlib/assert/has-iterator-symbol-support]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/has-iterator-symbol-support | ||||||||||||||
|
||||||||||||||
<!-- </related-links> --> | ||||||||||||||
|
||||||||||||||
Comment on lines
+113
to
+118
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
|
||||||||||||||
</section> | ||||||||||||||
|
||||||||||||||
<!-- /.links --> |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||||||
/** | ||||||||
* @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 no-empty-function */ | ||||||||
|
||||||||
'use strict'; | ||||||||
|
||||||||
// MODULES // | ||||||||
|
||||||||
var bench = require( '@stdlib/bench' ); | ||||||||
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; | ||||||||
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
|
||||||||
|
||||||||
bench( pkg, function benchmark( b ) { | ||||||||
var values; | ||||||||
var bool; | ||||||||
var i; | ||||||||
|
||||||||
values = [ | ||||||||
'5', | ||||||||
5, | ||||||||
NaN, | ||||||||
true, | ||||||||
false, | ||||||||
null, | ||||||||
void 0, | ||||||||
[], | ||||||||
{}, | ||||||||
new Date(), | ||||||||
function noop() {} | ||||||||
]; | ||||||||
|
||||||||
b.tic(); | ||||||||
for ( i = 0; i < b.iterations; i++ ) { | ||||||||
bool = hasSameConstructor( values[ i % values.length ], values[ (i+1) % values.length ] ); | ||||||||
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(); | ||||||||
}); |
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(); | ||||||
var bool = {{alias}}(obj1, obj2); | ||||||
true | ||||||
|
||||||
> var obj1 = new Date(); | ||||||
var obj3 = new String("Hello"); | ||||||
bool = {{alias}}(obj1, obj3); | ||||||
false | ||||||
|
||||||
> var obj3 = new String("Hello"); | ||||||
var obj4 = "World"; | ||||||
bool = {{alias}}(obj3, obj4); | ||||||
true | ||||||
|
||||||
See Also | ||||||
-------- | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -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. | ||||||
*/ | ||||||
|
||||||
// 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(); | ||||||
* var bool = hasSameConstructor(obj1, obj2); | ||||||
* // returns true | ||||||
* @example | ||||||
* var obj1 = new Date(); | ||||||
* var obj3 = new String("Hello"); | ||||||
* var bool = hasSameConstructor(obj1, obj3); | ||||||
* // returns false | ||||||
* | ||||||
* @example | ||||||
* var obj3 = new String("Hello"); | ||||||
* var obj4 = "World"; | ||||||
* var bool = hasSameConstructor(obj3, obj4); | ||||||
* // returns 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.
Suggested change
|
||||||
declare function hasSameConstructor(a: any, b: any): boolean; | ||||||
Check failure on line 46 in lib/node_modules/@stdlib/assert/has-same-constructor/docs/types/index.d.ts GitHub Actions / Lint Changed Files
Check warning on line 46 in lib/node_modules/@stdlib/assert/has-same-constructor/docs/types/index.d.ts GitHub Actions / Lint Changed Files
Check warning on line 46 in lib/node_modules/@stdlib/assert/has-same-constructor/docs/types/index.d.ts GitHub Actions / Lint Changed Files
|
||||||
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,44 @@ | ||
/** | ||
* @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 Float64Array = require( '@stdlib/array/float64' ); | ||
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. Require statement order. |
||
var Int32Array = require( '@stdlib/array/int32' ); | ||
var Complex64 = require( '@stdlib/complex/float32' ); | ||
|
||
var DateObj1 = new Date(); | ||
var DateObj2 = new Date(); | ||
var FloatArrObj = new Float64Array( 10 ); | ||
var IntArrObj = new Int32Array( [ 1, 2, 3 ] ); | ||
var ComplexObj = new Complex64( 5.0, 3.0 ); | ||
|
||
console.log( hasSameConstructor( DateObj1, DateObj2 ) ); | ||
// => true | ||
|
||
console.log( hasSameConstructor( DateObj1, FloatArrObj ) ); | ||
// => false | ||
|
||
console.log( hasSameConstructor( FloatArrObj , IntArrObj ) ); | ||
// => false | ||
|
||
console.log( hasSameConstructor( ComplexObj , IntArrObj ) ); | ||
// => |
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.