-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a package for localizing monetary amounts
This library was written for the in-progress composite-checkout package but has no external dependencies, so we're extracting it as its own package. Features: exact arithmetic and non-breaking spaces
- Loading branch information
nbloomf
committed
Nov 7, 2019
1 parent
1769e6e
commit ac5b5a8
Showing
8 changed files
with
2,123 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# localize-monetary-amount | ||
|
||
Locale- (language and geo) and currency-aware formatting of exact monetary amounts. | ||
|
||
What this package does: | ||
* Number localization (separator symbols, grouping conventions) | ||
* Integer arithmetic with all digits of precision. No floats. | ||
* Designed for displaying product prices in catalogs, carts, checkout, and receipts. | ||
* No dependencies | ||
|
||
What this package doesn't do: | ||
* Arithmetic on monetary amounts; formatting only | ||
* Conversion between currencies | ||
* Localize digit symbols (Hindu-Arabic digits only) | ||
* Take options. Locale preferences are hardcoded. Please submit an issue if a locale looks off. | ||
|
||
## Example usage | ||
|
||
You'll need: | ||
* An ISO 631-1 language code with an optional ISO 3166-1 alpha-2 region code, separated by a hyphen. Examples: `en`, `en-gb`, `fr-be`. | ||
* An ISO 4217 currency code. Examples: 'USD', 'JPY', 'BRL'. | ||
* An integer number of _minor units_. This is the minimal unit of your currency; e.g. cents for USD, yen for JPY. May be positive or negative. | ||
|
||
If the fractional part is zero it is omitted unless the locale prefers otherwise. | ||
|
||
```js | ||
import localizeMonetaryAmount from 'localize-monetary-amount'; | ||
|
||
// Strips zero minor units: | ||
localizeMonetaryAmount( 'en-us', 'USD', 500 ); // '$5' | ||
|
||
// Uses non-breaking spaces: | ||
localizeMonetaryAmount( 'fr-ca', 'CAD', 500000 ); // '$5\u00A0000\u00A0CAD' | ||
``` |
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 @@ | ||
module.exports = { rootDir: __dirname, testMatch: [ '**/test/**/*.[jt]s?(x)' ] }; |
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,35 @@ | ||
{ | ||
"name": "@automattic/localize-monetary-amount", | ||
"version": "1.0.0", | ||
"description": "Batteries included localized currency formatting", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"sideEffects": false, | ||
"scripts": { | ||
"clean": "npx rimraf dist", | ||
"prepublish": "npm run clean" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"keywords": [ | ||
"localization", | ||
"currency", | ||
"automattic" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Automattic/wp-calypso.git", | ||
"directory": "packages/localize-monetary-amount" | ||
}, | ||
"author": "@automattic", | ||
"license": "GPL-2.0-or-later", | ||
"bugs": { | ||
"url": "https://github.com/Automattic/wp-calypso/issues" | ||
}, | ||
"homepage": "https://github.com/Automattic/wp-calypso/tree/master/packages/localized-monetary-amount#readme" | ||
} |
Oops, something went wrong.