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

fix: Significantly improve Duration types #1338

Merged
merged 5 commits into from
Jan 17, 2021
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
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
}