Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Karamov authored Sep 7, 2019
2 parents 8e28f8c + 9942271 commit bcfd43f
Show file tree
Hide file tree
Showing 26 changed files with 11,636 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/config/
/dist/
41 changes: 41 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module.exports = {
parser: "@typescript-eslint/parser",
extends: ["plugin:@typescript-eslint/recommended"],
plugins: ["@typescript-eslint"],
rules: {
"array-bracket-spacing": 2,
"brace-style": [2, "1tbs"],
"camelcase": 2,
"comma-spacing": 2,
"comma-style": 2,
"computed-property-spacing": 2,
"consistent-return": 2,
"curly": 2,
"dot-location": [2, "property"],
"eqeqeq": [2, "smart"],
"func-call-spacing": 2,
"func-names": [2, "as-needed"],
"guard-for-in": 1,
"key-spacing": 2,
"keyword-spacing": 2,
"linebreak-style": [1, "unix"],
"no-console": 0,
"no-delete-var": 2,
"no-else-return": 2,
"no-empty": 2,
"no-mixed-operators": 2,
"no-multi-spaces": 2,
"no-trailing-spaces": 0,
"no-var": 2,
"object-curly-spacing": [2, "always"],
"operator-linebreak": [2, "before", { overrides: { "=": "after" } }],
"quote-props": [2, "consistent-as-needed"],
"quotes": [2, "double", { allowTemplateLiterals: true }],
"semi": [2, "always"],
"semi-spacing": 2,
"space-before-blocks": 2,
"space-in-parens": 2,
"spaced-comment": 2,
"yoda": 2
}
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ out/

# JIRA plugin
atlassian-ide-plugin.xml

dist/**/*.dev.js
54 changes: 54 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
language: node_js
node_js:
- stable
cache: npm

install: npm ci

stages:
- build
- test
- deploy

jobs:
include:
- stage: build
name: "Prod build"
script:
- npm run build

- stage: test
name: "Test"
script:
- npm run test && npm i -g codecov && codecov

- stage: deploy
name: Deploy to GitHub Releases
script: npm run build
before_deploy:
- echo "Deploying to GitHub Releases"
deploy:
provider: releases
api_key:
secure: PmUkI/oJ8HDDGlxMbTzmDC6BVHuv0ITBmSQkcO61ksjNgUpbaCx3b0K8S8v/CF1R0Lq34AavREdwrWvVIbWHLVqNlCU+7pFhc2Qv3pfErLFGW4z1aLQsmEWSm1nhHOdYa7G0Tna0F90lU8YZlilNH52yK0PXeAQyx3d0yWMIHq2HPasPh7NwEacGYbkyD8hdQfjl4UsKXZXIczyy0m0Li7+1mixadUeSGmz39p6idBwoLP3hgCX5V7kJD6b37zcuKINRVLMKMalUtKHSysyJ8WgGphTMSHvnSFXYJxDb6RDFTHiDzjHApMy2j9ykQbkdaeqmtjO2KQ950m84dAPcKvJzZpYSKl6NWKaR/YPKkCdjiiY9hO+079dNSu6fMKUCrtvHTJQz9oaMvuVYjQXTlJuQenfwtHS4dKceZOUyLC4Jywus4NooCagyxp4A6R0QI3OAbXAGAC0UEn0VtXkxgzszstdsYoJsNibxMBXV//RKszTyjBLNE8g0dnTibqIJMtviTcBf5PfppYvhUh+ax4v5Io2ADRjBrDZi0XDk9XaBliK2VPl3eoQ1Qp+Y0W3bsR8WRygle4h+fxHPOpJH2jOylnGCua4eTTBpDi6VslFwlgdNtanNSUn/qIPeAQCSvvj1ZjhbFgo6C95LB9pDXCTfE40Jc95WtXsVqIrv/rM=
file:
- ./dist/pretty-money.umd.js
- ./dist/pretty-money.esm.js
skip_cleanup: true
draft: true
on:
tags: true
- stage: deploy
name: Deploy to NPM
script: npm run build
before_deploy:
- echo "Deploying to NPM"
deploy:
provider: npm
email: [email protected]
api_key:
secure: T+fFRTOTZL1UtQ5ZlsayLfYodHCYmidLr2MPlo5QH8QfK7oURNiYMradgy7Xi2uQ7bWXzzuZtON3XkB1TLzxZmOFUfFJHuTYYdLCBj/94T12S5qmLGL/uHg5yrdy9o388wmYtZBCCH1pTmN8bJ6vxeolRg3TjAKbAy4omo8S0KnjSyGTKNrPW8K89iIx1ZQwlf2oVLaavbAV+twNft2UnldlNrxco3FBXpndujTT/uBP0gFLI6miXixY0nUZN8GI8VPzcvNDyY4qVCangfL0DMOfsQyieumRsC+630snPRzSUmyG0G3NO34gBg/oEkAqRnxHdmTuZDjMo02gMMABE6z4RY+ETwBYSda/6DbDYpkkyBvhavP2cRwwqZFsjM/qJl3dOhLiBmPRPPmuIEl5GNhVgSBGBv6nxV8vXOkkiHO5dJOrW6zlahOwT0eOO176ASTMB8dctLFg/mlUulZvKaQ0OZw67++MuqXFS1/DJM499mXrLE5qw+2dwq+sekuxbHA93+BBX7mztKm2SlzzvroKeE+KrVsvTlnfhK0oxotIvG4C7SbhkTis5PPhK4GemytAgtL1PWuHzk9To7ir4KyX2A3ZKqEC8ERfFt7f8rbJEEYaMJoLk/3lQUFjn7pZUnazh68gov7rRlmchZJm6oFZwGCmWXvysY2SfJl8IvA=
skip_cleanup: true
draft: true
on:
tags: true
153 changes: 148 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,152 @@
# pretty-money

A tiny money formatting library for JS
A tiny currency formatting library for JavaScript.

- **Small.** The code is dead simple and the size is controlled by [Size Limit](https://github.com/ai/size-limit)
- **FP-friendly.** The functions can be curried to create the formatting function that you need.
- **Tree-shakable.** You don't have to import the whole library to only format one currency.
- **International.** It supports the nationally-accepted currency signs and their placements for every currency.
- **Small.** Dependency-free. 469 bytes minified and gzipped. Controlled by [Size Limit](https://github.com/ai/size-limit).
- **Functional.** The function can be used traditionally or curried for later reuse.
- **Flexible.** The config can be tweaked to present any modern currency.

```js
import prettyMoney from "pretty-money";
let price = prettyMoney({ currency: "EUR" }, 10000); //=> "10000 EUR"
```

Works everywhere where there is support for ES3.

----

## Difference from toLocaleString

ECMAScript's `Number` has a method `toLocaleString`, which has a similar idea. It too can be used to format numbers as
financial values and it even has built-in locales, however the output of the function is different on different Node
versions and browsers.

```js
let price = (10000).toLocaleString("ru", {
style: "currency",
currency: "RUB"
});

console.log(price); //=> "10 000 ₽" on Node < 12.something
console.log(price); //=> "10,000.00 RUB" on Node >= 12.something
```

While pretty-money doesn't have any locales built-in, it provides a better API, so that the end user can compose any
currency formatting function they need.

```js
let price = prettyMoney({
currency: "",
thousandsDelimiter: " "
})(10000);

console.log(price); //=> "10 000 ₽" on any Node, in any browser
```

## Install

`pretty-money` is available on NPM, so you can install it your usual way:

```sh
npm install pretty-money
// or
yarn add pretty-money
```

If you want to use pretty-money in browser, you can install the latest version with jsDelivr:

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pretty-money.umd.js"></script>
```

## Usage

There are two ways you can use a formatting function: traditional and functional.

Traditional way is to extract a config object for reuse and to call the function with two parameters — config and number —
every time:

```js
const prettyDollarConfig = {
currency: "$",
position: "before",
spaced: false,
thousandsDelimiter: ","
}

const priceA = prettyMoney(prettyDollarConfig, 1234); //=> "$1,234"
const priceB = prettyMoney(prettyDollarConfig, 567.89); //=> "$567.89"
```

Functional way is to curry the function, i.e. to create a function with set config and to call it with one parameter — number:

```js
const prettyEuro = prettyMoney({
currency: "",
decimals: "fixed",
decimalDelimiter: ",",
thousandsDelimiter: "."
})

const priceA = prettyEuro(1234); //=> "1.234,00 €"
const priceB = prettyEuro(567.89); //=> "567,89 €"
```

You can read more about how to configure pretty-money in the next section, [Config](#config).

## Config

### `currency`
**Type:** `string`
**Default:** `""`

The string to be used as currency symbol. It can be the respective sign (like "$"), currency code (like "GBP") or a word
(like "peso").

### `decimalDelimiter`
**Type:** `string`
**Default:** `"."`

A string that separates the integer and the fraction parts of the number.

### `decimals`
**Type:** `string`
**Values:** `"fixed"`, `"fluid"` or `"minmax"`
**Default:** `"minmax"`

Sets the amount of decimal places.

- `"fixed"` — the amount of places will always stay at `maxDecimal`. `minDecimal` has no effect.
- `"fluid"` — the amount of places will stay at any number between `minDecimal` and `maxDecimal`, in order not to have trailing zeros.
- `"minmax"` — the amount of places will stay at `maxDecimal` unless it's possible to be at `minDecimal` without having trailing zeros.

### `maxDecimal`
**Type:** `number`
**Default:** `2`

The maximum number of decimal places allowed in the number.

### `minDecimal`
**Type:** `number`
**Default:** `0`

The minimum number of decimal places allowed in the number. Has no effect when `decimals` is `"fixed"`.

### `position`
**Type:** `string`
**Values:** `"before"` or `"after"`
**Default:** `"after"`

Sets the position of the currency symbol with respect to the number.

### `spaced`
**Type:** `boolean`
**Default:** `true`

Sets whether there should be a space between the number and the currency symbol.

### `thousandsDelimiter`
**Type:** `string`
**Default:** `""`

A string that separates the thousands of the number.
7 changes: 7 additions & 0 deletions config/build/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const input = "./src/pretty-money.ts";

export const defaultOutput = {
name: "prettyMoney",
};

export const getFilename = (infix) => `./dist/pretty-money.${infix}.js`;
28 changes: 28 additions & 0 deletions config/build/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import rpBanner from 'rollup-plugin-banner';
import rpCleanup from 'rollup-plugin-cleanup';
import rpStrip from 'rollup-plugin-strip';
import { terser as rpTerser } from 'rollup-plugin-terser';
import rpTypescript2 from 'rollup-plugin-typescript2';

export const banner = () => rpBanner(
'pretty-money\n' +
'<%= pkg.description %>\n' +
'\n' +
'@version <%= pkg.version %>\n' +
'@license MIT'
);

export const cleanup = () => rpCleanup({
comments: 'license',
extensions: ['.ts']
});

export const strip = () => rpStrip({
debugger: true,
functions: ['console.log', 'console.debug'],
sourceMap: false
});

export const terser = () => rpTerser();

export const typescript = () => rpTypescript2();
14 changes: 14 additions & 0 deletions config/build/rollup.config.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defaultOutput, getFilename, input } from './defaults';
import { terser, typescript } from './plugins';

export default {
input,
output: {
...defaultOutput,
file: getFilename('dev'),
format: "umd"
},
plugins: [
typescript()
]
}
22 changes: 22 additions & 0 deletions config/build/rollup.config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defaultOutput, getFilename, input } from './defaults';
import { terser, typescript } from './plugins';

export default {
input,
output: [
{
...defaultOutput,
file: getFilename('umd'),
format: "umd"
},
{
...defaultOutput,
file: getFilename('esm'),
format: "esm"
},
],
plugins: [
typescript(),
terser()
]
}
Loading

0 comments on commit bcfd43f

Please sign in to comment.