This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
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 #24 from ministryofjustice/feature/refine-file-upload
Feature/refine file upload
- Loading branch information
Showing
40 changed files
with
1,557 additions
and
3,332 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,25 @@ | ||
const bytes = require('bytes') | ||
|
||
const formatBytes = (size) => { | ||
if (typeof size === 'string') { | ||
return size | ||
} | ||
let formattedSize = bytes(size) | ||
formattedSize = formattedSize.replace(/(\d+)\.(\d+)(\w+)/, (m, int, decimal, unit) => { | ||
const unitType = unit.toUpperCase() | ||
const multiplier = unitType === 'KB' ? 100 : 10 | ||
const halfMultiplier = 5 * multiplier | ||
decimal = parseInt((decimal * multiplier + halfMultiplier) / 100, 10) | ||
if (decimal >= 10) { | ||
decimal = '' | ||
int++ | ||
} | ||
if (unitType === 'KB') { | ||
decimal = '' | ||
} | ||
return `${int}${decimal ? `.${decimal}` : ''}${unit}` | ||
}) | ||
return formattedSize | ||
} | ||
|
||
module.exports = formatBytes |
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,26 @@ | ||
const test = require('tape') | ||
|
||
const bytes = require('bytes') | ||
const formatBytes = require('./bytes') | ||
|
||
const getFormattedSize = (size) => { | ||
return formatBytes(bytes(size)) | ||
} | ||
|
||
test('When formatting files sizes', t => { | ||
t.equal(getFormattedSize('19MB'), '19MB', 'it should return exact MB values without decimal points') | ||
t.equal(getFormattedSize('19.77MB'), '19.8MB', 'it should round up MB values to the nearest decimal point') | ||
t.equal(getFormattedSize('19.71MB'), '19.7MB', 'it should round down MB values to the nearest decimal point') | ||
t.equal(getFormattedSize('19.97MB'), '20MB', 'it should round up MB values to the next integer') | ||
t.equal(getFormattedSize('19.03MB'), '19MB', 'it should round down MB values to the next integer') | ||
t.equal(getFormattedSize('19.8KB'), '20KB', 'it should round up KB values to the next integer') | ||
t.equal(getFormattedSize('19.3KB'), '19KB', 'it should round down KB values to the next integer') | ||
t.equal(getFormattedSize('19B'), '19B', 'it should B values exactly') | ||
|
||
t.end() | ||
}) | ||
|
||
test('When formatting files sizes passed as strings', t => { | ||
t.equal(formatBytes('19.71MB'), '19.71MB', 'it should return the value as is') | ||
t.end() | ||
}) |
Oops, something went wrong.