npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

filesize-utils

v1.0.1

Published

关于文件大小的工具类函数

Downloads

5

Readme

关于filesize-utils

关于文件大小的单位统一大写,目前仅支持以下几个单位:

['B', 'KB', 'MB', 'GB', 'TB', 'PB']

说明

这是一个帮助文件大小处理的一些工具类

API

1.getFileSize(file)

file: File文件对象

获取file文件的大小(没错就是file.size这么简单,别管我就喜欢套一层)。

2.formatSize(size, unit, options)

size: 文件大小(number)

unit: 文件大小单位(string,参考上面)

options:配置对象(object)

  • targetUnit(string):可以选择你想要转换的目标单位。
  • precision(number):精度,可以控制小数位数。
  • format('string' | 'array' | 'object'):返回格式,

如果传入'string',则会返回'25 KB'

如果传入'array',则会返回[25, 'KB']

如果传入'object',则会返回{ size: 25, unit: 'KB' }

3.compareSize(file1, file2):比较两个不同单位文件大小

file1,file2(string):文件大小,例如'5.2KB'

返回file1 > file2 的结果(boolean)

4.isOverSize(size, max):判断文件大小是否超过阈值max

size(string):文件大小,例如'5.2KB'

max(string):阈值max,例如'2GB'

返回 size > max 的结果(boolean)

例如:FileSizeUtils.isOverSize('1200KB', '1MB')

5.validateSize(size, range):判断文件大小是否在指定的范围内

size(string):文件大小,例如'5.2KB'

range(Array):文件大小范围,例如['2MB', '5MB']

返回 size 是否在range的范围内 的结果(boolean)

例如:FileSizeUtils.validateSize('1200KB', ['2MB', '5MB'])

使用方法

import FileSizeUtils from 'filesize-utils'

...

FileSizeUtils.getFileSize(file)

FileSizeUtils.formatSize(size, unit, options)

FileSizeUtils.compareSize(file1, file2)

FileSizeUtils.isOverSize(size, max)

FileSizeUtils.validateSize(size, range)