Skip to content

Commit

Permalink
Use spaceSeparated to format bytes
Browse files Browse the repository at this point in the history
Fix #411
  • Loading branch information
BenjaminVanRyseghem committed Jun 17, 2020
1 parent 611117c commit 9d236af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Next

- Introduce `lowPrecision` to tweak precision when computing average value. Thanks @BenjaminVanRyseghem
- Fix #411: spaceSeparated is not working for bytes. Thanks @BenjaminVanRyseghem

### 2.3.0

Expand Down
7 changes: 5 additions & 2 deletions src/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ function getFormatByteUnits(value, suffixes, scale) {
*/
function formatByte(instance, providedFormat, state, numbro) {
let base = providedFormat.base || "binary";
let options = Object.assign({}, defaultOptions, providedFormat);

const { binarySuffixes: localBinarySuffixes, decimalSuffixes: localDecimalSuffixes } = state.currentBytes();

const localBytes = {
Expand All @@ -211,14 +213,15 @@ function formatByte(instance, providedFormat, state, numbro) {
let baseInfo = localBytes[base];

let { value, suffix } = getFormatByteUnits(instance._value, baseInfo.suffixes, baseInfo.scale);

let output = formatNumber({
instance: numbro(value),
providedFormat,
state,
defaults: state.currentByteDefaultFormat()
});
let abbreviations = state.currentAbbreviations();
return `${output}${abbreviations.spaced ? " " : ""}${suffix}`;

return `${output}${options.spaceSeparated ? " " : ""}${suffix}`;
}

/**
Expand Down
43 changes: 31 additions & 12 deletions tests/src/formatting-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,31 +462,37 @@ describe("formatting", () => {
expect(getFormatByteUnits).toHaveBeenCalledWith(value, bytes.binary.suffixes, bytes.binary.scale);
});

it("separates the suffix with a space when `spaced` flag is true", () => {
it("separates the suffix with a space when `spaceSeparated` flag is true", () => {
let instance = jasmine.createSpy("instance");
let state = jasmine.createSpyObj("state", ["currentByteDefaultFormat", "currentAbbreviations", "currentBytes"]);
state.currentAbbreviations.and.returnValue({spaced: true});
let format = {
spaceSeparated: true
};
state.currentAbbreviations.and.returnValue({});
state.currentBytes.and.returnValue({
binarySuffixes: bytes.binary.suffixes
});
getFormatByteUnits.and.returnValue({suffix: "B"});
formatNumber.and.returnValue("2");

let result = formatByte(instance, {}, state, numbroStub);
let result = formatByte(instance, format, state, numbroStub);
expect(result).toMatch(/ /);
});

it("does not separate the suffix with a space when `spaced` flag is false", () => {
it("does not separate the suffix with a space when `spaceSeparated` flag is false", () => {
let instance = jasmine.createSpy("instance");
let state = jasmine.createSpyObj("state", ["currentByteDefaultFormat", "currentAbbreviations", "currentBytes"]);
state.currentAbbreviations.and.returnValue({spaced: false});
let format = {
spaceSeparated: false
};
state.currentAbbreviations.and.returnValue({});
state.currentBytes.and.returnValue({
binarySuffixes: bytes.binary.suffixes
});
getFormatByteUnits.and.returnValue({suffix: "B"});
formatNumber.and.returnValue("2");

let result = formatByte(instance, {}, state, numbroStub);
let result = formatByte(instance, format, state, numbroStub);
expect(result).not.toMatch(/ /);
});

Expand Down Expand Up @@ -765,15 +771,14 @@ describe("formatting", () => {
expect(result).toMatch(/ /);
});

it("does not separate the suffix with a space when `spaced` flag is false", () => {
it("does not separate the suffix with a space when `spaceSeparated` flag is false", () => {
let value = jasmine.createSpy("value");
let providedFormat = jasmine.createSpy("providedFormat");
let ordinalFn = jasmine.createSpy("ordinalFn").and.returnValue("nd");

let state = jasmine.createSpyObj("state", ["currentOrdinal", "currentAbbreviations", "currentOrdinalDefaults"]);
state.currentOrdinal.and.returnValue(ordinalFn);
state.currentAbbreviations.and.returnValue({spaced: false});

providedFormat.spaceSeparated = false;
formatNumber.and.returnValue("2");

let instance = numbroStub(value);
Expand Down Expand Up @@ -869,7 +874,7 @@ describe("formatting", () => {
});
});

it("separates the percent sign with a space when `spaced` flag is true", () => {
it("separates the percent sign with a space when `spaceSeparated` flag is true", () => {
let value = jasmine.createSpy("value");
let providedFormat = jasmine.createSpy("providedFormat");
let instance = numbroStub(value);
Expand All @@ -886,18 +891,19 @@ describe("formatting", () => {
expect(result).toMatch(/ /);
});

it("does not separate the percent sign with a space when `spaced` flag is false", () => {
it("does not separate the percent sign with a space when `spaceSeparated` flag is false", () => {
let value = jasmine.createSpy("value");
let providedFormat = jasmine.createSpy("providedFormat");
let instance = numbroStub(value);
let state = jasmine.createSpyObj("state", ["currentAbbreviations", "currentPercentageDefaults"]);
state.currentAbbreviations.and.returnValue({spaced: false});
state.currentAbbreviations.and.returnValue({});

let result = formatPercentage(instance, providedFormat, state, numbroStub);

expect(result).not.toMatch(/ /);

providedFormat.prefixSymbol = true;
providedFormat.spaceSeparated = false;
result = formatPercentage(instance, providedFormat, state, numbroStub);

expect(result).not.toMatch(/ /);
Expand Down Expand Up @@ -2841,5 +2847,18 @@ describe("formatting", () => {

expect(result).toEqual("-1.234");
});

it("Issue 411", () => {
let value = 2000;
let format = {
spaceSeparated: true,
output: "byte",
base: "decimal",
mantissa: 1
};
let result = formatting.format(numbroStub(value), format);

expect(result).toEqual("2.0 KB");
});
});
});

0 comments on commit 9d236af

Please sign in to comment.