@oliversalzburg/js-utils 0.1.3-dev.2 → 0.2.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.
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,39 @@
1
+ import { 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
+ const parts = format.formatToParts(-12345.6);
24
+ const numerals = Array.from({ length: 10 }).map((_, i) => format.format(i));
25
+ const index = new Map(numerals.map((d, i) => [d, i]));
26
+ const minusSign = new RegExp(`[${mustExist(parts.find(d => d.type === "minusSign")).value}]`);
27
+ const group = new RegExp(`[${mustExist(parts.find(d => d.type === "group")).value}]`, "g");
28
+ const decimal = new RegExp(`[${mustExist(parts.find(d => d.type === "decimal")).value}]`);
29
+ const numeral = new RegExp(`[${numerals.join("")}]`, "g");
30
+ const DIRECTION_MARK = /\u061c|\u200e/g;
31
+ return +count
32
+ .trim()
33
+ .replace(DIRECTION_MARK, "")
34
+ .replace(group, "")
35
+ .replace(decimal, ".")
36
+ .replace(numeral, d => mustExist(index.get(d)).toString())
37
+ .replace(minusSign, "-");
38
+ };
13
39
  //# 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,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAsB,EAAE,MAAe,EAAU,EAAE;IAC7E,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;QACnC,cAAc,EAAE,OAAO;QACvB,qBAAqB,EAAE,CAAC;QACxB,QAAQ,EAAE,SAAS;KACpB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnB,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAa,EAAE,MAAe,EAAU,EAAE;IACnE,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IAC7C,M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KAAK,EAAE,EAAE,CAAC;SAClB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;SACzD,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,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.0",
5
5
  "license": "MIT",
6
6
  "author": "Oliver Salzburg <oliver.salzburg@gmail.com>",
7
7
  "repository": {