Skip to content

Commit

Permalink
feat(assert): Add @stdlib/assert/has-same-constructor
Browse files Browse the repository at this point in the history
---
type: pre_push_report
description: Results of running various checks prior to pushing changes.
report:
  - task: run_javascript_examples
    status: na
  - task: run_c_examples
    status: na
  - task: run_cpp_examples
    status: na
  - task: run_javascript_readme_examples
    status: na
  - task: run_c_benchmarks
    status: na
  - task: run_cpp_benchmarks
    status: na
  - task: run_fortran_benchmarks
    status: na
  - task: run_javascript_benchmarks
    status: na
  - task: run_julia_benchmarks
    status: na
  - task: run_python_benchmarks
    status: na
  - task: run_r_benchmarks
    status: na
  - task: run_javascript_tests
    status: na
---
  • Loading branch information
abhishekblue committed Feb 25, 2025
1 parent ae8e7b9 commit 3285233
Show file tree
Hide file tree
Showing 10 changed files with 708 additions and 0 deletions.
155 changes: 155 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,155 @@
<!--
@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

> Tests if two values have the same constructor.
<section class="usage">

## Usage

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

#### hasSameConstructor( x, y )

Returns a `boolean` indicating whether two given values has same contructor function.

```javascript
var x = new Number(5);
var y = new Number(10);
var bool = hasSameConstructor( x, y );
// returns true

var x = new Number(5);
var y = [];
var bool = hasSameConstructor( x, y );
// returns false

var x = [];
var y = [];
var bool = hasSameConstructor( x, y );
// 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

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

- Value arguments other than `null` or `undefined` are coerced to `objects`(e.g., “Primitives like `5` or `'hello'` are autoboxed to objects, such as `new Number(5)` or `new String('hello')`, for constructor checking”).

```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( undefined, [1, 2, 3] );
// returns false

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

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

bool = hasSameConstructor( function() {}, () => {} );
// returns true
```

</section>

<!-- /.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-same-constructor`][@stdlib/assert/has-same-constructor]</span><span class="delimiter">: </span><span class="description">test if two values have the same constructor.</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">

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

[@stdlib/assert/has-same-constructor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/has-same-constructor

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @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 //

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();
});
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
----------
a: any
First input value.

b: any
Second input value.

Returns
-------
bool: boolean
Boolean indicating whether two values have 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,46 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2019 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
*

Check failure on line 27 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

Trailing spaces not allowed
* @example
* var bool = hasSameConstructor(new Date(), new Date());
* // returns true
*

Check failure on line 31 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

Trailing spaces not allowed
* @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 failure on line 41 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 41 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 41 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 41 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


// EXPORTS //

export = hasSameConstructor;

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

Newline required at end of file but not found
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2019 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
}
Loading

0 comments on commit 3285233

Please sign in to comment.