Skip to content

Commit 58601b7

Browse files
committed
Add package to convert number to word representation
1 parent 1c9c075 commit 58601b7

File tree

15 files changed

+1548
-0
lines changed

15 files changed

+1548
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# num2words
22+
23+
> Convert a number to a word representation.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var num2words = require( '@stdlib/string/number-to-words' );
37+
```
38+
39+
#### num2words( value\[, options] )
40+
41+
Converts a number to a word representation.
42+
43+
```javascript
44+
var out = num2words( 87 );
45+
// returns 'eighty-seven'
46+
47+
out = num2words( 23101 );
48+
// returns 'twenty-three thousand one hundred one'
49+
50+
out = num2words( 0.53 );
51+
// returns 'zero point five three'
52+
```
53+
54+
The function accepts the following `options`:
55+
56+
- **lang**: `string` indicating the language. Default: `'en'`.
57+
58+
By default, the function returns a word representation of a number in English. To return a word representation of a number in a different language, set the `lang` option.
59+
60+
```javascript
61+
var out = num2words( 22, {
62+
'lang': 'de'
63+
});
64+
// returns 'zweiundzwanzig'
65+
66+
out = num2words( 0.53, {
67+
'lang': 'de'
68+
});
69+
// returns 'null Komma fünf drei'
70+
```
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
77+
78+
<section class="notes">
79+
80+
## Notes
81+
82+
- The following languages are supported:
83+
84+
- **en**: English.
85+
- **de**: German.
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var num2words = require( '@stdlib/string/number-to-words' );
99+
100+
var out = num2words( 29 );
101+
// returns 'twenty-nine'
102+
103+
out = num2words( 113 );
104+
// returns 'one hundred thirteen'
105+
106+
out = num2words( 13.52 );
107+
// returns 'thirteen point five two'
108+
109+
out = num2words( 47, {
110+
'lang': 'de'
111+
});
112+
// returns 'siebenundvierzig'
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- 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. -->
120+
121+
<section class="references">
122+
123+
</section>
124+
125+
<!-- /.references -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
</section>
132+
133+
<!-- /.related -->
134+
135+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="links">
138+
139+
</section>
140+
141+
<!-- /.links -->
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 num2words = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var str;
34+
var i;
35+
36+
values = [
37+
0,
38+
1,
39+
21,
40+
100,
41+
1000,
42+
10319,
43+
1000000,
44+
13029111,
45+
1000000000000,
46+
1000000000000000,
47+
1000000000000000000
48+
];
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
str = num2words( values[ i % values.length ] );
53+
if ( typeof str !== 'string' ) {
54+
b.fail( 'should return a string' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isString( str ) ) {
59+
b.fail( 'should return a string' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( value[, options] )
3+
Converts a number to a word representation.
4+
5+
Parameters
6+
----------
7+
value: integer
8+
Input value.
9+
10+
options: Object (optional)
11+
Options.
12+
13+
options.lang: string (optional)
14+
Language code. Supported languages:
15+
16+
- en: English.
17+
- de: German.
18+
19+
Default: `'en'`.
20+
21+
Returns
22+
-------
23+
out: string
24+
Word representation of number.
25+
26+
Examples
27+
--------
28+
> var out = {{alias}}( 123 )
29+
'one hundred twenty-three'
30+
31+
> out = {{alias}}( 16.31 )
32+
'sixteen point three one'
33+
34+
> out = {{alias}}( 123, { 'lang': 'de' })
35+
'einhundertdreiundzwanzig'
36+
37+
See Also
38+
--------
39+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
* ## Notes
29+
*
30+
* Supported languages:
31+
*
32+
* - **en**: English.
33+
* - **de**: German.
34+
*/
35+
lang?: 'en' | 'de';
36+
}
37+
38+
39+
/**
40+
* Converts a number to a word representation.
41+
*
42+
* @param num - number to convert
43+
* @param options - options
44+
* @param options.lang - language code (default: 'en')
45+
* @throws must provide an integer
46+
* @throws must provide a safe integer
47+
* @returns string representation of a number
48+
*
49+
* @example
50+
* var out = num2words( 12 );
51+
* // returns 'twelve'
52+
*
53+
* @example
54+
* var out = num2words( 21.8 );
55+
* // returns 'twenty-one point eight'
56+
*
57+
* @example
58+
* var out = num2words( 1234 );
59+
* // returns 'one thousand two hundred thirty-four'
60+
*
61+
* @example
62+
* var out = num2words( 100381 );
63+
* // returns 'one hundred thousand three hundred eighty-one'
64+
*/
65+
declare function num2words( num: number, options?: Options ): string;
66+
67+
68+
// EXPORTS //
69+
70+
export = num2words;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 num2words = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
num2words( 3 ); // $ExpectType string
27+
num2words( 5, { 'lang': 'en' } ); // $ExpectType string
28+
}
29+
30+
// The compiler throws an error if the function is not provided a number as its first argument...
31+
{
32+
num2words( 'abc' ); // $ExpectError
33+
num2words( true ); // $ExpectError
34+
num2words( false ); // $ExpectError
35+
num2words( [] ); // $ExpectError
36+
num2words( {} ); // $ExpectError
37+
num2words( ( x: number ): number => x ); // $ExpectError
38+
39+
num2words( 'abc', {} ); // $ExpectError
40+
num2words( true, {} ); // $ExpectError
41+
num2words( false, {} ); // $ExpectError
42+
num2words( [], {} ); // $ExpectError
43+
num2words( {}, {} ); // $ExpectError
44+
num2words( ( x: number ): number => x, {} ); // $ExpectError
45+
}
46+
47+
// The compiler throws an error if the function is provided an options argument which is not an object...
48+
{
49+
num2words( 2, null ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a `lang` option which is not a recognized language code...
53+
{
54+
num2words( 3, { 'lang': 'hello' } ); // $ExpectError
55+
num2words( 3, { 'lang': 123 } ); // $ExpectError
56+
num2words( 3, { 'lang': true } ); // $ExpectError
57+
num2words( 3, { 'lang': false } ); // $ExpectError
58+
num2words( 3, { 'lang': {} } ); // $ExpectError
59+
num2words( 3, { 'lang': [] } ); // $ExpectError
60+
num2words( 3, { 'lang': ( x: number ): number => x } ); // $ExpectError
61+
}
62+
63+
// The compiler throws an error if the function is provided insufficient arguments...
64+
{
65+
num2words(); // $ExpectError
66+
}

0 commit comments

Comments
 (0)