Giter Site home page Giter Site logo

avoidwork / filesize.js Goto Github PK

View Code? Open in Web Editor NEW
1.6K 18.0 98.0 3.14 MB

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

JavaScript 99.81% Shell 0.19%
filesize file filesystem size size-calculation jedec iec bits bytes simple

filesize.js's People

Contributors

abaltuta avatar adroitwhiz avatar aitk avatar avoidwork avatar boris-petrov avatar christopherscott avatar dependabot[bot] avatar extend1994 avatar getsnoopy avatar gitter-badger avatar gurpreetbaidwan avatar laughinghan avatar litvinok avatar lngsx avatar pdehaan avatar realityking avatar rnike avatar ryanramage avatar ryanrhee avatar starius avatar tomoto avatar tomoto-hitachi avatar tschlechtweg avatar vitormil avatar wangxingkai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

filesize.js's Issues

Expose ES6 build in npm

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?

Incorrect Filesize Output

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).

Formatting to bits does not output thousands properly

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.

Possibility to show filesize as byte and not bit

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? :)

npmignore

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

Incorrect Output

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?

Localization API

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.

It would be nice to have a https a link to a cdn

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

Correctly check for AMD loader

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.

Add changelog

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.

Library is broken in non-ES6 browsers

All the versions >= 5.0 are broken in browsers that don't support ES6 because of this line:

"target": "esnext",

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.

broken when using typescript

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

Incorrect bit abbreviation in one case

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.

format

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?

exponent option won't take effect when filesize is 0

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.

Use correct units for base 2

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.

Question: Support localize converting ?

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

"use strict" out of function started to be global after compression

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);

Add localization addons

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.

*-nix ls -h styled filesize support?

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!

Changelog for the major update

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.

Convert filesize to bytes

How about converting strings like this
"16M", "16 M", "256KB", "256 KB"
and others to bytes? (revert functionality)

Intro.js code broken

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"]
};

1023 B != 1 kB

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.