avoidwork/filesize.js

JavaScript 库,用于生成描述文件大小的可读字符串

资助包维护!
avoidwork

安装: 368

依赖项: 0

建议者: 0

安全: 0

星星: 1,605

观察者: 18

分支: 97

开放问题: 0

语言:JavaScript

6.2.1 2021-04-13 12:45 UTC

README

downloads CDNJS version

filesize.js 提供了一种简单的方法,可以从数字(浮点数或整数)或字符串中获取可读的文件大小字符串。

import {filesize} from "filesize";
filesize(265318, {standard: "jedec"}); // "259.1 KB"

测试

filesize 的测试覆盖率达到了 100%。

--------------|---------|----------|---------|---------|-----------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-----------------------
All files     |     100 |    95.52 |     100 |     100 |                      
 filesize.cjs |     100 |    95.52 |     100 |     100 | 77-78,173,196,199,210
--------------|---------|----------|---------|---------|-----------------------

可选设置

filesize() 接受一个可选的描述对象作为第二个参数,因此您可以自定义输出。

base

(number) 数字基数,默认为 10

bits

(boolean) 启用 bit 大小,默认为 false

exponent

(number) 通过指数指定符号,例如 2 对于基数为 2,默认为 -1

fullform

(boolean) 启用度量单位的完整形式,默认为 false

fullforms

(array) 完整形式覆盖的数组,默认为 []

locale (覆盖 'separator')

(string || boolean) BCP 47 语言标记以指定区域设置,或 true 以使用默认区域设置,默认为 ""

localeOptions (覆盖 'separator',需要字符串作为 'locale' 选项)

(object) 由 ECMA-402 定义(Number.prototype.toLocaleString)的选项字典。需要显式传递区域设置选项作为字符串,否则将被忽略。

output

(string) 函数输出(arrayexponentobjectstring),默认为 string

pad

(boolean) 小数位末尾填充,默认为 false

precision

(number) 设置数值输出的精度,默认为 0

round

(number) 小数位,默认为 2

roundingMethod

(string) 四舍五入方法,可以是 roundfloorceil,默认为 round

separator

(string) 小数分隔符字符,默认为空字符串。

spacer

(string)resultsymbol 之间的字符,默认为 " "

standard

(string) 标准度量单位,可以是 iecjedecsi。默认为 si(基数 10)。si 选项是 jedec 的别名,因此对于其他配置选项无效。

symbols

(object) 用于本地化的 IEC/JEDEC 符号的字典,如果找不到匹配项则默认为英语;SI 自动使用 JEDEC 值处理。

示例

filesize(500);                        // "500 B"
filesize(500, {bits: true});          // "4 kbit"
filesize(265318, {base: 2});          // "259.1 KiB"
filesize(265318);                     // "265.32 kB"
filesize(265318, {round: 0});         // "265 kB"
filesize(265318, {output: "array"});  // [265.32, "kB"]
filesize(265318, {output: "object"}); // {value: 265.32, symbol: "kB", exponent: 1, unit: "kB"}
filesize(1, {symbols: {B: "Б"}});     // "1 Б"
filesize(1024);                       // "1.02 kB"
filesize(1024, {exponent: 0});        // "1024 B"
filesize(1024, {output: "exponent"}); // 1
filesize(265318, {standard: "jedec"});  // "259.1 KB"
filesize(265318, {base: 2, fullform: true}); // "259.1 kibibytes"
filesize(12, {fullform: true, fullforms: ["байтов"]});  // "12 байтов"
filesize(265318, {separator: ","});   // "265,32 kB"
filesize(265318, {locale: "de"});   // "265,32 kB"

部分应用

partial() 接受 filesize() 的第二个参数,并返回一个新的函数,在执行时应用配置。如果您在没有在词法作用域中缓存 descriptor 的情况下调用 filesize(),则可以使用此功能来减少 Object 创建。

import {partial} from "filesize";
const size = partial({standard: "jedec"});

size(265318); // "259.1 KB"

许可证

版权所有 © 2024 Jason Mulligan。在 BSD-3 许可下授权。