🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More

@oliversalzburg/js-utils

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@oliversalzburg/js-utils - npm Package Compare versions

Comparing version

to
0.2.1

/**
* Convert counts to a human readable string: `1234` → `1.234K`.
* @param count - The count to format.
* @param locale - The locale to use.
* @returns The formatted string.
*/
export declare const formatCount: (count: number | bigint) => string;
export declare const formatCount: (count: number | bigint, locale?: string) => string;
/**
* Convert counts from a human readable string back to a number: `1.234K` → `1234`.
* @param count - The count to parse.
* @param locale - The locale to use.
* @returns The number represented by the input string.
*/
export declare const parseCount: (count: string, locale?: string) => number;

@@ -0,8 +1,10 @@

import { coalesceArray, mustExist } from "../data/nil.js";
/**
* Convert counts to a human readable string: `1234` → `1.234K`.
* @param count - The count to format.
* @param locale - The locale to use.
* @returns The formatted string.
*/
export const formatCount = (count) => {
return new Intl.NumberFormat(undefined, {
export const formatCount = (count, locale) => {
return new Intl.NumberFormat(locale, {
compactDisplay: "short",

@@ -13,2 +15,37 @@ maximumFractionDigits: 3,

};
/**
* Convert counts from a human readable string back to a number: `1.234K` → `1234`.
* @param count - The count to parse.
* @param locale - The locale to use.
* @returns The number represented by the input string.
*/
export const parseCount = (count, locale) => {
const format = new Intl.NumberFormat(locale, {
compactDisplay: "short",
maximumFractionDigits: 3,
notation: "compact",
});
const parts = format.formatToParts(-12345.6);
const orders = coalesceArray(Array.from({ length: 20 }).map((_, i) => format.formatToParts(Math.pow(10, i)).find(d => d.type === "compact")?.value ?? ""));
const numerals = Array.from({ length: 10 }).map((_, i) => format.format(i));
const index = new Map(numerals.map((d, i) => [d, i]));
const minusSign = new RegExp(`[${mustExist(parts.find(d => d.type === "minusSign")).value}]`);
const decimal = new RegExp(`[${mustExist(parts.find(d => d.type === "decimal")).value}]`);
const numeral = new RegExp(`[${numerals.join("")}]`, "g");
const order = new RegExp(`(${[...new Set(orders.filter(o => o !== ""))].join("|")})`, "g");
const DIRECTION_MARK = /\u061c|\u200e|\u200f/g;
let multiplier = 1;
const value = count
.trim()
.replace(DIRECTION_MARK, "")
.replace(decimal, ".")
.replace(numeral, d => mustExist(index.get(d)).toString())
.replace(minusSign, "-")
.replace(order, o => {
multiplier = Math.pow(10, orders.indexOf(o));
return "";
})
.replace(/[ \u00a0]/g, "");
return +value * multiplier;
};
//# sourceMappingURL=count.js.map
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@oliversalzburg/js-utils",
"version": "0.1.3-dev.2",
"version": "0.2.1",
"license": "MIT",

@@ -6,0 +6,0 @@ "author": "Oliver Salzburg <oliver.salzburg@gmail.com>",

Sorry, the diff of this file is not supported yet