Skip to content

Commit

Permalink
Added byteFormat to Localization
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenalin committed Sep 17, 2020
1 parent a8ccf81 commit e7cfb99
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
42 changes: 42 additions & 0 deletions lib/Localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,48 @@ module.exports = class Localization {
)
}

/**
* Byte format or factors of 1024
*
* @param { number } value Value to format
* @param { number } precision Number of decimals
* @param { string } unit Unit
* @param { string } [power] Force to the given power
* @return { string } Value formatted in the powers of 1024
*/
byteFormat (value, precision = 1, unit = 'B', power = null) {
if (isObject(precision)) {
const opts = precision
opts.precision = opts.precision != null ? opts.precision : 1
opts.unit = opts.unit != null ? opts.unit : unit
opts.power = opts.power != null ? opts.power : 1

return this.byteFormat(value, opts.precision, opts.unit, opts.power)
}

const units = ['', 'k', 'M', 'G', 'T', 'P']
let pow = 0

for (let i = 0; i < units.length; i++) {
const d = Math.pow(1024, i)

if (Math.round(value / d) < 1) {
break
}

pow = i
}

if (power && units.includes(power)) {
pow = units.indexOf(power)
}

const v = value / Math.pow(1024, pow)
const f = units[pow]

return `${this.numberFormat(v, precision)} ${f}${unit}`
}

/**
* Localized percent format. Gets the number from numberFormat and appends a
* percent symbol to the return value.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "helpers.js",
"version": "0.15.2",
"version": "0.16.0",
"description": "Miscellaneous helpers",
"main": "index.js",
"scripts": {
Expand Down
34 changes: 34 additions & 0 deletions tests/localization/byteFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const expect = require('expect.js')
const Localization = require('../../lib/Localization')

describe('Localization byteFormat', () => {
const l10n = new Localization()

it('should output the given value appended with unit using the given precision', (done) => {
expect(l10n.byteFormat(20)).to.be('20.0 B')
expect(l10n.byteFormat(20, 2)).to.be('20.00 B')
done()
})

it('should output the given value appended with quantifier using the given precision', (done) => {
expect(l10n.byteFormat(1024, 0)).to.be('1 kB')
expect(l10n.byteFormat(1024 * 1024, 2)).to.be('1.00 MB')
expect(l10n.byteFormat(100 * 1024 * 1024, 2)).to.be('100.00 MB')
expect(l10n.byteFormat(256 * 1024 * 1024 * 1024, 0, 'Hz')).to.be('256 GHz')
done()
})

it('should override power when given', (done) => {
expect(l10n.byteFormat(100 * 1024, 3, 'B', 'M')).to.be('0.098 MB')
expect(l10n.byteFormat(100 * 1024 * 1024, 0, 'Hz', 'k')).to.be('102,400 kHz')
done()
})

it('should accept a configuration object for the parameters', (done) => {
expect(l10n.byteFormat(50, {})).to.be('50.0 B')
expect(l10n.byteFormat(50 * 1024, { power: 'k' })).to.be('50.0 kB')
expect(l10n.byteFormat(100 * 1024, { precision: 3, power: 'M' })).to.be('0.098 MB')
expect(l10n.byteFormat(100 * 1024 * 1024, { precision: 0, unit: 'Hz', power: 'k' })).to.be('102,400 kHz')
done()
})
})

0 comments on commit e7cfb99

Please sign in to comment.