Skip to content
Advertisement

Parse human-readable filesizes into bytes

JavaScript

I want to convert all this strings into bytes. So far I came up with this:

JavaScript

But I don’t like it and also I don’t think it works. I could find only modules that do the opposite thing.

Advertisement

Answer

Here’s a slightly prettier version. There’s probably no module for this, just define the function inline. It’s very small and readable.

JavaScript

(Note that different places use slightly different conventions for the definitions of KB, MB, etc — either using powers of 10**3 = 1000 or powers of 2**10 = 1024. If your context is Windows, you will want to use the latter. If your context is Mac OS, you will want to use the former.)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement