Skip to content

Commit

Permalink
fix: Improve Duration types (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Jan 17, 2021
1 parent fe5f1d0 commit 4aca4b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('Years', () => {
describe('prettyUnit', () => {
const d = dayjs.duration(2, 's')
expect(d.toISOString()).toBe('PT2S')
expect(d.as('Second')).toBe(2)
expect(d.as('seconds')).toBe(2)
expect(d.get('s')).toBe(2)
expect(dayjs.duration({
M: 12,
Expand Down
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ declare namespace dayjs {

export type OptionType = { locale?: string, format?: string, utc?: boolean } | string | string[]

type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'
export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'

type UnitTypeLong = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date'
export type UnitTypeLong = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date'

type UnitTypeLongPlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates'
export type UnitTypeLongPlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates'

export type UnitType = UnitTypeLong | UnitTypeLongPlural | UnitTypeShort;

Expand Down
32 changes: 21 additions & 11 deletions types/plugin/duration.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { PluginFunc } from 'dayjs'
import { OpUnitType, UnitTypeLongPlural } from "../index";

declare const plugin: PluginFunc
export as namespace plugin;
export = plugin

declare namespace plugin {
type DurationInputType = string | number | object
type DurationAddType = number | object | Duration
type DurationUnitsObjectType = Partial<{
[unit in Exclude<UnitTypeLongPlural, "dates"> | "weeks"]: number
}>;
type DurationUnitType = Exclude<OpUnitType, "date" | "dates">
type CreateDurationType =
((units: DurationUnitsObjectType) => Duration)
& ((time: number, unit?: DurationUnitType) => Duration)
& ((ISO_8601: string) => Duration)

interface Duration {
new (input: DurationInputType, unit?: string, locale?: string): Duration
new (input: string | number | object, unit?: string, locale?: string): Duration

clone(): Duration

Expand Down Expand Up @@ -39,13 +46,13 @@ declare namespace plugin {
years(): number
asYears(): number

as(unit: string): number
as(unit: DurationUnitType): number

get(unit: string): number
get(unit: DurationUnitType): number

add(input: DurationAddType, unit? : string): Duration

subtract(input: DurationAddType, unit? : string): Duration
add: CreateDurationType;
subtract: CreateDurationType

toJSON(): string

Expand All @@ -59,10 +66,13 @@ declare namespace plugin {

declare module 'dayjs' {
interface Dayjs {
add(value: plugin.Duration): Dayjs
subtract(value: plugin.Duration): Dayjs
add(duration: plugin.Duration): Dayjs
subtract(duration: plugin.Duration): Dayjs
}

export function duration(input?: plugin.DurationInputType , unit?: string): plugin.Duration
/**
* @param time If unit is not present, time treated as number of milliseconds
*/
export const duration: plugin.CreateDurationType;
export function isDuration(d: any): d is plugin.Duration
}

0 comments on commit 4aca4b1

Please sign in to comment.