Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit ddfb11e

Browse files
committed
Clean up byte string conversion. Pretty sure that bytes are genrally accepted to be multiples of 1024.
1 parent 5740203 commit ddfb11e

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
8+
[*]
9+
10+
indent_style = space
11+
indent_size = 2
12+
end_of_line = lf
13+
charset = utf-8
14+
trim_trailing_whitespace = true
15+
insert_final_newline = true
16+
17+
[*.md]
18+
trim_trailing_whitespace = false
19+
20+
[Makefile]
21+
indent_style = tab
22+
indent_size = 4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# Number to Bytesize
1+
# Byte String
22

33
Convert a number to a string that represents a rounded size in bytes.
44

55
## Example
66

77
```
8-
f(156833213) // => '156.83 MB'
9-
f('8101') // => '8.1 kB'
10-
f(12331, 3) // => '12.331 kB'
8+
f(156833213) // => "149.57 MB"
9+
f(8101) // => "7.91 KB"
10+
f(12331, 3) // => "12.042 KB"
1111
```
1212

13+
## Source
14+
1315
By [Riga](https://github.com/riga).

byte-string/byte-string.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var byteString = function (bytes, precision) {
2+
var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
3+
factor = Math.pow(10, precision > 0 ? precision : 2);
4+
// Using a for loop since it's perfect for this kind of problem
5+
for (var i = bytes, k = 0; i >= 1024 && k < suffixes.length; i /= 1024, k++);
6+
// Return the number rounded to precision
7+
return (Math.round(i * factor) / factor) + ' ' + suffixes[k];
8+
};

number-to-bytesize/number-to-bytesize.js

-24
This file was deleted.

0 commit comments

Comments
 (0)