Skip to content

Commit cc26fd2

Browse files
yernar707kgrytePlaneshifter
authored
feat: add string/first
PR-URL: #989 Closes: #853 Co-authored-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 6ed3486 commit cc26fd2

15 files changed

Lines changed: 1273 additions & 0 deletions

File tree

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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 visual character(s) of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var first = require( '@stdlib/string/first' );
31+
```
32+
33+
#### first( str\[, n] )
34+
35+
Returns the first visual character of a string.
36+
37+
```javascript
38+
var out = first( 'last man standing' );
39+
// returns 'l'
40+
41+
out = first( 'Hidden Treasures' );
42+
// returns 'H'
43+
```
44+
45+
If provided a second argument, the function returns the first `n` characters.
46+
47+
```javascript
48+
var out = first( 'foo bar', 5 );
49+
// returns 'foo b'
50+
51+
out = first( 'foo bar', 10 );
52+
// returns 'foo bar'
53+
```
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<section class="examples">
60+
61+
## Examples
62+
63+
<!-- eslint no-undef: "error" -->
64+
65+
```javascript
66+
var first = require( '@stdlib/string/first' );
67+
68+
var str = first( 'last man standing' );
69+
// returns 'l'
70+
71+
str = first( 'presidential election' );
72+
// returns 'p'
73+
74+
str = first( 'javaScript' );
75+
// returns 'j'
76+
77+
str = first( 'Hidden Treasures' );
78+
// returns 'H'
79+
80+
str = first( 'The Last of the Mohicans', 5 );
81+
// returns 'The L'
82+
83+
str = first( '🐶🐮🐷🐰🐸', 2 );
84+
// returns '🐶🐮'
85+
86+
str = first( '🐶🐮🐷🐰🐸', 10 );
87+
// returns '🐶🐮🐷🐰🐸'
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
* * *
95+
96+
<section class="cli">
97+
98+
## CLI
99+
100+
<section class="usage">
101+
102+
### Usage
103+
104+
```text
105+
Usage: first [options] [<string>]
106+
107+
Options:
108+
109+
-h, --help Print this message.
110+
-V, --version Print the package version.
111+
--n num Number of characters to return. Default: 1.
112+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
113+
```
114+
115+
</section>
116+
117+
<!-- /.usage -->
118+
119+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="notes">
122+
123+
### Notes
124+
125+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
126+
127+
```bash
128+
# Not escaped...
129+
$ echo -n $'beep\nboop' | first --split /\r?\n/
130+
131+
# Escaped...
132+
$ echo -n $'beep\nboop' | first --split /\\r?\\n/
133+
```
134+
135+
- The implementation ignores trailing delimiters.
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<section class="examples">
142+
143+
### Examples
144+
145+
```bash
146+
$ first beep
147+
b
148+
```
149+
150+
To use as a [standard stream][standard-streams],
151+
152+
```bash
153+
$ echo -n 'beep\nboop' | first --n=2
154+
be
155+
bo
156+
```
157+
158+
By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option.
159+
160+
```bash
161+
$ echo -n 'beep\tboop' | first --split '\t'
162+
b
163+
b
164+
```
165+
166+
</section>
167+
168+
<!-- /.examples -->
169+
170+
</section>
171+
172+
<!-- /.cli -->
173+
174+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
175+
176+
<section class="related">
177+
178+
</section>
179+
180+
<!-- /.related -->
181+
182+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
183+
184+
<section class="links">
185+
186+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
187+
188+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
189+
190+
</section>
191+
192+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = first( values[ i%values.length ] );
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( out ) ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2018 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var CLI = require( '@stdlib/cli/ctor' );
28+
var stdin = require( '@stdlib/process/read-stdin' );
29+
var stdinStream = require( '@stdlib/streams/node/stdin' );
30+
var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP;
31+
var isRegExpString = require( '@stdlib/assert/is-regexp-string' );
32+
var reFromString = require( '@stdlib/utils/regexp-from-string' );
33+
var first = require( './../lib' );
34+
35+
36+
// MAIN //
37+
38+
/**
39+
* Main execution sequence.
40+
*
41+
* @private
42+
* @returns {void}
43+
*/
44+
function main() {
45+
var split;
46+
var flags;
47+
var args;
48+
var cli;
49+
var n;
50+
51+
// Create a command-line interface:
52+
cli = new CLI({
53+
'pkg': require( './../package.json' ),
54+
'options': require( './../etc/cli_opts.json' ),
55+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
56+
'encoding': 'utf8'
57+
})
58+
});
59+
60+
// Get any provided command-line options:
61+
flags = cli.flags();
62+
if ( flags.help || flags.version ) {
63+
return;
64+
}
65+
if ( flags.n ) {
66+
n = parseInt( flags.n, 10 );
67+
} else {
68+
n = 1;
69+
}
70+
71+
// Get any provided command-line arguments:
72+
args = cli.args();
73+
74+
// Check if we are receiving data from `stdin`...
75+
if ( !stdinStream.isTTY ) {
76+
if ( flags.split ) {
77+
if ( !isRegExpString( flags.split ) ) {
78+
flags.split = '/'+flags.split+'/';
79+
}
80+
split = reFromString( flags.split );
81+
} else {
82+
split = RE_EOL;
83+
}
84+
return stdin( onRead );
85+
}
86+
console.log( first( args[ 0 ], n ) ); // eslint-disable-line no-console
87+
88+
/**
89+
* Callback invoked upon reading from `stdin`.
90+
*
91+
* @private
92+
* @param {(Error|null)} error - error object
93+
* @param {Buffer} data - data
94+
* @returns {void}
95+
*/
96+
function onRead( error, data ) {
97+
var lines;
98+
var i;
99+
if ( error ) {
100+
return cli.error( error );
101+
}
102+
lines = data.toString().split( split );
103+
104+
// Remove any trailing separators (e.g., trailing newline)...
105+
if ( lines[ lines.length-1 ] === '' ) {
106+
lines.pop();
107+
}
108+
for ( i = 0; i < lines.length; i++ ) {
109+
console.log( first( lines[ i ], n ) ); // eslint-disable-line no-console
110+
}
111+
}
112+
}
113+
114+
main();
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 visual character(s) of a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
n: integer (optional)
11+
Number of characters to return. Default: 1.
12+
13+
Returns
14+
-------
15+
out: string
16+
Updated string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'beep' )
21+
'b'
22+
> out = {{alias}}( 'Boop' )
23+
'B'
24+
> out = {{alias}}( 'foo bar', 5 )
25+
'foo b'
26+
27+
See Also
28+
--------
29+

0 commit comments

Comments
 (0)