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 12 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
93 changes: 93 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,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.
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 isArrayLikeObject = require( '@stdlib/assert/has-same-constructor' );
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
```

#### hasSameConstructor( value , value )
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
#### hasSameConstructor( value , value )
#### hasSameConstructor( v1, v2 )


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

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

Choose a reason for hiding this comment

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

Instead of using the String constructor, I suggest use more common examples. E.g., comparing a generic array [ 1, 2, 3 ], with a typed array instance (e.g., new Int32Array( [ 1, 2, 3 ] )). You can also use stdlib classes, such as @stdlib/complex/float64 and @stdlib/complex/float32.

var StringPrimitive = "World";

console.log( hasSameConstructor( DateObject1, DateObject2 ) );
// => 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


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>
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.

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


/**
* Benchmark the hasSameConstructor function when provided with same value types.
*/
Copy link
Member

Choose a reason for hiding this comment

The 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 );
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 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();
});
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();
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.
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();
* 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
*/

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;
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,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
Loading
Loading