Skip to content

Commit 0aba813

Browse files
committed
Add package to return part of string before last occurrence of a substring
1 parent a503750 commit 0aba813

File tree

15 files changed

+1100
-0
lines changed

15 files changed

+1100
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
# substringBeforeLast
22+
23+
> Return the part of a string before the last occurrence of 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 substringBeforeLast = require( '@stdlib/string/substring-before-last' );
41+
```
42+
43+
#### substringBeforeLast( str, search )
44+
45+
Returns the part of a string before the last occurrence of a specified substring.
46+
47+
```javascript
48+
var str = 'Beep Boop Beep';
49+
var out = substringBeforeLast( str, 'Beep' );
50+
// returns 'Beep Boop '
51+
52+
out = substringBeforeLast( str, 'Boop' );
53+
// returns 'Beep '
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 substringBeforeLast = require( '@stdlib/string/substring-before-last' );
78+
79+
var str = 'beep boop';
80+
var out = substringBeforeLast( str, ' ' );
81+
// returns 'beep'
82+
83+
out = substringBeforeLast( str, 'e' );
84+
// returns 'be'
85+
86+
out = substringBeforeLast( str, 'x' );
87+
// returns 'beep boop'
88+
89+
out = substringBeforeLast( str, '' );
90+
// returns 'beep boop'
91+
```
92+
93+
</section>
94+
95+
<!-- /.examples -->
96+
97+
98+
<!-- Section for describing a command-line interface. -->
99+
100+
* * *
101+
102+
<section class="cli">
103+
104+
## CLI
105+
106+
<!-- CLI usage documentation. -->
107+
108+
<section class="usage">
109+
110+
### Usage
111+
112+
```text
113+
Usage: substring-before-last [options] --search=<string> [<string>]
114+
115+
Options:
116+
117+
-h, --help Print this message.
118+
-V, --version Print the package version.
119+
--search string Search string.
120+
```
121+
122+
</section>
123+
124+
<!-- /.usage -->
125+
126+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
127+
128+
<section class="notes">
129+
130+
</section>
131+
132+
<!-- /.notes -->
133+
134+
<!-- CLI usage examples. -->
135+
136+
<section class="examples">
137+
138+
### Examples
139+
140+
```bash
141+
$ substring-before-last abcdefg --search d
142+
abc
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
</section>
150+
151+
<!-- /.cli -->
152+
153+
<!-- 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. -->
154+
155+
<section class="references">
156+
157+
</section>
158+
159+
<!-- /.references -->
160+
161+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
162+
163+
<section class="links">
164+
165+
</section>
166+
167+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
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 substringBeforeLast = 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 = substringBeforeLast( 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+
});
Lines changed: 103 additions & 0 deletions
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 substringBeforeLast = 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( substringBeforeLast( 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( substringBeforeLast( lines[ i ], flags.search ) ); // eslint-disable-line no-console
99+
}
100+
}
101+
}
102+
103+
main();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{alias}}( str, search )
2+
Returns the part of a string before the last occurrence of a specified
3+
substring.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
search: string
11+
Search value.
12+
13+
Returns
14+
-------
15+
out: string
16+
Substring.
17+
18+
Examples
19+
--------
20+
> var str = 'Beep Boop Beep';
21+
> var out = {{alias}}( str, 'Beep' )
22+
'Beep Boop '
23+
24+
> out = {{alias}}( str, 'Boop' )
25+
'Beep '
26+
27+
See Also
28+
--------

0 commit comments

Comments
 (0)