Skip to content

Commit 9ad9c12

Browse files
adityacodes30kgrytePlaneshifter
authored and
Utkarsh Gupta
committed
feat: add assert/is-same-date-object
PR-URL: stdlib-js#1348 Closes: stdlib-js#1337 --------- Signed-off-by: Aditya Sapra <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 5541bd3 commit 9ad9c12

File tree

10 files changed

+596
-0
lines changed

10 files changed

+596
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isSameDateObject
22+
23+
> Test if two values are [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) objects corresponding to the same date and time.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isSameDateObject = require( '@stdlib/assert/is-same-date-object' );
31+
```
32+
33+
#### isSameDateObject( d1, d2 )
34+
35+
Tests if two values are both Date objects corresponding to the same date and time.
36+
37+
```javascript
38+
var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
39+
var d2 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
40+
var bool = isSameDateObject( d1, d2 );
41+
// returns true
42+
43+
bool = isSameDateObject( d1, new Date( 2023, 11, 31, 23, 59, 59, 78 ) );
44+
// returns false
45+
```
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="examples">
52+
53+
## Examples
54+
55+
<!-- eslint no-undef: "error" -->
56+
57+
```javascript
58+
var isSameDateObject = require( '@stdlib/assert/is-same-date-object' );
59+
60+
var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
61+
var d2 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
62+
63+
var bool = isSameDateObject( d1, d2 );
64+
// returns true
65+
66+
d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
67+
d2 = new Date( 2024, 11, 31, 23, 59, 59, 78 );
68+
69+
bool = isSameDateObject( d1, d2 );
70+
// returns false
71+
72+
d1 = new Date();
73+
d2 = new Date( '2024-12-31T23:59:59.999' );
74+
75+
bool = isSameDateObject( d1, d2 );
76+
// returns false
77+
78+
var d3 = new Date( 2024, 11, 31 );
79+
var d4 = new Date( 'December 31, 2024 23:59:59:999' );
80+
81+
bool = isSameDateObject( d1, d3 );
82+
// returns false
83+
84+
bool = isSameDateObject( d2, d4 );
85+
// returns true
86+
```
87+
88+
</section>
89+
90+
91+
<!-- /.examples -->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var isSameDateObject = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var date1;
34+
var date2;
35+
var bool;
36+
var i;
37+
values = [
38+
'5',
39+
'Date',
40+
new Date( 'December 31, 2024 23:59:59:999' ),
41+
new Date( '2024-12-31T23:59:59.999' ),
42+
NaN,
43+
true,
44+
false,
45+
null
46+
];
47+
date1 = new Date();
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
date2 = values[ i%values.length ];
51+
bool = isSameDateObject( date1, date2 );
52+
if ( typeof bool !== 'boolean' ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isBoolean( bool ) ) {
58+
b.fail( 'should return a boolean' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( d1, d2 )
3+
Tests if two values are both Date objects corresponding
4+
to the same date and time.
5+
6+
Parameters
7+
----------
8+
d1: any
9+
First input value.
10+
d2: any
11+
Second input value.
12+
13+
Returns
14+
-------
15+
bool: boolean
16+
Boolean indicating whether both values are Date objects
17+
corresponding to the same date and time.
18+
19+
Examples
20+
--------
21+
> var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
22+
> var d2 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
23+
> var bool = {{alias}}( d1, d2 )
24+
true
25+
> var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
26+
> var d2 = new Date( 2024, 11, 31, 23, 59, 59, 78 );
27+
> var bool = {{alias}}( d1, d2 )
28+
false
29+
30+
See Also
31+
--------
32+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Tests if two values are both Date objects corresponding to the same date and time.
23+
*
24+
* @param d1 - first input value
25+
* @param d2 - second input value
26+
* @returns boolean indicating whether both are Date objects corresponding to the same date and time.
27+
*
28+
* @example
29+
* var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
30+
* var d2 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
31+
*
32+
* var bool = isSameDateObject( d1, d2 );
33+
* // returns true
34+
*
35+
* @example
36+
* var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
37+
* var d2 = new Date( 2024, 11, 31, 23, 59, 59, 78 );
38+
*
39+
* var bool = isSameDateObject( d1, d2 );
40+
* // returns false
41+
*/
42+
declare function isSameDateObject( d1: any, d2: any ): boolean;
43+
44+
45+
// EXPORTS //
46+
47+
export = isSameDateObject;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import isSameDateObject = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isSameDateObject( new Date(), new Date() ); // $ExpectType boolean
27+
isSameDateObject( null, null ); // $ExpectType boolean
28+
isSameDateObject( 'beep', 'boop' ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided an unsupported number of arguments...
32+
{
33+
isSameDateObject(); // $ExpectError
34+
isSameDateObject( new Date() ); // $ExpectError
35+
isSameDateObject( 'beep', 'beep', new Date() ); // $ExpectError
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var isSameDateObject = require( './../lib' );
22+
23+
var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
24+
var d2 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
25+
26+
var bool = isSameDateObject( d1, d2 );
27+
console.log( bool );
28+
// => true
29+
30+
d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
31+
d2 = new Date( 2024, 11, 31, 23, 59, 59, 78 );
32+
33+
bool = isSameDateObject( d1, d2 );
34+
console.log( bool );
35+
// => false
36+
37+
d1 = new Date();
38+
d2 = new Date( '2024-12-31T23:59:59.999' );
39+
bool = isSameDateObject( d1, d2 );
40+
// returns false
41+
42+
var d3 = new Date( 2024, 11, 31 );
43+
var d4 = new Date( 'December 31, 2024 23:59:59:999' );
44+
45+
bool = isSameDateObject( d1, d3 );
46+
console.log( bool );
47+
// => false
48+
49+
bool = isSameDateObject( d2, d4 );
50+
console.log( bool );
51+
// => true
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Test if two arguments are both Date objects corresponding to the same date and time.
23+
*
24+
* @module @stdlib/assert/is-same-date-object
25+
*
26+
* @example
27+
* var isSameDateObject = require( '@stdlib/assert/is-same-date-object' );
28+
*
29+
* var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
30+
* var d2 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
31+
*
32+
* var bool = isSameDateObject( d1, d2 );
33+
* // returns true
34+
*
35+
* @example
36+
* var isSameDateObject = require( '@stdlib/assert/is-same-date-object' );
37+
*
38+
* var d1 = new Date( 2024, 11, 31, 23, 59, 59, 999 );
39+
* var d2 = new Date( 2024, 11, 31, 23, 59, 59, 78 );
40+
*
41+
* var bool = isSameDateObject( d1, d2 );
42+
* // returns false
43+
*/
44+
45+
// MODULES //
46+
47+
var main = require( './main.js' );
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = main;

0 commit comments

Comments
 (0)