{"version":3,"file":"fileSize.mjs","sources":["../../lib/utils/fileSize.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { getCanonicalLocale } from '@nextcloud/l10n'\n\nconst humanList = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']\nconst humanListBinary = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']\n\n/**\n * Format a file size in a human-like format. e.g. 42GB\n *\n * The default for Nextcloud is like Windows using binary sizes but showing decimal units,\n * meaning 1024 bytes will show as 1KB instead of 1KiB or like on Apple 1.02 KB\n *\n * @param size in bytes\n * @param skipSmallSizes avoid rendering tiny sizes and return '< 1 KB' instead\n * @param binaryPrefixes True if size binary prefixes like `KiB` should be used as per IEC 80000-13\n * @param base1000 Set to true to use base 1000 as per SI or used by Apple (default is base 1024 like Linux or Windows)\n */\nexport function formatFileSize(size: number | string, skipSmallSizes = false, binaryPrefixes = false, base1000 = false): string {\n\t// Binary prefixes only work with base 1024\n\tbinaryPrefixes = binaryPrefixes && !base1000\n\n\tif (typeof size === 'string') {\n\t\tsize = Number(size)\n\t}\n\n\t// Calculate Log with base 1024 or 1000: size = base ** order\n\tlet order = size > 0 ? Math.floor(Math.log(size) / Math.log(base1000 ? 1000 : 1024)) : 0\n\n\t// Stay in range of the byte sizes that are defined\n\torder = Math.min((binaryPrefixes ? humanListBinary.length : humanList.length) - 1, order)\n\n\tconst readableFormat = binaryPrefixes ? humanListBinary[order] : humanList[order]\n\tlet relativeSize = (size / Math.pow(base1000 ? 1000 : 1024, order)).toFixed(1)\n\n\tif (skipSmallSizes === true && order === 0) {\n\t\treturn (relativeSize !== '0.0' ? '< 1 ' : '0 ') + (binaryPrefixes ? humanListBinary[1] : humanList[1])\n\t}\n\n\tif (order < 2) {\n\t\trelativeSize = parseFloat(relativeSize).toFixed(0)\n\t} else {\n\t\trelativeSize = parseFloat(relativeSize).toLocaleString(getCanonicalLocale())\n\t}\n\n\treturn relativeSize + ' ' + readableFormat\n}\n\n/**\n * Returns a file size in bytes from a humanly readable string\n * Note: `b` and `B` are both parsed as bytes and not as bit or byte.\n *\n * @param value file size in human-readable format\n * @param forceBinary for backwards compatibility this allows values to be base 2 (so 2KB means 2048 bytes instead of 2000 bytes)\n * @return or null if string could not be parsed\n */\nexport function parseFileSize(value: string, forceBinary = false) {\n\ttry {\n\t\tvalue = `${value}`.toLocaleLowerCase().replaceAll(/\\s+/g, '').replaceAll(',', '.')\n\t} catch {\n\t\treturn null\n\t}\n\n\tconst match = value.match(/^([0-9]*(\\.[0-9]*)?)([kmgtp]?)(i?)b?$/)\n\t// ignore not found, missing pre- and decimal, empty number\n\tif (match === null || match[1] === '.' || match[1] === '') {\n\t\treturn null\n\t}\n\n\tconst bytesArray = {\n\t\t'': 0,\n\t\tk: 1,\n\t\tm: 2,\n\t\tg: 3,\n\t\tt: 4,\n\t\tp: 5,\n\t\te: 6,\n\t}\n\n\tconst decimalString = `${match[1]}`\n\tconst base = (match[4] === 'i' || forceBinary) ? 1024 : 1000\n\n\treturn Math.round(Number.parseFloat(decimalString) * (base ** bytesArray[match[3]]))\n}\n"],"names":[],"mappings":";AAOA,MAAM,YAAY,CAAC,KAAK,MAAM,MAAM,MAAM,MAAM,IAAI;AACpD,MAAM,kBAAkB,CAAC,KAAK,OAAO,OAAO,OAAO,OAAO,KAAK;AAaxD,SAAS,eAAe,MAAuB,iBAAiB,OAAO,iBAAiB,OAAO,WAAW,OAAe;AAE/H,mBAAiB,kBAAkB,CAAC;AAEpC,MAAI,OAAO,SAAS,UAAU;AAC7B,WAAO,OAAO,IAAI;AAAA,EACnB;AAGA,MAAI,QAAQ,OAAO,IAAI,KAAK,MAAM,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,WAAW,MAAO,IAAI,CAAC,IAAI;AAGvF,UAAQ,KAAK,KAAK,iBAAiB,gBAAgB,SAAS,UAAU,UAAU,GAAG,KAAK;AAExF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