@oliversalzburg/js-utils 0.1.3-dev.2 → 0.2.1

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/format/count.d.ts CHANGED
@@ -1,6 +1,14 @@
1
1
  /**
2
2
  * Convert counts to a human readable string: `1234` → `1.234K`.
3
3
  * @param count - The count to format.
4
+ * @param locale - The locale to use.
4
5
  * @returns The formatted string.
5
6
  */
6
- export declare const formatCount: (count: number | bigint) => string;
7
+ export declare const formatCount: (count: number | bigint, locale?: string) => string;
8
+ /**
9
+ * Convert counts from a human readable string back to a number: `1.234K` → `1234`.
10
+ * @param count - The count to parse.
11
+ * @param locale - The locale to use.
12
+ * @returns The number represented by the input string.
13
+ */
14
+ export declare const parseCount: (count: string, locale?: string) => number;
package/format/count.js CHANGED
@@ -1,13 +1,50 @@
1
+ import { coalesceArray, mustExist } from "../data/nil.js";
1
2
  /**
2
3
  * Convert counts to a human readable string: `1234` → `1.234K`.
3
4
  * @param count - The count to format.
5
+ * @param locale - The locale to use.
4
6
  * @returns The formatted string.
5
7
  */
6
- export const formatCount = (count) => {
7
- return new Intl.NumberFormat(undefined, {
8
+ export const formatCount = (count, locale) => {
9
+ return new Intl.NumberFormat(locale, {
8
10
  compactDisplay: "short",
9
11
  maximumFractionDigits: 3,
10
12
  notation: "compact",
11
13
  }).format(count);
12
14
  };
15
+ /**
16
+ * Convert counts from a human readable string back to a number: `1.234K` → `1234`.
17
+ * @param count - The count to parse.
18
+ * @param locale - The locale to use.
19
+ * @returns The number represented by the input string.
20
+ */
21
+ export const parseCount = (count, locale) => {
22
+ const format = new Intl.NumberFormat(locale, {
23
+ compactDisplay: "short",
24
+ maximumFractionDigits: 3,
25
+ notation: "compact",
26
+ });
27
+ const parts = format.formatToParts(-12345.6);
28
+ const orders = coalesceArray(Array.from({ length: 20 }).map((_, i) => format.formatToParts(Math.pow(10, i)).find(d => d.type === "compact")?.value ?? ""));
29
+ const numerals = Array.from({ length: 10 }).map((_, i) => format.format(i));
30
+ const index = new Map(numerals.map((d, i) => [d, i]));
31
+ const minusSign = new RegExp(`[${mustExist(parts.find(d => d.type === "minusSign")).value}]`);
32
+ const decimal = new RegExp(`[${mustExist(parts.find(d => d.type === "decimal")).value}]`);
33
+ const numeral = new RegExp(`[${numerals.join("")}]`, "g");
34
+ const order = new RegExp(`(${[...new Set(orders.filter(o => o !== ""))].join("|")})`, "g");
35
+ const DIRECTION_MARK = /\u061c|\u200e|\u200f/g;
36
+ let multiplier = 1;
37
+ const value = count
38
+ .trim()
39
+ .replace(DIRECTION_MARK, "")
40
+ .replace(decimal, ".")
41
+ .replace(numeral, d => mustExist(index.get(d)).toString())
42
+ .replace(minusSign, "-")
43
+ .replace(order, o => {
44
+ multiplier = Math.pow(10, orders.indexOf(o));
45
+ return "";
46
+ })
47
+ .replace(/[ \u00a0]/g, "");
48
+ return +value * multiplier;
49
+ };
13
50
  //# sourceMappingURL=count.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"count.js","sourceRoot":"","sources":["../../source/format/count.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAsB,EAAU,EAAE;IAC5D,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE;QACtC,cAAc,EAAE,OAAO;QACvB,qBAAqB,EAAE,CAAC;QACxB,QAAQ,EAAE,SAAS;KACpB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnB,CAAC,CAAC"}
1
+ {"version":3,"file":"count.js","sourceRoot":"","sources":["../../source/format/count.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAsB,EAAE,MAAe,EAAU,EAAE;IAC7E,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;QACnC,cAAc,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EAAE,EAAE,CAAC,CAAC;IAC7B,OAAO,CAAC,KAAK,GAAG,UAAU,CAAC;AAC7B,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@oliversalzburg/js-utils",
4
- "version": "0.1.3-dev.2",
4
+ "version": "0.2.1",
5
5
  "license": "MIT",
6
6
  "author": "Oliver Salzburg <oliver.salzburg@gmail.com>",
7
7
  "repository": {