Skip to content

Commit

Permalink
refactor(DateTime, Numeric): Improve code readability
Browse files Browse the repository at this point in the history
- Defined a new constant for preset values.
- Reversed ternary operator.
- Used nullish coalescing operator instead of the logical OR operator.
  • Loading branch information
RoachxD committed Aug 12, 2023
1 parent 7e6316e commit 27315d8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/components/DateTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ export interface DateTimeProps extends DateTimeOptions {
export function DateTime(props: Readonly<DateTimeProps>) {
const i18n = useI18n();
const [local, others] = splitProps(props, ['date', 'preset', 'class']);
const dateTimePreset = i18n.presets.dateTime?.[local.preset ?? 'default'];

return (
<span class={local.class}>
{i18n.formatDateTime(local.date, !isEmpty(others)
? others
: i18n.presets.dateTime?.[local.preset || 'default'])}
{i18n.formatDateTime(
local.date,
isEmpty(others) ? dateTimePreset : others
)}
</span>
);
}
8 changes: 5 additions & 3 deletions src/components/Numeric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export function Numeric(props: Readonly<NumericProps>) {

const [local, other] = splitProps(props, ['value', 'preset', 'numberStyle', 'class']);
const options = mergeProps(other, { style: local.numberStyle });
const numberPreset = i18n.presets.number?.[local.preset ?? 'default'];

return (
<span class={local.class}>
{i18n.formatNumber(local.value, !isEmpty(options)
? options
: i18n.presets.number?.[local.preset || 'default'])}
{i18n.formatNumber(
local.value,
isEmpty(options) ? numberPreset : options
)}
</span>
);
}

0 comments on commit 27315d8

Please sign in to comment.