Merge pull request #575 from Chalarangelo/add-toCurrency

Add toCurrency
This commit is contained in:
Angelos Chalaris
2018-01-31 19:14:15 +02:00
committed by GitHub
6 changed files with 43 additions and 26 deletions

17
snippets/toCurrency.md Normal file
View File

@ -0,0 +1,17 @@
### toCurrency
Take a number and return specified currency formatting.
Use `Intl.NumberFormat` to enable country / currency sensitive formatting.
```js
const toCurrency = (n, curr, LanguageFormat = undefined) => Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n);
```
```js
toCurrency(123456.789, 'EUR') // €123,456.79 | currency: Euro | currencyLangFormat: Local
toCurrency(123456.789, 'USD', 'en-us') // €123,456.79 | currency: US Dollar | currencyLangFormat: English (United States)
toCurrency(123456.789, 'USD', 'fa') // ۱۲۳٬۴۵۶٫۷۹ ؜$ | currency: US Dollar | currencyLangFormat: Farsi
toCurrency(322342436423.2435, 'JPY') // ¥322,342,436,423 | currency: Japanese Yen | currencyLangFormat: Local
toCurrency(322342436423.2435, 'JPY', 'fi') // 322 342 436 423 ¥ | currency: Japanese Yen | currencyLangFormat: Finnish
```

View File

@ -236,6 +236,7 @@ throttle:function
times:function times:function
timeTaken:utility timeTaken:utility
toCamelCase:string,regexp toCamelCase:string,regexp
toCurrency:utility
toDecimalMark:utility,math toDecimalMark:utility,math
toggleClass:browser toggleClass:browser
toKebabCase:string,regexp toKebabCase:string,regexp

View File

@ -1,19 +1,10 @@
const test = require('tape'); const test = require('tape');
//const arrayToHtmlList = require('./arrayToHtmlList.js'); const arrayToHtmlList = require('./arrayToHtmlList.js');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const listID = 'myListID';
const dom = new JSDOM(`<ul id="${listID}"></ul>`);
// Override snippet to use jsdom
const arrayToHtmlList = (arr, listID) =>
arr.map(item => (dom.window.document.querySelector('#' + listID).innerHTML += `<li>${item}</li>`));
test('Testing arrayToHtmlList', (t) => { test('Testing arrayToHtmlList', (t) => {
//For more information on all the methods supported by tape //For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape //Please go to https://github.com/substack/tape
t.true(typeof arrayToHtmlList === 'function', 'arrayToHtmlList is a Function'); t.true(typeof arrayToHtmlList === 'function', 'arrayToHtmlList is a Function');
arrayToHtmlList(['item 1', 'item 2'], 'myListID');
t.equals(dom.window.document.querySelector(`ul#${listID}`).innerHTML, '<li>item 1</li><li>item 2</li>', 'Generates and fills a list element');
//t.deepEqual(arrayToHtmlList(args..), 'Expected'); //t.deepEqual(arrayToHtmlList(args..), 'Expected');
//t.equal(arrayToHtmlList(args..), 'Expected'); //t.equal(arrayToHtmlList(args..), 'Expected');
//t.false(arrayToHtmlList(args..), 'Expected'); //t.false(arrayToHtmlList(args..), 'Expected');

View File

@ -1,17 +1,10 @@
const test = require('tape'); const test = require('tape');
const hasClass = require('./hasClass.js'); const hasClass = require('./hasClass.js');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
test('Testing hasClass', (t) => { test('Testing hasClass', (t) => {
//For more information on all the methods supported by tape //For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape //Please go to https://github.com/substack/tape
t.true(typeof hasClass === 'function', 'hasClass is a Function'); t.true(typeof hasClass === 'function', 'hasClass is a Function');
const className = 'container'
const dom = new JSDOM(`
<p class="${className}">Some text</p>
`);
t.true(hasClass(dom.window.document.querySelector('p'), className), 'element has the specified class');
//t.deepEqual(hasClass(args..), 'Expected'); //t.deepEqual(hasClass(args..), 'Expected');
//t.equal(hasClass(args..), 'Expected'); //t.equal(hasClass(args..), 'Expected');
//t.false(hasClass(args..), 'Expected'); //t.false(hasClass(args..), 'Expected');

View File

@ -0,0 +1,2 @@
const toCurrency = (n, curr, LanguageFormat = undefined) => new Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n);
module.exports = toCurrency

View File

@ -0,0 +1,13 @@
const test = require('tape');
const toCurrency = require('./toCurrency.js');
test('Testing toCurrency', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof toCurrency === 'function', 'toCurrency is a Function');
//t.deepEqual(toCurrency(args..), 'Expected');
//t.equal(toCurrency(args..), 'Expected');
//t.false(toCurrency(args..), 'Expected');
//t.throws(toCurrency(args..), 'Expected');
t.end();
});