Skip to content
Open
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
152 changes: 55 additions & 97 deletions src/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('format(number, { long: true })', () => {

it('should support milliseconds', () => {
expect(format(500, { long: true })).toBe('500 ms');

expect(format(-500, { long: true })).toBe('-500 ms');
});

Expand Down Expand Up @@ -51,74 +50,51 @@ describe('format(number, { long: true })', () => {
expect(format(1 * 24 * 60 * 60 * 1200, { long: true })).toBe('1 day');
expect(format(6 * 24 * 60 * 60 * 1000, { long: true })).toBe('6 days');

expect(format(-1 * 1 * 24 * 60 * 60 * 1000, { long: true })).toBe('-1 day');
expect(format(-1 * 1 * 24 * 60 * 60 * 1200, { long: true })).toBe('-1 day');
expect(format(-1 * 6 * 24 * 60 * 60 * 1000, { long: true })).toBe(
'-6 days',
);
expect(format(-1 * 24 * 60 * 60 * 1000, { long: true })).toBe('-1 day');
expect(format(-1 * 24 * 60 * 60 * 1200, { long: true })).toBe('-1 day');
expect(format(-6 * 24 * 60 * 60 * 1000, { long: true })).toBe('-6 days');
});

it('should support weeks', () => {
expect(format(1 * 7 * 24 * 60 * 60 * 1000, { long: true })).toBe('1 week');
expect(format(2 * 7 * 24 * 60 * 60 * 1000, { long: true })).toBe('2 weeks');

expect(format(-1 * 1 * 7 * 24 * 60 * 60 * 1000, { long: true })).toBe(
'-1 week',
);
expect(format(-1 * 2 * 7 * 24 * 60 * 60 * 1000, { long: true })).toBe(
'-2 weeks',
);
expect(format(-1 * 7 * 24 * 60 * 60 * 1000, { long: true })).toBe('-1 week');
expect(format(-2 * 7 * 24 * 60 * 60 * 1000, { long: true })).toBe('-2 weeks');
});

it('should support months', () => {
expect(format(30.4375 * 24 * 60 * 60 * 1000, { long: true })).toBe(
'1 month',
);
expect(format(30.4375 * 24 * 60 * 60 * 1200, { long: true })).toBe(
'1 month',
);
expect(format(30.4375 * 24 * 60 * 60 * 10000, { long: true })).toBe(
'10 months',
);

expect(format(-1 * 30.4375 * 24 * 60 * 60 * 1000, { long: true })).toBe(
'-1 month',
);
expect(format(-1 * 30.4375 * 24 * 60 * 60 * 1200, { long: true })).toBe(
'-1 month',
);
expect(format(-1 * 30.4375 * 24 * 60 * 60 * 10000, { long: true })).toBe(
'-10 months',
);
expect(format(30.4375 * 24 * 60 * 60 * 1000, { long: true })).toBe('1 month');
expect(format(30.4375 * 24 * 60 * 60 * 1200, { long: true })).toBe('1 month');
expect(format(30.4375 * 24 * 60 * 60 * 10000, { long: true })).toBe('10 months');

expect(format(-30.4375 * 24 * 60 * 60 * 1000, { long: true })).toBe('-1 month');
expect(format(-30.4375 * 24 * 60 * 60 * 1200, { long: true })).toBe('-1 month');
expect(format(-30.4375 * 24 * 60 * 60 * 10000, { long: true })).toBe('-10 months');
});

it('should support years', () => {
expect(format(365.25 * 24 * 60 * 60 * 1000 + 1, { long: true })).toBe(
'1 year',
);
expect(format(365.25 * 24 * 60 * 60 * 1200 + 1, { long: true })).toBe(
'1 year',
);
expect(format(365.25 * 24 * 60 * 60 * 10000 + 1, { long: true })).toBe(
'10 years',
);

expect(format(-1 * 365.25 * 24 * 60 * 60 * 1000 - 1, { long: true })).toBe(
'-1 year',
);
expect(format(-1 * 365.25 * 24 * 60 * 60 * 1200 - 1, { long: true })).toBe(
'-1 year',
);
expect(format(-1 * 365.25 * 24 * 60 * 60 * 10000 - 1, { long: true })).toBe(
'-10 years',
);
expect(format(365.25 * 24 * 60 * 60 * 1000 + 1, { long: true })).toBe('1 year');
expect(format(365.25 * 24 * 60 * 60 * 1200 + 1, { long: true })).toBe('1 year');
expect(format(365.25 * 24 * 60 * 60 * 10000 + 1, { long: true })).toBe('10 years');

expect(format(-365.25 * 24 * 60 * 60 * 1000 - 1, { long: true })).toBe('-1 year');
expect(format(-365.25 * 24 * 60 * 60 * 1200 - 1, { long: true })).toBe('-1 year');
expect(format(-365.25 * 24 * 60 * 60 * 10000 - 1, { long: true })).toBe('-10 years');
});

it('should round', () => {
expect(format(234234234, { long: true })).toBe('3 days');

expect(format(-234234234, { long: true })).toBe('-3 days');
});

it('should handle rounding boundaries', () => {
expect(format(1499, { long: true })).toBe('1 second');
expect(format(1500, { long: true })).toBe('2 seconds');

expect(format(-1499, { long: true })).toBe('-1 second');
expect(format(-1500, { long: true })).toBe('-1 seconds');
});
});

// numbers
Expand All @@ -132,7 +108,6 @@ describe('format(number)', () => {

it('should support milliseconds', () => {
expect(format(500)).toBe('500ms');

expect(format(-500)).toBe('-500ms');
});

Expand All @@ -148,57 +123,56 @@ describe('format(number)', () => {
expect(format(60 * 1000)).toBe('1m');
expect(format(60 * 10000)).toBe('10m');

expect(format(-1 * 60 * 1000)).toBe('-1m');
expect(format(-1 * 60 * 10000)).toBe('-10m');
expect(format(-60 * 1000)).toBe('-1m');
expect(format(-60 * 10000)).toBe('-10m');
});

it('should support hours', () => {
expect(format(60 * 60 * 1000)).toBe('1h');
expect(format(60 * 60 * 10000)).toBe('10h');

expect(format(-1 * 60 * 60 * 1000)).toBe('-1h');
expect(format(-1 * 60 * 60 * 10000)).toBe('-10h');
expect(format(-60 * 60 * 1000)).toBe('-1h');
expect(format(-60 * 60 * 10000)).toBe('-10h');
});

it('should support days', () => {
expect(format(24 * 60 * 60 * 1000)).toBe('1d');
expect(format(24 * 60 * 60 * 6000)).toBe('6d');

expect(format(-1 * 24 * 60 * 60 * 1000)).toBe('-1d');
expect(format(-1 * 24 * 60 * 60 * 6000)).toBe('-6d');
expect(format(-24 * 60 * 60 * 1000)).toBe('-1d');
expect(format(-24 * 60 * 60 * 6000)).toBe('-6d');
});

it('should support weeks', () => {
expect(format(1 * 7 * 24 * 60 * 60 * 1000)).toBe('1w');
expect(format(7 * 24 * 60 * 60 * 1000)).toBe('1w');
expect(format(2 * 7 * 24 * 60 * 60 * 1000)).toBe('2w');

expect(format(-1 * 1 * 7 * 24 * 60 * 60 * 1000)).toBe('-1w');
expect(format(-1 * 2 * 7 * 24 * 60 * 60 * 1000)).toBe('-2w');
expect(format(-7 * 24 * 60 * 60 * 1000)).toBe('-1w');
expect(format(-2 * 7 * 24 * 60 * 60 * 1000)).toBe('-2w');
});

it('should support months', () => {
expect(format(30.4375 * 24 * 60 * 60 * 1000)).toBe('1mo');
expect(format(30.4375 * 24 * 60 * 60 * 1200)).toBe('1mo');
expect(format(30.4375 * 24 * 60 * 60 * 10000)).toBe('10mo');

expect(format(-1 * 30.4375 * 24 * 60 * 60 * 1000)).toBe('-1mo');
expect(format(-1 * 30.4375 * 24 * 60 * 60 * 1200)).toBe('-1mo');
expect(format(-1 * 30.4375 * 24 * 60 * 60 * 10000)).toBe('-10mo');
expect(format(-30.4375 * 24 * 60 * 60 * 1000)).toBe('-1mo');
expect(format(-30.4375 * 24 * 60 * 60 * 1200)).toBe('-1mo');
expect(format(-30.4375 * 24 * 60 * 60 * 10000)).toBe('-10mo');
});

it('should support years', () => {
expect(format(365.25 * 24 * 60 * 60 * 1000 + 1)).toBe('1y');
expect(format(365.25 * 24 * 60 * 60 * 1200 + 1)).toBe('1y');
expect(format(365.25 * 24 * 60 * 60 * 10000 + 1)).toBe('10y');

expect(format(-1 * 365.25 * 24 * 60 * 60 * 1000 - 1)).toBe('-1y');
expect(format(-1 * 365.25 * 24 * 60 * 60 * 1200 - 1)).toBe('-1y');
expect(format(-1 * 365.25 * 24 * 60 * 60 * 10000 - 1)).toBe('-10y');
expect(format(-365.25 * 24 * 60 * 60 * 1000 - 1)).toBe('-1y');
expect(format(-365.25 * 24 * 60 * 60 * 1200 - 1)).toBe('-1y');
expect(format(-365.25 * 24 * 60 * 60 * 10000 - 1)).toBe('-10y');
});

it('should round', () => {
expect(format(234234234)).toBe('3d');

expect(format(-234234234)).toBe('-3d');
});
});
Expand All @@ -207,55 +181,39 @@ describe('format(number)', () => {

describe('format(invalid inputs)', () => {
it('should throw an error, when format("")', () => {
expect(() => {
// @ts-expect-error - We expect this to throw.
format('');
}).toThrow();
// @ts-expect-error
expect(() => format('')).toThrow();
});

it('should throw an error, when format(undefined)', () => {
expect(() => {
// @ts-expect-error - We expect this to throw.
format(undefined);
}).toThrow();
// @ts-expect-error
expect(() => format(undefined)).toThrow();
});

it('should throw an error, when format(null)', () => {
expect(() => {
// @ts-expect-error - We expect this to throw.
format(null);
}).toThrow();
// @ts-expect-error
expect(() => format(null)).toThrow();
});

it('should throw an error, when format([])', () => {
expect(() => {
// @ts-expect-error - We expect this to throw.
format([]);
}).toThrow();
// @ts-expect-error
expect(() => format([])).toThrow();
});

it('should throw an error, when format({})', () => {
expect(() => {
// @ts-expect-error - We expect this to throw.
format({});
}).toThrow();
// @ts-expect-error
expect(() => format({})).toThrow();
});

it('should throw an error, when format(NaN)', () => {
expect(() => {
format(NaN);
}).toThrow();
expect(() => format(NaN)).toThrow();
});

it('should throw an error, when format(Infinity)', () => {
expect(() => {
format(Infinity);
}).toThrow();
expect(() => format(Infinity)).toThrow();
});

it('should throw an error, when format(-Infinity)', () => {
expect(() => {
format(-Infinity);
}).toThrow();
expect(() => format(-Infinity)).toThrow();
});
});
Loading