β¬ οΈ Back to Table of Contents
π useTimeAgo¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 15 |
| π¦ Imports | 9 |
| π Variables & Constants | 2 |
| π’ Vue Composition API | 1 |
| π Interfaces | 4 |
| π Type Aliases | 4 |
π Table of Contents¶
π οΈ File Location:¶
π packages/core/useTimeAgo/index.ts
π¦ Imports¶
| Name | Source |
|---|---|
AnyFn |
@vueuse/shared |
Pausable |
@vueuse/shared |
ComputedRef |
vue |
MaybeRefOrGetter |
vue |
ConfigurableScheduler |
../_configurable |
useIntervalFn |
@vueuse/shared |
computed |
vue |
toValue |
vue |
useNow |
../useNow |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
DEFAULT_UNITS |
UseTimeAgoUnit<UseTimeAgoUnitNamesDef... |
const | [ { max: 60000, value: 1000, name: 'second' }, { max: 2760000, value: 60000, ... |
β |
DEFAULT_MESSAGES |
UseTimeAgoMessages<UseTimeAgoUnitName... |
const | { justNow: 'just now', past: n => /\d/.test(n) ?${n} ago: n, future: n =>... |
β |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
computed |
computed | none | none |
Functions¶
useTimeAgo(time: MaybeRefOrGetter<Date | number | string>, options: UseTimeAgoOptions<false, UnitNames>): UseTimeAgoReturn<false>¶
Reactive time ago formatter.
See: https://vueuse.org/useTimeAgo
Tags: @__NO_SIDE_EFFECTS__
Raw JSDoc
Code
formatTimeAgo(from: Date, options: FormatTimeAgoOptions<UnitNames>, now: Date | number): string¶
Parameters:
fromDateoptionsFormatTimeAgoOptions<UnitNames>nowDate | number
Returns: string
Calls:
n.toFixedMath.absroundFngetValueapplyFormatformatterformatter.replaceval.toStringfullDateFormatterunits.findunits.entriesformat
Internal Comments:
Code
export function formatTimeAgo<UnitNames extends string = UseTimeAgoUnitNamesDefault>(from: Date, options: FormatTimeAgoOptions<UnitNames> = {}, now: Date | number = Date.now()): string {
const {
max,
messages = DEFAULT_MESSAGES as UseTimeAgoMessages<UnitNames>,
fullDateFormatter = DEFAULT_FORMATTER,
units = DEFAULT_UNITS as UseTimeAgoUnit<UnitNames>[],
showSecond = false,
rounding = 'round',
} = options
const roundFn = typeof rounding === 'number'
? (n: number) => +n.toFixed(rounding)
: Math[rounding]
const diff = +now - +from
const absDiff = Math.abs(diff)
function getValue(diff: number, unit: UseTimeAgoUnit<UnitNames>) {
return roundFn(Math.abs(diff) / unit.value)
}
function format(diff: number, unit: UseTimeAgoUnit<UnitNames>) {
const val = getValue(diff, unit)
const past = diff > 0
const str = applyFormat(unit.name as UnitNames, val, past)
return applyFormat(past ? 'past' : 'future', str, past)
}
function applyFormat(name: UnitNames | keyof UseTimeAgoMessagesBuiltIn, val: number | string, isPast: boolean) {
const formatter = messages[name]
if (typeof formatter === 'function')
return formatter(val as never, isPast)
return formatter.replace('{0}', val.toString())
}
// less than a minute
if (absDiff < 60000 && !showSecond)
return messages.justNow
if (typeof max === 'number' && absDiff > max)
return fullDateFormatter(new Date(from))
if (typeof max === 'string') {
const unitMax = units.find(i => i.name === max)?.max
if (unitMax && absDiff > unitMax)
return fullDateFormatter(new Date(from))
}
for (const [idx, unit] of units.entries()) {
const val = getValue(diff, unit)
if (val <= 0 && units[idx - 1])
return format(diff, units[idx - 1])
if (absDiff < unit.max)
return format(diff, unit)
}
return messages.invalid
}
past(n: string): string¶
Parameters:
nstring
Returns: string
future(n: string): string¶
Parameters:
nstring
Returns: string
month(n: number, past: boolean): string¶
Parameters:
nnumberpastboolean
Returns: string
year(n: number, past: boolean): string¶
Parameters:
nnumberpastboolean
Returns: string
day(n: number, past: boolean): string¶
Parameters:
nnumberpastboolean
Returns: string
week(n: number, past: boolean): string¶
Parameters:
nnumberpastboolean
Returns: string
hour(n: number): string¶
Parameters:
nnumber
Returns: string
minute(n: number): string¶
Parameters:
nnumber
Returns: string
second(n: number): string¶
Parameters:
nnumber
Returns: string
DEFAULT_FORMATTER(date: Date): string¶
Parameters:
dateDate
Returns: string
Calls:
date.toISOString().slice
Internal helpers¶
Declared inside another function in this file.
getValue(diff: number, unit: UseTimeAgoUnit<UnitNames>): number¶
Parameters:
diffnumberunitUseTimeAgoUnit<UnitNames>
Returns: number
Calls:
roundFnMath.abs
Code
format(diff: number, unit: UseTimeAgoUnit<UnitNames>): string¶
Parameters:
diffnumberunitUseTimeAgoUnit<UnitNames>
Returns: string
Calls:
getValueapplyFormat
Code
applyFormat(name: UnitNames | keyof UseTimeAgoMessagesBuiβ¦, val: number | string, isPast: boolean): string¶
Parameters:
nameUnitNames | keyof UseTimeAgoMessagesBuiltInvalnumber | stringisPastboolean
Returns: string
Calls:
formatterformatter.replaceval.toString
Code
Interfaces¶
UseTimeAgoMessagesBuiltIn¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
justNow |
string |
β | not shown |
past |
string \| UseTimeAgoFormatter<string> |
β | not shown |
future |
string \| UseTimeAgoFormatter<string> |
β | not shown |
invalid |
string |
β | not shown |
FormatTimeAgoOptions<UnitNames extends string = UseTimeAgoUnitNamesDefault>¶
Interface Code
export interface FormatTimeAgoOptions<UnitNames extends string = UseTimeAgoUnitNamesDefault> {
/**
* Maximum unit (of diff in milliseconds) to display the full date instead of relative
*
* @default undefined
*/
max?: UnitNames | number
/**
* Formatter for full date
*/
fullDateFormatter?: (date: Date) => string
/**
* Messages for formatting the string
*/
messages?: UseTimeAgoMessages<UnitNames>
/**
* Minimum display time unit (default is minute)
*
* @default false
*/
showSecond?: boolean
/**
* Rounding method to apply.
*
* @default 'round'
*/
rounding?: 'round' | 'ceil' | 'floor' | number
/**
* Custom units
*/
units?: UseTimeAgoUnit<UnitNames>[]
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
max |
UnitNames \| number |
β | not shown |
fullDateFormatter |
(date: Date) => string |
β | not shown |
messages |
UseTimeAgoMessages<UnitNames> |
β | not shown |
showSecond |
boolean |
β | not shown |
rounding |
'round' \| 'ceil' \| 'floor' \| number |
β | not shown |
units |
UseTimeAgoUnit<UnitNames>[] |
β | not shown |
UseTimeAgoOptions<Controls extends boolean, UnitNames extends string = UseTimeAgoUnitNamesDefault>¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
controls |
Controls |
β | not shown |
UseTimeAgoUnit<Unit extends string = UseTimeAgoUnitNamesDefault>¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
max |
number |
β | not shown |
value |
number |
β | not shown |
name |
Unit |
β | not shown |
Type Aliases¶
UseTimeAgoFormatter<T = number>¶
UseTimeAgoUnitNamesDefault¶
UseTimeAgoMessages<UnitNames extends string = UseTimeAgoUnitNamesDefault>¶
type UseTimeAgoMessages<UnitNames extends string = UseTimeAgoUnitNamesDefault> = UseTimeAgoMessagesBuiltIn
& Record<UnitNames, string | UseTimeAgoFormatter<number>>;
UseTimeAgoReturn<Controls extends boolean = false>¶
type UseTimeAgoReturn<Controls extends boolean = false> = Controls extends true ? { timeAgo: ComputedRef<string> } & Pausable : ComputedRef<string>;
Generated by Syntax Scribe