Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix to default formatter #288

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/utils/formatting-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import moment from "moment"
const NUMBER_LENGTH = 4

const numFormat = d => {
if (d < 1000) {
const abs = Math.abs(d)
if (abs < 1000) {
return d3.format(",.2f")(d)
} else if (d < 1000000) {
return d3.format(",.2s")(d)
} else {
return `${d3.format(",.0f")(Math.round(d / 1000000))}B`
const formatted = d3.format(",.2s")(d)
return formatted.replace("G", "B")
}
}

Expand All @@ -33,9 +33,9 @@ export const isArrayOfObjects = value =>
export const normalizeArrayByValue = collection =>
isArrayOfObjects(collection) ? collection.map(data => data.value) : collection

export function formatDataValue(data) {
export function formatDataValue(data, numAbbr) {
if (typeof data === "number") {
return formatNumber(data)
return formatNumber(data, numAbbr)
} else if (Array.isArray(data)) {
return formatArrayValue(data)
} else if (data instanceof Date) {
Expand All @@ -56,14 +56,16 @@ export function maybeFormatInfinity(data) {
})
}

export function formatNumber(d) {
export function formatNumber(d, abbr) {
if (typeof d !== "number") {
return d
}
const isLong = String(d).length > NUMBER_LENGTH
const formattedHasAlpha = numFormat(d).match(/[a-z]/i)
const isLargeNumber = isLong && formattedHasAlpha
return isLargeNumber ? numFormat(d) : commafy(parseFloat(d.toFixed(2)))
return isLargeNumber && abbr
? numFormat(d)
: commafy(parseFloat(d.toFixed(2)))
}

export function formatArrayValue(data) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/formatting-helpers.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ describe("Formatting Helpers", () => {
})
})
describe("formatNumber", () => {
it("should add commas to large numbers", () => {
expect(Helpers.formatNumber(10000000000)).to.equal("10,000B")
it("should format large numbers as SI", () => {
expect(Helpers.formatNumber(10000000000)).to.equal("10B")
})
it("should round decimenals", () => {
expect(Helpers.formatNumber(3.33333)).to.equal("3.33")
})
})
describe("formatValue", () => {
it("should format large numbers", () => {
expect(Helpers.formatDataValue(10000000000000)).to.equal("10,000,000B")
expect(Helpers.formatDataValue(10000000000000)).to.equal("10T")
})
it("should format dates", () => {
expect(Helpers.formatDataValue(new Date(Date.UTC(2001, 0, 1)))).to.equal(
Expand Down