Skip to content

Commit f35ebd9

Browse files
committed
feat: add assert/is-slice
1 parent cf13ba9 commit f35ebd9

File tree

10 files changed

+542
-0
lines changed

10 files changed

+542
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# isSlice
22+
23+
> Test if a value is a [`Slice`][@stdlib/slice/ctor].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isSlice = require( '@stdlib/assert/is-slice' );
31+
```
32+
33+
#### isSlice( value )
34+
35+
Tests if a value is a [`Slice`][@stdlib/slice/ctor].
36+
37+
```javascript
38+
var Slice = require( '@stdlib/slice/ctor' );
39+
40+
var s = new Slice( 0, 10 );
41+
42+
var bool = isSlice( s );
43+
// returns true
44+
```
45+
46+
</section>
47+
48+
<!-- /.usage -->
49+
50+
<section class="examples">
51+
52+
## Examples
53+
54+
<!-- eslint no-undef: "error" -->
55+
56+
```javascript
57+
var Slice = require( '@stdlib/slice/ctor' );
58+
var isSlice = require( '@stdlib/assert/is-slice' );
59+
60+
var out = isSlice( new Slice( 0, 10, 1 ) );
61+
// returns true
62+
63+
out = isSlice( {} );
64+
// returns false
65+
66+
out = isSlice( null );
67+
// returns false
68+
```
69+
70+
</section>
71+
72+
<!-- /.examples -->
73+
74+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
75+
76+
<section class="related">
77+
78+
</section>
79+
80+
<!-- /.related -->
81+
82+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
83+
84+
<section class="links">
85+
86+
[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/ctor
87+
88+
</section>
89+
90+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 Slice = require( '@stdlib/slice/ctor' );
26+
var pkg = require( './../package.json' ).name;
27+
var isSlice = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::true', function benchmark( b ) {
33+
var values;
34+
var bool;
35+
var i;
36+
37+
values = [
38+
new Slice( 1, 2 ),
39+
new Slice( 3, 4 ),
40+
new Slice( 5, 6 ),
41+
new Slice( 7, 8 )
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
bool = isSlice( values[ i%values.length ] );
47+
if ( typeof bool !== 'boolean' ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isBoolean( bool ) ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+'::false', function benchmark( b ) {
60+
var values;
61+
var bool;
62+
var i;
63+
64+
values = [
65+
'5',
66+
5,
67+
NaN,
68+
true,
69+
false,
70+
[],
71+
{}
72+
];
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
bool = isSlice( values[ i%values.length ] );
77+
if ( typeof bool !== 'boolean' ) {
78+
b.fail( 'should return a boolean' );
79+
}
80+
}
81+
b.toc();
82+
if ( !isBoolean( bool ) ) {
83+
b.fail( 'should return a boolean' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a Slice object.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether a value is a Slice object.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( new {{alias:@stdlib/slice/ctor}}( 10 ) )
18+
true
19+
> bool = {{alias}}( 3.14 )
20+
false
21+
> bool = {{alias}}( {} )
22+
false
23+
24+
See Also
25+
--------
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
/// <reference types="@stdlib/types"/>
22+
23+
import { Slice } from '@stdlib/types/slice';
24+
25+
/**
26+
* Tests if a value is a Slice object.
27+
*
28+
* @param value - value to test
29+
* @returns boolean indicating if a value is a Slice object
30+
*
31+
* @example
32+
* var Slice = require( `@stdlib/slice/ctor` );
33+
*
34+
* var s = new Slice( 0, 10, 1 );
35+
*
36+
* var bool = isSlice( s );
37+
* // returns true
38+
*/
39+
declare function isSlice( value: any ): value is Slice;
40+
41+
42+
// EXPORTS //
43+
44+
export = isSlice;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 Slice = require( '@stdlib/slice/ctor' );
20+
import isSlice = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a boolean...
26+
{
27+
isSlice( new Slice( 0, 10, 1 ) ); // $ExpectType boolean
28+
isSlice( { 'start': 0, 'stop': 10, 'step': 1 } ); // $ExpectType boolean
29+
isSlice( 123 ); // $ExpectType boolean
30+
}
31+
32+
// The compiler throws an error if the function is provided an unsupported number of arguments...
33+
{
34+
isSlice(); // $ExpectError
35+
isSlice( new Slice( 0, 10, 1 ), 123 ); // $ExpectError
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 Slice = require( '@stdlib/slice/ctor' );
22+
var isSlice = require( './../lib' );
23+
24+
console.log( isSlice( new Slice( 0, 10, 1 ) ) );
25+
// => true
26+
27+
console.log( isSlice( {} ) );
28+
// => false
29+
30+
console.log( isSlice( null ) );
31+
// => false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 a value is a Slice object.
23+
*
24+
* @module @stdlib/assert/is-slice
25+
*
26+
* @example
27+
* var Slice = require( '@stdlib/slice/ctor' );
28+
* var isSlice = require( '@stdlib/assert/is-slice' );
29+
*
30+
* var s = new Slice( 0, 10, 2 );
31+
*
32+
* var bool = isSlice( s );
33+
* // returns true
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)