{"version":3,"file":"humanFileSize.mjs","names":[],"sources":["../src/humanFileSize.ts"],"sourcesContent":["/**\n * Copyright (c) 2024-present, NDLA.\n *\n * This source code is licensed under the GPLv3 license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nimport { toIntlLanguage } from \"./toIntlLanguage\";\n\n// Code taken from https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string/72596863#72596863\n\nconst UNITS = [\"byte\", \"kilobyte\", \"megabyte\", \"gigabyte\", \"terabyte\", \"petabyte\"];\nconst BYTES_PER_KB = 1000;\n\nexport const humanFileSize = (sizeBytes: number | bigint, locale: string): string => {\n  let size = Math.abs(Number(sizeBytes));\n\n  let u = 0;\n  while (size >= BYTES_PER_KB && u < UNITS.length - 1) {\n    size /= BYTES_PER_KB;\n    ++u;\n  }\n\n  return new Intl.NumberFormat(toIntlLanguage(locale), {\n    style: \"unit\",\n    unit: UNITS[u],\n    unitDisplay: \"short\",\n    maximumFractionDigits: 2,\n  }).format(size);\n};\n"],"mappings":";;;;;;;;;AAYA,MAAM,QAAQ;CAAC;CAAQ;CAAY;CAAY;CAAY;CAAY;CAAW;AAClF,MAAM,eAAe;AAErB,MAAa,iBAAiB,WAA4B,WAA2B;CACnF,IAAI,OAAO,KAAK,IAAI,OAAO,UAAU,CAAC;CAEtC,IAAI,IAAI;AACR,QAAO,QAAQ,gBAAgB,IAAI,MAAM,SAAS,GAAG;AACnD,UAAQ;AACR,IAAE;;AAGJ,QAAO,IAAI,KAAK,aAAa,eAAe,OAAO,EAAE;EACnD,OAAO;EACP,MAAM,MAAM;EACZ,aAAa;EACb,uBAAuB;EACxB,CAAC,CAAC,OAAO,KAAK"}