Skip to content

feat: add assert/has-same-constructor #5446

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

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
138 changes: 138 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,138 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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 whether two provided arguments have the same constructor.

<section class="usage">

## Usage

```javascript
var hasSameConstructor = require( '@stdlib/assert/has-same-constructor' );
```

#### hasSameConstructor( x, y )

Tests if two arguments `x` and `y` have the same constructor.

```javascript
var bool = hasSameConstructor( 5, 10 );
// returns true

bool = hasSameConstructor( 5, [] );
// returns false

bool = hasSameConstructor( [], [] );
// returns true
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The function does **not** throw when provided `null` or `undefined`. Instead, the function returns `false`.

```javascript
var bool = hasSameConstructor(null, 5);
// returns false

bool = hasSameConstructor(void 0, 5);
// returns false
```

- Value arguments other than `null` or `undefined` are coerced to `objects`.

```javascript
var bool = hasSameConstructor( 'beep', 'length' );
// returns true
```

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint-disable object-curly-newline, object-curly-spacing -->

<!-- eslint no-undef: "error" -->

```javascript
var hasSameConstructor = require( '@stdlib/assert/has-same-constructor' );

var bool = hasSameConstructor([1, 2, 3], ['a', 'b', 'c']);
// returns true

bool = hasSameConstructor([1, 2, 3], { 'key': 'value' });
// returns false

bool = hasSameConstructor({ 'name': 'Alice' }, { 'age': 30 });
// returns true

bool = hasSameConstructor({'name': 'Alice' }, new Date());
// returns false

bool = hasSameConstructor( new Date(), new Date() );
// returns true

bool = hasSameConstructor( null, { 'name': 'Alice' } );
// returns false

bool = hasSameConstructor( void 0, [1, 2, 3] );
// returns false

bool = hasSameConstructor( 42, 3.14 );
// returns true

bool = hasSameConstructor( 42, 'Hello' );
// returns false
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</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">

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 //

bench( pkg, function benchmark( b ) {
var values;
var bool;
var x;
var y;
var i;

values = [
'5',
5,
NaN,
true,
false,
null,
void 0,
[],
{},
new Date(),
function noop() {}
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = values[ i%values.length ];
y = values[ (i+1)%values.length ];
bool = hasSameConstructor( x, y );
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();
});
28 changes: 28 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,28 @@

{{alias}}( x, y )
Tests if two values have the same constructor.

Parameters
----------
x: any
First input value.

y: any
Second input value.

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

Examples
--------
> var bool = {{alias}}( new Date(), new Date() );
true
> bool = {{alias}}( new Date(), 'Hello');
false
> bool = {{alias}}(new String('Hello'), 'World');
true

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 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 x - first input value
* @param y - second input value
* @returns boolean indicating whether two arguments have the same constructor
*
* @example
* var bool = hasSameConstructor(new Date(), new Date());
* // returns true
*
* @example
* var bool = hasSameConstructor("Hello", new Date());
* // returns false
*
* @example
* var bool = hasSameConstructor(new String("Hello"), "World");
* // returns true
*/
declare function hasSameConstructor( x: any, y: any ): boolean;

Check warning on line 40 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 40 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


// EXPORTS //

export = hasSameConstructor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 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 = require( './index' );


// TESTS //

// The function returns a boolean...
{
hasSameConstructor( new Date(), new String('Hello' ) ); // $ExpectType boolean
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,63 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 bool = hasSameConstructor([1, 2, 3], ['a', 'b', 'c']);
console.log( bool );
// => true

bool = hasSameConstructor([1, 2, 3], { key: 'value' });
console.log( bool );
// => false

bool = hasSameConstructor({ name: 'Alice' }, { age: 30 });
console.log( bool );
// => true

bool = hasSameConstructor({ name: 'Alice' }, new Date());
console.log( bool );
// => false

bool = hasSameConstructor( new Date(), new Date() );
console.log( bool );
// => true

bool = hasSameConstructor(null, { name: 'Alice' });
console.log( bool );
// => false

bool = hasSameConstructor(undefined, [1, 2, 3]);
console.log( bool );
// => false

bool = hasSameConstructor( 42, 3.14 );
console.log( bool );
// => true

bool = hasSameConstructor( 42, 'Hello' );
console.log( bool );
// => false

bool = hasSameConstructor(function () {}, () => {});
console.log( bool );
// => true
Loading
Loading