Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
feat(formatters): Exposed formatted display unit in DtFormattedValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oriol Barcelona authored and ffriedl89 committed Jun 9, 2020
1 parent 69a0d68 commit 4a38142
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions libs/barista-components/formatters/src/formatted-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,39 @@ export class DtFormattedValue {
return this._formattedData;
}

/** Formatted display unit composed by unit and rate unit */
get formattedDisplayUnit(): string {
const {
displayUnit,
displayWhiteSpace,
displayRateUnit,
} = this._formattedData;

if (displayUnit !== undefined && displayRateUnit !== undefined) {
return `${displayUnit}${displayWhiteSpace ? ' ' : ''}/${displayRateUnit}`;
}
if (displayUnit !== undefined) {
return displayUnit;
}
if (displayRateUnit !== undefined) {
return `/${displayRateUnit}`;
}

return '';
}

/** @return the string as a combination of the display data */
toString(): string {
if (this._formattedData.displayValue === undefined) {
const { displayValue } = this._formattedData;
if (displayValue === undefined) {
return NO_DATA;
}
let text = this._formattedData.displayValue;
if (this._formattedData.displayUnit !== undefined) {
text = `${text} ${this._formattedData.displayUnit}`;
}
if (this._formattedData.displayRateUnit !== undefined) {
text =
this._formattedData.displayUnit !== undefined ||
this._formattedData.displayWhiteSpace === false
? `${text}/${this._formattedData.displayRateUnit}`
: `${text} /${this._formattedData.displayRateUnit}`;

const displayUnit = this.formattedDisplayUnit;
if (displayUnit === '') {
return displayValue;
}

return text;
return `${displayValue} ${displayUnit}`;
}
}

0 comments on commit 4a38142

Please sign in to comment.