Skip to content

Commit

Permalink
feat: make time formatter handle number and fix formatters type warni…
Browse files Browse the repository at this point in the history
…ngs (#358)

* fix: type warnings for formatters

* docs: add comment

* fix: unit tests

* fix: type

* feat: make time formatter handle timestamp
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 26, 2021
1 parent 735e8b2 commit aa84115
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { ExtensibleFunction } from '../../src';

describe('ExtensibleFunction', () => {
interface Func1 {
(): number;
}

class Func1 extends ExtensibleFunction {
constructor(x: unknown) {
super(() => x); // closure
}
}

interface Func2 {
(): number;
}

class Func2 extends ExtensibleFunction {
x: unknown;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ export interface NumberFormatterConfig {
isInvalid?: boolean;
}

export default class NumberFormatter extends ExtensibleFunction {
// Use type augmentation to indicate that
// an instance of NumberFormatter is also a function
interface NumberFormatter {
(value: number | null | undefined): string;
}

class NumberFormatter extends ExtensibleFunction {
id: string;

label: string;
Expand Down Expand Up @@ -57,3 +63,5 @@ export default class NumberFormatter extends ExtensibleFunction {
return `${value} => ${this.format(value)}`;
}
}

export default NumberFormatter;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { TimeFormatFunction } from './types';

export const PREVIEW_TIME = new Date(Date.UTC(2017, 1, 14, 11, 22, 33));

export default class TimeFormatter extends ExtensibleFunction {
// Use type augmentation to indicate that
// an instance of TimeFormatter is also a function
interface TimeFormatter {
(value: Date | number | null | undefined): string;
}

class TimeFormatter extends ExtensibleFunction {
id: string;

label: string;
Expand All @@ -21,7 +27,7 @@ export default class TimeFormatter extends ExtensibleFunction {
formatFunc: TimeFormatFunction;
useLocalTime?: boolean;
}) {
super((value: Date | null | undefined) => this.format(value));
super((value: Date | number | null | undefined) => this.format(value));

const {
id = isRequired('config.id'),
Expand All @@ -38,15 +44,17 @@ export default class TimeFormatter extends ExtensibleFunction {
this.useLocalTime = useLocalTime;
}

format(value: Date | null | undefined) {
format(value: Date | number | null | undefined) {
if (value === null || value === undefined) {
return `${value}`;
}

return this.formatFunc(value);
return this.formatFunc(value instanceof Date ? value : new Date(value));
}

preview(value: Date = PREVIEW_TIME) {
return `${value.toUTCString()} => ${this.format(value)}`;
}
}

export default TimeFormatter;
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe('TimeFormatter', () => {
it('handles undefined', () => {
expect(formatter.format(undefined)).toEqual('undefined');
});
it('handles number, treating it as a timestamp', () => {
expect(formatter.format(PREVIEW_TIME.getTime())).toEqual('2017');
});
it('otherwise returns formatted value', () => {
expect(formatter.format(PREVIEW_TIME)).toEqual('2017');
});
Expand Down

0 comments on commit aa84115

Please sign in to comment.