Skip to content

Commit cfdd473

Browse files
committed
feat: add string/base/first
This commit adds "base" support for returning the first `n` Unicode code units of a provided string.
1 parent 26da930 commit cfdd473

File tree

10 files changed

+576
-0
lines changed

10 files changed

+576
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
# first
22+
23+
> Return the first `n` Unicode code units of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var first = require( '@stdlib/string/base/first' );
31+
```
32+
33+
#### first( str, n )
34+
35+
Returns the first `n` Unicode code units of a string.
36+
37+
```javascript
38+
var out = first( 'last man standing', 1 );
39+
// returns 'l'
40+
41+
out = first( 'Hidden Treasures', 1 );
42+
// returns 'H'
43+
44+
out = first( 'foo bar', 5 );
45+
// returns 'foo b'
46+
47+
out = first( 'foo bar', 10 );
48+
// returns 'foo bar'
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
<!-- eslint no-undef: "error" -->
60+
61+
```javascript
62+
var first = require( '@stdlib/string/base/first' );
63+
64+
var str = first( 'presidential election', 1 );
65+
// returns 'p'
66+
67+
str = first( 'JavaScript', 1 );
68+
// returns 'J'
69+
70+
str = first( 'The Last of the Mohicans', 5 );
71+
// returns 'The L'
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
79+
80+
<section class="related">
81+
82+
</section>
83+
84+
<!-- /.related -->
85+
86+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="links">
89+
90+
</section>
91+
92+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var first = 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+
'beep boop',
38+
'foo bar',
39+
'xyz abc',
40+
'🐶🐮🐷🐰🐸'
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = first( values[ i%values.length ], 1 );
46+
if ( typeof out !== 'string' ) {
47+
b.fail( 'should return a string' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isString( out ) ) {
52+
b.fail( 'should return a string' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( str, n )
3+
Returns the first `n` Unicode code units of a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
n: integer
11+
Number of Unicode code units to return.
12+
13+
Returns
14+
-------
15+
out: string
16+
Output string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'beep', 1 )
21+
'b'
22+
> out = {{alias}}( 'Boop', 1 )
23+
'B'
24+
> out = {{alias}}( 'foo bar', 5 )
25+
'foo b'
26+
27+
See Also
28+
--------
29+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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: 2.0
20+
21+
/**
22+
* Returns the first `n` Unicode code units of a string.
23+
*
24+
* @param str - input string
25+
* @param n - number of code units to return
26+
* @returns output string
27+
*
28+
* @example
29+
* var out = first( 'last man standing', 1 );
30+
* // returns 'l'
31+
*
32+
* @example
33+
* var out = first( 'presidential election', 1 );
34+
* // returns 'p'
35+
*
36+
* @example
37+
* var out = first( 'JavaScript', 1 );
38+
* // returns 'J'
39+
*
40+
* @example
41+
* var out = first( 'Hidden Treasures', 1 );
42+
* // returns 'H'
43+
*
44+
* @example
45+
* var out = first( 'foo bar', 5 );
46+
* // returns 'foo b'
47+
*/
48+
declare function first( str: string, n: number ): string;
49+
50+
51+
// EXPORTS //
52+
53+
export = first;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 first = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
first( 'abc', 1 ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
first( true, 1 ); // $ExpectError
32+
first( false, 1 ); // $ExpectError
33+
first( null, 1 ); // $ExpectError
34+
first( undefined, 1 ); // $ExpectError
35+
first( 5, 1 ); // $ExpectError
36+
first( [], 1 ); // $ExpectError
37+
first( {}, 1 ); // $ExpectError
38+
first( ( x: number ): number => x, 1 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument that is not a number...
42+
{
43+
first( 'abc', true ); // $ExpectError
44+
first( 'abc', false ); // $ExpectError
45+
first( 'abc', null ); // $ExpectError
46+
first( 'abc', 'abc' ); // $ExpectError
47+
first( 'abc', [] ); // $ExpectError
48+
first( 'abc', {} ); // $ExpectError
49+
first( 'abc', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
first(); // $ExpectError
55+
first( 'abc' ); // $ExpectError
56+
first( 'abc', 1, 2 ); // $ExpectError
57+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 first = require( './../lib' );
22+
23+
console.log( first( 'presidential election', 1 ) );
24+
// => 'p'
25+
26+
console.log( first( 'JavaScript', 1 ) );
27+
// => 'J'
28+
29+
console.log( first( 'The Last of the Mohicans', 5 ) );
30+
// => 'The L'
Lines changed: 43 additions & 0 deletions
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+
* Return the first `n` Unicode code units of a string.
23+
*
24+
* @module @stdlib/string/base/first
25+
*
26+
* @example
27+
* var first = require( '@stdlib/string/base/first' );
28+
*
29+
* var out = first( 'last man standing', 1 );
30+
* // returns 'l'
31+
*
32+
* out = first( 'Hidden Treasures', 1 );
33+
* // returns 'H';
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)