Skip to content

Commit 681c185

Browse files
committed
Add package to return ordinal string
1 parent e317ff3 commit 681c185

File tree

12 files changed

+1418
-0
lines changed

12 files changed

+1418
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
# ordinalize
22+
23+
> Turn an integer into an ordinal string (e.g., `1st`, `2nd`, etc.).
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var ordinalize = require( '@stdlib/nlp/ordinalize' );
37+
```
38+
39+
#### ordinalize( value )
40+
41+
Turns an integer into an ordinal string (e.g., `1st`, `2nd`, etc.).
42+
43+
```javascript
44+
var out = ordinalize( '22' );
45+
// returns '22nd'
46+
47+
out = ordinalize( 13 );
48+
// returns '13th'
49+
```
50+
51+
The function accepts the following `options`:
52+
53+
- **lang**: `string` indicating the language. Default: `'en'`.
54+
- **suffixOnly**: `boolean` indicating whether to return only the ordinal suffix. Default: `false`.
55+
- **gender**: `string` indicating whether to use the `masculine` or `feminine` grammatical form. Default: `'masculine'`.
56+
57+
By default, the function returns the ordinal string. To return only the ordinal suffix, set the `suffixOnly` option.
58+
59+
```javascript
60+
var out = ordinalize( '22', {
61+
'suffixOnly': true
62+
});
63+
// returns 'nd'
64+
```
65+
66+
By default, the function returns the ordinal string in English. To return the ordinal string in a different language, set the `lang` option.
67+
68+
```javascript
69+
var out = ordinalize( '22', {
70+
'lang': 'fr'
71+
});
72+
// returns '22e'
73+
```
74+
75+
If the respective language differentiates between the `masculine` and `feminine` grammatical forms, the functions returns by default the ordinal string in the masculine form. To return the ordinal string in the feminine form, set the `gender` option.
76+
77+
```javascript
78+
var out = ordinalize( 7, {
79+
'lang': 'es'
80+
};
81+
// returns '7ª'
82+
83+
out = ordinalize( 7, {
84+
'lang': 'es',
85+
'gender': 'feminine'
86+
});
87+
// returns '7º'
88+
```
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
* * *
95+
96+
<section class="references">
97+
98+
</section>
99+
100+
<!-- /.references -->
101+
102+
<section class="examples">
103+
104+
## Examples
105+
106+
<!-- eslint no-undef: "error" -->
107+
108+
```javascript
109+
var ordinalize = require( '@stdlib/nlp/ordinalize' );
110+
111+
var out = ordinalize( '1' );
112+
// returns '1st'
113+
114+
out = ordinalize( 2 );
115+
// returns '2nd'
116+
117+
out = ordinalize( '3', {
118+
'suffixOnly': true
119+
});
120+
// returns 'rd'
121+
122+
out = ordinalize( '3', {
123+
'lang': 'de'
124+
});
125+
// returns '3.'
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
</section>
145+
146+
<!-- /.links -->
Lines changed: 67 additions & 0 deletions
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var ordinalize = 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+
'5',
38+
5,
39+
4,
40+
'7',
41+
11,
42+
12,
43+
13,
44+
14,
45+
'9',
46+
111,
47+
112,
48+
113,
49+
118,
50+
119,
51+
120
52+
];
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
bool = ordinalize( values[ i % values.length ] );
57+
if ( !isString( bool ) ) {
58+
b.fail( 'should return a string' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isString( bool ) ) {
63+
b.fail( 'should return a string' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( value[, options] )
3+
Turn an integer into an ordinal string (e.g., `1st`, `2nd`, etc.).
4+
5+
Parameters
6+
----------
7+
value: string
8+
Input word.
9+
10+
options: Object (optional)
11+
Options.
12+
13+
options.lang: string (optional)
14+
Language code. Default: `'en'`.
15+
16+
options.suffixOnly: boolean (optional)
17+
Boolean indicating whether to return only the suffix. Default: `false`.
18+
19+
options.gender: string (optional)
20+
Grammatical gender (used if applicable); must be either 'masculine' or
21+
'feminine'. Default: `'masculine'`.
22+
23+
Returns
24+
-------
25+
out: string
26+
Ordinal string or suffix
27+
28+
Examples
29+
--------
30+
> var out = {{alias}}( '1' )
31+
'1st'
32+
> out = {{alias}}( 2, { 'suffixOnly': true } )
33+
'nd'
34+
> out = {{alias}}( '3', { 'lang': 'es' } )
35+
'3ª'
36+
37+
See Also
38+
--------
39+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
* Interface defining function options.
23+
*/
24+
interface Options {
25+
/**
26+
* Language code (default: 'en').
27+
*/
28+
lang?: 'en' | 'es' | 'fin' |'fr' | 'de' | 'it' | 'pt' | 'swe';
29+
30+
/**
31+
* Boolean indicating whether to return only the suffix (default: false).
32+
*/
33+
suffixOnly?: boolean;
34+
35+
/**
36+
* Grammatical gender (default: 'masculine').
37+
*/
38+
gender?: 'masculine' | 'feminine';
39+
}
40+
41+
/**
42+
* Turns an integer into an ordinal string (e.g., `1st`, `2nd`, etc.).
43+
*
44+
* @param value - string or number to convert
45+
* @param options - options
46+
* @param options.suffixOnly - boolean indicating whether to return only the suffix (default: false)
47+
* @param options.lang - language code (default: 'en')
48+
* @param options.gender - grammatical gender (used if applicable; either 'masculine' or 'feminine'; default: 'masculine')
49+
* @returns ordinal string or suffix
50+
*
51+
* @example
52+
* var out = ordinalize( '1' );
53+
* // returns '1st'
54+
*
55+
* @example
56+
* var out = ordinalize( '2' );
57+
* // returns '2nd'
58+
*
59+
* @example
60+
* var out = ordinalize( '21' );
61+
* // returns '21st'
62+
*
63+
* @example
64+
* var out = ordinalize( '1', { 'lang': 'de' } );
65+
* // returns '1.'
66+
*
67+
* @example
68+
* var out = ordinalize( '7', { 'lang': 'es' } );
69+
* // returns '7ª'
70+
*/
71+
declare function ordinalize( value: string | number, options?: Options ): string;
72+
73+
74+
// EXPORTS //
75+
76+
export = ordinalize;

0 commit comments

Comments
 (0)