diff --git a/dist/filesize.cjs b/dist/filesize.cjs index b86b644..613ac8c 100644 --- a/dist/filesize.cjs +++ b/dist/filesize.cjs @@ -61,7 +61,7 @@ const STRINGS = { /** * Converts a file size in bytes to a human-readable string with appropriate units - * @param {number|bigint} arg - The file size in bytes to convert + * @param {number|string|bigint} arg - The file size in bytes to convert * @param {Object} [options={}] - Configuration options for formatting * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter diff --git a/dist/filesize.js b/dist/filesize.js index 0b0ba6a..76037b6 100644 --- a/dist/filesize.js +++ b/dist/filesize.js @@ -57,7 +57,7 @@ const STRINGS = { } };/** * Converts a file size in bytes to a human-readable string with appropriate units - * @param {number|bigint} arg - The file size in bytes to convert + * @param {number|string|bigint} arg - The file size in bytes to convert * @param {Object} [options={}] - Configuration options for formatting * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter diff --git a/dist/filesize.min.js.map b/dist/filesize.min.js.map index c291a48..8a93051 100644 --- a/dist/filesize.min.js.map +++ b/dist/filesize.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filesize.min.js","sources":["../src/constants.js","../src/filesize.js"],"sourcesContent":["// Error Messages\nexport const INVALID_NUMBER = \"Invalid number\";\nexport const INVALID_ROUND = \"Invalid rounding method\";\n\n// Standard Types\nexport const IEC = \"iec\";\nexport const JEDEC = \"jedec\";\nexport const SI = \"si\";\n\n// Unit Types\nexport const BIT = \"bit\";\nexport const BITS = \"bits\";\nexport const BYTE = \"byte\";\nexport const BYTES = \"bytes\";\nexport const SI_KBIT = \"kbit\";\nexport const SI_KBYTE = \"kB\";\n\n// Output Format Types\nexport const ARRAY = \"array\";\nexport const FUNCTION = \"function\";\nexport const OBJECT = \"object\";\nexport const STRING = \"string\";\n\n// Processing Constants\nexport const EXPONENT = \"exponent\";\nexport const ROUND = \"round\";\n\n// Special Characters and Values\nexport const EMPTY = \"\";\nexport const PERIOD = \".\";\nexport const S = \"s\";\nexport const SPACE = \" \";\nexport const ZERO = \"0\";\n\n// Data Structures\nexport const STRINGS = {\n\tsymbol: {\n\t\tiec: {\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform: {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t}\n};\n","import {\n\tARRAY,\n\tBIT,\n\tBITS,\n\tBYTE,\n\tBYTES,\n\tEMPTY,\n\tEXPONENT,\n\tFUNCTION,\n\tIEC,\n\tINVALID_NUMBER,\n\tINVALID_ROUND,\n\tJEDEC,\n\tOBJECT,\n\tPERIOD,\n\tROUND,\n\tS,\n\tSI,\n\tSI_KBIT,\n\tSI_KBYTE,\n\tSPACE,\n\tSTRING,\n\tSTRINGS,\n\tZERO\n} from \"./constants\";\n\n/**\n * Converts a file size in bytes to a human-readable string with appropriate units\n * @param {number|bigint} arg - The file size in bytes to convert\n * @param {Object} [options={}] - Configuration options for formatting\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {string|Array|Object|number} Formatted file size based on output option\n * @throws {TypeError} When arg is not a valid number or roundingMethod is invalid\n * @example\n * filesize(1024) // \"1 KB\"\n * filesize(1024, {bits: true}) // \"8 Kb\"\n * filesize(1024, {output: \"object\"}) // {value: 1, symbol: \"KB\", exponent: 1, unit: \"KB\"}\n */\nexport function filesize (arg, {\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\tlet e = exponent,\n\t\tnum = Number(arg),\n\t\tresult = [],\n\t\tval = 0,\n\t\tu = EMPTY;\n\n\t// Sync base & standard\n\tif (standard === SI) {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t} else if (standard === IEC || standard === JEDEC) {\n\t\tbase = 2;\n\t} else if (base === 2) {\n\t\tstandard = IEC;\n\t} else {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t}\n\n\tconst ceil = base === 10 ? 1000 : 1024,\n\t\tfull = fullform === true,\n\t\tneg = num < 0,\n\t\troundingFunc = Math[roundingMethod];\n\n\tif (typeof arg !== \"bigint\" && isNaN(arg)) {\n\t\tthrow new TypeError(INVALID_NUMBER);\n\t}\n\n\tif (typeof roundingFunc !== FUNCTION) {\n\t\tthrow new TypeError(INVALID_ROUND);\n\t}\n\n\t// Flipping a negative number to determine the size\n\tif (neg) {\n\t\tnum = -num;\n\t}\n\n\t// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\tif (precision > 0) {\n\t\t\tprecision += 8 - e;\n\t\t}\n\n\t\te = 8;\n\t}\n\n\tif (output === EXPONENT) {\n\t\treturn e;\n\t}\n\n\t// Zero is now a special case because bytes divide by 1\n\tif (num === 0) {\n\t\tresult[0] = 0;\n\t\tu = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && exponent === -1) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t}\n\n\t// Decorating a 'diff'\n\tif (neg) {\n\t\tresult[0] = -result[0];\n\t}\n\n\t// Setting optional precision\n\tif (precision > 0) {\n\t\tresult[0] = result[0].toPrecision(precision);\n\t}\n\n\t// Applying custom symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(PERIOD, separator);\n\t}\n\n\tif (pad && round > 0) {\n\t\tconst i = result[0].toString(),\n\t\t\tx = separator || ((i.match(/(\\D)/g) || []).pop() || PERIOD),\n\t\t\ttmp = i.toString().split(x),\n\t\t\ts = tmp[1] || EMPTY,\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === ARRAY ? result : output === OBJECT ? {\n\t\tvalue: result[0],\n\t\tsymbol: result[1],\n\t\texponent: e,\n\t\tunit: u\n\t} : result.join(spacer);\n}\n\n/**\n * Creates a partially applied version of filesize with preset options\n * @param {Object} [options={}] - Default options to apply to the returned function\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {Function} A function that takes a file size and returns formatted output\n * @example\n * const formatBytes = partial({round: 1, standard: \"IEC\"});\n * formatBytes(1024) // \"1.0 KiB\"\n * formatBytes(2048) // \"2.0 KiB\"\n */\n// Partial application for functional programming\nexport function partial ({\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\treturn arg => filesize(arg, {\n\t\tbits,\n\t\tpad,\n\t\tbase,\n\t\tround,\n\t\tlocale,\n\t\tlocaleOptions,\n\t\tseparator,\n\t\tspacer,\n\t\tsymbols,\n\t\tstandard,\n\t\toutput,\n\t\tfullform,\n\t\tfullforms,\n\t\texponent,\n\t\troundingMethod,\n\t\tprecision\n\t});\n}\n"],"names":["JEDEC","BITS","BYTES","STRING","ROUND","EMPTY","STRINGS","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","length","toString","replace","i","x","match","pop","tmp","split","s","l","n","padEnd","value","unit","join","partial"],"mappings":";;;;AACO,MAKMA,EAAQ,QAKRC,EAAO,OAEPC,EAAQ,QAQRC,EAAS,SAITC,EAAQ,QAGRC,EAAQ,GAORC,EAAU,CACtBC,OAAQ,CACPC,IAAK,CAC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,EAAQ,EAAG,CACrB,MAAMkC,EAAKnB,EAAO,GAAGiB,WACpBG,EAAIhC,IAAe+B,EAAEE,MAAM,UAAY,IAAIC,ODvJxB,ICwJnBC,EAAMJ,EAAEF,WAAWO,MAAMJ,GACzBK,EAAIF,EAAI,IAAMlD,EACdqD,EAAID,EAAET,OACNW,EAAI1C,EAAQyC,EAEb1B,EAAO,GAAK,GAAGuB,EAAI,KAAKH,IAAIK,EAAEG,OAAOF,EAAIC,ED1JvB,MC2JnB,CAOA,OALIvB,IACHJ,EAAO,GAAKP,EAAUI,GAAKJ,EAAUI,GAAKvB,EAAQM,SAASW,GAAUM,IAAMpB,EDpL1D,MAEC,SCkL6F,IAAduB,EAAO,GAAW3B,EDhKpG,MAZI,UCgLbmB,EAAmBQ,ED9KL,WC8KcR,EAAoB,CACtDqC,MAAO7B,EAAO,GACdzB,OAAQyB,EAAO,GACfN,SAAUG,EACViC,KAAM5B,GACHF,EAAO+B,KAAK1C,EACjB,CA4BO,SAAS2C,GAASvD,KACxBA,GAAO,EAAKM,IACZA,GAAM,EAAKC,KACXA,GAAO,EAAEC,MACTA,EAAQ,EAACC,OACTA,EAASb,GAAKc,cACdA,EAAgB,CAAA,EAAEC,UAClBA,EAAYf,GAAKgB,OACjBA,ED7MoB,IC6MNC,QACdA,EAAU,CAAA,EAAEC,SACZA,EAAWlB,GAAKmB,OAChBA,EAASrB,EAAMS,SACfA,GAAW,EAAKa,UAChBA,EAAY,GAAEC,SACdA,GAAW,EAAEC,eACbA,EAAiBvB,EAAKwB,UACtBA,EAAY,GACT,IACH,OAAOd,GAAOD,EAASC,EAAK,CAC3BL,OACAM,MACAC,OACAC,QACAC,SACAC,gBACAC,YACAC,SACAC,UACAC,WACAC,SACAZ,WACAa,YACAC,WACAC,iBACAC,aAEF,QAAAf,cAAAmD"} \ No newline at end of file +{"version":3,"file":"filesize.min.js","sources":["../src/constants.js","../src/filesize.js"],"sourcesContent":["// Error Messages\nexport const INVALID_NUMBER = \"Invalid number\";\nexport const INVALID_ROUND = \"Invalid rounding method\";\n\n// Standard Types\nexport const IEC = \"iec\";\nexport const JEDEC = \"jedec\";\nexport const SI = \"si\";\n\n// Unit Types\nexport const BIT = \"bit\";\nexport const BITS = \"bits\";\nexport const BYTE = \"byte\";\nexport const BYTES = \"bytes\";\nexport const SI_KBIT = \"kbit\";\nexport const SI_KBYTE = \"kB\";\n\n// Output Format Types\nexport const ARRAY = \"array\";\nexport const FUNCTION = \"function\";\nexport const OBJECT = \"object\";\nexport const STRING = \"string\";\n\n// Processing Constants\nexport const EXPONENT = \"exponent\";\nexport const ROUND = \"round\";\n\n// Special Characters and Values\nexport const EMPTY = \"\";\nexport const PERIOD = \".\";\nexport const S = \"s\";\nexport const SPACE = \" \";\nexport const ZERO = \"0\";\n\n// Data Structures\nexport const STRINGS = {\n\tsymbol: {\n\t\tiec: {\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform: {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t}\n};\n","import {\n\tARRAY,\n\tBIT,\n\tBITS,\n\tBYTE,\n\tBYTES,\n\tEMPTY,\n\tEXPONENT,\n\tFUNCTION,\n\tIEC,\n\tINVALID_NUMBER,\n\tINVALID_ROUND,\n\tJEDEC,\n\tOBJECT,\n\tPERIOD,\n\tROUND,\n\tS,\n\tSI,\n\tSI_KBIT,\n\tSI_KBYTE,\n\tSPACE,\n\tSTRING,\n\tSTRINGS,\n\tZERO\n} from \"./constants\";\n\n/**\n * Converts a file size in bytes to a human-readable string with appropriate units\n * @param {number|string|bigint} arg - The file size in bytes to convert\n * @param {Object} [options={}] - Configuration options for formatting\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {string|Array|Object|number} Formatted file size based on output option\n * @throws {TypeError} When arg is not a valid number or roundingMethod is invalid\n * @example\n * filesize(1024) // \"1 KB\"\n * filesize(1024, {bits: true}) // \"8 Kb\"\n * filesize(1024, {output: \"object\"}) // {value: 1, symbol: \"KB\", exponent: 1, unit: \"KB\"}\n */\nexport function filesize (arg, {\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\tlet e = exponent,\n\t\tnum = Number(arg),\n\t\tresult = [],\n\t\tval = 0,\n\t\tu = EMPTY;\n\n\t// Sync base & standard\n\tif (standard === SI) {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t} else if (standard === IEC || standard === JEDEC) {\n\t\tbase = 2;\n\t} else if (base === 2) {\n\t\tstandard = IEC;\n\t} else {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t}\n\n\tconst ceil = base === 10 ? 1000 : 1024,\n\t\tfull = fullform === true,\n\t\tneg = num < 0,\n\t\troundingFunc = Math[roundingMethod];\n\n\tif (typeof arg !== \"bigint\" && isNaN(arg)) {\n\t\tthrow new TypeError(INVALID_NUMBER);\n\t}\n\n\tif (typeof roundingFunc !== FUNCTION) {\n\t\tthrow new TypeError(INVALID_ROUND);\n\t}\n\n\t// Flipping a negative number to determine the size\n\tif (neg) {\n\t\tnum = -num;\n\t}\n\n\t// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\tif (precision > 0) {\n\t\t\tprecision += 8 - e;\n\t\t}\n\n\t\te = 8;\n\t}\n\n\tif (output === EXPONENT) {\n\t\treturn e;\n\t}\n\n\t// Zero is now a special case because bytes divide by 1\n\tif (num === 0) {\n\t\tresult[0] = 0;\n\t\tu = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && exponent === -1) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t}\n\n\t// Decorating a 'diff'\n\tif (neg) {\n\t\tresult[0] = -result[0];\n\t}\n\n\t// Setting optional precision\n\tif (precision > 0) {\n\t\tresult[0] = result[0].toPrecision(precision);\n\t}\n\n\t// Applying custom symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(PERIOD, separator);\n\t}\n\n\tif (pad && round > 0) {\n\t\tconst i = result[0].toString(),\n\t\t\tx = separator || ((i.match(/(\\D)/g) || []).pop() || PERIOD),\n\t\t\ttmp = i.toString().split(x),\n\t\t\ts = tmp[1] || EMPTY,\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === ARRAY ? result : output === OBJECT ? {\n\t\tvalue: result[0],\n\t\tsymbol: result[1],\n\t\texponent: e,\n\t\tunit: u\n\t} : result.join(spacer);\n}\n\n/**\n * Creates a partially applied version of filesize with preset options\n * @param {Object} [options={}] - Default options to apply to the returned function\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {Function} A function that takes a file size and returns formatted output\n * @example\n * const formatBytes = partial({round: 1, standard: \"IEC\"});\n * formatBytes(1024) // \"1.0 KiB\"\n * formatBytes(2048) // \"2.0 KiB\"\n */\n// Partial application for functional programming\nexport function partial ({\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\treturn arg => filesize(arg, {\n\t\tbits,\n\t\tpad,\n\t\tbase,\n\t\tround,\n\t\tlocale,\n\t\tlocaleOptions,\n\t\tseparator,\n\t\tspacer,\n\t\tsymbols,\n\t\tstandard,\n\t\toutput,\n\t\tfullform,\n\t\tfullforms,\n\t\texponent,\n\t\troundingMethod,\n\t\tprecision\n\t});\n}\n"],"names":["JEDEC","BITS","BYTES","STRING","ROUND","EMPTY","STRINGS","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","length","toString","replace","i","x","match","pop","tmp","split","s","l","n","padEnd","value","unit","join","partial"],"mappings":";;;;AACO,MAKMA,EAAQ,QAKRC,EAAO,OAEPC,EAAQ,QAQRC,EAAS,SAITC,EAAQ,QAGRC,EAAQ,GAORC,EAAU,CACtBC,OAAQ,CACPC,IAAK,CACJC,KAAM,CAAC,MAAO,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,SAC7EC,MAAO,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,QAE/DC,MAAO,CACNF,KAAM,CAAC,MAAO,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QACtEC,MAAO,CAAC,IAAK,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,QAGzDE,SAAU,CACTJ,IAAK,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QAClEG,MAAO,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,MAAO,QAAS,WCK/D,SAASE,EAAUC,GAAKL,KAC9BA,GAAO,EAAKM,IACZA,GAAM,EAAKC,KACXA,GAAO,EAAEC,MACTA,EAAQ,EAACC,OACTA,EAASb,GAAKc,cACdA,EAAgB,CAAA,EAAEC,UAClBA,EAAYf,GAAKgB,OACjBA,ED9BoB,IC8BNC,QACdA,EAAU,CAAA,EAAEC,SACZA,EAAWlB,GAAKmB,OAChBA,EAASrB,EAAMS,SACfA,GAAW,EAAKa,UAChBA,EAAY,GAAEC,SACdA,GAAW,EAAEC,eACbA,EAAiBvB,EAAKwB,UACtBA,EAA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A,EAAEC,SACZA,EAAWlB,GAAKmB,OAChBA,EAASrB,EAAMS,SACfA,GAAW,EAAKa,UAChBA,EAAY,GAAEC,SACdA,GAAW,EAAEC,eACbA,EAAiBvB,EAAKwB,UACtBA,EAAY,GACT,IACH,OAAOd,GAAOD,EAASC,EAAK,CAC3BL,OACAM,MACAC,OACAC,QACAC,SACAC,gBACAC,YACAC,SACAC,UACAC,WACAC,SACAZ,WACAa,YACAC,WACAC,iBACAC,aAEF,QAAAf,cAAAmD"} \ No newline at end of file diff --git a/dist/filesize.umd.js b/dist/filesize.umd.js index 6b97b7e..9e5bb36 100644 --- a/dist/filesize.umd.js +++ b/dist/filesize.umd.js @@ -57,7 +57,7 @@ const STRINGS = { } };/** * Converts a file size in bytes to a human-readable string with appropriate units - * @param {number|bigint} arg - The file size in bytes to convert + * @param {number|string|bigint} arg - The file size in bytes to convert * @param {Object} [options={}] - Configuration options for formatting * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter diff --git a/dist/filesize.umd.min.js.map b/dist/filesize.umd.min.js.map index 610e7d9..7b7665e 100644 --- a/dist/filesize.umd.min.js.map +++ b/dist/filesize.umd.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filesize.umd.min.js","sources":["../src/constants.js","../src/filesize.js"],"sourcesContent":["// Error Messages\nexport const INVALID_NUMBER = \"Invalid number\";\nexport const INVALID_ROUND = \"Invalid rounding method\";\n\n// Standard Types\nexport const IEC = \"iec\";\nexport const JEDEC = \"jedec\";\nexport const SI = \"si\";\n\n// Unit Types\nexport const BIT = \"bit\";\nexport const BITS = \"bits\";\nexport const BYTE = \"byte\";\nexport const BYTES = \"bytes\";\nexport const SI_KBIT = \"kbit\";\nexport const SI_KBYTE = \"kB\";\n\n// Output Format Types\nexport const ARRAY = \"array\";\nexport const FUNCTION = \"function\";\nexport const OBJECT = \"object\";\nexport const STRING = \"string\";\n\n// Processing Constants\nexport const EXPONENT = \"exponent\";\nexport const ROUND = \"round\";\n\n// Special Characters and Values\nexport const EMPTY = \"\";\nexport const PERIOD = \".\";\nexport const S = \"s\";\nexport const SPACE = \" \";\nexport const ZERO = \"0\";\n\n// Data Structures\nexport const STRINGS = {\n\tsymbol: {\n\t\tiec: {\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform: {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t}\n};\n","import {\n\tARRAY,\n\tBIT,\n\tBITS,\n\tBYTE,\n\tBYTES,\n\tEMPTY,\n\tEXPONENT,\n\tFUNCTION,\n\tIEC,\n\tINVALID_NUMBER,\n\tINVALID_ROUND,\n\tJEDEC,\n\tOBJECT,\n\tPERIOD,\n\tROUND,\n\tS,\n\tSI,\n\tSI_KBIT,\n\tSI_KBYTE,\n\tSPACE,\n\tSTRING,\n\tSTRINGS,\n\tZERO\n} from \"./constants\";\n\n/**\n * Converts a file size in bytes to a human-readable string with appropriate units\n * @param {number|bigint} arg - The file size in bytes to convert\n * @param {Object} [options={}] - Configuration options for formatting\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {string|Array|Object|number} Formatted file size based on output option\n * @throws {TypeError} When arg is not a valid number or roundingMethod is invalid\n * @example\n * filesize(1024) // \"1 KB\"\n * filesize(1024, {bits: true}) // \"8 Kb\"\n * filesize(1024, {output: \"object\"}) // {value: 1, symbol: \"KB\", exponent: 1, unit: \"KB\"}\n */\nexport function filesize (arg, {\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\tlet e = exponent,\n\t\tnum = Number(arg),\n\t\tresult = [],\n\t\tval = 0,\n\t\tu = EMPTY;\n\n\t// Sync base & standard\n\tif (standard === SI) {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t} else if (standard === IEC || standard === JEDEC) {\n\t\tbase = 2;\n\t} else if (base === 2) {\n\t\tstandard = IEC;\n\t} else {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t}\n\n\tconst ceil = base === 10 ? 1000 : 1024,\n\t\tfull = fullform === true,\n\t\tneg = num < 0,\n\t\troundingFunc = Math[roundingMethod];\n\n\tif (typeof arg !== \"bigint\" && isNaN(arg)) {\n\t\tthrow new TypeError(INVALID_NUMBER);\n\t}\n\n\tif (typeof roundingFunc !== FUNCTION) {\n\t\tthrow new TypeError(INVALID_ROUND);\n\t}\n\n\t// Flipping a negative number to determine the size\n\tif (neg) {\n\t\tnum = -num;\n\t}\n\n\t// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\tif (precision > 0) {\n\t\t\tprecision += 8 - e;\n\t\t}\n\n\t\te = 8;\n\t}\n\n\tif (output === EXPONENT) {\n\t\treturn e;\n\t}\n\n\t// Zero is now a special case because bytes divide by 1\n\tif (num === 0) {\n\t\tresult[0] = 0;\n\t\tu = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && exponent === -1) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t}\n\n\t// Decorating a 'diff'\n\tif (neg) {\n\t\tresult[0] = -result[0];\n\t}\n\n\t// Setting optional precision\n\tif (precision > 0) {\n\t\tresult[0] = result[0].toPrecision(precision);\n\t}\n\n\t// Applying custom symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(PERIOD, separator);\n\t}\n\n\tif (pad && round > 0) {\n\t\tconst i = result[0].toString(),\n\t\t\tx = separator || ((i.match(/(\\D)/g) || []).pop() || PERIOD),\n\t\t\ttmp = i.toString().split(x),\n\t\t\ts = tmp[1] || EMPTY,\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === ARRAY ? result : output === OBJECT ? {\n\t\tvalue: result[0],\n\t\tsymbol: result[1],\n\t\texponent: e,\n\t\tunit: u\n\t} : result.join(spacer);\n}\n\n/**\n * Creates a partially applied version of filesize with preset options\n * @param {Object} [options={}] - Default options to apply to the returned function\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {Function} A function that takes a file size and returns formatted output\n * @example\n * const formatBytes = partial({round: 1, standard: \"IEC\"});\n * formatBytes(1024) // \"1.0 KiB\"\n * formatBytes(2048) // \"2.0 KiB\"\n */\n// Partial application for functional programming\nexport function partial ({\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\treturn arg => filesize(arg, {\n\t\tbits,\n\t\tpad,\n\t\tbase,\n\t\tround,\n\t\tlocale,\n\t\tlocaleOptions,\n\t\tseparator,\n\t\tspacer,\n\t\tsymbols,\n\t\tstandard,\n\t\toutput,\n\t\tfullform,\n\t\tfullforms,\n\t\texponent,\n\t\troundingMethod,\n\t\tprecision\n\t});\n}\n"],"names":["g","f","exports","module","define","amd","globalThis","self","filesize","this","JEDEC","BITS","BYTES","STRING","ROUND","EMPTY","STRINGS","symbol","iec","bits","bytes","jedec","fullform","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","length","toString","replace","i","x","match","pop","tmp","split","s","l","n","padEnd","value","unit","join","partial"],"mappings":";;;;CAAA,SAAAA,EAAAC,GAAA,iBAAAC,SAAA,oBAAAC,O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BA,EAAY,GAAEC,SACdA,GAAW,EAAEC,eACbA,EAAiBtB,EAAKuB,UACtBA,EAAY,GACT,IACH,OAAOd,GAAOf,EAASe,EAAK,CAC3BJ,OACAK,MACAC,OACAC,QACAC,SACAC,gBACAC,YACAC,SACAC,UACAC,WACAC,SACAX,WACAY,YACAC,WACAC,iBACAC,aAEF,CAAA"} \ No newline at end of file +{"version":3,"file":"filesize.umd.min.js","sources":["../src/constants.js","../src/filesize.js"],"sourcesContent":["// Error Messages\nexport const INVALID_NUMBER = \"Invalid number\";\nexport const INVALID_ROUND = \"Invalid rounding method\";\n\n// Standard Types\nexport const IEC = \"iec\";\nexport const JEDEC = \"jedec\";\nexport const SI = \"si\";\n\n// Unit Types\nexport const BIT = \"bit\";\nexport const BITS = \"bits\";\nexport const BYTE = \"byte\";\nexport const BYTES = \"bytes\";\nexport const SI_KBIT = \"kbit\";\nexport const SI_KBYTE = \"kB\";\n\n// Output Format Types\nexport const ARRAY = \"array\";\nexport const FUNCTION = \"function\";\nexport const OBJECT = \"object\";\nexport const STRING = \"string\";\n\n// Processing Constants\nexport const EXPONENT = \"exponent\";\nexport const ROUND = \"round\";\n\n// Special Characters and Values\nexport const EMPTY = \"\";\nexport const PERIOD = \".\";\nexport const S = \"s\";\nexport const SPACE = \" \";\nexport const ZERO = \"0\";\n\n// Data Structures\nexport const STRINGS = {\n\tsymbol: {\n\t\tiec: {\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform: {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t}\n};\n","import {\n\tARRAY,\n\tBIT,\n\tBITS,\n\tBYTE,\n\tBYTES,\n\tEMPTY,\n\tEXPONENT,\n\tFUNCTION,\n\tIEC,\n\tINVALID_NUMBER,\n\tINVALID_ROUND,\n\tJEDEC,\n\tOBJECT,\n\tPERIOD,\n\tROUND,\n\tS,\n\tSI,\n\tSI_KBIT,\n\tSI_KBYTE,\n\tSPACE,\n\tSTRING,\n\tSTRINGS,\n\tZERO\n} from \"./constants\";\n\n/**\n * Converts a file size in bytes to a human-readable string with appropriate units\n * @param {number|string|bigint} arg - The file size in bytes to convert\n * @param {Object} [options={}] - Configuration options for formatting\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {string|Array|Object|number} Formatted file size based on output option\n * @throws {TypeError} When arg is not a valid number or roundingMethod is invalid\n * @example\n * filesize(1024) // \"1 KB\"\n * filesize(1024, {bits: true}) // \"8 Kb\"\n * filesize(1024, {output: \"object\"}) // {value: 1, symbol: \"KB\", exponent: 1, unit: \"KB\"}\n */\nexport function filesize (arg, {\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\tlet e = exponent,\n\t\tnum = Number(arg),\n\t\tresult = [],\n\t\tval = 0,\n\t\tu = EMPTY;\n\n\t// Sync base & standard\n\tif (standard === SI) {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t} else if (standard === IEC || standard === JEDEC) {\n\t\tbase = 2;\n\t} else if (base === 2) {\n\t\tstandard = IEC;\n\t} else {\n\t\tbase = 10;\n\t\tstandard = JEDEC;\n\t}\n\n\tconst ceil = base === 10 ? 1000 : 1024,\n\t\tfull = fullform === true,\n\t\tneg = num < 0,\n\t\troundingFunc = Math[roundingMethod];\n\n\tif (typeof arg !== \"bigint\" && isNaN(arg)) {\n\t\tthrow new TypeError(INVALID_NUMBER);\n\t}\n\n\tif (typeof roundingFunc !== FUNCTION) {\n\t\tthrow new TypeError(INVALID_ROUND);\n\t}\n\n\t// Flipping a negative number to determine the size\n\tif (neg) {\n\t\tnum = -num;\n\t}\n\n\t// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\tif (precision > 0) {\n\t\t\tprecision += 8 - e;\n\t\t}\n\n\t\te = 8;\n\t}\n\n\tif (output === EXPONENT) {\n\t\treturn e;\n\t}\n\n\t// Zero is now a special case because bytes divide by 1\n\tif (num === 0) {\n\t\tresult[0] = 0;\n\t\tu = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && exponent === -1) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];\n\t}\n\n\t// Decorating a 'diff'\n\tif (neg) {\n\t\tresult[0] = -result[0];\n\t}\n\n\t// Setting optional precision\n\tif (precision > 0) {\n\t\tresult[0] = result[0].toPrecision(precision);\n\t}\n\n\t// Applying custom symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(PERIOD, separator);\n\t}\n\n\tif (pad && round > 0) {\n\t\tconst i = result[0].toString(),\n\t\t\tx = separator || ((i.match(/(\\D)/g) || []).pop() || PERIOD),\n\t\t\ttmp = i.toString().split(x),\n\t\t\ts = tmp[1] || EMPTY,\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : STRINGS.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === ARRAY ? result : output === OBJECT ? {\n\t\tvalue: result[0],\n\t\tsymbol: result[1],\n\t\texponent: e,\n\t\tunit: u\n\t} : result.join(spacer);\n}\n\n/**\n * Creates a partially applied version of filesize with preset options\n * @param {Object} [options={}] - Default options to apply to the returned function\n * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes\n * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter\n * @param {number} [options.base=-1] - Number base (2 for binary, 10 for decimal, -1 for auto)\n * @param {number} [options.round=2] - Number of decimal places to round to\n * @param {string|boolean} [options.locale=\"\"] - Locale for number formatting, true for system locale\n * @param {Object} [options.localeOptions={}] - Additional options for locale formatting\n * @param {string} [options.separator=\"\"] - Custom decimal separator\n * @param {string} [options.spacer=\" \"] - String to separate value and unit\n * @param {Object} [options.symbols={}] - Custom unit symbols\n * @param {string} [options.standard=\"\"] - Unit standard to use (SI, IEC, JEDEC)\n * @param {string} [options.output=\"string\"] - Output format: \"string\", \"array\", \"object\", or \"exponent\"\n * @param {boolean} [options.fullform=false] - If true, uses full unit names instead of abbreviations\n * @param {Array} [options.fullforms=[]] - Custom full unit names\n * @param {number} [options.exponent=-1] - Force specific exponent (-1 for auto)\n * @param {string} [options.roundingMethod=\"round\"] - Math rounding method to use\n * @param {number} [options.precision=0] - Number of significant digits (0 for auto)\n * @returns {Function} A function that takes a file size and returns formatted output\n * @example\n * const formatBytes = partial({round: 1, standard: \"IEC\"});\n * formatBytes(1024) // \"1.0 KiB\"\n * formatBytes(2048) // \"2.0 KiB\"\n */\n// Partial application for functional programming\nexport function partial ({\n\tbits = false,\n\tpad = false,\n\tbase = -1,\n\tround = 2,\n\tlocale = EMPTY,\n\tlocaleOptions = {},\n\tseparator = EMPTY,\n\tspacer = SPACE,\n\tsymbols = {},\n\tstandard = EMPTY,\n\toutput = STRING,\n\tfullform = false,\n\tfullforms = [],\n\texponent = -1,\n\troundingMethod = ROUND,\n\tprecision = 0\n} = {}) {\n\treturn arg => filesize(arg, {\n\t\tbits,\n\t\tpad,\n\t\tbase,\n\t\tround,\n\t\tlocale,\n\t\tlocaleOptions,\n\t\tseparator,\n\t\tspacer,\n\t\tsymbols,\n\t\tstandard,\n\t\toutput,\n\t\tfullform,\n\t\tfullforms,\n\t\texponent,\n\t\troundingMethod,\n\t\tprecision\n\t});\n}\n"],"names":["g","f","exports","module","define","amd","globalThis","self","filesize","this","JEDEC","BITS","BYTES","STRING","ROUND","EMPTY","STRINGS","symbol","iec","bits","bytes","jedec","fullform","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","length","toString","replace","i","x","match","pop","tmp","split","s","l","n","padEnd","value","unit","join","partial"],"mappings":";;;;CAAA,SAAAA,EAAAC,GAAA,iBAAAC,SAAA,oBAAAC,OAAAF,EAAAC,SAAA,mBAAAE,QAAAA,OAAAC,IAAAD,OAAA,CAAA,WAAAH,GAAAA,GAAAD,EAAA,oBAAAM,WAAAA,WAAAN,GAAAO,MAAAC,SAAA,CAAA,EAAA,CAAA,CAAAC,KAAA,SAAAP,GAAA,aACO,MAKMQ,EAAQ,QAKRC,EAAO,OAEPC,EAAQ,QAQRC,EAAS,SAITC,EAAQ,QAGRC,EAAQ,GAORC,EAAU,CACtBC,OAAQ,CACPC,IAAK,CACJC,KAAM,CAAC,MAAO,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,SAC7EC,MAAO,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,QAE/DC,MAAO,CACNF,KAAM,CAAC,MAAO,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QACtE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1B,EAAO,GAAK,GAAGuB,EAAI,KAAKH,IAAIK,EAAEG,OAAOF,EAAIC,ED1JvB,MC2JnB,CAOA,OALIvB,IACHJ,EAAO,GAAKP,EAAUI,GAAKJ,EAAUI,GAAKtB,EAAQM,SAASU,GAAUM,IAAMnB,EDpL1D,MAEC,SCkL6F,IAAdsB,EAAO,GAAW1B,EDhKpG,MAZI,UCgLbkB,EAAmBQ,ED9KL,WC8KcR,EAAoB,CACtDqC,MAAO7B,EAAO,GACdxB,OAAQwB,EAAO,GACfN,SAAUG,EACViC,KAAM5B,GACHF,EAAO+B,KAAK1C,EACjB,CAgEA5B,EAAAM,SAAAA,EAAAN,EAAAuE,QApCO,UAAkBtD,KACxBA,GAAO,EAAKK,IACZA,GAAM,EAAKC,KACXA,GAAO,EAAEC,MACTA,EAAQ,EAACC,OACTA,EAASZ,GAAKa,cACdA,EAAgB,CAAA,EAAEC,UAClBA,EAAYd,GAAKe,OACjBA,ED7MoB,IC6MNC,QACdA,EAAU,CAAA,EAAEC,SACZA,EAAWjB,GAAKkB,OAChBA,EAASpB,EAAMS,SACfA,GAAW,EAAKY,UAChBA,EAAY,GAAEC,SACdA,GAAW,EAAEC,eACbA,EAAiBtB,EAAKuB,UACtBA,EAAY,GACT,IACH,OAAOd,GAAOf,EAASe,EAAK,CAC3BJ,OACAK,MACAC,OACAC,QACAC,SACAC,gBACAC,YACAC,SACAC,UACAC,WACAC,SACAX,WACAY,YACAC,WACAC,iBACAC,aAEF,CAAA"} \ No newline at end of file diff --git a/src/filesize.js b/src/filesize.js index fc998ee..231d5dd 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -26,7 +26,7 @@ import { /** * Converts a file size in bytes to a human-readable string with appropriate units - * @param {number|bigint} arg - The file size in bytes to convert + * @param {number|string|bigint} arg - The file size in bytes to convert * @param {Object} [options={}] - Configuration options for formatting * @param {boolean} [options.bits=false] - If true, calculates bits instead of bytes * @param {boolean} [options.pad=false] - If true, pads decimal places to match round parameter diff --git a/types/filesize.d.ts b/types/filesize.d.ts index 4b40847..1e7a813 100644 --- a/types/filesize.d.ts +++ b/types/filesize.d.ts @@ -58,7 +58,7 @@ export type FilesizeArray = [number | string, string]; /** * Return type based on output option */ -export type FilesizeReturn = +export type FilesizeReturn = T['output'] extends "object" ? FilesizeObject : T['output'] extends "array" ? FilesizeArray : T['output'] extends "exponent" ? number : @@ -76,7 +76,7 @@ export type FilesizeReturn = * filesize(1024, {output: "object"}) // {value: 1, symbol: "KB", exponent: 1, unit: "KB"} */ export function filesize( - arg: number | bigint, + arg: number | string | bigint, options?: T ): FilesizeReturn; @@ -91,4 +91,4 @@ export function filesize( */ export function partial( options?: T -): (arg: number | bigint) => FilesizeReturn; \ No newline at end of file +): (arg: number | string | bigint) => FilesizeReturn;