avoidwork / filesize.js Goto Github PK
View Code? Open in Web Editor NEWJavaScript library to generate a human readable String describing the file size
Home Page: https://filesizejs.com
License: BSD 3-Clause "New" or "Revised" License
JavaScript library to generate a human readable String describing the file size
Home Page: https://filesizejs.com
License: BSD 3-Clause "New" or "Revised" License
Thanks for the library! We've been using the ES6 file from the bower distribution, but now have switched to the NPM one where only an ES5 file is available. Perhaps you could also include the ES6 one?
First, thanks for this great library. :-)
I'm running 1.10.0 and if I execute:
filesize(499418224640)
I get "4.00 Tb"
Pretty sure the answer should be "499.42 GB" or am I missing something? I really hope I'm just being stupid here.
My Possibly Flawed-Logic:
499 418 224 640
GB MB KB B
Edit:
Okay, it just dawned on me that it's almost exactly 8x what it's supposed to be. 8 bits in a byte. Also, it gave it in Tb not TB. Hhmmmm... ok. So, I ran:
filesize(499418224640,2,false)
and I get the proper value: "499.42 GB". Why would it decide that I'm passing it bits and not bytes? Seems like anything over the integer: 124999999999 causes it to go into some sort of "bit" mode. Is this by design?
Example:
filesize(124999999999) // "125.00 GB"
filesize(125000000000) // "1.00 Tb"
Either way, I've got a work around for now, but, this seems a bit arbitrary (pun not intended, haha).
Using the gh-pages
branch to serve as a CDN is fine enough. The raw.github.com
is not suitable for serving JavaScript files for browsers (especially IE) directly.
Thanks!
require('filesize')(-4)
returns an empty string. Should return -4.00B
.
I am using the 3.4.2 version of package. I've got an issue while formatting bits. Please see the code and the output below.
filesize(124, {bits: true, base: 10}) // "992 b"
filesize(125, {bits: true, base: 10}) // "1000 b" => I would expect "1 kb"
filesize(126, {bits: true, base: 10}) // "1.01 kb"
Same goes for other thousands:
filesize(125000, {bits: true, base: 10}) // "1000 kb" => I would expect "1 Mb"
filesize(125000000, {bits: true, base: 10}) // "1000 Mb" => I would expect "1 Gb"
Hope it does not require much time to fix this. Thank you.
version: [email protected]
> filesize(1/8, { bits: true })
'1024 '
> filesize(1024*1024*1024*1024*1024*1024*1024*1024*1024, { bits: true })
'8 '
> filesize(1, { bits: true })
'8 b'
> filesize(1024*1024*1024*1024*1024*1024*1024*1024, { bits: true })
'8 Yb'
>
Hello,
Just want to report that the SSL cert for the URL https://cdn.filesizejs.com/filesize.min.js is invalid.
It would be nice to have an option to specify that one wants to see the size just in bytes.
On Windows it's not common to see filesizes in bits.
For example, I have a file which is 265318 bytes and it translates to 2.02Mb, which is correct, but I would like to have it translated to 259.10KB.
Would it be possible to have this option? :)
Hi, I'm trying to reduce our dep size in yeoman and was wondering if you could put these paths into .npmignore? Since they're not needed.
lib/filesize.min.js
src
test
.jamignore
.travis.yml
Gruntfile.js
var bytes = 197621805,
size = filesize(bytes);
// size is '1.47 Gb'
Shouldn't this output be '188.47 MB'? Why is it spitting out the value in gigabits?
I can see that your library supports some localization via separator
, symbols
and fullforms
.
I guess, for Russian it would be something like:
filesize(..., {
separator: ',',
symbols: {
B: "Б",
KB: "кБ",
MB: "мБ",
...
},
fullforms: [
'',
'кило',
'мега',
...
]
})
You could make a directory called locales
where people could submit pull requests with their languages.
import ru from 'filesize/locales/ru'
filesize(..., { locale: ru })
A similar API:
https://github.com/sindresorhus/pretty-bytes#readme
// Localized output using German locale
prettyBytes(1337, {locale: 'de'});
//=> '1,34 kB'
Notice how they also use ,
in that example.
That's because they use number.toLocaleString()
instead of separator
.
They should have also checked for number.toLocaleString
existence before using it and then fall back to a default separator.
They also don't have kB
translated.
These are localization API ideas you might want to review in some future if you decide on adding localization to this library.
I don't need localization, I'm using en-US
.
2014.12.17: Now that SSL is encouraged for everyone and doesn’t have performance concerns, this technique is now an anti-pattern. If the asset you need is available on SSL, then always use the https:// asset.
-- Paul Irish
The library is cool, but I had issues using it over the https. I've ended up using rawgit: https://cdn.rawgit.com/avoidwork/filesize.js/3.1.2/lib/filesize.js
It would be nice to have a https link to the lib in the documentation
Looks like there is no support for multiple languages.
Maybe it's good to have one?
I see the only way of solving it – very inelegant corner case management.
let res = parseFloat((bytes / (1024 ** pow)).toFixed(precision));
if (res === 1024) {
res = 1;
pow++;
}
You are currently doing:
if (typeof define === 'function')
While it should be:
if (typeof define === 'function' && define.amd)
Otherwise it breaks, e.g. in Ember.js.
is it possible to make filesize(3768003) output in kilobytes and not mb?
I have noticed library has been updated from v4.0.0
to v4.1.1
but I can't find changelog for that. It could be nice to write some strings about changes (different file or maybe inside github releases page).
Right now it's a little bit hard to maintain TS definitions for this library when you don't know about changes.
All the versions >= 5.0
are broken in browsers that don't support ES6 because of this line:
Line 9 in 3a088db
It may potentially break even evergreen browsers because target: "esnext"
doesn't transpile ESNext code at all which means output may contain unstable ES features (stage-4) which are not yet supported by the browsers natively.
Hi, thanks for adding typescript types!
It seems that they aren't correct, making this unusable in Typescript when type checking is enabled.
export function filesize(arg: any, descriptor: object): any;
export interface filesize {
partial: any;
}
Usually we want to avoid any
in TypeScript, it basically means "ignore my type" to TypeScript.
Here's what I had before, from the @types
repo:
// Type definitions for filesize 4.2
// Project: https://github.com/avoidwork/filesize.js, https://filesizejs.com
// Definitions by: Giedrius Grabauskas <https://github.com/GiedriusGrabauskas>
// Renaud Chaput <https://github.com/renchap>
// Roman Nuritdinov <https://github.com/Ky6uk>
// Sam Hulick <https://github.com/ffxsam>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare var fileSize: Filesize.Filesize;
export = fileSize;
export as namespace filesize;
declare namespace Filesize {
interface SiJedecBits {
b?: string;
Kb?: string;
Mb?: string;
Gb?: string;
Tb?: string;
Pb?: string;
Eb?: string;
Zb?: string;
Yb?: string;
}
interface SiJedecBytes {
B?: string;
KB?: string;
MB?: string;
GB?: string;
TB?: string;
PB?: string;
EB?: string;
ZB?: string;
YB?: string;
}
type SiJedec = SiJedecBits & SiJedecBytes & { [name: string]: string };
interface Options {
/**
* Number base, default is 2
*/
base?: number;
/**
* Enables bit sizes, default is false
*/
bits?: boolean;
/**
* Specifies the SI suffix via exponent, e.g. 2 is MB for bytes, default is -1
*/
exponent?: number;
/**
* Enables full form of unit of measure, default is false
*/
fullform?: boolean;
/**
* Array of full form overrides, default is []
*/
fullforms?: string[];
/**
* BCP 47 language tag to specify a locale, or true to use default locale, default is ""
*/
locale?: string | boolean;
/**
* ECMA-402 number format option overrides, default is "{}"
*/
localeOptions?: Intl.NumberFormatOptions;
/**
* Output of function (array, exponent, object, or string), default is string
*/
output?: "array" | "exponent" | "object" | "string";
/**
* Decimal place, default is 2
*/
round?: number;
/**
* Decimal separator character, default is `.`
*/
separator?: string;
/**
* Character between the result and suffix, default is ` `
*/
spacer?: string;
/**
* Standard unit of measure, can be iec or jedec, default is jedec; can be overruled by base
*/
standard?: "iec" | "jedec";
/**
* Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
*/
symbols?: SiJedec;
/**
* Enables unix style human readable output, e.g ls -lh, default is false
*/
unix?: boolean;
}
interface Filesize {
(bytes: number, options?: Options): string;
partial: (options: Options) => ((bytes: number) => string);
}
}
Source: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/filesize/index.d.ts
Hi, it seems like https is working, but that Cloudflare can't reach the backend.
In src/intro.js, the line:
bits: [ "B", "kb", "Mb", ... ],
... should be:
bits: [ "b", "kb", "Mb", ... ],
This way, one bit will be described as "1 b" and not "1 B", which looks like one byte.
Thanks for amazing work!
One thing this module misses is backward converting from human readable format to bytes. Like so
filesize('1KB')
// => 1024
Can't seem to get this. I like the output of filesize(num, true);
but I want to take it 2 decimal places...can I do this?
Let's consider scenario:
> filesize(100000000, { exponent: 2 })
'95.37 MB'
> filesize(0, { exponent: 2 })
'0 B'
In second call you can see that even if exponent
option has been passed a result is returned in bytes.
The International System of Units, or SI,[19] and the style guide on the English-language
Wikipedia[20] recommend a (non-breakable, thin) space between a number and its
units, as well as between units in the case of compound units, but never between the
prefix of an SI unit and the unit.45 kg not 45kg or 45 k g
source: http://en.wikipedia.org/wiki/Space_%28punctuation%29#Spaces_and_unit_symbols
For formatting the data size with base 2, the units need to be kiB
, MiB
, GiB
instead of kB
, MB
, GB
according to IEC standards.
The certificate for filesizejs.com and cdn.filesizejs.com expired on 26 November.
Steps to reproduce:
Looks like with the recent version bump v3.5.8 filesize is breaking my local branch for the videomail app.
Does not happen when I revert back to the previous version. Using browserify here to require it. Can you look at this?
Hi,
I checked the document of filesize.js about converting to localized number but I couldn't find any options. Does filesize.js support converting to localized number ? For example in UK they use dot
as decimal separator while some other countries they use comma
The following case is not tested:
if (e > 8) {
e = 8;
}
After using of the package in compression job (joining list of files to single assert, like UglifyJS) "use strickt" will be placed in global scope.
For providing isolation of libraries scope better to move "use strict" usage into function's block:
(function (global) {
"use strict";
// code...
})(typeof window !== "undefined" ? window : global);
I suggest to create right-crafted localization javascript files to choose from (or better detect automatically), something like filesize.ru.js
with following content:
{
"B": "Б",
"kB": "кБ",
"MB": "МБ",
"GB": "ГБ",
"TB": "ТБ",
"PB": "ПБ",
"EB": "ЭБ",
"ZB": "ЗБ",
"YB": "ЙБ"
}
...but with right context.
And call to PR new languages.
http://stackoverflow.com/questions/28932/best-javascript-compressor
Personally I find it a lot easier and faster to use.
I can make a pull request if it's ok.
It would be nicer to have an option to generate K
, M
, G
, instead of KB
, MG
, and GB
like ls -h
It could also benefits some scenarios requiring perfect number alignment.
Great work!
I just noticed that the library has had a major update. Has there been any breaking changes? A changelog would be nice so that I can determine the changes required on my end.
How about converting strings like this
"16M", "16 M", "256KB", "256 KB"
and others to bytes? (revert functionality)
I see MB,GB etc....all in capital
but kb is kB (k small)
This is not ideal.
https://github.com/avoidwork/filesize.js/blob/f7a059f/test/filesize_test.js#L22
this.kilobit = 500;
// ...
test.equal(filesize(this.kilobit, {bits :true}), "3.91 Kb", "Should be '3.91 Kb'");
https://github.com/avoidwork/filesize.js/blob/f7a059f/README.md
filesize(500, {bits: true}); // "4 kb"
Discrepancies:
See also #69
The intro.js
code is broken - missing });
at the end of file.
(function (global) {
const b = /^(b|B)$/;
const si = {
bits: ["b", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
bytes: ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
};
Since today I cant access the CDN pointing to the filesize.js file neither the main website. I getting a ERR_CONNECTION_REFUSED error.
What happen?
wat
At version 3.1.5
When I do
filesize(1023, {
round: 0
})
it returns 1 kB
but I expect 1023 B
... is this a bug or am I missing an option?
You many have considered this and decided scaling by powers of 10 was just the human thing to do....
But its not how computers work.
hello.
I wrote a converter: human size -> bytes.
I propose add the information to your readme.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.