Skip to content

Commit 84a6f4b

Browse files
anudeeps0306stdlib-botPlaneshifter
authored
feat: add string/base/stickycase
PR-URL: #1447 Closes: #852 --------- Signed-off-by: Anudeep Sanapala <71971574+anudeeps0306@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 1aaab57 commit 84a6f4b

File tree

10 files changed

+575
-0
lines changed

10 files changed

+575
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# stickycase
22+
23+
> Convert a string to sticky case.
24+
25+
<!-- Package usage documentation. -->
26+
27+
<section class="usage">
28+
29+
## Usage
30+
31+
```javascript
32+
var stickycase = require( '@stdlib/string/base/stickycase' );
33+
```
34+
35+
#### stickycase( str\[, p] )
36+
37+
Converts a string to sticky case, where each character in the input string is randomly converted to either uppercase or lowercase.
38+
39+
```javascript
40+
var str = 'hello world';
41+
var out = stickycase( 'hello world' );
42+
// returns <string>
43+
```
44+
45+
By default, the probability for any character to be capitalized is `0.5`. To set a different probability, provide a `p` argument.
46+
47+
```javascript
48+
var str = 'welcome!';
49+
var out = stickycase( 'welcome!', 0.2 );
50+
// returns <string>
51+
52+
str = 'good morning';
53+
out = stickycase( 'good morning', 0.8 );
54+
// returns <string>
55+
```
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage examples. -->
62+
63+
<section class="examples">
64+
65+
## Examples
66+
67+
```javascript
68+
var stickycase = require( '@stdlib/string/base/stickycase' );
69+
70+
var str = 'Hello World!';
71+
var out = stickycase( str );
72+
// returns <string>
73+
// returns <string>
74+
75+
str = 'I am a tiny little teapot';
76+
out = stickycase( str );
77+
// returns <string>
78+
79+
str = 'with big problems';
80+
out = stickycase( str, 0.1 );
81+
// returns <string>
82+
83+
str = 'To be, or not to be: that is the question.';
84+
out = stickycase( str, 0.9 );
85+
// returns <string>
86+
87+
str = 'isMobile';
88+
out = stickycase( str );
89+
// returns <string>
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
97+
98+
<section class="related">
99+
100+
</section>
101+
102+
<!-- /.related -->
103+
104+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
105+
106+
<section class="links">
107+
108+
</section>
109+
110+
<!-- /.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) 2024 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( '@stdlib/string/base/stickycase/package.json' ).name;
26+
var stickycase = require( '@stdlib/string/base/stickycase/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+
'hello world',
38+
'lorem ipsum dolor sit amet',
39+
'quick brown fox jumps over the lazy dog'
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = stickycase( 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( str[, p] )
3+
Converts a string to sticky case.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
p: number
11+
Probability of capitalization.
12+
13+
Returns
14+
-------
15+
out: string
16+
Sticky-cased string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'Hello World!' )
21+
<string>
22+
23+
> out = {{alias}}( 'I am a tiny little teapot' )
24+
<string>
25+
See Also
26+
--------
27+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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: 4.1
20+
21+
/**
22+
* Converts a string to "sticky caps" case.
23+
*
24+
* @param str - input string
25+
* @param p - probability of capitalization (default: 0.5)
26+
* @returns sticky case string
27+
*
28+
* @example
29+
* var str = stickycase( 'hello world' );
30+
* // returns <string>
31+
*
32+
* @example
33+
* var str = stickycase( 'hello world', 0.2 );
34+
* // returns <string>
35+
*
36+
* @example
37+
* var str = stickycase( 'hello world', 0.8 );
38+
* // returns <string>
39+
*/
40+
declare function stickycase( str: string, p?: number ): string;
41+
42+
43+
// EXPORTS //
44+
45+
export = stickycase;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 stickycase from './index';
20+
21+
// TESTS //
22+
23+
// The function returns a string...
24+
{
25+
stickycase( 'hello world' ); // $ExpectType string
26+
stickycase( 'hello world', 0.3 ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
stickycase( true ); // $ExpectError
32+
stickycase( false ); // $ExpectError
33+
stickycase( undefined ); // $ExpectError
34+
stickycase( 5 ); // $ExpectError
35+
stickycase( [] ); // $ExpectError
36+
stickycase( {} ); // $ExpectError
37+
stickycase( ( x: number ): number => x ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the second parameter is not a number...
41+
{
42+
stickycase('hello world', true); // $ExpectError
43+
stickycase('hello world', false); // $ExpectError
44+
stickycase('hello world', undefined); // $ExpectError
45+
stickycase('hello world', 'test'); // $ExpectError
46+
stickycase('hello world', []); // $ExpectError
47+
stickycase('hello world', {}); // $ExpectError
48+
stickycase('hello world', (x: number): number => x); // $ExpectError
49+
}
50+
51+
// The compiler throws an error if the function is provided insufficient arguments...
52+
{
53+
stickycase(); // $ExpectError
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 stickycase = require( './../lib' );
22+
23+
var str = 'Hello World!';
24+
var out = stickycase( str );
25+
console.log( out );
26+
// => <string>
27+
28+
str = 'I am a tiny little teapot';
29+
out = stickycase( str );
30+
console.log( out );
31+
// => <string>
32+
33+
str = 'with big problems';
34+
out = stickycase( str, 0.1 );
35+
console.log( out );
36+
// => <string>
37+
38+
str = 'To be, or not to be: that is the question.';
39+
out = stickycase( str, 0.9 );
40+
console.log( out );
41+
// => <string>
42+
43+
str = 'isMobile';
44+
out = stickycase( str, 0.5 );
45+
console.log( out );
46+
// => <string>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 "sticky caps" case.
23+
*
24+
* @module @stdlib/string/base/stickycase
25+
*
26+
* @example
27+
* var stickycase = require( '@stdlib/string/base/stickycase' );
28+
* var str = stickycase( 'hello world' );
29+
* // returns <string>
30+
*
31+
* @example
32+
* var stickycase = require( '@stdlib/string/base/stickycase' );
33+
* var str = stickycase( 'hello world', 0.2 );
34+
* // returns <string>
35+
*
36+
* @example
37+
* var stickycase = require( '@stdlib/string/base/stickycase' );
38+
* var str = stickycase( 'hello world', 0.8 );
39+
* // returns <string>
40+
*/
41+
42+
// MODULES //
43+
44+
var main = require( './main.js' );
45+
46+
47+
// EXPORTS //
48+
49+
module.exports = main;

0 commit comments

Comments
 (0)