Skip to content

Commit a503750

Browse files
committed
Add package to return part of string before a substring
1 parent 43b56a5 commit a503750

File tree

15 files changed

+1094
-0
lines changed

15 files changed

+1094
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# substringBefore
22+
23+
> Return the part of a string before a specified substring.
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 substringBefore = require( '@stdlib/string/substring-before' );
41+
```
42+
43+
#### substringBefore( str, search )
44+
45+
Returns the part of a string before a specified substring.
46+
47+
```javascript
48+
var str = 'beep boop';
49+
var out = substringBefore( str, ' ' );
50+
// returns 'beep'
51+
52+
out = substringBefore( str, 'o' );
53+
// returns 'beep b'
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 substringBefore = require( '@stdlib/string/substring-before' );
78+
79+
var out = substringBefore( 'beep boop', 'p' );
80+
// returns 'bee'
81+
82+
out = substringBefore( 'Hello World!', 'xyz' );
83+
// returns 'Hello World!'
84+
85+
out = substringBefore( 'Hello World!', '' );
86+
// returns ''
87+
88+
out = substringBefore( '', 'xyz' );
89+
// returns ''
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
97+
<!-- Section for describing a command-line interface. -->
98+
99+
* * *
100+
101+
<section class="cli">
102+
103+
## CLI
104+
105+
<!-- CLI usage documentation. -->
106+
107+
<section class="usage">
108+
109+
### Usage
110+
111+
```text
112+
Usage: substring-before [options] --search=<string> [<string>]
113+
114+
Options:
115+
116+
-h, --help Print this message.
117+
-V, --version Print the package version.
118+
--search string Search string.
119+
```
120+
121+
</section>
122+
123+
<!-- /.usage -->
124+
125+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="notes">
128+
129+
</section>
130+
131+
<!-- /.notes -->
132+
133+
<!-- CLI usage examples. -->
134+
135+
<section class="examples">
136+
137+
### Examples
138+
139+
```bash
140+
$ substring-before abcdefg --search d
141+
abc
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
</section>
149+
150+
<!-- /.cli -->
151+
152+
<!-- 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. -->
153+
154+
<section class="references">
155+
156+
</section>
157+
158+
<!-- /.references -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
</section>
165+
166+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 fromCodePoint = require( '@stdlib/string/from-code-point' );
26+
var pkg = require( './../package.json' ).name;
27+
var substringBefore = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var out;
34+
var str;
35+
var i;
36+
37+
str = 'To be, or not to be, that is the question.';
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
out = substringBefore( str, fromCodePoint( i%126 ) );
42+
if ( !isString( out ) ) {
43+
b.fail( 'should return a string' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isString( out ) ) {
48+
b.fail( 'should return a string' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2021 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 reEOL = require( '@stdlib/regexp/eol' );
31+
var substringBefore = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
/**
37+
* Main execution sequence.
38+
*
39+
* @private
40+
* @returns {void}
41+
*/
42+
function main() {
43+
var flags;
44+
var args;
45+
var cli;
46+
var len;
47+
var str;
48+
49+
// Create a command-line interface:
50+
cli = new CLI({
51+
'pkg': require( './../package.json' ),
52+
'options': require( './../etc/cli_opts.json' ),
53+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
54+
'encoding': 'utf8'
55+
})
56+
});
57+
58+
// Get any provided command-line options:
59+
flags = cli.flags();
60+
if ( flags.help || flags.version ) {
61+
return;
62+
}
63+
64+
// Get any provided command-line arguments:
65+
args = cli.args();
66+
67+
if ( args.length ) {
68+
str = args[ 0 ];
69+
} else {
70+
// Treat an empty value as an empty string:
71+
str = '';
72+
}
73+
// Check if we are receiving data from `stdin`...
74+
if ( !stdinStream.isTTY ) {
75+
return stdin( onRead );
76+
}
77+
console.log( substringBefore( str, flags.search ) ); // eslint-disable-line no-console
78+
79+
/**
80+
* Callback invoked upon reading from `stdin`.
81+
*
82+
* @private
83+
* @param {(Error|null)} error - error object
84+
* @param {Buffer} data - data
85+
* @returns {void}
86+
*/
87+
function onRead( error, data ) {
88+
var lines;
89+
var i;
90+
if ( error ) {
91+
return cli.error( error );
92+
}
93+
lines = data.toString().split( reEOL.REGEXP );
94+
for ( i = 0; i < lines.length; i++ ) {
95+
if ( !len ) {
96+
len = lines[ i ].length;
97+
}
98+
console.log( substringBefore( lines[ i ], flags.search ) ); // eslint-disable-line no-console
99+
}
100+
}
101+
}
102+
103+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{{alias}}( str, search )
2+
Returns the part of a string before a specified substring.
3+
4+
Parameters
5+
----------
6+
str: string
7+
Input string.
8+
9+
search: string
10+
Search string.
11+
12+
Returns
13+
-------
14+
out: string
15+
Substring.
16+
17+
Examples
18+
--------
19+
> var str = 'beep boop';
20+
> var out = {{alias}}( str, ' ' )
21+
'beep'
22+
23+
> out = {{alias}}( str, 'o' )
24+
'beep b'
25+
26+
See Also
27+
--------

0 commit comments

Comments
 (0)