Skip to content

Commit 31e7253

Browse files
committed
Add base utility to convert a string to kebab-case
1 parent 38d3bdf commit 31e7253

File tree

10 files changed

+702
-0
lines changed

10 files changed

+702
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# kebabcase
22+
23+
> Convert a string to kebab case.
24+
25+
<!-- Package usage documentation. -->
26+
27+
<section class="usage">
28+
29+
## Usage
30+
31+
```javascript
32+
var kebabcase = require( '@stdlib/string/base/kebabcase' );
33+
```
34+
35+
#### kebabcase( str )
36+
37+
Converts a string to kebab case.
38+
39+
```javascript
40+
var str = kebabcase( 'Foo Bar' );
41+
// returns 'foo-bar'
42+
43+
str = kebabcase( 'I am a tiny little house' );
44+
// returns 'i-am-a-tiny-little-house'
45+
46+
str = kebabcase( 'Hello World!' );
47+
// returns 'hello-world'
48+
```
49+
50+
</section>
51+
52+
<!-- /.usage -->
53+
54+
<!-- Package usage examples. -->
55+
56+
<section class="examples">
57+
58+
## Examples
59+
60+
```javascript
61+
var kebabcase = require( '@stdlib/string/base/kebabcase' );
62+
63+
var str = 'foo bar baz';
64+
var out = kebabcase( str );
65+
// returns 'foo-bar-baz'
66+
67+
str = 'foo_baz';
68+
out = kebabcase( str );
69+
// returns 'foo-baz'
70+
71+
str = 'foo-bar-baz!';
72+
out = kebabcase( str );
73+
// returns 'foo-bar-baz'
74+
75+
str = 'beep boop!';
76+
out = kebabcase( str );
77+
// returns 'beep-boop'
78+
79+
str = 'foo-baz';
80+
out = kebabcase( str );
81+
// returns 'foo-baz'
82+
83+
str = 'Welcome! 😀';
84+
out = kebabcase( str );
85+
// returns 'welcome-😀'
86+
```
87+
88+
</section>
89+
90+
<!-- /.examples -->
91+
92+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
93+
94+
<section class="related">
95+
96+
</section>
97+
98+
<!-- /.related -->
99+
100+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
101+
102+
<section class="links">
103+
104+
</section>
105+
106+
<!-- /.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) 2022 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 kebabcase = 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 = kebabcase( 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: 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 kebab case.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
Returns
11+
-------
12+
out: string
13+
Kebab-cased string.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( 'Hello World!' )
18+
'hello-world'
19+
> out = {{alias}}( 'I am a tiny little teapot' )
20+
'i-am-a-tiny-little-teapot'
21+
22+
See Also
23+
--------
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
* Converts a string to kebab case.
23+
*
24+
* @param str - string to convert
25+
* @returns kebab-cased string
26+
*
27+
* @example
28+
* var str = kebabcase( 'fooBar' );
29+
* // returns 'foo-bar'
30+
*
31+
* @example
32+
* var str = kebabcase( 'foo_bar' );
33+
* // returns 'foo-bar'
34+
*
35+
* @example
36+
* var str = kebabcase( 'foo-bar' );
37+
* // returns 'foo-bar'
38+
*/
39+
declare function kebabcase( str: string ): string;
40+
41+
42+
// EXPORTS //
43+
44+
export = kebabcase;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 kebabcase = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
kebabcase( 'Hello World!' ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
kebabcase( true ); // $ExpectError
32+
kebabcase( false ); // $ExpectError
33+
kebabcase( null ); // $ExpectError
34+
kebabcase( undefined ); // $ExpectError
35+
kebabcase( 5 ); // $ExpectError
36+
kebabcase( [] ); // $ExpectError
37+
kebabcase( {} ); // $ExpectError
38+
kebabcase( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
kebabcase(); // $ExpectError
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 kebabcase = require( './../lib' );
22+
23+
var str = 'foo bar baz';
24+
var out = kebabcase( str );
25+
console.log( out );
26+
// => 'foo-bar-baz'
27+
28+
str = 'foo_baz';
29+
out = kebabcase( str );
30+
console.log( out );
31+
// => 'foo-baz'
32+
33+
str = 'foo-bar-baz!';
34+
out = kebabcase( str );
35+
console.log( out );
36+
// => 'foo-bar-baz'
37+
38+
str = 'beep boop!';
39+
out = kebabcase( str );
40+
console.log( out );
41+
// => 'beep-boop'
42+
43+
str = 'foo-baz';
44+
out = kebabcase( str );
45+
console.log( out );
46+
// => 'foo-baz'
47+
48+
str = 'Welcome! 😀';
49+
out = kebabcase( str );
50+
console.log( out );
51+
// => 'welcome-😀'
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) 2022 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+
* Convert a string to kebab case.
23+
*
24+
* @module @stdlib/string/base/kebabcase
25+
*
26+
* @example
27+
* var kebabcase = require( '@stdlib/string/base/kebabcase' );
28+
*
29+
* var str = kebabcase( 'Foo Bar' );
30+
* // returns 'foo-bar'
31+
*
32+
* str = kebabcase( 'I am a tiny little house' );
33+
* // returns 'i-am-a-tiny-little-house'
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)