make-it-rain 1.0.2 → 1.1.0

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.
Files changed (3) hide show
  1. package/README.md +4 -1
  2. package/index.js +31 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # 💸 make-it-rain 💸
2
+ [![Version](https://img.shields.io/badge/version-1.1.0-blue)](https://npmjs.com/package/make-it-rain) [![Node.js](https://img.shields.io/badge/node-%3E%3D12.0-brightgreen)](https://nodejs.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
3
+
2
4
  ### The Only Money Formatter You'll Ever Need (Probably)
3
5
 
4
6
  So, you've finally decided that numbers should look nice? **Wow, groundbreaking.**
@@ -61,4 +63,5 @@ MIT – Because I'm not here to sue you.
61
63
  ## ⭐ Pro Tip
62
64
  Use this package and pretend you’re rich. You’re welcome.
63
65
 
64
- This README will **ensure** your package is taken *seriously* while also making people question their life choices.
66
+ This README will **ensure** your package is taken *seriously* while also making people question their life choices.
67
+
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? Try again with an actual number.");
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "make-it-rain",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Because who formats money manually like a peasant?",
5
5
  "main": "index.js",
6
6
  "bin": {