Skip to content
Open
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
31 changes: 29 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,38 @@ export default function TimeAgo({
? { ...passDownProps, dateTime: dateParser(date).toISOString() }
: passDownProps

const nextFormatter = defaultFormatter.bind(null, value, unit, suffix)
const nextFormatter = (value, unit, suffix) => {
// Use the default formatter if `formatter` is not a valid function or if it doesn't behave correctly
if (typeof formatter !== 'function') {
console.warn(
'[react-timeago] Formatter is not a function. Using default formatter.',
)
return defaultFormatter(value, unit, suffix, then, nextFormatter, now)
}

try {
// Call the formatter to see if it behaves correctly
const result = formatter(value, unit, suffix, then, nextFormatter, now)
// If the result is falsy, fall back to defaultFormatter
if (!result) {
console.warn(
'[react-timeago] Formatter is invalid. Using default formatter.',
)
return defaultFormatter(value, unit, suffix, then, nextFormatter, now)
}
return result
} catch (error) {
console.warn(
'[react-timeago] Formatter is invalid. Using default formatter.',
error,
)
return defaultFormatter(value, unit, suffix, then, nextFormatter, now)
}
}

return (
<Komponent {...spreadProps} title={passDownTitle}>
{formatter(value, unit, suffix, then, nextFormatter, now)}
{nextFormatter(value, unit, suffix)}
</Komponent>
)
}