-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e47ded
commit 6302705
Showing
10 changed files
with
316 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
module.exports = { | ||
"src/**/*.js": ["yarn lint"], | ||
"packages/core/src/**/*.js": ["cd packages/core && yarn lint"], | ||
"packages/dinero/src/**/*.js": ["cd packages/dinero && yarn lint"], | ||
"packages/locale/src/**/*.js": ["cd packages/locale && yarn lint"], | ||
"packages/format-dinero/src/**/*.js": ["cd packages/format-dinero && yarn lint"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2018 Sarah Dayan | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
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 NONINFRINGEMENT. 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import withFormatDinero from './src/format-dinero.js'; | ||
|
||
export default withFormatDinero; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@soldi/format-dinero", | ||
"version": "0.1.0", | ||
"description": "A formating layer for soldi that is compatible with Dinero v1 toFormat", | ||
"main": "index.js", | ||
"repository": "http://github.com/nick.palmer/soldi.js", | ||
"author": "Nick Palmer <nick@nick.codes>", | ||
"license": "MIT", | ||
"type": "module", | ||
"scripts": { | ||
"lint": "cd ../../ && eslint packages/format-dinero/src packages/format-dinero/test", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules jest ." | ||
}, | ||
"dependencies": { | ||
"@soldi/core": "0.1.0", | ||
"@soldi/dinero": "0.1.0" | ||
}, | ||
"devDependencies": { | ||
"jest": "27.0.6", | ||
"jsverify": "0.8.4" | ||
}, | ||
"jest": { | ||
"verbose": true, | ||
"transform": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { assert } from '@soldi/core/index.mjs'; | ||
import Format from './format.js'; | ||
|
||
/** | ||
This extension adds a toFormat options | ||
which is copied from Dinero v1's toFormat(). | ||
It expects your base instance to have a getLocale() | ||
function on it | ||
The code is almost entirely copied from Dinero v1 and | ||
is thus subject to the license from that library | ||
located in the root of this extension in the LICENSE.dinero.md | ||
file. | ||
**/ | ||
const withFormatDinero = (base) => { | ||
assert.validBase('withFormatDinero', base); | ||
assert.hasMethod(base({ currency: 'USD' }), 'getLocale'); | ||
|
||
const FormatDinero = base.extend('DineroFormat', { | ||
globals: { | ||
globalFormat: '$0,0.00', | ||
globalFormatRoundingMode: base.ROUND.HALF_AWAY_FROM_ZERO, | ||
}, | ||
/** | ||
* Returns this object formatted as a string. | ||
* | ||
* The format is a mask which defines how the output string will be formatted. | ||
* It defines whether to display a currency, in what format, how many fraction digits to display and whether to use grouping separators. | ||
* The output is formatted according to the applying locale. | ||
* | ||
* Object | Format | String | ||
* :--------------------------- | :---------------- | :--- | ||
* `Dinero({ amount: 500050 })` | `'$0,0.00'` | $5,000.50 | ||
* `Dinero({ amount: 500050 })` | `'$0,0'` | $5,001 | ||
* `Dinero({ amount: 500050 })` | `'$0'` | $5001 | ||
* `Dinero({ amount: 500050 })` | `'$0.0'` | $5000.5 | ||
* `Dinero({ amount: 500050 })` | `'USD0,0.0'` | USD5,000.5 | ||
* `Dinero({ amount: 500050 })` | `'0,0.0 dollar'` | 5,000.5 dollars | ||
* | ||
* Don't try to substitute the `$` sign or the `USD` code with your target currency, nor adapt the format string to the exact format you want. | ||
* The format is a mask which defines a pattern and returns a valid, localized currency string. | ||
* If you want to display the object in a custom way, either use {@link module:Dinero~getAmount getAmount}, {@link module:Dinero~toUnit toUnit} or {@link module:Dinero~toRoundedUnit toRoundedUnit} and manipulate the output string as you wish. | ||
* | ||
* {@link module:Dinero~toFormat toFormat} wraps around `Number.prototype.toLocaleString`. For that reason, **format will vary depending on how it's implemented in the end user's environment**. | ||
* | ||
* You can also use `toLocaleString` directly: | ||
* `Dinero().toRoundedUnit(digits, roundingMode).toLocaleString(locale, options)`. | ||
* | ||
* By default, amounts are rounded using the **half away from zero** rule ([commercial rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero)). | ||
* You can also specify a different `roundingMode` to better fit your needs. | ||
* | ||
* @param {String} [format='$0,0.00'] - The format mask to format to. | ||
* @param {String} [roundingMode='HALF_AWAY_FROM_ZERO'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`. | ||
* | ||
* @example | ||
* // returns $2,000 | ||
* Dinero({ amount: 200000 }).toFormat('$0,0') | ||
* @example | ||
* // returns €50.5 | ||
* Dinero({ amount: 5050, currency: 'EUR' }).toFormat('$0,0.0') | ||
* @example | ||
* // returns 100 euros | ||
* Dinero({ amount: 10000, currency: 'EUR' }).setLocale('fr-FR').toFormat('0,0 dollar') | ||
* @example | ||
* // returns 2000 | ||
* Dinero({ amount: 200000, currency: 'EUR' }).toFormat() | ||
* @example | ||
* // returns $10 | ||
* Dinero({ amount: 1050 }).toFormat('$0', 'HALF_EVEN') | ||
* | ||
* @return {String} | ||
*/ | ||
toFormat(format = FormatDinero.globalFormat, roundingMode = FormatDinero.globalFormatRoundingMode) { | ||
const formatter = Format(format); | ||
|
||
return this.toRoundedUnit( | ||
formatter.getMinimumFractionDigits(), | ||
roundingMode | ||
).toLocaleString(this.getLocale(), { | ||
currencyDisplay: formatter.getCurrencyDisplay(), | ||
useGrouping: formatter.getUseGrouping(), | ||
minimumFractionDigits: formatter.getMinimumFractionDigits(), | ||
style: formatter.getStyle(), | ||
currency: this.getCurrency() | ||
}); | ||
}, | ||
}); | ||
|
||
return FormatDinero; | ||
}; | ||
|
||
export default withFormatDinero; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { _isDefined } from '@soldi/core/index.mjs'; | ||
|
||
const isUndefined = (arg) => !_isDefined(arg); | ||
|
||
export default function Format(format) { | ||
const matches = /^(?:(\$|USD)?0(?:(,)0)?(\.)?(0+)?|0(?:(,)0)?(\.)?(0+)?\s?(dollar)?)$/gm.exec( | ||
format | ||
); | ||
|
||
return { | ||
/** | ||
* Returns the matches. | ||
* @ignore | ||
* | ||
* @return {Array} | ||
*/ | ||
getMatches() { | ||
return matches !== null | ||
? matches.slice(1).filter(match => !isUndefined(match)) | ||
: []; | ||
}, | ||
/** | ||
* Returns the amount of fraction digits to display. | ||
* @ignore | ||
* | ||
* @return {Number} | ||
*/ | ||
getMinimumFractionDigits() { | ||
const decimalPosition = match => match === '.'; | ||
const matches = this.getMatches(); | ||
return !isUndefined(matches.find(decimalPosition)) | ||
? matches[ | ||
matches.findIndex(decimalPosition) + 1 | ||
].split('').length | ||
: 0; | ||
}, | ||
/** | ||
* Returns the currency display mode. | ||
* @ignore | ||
* | ||
* @return {String} | ||
*/ | ||
getCurrencyDisplay() { | ||
const modes = { | ||
USD: 'code', | ||
dollar: 'name', | ||
$: 'symbol' | ||
}; | ||
return modes[ | ||
this.getMatches().find(match => { | ||
return match === 'USD' || match === 'dollar' || match === '$'; | ||
}) | ||
]; | ||
}, | ||
/** | ||
* Returns the formatting style. | ||
* @ignore | ||
* | ||
* @return {String} | ||
*/ | ||
getStyle() { | ||
return !isUndefined(this.getCurrencyDisplay(this.getMatches())) | ||
? 'currency' | ||
: 'decimal'; | ||
}, | ||
/** | ||
* Returns whether grouping should be used or not. | ||
* @ignore | ||
* | ||
* @return {Boolean} | ||
*/ | ||
getUseGrouping() { | ||
return !isUndefined(this.getMatches().find(match => match === ',')); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import withFormatDinero from '../index.mjs'; | ||
import withLocale from '@soldi/locale/index.mjs'; | ||
import Soldi from '@soldi/core/index.mjs'; | ||
|
||
const currency = 'USD'; | ||
|
||
const FormatDinero = withFormatDinero(withLocale(Soldi)); | ||
|
||
describe('withFormatDinero', () => { | ||
it('should puke with no base', () => { | ||
expect(() => withFormatDinero()).toThrow('withFormatDinero base cannot be undefined'); | ||
}); | ||
it('should throw if you cannot build with base', () => { | ||
expect(() => withFormatDinero(function() { })).toThrow('base return value cannot be undefined'); | ||
}); | ||
it('should throw if base does not have getLocale', () => { | ||
expect(() => withFormatDinero(Soldi)).toThrow('instance is missing method getLocale'); | ||
}); | ||
it('should make instances with a toFormat', () => { | ||
const instance = FormatDinero({ currency }); | ||
expect(instance).toBeTruthy(); | ||
// eslint-disable-next-line no-prototype-builtins | ||
expect(Object.getPrototypeOf(instance).hasOwnProperty('toFormat')).toBeTruthy(); | ||
}); | ||
it('should format using the default global format', () => { | ||
const instance = FormatDinero({ currency, amount: 100 }); | ||
expect(instance.toFormat()).toEqual('$1.00'); | ||
}); | ||
it('should format using the format argument', () => { | ||
const instance = FormatDinero({ currency, amount: 200 }); | ||
expect(instance.toFormat('$0')).toEqual('$2'); | ||
}); | ||
it('should respect format rounding mode', () => { | ||
const instance = FormatDinero({ currency, amount: 250 }); | ||
expect(instance.toFormat('$0', FormatDinero.ROUND.HALF_DOWN)).toEqual('$2'); | ||
expect(instance.toFormat('$0', FormatDinero.ROUND.HALF_UP)).toEqual('$3'); | ||
}); | ||
it('should respect global format rounding mode', () => { | ||
FormatDinero.globalFormatRoundingMode = FormatDinero.ROUND.HALF_DOWN; | ||
const instance = FormatDinero({ currency, amount: 250 }); | ||
expect(instance.toFormat('$0')).toEqual('$2'); | ||
FormatDinero.globalFormatRoundingMode = FormatDinero.ROUND.HALF_UP; | ||
expect(instance.toFormat('$0')).toEqual('$3'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Format from '../src/format.js'; | ||
|
||
describe('Format', () => { | ||
describe('#getMatches', () => { | ||
test('should return all matches as an array when there is a match', () => { | ||
expect(Format('0,0').getMatches()).toEqual([',']); | ||
}); | ||
test('should return an empty array when there is no match', () => { | ||
expect(Format('abc').getMatches()).toHaveLength(0); | ||
}); | ||
}); | ||
describe('#getMinimumFractionDigits', () => { | ||
test('should return the number of decimal places when format contains any', () => { | ||
expect(Format('0.00').getMinimumFractionDigits()).toBe(2); | ||
}); | ||
test('should return 0 when there is no specified amount of decimal places required', () => { | ||
expect(Format('0,0').getMinimumFractionDigits()).toBe(0); | ||
}); | ||
}); | ||
describe('#getCurrencyDisplay', () => { | ||
test('should return "code" when mask is valid and contains "USD"', () => { | ||
expect(Format('USD0,0').getCurrencyDisplay()).toBe('code'); | ||
}); | ||
test('should return "name" when mask is valid and contains "dollar"', () => { | ||
expect(Format('0,0 dollar').getCurrencyDisplay()).toBe('name'); | ||
}); | ||
test('should return "symbol" when mask is valid and contains "$"', () => { | ||
expect(Format('$0,0').getCurrencyDisplay()).toBe('symbol'); | ||
}); | ||
test('should return "undefined" when mask is valid but contains no currency indicator', () => { | ||
expect(Format('0,0').getCurrencyDisplay()).toBeUndefined(); | ||
}); | ||
test('should return "undefined" when mask is invalid', () => { | ||
expect(Format('abc').getCurrencyDisplay()).toBeUndefined(); | ||
}); | ||
}); | ||
describe('#getStyle', () => { | ||
test('should return "currency" when display mode is not undefined', () => { | ||
expect(Format('$0,0').getStyle()).toBe('currency'); | ||
}); | ||
test('should return "decimal" when display mode is undefined', () => { | ||
expect(Format('0,0').getStyle()).toBe('decimal'); | ||
}); | ||
}); | ||
describe('#getUseGrouping', () => { | ||
test('should return true when mask is valid and contains ","', () => { | ||
expect(Format('0,0').getUseGrouping()).toBe(true); | ||
}); | ||
test('should return false when mask is valid but contains no ","', () => { | ||
expect(Format('0.0').getUseGrouping()).toBe(false); | ||
}); | ||
test('should return false when mask is invalid', () => { | ||
expect(Format('abc').getUseGrouping()).toBe(false); | ||
}); | ||
}); | ||
}); |