Skip to content

Commit fb675fb

Browse files
committed
feat: add slice/base/slice2seq
1 parent d75d35c commit fb675fb

File tree

10 files changed

+768
-0
lines changed

10 files changed

+768
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
# slice2seq
22+
23+
> Convert a [`Slice`][@stdlib/slice/ctor] object to a subsequence string.
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 slice2seq = require( '@stdlib/slice/base/slice2seq' );
41+
```
42+
43+
<a name="main"></a>
44+
45+
#### slice2seq( str )
46+
47+
Converts a [`Slice`][@stdlib/slice/ctor] object to a subsequence string.
48+
49+
```javascript
50+
var Slice = require( '@stdlib/slice/ctor' );
51+
52+
var str = slice2seq( new Slice( 0, 5, 1 ) );
53+
// returns '0:5:1'
54+
```
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
61+
62+
<section class="notes">
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<!-- Package usage examples. -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var Slice = require( '@stdlib/slice/ctor' );
78+
var slice2seq = require( '@stdlib/slice/base/slice2seq' );
79+
80+
var str = slice2seq( new Slice( null, null, null ) );
81+
// returns ':'
82+
83+
str = slice2seq( new Slice() );
84+
// returns ':'
85+
86+
str = slice2seq( new Slice( null ) );
87+
// returns ':'
88+
89+
str = slice2seq( new Slice( 10 ) );
90+
// returns ':10'
91+
92+
str = slice2seq( new Slice( -1 ) );
93+
// returns ':-1'
94+
95+
str = slice2seq( new Slice( 2, 10 ) );
96+
// returns '2:10'
97+
98+
str = slice2seq( new Slice( -2, 10 ) );
99+
// returns '-2:10'
100+
101+
str = slice2seq( new Slice( -2, -10 ) );
102+
// returns '-2:-10'
103+
104+
str = slice2seq( new Slice( 2, null ) );
105+
// returns '2:'
106+
107+
str = slice2seq( new Slice( null, 10 ) );
108+
// returns ':10'
109+
110+
str = slice2seq( new Slice( 2, 10, 2 ) );
111+
// returns '2:10:2'
112+
113+
str = slice2seq( new Slice( -1, null, -1 ) );
114+
// returns '-1::-1'
115+
116+
str = slice2seq( new Slice( -1, -5, -1 ) );
117+
// returns '-1:-5:-1'
118+
119+
str = slice2seq( new Slice( -1, 10, -1 ) );
120+
// returns '-1:10:-1'
121+
122+
str = slice2seq( new Slice( 1, -1, 2 ) );
123+
// returns '1:-1:2'
124+
125+
str = slice2seq( new Slice( null, null, -1 ) );
126+
// returns '::-1'
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- 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. -->
134+
135+
<section class="references">
136+
137+
</section>
138+
139+
<!-- /.references -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/ctor
154+
155+
</section>
156+
157+
<!-- /.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) 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 Slice = require( '@stdlib/slice/ctor' );
25+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var slice2seq = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
new Slice( null, null, null ),
39+
new Slice( 0, 10, 2 ),
40+
new Slice( null, 10, 2 ),
41+
new Slice( 0, null, 2 ),
42+
new Slice( 0, 10, null ),
43+
new Slice( -1, -10, -1 )
44+
];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
out = slice2seq( values[ i%values.length ] );
49+
if ( typeof out !== 'string' ) {
50+
b.fail( 'should return a string' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isString( out ) ) {
55+
b.fail( 'should return a string' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
{{alias}}( slice )
3+
Converts a Slice object to a subsequence string.
4+
5+
Parameters
6+
----------
7+
slice: Slice
8+
Input slice.
9+
10+
Returns
11+
-------
12+
out: string
13+
Subsequence string.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( new {{alias:@stdlib/slice/ctor}}( 1, 10, 1 ) )
18+
'1:10:1'
19+
> out = {{alias}}( new {{alias:@stdlib/slice/ctor}}( null, 10 ) )
20+
':10'
21+
22+
See Also
23+
--------
24+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
* Converts a Slice object to a subsequence string.
27+
*
28+
* @param slice - input slice
29+
* @returns subsequence string
30+
*
31+
* @example
32+
* var str = slice2seq( new Slice( null, null, null ) );
33+
* // returns ':'
34+
*
35+
* str = slice2seq( new Slice( null ) );
36+
* // returns ':'
37+
*
38+
* str = slice2seq( new Slice( 10 ) );
39+
* // returns ':10'
40+
*
41+
* str = slice2seq( new Slice( -1 ) );
42+
* // returns ':-1'
43+
*
44+
* str = slice2seq( new Slice( 2, 10 ) );
45+
* // returns '2:10'
46+
*
47+
* str = slice2seq( new Slice( -2, 10 ) );
48+
* // returns '-2:10'
49+
*
50+
* str = slice2seq( new Slice( -2, -10 ) );
51+
* // returns '-2:-10'
52+
*
53+
* str = slice2seq( new Slice( 2, null ) );
54+
* // returns '2:'
55+
*
56+
* str = slice2seq( new Slice( null, 10 ) );
57+
* // returns ':10'
58+
*
59+
* str = slice2seq( new Slice( 2, 10, 2 ) );
60+
* // returns '2:10:2'
61+
*
62+
* str = slice2seq( new Slice( -1, null, -1 ) );
63+
* // returns '-1::-1'
64+
*
65+
* str = slice2seq( new Slice( -1, -5, -1 ) );
66+
* // returns '-1:-5:-1'
67+
*
68+
* str = slice2seq( new Slice( -1, 10, -1 ) );
69+
* // returns '-1:10:-1'
70+
*
71+
* str = slice2seq( new Slice( 1, -1, 2 ) );
72+
* // returns '1:-1:2'
73+
*
74+
* str = slice2seq( new Slice( null, null, -1 ) );
75+
* // returns '::-1'
76+
*/
77+
declare function slice2seq( slice: Slice ): string;
78+
79+
80+
// EXPORTS //
81+
82+
export = slice2seq;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 slice2seq = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a string...
26+
{
27+
slice2seq( new Slice( 0, 10, 2 ) ); // $ExpectType string
28+
slice2seq( new Slice( null, null, -2 ) ); // $ExpectType string
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not a slice object...
32+
{
33+
slice2seq( '1' ); // $ExpectError
34+
slice2seq( 1 ); // $ExpectError
35+
slice2seq( true ); // $ExpectError
36+
slice2seq( false ); // $ExpectError
37+
slice2seq( null ); // $ExpectError
38+
slice2seq( undefined ); // $ExpectError
39+
slice2seq( [] ); // $ExpectError
40+
slice2seq( {} ); // $ExpectError
41+
slice2seq( ( x: number ): number => x ); // $ExpectError
42+
}
43+
44+
// The compiler throws an error if the function is provided an unsupported number of arguments...
45+
{
46+
slice2seq(); // $ExpectError
47+
slice2seq( new Slice( 0, 10, 2 ), {} ); // $ExpectError
48+
}

0 commit comments

Comments
 (0)