make-it-rain 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -6
- package/index.js +31 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ Because:
|
|
|
10
10
|
✅ You're too lazy to format numbers manually (same).
|
|
11
11
|
✅ You finally realized not everyone uses the same thousands separator.
|
|
12
12
|
✅ You enjoy writing `toLocaleString()` 50 times per project? No? Didn’t think so.
|
|
13
|
+
✅ Now lets you swap between currency symbols (€, $, ¥) or ISO codes (EUR, USD, JPY) because preferences exist.
|
|
13
14
|
|
|
14
15
|
## 📦 Installation
|
|
15
16
|
Oh no, another package to install? **Deal with it.**
|
|
@@ -26,19 +27,20 @@ yarn add make-it-rain
|
|
|
26
27
|
Initialize this life-changing formatter once, then flex on your ugly numbers forever.
|
|
27
28
|
|
|
28
29
|
```js
|
|
29
|
-
const makeItRain = require("make-it-rain");
|
|
30
|
+
const makeItRain = require("make-it-rain");
|
|
30
31
|
|
|
31
|
-
const money = makeItRain("EUR", "."); // Set it and forget it
|
|
32
|
+
const money = makeItRain("EUR", ".", true); // Set it and forget it
|
|
32
33
|
|
|
33
|
-
console.log(money.makeItPretty(1234567.89)); // €1.234.567,89
|
|
34
|
-
|
|
35
|
-
console.log(money.makeItPretty(
|
|
34
|
+
console.log(money.makeItPretty(1234567.89)); // €1.234.567,89
|
|
35
|
+
money.useWhat(false); // Switch to ISO code mode
|
|
36
|
+
console.log(money.makeItPretty(1234567.89)); // EUR 1.234.567,89
|
|
36
37
|
```
|
|
37
38
|
## 🎩 Features (That Should Have Existed in JavaScript Already)
|
|
38
39
|
- One-time setup: Set currency & separator once, use it everywhere.
|
|
39
40
|
- Formats numbers beautifully: Because nobody likes reading 1000000.00.
|
|
40
41
|
- Handles separators like a champ: Pick , or ., because the world can't agree.
|
|
41
42
|
- Errors when you mess up: Because I'm not letting you pass "lol" as a number.
|
|
43
|
+
- Toggle between currency symbols & ISO codes: Because some people prefer "€" while others need "EUR" for some serious reason.
|
|
42
44
|
|
|
43
45
|
## ❌ What This Won’t Do
|
|
44
46
|
- Convert currency: This isn't a forex exchange, buddy.
|
|
@@ -49,8 +51,11 @@ console.log(money.makeItPretty(1000000)); // €1.000.000,00
|
|
|
49
51
|
|
|
50
52
|
| Method | Description |
|
|
51
53
|
|--------|-------------|
|
|
52
|
-
|`makeItRain(currency, separator)` | Initializes the formatter once. Currency = any ISO currency code, separator = `,` or
|
|
54
|
+
|`makeItRain(currency, separator, useSymbol)` | Initializes the formatter once. Currency = any ISO currency code, separator = `,` or .`,` useSymbol = `true` for symbols (`$`), `false` for codes (`USD`). |
|
|
53
55
|
| `makeItPretty(amount)` | Takes a number and makes it visually acceptable.|
|
|
56
|
+
| `swapMoney(newCurrency)` | Change the currency to something else, because why not? |
|
|
57
|
+
| `useWhat(symbol)` | Toggle between using currency symbols (`$`) or ISO codes (`USD`). |
|
|
58
|
+
| `whatNow()` | Returns the current currency, in case you forgot. |
|
|
54
59
|
|
|
55
60
|
## 🏆 Contributing
|
|
56
61
|
Want to contribute? Cool. Fork it, break it, fix it, and submit a PR.
|
package/index.js
CHANGED
|
@@ -1,23 +1,40 @@
|
|
|
1
1
|
const currencySymbols = require("currency-symbol-map");
|
|
2
2
|
|
|
3
3
|
class MoneyFormatter {
|
|
4
|
-
constructor(currency = "USD", separator = ",") {
|
|
4
|
+
constructor(currency = "USD", separator = ",", useSymbol = true) {
|
|
5
5
|
if (![".", ","].includes(separator)) {
|
|
6
6
|
throw new Error("Separator must be either ',' or '.' — not whatever creative mess you just passed.");
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
this.currency = currency;
|
|
10
10
|
this.separator = separator;
|
|
11
|
+
this.useSymbol = useSymbol;
|
|
12
|
+
this.fixIt();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
fixIt() {
|
|
11
16
|
this.formatter = new Intl.NumberFormat("en-US", {
|
|
12
17
|
style: "currency",
|
|
13
|
-
currency: currency,
|
|
18
|
+
currency: this.currency,
|
|
14
19
|
minimumFractionDigits: 2,
|
|
15
20
|
});
|
|
16
21
|
}
|
|
17
22
|
|
|
23
|
+
swapMoney(newCurrency) {
|
|
24
|
+
if (typeof newCurrency !== "string" || newCurrency.length !== 3) {
|
|
25
|
+
throw new Error("Nice try. Use a real 3-letter currency code.");
|
|
26
|
+
}
|
|
27
|
+
this.currency = newCurrency.toUpperCase();
|
|
28
|
+
this.fixIt();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
useWhat(symbol = true) {
|
|
32
|
+
this.useSymbol = symbol;
|
|
33
|
+
}
|
|
34
|
+
|
|
18
35
|
makeItPretty(amount) {
|
|
19
36
|
if (typeof amount !== "number") {
|
|
20
|
-
throw new Error("Oh, so now we're formatting non-numbers?
|
|
37
|
+
throw new Error("Oh, so now we're formatting non-numbers? Genius.");
|
|
21
38
|
}
|
|
22
39
|
|
|
23
40
|
let formatted = this.formatter.format(amount);
|
|
@@ -26,15 +43,24 @@ class MoneyFormatter {
|
|
|
26
43
|
formatted = formatted.replace(/,/g, ".").replace(/\./g, ",");
|
|
27
44
|
}
|
|
28
45
|
|
|
46
|
+
if (!this.useSymbol) {
|
|
47
|
+
let symbol = currencySymbols.getSymbol(this.currency) || "";
|
|
48
|
+
formatted = formatted.replace(symbol, this.currency);
|
|
49
|
+
}
|
|
50
|
+
|
|
29
51
|
return formatted;
|
|
30
52
|
}
|
|
53
|
+
|
|
54
|
+
whatNow() {
|
|
55
|
+
return this.currency;
|
|
56
|
+
}
|
|
31
57
|
}
|
|
32
58
|
|
|
33
59
|
let instance = null;
|
|
34
60
|
|
|
35
|
-
function makeItRain(currency = "USD", separator = ",") {
|
|
61
|
+
function makeItRain(currency = "USD", separator = ",", useSymbol = true) {
|
|
36
62
|
if (!instance) {
|
|
37
|
-
instance = new MoneyFormatter(currency, separator);
|
|
63
|
+
instance = new MoneyFormatter(currency, separator, useSymbol);
|
|
38
64
|
}
|
|
39
65
|
return instance;
|
|
40
66
|
}
|