Skip to content

Commit

Permalink
fix: add get-better-date
Browse files Browse the repository at this point in the history
  • Loading branch information
uzenith360 committed Jan 14, 2024
1 parent 4d2d6b1 commit d944ad3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/get-better-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getDayText from "./get-day-text";
import getEndOfDayDate from "./get-end-of-day-date";
import getFormattedTimeString from "./get-formatted-time-string";
import getShortTextDayMonthOptionalyear from "./get-short-text-day-month-optionalyear";

export default (dateTimeStringOrTimestampOrDate: string|number|Date): string => {
const date: Date = new Date(dateTimeStringOrTimestampOrDate);

if (date >= getEndOfDayDate(1)) {
return getFormattedTimeString(date);
} else if (date >= getEndOfDayDate(2)) {
return 'Yesterday';
} else if (date >= getEndOfDayDate(7)) {
return getDayText(date);
} else {
return getShortTextDayMonthOptionalyear(date);
}
}
40 changes: 40 additions & 0 deletions tests/get-better-date.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'mocha';
import { assert } from 'chai';
import getBetterDate from '../src/get-better-date';
import getFormattedTimeString from '../src/get-formatted-time-string';
import getDayText from '../src/get-day-text';
import getEndOfDayDate from '../src/get-end-of-day-date';

describe('Get better date', () => {
it('should return the date within same day', () => {
const date: Date = getEndOfDayDate();

const expected = getFormattedTimeString(date);
const actual = getBetterDate(date);
assert.equal(actual, expected);
});

it('should return the date within past day', () => {
const date: Date = new Date();

date.setDate(date.getDate() - 1); // yesterday

const expected = 'Yesterday';
const actual = getBetterDate(date);
assert.equal(actual, expected);
});

it('should return the date within same week', () => {
const date: Date = new Date();

date.setDate(date.getDate() - 3); // 3 days

const expected = getDayText(date);
const actual = getBetterDate(date);
assert.equal(actual, expected);
});

it('should return the date in same year from a date object', () => { });

it('should return the date in different year from a date object', () => { });
});

0 comments on commit d944ad3

Please sign in to comment.