Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new language: no (Norwegian) #8

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Coverage Status](https://coveralls.io/repos/github/forzagreen/n2words/badge.svg?branch=master)](https://coveralls.io/github/forzagreen/n2words?branch=master)
[![npm](https://img.shields.io/npm/v/n2words.svg)](https://www.npmjs.com/package/n2words)

Convert numbers to words
Convert numbers to words, in multiple languages

## Install

Expand Down Expand Up @@ -33,6 +33,7 @@ n2words(123, {lang: 'es'}) // 'ciento veintitrés'
- `es` (Spanish)
- `fr` (French)
- `it` (Italian)
- `no` (Norwegian)
- `pt` (Portuguese)
- `ru` (Russian)
- `tr` (Turkish)
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
exports = module.exports = n2words;


var supportedLanguages = ['en', 'fr', 'es', 'de', 'pt', 'it', 'tr', 'ru', 'cz']
var supportedLanguages = ['en', 'fr', 'es', 'de', 'pt', 'it', 'tr', 'ru', 'cz', 'no']

/**
* Converts numbers to their written form.
Expand Down Expand Up @@ -38,6 +38,8 @@ function n2words(n, options) {
num = new Num2Word_RU();
} else if (lang === 'CZ') {
num = new Num2Word_CZ();
} else if (lang === 'NO') {
num = new Num2Word_NO();
}
return num.toCardinal(n);
}
Expand Down Expand Up @@ -862,3 +864,18 @@ function Num2Word_CZ() {
return words.join(' ')
}
}

function Num2Word_NO() {
Num2Word_Base.call(this)

this.cards = [{"1000000000000000000000000000000000": 'quintillard'}, {"1000000000000000000000000000000": 'quintillion'}, {"1000000000000000000000000000": 'quadrillard'}, {"1000000000000000000000000": 'quadrillion'}, {"1000000000000000000000": 'trillard'}, {"1000000000000000000": 'trillion'}, {"1000000000000000": 'billard'}, {"1000000000000": 'billion'}, {"1000000000": 'millard'}, {"1000000": 'million'}, {"1000": 'tusen'}, {"100": 'hundre'}, {"90": 'nitti'}, {"80": 'åtti'}, {"70": 'sytti'}, {"60": 'seksti'}, {"50": 'femti'}, {"40": 'førti'}, {"30": 'tretti'}, {"20": 'tjue'}, {"19": 'nitten'}, {"18": 'atten'}, {"17": 'sytten'}, {"16": 'seksten'}, {"15": 'femten'}, {"14": 'fjorten'}, {"13": 'tretten'}, {"12": 'tolv'}, {"11": 'elleve'}, {"10": 'ti'}, {"9": 'ni'}, {"8": 'åtte'}, {"7": 'syv'}, {"6": 'seks'}, {"5": 'fem'}, {"4": 'fire'}, {"3": 'tre'}, {"2": 'to'}, {"1": 'en'}, {"0": 'null'}]
this.merge = (lpair, rpair) => { //{'one':1}, {'hundred':100}
var ltext = Object.keys(lpair)[0], lnum = parseInt(Object.values(lpair)[0])
var rtext = Object.keys(rpair)[0], rnum = parseInt(Object.values(rpair)[0])
if (lnum == 1 && rnum < 100) return { [rtext]: rnum }
else if (100 > lnum && lnum > rnum) return { [`${ltext}-${rtext}`]: lnum + rnum }
else if (lnum >= 100 && 100 > rnum) return { [`${ltext} og ${rtext}`]: lnum + rnum }
else if (rnum > lnum) return { [`${ltext} ${rtext}`]: lnum * rnum }
return { [`${ltext}, ${rtext}`]: lnum + rnum }
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"italian",
"turkish",
"russian",
"czech"
"czech",
"norwegian"
],
"author": "Wael TELLAT",
"license": "MIT",
Expand Down
69 changes: 69 additions & 0 deletions test/test_no.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const assert = require('assert');
const n2words = require('../index.js');

const testCasesNorwegian = [
[0, 'null'],
[1, 'en'],
[2, 'to'],
[3, 'tre'],
[11, 'elleve'],
[12, 'tolv'],
[16, 'seksten'],
[19, 'nitten'],
[20, 'tjue'],
[21, 'tjue-en'],
[26, 'tjue-seks'],
[28, 'tjue-åtte'],
[30, 'tretti'],
[31, 'tretti-en'],
[40, 'førti'],
[44, 'førti-fire'],
[50, 'femti'],
[55, 'femti-fem'],
[60, 'seksti'],
[67, 'seksti-syv'],
[70, 'sytti'],
[79, 'sytti-ni'],
[89, 'åtti-ni'],
[95, 'nitti-fem'],
[100, 'en hundre'],
[101, 'en hundre og en'],
[199, 'en hundre og nitti-ni'],
[203, 'to hundre og tre'],
[287, 'to hundre og åtti-syv'],
[356, 'tre hundre og femti-seks'],
[400, 'fire hundre'],
[434, 'fire hundre og tretti-fire'],
[578, 'fem hundre og sytti-åtte'],
[689, 'seks hundre og åtti-ni'],
[729, 'syv hundre og tjue-ni'],
[894, 'åtte hundre og nitti-fire'],
[999, 'ni hundre og nitti-ni'],
[1000, 'en tusen'],
[1001, 'en tusen og en'],
[1097, 'en tusen og nitti-syv'],
[1104, 'en tusen, en hundre og fire'],
[1243, 'en tusen, to hundre og førti-tre'],
[2385, 'to tusen, tre hundre og åtti-fem'],
[3766, 'tre tusen, syv hundre og seksti-seks'],
[4196, 'fire tusen, en hundre og nitti-seks'],
[5846, 'fem tusen, åtte hundre og førti-seks'],
[6459, 'seks tusen, fire hundre og femti-ni'],
[7232, 'syv tusen, to hundre og tretti-to'],
[8569, 'åtte tusen, fem hundre og seksti-ni'],
[9539, 'ni tusen, fem hundre og tretti-ni'],
[1000000, 'en million'],
[1000001, 'en million og en'],
[4000000, 'fire million'],
[10000000000000, 'ti billion'],
[100000000000000, 'en hundre billion'],
[1000000000000000000, 'en trillion']
]

describe('Norwegian', function () {
it('should convert numbers correctly (Norwegian)', function () {
for (let i = 0; i < testCasesNorwegian.length; i++) {
assert.equal(n2words(testCasesNorwegian[i][0], { lang: 'no' }), testCasesNorwegian[i][1])
}
});
});