Skip to content

Commit 228978f

Browse files
Planeshifterstdlib-botkgryte
authored
Add support for testing whether a value is a string in Pascal case (stdlib-js#542)
* Add README.md * Scaffold assert/is-pascalcase package * Update tests * Add implementation, fix docs, and update tests * Update description * Update description * Update description * Update descriptions * Update description * Remove test value * Update description * Update description Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Athan <[email protected]>
1 parent 9f8340f commit 228978f

File tree

15 files changed

+1097
-0
lines changed

15 files changed

+1097
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
# isPascalcase
22+
23+
> Test if a value is a string in Pascal case.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isPascalcase = require( '@stdlib/assert/is-pascalcase' );
31+
```
32+
33+
#### isPascalcase( value )
34+
35+
Tests if a `value` is a `string` in Pascal case.
36+
37+
```javascript
38+
var bool = isPascalcase( 'HelloWorld' );
39+
// returns true
40+
41+
bool = isPascalcase( 'Hello World' );
42+
// returns false
43+
```
44+
45+
</section>
46+
47+
<!-- /.usage -->
48+
49+
<section class="notes">
50+
51+
## Notes
52+
53+
- The function validates that a `value` is a `string`. For all other types, the function returns `false`.
54+
55+
</section>
56+
57+
<!-- /.notes -->
58+
59+
<section class="examples">
60+
61+
## Examples
62+
63+
<!-- eslint no-undef: "error" -->
64+
65+
```javascript
66+
var isPascalcase = require( '@stdlib/assert/is-pascalcase' );
67+
68+
var bool = isPascalcase( 'FooBarBaz' );
69+
// returns true
70+
71+
bool = isPascalcase( 'fooBarBaz' );
72+
// returns false
73+
74+
bool = isPascalcase( 'Foo Bar Baz' );
75+
// returns false
76+
77+
bool = isPascalcase( 'Beep-Boop' );
78+
// returns false
79+
80+
bool = isPascalcase( null );
81+
// returns false
82+
```
83+
84+
</section>
85+
86+
<!-- /.examples -->
87+
88+
* * *
89+
90+
<section class="cli">
91+
92+
## CLI
93+
94+
<section class="usage">
95+
96+
### Usage
97+
98+
```text
99+
Usage: is-pascalcase [options] [<string>]
100+
101+
Options:
102+
103+
-h, --help Print this message.
104+
-V, --version Print the package version.
105+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
106+
```
107+
108+
</section>
109+
110+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
111+
112+
<section class="notes">
113+
114+
### Notes
115+
116+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
117+
118+
```bash
119+
# Not escaped...
120+
$ echo -n $'beEp booP\nFooBar' | is-pascalcase --split /\r?\n/
121+
# Escaped...
122+
$ echo -n $'beEp booP\nFooBar' | is-pascalcase --split /\\r?\\n/
123+
```
124+
125+
- The implementation ignores trailing delimiters.
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<!-- /.usage -->
132+
133+
<section class="examples">
134+
135+
### Examples
136+
137+
```bash
138+
$ is-pascalcase Beep
139+
true
140+
```
141+
142+
</section>
143+
144+
To use as a [standard stream][standard-streams],
145+
146+
```bash
147+
$ echo -n 'boop' | is-pascalcase
148+
false
149+
```
150+
151+
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.
152+
153+
```bash
154+
$ echo -n 'beep\tFooBar' | is-pascalcase --split '\t'
155+
false
156+
true
157+
```
158+
159+
<!-- /.examples -->
160+
161+
</section>
162+
163+
<!-- /.cli -->
164+
165+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
166+
167+
<section class="related">
168+
169+
</section>
170+
171+
<!-- /.related -->
172+
173+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="links">
176+
177+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
178+
179+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
180+
181+
<!-- <related-links> -->
182+
183+
<!-- </related-links> -->
184+
185+
</section>
186+
187+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var isPascalcase = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var bool;
34+
var i;
35+
36+
values = [
37+
'',
38+
'HelloWorld',
39+
'helloWorld',
40+
'HELLO_WORLD',
41+
'123',
42+
'beepBoop123',
43+
'beep',
44+
'foo-bar',
45+
void 0,
46+
null,
47+
true,
48+
NaN,
49+
[],
50+
{},
51+
123
52+
];
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
bool = isPascalcase( values[ i % values.length ] );
57+
if ( typeof bool !== 'boolean' ) {
58+
b.fail( 'should return a boolean' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isBoolean( bool ) ) {
63+
b.fail( 'should return a boolean' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
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) 2022 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 isPascalcase = 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( isPascalcase( 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 {(string|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( isPascalcase( lines[ i ] ) ); // eslint-disable-line no-console
104+
}
105+
}
106+
}
107+
108+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a string in Pascal case.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether a value is a string in Pascal case.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( 'HelloWorld' )
18+
true
19+
> bool = {{alias}}( 'hello-world' )
20+
false
21+
22+
See Also
23+
--------

0 commit comments

Comments
 (0)