-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2640 from NDLANO/chore/replace-pretty-bytes-with-…
…own-implementation chore: replace pretty-bytes with our own implementation
- Loading branch information
Showing
7 changed files
with
76 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) 2024-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { humanFileSize } from "../humanFileSize"; | ||
|
||
describe("humanFileSize", () => { | ||
it("handles bytes", () => { | ||
expect(humanFileSize(250, "nb")).toEqual("250 B"); | ||
}); | ||
|
||
it("handles kilobytes", () => { | ||
expect(humanFileSize(1000, "nb")).toEqual("1 kB"); | ||
}); | ||
|
||
it("handles megabytes", () => { | ||
expect(humanFileSize(1000000, "nb")).toEqual("1 MB"); | ||
}); | ||
|
||
it("handles border values", () => { | ||
expect(humanFileSize(999999, "nb")).toEqual("1 000 kB"); | ||
expect(humanFileSize(1000000, "nb")).toEqual("1 MB"); | ||
expect(humanFileSize(1001000, "nb")).toEqual("1 MB"); | ||
}); | ||
|
||
it("rounds to two decimal places", () => { | ||
expect(humanFileSize(1234567, "nb")).toEqual("1,23 MB"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) 2024-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
// Code taken from https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string/72596863#72596863 | ||
|
||
const UNITS = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte", "petabyte"]; | ||
const BYTES_PER_KB = 1000; | ||
|
||
export const humanFileSize = (sizeBytes: number | bigint, locale: string): string => { | ||
let size = Math.abs(Number(sizeBytes)); | ||
|
||
let u = 0; | ||
while (size >= BYTES_PER_KB && u < UNITS.length - 1) { | ||
size /= BYTES_PER_KB; | ||
++u; | ||
} | ||
|
||
return new Intl.NumberFormat(locale, { | ||
style: "unit", | ||
unit: UNITS[u], | ||
unitDisplay: "short", | ||
maximumFractionDigits: 2, | ||
}).format(size); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters