Skip to content

Commit c85038d

Browse files
committed
feat: add string/headercase
1 parent 3fbd1f9 commit c85038d

File tree

15 files changed

+1215
-0
lines changed

15 files changed

+1215
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
# headercase
22+
23+
> Convert a string to HTTP header case.
24+
25+
<!-- Package usage documentation. -->
26+
27+
<section class="usage">
28+
29+
## Usage
30+
31+
```javascript
32+
var headercase = require( '@stdlib/string/headercase' );
33+
```
34+
35+
#### headercase( str )
36+
37+
Converts a string to HTTP header case.
38+
39+
```javascript
40+
var out = headercase( 'foo bar' );
41+
// returns 'Foo-Bar'
42+
43+
out = headercase( 'IS_MOBILE' );
44+
// returns 'Is-Mobile'
45+
46+
out = headercase( 'Hello World!' );
47+
// returns 'Hello-World'
48+
49+
out = headercase( '--foo-bar--' );
50+
// returns 'Foo-Bar'
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<!-- Package usage examples. -->
58+
59+
<section class="examples">
60+
61+
## Examples
62+
63+
```javascript
64+
var headercase = require( '@stdlib/string/headercase' );
65+
66+
var str = 'Hello World!';
67+
var out = headercase( str );
68+
// returns 'Hello-World'
69+
70+
str = 'HELLO WORLD!';
71+
out = headercase( str );
72+
// returns 'Hello-World'
73+
74+
str = 'To be, or not to be: that is the question.';
75+
out = headercase( str );
76+
// returns 'To-Be-Or-Not-To-Be-That-Is-The-Question'
77+
```
78+
79+
</section>
80+
81+
<!-- /.examples -->
82+
83+
* * *
84+
85+
<section class="cli">
86+
87+
## CLI
88+
89+
<section class="usage">
90+
91+
### Usage
92+
93+
```text
94+
Usage: headercase [options] [<string>]
95+
96+
Options:
97+
98+
-h, --help Print this message.
99+
-V, --version Print the package version.
100+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="notes">
110+
111+
### Notes
112+
113+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
114+
115+
```bash
116+
# Not escaped...
117+
$ echo -n $'beep\nfoo_bar' | headercase --split /\r?\n/
118+
119+
# Escaped...
120+
$ echo -n $'beep\nfoo_bar' | headercase --split /\\r?\\n/
121+
```
122+
123+
- The implementation ignores trailing delimiters.
124+
125+
</section>
126+
127+
<!-- /.notes -->
128+
129+
<section class="examples">
130+
131+
### Examples
132+
133+
```bash
134+
$ headercase 'hello world!'
135+
Hello-World
136+
```
137+
138+
To use as a [standard stream][standard-streams],
139+
140+
```bash
141+
$ echo -n 'beEp booP' | headercase
142+
Beep-Boop
143+
```
144+
145+
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.
146+
147+
```bash
148+
$ echo -n 'beep\tfoo_bar' | headercase --split '\t'
149+
Beep
150+
Foo-Bar
151+
```
152+
153+
</section>
154+
155+
<!-- /.examples -->
156+
157+
</section>
158+
159+
<!-- /.cli -->
160+
161+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
162+
163+
<section class="related">
164+
165+
</section>
166+
167+
<!-- /.related -->
168+
169+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
170+
171+
<section class="links">
172+
173+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
174+
175+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
176+
177+
</section>
178+
179+
<!-- /.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) 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 headercase = 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 = headercase( 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: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2023 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 headercase = 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+
50+
// Create a command-line interface:
51+
cli = new CLI({
52+
'pkg': require( './../package.json' ),
53+
'options': require( './../etc/cli_opts.json' ),
54+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
55+
'encoding': 'utf8'
56+
})
57+
});
58+
59+
// Get any provided command-line options:
60+
flags = cli.flags();
61+
if ( flags.help || flags.version ) {
62+
return;
63+
}
64+
65+
// Get any provided command-line arguments:
66+
args = cli.args();
67+
68+
// Check if we are receiving data from `stdin`...
69+
if ( !stdinStream.isTTY ) {
70+
if ( flags.split ) {
71+
if ( !isRegExpString( flags.split ) ) {
72+
flags.split = '/'+flags.split+'/';
73+
}
74+
split = reFromString( flags.split );
75+
} else {
76+
split = RE_EOL;
77+
}
78+
return stdin( onRead );
79+
}
80+
console.log( headercase( args[ 0 ] ) ); // eslint-disable-line no-console
81+
82+
/**
83+
* Callback invoked upon reading from `stdin`.
84+
*
85+
* @private
86+
* @param {(Error|null)} error - error object
87+
* @param {Buffer} data - data
88+
* @returns {void}
89+
*/
90+
function onRead( error, data ) {
91+
var lines;
92+
var i;
93+
if ( error ) {
94+
return cli.error( error );
95+
}
96+
lines = data.toString().split( split );
97+
98+
// Remove any trailing separators (e.g., trailing newline)...
99+
if ( lines[ lines.length-1 ] === '' ) {
100+
lines.pop();
101+
}
102+
for ( i = 0; i < lines.length; i++ ) {
103+
console.log( headercase( lines[ i ] ) ); // eslint-disable-line no-console
104+
}
105+
}
106+
}
107+
108+
main();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{{alias}}( str )
3+
Converts a string to HTTP header case.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
Returns
11+
-------
12+
out: string
13+
HTTP header-cased string.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( 'Hello World!' )
18+
'Hello-World'
19+
> out = {{alias}}( 'beep boop' )
20+
'Beep-Boop'
21+
22+
See Also
23+
--------

0 commit comments

Comments
 (0)