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

handle 0 value durations #259

Merged
merged 2 commits into from
Sep 12, 2024
Merged
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
12 changes: 10 additions & 2 deletions src/data/Duration/Duration.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export default {

export const Simple = () => (
<Fragment>
<Duration value="0" />
<br />
<Duration value="1" />
<br />
<Duration value="125" />
<br />
<Duration value="256" />
Expand All @@ -20,13 +24,17 @@ export const Simple = () => (
</Fragment>
);

export const CustomZero = () => <Duration value="0" zeroValue="No value" />;

export const Empty = () => (
<Fragment>
<Duration value={null} />
<br />
<Duration value={undefined} />
<br />
<Duration value={null} emptyValue="Nothing to see" />
<br />
</Fragment>
);

export const CustomEmpty = () => (
<Duration value={null} emptyValue="Nothing to see" />
);
13 changes: 10 additions & 3 deletions src/data/Duration/Duration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ export interface DurationProps {
* If the value is undefined/null it will return this value.
*/
emptyValue?: string;

/**
* If the value is 0 it will return this value. Default to '0 ms'
*/
zeroValue?: string;
}

export const Duration: FC<DurationProps> = ({ value, emptyValue = 'N/A' }) => (
<>{formatDuration(value, emptyValue)}</>
);
export const Duration: FC<DurationProps> = ({
value,
emptyValue = 'N/A',
zeroValue = '0 ms'
}) => <>{formatDuration(value, emptyValue, zeroValue)}</>;
10 changes: 8 additions & 2 deletions src/data/Duration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const timeScale = new humanFormat.Scale({
month: 2592000 * 1000
});

export function formatDuration(time: DurationFormatTypes, emptyValue = 'N/A') {
export function formatDuration(
time: DurationFormatTypes,
emptyValue = 'N/A',
zeroValue = '0 ms'
) {
let newTime = time;

// Try and tease out the different formats users might pass
Expand All @@ -27,7 +31,9 @@ export function formatDuration(time: DurationFormatTypes, emptyValue = 'N/A') {
const [valueStr, unitStr] = humanized.split(' ');

const value = parseFloat(valueStr);
if (value === 1) {
if (value === 0) {
return zeroValue;
} else if (value === 1) {
return humanized;
}

Expand Down
Loading