Skip to content

Commit e9b8854

Browse files
committed
feat: add stats/base/ztest/alternative-str2enum
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: skipped - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 227bb1e commit e9b8854

File tree

10 files changed

+535
-0
lines changed

10 files changed

+535
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# str2enum
22+
23+
> Return the enumeration constant associated with a Z-test alternative hypothesis string.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var str2enum = require( '@stdlib/stats/base/ztest/alternative-str2enum' );
41+
```
42+
43+
#### str2enum( diagonal )
44+
45+
Returns the enumeration constant associated with a Z-test alternative hypothesis string.
46+
47+
```javascript
48+
var v = str2enum( 'greater' );
49+
// returns <number>
50+
```
51+
52+
If unable to resolve an enumeration constant, the function returns `null`.
53+
54+
```javascript
55+
var v = str2enum( 'beep' );
56+
// returns null
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
## Notes
68+
69+
- Downstream consumers of this function should **not** rely on specific integer values (e.g., `TWO_SIDED == 0`). Instead, the function should be used in an opaque manner.
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<!-- Package usage examples. -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var str2enum = require( '@stdlib/stats/base/ztest/alternative-str2enum' );
85+
86+
var v = str2enum( 'greater' );
87+
// returns <number>
88+
89+
v = str2enum( 'two-sided' );
90+
// returns <number>
91+
```
92+
93+
</section>
94+
95+
<!-- /.examples -->
96+
97+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
98+
99+
<section class="references">
100+
101+
</section>
102+
103+
<!-- /.references -->
104+
105+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
106+
107+
<section class="related">
108+
109+
</section>
110+
111+
<!-- /.related -->
112+
113+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="links">
116+
117+
</section>
118+
119+
<!-- /.links -->
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) 2025 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var str2enum = 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+
'greater',
38+
'two-sided'
39+
];
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
out = str2enum( values[ i%values.length ] );
44+
if ( typeof out !== 'number' ) {
45+
b.fail( 'should return a number' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isInteger( out ) ) {
50+
b.fail( 'should return an integer' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( alternative )
3+
Returns the enumeration constant associated with a Z-test alternative
4+
hypothesis string.
5+
6+
Downstream consumers of this function should *not* rely on specific integer
7+
values (e.g., `TWO_SIDED == 0`). Instead, the function should be used in an
8+
opaque manner.
9+
10+
Parameters
11+
----------
12+
alternative: string
13+
Alternative hypothesis.
14+
15+
Returns
16+
-------
17+
out: integer|null
18+
Enumeration constant.
19+
20+
Examples
21+
--------
22+
> var out = {{alias}}( 'greater' )
23+
<number>
24+
25+
See Also
26+
--------
27+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
* Returns the enumeration constant associated with a Z-test alternative hypothesis string.
23+
*
24+
* ## Notes
25+
*
26+
* - Downstream consumers of this function should **not** rely on specific integer values (e.g., `TWO_SIDED == 0`). Instead, the function should be used in an opaque manner.
27+
*
28+
* @param alternative - alternative hypothesis string
29+
* @returns enumeration constant
30+
*
31+
* @example
32+
* var v = str2enum( 'greater' );
33+
* // returns <number>
34+
*/
35+
declare function str2enum( alternative: string ): number | null;
36+
37+
38+
// EXPORTS //
39+
40+
export = str2enum;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 str2enum = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number or null...
25+
{
26+
str2enum( 'greater' ); // $ExpectType number | null
27+
}
28+
29+
// The compiler throws an error if not provided a string...
30+
{
31+
str2enum( 10 ); // $ExpectError
32+
str2enum( true ); // $ExpectError
33+
str2enum( false ); // $ExpectError
34+
str2enum( null ); // $ExpectError
35+
str2enum( undefined ); // $ExpectError
36+
str2enum( [] ); // $ExpectError
37+
str2enum( {} ); // $ExpectError
38+
str2enum( ( x: number ): number => x ); // $ExpectError
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 str2enum = require( './../lib' );
22+
23+
var v = str2enum( 'greater' );
24+
console.log( v );
25+
// => <number>
26+
27+
v = str2enum( 'two-sided' );
28+
console.log( v );
29+
// => <number>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
* Return the enumeration constant associated with a Z-test alternative hypothesis string.
23+
*
24+
* @module @stdlib/stats/base/ztest/alternative-str2enum
25+
*
26+
* @example
27+
* var str2enum = require( '@stdlib/stats/base/ztest/alternative-str2enum' );
28+
*
29+
* var v = str2enum( 'greater' );
30+
* // returns <number>
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;

0 commit comments

Comments
 (0)