Skip to content

Commit a059e60

Browse files
authored
Merge branch 'develop' into master
2 parents 0c9a95a + 55d04e7 commit a059e60

File tree

9 files changed

+1182
-366
lines changed

9 files changed

+1182
-366
lines changed

README.md

Lines changed: 105 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
- - -
1212
Convert numbers to words - their written form.
1313

14-
## Install with npm
15-
14+
## Install
15+
With npm:
1616
```bash
17-
npm i --save written-number
17+
npm install --save written-number
1818
```
19-
20-
## Install with bower
21-
19+
With bower:
2220
```bash
2321
bower install written-number
2422
```
@@ -27,6 +25,18 @@ bower install written-number
2725
```javascript
2826
var writtenNumber = require('written-number');
2927
writtenNumber(1234); // => 'one thousand two hundred and thirty-four'
28+
29+
writtenNumber(1234, {lang: 'fr'}); // => 'mille deux cent trente-quatre'
30+
writtenNumber.defaults.lang = 'es';
31+
writtenNumber(4758); // => 'cuatro mil setecientos cincuenta y ocho'
32+
33+
writtenNumber(1234, {lang: 'es'}); // => 'mil doscientos treinta y cuatro'
34+
writtenNumber(1234, {lang: 'pt'}); // => 'mil duzentos e trinta e quatro'
35+
writtenNumber(1234, {lang: 'ar'}); // => 'ألف ومائتان وأربعة وثلاثون'
36+
writtenNumber(1234, {lang: 'eo'}); // => 'mil ducent tridek kvar'
37+
writtenNumber(1234, {lang: 'vi'}); // => 'một ngàn hai trăm và ba mươi bốn'
38+
writtenNumber(1234, {lang: 'uk'}); // => 'одна тисяча двісті тридцять чотири'
39+
writtenNumber(1234, {lang: 'id'}); // => 'seribu dua ratus tiga puluh empat'
3040
```
3141

3242
## Options
@@ -36,77 +46,32 @@ writtenNumber(1234); // => 'one thousand two hundred and thirty-four'
3646
language to use. An i18n configuration object may be passed to support
3747
external language definitions.
3848

39-
## Internationalization
4049
Currently supported languages are:
41-
- English `lang = "en"`
42-
- Portuguese (Brazil) `lang = "pt"`
43-
- Portuguese (Portugal) `lang = "ptPT"`
44-
- Spanish `lang = "es"`
45-
- French `lang = "fr"`
46-
- Esperanto `lang = "eo"`
47-
- Vietnamese `lang = "vi"`
48-
- Turkish `lang = "tr"`
49-
- Indonesian `lang = "id"`
50-
- English (Indian) `lang = "enIndian"`
51-
52-
### Spanish Example
53-
```javascript
54-
var writtenNumber = require('written-number');
55-
writtenNumber(1234, { lang: 'es' }); // => 'mil doscientos treinta y cuatro'
56-
```
5750

58-
```javascript
59-
var writtenNumber = require('written-number');
60-
writtenNumber.defaults.lang = 'es';
61-
writtenNumber(4758); // => 'cuatro mil setecientos cincuenta y ocho'
62-
```
63-
64-
### Portuguese (Brazil) Example
65-
```javascript
66-
var writtenNumber = require('written-number');
67-
writtenNumber(1234, { lang: 'pt' }); // => 'mil duzentos e trinta e quatro'
68-
```
69-
70-
### French Example
71-
```javascript
72-
var writtenNumber = require('written-number');
73-
writtenNumber(1234, { lang: 'fr' }); // => 'mille deux cent trente-quatre'
74-
```
75-
76-
### Esperanto Example
77-
```javascript
78-
var writtenNumber = require('written-number');
79-
writtenNumber(1234, { lang: 'eo' }); // => 'mil ducent tridek kvar'
80-
```
81-
82-
### Vietnamese Example
83-
```javascript
84-
var writtenNumber = require('written-number');
85-
writtenNumber(1234, { lang: 'vi' }); // => 'một ngàn hai trăm và ba mươi bốn'
86-
```
87-
88-
### Indonesian Example
89-
```javascript
90-
var writtenNumber = require('written-number');
91-
writtenNumber(1234, { lang: 'id' }); // => 'seribu dua ratus tiga puluh empat'
92-
```
93-
94-
## Options
95-
Property | Value
96-
-------------- | -------------
97-
noAnd | false
98-
lang | 'en'
99-
100-
### Configure your own language
101-
Each language has it's own unique grammar exceptions. You can create your own
102-
language.json file in the folder "i18n" and give writtenNumber support for it. I
103-
don't think the current scheme and logic cover all the cases, but may be cover
51+
| Language | `lang` |
52+
|---------|--------|
53+
| English | `en` |
54+
| Portuguese (Brazil) | `pt` |
55+
| Portuguese (Portugal) | `ptPT` |
56+
| Spanish | `es` |
57+
| French | `fr` |
58+
| Esperanto | `eo` |
59+
| Vietnamese | `vi` |
60+
| Arabic | `ar` |
61+
| Turkish | `tr` |
62+
| English (Indian) | `enIndian` |
63+
| Ukrainian | `uk` |
64+
| Indonesian | `id` |
65+
66+
67+
## Configure your own language
68+
Each language has it's own unique grammar exceptions. You can create your own
69+
language.json file in the folder "i18n" and give writtenNumber support for it. I
70+
don't think the current scheme and logic cover all the cases, but may be cover
10471
some.
10572

10673
##### useLongScale:
107-
'Boolean' that indicates if it use [long or short
108-
scale](http://en.wikipedia.org/wiki/Long_and_short_scales). This differs the
109-
meaning of the words `billion`, `trillion` and so on.
74+
'Boolean' that indicates if it use [long or short scale](http://en.wikipedia.org/wiki/Long_and_short_scales). This differs the meaning of the words `billion`, `trillion` and so on.
11075

11176
##### baseSeparator:
11277
'String' that separates the base cardinal numbers.
@@ -116,31 +81,69 @@ Example: 29 -> twenty`-`eight. Spanish uses the conector " y ".
11681
'String' that separates the units from the last base cardinal numbers.
11782
Example: 1234 -> one thousand two hundred **and** thirty-four
11883

84+
##### allSeparator:
85+
'String' that separates all cardinals, not only the last one.
86+
Example: 1125 -> ألف **و**مائة **و**خمسة **و**عشرون
87+
11988
##### base:
120-
Base cardinals numbers. Numbers that have unique names and are used to build
121-
others.
89+
Base cardinals numbers. Numbers that have unique names and are used to build others.
90+
91+
##### alternativeBase:
92+
Alternative versions of base cardinals numbers for usage with specific units (ex. thousands in Ukrainian use feminine form of base cardinal numbers). These bases will be treated as an extension for the default `base`.
93+
94+
```json
95+
"alternativeBase": {
96+
"feminine": {
97+
"1": "одна",
98+
"2": "дві"
99+
}
100+
}
101+
```
122102

123103
##### units:
124104
Number units.
125105
It can be:
126106
- String
127-
128-
- Object normal flow. Give support to singular and plural units. English does
129-
not need this, but spanish does.
130-
107+
- Object normal flow. Give support to singular, dual, and plural units. English does not need this, but spanish does.
131108
```json
132109
{
133110
"singular": "millón",
134111
"plural": "millones"
135112
}
136113
```
137114

115+
- Object with `few` word form.
116+
117+
In some languages like Ukrainian, there are specific unit forms for values from 2 (including) to 4 (including). This forms can be specified with `few`.
118+
119+
```json
120+
{
121+
"singular": "мільйон",
122+
"few": "мільйони",
123+
"plural": "мільйонів",
124+
}
125+
```
126+
127+
- Object with `useAlternativeBase`.
128+
129+
Selects an `alternativeBase` name which this unit should prefer over the default `base` if possible.
130+
131+
```json
132+
{
133+
"singular": "тисяча",
134+
"few": "тисячі",
135+
"plural": "тисяч",
136+
"useAlternativeBase": "feminine"
137+
}
138+
```
139+
140+
138141
- Object with `useBaseInstead` exception.
139-
In some languages like spanish, specific units like "ciento", use the base
140-
cardinal number instead.
141142

142-
With `useBaseException` you can also specify with which unit (1 to 9) you don't
143-
want use the base cardinal instead and use the regular behaviour.
143+
In some languages like spanish and arabic, specific units like "ciento", use the base cardinal number instead.
144+
145+
- Object with `useBaseException`: You can also specify with which unit (1 to 9) you don't
146+
want use the base cardinal instead and use the regular behaviour:
144147

145148
```json
146149
{
@@ -149,115 +152,54 @@ want use the base cardinal instead and use the regular behaviour.
149152
"useBaseException": [1]
150153
}
151154
```
152-
153-
- Object with `avoidPrefixException` exception.
154-
155+
- Object with `avoidPrefixException` exception:
155156
In some languages like spanish, specific units like "mil" does not use the base
156157
cardinal number prefix for unit 1.
157-
158158
```json
159159
{
160160
"singular": "mil",
161161
"avoidPrefixException": [1]
162162
}
163163
```
164-
165-
166164
- Object with `avoidInNumberPlural` exception.
167-
168165
In some languages like french, specific units like "cent" does not use the plural form inside of
169166
numbers wioth trailing numbers other than 0, for example "deux cents" and "deux cent trois".
170-
171167
```json
172168
{
173169
"singular": "cent",
174170
"plural": "cents",
175171
"avoidInNumberPlural": true
176172
}
177173
```
174+
- Object with `restrictedPlural` boolean:
175+
If plural is used only for numbers from 3 to 10 , but the singular form is used if the number is older than 11.
178176

179-
##### unitExceptions:
180-
Sometimes grammar exceptions affect the base cardinal joined to the unit. You
181-
can set specific exceptions to any base cardinal number.
177+
- Object with `useSingularEnding` exception and `useFewEnding` exception.
182178

183-
Spanish example:
184-
185-
```
186-
Without Exception (Wrong): 1232000 -> **uno** millón doscientos treinta y dos mil
187-
```
188-
189-
```
190-
With Exception: 1232000 -> **un** millón doscientos treinta y dos mil
191-
```
179+
In some languages like Ukrainian, singular form of the unit is also used for any values that end with 1 (21, 31, 14, ..., 101, ...) and "few" form of the unit is also used for any values that end with 2, 3 and 4 (22, 33, 44, ..., 104, ...). The `avoidEndingRules` exception provides values (1 to 999) where these rules must be ignored and the plural form must be used instead.
192180

193-
### English configuration example
194181
```json
195182
{
196-
"useLongScale": false,
197-
"baseSeparator": "-",
198-
"unitSeparator": "and ",
199-
"base": {
200-
"0": "zero",
201-
"1": "one",
202-
"2": "two",
203-
"3": "three",
204-
...
205-
"90": "ninety"
206-
},
207-
"units" : [
208-
"hundred",
209-
"thousand",
210-
"million",
211-
"billion",
212-
"trillion",
213-
...
214-
"quindecillion"
215-
],
216-
"unitExceptions": []
183+
"singular": "мільйон",
184+
"few": "мільйони",
185+
"plural": "мільйонів",
186+
"useSingularEnding": true,
187+
"useFewEnding": true,
188+
"avoidEndingRules": [11, 12, 13, 14, 111, 112, 113, 114, 211, 212, 213, 214, 311, 312, 313, 314, 411, 412, 413, 414, 511, 512, 513, 514, 611, 612, 613, 614, 711, 712, 713, 714, 811, 812, 813, 814, 911, 912, 913, 914]
217189
}
218190
```
219191

220-
### Spanish configuration example
221-
```json
222-
{
223-
"useLongScale": true,
224-
"baseSeparator": " y ",
225-
"unitSeparator": "",
226-
"base": {
227-
"0": "cero",
228-
"1": "uno",
229-
"2": "dos",
230-
"3": "tres",
231-
...
232-
"1000": "mil"
233-
},
234-
"unitExceptions": {
235-
"1": "un"
236-
},
237-
"units" : [
238-
{
239-
"singular": "ciento",
240-
"useBaseInstead": true,
241-
"useBaseException": [1]
242-
},
243-
{
244-
"singular": "mil",
245-
"avoidPrefixException": [1]
246-
},
247-
{
248-
"singular": "millón",
249-
"plural": "millones"
250-
},
251-
...
252-
]
253-
}
192+
##### unitExceptions:
193+
Sometimes grammar exceptions affect the base cardinal joined to the unit. You
194+
can set specific exceptions to any base cardinal number.
195+
Spanish example:
196+
```
197+
Without Exception (Wrong): 1232000 -> **uno** millón doscientos treinta y dos mil
198+
With Exception: 1232000 -> **un** millón doscientos treinta y dos mil
254199
```
255200

256201
## Contributing
257-
Do your changes and submit a PR. If you've write access and want to bump the
258-
version, run `mversion [major|minor|patch] -m`. That'll bump both `bower.json`
259-
and `package.json`.
202+
Do your changes and submit a PR. If you've write access and want to bump the version, run `mversion [major|minor|patch] -m`. That'll bump both `bower.json` and `package.json`.
260203

261204
## License
262-
This code is licensed under the MIT license for Pedro Tacla Yamada. For more
263-
information, please refer to the [LICENSE](/LICENSE) file.
205+
This code is licensed under the MIT license for Pedro Tacla Yamada. For more information, please refer to the [LICENSE](/LICENSE) file.

0 commit comments

Comments
 (0)