Skip to content

feat: add string/base/stickycase #1447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4cab156
feat(string): implement stickycase
anudeeps0306 Mar 2, 2024
37e746e
fix: correct JavaScript example in README.md
anudeeps0306 Mar 2, 2024
b8e84a7
chore: update copyright years
stdlib-bot Mar 2, 2024
c2ed871
Update lib/node_modules/@stdlib/string/base/stickycase/README.md
anudeeps0306 Mar 4, 2024
a1c0dfb
Update lib/node_modules/@stdlib/string/base/stickycase/README.md
anudeeps0306 Mar 4, 2024
d9aad63
Update lib/node_modules/@stdlib/string/base/stickycase/docs/repl.txt
anudeeps0306 Mar 4, 2024
74de902
Update lib/node_modules/@stdlib/string/base/stickycase/docs/repl.txt
anudeeps0306 Mar 4, 2024
8b40e50
Update lib/node_modules/@stdlib/string/base/stickycase/docs/repl.txt
anudeeps0306 Mar 4, 2024
8b21dc2
Update lib/node_modules/@stdlib/string/base/stickycase/docs/repl.txt
anudeeps0306 Mar 4, 2024
ae888a0
Update lib/node_modules/@stdlib/string/base/stickycase/docs/types/ind…
anudeeps0306 Mar 4, 2024
379a186
Update lib/node_modules/@stdlib/string/base/stickycase/docs/types/tes…
anudeeps0306 Mar 4, 2024
0583e00
Update lib/node_modules/@stdlib/string/base/stickycase/test/test.js
anudeeps0306 Mar 4, 2024
d92a91e
Update lib/node_modules/@stdlib/string/base/stickycase/lib/main.js
anudeeps0306 Mar 4, 2024
842f85c
Update lib/node_modules/@stdlib/string/base/stickycase/lib/main.js
anudeeps0306 Mar 4, 2024
98ffb43
Update lib/node_modules/@stdlib/string/base/stickycase/docs/types/ind…
anudeeps0306 Mar 4, 2024
7d61c0d
Update lib/node_modules/@stdlib/string/base/stickycase/docs/types/ind…
anudeeps0306 Mar 4, 2024
5410b18
Update lib/node_modules/@stdlib/string/base/stickycase/docs/types/tes…
anudeeps0306 Mar 4, 2024
0344954
Update lib/node_modules/@stdlib/string/base/stickycase/examples/index.js
anudeeps0306 Mar 4, 2024
d2f97c5
Update lib/node_modules/@stdlib/string/base/stickycase/README.md
anudeeps0306 Mar 4, 2024
d820594
Update lib/node_modules/@stdlib/string/base/stickycase/lib/index.js
anudeeps0306 Mar 4, 2024
24bcb87
Update lib/node_modules/@stdlib/string/base/stickycase/package.json
anudeeps0306 Mar 4, 2024
0437efb
Update lib/node_modules/@stdlib/string/base/stickycase/package.json
anudeeps0306 Mar 4, 2024
03b65c4
Update test.ts
anudeeps0306 Mar 4, 2024
22d647e
Update lib/node_modules/@stdlib/string/base/stickycase/test/test.js
Planeshifter Mar 5, 2024
70674ae
Update lib/node_modules/@stdlib/string/base/stickycase/README.md
Planeshifter Mar 5, 2024
c6fab68
Update lib/node_modules/@stdlib/string/base/stickycase/README.md
Planeshifter Mar 5, 2024
56006e2
Update lib/node_modules/@stdlib/string/base/stickycase/README.md
Planeshifter Mar 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions lib/node_modules/@stdlib/string/base/stickycase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# stickycase

> Convert a string to sticky case.

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var stickycase = require( '@stdlib/string/base/stickycase' );
```

#### stickycase( str\[, p] )

Converts a string to sticky case, where each character in the input string is randomly converted to either uppercase or lowercase.

```javascript
var str = 'hello world';
var out = stickycase( 'hello world' );
// returns <string>
```

By default, the probability for any character to be capitalized is `0.5`. To set a different probability, provide a `p` argument.

```javascript
var str = 'welcome!';
var out = stickycase( 'welcome!', 0.2 );
// returns <string>

str = 'good morning';
out = stickycase( 'good morning', 0.8 );
// returns <string>
```

</section>

<!-- /.usage -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

```javascript
var stickycase = require( '@stdlib/string/base/stickycase' );

var str = 'Hello World!';
var out = stickycase( str );
// returns <string>
// returns <string>

str = 'I am a tiny little teapot';
out = stickycase( str );
// returns <string>

str = 'with big problems';
out = stickycase( str, 0.1 );
// returns <string>

str = 'To be, or not to be: that is the question.';
out = stickycase( str, 0.9 );
// returns <string>

str = 'isMobile';
out = stickycase( str );
// returns <string>
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var pkg = require( '@stdlib/string/base/stickycase/package.json' ).name;
var stickycase = require( '@stdlib/string/base/stickycase/lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var out;
var i;

values = [
'hello world',
'lorem ipsum dolor sit amet',
'quick brown fox jumps over the lazy dog'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = stickycase( values[ i%values.length ] );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( out ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
27 changes: 27 additions & 0 deletions lib/node_modules/@stdlib/string/base/stickycase/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

{{alias}}( str[, p] )
Converts a string to sticky case.

Parameters
----------
str: string
Input string.

p: number
Probability of capitalization.

Returns
-------
out: string
Sticky-cased string.

Examples
--------
> var out = {{alias}}( 'Hello World!' )
<string>

> out = {{alias}}( 'I am a tiny little teapot' )
<string>
See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Converts a string to "sticky caps" case.
*
* @param str - input string
* @param p - probability of capitalization (default: 0.5)
* @returns sticky case string
*
* @example
* var str = stickycase( 'hello world' );
* // returns <string>
*
* @example
* var str = stickycase( 'hello world', 0.2 );
* // returns <string>
*
* @example
* var str = stickycase( 'hello world', 0.8 );
* // returns <string>
*/
declare function stickycase( str: string, p?: number ): string;


// EXPORTS //

export = stickycase;
54 changes: 54 additions & 0 deletions lib/node_modules/@stdlib/string/base/stickycase/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import stickycase from './index';

// TESTS //

// The function returns a string...
{
stickycase( 'hello world' ); // $ExpectType string
stickycase( 'hello world', 0.3 ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
{
stickycase( true ); // $ExpectError
stickycase( false ); // $ExpectError
stickycase( undefined ); // $ExpectError
stickycase( 5 ); // $ExpectError
stickycase( [] ); // $ExpectError
stickycase( {} ); // $ExpectError
stickycase( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the second parameter is not a number...
{
stickycase('hello world', true); // $ExpectError
stickycase('hello world', false); // $ExpectError
stickycase('hello world', undefined); // $ExpectError
stickycase('hello world', 'test'); // $ExpectError
stickycase('hello world', []); // $ExpectError
stickycase('hello world', {}); // $ExpectError
stickycase('hello world', (x: number): number => x); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
stickycase(); // $ExpectError
}
46 changes: 46 additions & 0 deletions lib/node_modules/@stdlib/string/base/stickycase/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var stickycase = require( './../lib' );

var str = 'Hello World!';
var out = stickycase( str );
console.log( out );
// => <string>

str = 'I am a tiny little teapot';
out = stickycase( str );
console.log( out );
// => <string>

str = 'with big problems';
out = stickycase( str, 0.1 );
console.log( out );
// => <string>

str = 'To be, or not to be: that is the question.';
out = stickycase( str, 0.9 );
console.log( out );
// => <string>

str = 'isMobile';
out = stickycase( str, 0.5 );
console.log( out );
// => <string>
49 changes: 49 additions & 0 deletions lib/node_modules/@stdlib/string/base/stickycase/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Convert a string to "sticky caps" case.
*
* @module @stdlib/string/base/stickycase
*
* @example
* var stickycase = require( '@stdlib/string/base/stickycase' );
* var str = stickycase( 'hello world' );
* // returns <string>
*
* @example
* var stickycase = require( '@stdlib/string/base/stickycase' );
* var str = stickycase( 'hello world', 0.2 );
* // returns <string>
*
* @example
* var stickycase = require( '@stdlib/string/base/stickycase' );
* var str = stickycase( 'hello world', 0.8 );
* // returns <string>
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading