Skip to content

Commit 289d4a9

Browse files
authored
Merge pull request #52 from ForzaGreen/master
Add support for Arabic
2 parents 08336d9 + 5f87083 commit 289d4a9

File tree

4 files changed

+279
-159
lines changed

4 files changed

+279
-159
lines changed

README.md

Lines changed: 42 additions & 153 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
1717
npm i --save written-number
1818
```
19-
20-
## Install with bower
21-
19+
With bower:
2220
```bash
2321
bower install written-number
2422
```
@@ -27,6 +25,10 @@ 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'
3032
```
3133

3234
## Options
@@ -36,70 +38,37 @@ writtenNumber(1234); // => 'one thousand two hundred and thirty-four'
3638
language to use. An i18n configuration object may be passed to support
3739
external language definitions.
3840

39-
## Internationalization
4041
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-
- English (Indian) `lang = "enIndian"`
50-
51-
### Spanish Example
52-
```javascript
53-
var writtenNumber = require('written-number');
54-
writtenNumber(1234, { lang: 'es' }); // => 'mil doscientos treinta y cuatro'
55-
```
56-
57-
```javascript
58-
var writtenNumber = require('written-number');
59-
writtenNumber.defaults.lang = 'es';
60-
writtenNumber(4758); // => 'cuatro mil setecientos cincuenta y ocho'
61-
```
6242

63-
### Portuguese (Brazil) Example
64-
```javascript
65-
var writtenNumber = require('written-number');
66-
writtenNumber(1234, { lang: 'pt' }); // => 'mil duzentos e trinta e quatro'
67-
```
68-
69-
### French Example
70-
```javascript
71-
var writtenNumber = require('written-number');
72-
writtenNumber(1234, { lang: 'fr' }); // => 'mille deux cent trente-quatre'
73-
```
43+
| Language | `lang` |
44+
|---------|--------|
45+
| English | `en` |
46+
| Portuguese (Brazil) | `pt` |
47+
| Portuguese (Portugal) | `ptPT` |
48+
| Spanish | `es` |
49+
| French | `fr` |
50+
| Esperanto | `eo` |
51+
| Vietnamese | `vi` |
52+
| Arabic | `ar` |
53+
| Turkish | `tr` |
54+
| English (Indian) | `enIndian` |
7455

75-
### Esperanto Example
76-
```javascript
77-
var writtenNumber = require('written-number');
78-
writtenNumber(1234, { lang: 'eo' }); // => 'mil ducent tridek kvar'
79-
```
8056

81-
### Vietnamese Example
8257
```javascript
8358
var writtenNumber = require('written-number');
84-
writtenNumber(1234, { lang: 'vi' }); // => 'một ngàn hai trăm và ba mươi bốn'
59+
writtenNumber(1234, {lang: 'es'}); // => 'mil doscientos treinta y cuatro'
60+
writtenNumber(1234, {lang: 'pt'}); // => 'mil duzentos e trinta e quatro'
61+
writtenNumber(1234, {lang: 'fr'}); // => 'mille deux cent trente-quatre'
62+
writtenNumber(1234, {lang: 'ar'}); // => 'ألف ومائتان وأربعة وثلاثون'
63+
writtenNumber(1234, {lang: 'eo'}); // => 'mil ducent tridek kvar'
64+
writtenNumber(1234, {lang: 'vi'}); // => 'một ngàn hai trăm và ba mươi bốn'
8565
```
8666

87-
## Options
88-
Property | Value
89-
-------------- | -------------
90-
noAnd | false
91-
lang | 'en'
92-
93-
### Configure your own language
94-
Each language has it's own unique grammar exceptions. You can create your own
95-
language.json file in the folder "i18n" and give writtenNumber support for it. I
96-
don't think the current scheme and logic cover all the cases, but may be cover
97-
some.
67+
## Configure your own language
68+
Each language has it's own unique grammar exceptions. You can create your own language.json file in the folder "i18n" and give writtenNumber support for it. I don't think the current scheme and logic cover all the cases, but may be cover some.
9869

9970
##### useLongScale:
100-
'Boolean' that indicates if it use [long or short
101-
scale](http://en.wikipedia.org/wiki/Long_and_short_scales). This differs the
102-
meaning of the words `billion`, `trillion` and so on.
71+
'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.
10372

10473
##### baseSeparator:
10574
'String' that separates the base cardinal numbers.
@@ -109,148 +78,68 @@ Example: 29 -> twenty`-`eight. Spanish uses the conector " y ".
10978
'String' that separates the units from the last base cardinal numbers.
11079
Example: 1234 -> one thousand two hundred **and** thirty-four
11180

81+
##### allSeparator:
82+
'String' that separates all cardinals, not only the last one.
83+
Example: 1125 -> ألف **و**مائة **و**خمسة **و**عشرون
84+
11285
##### base:
113-
Base cardinals numbers. Numbers that have unique names and are used to build
114-
others.
86+
Base cardinals numbers. Numbers that have unique names and are used to build others.
11587

11688
##### units:
11789
Number units.
11890
It can be:
11991
- String
120-
121-
- Object normal flow. Give support to singular and plural units. English does
122-
not need this, but spanish does.
123-
92+
- Object normal flow. Give support to singular, dual, and plural units. English does not need this, but spanish does.
12493
```json
12594
{
12695
"singular": "millón",
12796
"plural": "millones"
12897
}
12998
```
130-
13199
- Object with `useBaseInstead` exception.
132-
In some languages like spanish, specific units like "ciento", use the base
133-
cardinal number instead.
134-
135-
With `useBaseException` you can also specify with which unit (1 to 9) you don't
136-
want use the base cardinal instead and use the regular behaviour.
137-
100+
In some languages like spanish and arabic, specific units like "ciento", use the base cardinal number instead.
101+
- Object with `useBaseException`: You can also specify with which unit (1 to 9) you don't
102+
want use the base cardinal instead and use the regular behaviour:
138103
```json
139104
{
140105
"singular": "ciento",
141106
"useBaseInstead": true,
142107
"useBaseException": [1]
143108
}
144109
```
145-
146-
- Object with `avoidPrefixException` exception.
147-
110+
- Object with `avoidPrefixException` exception:
148111
In some languages like spanish, specific units like "mil" does not use the base
149112
cardinal number prefix for unit 1.
150-
151113
```json
152114
{
153115
"singular": "mil",
154116
"avoidPrefixException": [1]
155117
}
156118
```
157-
158-
159119
- Object with `avoidInNumberPlural` exception.
160-
161120
In some languages like french, specific units like "cent" does not use the plural form inside of
162121
numbers wioth trailing numbers other than 0, for example "deux cents" and "deux cent trois".
163-
164122
```json
165123
{
166124
"singular": "cent",
167125
"plural": "cents",
168126
"avoidInNumberPlural": true
169127
}
170128
```
129+
- Object with `restrictedPlural` boolean:
130+
If plural is used only for numbers from 3 to 10 , but the singular form is used if the number is older than 11.
171131

172132
##### unitExceptions:
173133
Sometimes grammar exceptions affect the base cardinal joined to the unit. You
174134
can set specific exceptions to any base cardinal number.
175-
176135
Spanish example:
177-
178136
```
179137
Without Exception (Wrong): 1232000 -> **uno** millón doscientos treinta y dos mil
180-
```
181-
182-
```
183138
With Exception: 1232000 -> **un** millón doscientos treinta y dos mil
184139
```
185140

186-
### English configuration example
187-
```json
188-
{
189-
"useLongScale": false,
190-
"baseSeparator": "-",
191-
"unitSeparator": "and ",
192-
"base": {
193-
"0": "zero",
194-
"1": "one",
195-
"2": "two",
196-
"3": "three",
197-
...
198-
"90": "ninety"
199-
},
200-
"units" : [
201-
"hundred",
202-
"thousand",
203-
"million",
204-
"billion",
205-
"trillion",
206-
...
207-
"quindecillion"
208-
],
209-
"unitExceptions": []
210-
}
211-
```
212-
213-
### Spanish configuration example
214-
```json
215-
{
216-
"useLongScale": true,
217-
"baseSeparator": " y ",
218-
"unitSeparator": "",
219-
"base": {
220-
"0": "cero",
221-
"1": "uno",
222-
"2": "dos",
223-
"3": "tres",
224-
...
225-
"1000": "mil"
226-
},
227-
"unitExceptions": {
228-
"1": "un"
229-
},
230-
"units" : [
231-
{
232-
"singular": "ciento",
233-
"useBaseInstead": true,
234-
"useBaseException": [1]
235-
},
236-
{
237-
"singular": "mil",
238-
"avoidPrefixException": [1]
239-
},
240-
{
241-
"singular": "millón",
242-
"plural": "millones"
243-
},
244-
...
245-
]
246-
}
247-
```
248-
249141
## Contributing
250-
Do your changes and submit a PR. If you've write access and want to bump the
251-
version, run `mversion [major|minor|patch] -m`. That'll bump both `bower.json`
252-
and `package.json`.
142+
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`.
253143

254144
## License
255-
This code is licensed under the MIT license for Pedro Tacla Yamada. For more
256-
information, please refer to the [LICENSE](/LICENSE) file.
145+
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)