Skip to content

Commit

Permalink
Add a package for localizing monetary amounts
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 8 changed files with 2,123 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/localize-monetary-amount/README.md
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'
```
1 change: 1 addition & 0 deletions packages/localize-monetary-amount/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { rootDir: __dirname, testMatch: [ '**/test/**/*.[jt]s?(x)' ] };
35 changes: 35 additions & 0 deletions packages/localize-monetary-amount/package.json
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"
}
Loading

0 comments on commit ac5b5a8

Please sign in to comment.