From 73e14e1d0d01b7b86ffe2f2cf3b6339302b00c67 Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong Date: Wed, 6 Jan 2021 15:00:33 +0700 Subject: [PATCH] fix(ui): tmp fixed negative duration (iamkun/dayjs#1317) --- .eslintcache | 2 +- src/models/Datetime.ts | 7 +++++-- src/models/Output.ts | 46 ++++++++++++++++++++++++------------------ 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/.eslintcache b/.eslintcache index b36f6f9..009e802 100644 --- a/.eslintcache +++ b/.eslintcache @@ -1 +1 @@ -[{"/Users/kchantrachir/Desktop/Personal/timing/src/index.tsx":"1","/Users/kchantrachir/Desktop/Personal/timing/src/reportWebVitals.ts":"2","/Users/kchantrachir/Desktop/Personal/timing/src/models/Interval.ts":"3","/Users/kchantrachir/Desktop/Personal/timing/src/models/Logger.ts":"4","/Users/kchantrachir/Desktop/Personal/timing/src/models/Datetime.ts":"5","/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingContainer.tsx":"6","/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingElement.tsx":"7","/Users/kchantrachir/Desktop/Personal/timing/src/pages/Index.tsx":"8","/Users/kchantrachir/Desktop/Personal/timing/src/models/Output.ts":"9","/Users/kchantrachir/Desktop/Personal/timing/src/components/Glassily.tsx":"10","/Users/kchantrachir/Desktop/Personal/timing/src/components/Footer.tsx":"11","/Users/kchantrachir/Desktop/Personal/timing/src/components/Header.tsx":"12","/Users/kchantrachir/Desktop/Personal/timing/src/models/SpecialDay.ts":"13"},{"size":939,"mtime":1609828259581,"results":"14","hashOfConfig":"15"},{"size":425,"mtime":1609748895454,"results":"16","hashOfConfig":"15"},{"size":1763,"mtime":1609910780082,"results":"17","hashOfConfig":"15"},{"size":577,"mtime":1609753155175,"results":"18","hashOfConfig":"15"},{"size":2331,"mtime":1609858513802,"results":"19","hashOfConfig":"15"},{"size":1661,"mtime":1609911326188,"results":"20","hashOfConfig":"15"},{"size":1367,"mtime":1609906997189,"results":"21","hashOfConfig":"15"},{"size":1613,"mtime":1609911467345,"results":"22","hashOfConfig":"15"},{"size":3114,"mtime":1609911273282,"results":"23","hashOfConfig":"15"},{"size":661,"mtime":1609906892662,"results":"24","hashOfConfig":"15"},{"size":554,"mtime":1609844234258,"results":"25","hashOfConfig":"15"},{"size":343,"mtime":1609834862066,"results":"26","hashOfConfig":"15"},{"size":2803,"mtime":1609910410275,"results":"27","hashOfConfig":"15"},{"filePath":"28","messages":"29","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"9qdc2m",{"filePath":"30","messages":"31","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"32","messages":"33","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"34","messages":"35","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"36","messages":"37","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"38","messages":"39","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"42","messages":"43","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"44","messages":"45","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"46","messages":"47","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"48","messages":"49","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"50","messages":"51","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"52","messages":"53","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/kchantrachir/Desktop/Personal/timing/src/index.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/reportWebVitals.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Interval.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Logger.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Datetime.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingContainer.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingElement.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/pages/Index.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Output.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/Glassily.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/Footer.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/Header.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/SpecialDay.ts",[]] \ No newline at end of file +[{"/Users/kchantrachir/Desktop/Personal/timing/src/index.tsx":"1","/Users/kchantrachir/Desktop/Personal/timing/src/reportWebVitals.ts":"2","/Users/kchantrachir/Desktop/Personal/timing/src/models/Interval.ts":"3","/Users/kchantrachir/Desktop/Personal/timing/src/models/Logger.ts":"4","/Users/kchantrachir/Desktop/Personal/timing/src/models/Datetime.ts":"5","/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingContainer.tsx":"6","/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingElement.tsx":"7","/Users/kchantrachir/Desktop/Personal/timing/src/pages/Index.tsx":"8","/Users/kchantrachir/Desktop/Personal/timing/src/models/Output.ts":"9","/Users/kchantrachir/Desktop/Personal/timing/src/components/Glassily.tsx":"10","/Users/kchantrachir/Desktop/Personal/timing/src/components/Footer.tsx":"11","/Users/kchantrachir/Desktop/Personal/timing/src/components/Header.tsx":"12","/Users/kchantrachir/Desktop/Personal/timing/src/models/SpecialDay.ts":"13"},{"size":939,"mtime":1609828259581,"results":"14","hashOfConfig":"15"},{"size":425,"mtime":1609748895454,"results":"16","hashOfConfig":"15"},{"size":1763,"mtime":1609910780082,"results":"17","hashOfConfig":"15"},{"size":577,"mtime":1609753155175,"results":"18","hashOfConfig":"15"},{"size":2644,"mtime":1609919748129,"results":"19","hashOfConfig":"15"},{"size":1661,"mtime":1609919984867,"results":"20","hashOfConfig":"15"},{"size":1367,"mtime":1609906997189,"results":"21","hashOfConfig":"15"},{"size":1613,"mtime":1609911467345,"results":"22","hashOfConfig":"15"},{"size":3587,"mtime":1609919931046,"results":"23","hashOfConfig":"15"},{"size":661,"mtime":1609906892662,"results":"24","hashOfConfig":"15"},{"size":554,"mtime":1609844234258,"results":"25","hashOfConfig":"15"},{"size":343,"mtime":1609834862066,"results":"26","hashOfConfig":"15"},{"size":2803,"mtime":1609910410275,"results":"27","hashOfConfig":"15"},{"filePath":"28","messages":"29","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"9qdc2m",{"filePath":"30","messages":"31","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"32","messages":"33","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"34","messages":"35","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"36","messages":"37","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"38","messages":"39","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"42","messages":"43","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"44","messages":"45","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"46","messages":"47","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"48","messages":"49","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"50","messages":"51","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"52","messages":"53","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/kchantrachir/Desktop/Personal/timing/src/index.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/reportWebVitals.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Interval.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Logger.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Datetime.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingContainer.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/TimingElement.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/pages/Index.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/Output.ts",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/Glassily.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/Footer.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/components/Header.tsx",[],"/Users/kchantrachir/Desktop/Personal/timing/src/models/SpecialDay.ts",[]] \ No newline at end of file diff --git a/src/models/Datetime.ts b/src/models/Datetime.ts index 3a75a09..c3306d5 100644 --- a/src/models/Datetime.ts +++ b/src/models/Datetime.ts @@ -54,8 +54,11 @@ class Datetime { return new Datetime(dayjs(ms)); } - static diff(ms: number) { - return dayjs.duration(ms); + // FIXME: This should return only `DurationPlugin.Duration`, but PR iamkun/dayjs#1317 must be resolved first + // PR: https://github.com/iamkun/dayjs/pull/1317 + static diff(ms: number): { d: DurationPlugin.Duration; negative: boolean } { + if (ms < 0) return { d: dayjs.duration(Math.abs(ms)), negative: true }; + return { d: dayjs.duration(ms), negative: false }; } static new(specialDay: SpecialDay, input: string, format?: string | null) { diff --git a/src/models/Output.ts b/src/models/Output.ts index 4f7c738..5618180 100644 --- a/src/models/Output.ts +++ b/src/models/Output.ts @@ -29,11 +29,16 @@ interface OutputObject { shortUnit: string; } -const numberUnit = (n: number, u: TimeUnit, d: number): OutputObject => { +interface NumberOption { + digit: number; + negative: boolean; +} + +const numberUnit = (n: number, u: TimeUnit, opt: NumberOption): OutputObject => { const a = u.split("|"); return { - number: Math.floor(n), - digit: d, + number: Math.floor(n) * (opt.negative ? -1 : 1), + digit: opt.digit, shortUnit: a[0], unit: a[1], }; @@ -54,40 +59,41 @@ class Output { } format(ms: number): OutputObject[] { - return this._format(Datetime.diff(ms)); + const o = Datetime.diff(ms); + return this._format(o.d, o.negative); } - _format(duration: DurationPlugin.Duration): OutputObject[] { + _format(duration: DurationPlugin.Duration, negative: boolean): OutputObject[] { switch (this.name) { case OutputType.MILLISECOND: - return [numberUnit(duration.asMilliseconds(), TimeUnit.MILLISECOND, 12)]; + return [numberUnit(duration.asMilliseconds(), TimeUnit.MILLISECOND, { digit: 12, negative })]; case OutputType.SECOND: - return [numberUnit(duration.asSeconds(), TimeUnit.SECOND, 9)]; + return [numberUnit(duration.asSeconds(), TimeUnit.SECOND, { digit: 9, negative })]; case OutputType.WEEK: - return [numberUnit(duration.asWeeks(), TimeUnit.WEEK, 5)]; + return [numberUnit(duration.asWeeks(), TimeUnit.WEEK, { digit: 5, negative })]; case OutputType.SECOND_MILLISECOND: return [ - numberUnit(duration.asSeconds(), TimeUnit.SECOND, 9), - numberUnit(duration.milliseconds(), TimeUnit.MILLISECOND, 3), + numberUnit(duration.asSeconds(), TimeUnit.SECOND, { digit: 9, negative }), + numberUnit(duration.milliseconds(), TimeUnit.MILLISECOND, { digit: 3, negative }), ]; case OutputType.MINUTE_SECOND_MILLISECOND: return [ - numberUnit(duration.asMinutes(), TimeUnit.MINUTE, 7), - numberUnit(duration.seconds(), TimeUnit.SECOND, 2), - numberUnit(duration.milliseconds(), TimeUnit.MILLISECOND, 3), + numberUnit(duration.asMinutes(), TimeUnit.MINUTE, { digit: 7, negative }), + numberUnit(duration.seconds(), TimeUnit.SECOND, { digit: 2, negative }), + numberUnit(duration.milliseconds(), TimeUnit.MILLISECOND, { digit: 3, negative }), ]; case OutputType.HOUR_MINUTE_SECOND: return [ - numberUnit(duration.asHours(), TimeUnit.HOUR, 6), - numberUnit(duration.minutes(), TimeUnit.MINUTE, 2), - numberUnit(duration.seconds(), TimeUnit.SECOND, 2), + numberUnit(duration.asHours(), TimeUnit.HOUR, { digit: 6, negative }), + numberUnit(duration.minutes(), TimeUnit.MINUTE, { digit: 2, negative }), + numberUnit(duration.seconds(), TimeUnit.SECOND, { digit: 2, negative }), ]; case OutputType.DAY_HOUR_MINUTE_SECOND: return [ - numberUnit(duration.asDays(), TimeUnit.DAY, 4), - numberUnit(duration.hours(), TimeUnit.HOUR, 2), - numberUnit(duration.minutes(), TimeUnit.MINUTE, 2), - numberUnit(duration.seconds(), TimeUnit.SECOND, 2), + numberUnit(duration.asDays(), TimeUnit.DAY, { digit: 4, negative }), + numberUnit(duration.hours(), TimeUnit.HOUR, { digit: 2, negative }), + numberUnit(duration.minutes(), TimeUnit.MINUTE, { digit: 2, negative }), + numberUnit(duration.seconds(), TimeUnit.SECOND, { digit: 2, negative }), ]; default: return [];