Skip to content
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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 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
121 changes: 121 additions & 0 deletions lib/node_modules/@stdlib/assert/has-same-constructor/README.md
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> Test if two values have same constructor.
> Test if two values have the 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tests if two values have same constructor.
Tests if two values have the same constructor.


<!-- 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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var obj3 = new String("Hello");
var obj3 = new String( 'Hello' );

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>


Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!-- eslint-disable no-new-wrappers -->

<!-- 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
Copy link
Member

Choose a reason for hiding this comment

The 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>
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* * *
## 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>


</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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!-- <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> -->

</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 //
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// MAIN //
// MAIN //


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();
});
35 changes: 35 additions & 0 deletions lib/node_modules/@stdlib/assert/has-same-constructor/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{alias}}( value )
Tests if two values have same constructor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tests if two values have same constructor.
Tests if two values have the same constructor.

Here and elsewhere.


If provided with two values with different constructors, the function returns `false`.

Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If provided with two values with different constructors, the function returns `false`.

Parameters
----------
value: any
Value to test.

Returns
-------
bool: boolean
Boolean indicating whether two values have same constructor.

Examples
--------
> var obj1 = new Date();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Member

Choose a reason for hiding this comment

The 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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @returns {boolean} Returns `true` if both values have the same constructor, else `false`.
* @returns boolean indicating whether two values have the same constructor


Copy link
Member

Choose a reason for hiding this comment

The 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
*/

Copy link
Member

Choose a reason for hiding this comment

The 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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

There must be a space after this paren

Check warning on line 46 in lib/node_modules/@stdlib/assert/has-same-constructor/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 46 in lib/node_modules/@stdlib/assert/has-same-constructor/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check failure on line 46 in lib/node_modules/@stdlib/assert/has-same-constructor/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

There must be a space before this paren
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
declare function hasSameConstructor(a: any, b: any): boolean;
declare function hasSameConstructor( a: any, b: any ): boolean;


// 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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import hasSameConstructor from './index';
import hasSameConstructor = require( './index' );

Please follow practices observed in other packages.


// TESTS //

// The function returns a boolean...
{
hasSameConstructor(new Date(), new String("Hello")); // $ExpectType boolean
Copy link
Member

Choose a reason for hiding this comment

The 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' );
Copy link
Member

Choose a reason for hiding this comment

The 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 ) );
// =>
Loading
Loading