Skip to content

Commit 71c1bc4

Browse files
committed
feat: add slice/base/int2slice
1 parent 4fbe560 commit 71c1bc4

File tree

11 files changed

+797
-0
lines changed

11 files changed

+797
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
# int2slice
22+
23+
> Convert an integer to a [`Slice`][@stdlib/slice/ctor] object.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var int2slice = require( '@stdlib/slice/base/int2slice' );
41+
```
42+
43+
<a name="main"></a>
44+
45+
#### int2slice( value, max, strict )
46+
47+
Converts an integer to a [`Slice`][@stdlib/slice/ctor] object, where `max` specifies the index upper bound.
48+
49+
<!-- eslint-disable stdlib/no-redeclare -->
50+
51+
```javascript
52+
var s = int2slice( -4, 10, false );
53+
// returns <Slice>
54+
55+
var start = s.start;
56+
// returns 6
57+
58+
var stop = s.stop;
59+
// returns 7
60+
61+
var step = s.step;
62+
// returns 1
63+
```
64+
65+
When `strict` is `true`, the function returns an error object if an input value exceeds index bounds.
66+
67+
```javascript
68+
var s = int2slice( 100, 10, true );
69+
// returns { 'code': 'ERR_SLICE_OUT_OF_BOUNDS' }
70+
```
71+
72+
A returned error object may have one of the following error codes:
73+
74+
- **ERR_SLICE_OUT_OF_BOUNDS**: a slice exceeds index bounds.
75+
76+
</section>
77+
78+
<!-- /.usage -->
79+
80+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
81+
82+
<section class="notes">
83+
84+
</section>
85+
86+
<!-- /.notes -->
87+
88+
<!-- Package usage examples. -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var int2slice = require( '@stdlib/slice/base/int2slice' );
98+
99+
var s = int2slice( -1, 7, false );
100+
console.log( '%s', s.toString() );
101+
102+
s = int2slice( 3, 5, false );
103+
console.log( '%s', s.toString() );
104+
105+
s = int2slice( -3, 5, false );
106+
console.log( '%s', s.toString() );
107+
108+
s = int2slice( 10, 5, false );
109+
console.log( '%s', s.toString() );
110+
111+
s = int2slice( -10, 5, false );
112+
console.log( '%s', s.toString() );
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="references">
122+
123+
</section>
124+
125+
<!-- /.references -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
</section>
132+
133+
<!-- /.related -->
134+
135+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="links">
138+
139+
[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/ctor
140+
141+
</section>
142+
143+
<!-- /.links -->
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 isSlice = require( '@stdlib/assert/is-slice' );
25+
var pkg = require( './../package.json' ).name;
26+
var int2slice = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
-3,
38+
4,
39+
5,
40+
-1,
41+
-2,
42+
100,
43+
-100
44+
];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
out = int2slice( values[ i%values.length ], 10, false );
49+
if ( typeof out !== 'object' ) {
50+
b.fail( 'should return an object' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isSlice( out ) ) {
55+
b.fail( 'should return a slice object' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( value, max, strict )
3+
Converts an integer to a Slice object.
4+
5+
In strict mode, the function returns an error object if an input value
6+
exceeds index bounds.
7+
8+
A returned error object is a plain object having the following properties:
9+
10+
- code: error code.
11+
12+
A returned error object may have one of the following error codes:
13+
14+
- ERR_SLICE_OUT_OF_BOUNDS: a slice exceeds index bounds.
15+
16+
Parameters
17+
----------
18+
value: integer
19+
Input value.
20+
21+
max: integer
22+
Index upper bound (exclusive).
23+
24+
strict: boolean
25+
Boolean indicating whether to enforce strict bounds checking.
26+
27+
Returns
28+
-------
29+
s: Slice|Object
30+
Slice instance (or an error object).
31+
32+
Examples
33+
--------
34+
> var s = {{alias}}( -1, 5, false );
35+
> s.start
36+
4
37+
> s.stop
38+
5
39+
> s.step
40+
1
41+
42+
See Also
43+
--------
44+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { Slice } from '@stdlib/types/slice';
24+
25+
/**
26+
* Interface describing an error object.
27+
*/
28+
interface ErrorObject {
29+
/**
30+
* Error code.
31+
*/
32+
code: 'ERR_SLICE_OUT_OF_BOUNDS';
33+
}
34+
35+
/**
36+
* Slice return value.
37+
*/
38+
type SliceResult = Slice | ErrorObject;
39+
40+
/**
41+
* Converts an integer to a Slice object.
42+
*
43+
* ## Notes
44+
*
45+
* - If `strict` is `true`, the function returns an error object when an input value exceeds index bounds.
46+
*
47+
* @param value - input value
48+
* @param max - index upper bound (exclusive)
49+
* @param strict - boolean indicating whether to enforce strict bounds checking
50+
* @returns Slice object (or an error object)
51+
*
52+
* @example
53+
* var s = int2slice( -4, 10, false );
54+
* // returns <Slice>
55+
*
56+
* var start = s.start;
57+
* // returns 6
58+
*
59+
* var stop = s.stop;
60+
* // returns 7
61+
*
62+
* var step = s.step;
63+
* // returns 1
64+
*/
65+
declare function int2slice( value: number, max: number, strict: boolean ): SliceResult;
66+
67+
68+
// EXPORTS //
69+
70+
export = int2slice;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 int2slice = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a Slice object or an error object...
25+
{
26+
int2slice( 0, 10, false ); // $ExpectType SliceResult
27+
int2slice( 0, 10, true ); // $ExpectType SliceResult
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is not a number...
31+
{
32+
int2slice( '1', 10, false ); // $ExpectError
33+
int2slice( true, 10, false ); // $ExpectError
34+
int2slice( false, 10, false ); // $ExpectError
35+
int2slice( null, 10, false ); // $ExpectError
36+
int2slice( undefined, 10, false ); // $ExpectError
37+
int2slice( [], 10, false ); // $ExpectError
38+
int2slice( {}, 10, false ); // $ExpectError
39+
int2slice( ( x: number ): number => x, 10, false ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided a second argument which is not a number...
43+
{
44+
int2slice( 0, '1', false ); // $ExpectError
45+
int2slice( 0, true, false ); // $ExpectError
46+
int2slice( 0, false, false ); // $ExpectError
47+
int2slice( 0, null, false ); // $ExpectError
48+
int2slice( 0, undefined, false ); // $ExpectError
49+
int2slice( 0, [], false ); // $ExpectError
50+
int2slice( 0, {}, false ); // $ExpectError
51+
int2slice( 0, ( x: number ): number => x, false ); // $ExpectError
52+
}
53+
54+
// The compiler throws an error if the function is provided a third argument which is not a boolean...
55+
{
56+
int2slice( 0, 10, '1' ); // $ExpectError
57+
int2slice( 0, 10, 1 ); // $ExpectError
58+
int2slice( 0, 10, null ); // $ExpectError
59+
int2slice( 0, 10, undefined ); // $ExpectError
60+
int2slice( 0, 10, [] ); // $ExpectError
61+
int2slice( 0, 10, {} ); // $ExpectError
62+
int2slice( 0, 10, ( x: number ): number => x ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided an unsupported number of arguments...
66+
{
67+
int2slice(); // $ExpectError
68+
int2slice( 0 ); // $ExpectError
69+
int2slice( 0, 10 ); // $ExpectError
70+
int2slice( 0, 10, false, {} ); // $ExpectError
71+
}

0 commit comments

Comments
 (0)