Skip to content

Commit

Permalink
fix(ui): tmp fixed negative duration (iamkun/dayjs#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamontat committed Jan 6, 2021
1 parent 0a13883 commit 73e14e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .eslintcache
Original file line number Diff line number Diff line change
@@ -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",[]]
[{"/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",[]]
7 changes: 5 additions & 2 deletions src/models/Datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 26 additions & 20 deletions src/models/Output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
};
Expand All @@ -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 [];
Expand Down

0 comments on commit 73e14e1

Please sign in to comment.