├── .zappr.yaml ├── .npmignore ├── test ├── .eslintrc └── index.js ├── MAINTAINERS ├── .eslintrc ├── update ├── fetch-npm-modules.js ├── generate-currency-symbol-map.js ├── generate-country-information.js ├── index.js ├── generate-number-separators.js └── generate-position-functions.js ├── .editorconfig ├── bower.json ├── .github └── workflows │ ├── publish.yml │ └── build.yml ├── .gitignore ├── package.json ├── LICENSE ├── benchmark.js ├── data ├── symbol-map.js ├── separators.js ├── positions.js └── country-currency.js ├── ACKNOWLEDGEMENTS ├── index.js └── README.md /.zappr.yaml: -------------------------------------------------------------------------------- 1 | X-Zalando-Type: tools 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # IDEs 2 | .idea 3 | 4 | # Logs 5 | logs 6 | *.log 7 | 8 | # Development 9 | test 10 | update 11 | 12 | # Dotfiles 13 | .* 14 | 15 | # Dependency directory 16 | node_modules 17 | .github 18 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "globals": { 3 | "describe": false, 4 | "it": false, 5 | "before": false, 6 | "after": false, 7 | "beforeEach": false, 8 | "afterEach": false 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MAINTAINERS: -------------------------------------------------------------------------------- 1 | Aditya Pratap Singh 2 | Aleksandr Ponimaskin 3 | Boopathi Nedunchezhiyan 4 | Dmitriy Kubyshkin 5 | Mariano Carballal 6 | Oskari Porkka 7 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true, 4 | "es6": true 5 | }, 6 | "rules": { 7 | "brace-style": [2, "1tbs"], 8 | "comma-style": [2, "last"], 9 | "no-constant-condition": 2, 10 | "semi": [2, "always"], 11 | "keyword-spacing": [2], 12 | "space-before-blocks": [2, "always"], 13 | "strict": [2, "global"], 14 | "quotes": [2, "single"] 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /update/fetch-npm-modules.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const childProcess = require('child_process'); 5 | const ROOT_DIR = path.normalize(path.join(__dirname, '..')); 6 | const CLDR_DATA_DIR = path.join(ROOT_DIR, 'node_modules', 'cldr-data', 'main'); 7 | 8 | module.exports = function () { 9 | childProcess.execSync('npm i --no-save --progress false cldr-data country-data', { cwd: ROOT_DIR }); 10 | return CLDR_DATA_DIR; 11 | }; 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # For most files 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | indent_style = space 13 | indent_size = 4 14 | 15 | # Override specific settings for npm generated json files to avoid big diffs 16 | [{package.json, npm-shrinkwrap.json}] 17 | indent_style = space 18 | indent_size = 2 19 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "banknote", 3 | "description": "Module for handling currency formatting.", 4 | "main": "index.js", 5 | "authors": [ 6 | "Dmitriy Kubyshkin " 7 | ], 8 | "license": "MIT", 9 | "keywords": [ 10 | "i18n" 11 | ], 12 | "homepage": "https://github.com/zalando/banknote", 13 | "moduleType": [ 14 | "globals", 15 | "node" 16 | ], 17 | "ignore": [ 18 | "node_modules", 19 | "bower_components", 20 | "public/lib", 21 | "test", 22 | "update" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | on: 3 | release: 4 | types: [created] 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | # Setup .npmrc file to publish to npm 11 | - uses: actions/setup-node@v2 12 | with: 13 | node-version: 14 14 | registry-url: "https://registry.npmjs.org" 15 | - run: npm install 16 | - run: npm run lint && npm run test 17 | - run: npm publish --access public 18 | env: 19 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 20 | -------------------------------------------------------------------------------- /update/generate-currency-symbol-map.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | 6 | module.exports = function (dataDir, outputFileName) { 7 | const map = {}; 8 | const info = require(path.join(dataDir, 'en', 'currencies.json')); 9 | const data = info.main.en.numbers.currencies; 10 | Object.keys(data).forEach(function (currencyCode) { 11 | map[currencyCode] = data[currencyCode]['symbol-alt-narrow']; 12 | }); 13 | 14 | var output = '\'use strict\';\n\nmodule.exports = ' + JSON.stringify(map, null, 4).replace(/"/g, '\'') + ';\n'; 15 | fs.writeFileSync(outputFileName, output); 16 | }; 17 | -------------------------------------------------------------------------------- /update/generate-country-information.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | 6 | module.exports = function (dataDir, outputFileName) { 7 | const map = {}; 8 | require('country-data').countries.all.map(function (country) { 9 | // Since this data is used only when explicit currency is not set in 10 | // a formatting function, it's ok to use the first one as the default. 11 | map[country.alpha2] = country.currencies[0]; 12 | }); 13 | var output = '\'use strict\';\n\nmodule.exports = ' + JSON.stringify(map, null, 4).replace(/"/g, '\'') + ';\n'; 14 | fs.writeFileSync(outputFileName, output); 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IDEs 2 | .idea 3 | 4 | # Logs 5 | logs 6 | *.log 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | 13 | # Directory for instrumented libs generated by jscoverage/JSCover 14 | lib-cov 15 | 16 | # Coverage directory used by tools like istanbul 17 | coverage 18 | 19 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 20 | .grunt 21 | 22 | # node-waf configuration 23 | .lock-wscript 24 | 25 | # Compiled binary addons (http://nodejs.org/api/addons.html) 26 | build/Release 27 | 28 | # Dependency directory 29 | # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git 30 | node_modules 31 | public 32 | bower_components 33 | 34 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | strategy: 14 | matrix: 15 | node-version: [10, 12, 14, 15] 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Use Node.js ${{ matrix.node-version }} 20 | uses: actions/setup-node@v2 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | 24 | - name: Install 25 | run: npm install 26 | 27 | - name: Link check 28 | run: npm run lint 29 | 30 | - name: Tests and benchmark 31 | run: npm run test && npm run benchmark 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "banknote", 3 | "version": "0.2.7", 4 | "description": "Module for handling currency formatting.", 5 | "main": "index.js", 6 | "scripts": { 7 | "update-cldr-data": "node update", 8 | "lint": "eslint .", 9 | "test": "mocha test", 10 | "benchmark": "node benchmark", 11 | "tdd": "mocha --watch test" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "ssh://git@github.com:zalando/banknote.git" 16 | }, 17 | "keywords": [ 18 | "i18n" 19 | ], 20 | "author": "Dmitriy Kubyshkin ", 21 | "license": "MIT", 22 | "devDependencies": { 23 | "benchmark": "^2.1.4", 24 | "eslint": "^6.8.0", 25 | "mocha": "^7.1.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /update/index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | const path = require('path'); 5 | 6 | console.info('[INFO] Installing necessary NPM modules...'); 7 | const cldrDataDir = require('./fetch-npm-modules')(); 8 | 9 | console.info('[INFO] Generating Positioning Functions...'); 10 | require('./generate-position-functions')(cldrDataDir, path.join(__dirname, '..', 'data', 'positions.js')); 11 | 12 | console.info('[INFO] Generating Number Separator Map...'); 13 | require('./generate-number-separators')(cldrDataDir, path.join(__dirname, '..', 'data', 'separators.js')); 14 | 15 | console.info('[INFO] Generating Currency Symbol Map...'); 16 | require('./generate-currency-symbol-map')(cldrDataDir, path.join(__dirname, '..', 'data', 'symbol-map.js')); 17 | 18 | console.info('[INFO] Generating Country Information...'); 19 | require('./generate-country-information')(cldrDataDir, path.join(__dirname, '..', 'data', 'country-currency.js')); 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Zalando SE 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /benchmark.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Benchmark = require('benchmark'); 4 | const {formatSubunitAmount, formattingForLocale} = require('.'); 5 | 6 | const intlFormatter = Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }); 7 | 8 | const formatting = formattingForLocale('de-DE', 'EUR'); 9 | 10 | new Benchmark.Suite('') 11 | .add('big number', () => { 12 | formatSubunitAmount(100052233, formatting); 13 | }) 14 | .add('big negative number', () => { 15 | formatSubunitAmount(-100052233, formatting); 16 | }) 17 | .add('middle number', () => { 18 | formatSubunitAmount(120004, formatting); 19 | }) 20 | .add('small number', () => { 21 | formatSubunitAmount(1234, formatting); 22 | }) 23 | .add('big number intl', () => { 24 | intlFormatter.format(100052233); 25 | }) 26 | .add('big negative number intl', () => { 27 | intlFormatter.format(-100052233); 28 | }) 29 | .add('middle number intl', () => { 30 | intlFormatter.format(120004); 31 | }) 32 | .add('small number intl', () => { 33 | intlFormatter.format(1234); 34 | }) 35 | // add listeners 36 | .on('cycle', (event) => { 37 | // tslint:disable-next-line 38 | console.log(String(event.target)); 39 | }).run(); 40 | -------------------------------------------------------------------------------- /update/generate-number-separators.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const fs = require('fs'); 5 | 6 | module.exports = function (dataDir, outputFileName) { 7 | 8 | const ruleCache = {}; 9 | fs.readdirSync(dataDir).forEach(function (locale) { 10 | const info = require(path.join(dataDir, locale, 'numbers.json')); 11 | const rules = info.main[locale].numbers['symbols-numberSystem-latn']; 12 | const key = rules.decimal + rules.group; 13 | const language = locale.match(/^([a-zA-Z]{2,4})[-_]?/)[1]; 14 | if (ruleCache[key]) { 15 | if (ruleCache[key].indexOf(language) === -1) { 16 | ruleCache[key].push(locale); 17 | } 18 | } else { 19 | ruleCache[key] = [locale]; 20 | } 21 | if (key.length !== 2) { 22 | console.warn('[WARN] Locale ' + locale + ' has a number separator bigger than 1 code point'); 23 | } 24 | }); 25 | 26 | var output = '\'use strict\';\n\nvar separators = {};\n\n'; 27 | 28 | Object.keys(ruleCache).forEach(function (separatorPair) { 29 | const locales = ruleCache[separatorPair]; 30 | const exports = locales.map(function (locale) { 31 | return 'separators[\'' + locale + '\']'; 32 | }).join(' = \n'); 33 | output += exports + ' = \'' + separatorPair.replace('\'', '\\\'') + '\';\n\n'; 34 | }); 35 | 36 | output += 'module.exports = separators;\n'; 37 | fs.writeFileSync(outputFileName, output); 38 | }; 39 | -------------------------------------------------------------------------------- /data/symbol-map.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 'AOA': 'Kz', 5 | 'ARS': '$', 6 | 'AUD': '$', 7 | 'BAM': 'KM', 8 | 'BBD': '$', 9 | 'BDT': '৳', 10 | 'BMD': '$', 11 | 'BND': '$', 12 | 'BOB': 'Bs', 13 | 'BRL': 'R$', 14 | 'BSD': '$', 15 | 'BWP': 'P', 16 | 'BYN': 'р.', 17 | 'BZD': '$', 18 | 'CAD': '$', 19 | 'CLP': '$', 20 | 'CNY': '¥', 21 | 'COP': '$', 22 | 'CRC': '₡', 23 | 'CUC': '$', 24 | 'CUP': '$', 25 | 'CZK': 'Kč', 26 | 'DKK': 'kr', 27 | 'DOP': '$', 28 | 'EGP': 'E£', 29 | 'ESP': '₧', 30 | 'EUR': '€', 31 | 'FJD': '$', 32 | 'FKP': '£', 33 | 'GBP': '£', 34 | 'GEL': '₾', 35 | 'GIP': '£', 36 | 'GNF': 'FG', 37 | 'GTQ': 'Q', 38 | 'GYD': '$', 39 | 'HKD': '$', 40 | 'HNL': 'L', 41 | 'HRK': 'kn', 42 | 'HUF': 'Ft', 43 | 'IDR': 'Rp', 44 | 'ILS': '₪', 45 | 'INR': '₹', 46 | 'ISK': 'kr', 47 | 'JMD': '$', 48 | 'JPY': '¥', 49 | 'KHR': '៛', 50 | 'KMF': 'CF', 51 | 'KPW': '₩', 52 | 'KRW': '₩', 53 | 'KYD': '$', 54 | 'KZT': '₸', 55 | 'LAK': '₭', 56 | 'LBP': 'L£', 57 | 'LKR': 'Rs', 58 | 'LRD': '$', 59 | 'LTL': 'Lt', 60 | 'LVL': 'Ls', 61 | 'MGA': 'Ar', 62 | 'MMK': 'K', 63 | 'MNT': '₮', 64 | 'MUR': 'Rs', 65 | 'MXN': '$', 66 | 'MYR': 'RM', 67 | 'NAD': '$', 68 | 'NGN': '₦', 69 | 'NIO': 'C$', 70 | 'NOK': 'kr', 71 | 'NPR': 'Rs', 72 | 'NZD': '$', 73 | 'PHP': '₱', 74 | 'PKR': 'Rs', 75 | 'PLN': 'zł', 76 | 'PYG': '₲', 77 | 'RON': 'lei', 78 | 'RUB': '₽', 79 | 'RUR': 'р.', 80 | 'RWF': 'RF', 81 | 'SBD': '$', 82 | 'SEK': 'kr', 83 | 'SGD': '$', 84 | 'SHP': '£', 85 | 'SRD': '$', 86 | 'SSP': '£', 87 | 'STN': 'Db', 88 | 'SYP': '£', 89 | 'THB': '฿', 90 | 'TOP': 'T$', 91 | 'TRY': '₺', 92 | 'TTD': '$', 93 | 'TWD': '$', 94 | 'UAH': '₴', 95 | 'USD': '$', 96 | 'UYU': '$', 97 | 'VEF': 'Bs', 98 | 'VND': '₫', 99 | 'XCD': '$', 100 | 'ZAR': 'R', 101 | 'ZMW': 'ZK' 102 | }; 103 | -------------------------------------------------------------------------------- /ACKNOWLEDGEMENTS: -------------------------------------------------------------------------------- 1 | ACKNOWLEDGEMENTS 2 | 3 | This open source software makes use other 3rd party open source 4 | software. Below is a list of notices may be required to be included 5 | with this software. 6 | 7 | ----------------------------------------------------------------------- 8 | 9 | CLDR - Unicode Common Locale Data Repository 10 | 11 | COPYRIGHT AND PERMISSION NOTICE 12 | 13 | Copyright © 1991-2015 Unicode, Inc. All rights reserved. 14 | Distributed under the Terms of Use in 15 | http://www.unicode.org/copyright.html. 16 | 17 | Permission is hereby granted, free of charge, to any person obtaining 18 | a copy of the Unicode data files and any associated documentation 19 | (the "Data Files") or Unicode software and any associated documentation 20 | (the "Software") to deal in the Data Files or Software 21 | without restriction, including without limitation the rights to use, 22 | copy, modify, merge, publish, distribute, and/or sell copies of 23 | the Data Files or Software, and to permit persons to whom the Data Files 24 | or Software are furnished to do so, provided that 25 | (a) this copyright and permission notice appear with all copies 26 | of the Data Files or Software, 27 | (b) this copyright and permission notice appear in associated 28 | documentation, and 29 | (c) there is clear notice in each modified Data File or in the Software 30 | as well as in the documentation associated with the Data File(s) or 31 | Software that the data or software has been modified. 32 | 33 | THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF 34 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 35 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 36 | NONINFRINGEMENT OF THIRD PARTY RIGHTS. 37 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS 38 | NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL 39 | DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 40 | DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 41 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 42 | PERFORMANCE OF THE DATA FILES OR SOFTWARE. 43 | 44 | Except as contained in this notice, the name of a copyright holder 45 | shall not be used in advertising or otherwise to promote the sale, 46 | use or other dealings in these Data Files or Software without prior 47 | written authorization of the copyright holder. 48 | 49 | ----------------------------------------------------------------------- 50 | -------------------------------------------------------------------------------- /update/generate-position-functions.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const fs = require('fs'); 5 | 6 | const LEFT_TO_RIGHT_MARK = '\u200e'; 7 | const CURRENCY_SYMBOL_VAR_NAME = 'symbol'; 8 | const AMOUNT_VAR_NAME = 'amount'; 9 | const MINUS_VAR_NAME = 'minus'; 10 | 11 | function transformPatternIntoJsExpression(pattern, patternType) { 12 | var hasCustomMinusPosition = false; 13 | var hasLeftToRightMark = false; 14 | var parts = []; 15 | pattern.replace(/(\u00a4)|(-)|([#0.,]+)|([^\u00a4#0.,-]+)/g, function (part, symbol, minus, amount, rest) { 16 | if (symbol) { 17 | parts.push(CURRENCY_SYMBOL_VAR_NAME); 18 | } else if (minus) { 19 | hasCustomMinusPosition = true; 20 | parts.push(MINUS_VAR_NAME); 21 | } else if (amount) { 22 | parts.push(AMOUNT_VAR_NAME); 23 | } else if (rest && rest.length) { 24 | if (rest === LEFT_TO_RIGHT_MARK) { 25 | hasLeftToRightMark = true; 26 | } else { 27 | parts.push('\'' + rest + '\''); 28 | } 29 | } 30 | return parts; 31 | }); 32 | if (!hasCustomMinusPosition && patternType === 'implicitMinus') { 33 | parts.unshift(MINUS_VAR_NAME); 34 | } 35 | if (hasLeftToRightMark) { 36 | parts.unshift('\'' + LEFT_TO_RIGHT_MARK + '\''); 37 | } 38 | return parts.join(' + '); 39 | } 40 | 41 | function generateFunctionBody(positive, negative) { 42 | if (negative) { 43 | return 'return ' + MINUS_VAR_NAME + ' ? (' + 44 | transformPatternIntoJsExpression(negative, 'implicitMinus') + ') : (' + 45 | transformPatternIntoJsExpression(positive) + ');'; 46 | } else { 47 | return 'return ' + transformPatternIntoJsExpression(positive, 'implicitMinus') + ';'; 48 | } 49 | } 50 | 51 | module.exports = function (dataDir, outputFileName) { 52 | 53 | const functionCache = {}; 54 | fs.readdirSync(dataDir).forEach(function (locale) { 55 | const info = require(path.join(dataDir, locale, 'numbers.json')); 56 | const pattern = info.main[locale].numbers['currencyFormats-numberSystem-latn'].standard; 57 | const positiveAndNegative = pattern.split(';'); 58 | const functionBody = generateFunctionBody.apply(undefined, positiveAndNegative); 59 | 60 | const language = locale.match(/^([a-zA-Z]{2,4})[-_]?/)[1]; 61 | 62 | if (functionCache[functionBody]) { 63 | if (functionCache[functionBody].indexOf(language) === -1) { 64 | functionCache[functionBody].push(locale); 65 | } 66 | } else { 67 | functionCache[functionBody] = [locale]; 68 | } 69 | }); 70 | 71 | var result = '\'use strict\';\n\nvar positions = {};\n\n'; 72 | 73 | Object.keys(functionCache).forEach(function (functionBody) { 74 | const locales = functionCache[functionBody]; 75 | const exports = locales.map(function (locale) { 76 | return 'positions[\'' + locale + '\']'; 77 | }).join(' = \n'); 78 | result += exports + 79 | ' = function (' + CURRENCY_SYMBOL_VAR_NAME + ', ' + AMOUNT_VAR_NAME + ', ' + MINUS_VAR_NAME + ') {\n' + 80 | ' ' + functionBody + '\n};\n\n'; 81 | }); 82 | 83 | result += 'module.exports = positions;\n'; 84 | 85 | fs.writeFileSync(outputFileName, result); 86 | }; 87 | -------------------------------------------------------------------------------- /data/separators.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var separators = {}; 4 | 5 | separators['af'] = 6 | separators['be'] = 7 | separators['bg'] = 8 | separators['cs'] = 9 | separators['de-AT'] = 10 | separators['en-FI'] = 11 | separators['en-SE'] = 12 | separators['en-ZA'] = 13 | separators['es-CR'] = 14 | separators['et'] = 15 | separators['fi'] = 16 | separators['fr-CA'] = 17 | separators['hu'] = 18 | separators['hy'] = 19 | separators['ka'] = 20 | separators['kk'] = 21 | separators['ky'] = 22 | separators['lt'] = 23 | separators['lv'] = 24 | separators['nb'] = 25 | separators['pl'] = 26 | separators['no'] = 27 | separators['pt-AO'] = 28 | separators['pt-CH'] = 29 | separators['pt-CV'] = 30 | separators['pt-GQ'] = 31 | separators['pt-GW'] = 32 | separators['pt-LU'] = 33 | separators['pt-MO'] = 34 | separators['pt-MZ'] = 35 | separators['pt-PT'] = 36 | separators['pt-ST'] = 37 | separators['pt-TL'] = 38 | separators['ru'] = 39 | separators['sk'] = 40 | separators['sq'] = 41 | separators['sv'] = 42 | separators['tk'] = 43 | separators['uk'] = 44 | separators['uz'] = ', '; 45 | 46 | separators['am'] = 47 | separators['ar'] = 48 | separators['as'] = 49 | separators['bn'] = 50 | separators['cy'] = 51 | separators['en'] = 52 | separators['es-419'] = 53 | separators['es-BR'] = 54 | separators['es-BZ'] = 55 | separators['es-CU'] = 56 | separators['es-DO'] = 57 | separators['es-GT'] = 58 | separators['es-HN'] = 59 | separators['es-MX'] = 60 | separators['es-NI'] = 61 | separators['es-PA'] = 62 | separators['es-PE'] = 63 | separators['es-PR'] = 64 | separators['es-SV'] = 65 | separators['es-US'] = 66 | separators['fa'] = 67 | separators['fil'] = 68 | separators['ga'] = 69 | separators['gu'] = 70 | separators['he'] = 71 | separators['hi'] = 72 | separators['ja'] = 73 | separators['kn'] = 74 | separators['ko'] = 75 | separators['ml'] = 76 | separators['mn'] = 77 | separators['mr'] = 78 | separators['ms'] = 79 | separators['my'] = 80 | separators['ne'] = 81 | separators['or'] = 82 | separators['pa'] = 83 | separators['root'] = 84 | separators['sd'] = 85 | separators['si'] = 86 | separators['so'] = 87 | separators['sw'] = 88 | separators['ta'] = 89 | separators['te'] = 90 | separators['th'] = 91 | separators['ur'] = 92 | separators['yue'] = 93 | separators['zh'] = 94 | separators['zu'] = '.,'; 95 | 96 | separators['ar-DZ'] = 97 | separators['ar-LB'] = 98 | separators['ar-LY'] = 99 | separators['ar-MA'] = 100 | separators['ar-MR'] = 101 | separators['ar-TN'] = 102 | separators['az'] = 103 | separators['bs'] = 104 | separators['ca'] = 105 | separators['da'] = 106 | separators['de'] = 107 | separators['el'] = 108 | separators['en-AT'] = 109 | separators['en-BE'] = 110 | separators['en-DE'] = 111 | separators['en-DK'] = 112 | separators['en-NL'] = 113 | separators['en-SI'] = 114 | separators['es'] = 115 | separators['eu'] = 116 | separators['fr-LU'] = 117 | separators['fr-MA'] = 118 | separators['gl'] = 119 | separators['hr'] = 120 | separators['id'] = 121 | separators['is'] = 122 | separators['it'] = 123 | separators['jv'] = 124 | separators['km'] = 125 | separators['lo'] = 126 | separators['mk'] = 127 | separators['ms-BN'] = 128 | separators['nl'] = 129 | separators['ps'] = 130 | separators['pt'] = 131 | separators['ro'] = 132 | separators['sl'] = 133 | separators['sr'] = 134 | separators['sw-CD'] = 135 | separators['tr'] = 136 | separators['vi'] = ',.'; 137 | 138 | separators['de-CH'] = 139 | separators['de-LI'] = 140 | separators['en-CH'] = 141 | separators['fr-CH'] = 142 | separators['it-CH'] = '.’'; 143 | 144 | separators['fr'] = ', '; 145 | 146 | module.exports = separators; 147 | -------------------------------------------------------------------------------- /data/positions.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var positions = {}; 4 | 5 | positions['af'] = 6 | positions['am'] = 7 | positions['cy'] = 8 | positions['en'] = 9 | positions['es-419'] = 10 | positions['es-BO'] = 11 | positions['es-BR'] = 12 | positions['es-BZ'] = 13 | positions['es-CR'] = 14 | positions['es-CU'] = 15 | positions['es-DO'] = 16 | positions['es-GQ'] = 17 | positions['es-GT'] = 18 | positions['es-HN'] = 19 | positions['es-MX'] = 20 | positions['es-NI'] = 21 | positions['es-PA'] = 22 | positions['es-PR'] = 23 | positions['es-SV'] = 24 | positions['es-US'] = 25 | positions['fil'] = 26 | positions['ga'] = 27 | positions['gu'] = 28 | positions['hi'] = 29 | positions['id'] = 30 | positions['ja'] = 31 | positions['kn'] = 32 | positions['ko'] = 33 | positions['ml'] = 34 | positions['mr'] = 35 | positions['ms'] = 36 | positions['or'] = 37 | positions['si'] = 38 | positions['so'] = 39 | positions['te'] = 40 | positions['th'] = 41 | positions['tr'] = 42 | positions['yue'] = 43 | positions['zh'] = 44 | positions['zu'] = function (symbol, amount, minus) { 45 | return minus + symbol + amount; 46 | }; 47 | 48 | positions['ar'] = 49 | positions['as'] = 50 | positions['de-AT'] = 51 | positions['de-LI'] = 52 | positions['en-AT'] = 53 | positions['en-US-POSIX'] = 54 | positions['es-AR'] = 55 | positions['es-CO'] = 56 | positions['es-PE'] = 57 | positions['es-UY'] = 58 | positions['fa-AF'] = 59 | positions['jv'] = 60 | positions['mn'] = 61 | positions['ms-BN'] = 62 | positions['nb'] = 63 | positions['ne'] = 64 | positions['pa'] = 65 | positions['pt'] = 66 | positions['root'] = 67 | positions['sd'] = 68 | positions['sw'] = 69 | positions['ta'] = 70 | positions['ur'] = function (symbol, amount, minus) { 71 | return minus + symbol + ' ' + amount; 72 | }; 73 | 74 | positions['az'] = 75 | positions['be'] = 76 | positions['bg'] = 77 | positions['bs'] = 78 | positions['ca'] = 79 | positions['cs'] = 80 | positions['da'] = 81 | positions['de'] = 82 | positions['el'] = 83 | positions['en-150'] = 84 | positions['en-BE'] = 85 | positions['en-DE'] = 86 | positions['en-DK'] = 87 | positions['en-FI'] = 88 | positions['en-SE'] = 89 | positions['en-SI'] = 90 | positions['es'] = 91 | positions['et'] = 92 | positions['eu'] = 93 | positions['fi'] = 94 | positions['fr'] = 95 | positions['gl'] = 96 | positions['hr'] = 97 | positions['hu'] = 98 | positions['hy'] = 99 | positions['is'] = 100 | positions['it'] = 101 | positions['ka'] = 102 | positions['kk'] = 103 | positions['ky'] = 104 | positions['lt'] = 105 | positions['lv'] = 106 | positions['mk'] = 107 | positions['my'] = 108 | positions['pl'] = 109 | positions['no'] = 110 | positions['ps'] = 111 | positions['pt-AO'] = 112 | positions['pt-CH'] = 113 | positions['pt-CV'] = 114 | positions['pt-GQ'] = 115 | positions['pt-GW'] = 116 | positions['pt-LU'] = 117 | positions['pt-MO'] = 118 | positions['pt-MZ'] = 119 | positions['pt-PT'] = 120 | positions['pt-ST'] = 121 | positions['pt-TL'] = 122 | positions['ro'] = 123 | positions['ru'] = 124 | positions['sk'] = 125 | positions['sl'] = 126 | positions['sq'] = 127 | positions['sr'] = 128 | positions['sv'] = 129 | positions['tk'] = 130 | positions['uk'] = 131 | positions['uz'] = 132 | positions['vi'] = function (symbol, amount, minus) { 133 | return minus + amount + ' ' + symbol; 134 | }; 135 | 136 | positions['bn'] = 137 | positions['km'] = function (symbol, amount, minus) { 138 | return minus + amount + symbol; 139 | }; 140 | 141 | positions['de-CH'] = 142 | positions['en-CH'] = 143 | positions['fr-CH'] = 144 | positions['it-CH'] = function (symbol, amount, minus) { 145 | return minus ? (symbol + minus + amount) : (symbol + ' ' + amount); 146 | }; 147 | 148 | positions['en-NL'] = 149 | positions['es-PY'] = 150 | positions['nl'] = function (symbol, amount, minus) { 151 | return minus ? (symbol + ' ' + minus + amount) : (symbol + ' ' + amount); 152 | }; 153 | 154 | positions['es-CL'] = 155 | positions['es-EC'] = 156 | positions['es-VE'] = 157 | positions['lo'] = function (symbol, amount, minus) { 158 | return minus ? (symbol + minus + amount) : (symbol + amount); 159 | }; 160 | 161 | positions['fa'] = function (symbol, amount, minus) { 162 | return '‎' + minus + symbol + ' ' + amount; 163 | }; 164 | 165 | positions['he'] = function (symbol, amount, minus) { 166 | return minus ? ('‏' + minus + amount + ' ' + symbol) : ('‏' + amount + ' ' + symbol); 167 | }; 168 | 169 | module.exports = positions; 170 | -------------------------------------------------------------------------------- /data/country-currency.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 'AC': 'USD', 5 | 'AD': 'EUR', 6 | 'AE': 'AED', 7 | 'AF': 'AFN', 8 | 'AG': 'XCD', 9 | 'AL': 'ALL', 10 | 'AM': 'AMD', 11 | 'AO': 'AOA', 12 | 'AR': 'ARS', 13 | 'AS': 'USD', 14 | 'AT': 'EUR', 15 | 'AU': 'AUD', 16 | 'AW': 'AWG', 17 | 'AX': 'EUR', 18 | 'AZ': 'AZN', 19 | 'BA': 'BAM', 20 | 'BB': 'BBD', 21 | 'BD': 'BDT', 22 | 'BE': 'EUR', 23 | 'BF': 'XOF', 24 | 'BG': 'BGN', 25 | 'BH': 'BHD', 26 | 'BI': 'BIF', 27 | 'BJ': 'XOF', 28 | 'BL': 'EUR', 29 | 'BM': 'BMD', 30 | 'BN': 'BND', 31 | 'BO': 'BOB', 32 | 'BR': 'BRL', 33 | 'BS': 'BSD', 34 | 'BT': 'INR', 35 | 'BV': 'NOK', 36 | 'BW': 'BWP', 37 | 'BZ': 'BZD', 38 | 'CA': 'CAD', 39 | 'CC': 'AUD', 40 | 'CD': 'CDF', 41 | 'CF': 'XAF', 42 | 'CG': 'XAF', 43 | 'CH': 'CHF', 44 | 'CI': 'XOF', 45 | 'CK': 'NZD', 46 | 'CL': 'CLP', 47 | 'CM': 'XAF', 48 | 'CN': 'CNY', 49 | 'CO': 'COP', 50 | 'CP': 'EUR', 51 | 'CR': 'CRC', 52 | 'CU': 'CUP', 53 | 'CV': 'CVE', 54 | 'CW': 'ANG', 55 | 'CX': 'AUD', 56 | 'CY': 'EUR', 57 | 'CZ': 'CZK', 58 | 'DE': 'EUR', 59 | 'DG': 'USD', 60 | 'DJ': 'DJF', 61 | 'DK': 'DKK', 62 | 'DM': 'XCD', 63 | 'DO': 'DOP', 64 | 'DZ': 'DZD', 65 | 'EA': 'EUR', 66 | 'EC': 'USD', 67 | 'EE': 'EUR', 68 | 'EG': 'EGP', 69 | 'EH': 'MAD', 70 | 'ER': 'ERN', 71 | 'ES': 'EUR', 72 | 'ET': 'ETB', 73 | 'EU': 'EUR', 74 | 'FI': 'EUR', 75 | 'FJ': 'FJD', 76 | 'FK': 'FKP', 77 | 'FM': 'USD', 78 | 'FO': 'DKK', 79 | 'FR': 'EUR', 80 | 'FX': 'EUR', 81 | 'GA': 'XAF', 82 | 'GB': 'GBP', 83 | 'GD': 'XCD', 84 | 'GF': 'EUR', 85 | 'GG': 'GBP', 86 | 'GH': 'GHS', 87 | 'GI': 'GIP', 88 | 'GL': 'DKK', 89 | 'GM': 'GMD', 90 | 'GN': 'GNF', 91 | 'GP': 'EUR', 92 | 'GQ': 'XAF', 93 | 'GR': 'EUR', 94 | 'GS': 'GBP', 95 | 'GT': 'GTQ', 96 | 'GU': 'USD', 97 | 'GW': 'XOF', 98 | 'GY': 'GYD', 99 | 'HK': 'HKD', 100 | 'HM': 'AUD', 101 | 'HN': 'HNL', 102 | 'HR': 'HRK', 103 | 'HT': 'HTG', 104 | 'HU': 'HUF', 105 | 'IC': 'EUR', 106 | 'ID': 'IDR', 107 | 'IE': 'EUR', 108 | 'IL': 'ILS', 109 | 'IM': 'GBP', 110 | 'IN': 'INR', 111 | 'IO': 'USD', 112 | 'IQ': 'IQD', 113 | 'IR': 'IRR', 114 | 'IS': 'ISK', 115 | 'IT': 'EUR', 116 | 'JE': 'GBP', 117 | 'JM': 'JMD', 118 | 'JO': 'JOD', 119 | 'JP': 'JPY', 120 | 'KE': 'KES', 121 | 'KG': 'KGS', 122 | 'KH': 'KHR', 123 | 'KI': 'AUD', 124 | 'KM': 'KMF', 125 | 'KN': 'XCD', 126 | 'KP': 'KPW', 127 | 'KR': 'KRW', 128 | 'KW': 'KWD', 129 | 'KY': 'KYD', 130 | 'KZ': 'KZT', 131 | 'LA': 'LAK', 132 | 'LB': 'LBP', 133 | 'LC': 'XCD', 134 | 'LI': 'CHF', 135 | 'LK': 'LKR', 136 | 'LR': 'LRD', 137 | 'LS': 'LSL', 138 | 'LT': 'EUR', 139 | 'LU': 'EUR', 140 | 'LV': 'EUR', 141 | 'LY': 'LYD', 142 | 'MA': 'MAD', 143 | 'MC': 'EUR', 144 | 'MD': 'MDL', 145 | 'ME': 'EUR', 146 | 'MF': 'EUR', 147 | 'MG': 'MGA', 148 | 'MH': 'USD', 149 | 'MK': 'MKD', 150 | 'ML': 'XOF', 151 | 'MM': 'MMK', 152 | 'MN': 'MNT', 153 | 'MO': 'MOP', 154 | 'MP': 'USD', 155 | 'MQ': 'EUR', 156 | 'MR': 'MRO', 157 | 'MS': 'XCD', 158 | 'MT': 'EUR', 159 | 'MU': 'MUR', 160 | 'MV': 'MVR', 161 | 'MW': 'MWK', 162 | 'MX': 'MXN', 163 | 'MY': 'MYR', 164 | 'MZ': 'MZN', 165 | 'NA': 'NAD', 166 | 'NC': 'XPF', 167 | 'NE': 'XOF', 168 | 'NF': 'AUD', 169 | 'NG': 'NGN', 170 | 'NI': 'NIO', 171 | 'NL': 'EUR', 172 | 'NO': 'NOK', 173 | 'NP': 'NPR', 174 | 'NR': 'AUD', 175 | 'NU': 'NZD', 176 | 'NZ': 'NZD', 177 | 'OM': 'OMR', 178 | 'PA': 'PAB', 179 | 'PE': 'PEN', 180 | 'PF': 'XPF', 181 | 'PG': 'PGK', 182 | 'PH': 'PHP', 183 | 'PK': 'PKR', 184 | 'PL': 'PLN', 185 | 'PM': 'EUR', 186 | 'PN': 'NZD', 187 | 'PR': 'USD', 188 | 'PS': 'JOD', 189 | 'PT': 'EUR', 190 | 'PW': 'USD', 191 | 'PY': 'PYG', 192 | 'QA': 'QAR', 193 | 'RE': 'EUR', 194 | 'RO': 'RON', 195 | 'RS': 'RSD', 196 | 'RU': 'RUB', 197 | 'RW': 'RWF', 198 | 'SA': 'SAR', 199 | 'SB': 'SBD', 200 | 'SC': 'SCR', 201 | 'SD': 'SDG', 202 | 'SE': 'SEK', 203 | 'SG': 'SGD', 204 | 'SH': 'SHP', 205 | 'SI': 'EUR', 206 | 'SJ': 'NOK', 207 | 'SK': 'EUR', 208 | 'SL': 'SLL', 209 | 'SM': 'EUR', 210 | 'SN': 'XOF', 211 | 'SO': 'SOS', 212 | 'SR': 'SRD', 213 | 'SS': 'SSP', 214 | 'ST': 'STD', 215 | 'SU': 'RUB', 216 | 'SV': 'USD', 217 | 'SX': 'ANG', 218 | 'SY': 'SYP', 219 | 'SZ': 'SZL', 220 | 'TA': 'GBP', 221 | 'TC': 'USD', 222 | 'TD': 'XAF', 223 | 'TF': 'EUR', 224 | 'TG': 'XOF', 225 | 'TH': 'THB', 226 | 'TJ': 'TJS', 227 | 'TK': 'NZD', 228 | 'TL': 'USD', 229 | 'TM': 'TMT', 230 | 'TN': 'TND', 231 | 'TO': 'TOP', 232 | 'TR': 'TRY', 233 | 'TT': 'TTD', 234 | 'TV': 'AUD', 235 | 'TW': 'TWD', 236 | 'TZ': 'TZS', 237 | 'UA': 'UAH', 238 | 'UG': 'UGX', 239 | 'UK': 'GBP', 240 | 'UM': 'USD', 241 | 'US': 'USD', 242 | 'UY': 'UYU', 243 | 'UZ': 'UZS', 244 | 'VA': 'EUR', 245 | 'VC': 'XCD', 246 | 'VE': 'VEF', 247 | 'VG': 'USD', 248 | 'VI': 'USD', 249 | 'VN': 'VND', 250 | 'VU': 'VUV', 251 | 'WF': 'XPF', 252 | 'WS': 'WST', 253 | 'XK': 'EUR', 254 | 'YE': 'YER', 255 | 'YT': 'EUR', 256 | 'ZA': 'ZAR', 257 | 'ZM': 'ZMW', 258 | 'ZW': 'USD' 259 | }; 260 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2015 Zalando SE 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | */ 22 | 23 | 'use strict'; 24 | 25 | const countryCurrencyMap = require('./data/country-currency'); 26 | const currencySymbolMap = require('./data/symbol-map'); 27 | const localeSeparatorsMap = require('./data/separators'); 28 | const localePositionersMap = require('./data/positions'); 29 | 30 | var LOCALE_MATCHER = /^\s*([a-zA-Z]{2,4})(?:[-_][a-zA-Z]{4})?(?:[-_]([a-zA-Z]{2}|\d{3}))?\s*(?:$|[-_])/; 31 | var LOCALE_LANGUAGE = 1; 32 | var LOCALE_REGION = 2; 33 | 34 | function error(message) { 35 | throw new Error(message); 36 | } 37 | 38 | /** 39 | * @param {Object} map 40 | * @param {string} locale 41 | * @param {Array} localeParts 42 | * @throws {Error} 43 | * @returns {string | function} 44 | */ 45 | function findWithFallback(map, locale, localeParts) { 46 | const result = map[locale] || 47 | map[localeParts[LOCALE_LANGUAGE] + '-' + localeParts[LOCALE_REGION]] || 48 | map[localeParts[LOCALE_LANGUAGE]]; 49 | if (!result) { 50 | error('Could not find info for locale "' + locale + '"'); 51 | } 52 | 53 | return result; 54 | } 55 | 56 | /** 57 | * @param {string} region 58 | * @returns {string} 59 | */ 60 | function getCurrencyFromRegion(region) { 61 | const currencyCode = countryCurrencyMap[region]; 62 | if (!currencyCode) { 63 | error('Could not find default currency for locale region "' + region + '". Please provide explicit currency.'); 64 | } 65 | return currencyCode; 66 | } 67 | 68 | /** 69 | * @typedef {{ 70 | * showDecimalIfWhole: boolean, 71 | * subunitsPerUnit: number, 72 | * centsZeroFill: number, 73 | * effectiveLocale: string, 74 | * currencyCode: string, 75 | * currencySymbol: string, 76 | * currencyFormatter: Function, 77 | * thousandSeparator: string, 78 | * decimalSeparator: string 79 | * }} BanknoteFormatting 80 | */ 81 | 82 | /** 83 | * This function tries hard to figure out full set formatting options necessary to format money. 84 | * If the locale is valid and contains are territory that is also a valid ISO3166-1-Alpha-2 country 85 | * code (e.g. en-US), then the default currency for that country is taken. Otherwise you have to 86 | * provide an explicit currency code. 87 | * @throws Error thrown if the lookup of formatting rules has failed. 88 | * @param {string} locale a BCP47 locale string 89 | * @param {string=} currencyCode explicit currency code for for the currency symbol lookup 90 | * @returns {BanknoteFormatting} 91 | */ 92 | exports.formattingForLocale = function (locale, currencyCode) { 93 | const localeParts = locale.match(LOCALE_MATCHER); 94 | 95 | if (!localeParts) { 96 | error('Locale provided does not conform to BCP47.'); 97 | } 98 | 99 | currencyCode = currencyCode || getCurrencyFromRegion(localeParts[LOCALE_REGION]); 100 | const separators = findWithFallback(localeSeparatorsMap, locale, localeParts); 101 | 102 | const subunitsPerUnit = 100; // TODO change 100 with real information 103 | return { 104 | showDecimalIfWhole: true, 105 | subunitsPerUnit, 106 | centsZeroFill: String(subunitsPerUnit).length - 1, 107 | currencyCode: currencyCode, 108 | currencySymbol: currencySymbolMap[currencyCode] || currencyCode, 109 | currencyFormatter: findWithFallback(localePositionersMap, locale, localeParts), 110 | decimalSeparator: separators.charAt(0), 111 | thousandSeparator: separators.charAt(1) 112 | }; 113 | }; 114 | 115 | /** 116 | * Returns a currency code for the given country or `undefined` if nothing found. 117 | * @param {string} twoCharacterCountryCode 118 | * @returns {string|undefined} 119 | */ 120 | exports.currencyForCountry = function (twoCharacterCountryCode) { 121 | return countryCurrencyMap[twoCharacterCountryCode]; 122 | }; 123 | 124 | /** 125 | * This function accepts an amount in subunits (which are called "cents" in currencies like EUR or USD), 126 | * and also a formatting options object, that can be either constructed manually or created from locale 127 | * using `banknote.formattingForLocale()` method. This function doesn't provide any defaults for formatting. 128 | * @param {Number} subunitAmount 129 | * @param {BanknoteFormatting} formatting 130 | * @returns {string} 131 | */ 132 | exports.formatSubunitAmount = function (subunitAmount, formatting) { 133 | const minus = subunitAmount < 0 ? '-' : ''; 134 | const absAmount = Math.abs(subunitAmount); 135 | const mainPart = absAmount / formatting.subunitsPerUnit | 0; // | 0 cuts of the decimal part 136 | let decimalPart = '' + (absAmount % formatting.subunitsPerUnit | 0); 137 | let formattedAmount; 138 | if (formatting.thousandSeparator) { 139 | formattedAmount = addThousandSeparator(mainPart, formatting.thousandSeparator); 140 | } else { 141 | formattedAmount = '' + mainPart; 142 | } 143 | 144 | if (!(!formatting.showDecimalIfWhole && decimalPart === '0')) { 145 | formattedAmount += formatting.decimalSeparator + padLeft(decimalPart, formatting.centsZeroFill);; 146 | } 147 | 148 | return formatting.currencyFormatter(formatting.currencySymbol, formattedAmount, minus); 149 | }; 150 | 151 | function addThousandSeparator(number, separator) { 152 | let mainPart = '' + (number % 1000); 153 | number = (number / 1000) | 0; 154 | if (number > 0) { 155 | mainPart = padLeft(mainPart, 3); 156 | } 157 | while (number > 0) { 158 | let subPart = '' + (number % 1000); 159 | number = (number / 1000) | 0; 160 | if (number > 0) { 161 | subPart = padLeft(subPart, 3); 162 | } 163 | mainPart = subPart + separator + mainPart; 164 | } 165 | return mainPart; 166 | } 167 | 168 | function padLeft(subPart, length) { 169 | while (subPart.length < length) { 170 | subPart = '0' + subPart; 171 | } 172 | return subPart; 173 | } 174 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | Copyright (c) 2015 Zalando SE 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | */ 22 | 23 | 'use strict'; 24 | 25 | const assert = require('assert'); 26 | const banknote = require('../'); 27 | 28 | const ALL_KNOWN_LOCALES = Object.keys(require('../data/separators')); 29 | const COUNTRY_CURRENCY_MAP = require('../data/country-currency'); 30 | const EXPECTED_US_OPTIONS = { 31 | decimalSeparator: '.', 32 | thousandSeparator: ',', 33 | currencyCode: 'USD', 34 | currencySymbol: '$', 35 | subunitsPerUnit: 100, 36 | centsZeroFill: 2, 37 | showDecimalIfWhole: true 38 | }; 39 | 40 | describe('banknote', function () { 41 | 42 | describe('formattingOptionsForLocale', function () { 43 | 44 | it('should throw for missing locale', function () { 45 | assert.throws(function () { 46 | banknote.formattingForLocale(); 47 | }); 48 | }); 49 | 50 | it('should throw for unknown locale', function () { 51 | assert.throws(function () { 52 | banknote.formattingForLocale('i-klingon'); 53 | }); 54 | }); 55 | 56 | it('should throw for a locale without a region', function () { 57 | assert.throws(function () { 58 | banknote.formattingForLocale('en'); 59 | }); 60 | }); 61 | 62 | it('should give a full options given a valid locale with a territory', function () { 63 | const options = banknote.formattingForLocale('en-US'); 64 | assert.equal(typeof options.currencyFormatter, 'function'); 65 | delete options.currencyFormatter; 66 | assert.deepEqual(options, EXPECTED_US_OPTIONS); 67 | }); 68 | 69 | it('should work for all known locales given an explicit currency', function () { 70 | ALL_KNOWN_LOCALES.forEach(function (locale) { 71 | assert(typeof banknote.formattingForLocale(locale, 'USD'), 'object'); 72 | }); 73 | }); 74 | 75 | it('should work for all known locales where region is a valid country code', function () { 76 | ALL_KNOWN_LOCALES.forEach(function (locale) { 77 | var region = locale.split('-')[1]; 78 | if (region && region.length === 2 && (region in COUNTRY_CURRENCY_MAP)) { 79 | assert(typeof banknote.formattingForLocale(locale), 'object'); 80 | } 81 | }); 82 | }); 83 | 84 | }); 85 | 86 | describe('currencyForCountry', function () { 87 | it('should allow to find a currency code from the country code', function () { 88 | assert.equal(banknote.currencyForCountry('US'), 'USD'); 89 | assert.equal(banknote.currencyForCountry('DE'), 'EUR'); 90 | }); 91 | }); 92 | 93 | describe('formatSubunitAmount', function () { 94 | 95 | it('should work for 0 values', function () { 96 | const options = banknote.formattingForLocale('en-US'); 97 | assert.equal(banknote.formatSubunitAmount(0, options), '$0.00'); 98 | }); 99 | 100 | it('should work for "en-US" locale', function () { 101 | const options = banknote.formattingForLocale('en-US'); 102 | assert.equal(banknote.formatSubunitAmount(123456, options), '$1,234.56'); 103 | }); 104 | 105 | it('should work for "de-AT" locale', function () { 106 | const options = banknote.formattingForLocale('de-AT'); 107 | assert.equal(banknote.formatSubunitAmount(-123456, options), '-€ 1 234,56'); 108 | }); 109 | 110 | it('should work for "de-CH" locale', function () { 111 | const options = banknote.formattingForLocale('de-CH'); 112 | assert.equal(banknote.formatSubunitAmount(-123456, options), 'CHF-1’234.56'); 113 | }); 114 | 115 | 116 | it('should work for "fr-CH" locale', function () { 117 | const options = banknote.formattingForLocale('fr-CH'); 118 | assert.equal(banknote.formatSubunitAmount(-123456, options), 'CHF-1’234.56'); 119 | }); 120 | 121 | it('should work for "en-US" locale with "EUR" currency', function () { 122 | const options = banknote.formattingForLocale('en-US', 'EUR'); 123 | assert.equal(banknote.formatSubunitAmount(123456, options), '€1,234.56'); 124 | }); 125 | 126 | it('should work for "de" locale with "EUR" currency', function () { 127 | const options = banknote.formattingForLocale('de', 'EUR'); 128 | assert.equal(banknote.formatSubunitAmount(123456, options), '1.234,56 €'); 129 | }); 130 | 131 | it('should work for "no" locale with "NOK" currency', function () { 132 | const options = banknote.formattingForLocale('no-NO', 'NOK'); 133 | assert.equal(banknote.formatSubunitAmount(123456, options), '1 234,56 kr'); 134 | }); 135 | 136 | it('should add the thousand separator', function () { 137 | const options = banknote.formattingForLocale('no-NO', 'NOK'); 138 | assert.equal(banknote.formatSubunitAmount(123456789, options), '1 234 567,89 kr'); 139 | assert.equal(banknote.formatSubunitAmount(103456789, options), '1 034 567,89 kr'); 140 | assert.equal(banknote.formatSubunitAmount(103406789, options), '1 034 067,89 kr'); 141 | assert.equal(banknote.formatSubunitAmount(100400089, options), '1 004 000,89 kr'); 142 | }); 143 | 144 | it('should correctly fill up the cents amount', function () { 145 | const options = banknote.formattingForLocale('de-DE'); 146 | assert.equal(banknote.formatSubunitAmount(123406, options), '1.234,06 €'); 147 | assert.equal(banknote.formatSubunitAmount(123400, options), '1.234,00 €'); 148 | }); 149 | 150 | it('should correctly hide decimal when appropriate options is specified', function () { 151 | const options = banknote.formattingForLocale('de-DE'); 152 | options.showDecimalIfWhole = false; 153 | assert.equal(banknote.formatSubunitAmount(123400, options), '1.234 €'); 154 | }); 155 | 156 | }); 157 | }); 158 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Banknote 2 | [![NPM](https://nodei.co/npm/banknote.png)](https://npmjs.org/package/banknote) 3 | 4 | Banknote is a small, easy-to-use JavaScript library that provides a simple way to format monetary amounts in multiple locales and currencies. It’s mainly targeted at Node.js, but also works in the browser (if needed) with module bundlers like [Webpack](https://webpack.github.io/) and [Browserify](http://browserify.org/). 5 | 6 | 7 | ![build](https://github.com/zalando-incubator/banknote/actions/workflows/build.yml/badge.svg) 8 | [![downloads](https://img.shields.io/npm/dt/banknote.svg)](https://npmjs.org/package/banknote?cacheSeconds=3600) 9 | [![version](https://img.shields.io/npm/v/banknote.svg)](https://npmjs.org/package/banknote?cacheSeconds=3600) 10 | 11 | ## Features 12 | 13 | Banknote addresses a common problem faced by anyone (for example, an e-commerce company) who has to update and format prices on the frontend. It is different from similar projects in that it follows Unicode CLDR formatting standards, not an ad hoc data solution. It also: 14 | - is customizable — you can use emoticons, preferred symbols, etc. 15 | - allows you to override its default settings — for example, if you want to apply US formatting to amounts in Chinese yen 16 | 17 | If you want to do more than just format monetary amounts, and you are open to changing your build process or accepting a ~300MB node module, then we recommend using jQuery Foundation’s [globalize](https://github.com/jquery/globalize). It uses the same data as Banknote, but includes access to all of the [Unicode CLDR](http://cldr.unicode.org/). 18 | 19 | ## How to Use 20 | 21 | ### Requirements 22 | 23 | - npm (either backend or frontend) 24 | 25 | ### Examples 26 | 27 | #### Single Locale 28 | 29 | If your app only uses one locale, then the code is very straightforward: 30 | 31 | ```js 32 | var banknote = require('banknote'); 33 | var formattingOptions = banknote.formattingForLocale('en-US'); 34 | 35 | console.log(banknote.formatSubunitAmount(123456, formattingOptions)); 36 | // "$1,234.56" 37 | ``` 38 | 39 | #### Explicit Currency 40 | 41 | For some applications, you’ll need a way to specify different currencies without changing number-formatting rules. Here’s an example of the “en” number-formatting rules, but with Euro: 42 | 43 | ```js 44 | var banknote = require('banknote'); 45 | var formattingOptions = banknote.formattingForLocale('en-US', 'EUR'); 46 | 47 | console.log(banknote.formatSubunitAmount(123456, formattingOptions)); 48 | // "€1,234.56" 49 | ``` 50 | 51 | #### Dynamic Locales 52 | 53 | Quite often, you have to change a locale based on an incoming request or some other input. We recommended using 54 | the [memoization](https://en.wikipedia.org/wiki/Memoization) function together with a fallback logic. For example: 55 | 56 | ```js 57 | var banknote = require('banknote'); 58 | var memoize = require('memoizee'); 59 | var memoizedFormattingOptions = memoize(banknote.formattingForLocale); 60 | var defaultFormattingOptions = banknote.formattingForLocale('en-US'); 61 | 62 | // Express.js init here ... 63 | 64 | app.get('/', function(req, res){ 65 | var locale = parseAcceptLanguageForMainLocale(req.headers['accept-language']); 66 | var formattingOptions; 67 | try { 68 | formattingOptions = memoizedFormattingOptions(locale); 69 | } catch (e) { 70 | formattingOptions = defaultFormattingOptions; 71 | } 72 | res.write(banknote.formatSubunitAmount(123456, formattingOptions)); 73 | // "€1,234.56" 74 | }); 75 | 76 | ``` 77 | 78 | #### Customizing Default Formatters 79 | 80 | With `banknote`, you can also customize any of the formatting options yourself. For example: 81 | 82 | ```js 83 | var banknote = require('banknote'); 84 | var formattingOptions = banknote.formattingForLocale('en-US'); 85 | 86 | // disable "cents" 87 | formattingOptions.showDecimalIfWhole = false; 88 | 89 | // remove the thousand separator 90 | formattingOptions.thousandSeparator = ''; 91 | 92 | formattingOptions.currencyFormatter = function (symbol, formattedNumber, minus) { 93 | return minus + symbol + ' ' + formattedNumber; 94 | }; 95 | 96 | console.log(banknote.formatSubunitAmount(123400, formattingOptions)); 97 | // "$1234" 98 | ``` 99 | 100 | We’ve noticed that many libraries lack a `currencyFormatter` function to place things in their correct positions — providing separate options instead. Unfortunately, separate options won’t satisfy all possible locale settings. For example, `de-CH` requires placing `-` after the currency symbol, which is usually unsupported or results in an explosion of parameters. 101 | 102 | ## Formatting Options 103 | 104 | Here’s a list of all the properties in formatting options obtained from the `formattingForLocale` call: 105 | 106 | ```js 107 | var formattingOptions = { 108 | /** 109 | * Controls whether the subunit (decimal) part is shown when 110 | * the value is exact, e.g. exactly $8 and 0¢ 111 | * @type boolean 112 | */ 113 | showDecimalIfWhole: true, 114 | 115 | /** 116 | * 117 | * @type number 118 | */ 119 | subunitsPerUnit: 100, 120 | 121 | /** 122 | * Effective locale means the exact locale that will be used 123 | * when formatting numbers. It doesn't necessarily match the 124 | * locale passed to `formattingForLocale()` call because 125 | * of the fallback logic. 126 | * @type string 127 | */ 128 | effectiveLocale: 'en', 129 | 130 | /** 131 | * Currency code is not used directly and is provided here 132 | * for reference or custom logic for the clients of the lib 133 | * @type string 134 | */ 135 | currencyCode: 'USD', 136 | 137 | /** 138 | * Symbol passed in to `currencyFormatter` function. 139 | * @type string 140 | */ 141 | currencySymbol: '$', 142 | 143 | /** 144 | * Separator used to format thousands. 145 | * @type string 146 | */ 147 | thousandSeparator: ',', 148 | 149 | /** 150 | * Separator between whole and decimal part of the amount. 151 | * @type string 152 | */ 153 | decimalSeparator: '.', 154 | 155 | /** 156 | * Function that correctly positions symbol, formattedAmount 157 | * and a minus relative to each other. 158 | * @param {string} symbol 159 | * @param {string} formattedAmount 160 | * @param {string} minus 161 | * @returns {string} 162 | */ 163 | currencyFormatter: function (symbol, formattedAmount, minus) { 164 | return minus + formattedAmount + symbol; 165 | } 166 | }; 167 | ``` 168 | 169 | ### Contributions 170 | 171 | We welcome contributions to this project. Please keep in mind that we want to avoid feature overload, so if you’d like to help out please consider working on the following: 172 | - performance/speed 173 | - automating updates of new Unicode releases, or manually submitting pull requests with new Unicode 174 | - adding a continuous integration build 175 | 176 | ## License 177 | 178 | Copyright (c) 2015-2017 Zalando SE 179 | 180 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 181 | 182 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 183 | 184 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 185 | --------------------------------------------------------------------------------