-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(day): module resolution not working
- Loading branch information
Showing
5 changed files
with
173 additions
and
12 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// This file is copied from the source to avoid plugin resolution problem. | ||
|
||
export const SECONDS_A_MINUTE = 60 | ||
export const SECONDS_A_HOUR = SECONDS_A_MINUTE * 60 | ||
export const SECONDS_A_DAY = SECONDS_A_HOUR * 24 | ||
export const SECONDS_A_WEEK = SECONDS_A_DAY * 7 | ||
|
||
export const MILLISECONDS_A_SECOND = 1e3 | ||
export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND | ||
export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND | ||
export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND | ||
export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND | ||
|
||
// English locales | ||
export const MS = 'millisecond' | ||
export const S = 'second' | ||
export const MIN = 'minute' | ||
export const H = 'hour' | ||
export const D = 'day' | ||
export const W = 'week' | ||
export const M = 'month' | ||
export const Q = 'quarter' | ||
export const Y = 'year' | ||
export const DATE = 'date' | ||
|
||
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ' | ||
|
||
export const INVALID_DATE_STRING = 'Invalid Date' | ||
|
||
// Regex | ||
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/ | ||
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import dayjs, { ConfigType, Dayjs } from 'dayjs' | ||
import { relativeTime } from './plugins/RelativeTime' | ||
|
||
export type Day = Dayjs | ||
export type Input = ConfigType | ||
|
||
dayjs.extend(relativeTime) | ||
|
||
export function day(input?: Input): Dayjs { | ||
return dayjs(input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// This file is copied from the source to avoid plugin resolution problem. | ||
|
||
import { PluginFunc } from 'dayjs' | ||
import * as C from '../Constant' | ||
|
||
declare module 'dayjs' { | ||
interface Dayjs { | ||
fromNow(withoutSuffix?: boolean): string | ||
from(compared: ConfigType, withoutSuffix?: boolean): string | ||
toNow(withoutSuffix?: boolean): string | ||
to(compared: ConfigType, withoutSuffix?: boolean): string | ||
} | ||
} | ||
|
||
export interface RelativeTimeOptions { | ||
rounding?: (num: number) => number | ||
thresholds?: RelativeTimeThreshold[] | ||
} | ||
|
||
export interface RelativeTimeThreshold { | ||
l: string | ||
r?: number | ||
d?: string | ||
} | ||
|
||
export const relativeTime: PluginFunc<RelativeTimeOptions> = (o, c, d) => { | ||
o = o || {} | ||
|
||
const proto = c.prototype | ||
|
||
const relObj = { | ||
future: 'in %s', | ||
past: '%s ago', | ||
s: 'a few seconds', | ||
m: 'a minute', | ||
mm: '%d minutes', | ||
h: 'an hour', | ||
hh: '%d hours', | ||
d: 'a day', | ||
dd: '%d days', | ||
M: 'a month', | ||
MM: '%d months', | ||
y: 'a year', | ||
yy: '%d years' | ||
} | ||
|
||
;(d as any).en.relativeTime = relObj | ||
|
||
;(proto as any).fromToBase = (input: any, withoutSuffix: any, instance: any, isFrom: any, postFormat: any) => { | ||
const loc = instance.$locale().relativeTime || relObj | ||
|
||
const T = o.thresholds || [ | ||
{ l: 's', r: 44, d: C.S }, | ||
{ l: 'm', r: 89 }, | ||
{ l: 'mm', r: 44, d: C.MIN }, | ||
{ l: 'h', r: 89 }, | ||
{ l: 'hh', r: 21, d: C.H }, | ||
{ l: 'd', r: 35 }, | ||
{ l: 'dd', r: 25, d: C.D }, | ||
{ l: 'M', r: 45 }, | ||
{ l: 'MM', r: 10, d: C.M }, | ||
{ l: 'y', r: 17 }, | ||
{ l: 'yy', d: C.Y } | ||
] | ||
|
||
const Tl = T.length | ||
|
||
let result | ||
let out | ||
let isFuture | ||
|
||
for (let i = 0; i < Tl; i += 1) { | ||
let t = T[i] | ||
if (t.d) { | ||
result = isFrom | ||
? d(input).diff(instance, (t as any).d, true) | ||
: instance.diff(input, t.d, true) | ||
} | ||
let abs = (o.rounding || Math.round)(Math.abs(result)) | ||
isFuture = result > 0 | ||
if (abs <= (t as any).r || !t.r) { | ||
if (abs <= 1 && i > 0) { t = T[i - 1] } // 1 minutes -> a minute, 0 seconds -> 0 second | ||
const format = loc[t.l] | ||
if (postFormat) { | ||
abs = postFormat(`${abs}`) | ||
} | ||
if (typeof format === 'string') { | ||
out = (format as any).replace('%d', abs) | ||
} | ||
else { | ||
out = format(abs, withoutSuffix, t.l, isFuture) | ||
} | ||
break | ||
} | ||
} | ||
if (withoutSuffix) { return out } | ||
const pastOrFuture = isFuture ? loc.future : loc.past | ||
if (typeof pastOrFuture === 'function') { | ||
return pastOrFuture(out) | ||
} | ||
return pastOrFuture.replace('%s', out) | ||
} | ||
|
||
function fromTo(input: any, withoutSuffix: any, instance: any, isFrom?: any) { | ||
return (proto as any).fromToBase(input, withoutSuffix, instance, isFrom) | ||
} | ||
|
||
proto.to = function (input, withoutSuffix) { | ||
return fromTo(input, withoutSuffix, this, true) | ||
} | ||
|
||
proto.from = function (input, withoutSuffix) { | ||
return fromTo(input, withoutSuffix, this) | ||
} | ||
|
||
const makeNow = (thisDay: any) => (thisDay.$u ? (d as any).utc() : d()) | ||
|
||
proto.toNow = function (withoutSuffix?: boolean) { | ||
return this.to(makeNow(this), withoutSuffix) | ||
} | ||
|
||
proto.fromNow = function (withoutSuffix) { | ||
return this.from(makeNow(this), withoutSuffix) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters