From 1c2eb49fabc24540c19eb2e8bd97667caa799e65 Mon Sep 17 00:00:00 2001 From: wael Date: Tue, 25 Feb 2020 21:16:26 +0100 Subject: [PATCH] Add no: Norwegian --- README.md | 3 ++- index.js | 19 +++++++++++++- package.json | 3 ++- test/test_no.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 test/test_no.js diff --git a/README.md b/README.md index 5e88ca2..7f09d6f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/index.js b/index.js index 23130c7..d32a53b 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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); } @@ -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 } + } +} \ No newline at end of file diff --git a/package.json b/package.json index 885c07e..f5a1754 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "italian", "turkish", "russian", - "czech" + "czech", + "norwegian" ], "author": "Wael TELLAT", "license": "MIT", diff --git a/test/test_no.js b/test/test_no.js new file mode 100644 index 0000000..4424649 --- /dev/null +++ b/test/test_no.js @@ -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]) + } + }); +});