Skip to content

Commit

Permalink
feat(DateTime): use own format and all dates math, remove unused dayj…
Browse files Browse the repository at this point in the history
…s plugins (#62)
  • Loading branch information
ValeraS authored May 23, 2024
1 parent bae94bd commit 72c210c
Show file tree
Hide file tree
Showing 22 changed files with 1,298 additions and 192 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"prettier": "^3.1.0",
"sinon": "^10.0.1",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
"typescript": "^5.4.5"
},
"dependencies": {
"dayjs": "1.11.10",
Expand Down
1 change: 1 addition & 0 deletions scripts/updateLocales.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
/**
* Fetches and writes to the locale file from github dayjs
* node.js >= 20 lts
Expand Down
1 change: 1 addition & 0 deletions src/constants/format.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const DEFAULT_SYSTEM_DATE_FORMAT = 'YYYY-MM-DD';
export const INVALID_DATE_STRING = 'Invalid Date';
61 changes: 61 additions & 0 deletions src/dateTime/__tests__/relative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {dateTime} from '../dateTime';

test('from', () => {
const start = dateTime();
expect(start.from(start.add(5, 'seconds'))).toBe('a few seconds ago');
expect(start.from(start.add(1, 'minute'))).toBe('a minute ago');
expect(start.from(start.add(5, 'minutes'))).toBe('5 minutes ago');

expect(start.from(start.subtract(5, 'seconds'))).toBe('in a few seconds');
expect(start.from(start.subtract(1, 'minute'))).toBe('in a minute');
expect(start.from(start.subtract(5, 'minutes'))).toBe('in 5 minutes');
});

test('from with absolute duration', () => {
const start = dateTime();
expect(start.from(start.add(5, 'seconds'), true)).toBe('a few seconds');
expect(start.from(start.add(1, 'minute'), true)).toBe('a minute');
expect(start.from(start.add(5, 'minutes'), true)).toBe('5 minutes');

expect(start.from(start.subtract(5, 'seconds'), true)).toBe('a few seconds');
expect(start.from(start.subtract(1, 'minute'), true)).toBe('a minute');
expect(start.from(start.subtract(5, 'minutes'), true)).toBe('5 minutes');
});

test('default thresholds fromNow', () => {
let a = dateTime();

// Seconds to minutes threshold
a = a.subtract(44, 'seconds');
expect(a.fromNow()).toBe('a few seconds ago'); //'Below default seconds to minutes threshold'
a = a.subtract(1, 'seconds');
expect(a.fromNow()).toBe('a minute ago'); // 'Above default seconds to minutes threshold'

// Minutes to hours threshold
a = dateTime();
a = a.subtract(44, 'minutes');
expect(a.fromNow()).toBe('44 minutes ago'); //'Below default minute to hour threshold'
a = a.subtract(1, 'minutes');
expect(a.fromNow()).toBe('an hour ago'); // 'Above default minute to hour threshold'

// Hours to days threshold
a = dateTime();
a = a.subtract(21, 'hours');
expect(a.fromNow()).toBe('21 hours ago'); // 'Below default hours to day threshold'
a = a.subtract(1, 'hours');
expect(a.fromNow()).toBe('a day ago'); // 'Above default hours to day threshold'

// Days to month threshold
a = dateTime();
a = a.subtract(25, 'days');
expect(a.fromNow()).toBe('25 days ago'); // 'Below default days to month (singular) threshold'
a = a.subtract(1, 'days');
expect(a.fromNow()).toBe('a month ago'); // 'Above default days to month (singular) threshold'

// months to year threshold
a = dateTime();
a = a.subtract(10, 'months');
expect(a.fromNow()).toBe('10 months ago'); // 'Below default days to years threshold'
a = a.subtract(1, 'month');
expect(a.fromNow()).toBe('a year ago'); // 'Above default days to years threshold'
});
82 changes: 78 additions & 4 deletions src/dateTime/__tests__/weekday.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ afterEach(() => {
});

test('iso weekday', () => {
let i;

for (i = 0; i < 7; ++i) {
// moment.locale('dow:' + i + ',doy: 6', {week: {dow: i, doy: 6}});
for (let i = 0; i < 7; ++i) {
settings.updateLocale({weekStart: i, yearStart: 1 + i});
expect(dateTime({input: [1985, 1, 4]}).isoWeekday()).toBe(1); // 'Feb 4 1985 is Monday -- 1st day'
expect(dateTime({input: [2029, 8, 18]}).isoWeekday()).toBe(2); // 'Sep 18 2029 is Tuesday -- 2nd day'
Expand Down Expand Up @@ -55,3 +52,80 @@ test('iso weekday setter', () => {
expect(a.isoWeekday(11).date()).toBe(20); // 'set from sun to next thu'
expect(a.isoWeekday(14).date()).toBe(23); // 'set from sun to next sun'
});

test('weekday first day of week Sunday', () => {
settings.updateLocale({weekStart: 0, yearStart: 1});
expect(dateTime({input: [1985, 1, 3]}).weekday()).toBe(0); // 'Feb 3 1985 is Sunday -- 0th day'
expect(dateTime({input: [2029, 8, 17]}).weekday()).toBe(1); // 'Sep 17 2029 is Monday -- 1st day'
expect(dateTime({input: [2013, 3, 23]}).weekday()).toBe(2); // 'Apr 23 2013 is Tuesday -- 2nd day'
expect(dateTime({input: [2015, 2, 4]}).weekday()).toBe(3); // 'Mar 4 2015 is Wednesday -- 3nd day'
expect(dateTime({input: [1970, 0, 1]}).weekday()).toBe(4); // 'Jan 1 1970 is Thursday -- 4th day'
expect(dateTime({input: [2001, 4, 11]}).weekday()).toBe(5); // 'May 11 2001 is Friday -- 5th day'
expect(dateTime({input: [2000, 0, 1]}).weekday()).toBe(6); // 'Jan 1 2000 is Saturday -- 6th day'
});

test('weekday first day of week Monday', () => {
settings.updateLocale({weekStart: 1, yearStart: 1});
expect(dateTime({input: [1985, 1, 4]}).weekday()).toBe(0); // 'Feb 4 1985 is Monday -- 0th day'
expect(dateTime({input: [2029, 8, 18]}).weekday()).toBe(1); // 'Sep 18 2029 is Tuesday -- 1st day'
expect(dateTime({input: [2013, 3, 24]}).weekday()).toBe(2); // 'Apr 24 2013 is Wednesday -- 2nd day'
expect(dateTime({input: [2015, 2, 5]}).weekday()).toBe(3); // 'Mar 5 2015 is Thursday -- 3nd day'
expect(dateTime({input: [1970, 0, 2]}).weekday()).toBe(4); // 'Jan 2 1970 is Friday -- 4th day'
expect(dateTime({input: [2001, 4, 12]}).weekday()).toBe(5); // 'May 12 2001 is Saturday -- 5th day'
expect(dateTime({input: [2000, 0, 2]}).weekday()).toBe(6); // 'Jan 2 2000 is Sunday -- 6th day'
});

test('weekday first day of week Tuesday', () => {
settings.updateLocale({weekStart: 2, yearStart: 1});
expect(dateTime({input: [1985, 1, 5]}).weekday()).toBe(0); // 'Feb 5 1985 is Tuesday -- 0th day'
expect(dateTime({input: [2029, 8, 19]}).weekday()).toBe(1); // 'Sep 19 2029 is Wednesday -- 1st day'
expect(dateTime({input: [2013, 3, 25]}).weekday()).toBe(2); // 'Apr 25 2013 is Thursday -- 2nd day'
expect(dateTime({input: [2015, 2, 6]}).weekday()).toBe(3); // 'Mar 6 2015 is Friday -- 3nd day'
expect(dateTime({input: [1970, 0, 3]}).weekday()).toBe(4); // 'Jan 3 1970 is Saturday -- 4th day'
expect(dateTime({input: [2001, 4, 13]}).weekday()).toBe(5); // 'May 13 2001 is Sunday -- 5th day'
expect(dateTime({input: [2000, 0, 3]}).weekday()).toBe(6); // 'Jan 3 2000 is Monday -- 6th day'
});

test('weekday first day of week Wednesday', () => {
settings.updateLocale({weekStart: 3, yearStart: 1});
expect(dateTime({input: [1985, 1, 6]}).weekday()).toBe(0); // 'Feb 6 1985 is Wednesday -- 0th day'
expect(dateTime({input: [2029, 8, 20]}).weekday()).toBe(1); // 'Sep 20 2029 is Thursday -- 1st day'
expect(dateTime({input: [2013, 3, 26]}).weekday()).toBe(2); // 'Apr 26 2013 is Friday -- 2nd day'
expect(dateTime({input: [2015, 2, 7]}).weekday()).toBe(3); // 'Mar 7 2015 is Saturday -- 3nd day'
expect(dateTime({input: [1970, 0, 4]}).weekday()).toBe(4); // 'Jan 4 1970 is Sunday -- 4th day'
expect(dateTime({input: [2001, 4, 14]}).weekday()).toBe(5); // 'May 14 2001 is Monday -- 5th day'
expect(dateTime({input: [2000, 0, 4]}).weekday()).toBe(6); // 'Jan 4 2000 is Tuesday -- 6th day'
});

test('weekday first day of week Thursday', () => {
settings.updateLocale({weekStart: 4, yearStart: 1});
expect(dateTime({input: [1985, 1, 7]}).weekday()).toBe(0); // 'Feb 7 1985 is Thursday -- 0th day'
expect(dateTime({input: [2029, 8, 21]}).weekday()).toBe(1); // 'Sep 21 2029 is Friday -- 1st day'
expect(dateTime({input: [2013, 3, 27]}).weekday()).toBe(2); // 'Apr 27 2013 is Saturday -- 2nd day'
expect(dateTime({input: [2015, 2, 8]}).weekday()).toBe(3); // 'Mar 8 2015 is Sunday -- 3nd day'
expect(dateTime({input: [1970, 0, 5]}).weekday()).toBe(4); // 'Jan 5 1970 is Monday -- 4th day'
expect(dateTime({input: [2001, 4, 15]}).weekday()).toBe(5); // 'May 15 2001 is Tuesday -- 5th day'
expect(dateTime({input: [2000, 0, 5]}).weekday()).toBe(6); // 'Jan 5 2000 is Wednesday -- 6th day'
});

test('weekday first day of week Friday', () => {
settings.updateLocale({weekStart: 5, yearStart: 1});
expect(dateTime({input: [1985, 1, 8]}).weekday()).toBe(0); // 'Feb 8 1985 is Friday -- 0th day'
expect(dateTime({input: [2029, 8, 22]}).weekday()).toBe(1); // 'Sep 22 2029 is Saturday -- 1st day'
expect(dateTime({input: [2013, 3, 28]}).weekday()).toBe(2); // 'Apr 28 2013 is Sunday -- 2nd day'
expect(dateTime({input: [2015, 2, 9]}).weekday()).toBe(3); // 'Mar 9 2015 is Monday -- 3nd day'
expect(dateTime({input: [1970, 0, 6]}).weekday()).toBe(4); // 'Jan 6 1970 is Tuesday -- 4th day'
expect(dateTime({input: [2001, 4, 16]}).weekday()).toBe(5); // 'May 16 2001 is Wednesday -- 5th day'
expect(dateTime({input: [2000, 0, 6]}).weekday()).toBe(6); // 'Jan 6 2000 is Thursday -- 6th day'
});

test('weekday first day of week Saturday', () => {
settings.updateLocale({weekStart: 6, yearStart: 1});
expect(dateTime({input: [1985, 1, 9]}).weekday()).toBe(0); // 'Feb 9 1985 is Saturday -- 0th day'
expect(dateTime({input: [2029, 8, 23]}).weekday()).toBe(1); // 'Sep 23 2029 is Sunday -- 1st day'
expect(dateTime({input: [2013, 3, 29]}).weekday()).toBe(2); // 'Apr 29 2013 is Monday -- 2nd day'
expect(dateTime({input: [2015, 2, 10]}).weekday()).toBe(3); // 'Mar 10 2015 is Tuesday -- 3nd day'
expect(dateTime({input: [1970, 0, 7]}).weekday()).toBe(4); // 'Jan 7 1970 is Wednesday -- 4th day'
expect(dateTime({input: [2001, 4, 17]}).weekday()).toBe(5); // 'May 17 2001 is Thursday -- 5th day'
expect(dateTime({input: [2000, 0, 7]}).weekday()).toBe(6); // 'Jan 7 2000 is Friday -- 6th day'
});
14 changes: 13 additions & 1 deletion src/dateTime/dateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('DateTime', () => {
});

it('should work with years >= 0 and < 100 ', () => {
const date = dateTime({input: '0001-01-12T00:00:00Z', timeZone: 'Europe/Amsterdam'});
let date = dateTime({input: '0001-01-12T00:00:00Z', timeZone: 'Europe/Amsterdam'});
expect(date.toISOString()).toBe('0001-01-12T00:00:00.000Z');
expect(date.startOf('s').toISOString()).toBe('0001-01-12T00:00:00.000Z');
expect(date.startOf('s').valueOf()).toBe(date.valueOf());
Expand All @@ -226,6 +226,18 @@ describe('DateTime', () => {

expect(date.isSame(date)).toBe(true);
expect(date.valueOf()).toBe(date.startOf('ms').valueOf());

date = dateTime({input: '2023-01-01T00:00:00.000Z', timeZone: 'Europe/Moscow'});
const date2 = date.set('y', 2);
const date3 = date2.set({h: date2.hour(), m: date2.minute(), s: date2.second()});
expect(date3.toISOString()).toBe(date2.toISOString());

expect(
dateTime({
input: '0002-01-01T00:00:00.000Z',
timeZone: 'Europe/Moscow',
}).toISOString(),
).toBe('0002-01-01T00:00:00.000Z');
});
});
});
Loading

0 comments on commit 72c210c

Please sign in to comment.