From 4e73b176459c40972f6eea53c850f614c1bc60c3 Mon Sep 17 00:00:00 2001 From: Pawel Stanecki Date: Thu, 12 Dec 2019 15:49:55 +0100 Subject: [PATCH 1/6] Update dateDiff.ts and add tests --- README.md | 8 ++++++++ async/README.md | 6 ++++++ async/debounce.md | 2 ++ async/delay.md | 2 ++ async/sequence.js | 12 +++++------- async/sequence.md | 2 ++ date/README.md | 2 ++ date/dateDiff.json | 2 +- date/dateDiff.md | 2 ++ date/dateDiff.test.ts | 18 ++++++++++++++++-- date/dateDiff.ts | 2 +- 11 files changed, 47 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b4e2b621..371cbafd 100644 --- a/README.md +++ b/README.md @@ -198,10 +198,16 @@ Zips given arrays together with the given function. #### debounce +Makes the function run after the given period of not being called. Useful to delay input submission for autocomplete etc. + #### delay +When awaited, delays the execution by the given number of milliseconds. + #### sequence +Runs the given tasks in a sequence. + ### date #### byDateWithFallback @@ -210,6 +216,8 @@ Zips given arrays together with the given function. #### dateDiff +Calculates the difference in milliseconds between two dates. + #### dateInRange #### dayRange diff --git a/async/README.md b/async/README.md index 0ea12fa9..e2381449 100644 --- a/async/README.md +++ b/async/README.md @@ -1,5 +1,11 @@ # debounce +Makes the function run after the given period of not being called. Useful to delay input submission for autocomplete etc. + # delay +When awaited, delays the execution by the given number of milliseconds. + # sequence + +Runs the given tasks in a sequence. diff --git a/async/debounce.md b/async/debounce.md index dce82919..a34e4c1e 100644 --- a/async/debounce.md +++ b/async/debounce.md @@ -1 +1,3 @@ # debounce + +Makes the function run after the given period of not being called. Useful to delay input submission for autocomplete etc. diff --git a/async/delay.md b/async/delay.md index 021604ae..da6fc617 100644 --- a/async/delay.md +++ b/async/delay.md @@ -1 +1,3 @@ # delay + +When awaited, delays the execution by the given number of milliseconds. diff --git a/async/sequence.js b/async/sequence.js index be0ac9e2..09738849 100644 --- a/async/sequence.js +++ b/async/sequence.js @@ -1,13 +1,11 @@ export default async tasks => { const results = tasks.map(() => undefined); - await tasks.reduce((chain, current, i) => { - return chain.then(() => - current().then(x => { - results[i] = x; + await tasks.reduce(async (chain, current, i) => { + await chain; + const x = await current(); + results[i] = x; - return x; - }) - ); + return x; }, Promise.resolve()); return results; diff --git a/async/sequence.md b/async/sequence.md index a6fb8760..32faf064 100644 --- a/async/sequence.md +++ b/async/sequence.md @@ -1 +1,3 @@ # sequence + +Runs the given tasks in a sequence. diff --git a/date/README.md b/date/README.md index 8c1e867e..5dd42f97 100644 --- a/date/README.md +++ b/date/README.md @@ -4,6 +4,8 @@ # dateDiff +Calculates the difference in milliseconds between two dates. + # dateInRange # dayRange diff --git a/date/dateDiff.json b/date/dateDiff.json index c50775e0..b449bd97 100644 --- a/date/dateDiff.json +++ b/date/dateDiff.json @@ -1,6 +1,6 @@ { "name": "dateDiff", - "description": "TODO: Fill short description here.", + "description": "Calculates the difference in milliseconds between two dates.", "signature": "TODO: Fill type signature here.", "examples": [ { diff --git a/date/dateDiff.md b/date/dateDiff.md index a299ea56..e74d2aea 100644 --- a/date/dateDiff.md +++ b/date/dateDiff.md @@ -1 +1,3 @@ # dateDiff + +Calculates the difference in milliseconds between two dates. diff --git a/date/dateDiff.test.ts b/date/dateDiff.test.ts index 7fd8c8bb..a650200e 100644 --- a/date/dateDiff.test.ts +++ b/date/dateDiff.test.ts @@ -3,7 +3,21 @@ import dateDiff from "./dateDiff.ts"; describe("dateDiff", () => { - it.skip("TODO", () => { - expect(dateDiff()).toBeDefined(); + it("returns the difference of 24 hours in miliseconds", () => { + const firstDate = new Date("2019-12-18T12:00:00"); + const secondDate = new Date("2019-12-17T12:00:00"); + + const dayInMiliseconds = 86400000; + + expect(dateDiff(firstDate, secondDate)).toEqual(dayInMiliseconds); + }); + + it("returns 0 difference of eqact same dates", () => { + const firstDate = new Date("2019-10-10T10:00:00"); + const secondDate = new Date("2019-10-10T10:00:00"); + + const noDifference = 0; + + expect(dateDiff(firstDate, secondDate)).toEqual(noDifference); }); }); diff --git a/date/dateDiff.ts b/date/dateDiff.ts index 2e4d2ce6..aadcb67b 100644 --- a/date/dateDiff.ts +++ b/date/dateDiff.ts @@ -1,4 +1,4 @@ -export default (a, b) => { +export default (a: string | Date, b: string | Date): number => { const d1 = new Date(a); const d2 = new Date(b); From e904ce3c112d84149db30d757eba1145c154a6a6 Mon Sep 17 00:00:00 2001 From: Pawel Stanecki Date: Thu, 12 Dec 2019 16:06:47 +0100 Subject: [PATCH 2/6] Add types in dateInRange.ts --- date/dateInRange.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/date/dateInRange.ts b/date/dateInRange.ts index 29997ef7..cfed7428 100644 --- a/date/dateInRange.ts +++ b/date/dateInRange.ts @@ -1,4 +1,6 @@ -export default (from, to) => (date = new Date()) => { +export default (from: string | Date, to: string | Date) => ( + date = new Date() +): boolean => { const dateTime = new Date(date).getTime(); const fromTime = new Date(from).getTime(); const toTime = new Date(to).getTime(); From f941666560edc8124ca03aa7d897d7805dcc80ea Mon Sep 17 00:00:00 2001 From: Pawel Stanecki Date: Fri, 13 Dec 2019 09:11:19 +0100 Subject: [PATCH 3/6] Updated dateInRange --- README.md | 6 ++++++ date/README.md | 6 ++++++ date/dateInRange.json | 2 +- date/dateInRange.md | 6 ++++++ date/dateInRange.test.ts | 20 ++++++++++++++++++-- 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 371cbafd..e5982300 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,12 @@ Calculates the difference in milliseconds between two dates. #### dateInRange +##### Type signature + +``` +Checks if provided date is within the range. +``` + #### dayRange #### daysInMonths diff --git a/date/README.md b/date/README.md index 5dd42f97..f3d1edec 100644 --- a/date/README.md +++ b/date/README.md @@ -8,6 +8,12 @@ Calculates the difference in milliseconds between two dates. # dateInRange +## Type signature + +``` +Checks if provided date is within the range. +``` + # dayRange # daysInMonths diff --git a/date/dateInRange.json b/date/dateInRange.json index 35f60ade..dce8b0e7 100644 --- a/date/dateInRange.json +++ b/date/dateInRange.json @@ -1,7 +1,7 @@ { "name": "dateInRange", "description": "TODO: Fill short description here.", - "signature": "TODO: Fill type signature here.", + "signature": "Checks if provided date is within the range.", "examples": [ { "language": "javascript", diff --git a/date/dateInRange.md b/date/dateInRange.md index 87f3e12c..0aeb1578 100644 --- a/date/dateInRange.md +++ b/date/dateInRange.md @@ -1 +1,7 @@ # dateInRange + +## Type signature + +``` +Checks if provided date is within the range. +``` diff --git a/date/dateInRange.test.ts b/date/dateInRange.test.ts index 0ba4939f..96ceba00 100644 --- a/date/dateInRange.test.ts +++ b/date/dateInRange.test.ts @@ -3,7 +3,23 @@ import dateInRange from "./dateInRange.ts"; describe("dateInRange", () => { - it.skip("TODO", () => { - expect(dateInRange()).toBeDefined(); + it("provided date is within the day range", () => { + const startRangeDate = new Date("2019-05-28T12:00:00"); + const endRangeDate = new Date("2019-05-29T12:00:00"); + const dateToCheck = new Date("2019-05-28T24:00:00"); + + expect(dateInRange(startRangeDate, endRangeDate)(dateToCheck)).toEqual( + true + ); + }); + + it("provided date is not within the year range", () => { + const startRangeDate = new Date("2018-05-28T12:00:00"); + const endRangeDate = new Date("2019-05-29T12:00:00"); + const dateToCheck = new Date("2017-05-28T24:00:00"); + + expect(dateInRange(startRangeDate, endRangeDate)(dateToCheck)).toEqual( + false + ); }); }); From 312c53ef897f6d016d7a89b9f0fc03f963bc6a7a Mon Sep 17 00:00:00 2001 From: Pawel Stanecki Date: Fri, 13 Dec 2019 09:41:10 +0100 Subject: [PATCH 4/6] Update datesInMonths --- date/dateInRange.json | 4 ++-- date/daysInMonths.json | 2 +- date/daysInMonths.test.ts | 38 ++++++++++++++++++++++++++++++++++++-- date/daysInMonths.ts | 2 +- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/date/dateInRange.json b/date/dateInRange.json index dce8b0e7..bf5236e3 100644 --- a/date/dateInRange.json +++ b/date/dateInRange.json @@ -1,7 +1,7 @@ { "name": "dateInRange", - "description": "TODO: Fill short description here.", - "signature": "Checks if provided date is within the range.", + "description": "Checks if provided date is within the range.", + "signature": "TODO: Fill type signature here.", "examples": [ { "language": "javascript", diff --git a/date/daysInMonths.json b/date/daysInMonths.json index 054f2c19..bb1402f2 100644 --- a/date/daysInMonths.json +++ b/date/daysInMonths.json @@ -1,6 +1,6 @@ { "name": "daysInMonths", - "description": "TODO: Fill short description here.", + "description": "Returns numbers of days in all months of the year having regard to leap year.", "signature": "TODO: Fill type signature here.", "examples": [ { diff --git a/date/daysInMonths.test.ts b/date/daysInMonths.test.ts index bed95fea..c50a523a 100644 --- a/date/daysInMonths.test.ts +++ b/date/daysInMonths.test.ts @@ -3,7 +3,41 @@ import daysInMonths from "./daysInMonths.ts"; describe("daysInMonths", () => { - it.skip("TODO", () => { - expect(daysInMonths()).toBeDefined(); + it("returns number of days in a months in a leap year", () => { + const leapYear = true; + const numberOfDaysInMonthsInLeapYear = [ + 31, + 29, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31 + ]; + expect(daysInMonths(leapYear)).toEqual(numberOfDaysInMonthsInLeapYear); + }); + + it("returns number of days in a months in a year", () => { + const leapYear = false; + const numberOfDaysInMonths = [ + 31, + 28, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31 + ]; + expect(daysInMonths(leapYear)).toEqual(numberOfDaysInMonths); }); }); diff --git a/date/daysInMonths.ts b/date/daysInMonths.ts index 2ad5657f..672f2333 100644 --- a/date/daysInMonths.ts +++ b/date/daysInMonths.ts @@ -1,4 +1,4 @@ -export default leapYear => [ +export default (leapYear): Number[] => [ 31, leapYear ? 29 : 28, 31, From 30e4006b3683e1dc08defea681bcbce5961b096f Mon Sep 17 00:00:00 2001 From: Pawel Stanecki Date: Fri, 13 Dec 2019 09:56:26 +0100 Subject: [PATCH 5/6] build and test daysinMonths --- README.md | 6 ++---- date/README.md | 6 ++---- date/dateInRange.md | 4 ---- date/daysInMonths.md | 2 ++ date/daysInMonths.test.ts | 4 ++++ date/daysInMonths.ts | 2 +- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e5982300..8b0fd19f 100644 --- a/README.md +++ b/README.md @@ -220,16 +220,14 @@ Calculates the difference in milliseconds between two dates. #### dateInRange -##### Type signature - -``` Checks if provided date is within the range. -``` #### dayRange #### daysInMonths +Returns numbers of days in all months of the year having regard to leap year. + #### daysInYear #### displayMonth diff --git a/date/README.md b/date/README.md index f3d1edec..6514415d 100644 --- a/date/README.md +++ b/date/README.md @@ -8,16 +8,14 @@ Calculates the difference in milliseconds between two dates. # dateInRange -## Type signature - -``` Checks if provided date is within the range. -``` # dayRange # daysInMonths +Returns numbers of days in all months of the year having regard to leap year. + # daysInYear # displayMonth diff --git a/date/dateInRange.md b/date/dateInRange.md index 0aeb1578..226f3cc7 100644 --- a/date/dateInRange.md +++ b/date/dateInRange.md @@ -1,7 +1,3 @@ # dateInRange -## Type signature - -``` Checks if provided date is within the range. -``` diff --git a/date/daysInMonths.md b/date/daysInMonths.md index 6257a25f..d91d7b5a 100644 --- a/date/daysInMonths.md +++ b/date/daysInMonths.md @@ -1 +1,3 @@ # daysInMonths + +Returns numbers of days in all months of the year having regard to leap year. diff --git a/date/daysInMonths.test.ts b/date/daysInMonths.test.ts index c50a523a..3bd70300 100644 --- a/date/daysInMonths.test.ts +++ b/date/daysInMonths.test.ts @@ -5,6 +5,7 @@ import daysInMonths from "./daysInMonths.ts"; describe("daysInMonths", () => { it("returns number of days in a months in a leap year", () => { const leapYear = true; + const numberOfDaysInMonthsInLeapYear = [ 31, 29, @@ -19,11 +20,13 @@ describe("daysInMonths", () => { 30, 31 ]; + expect(daysInMonths(leapYear)).toEqual(numberOfDaysInMonthsInLeapYear); }); it("returns number of days in a months in a year", () => { const leapYear = false; + const numberOfDaysInMonths = [ 31, 28, @@ -38,6 +41,7 @@ describe("daysInMonths", () => { 30, 31 ]; + expect(daysInMonths(leapYear)).toEqual(numberOfDaysInMonths); }); }); diff --git a/date/daysInMonths.ts b/date/daysInMonths.ts index 672f2333..6a6b17b7 100644 --- a/date/daysInMonths.ts +++ b/date/daysInMonths.ts @@ -1,4 +1,4 @@ -export default (leapYear): Number[] => [ +export default (leapYear): number[] => [ 31, leapYear ? 29 : 28, 31, From b2b47591949ef1a0ea433362544ca92c52a69516 Mon Sep 17 00:00:00 2001 From: Pawel Stanecki Date: Fri, 13 Dec 2019 11:08:52 +0100 Subject: [PATCH 6/6] Added monthnames, start/end of a day tests --- README.md | 6 ++++++ date/README.md | 6 ++++++ date/endOfDay.json | 2 +- date/endOfDay.md | 2 ++ date/endOfDay.test.ts | 19 +++++++++++++++++-- date/endOfDay.ts | 6 +++++- date/monthNames.js | 2 +- date/monthNames.json | 2 +- date/monthNames.md | 2 ++ date/monthNames.test.ts | 19 +++++++++++++++++-- date/monthNames.ts | 2 +- date/startOfDay.json | 2 +- date/startOfDay.md | 2 ++ date/startOfDay.test.ts | 19 +++++++++++++++++-- date/startOfDay.ts | 6 +++++- 15 files changed, 84 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8b0fd19f..39a4d41a 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,8 @@ Returns numbers of days in all months of the year having regard to leap year. #### endOfDay +Calculates end of a day in provided date. + #### formatDate #### formatDateTime @@ -260,6 +262,8 @@ Returns numbers of days in all months of the year having regard to leap year. #### monthNames +Returns all months names. + #### offsetByBit #### parseHourMinutePair @@ -268,6 +272,8 @@ Returns numbers of days in all months of the year having regard to leap year. #### startOfDay +Calculates start of a day in provided date. + #### subtractDays #### toDate diff --git a/date/README.md b/date/README.md index 6514415d..08b92a1a 100644 --- a/date/README.md +++ b/date/README.md @@ -24,6 +24,8 @@ Returns numbers of days in all months of the year having regard to leap year. # endOfDay +Calculates end of a day in provided date. + # formatDate # formatDateTime @@ -48,6 +50,8 @@ Returns numbers of days in all months of the year having regard to leap year. # monthNames +Returns all months names. + # offsetByBit # parseHourMinutePair @@ -56,6 +60,8 @@ Returns numbers of days in all months of the year having regard to leap year. # startOfDay +Calculates start of a day in provided date. + # subtractDays # toDate diff --git a/date/endOfDay.json b/date/endOfDay.json index 3676c1c5..fbca114d 100644 --- a/date/endOfDay.json +++ b/date/endOfDay.json @@ -1,6 +1,6 @@ { "name": "endOfDay", - "description": "TODO: Fill short description here.", + "description": "Calculates end of a day in provided date.", "signature": "TODO: Fill type signature here.", "examples": [ { diff --git a/date/endOfDay.md b/date/endOfDay.md index e16e7108..878cd7ef 100644 --- a/date/endOfDay.md +++ b/date/endOfDay.md @@ -1 +1,3 @@ # endOfDay + +Calculates end of a day in provided date. diff --git a/date/endOfDay.test.ts b/date/endOfDay.test.ts index d0fe743d..2e7ba82b 100644 --- a/date/endOfDay.test.ts +++ b/date/endOfDay.test.ts @@ -3,7 +3,22 @@ import endOfDay from "./endOfDay.ts"; describe("endOfDay", () => { - it.skip("TODO", () => { - expect(endOfDay()).toBeDefined(); + const date = new Date("2018-12-31T13:54:33.232Z"); + + it("should handle timezone offsets", () => { + const localOffset = -180; + + const end = endOfDay(date, localOffset); + const expectedUTC = new Date("2019-01-01T03:00:00.000Z"); + const expectedLocal = new Date("2018-12-31T24:00:00.000-03:00"); + + expect(end).toEqual(expectedUTC); + expect(end).toEqual(expectedLocal); + }); + + it("should handle default local date time display", () => { + const localOffset = 0; + + expect(endOfDay(date)).toEqual(endOfDay(date, localOffset)); }); }); diff --git a/date/endOfDay.ts b/date/endOfDay.ts index 71f92a0f..54005c08 100644 --- a/date/endOfDay.ts +++ b/date/endOfDay.ts @@ -1,6 +1,10 @@ import toLocalDateTime from "./toLocalDateTime"; -export default (date, timezoneOffset = 0, local = true) => { +export default ( + date: string | Date, + timezoneOffset = 0, + local = true +): Date => { const newDate = new Date(date); newDate.setHours(24, 0, 0, 0); diff --git a/date/monthNames.js b/date/monthNames.js index c870f165..bfb0bc7e 100644 --- a/date/monthNames.js +++ b/date/monthNames.js @@ -1,4 +1,4 @@ -export default [ +export default () => [ "January", "February", "March", diff --git a/date/monthNames.json b/date/monthNames.json index 31794929..fe7b6a5e 100644 --- a/date/monthNames.json +++ b/date/monthNames.json @@ -1,6 +1,6 @@ { "name": "monthNames", - "description": "TODO: Fill short description here.", + "description": "Returns all months names.", "signature": "TODO: Fill type signature here.", "examples": [ { diff --git a/date/monthNames.md b/date/monthNames.md index 30f0f68a..958ae407 100644 --- a/date/monthNames.md +++ b/date/monthNames.md @@ -1 +1,3 @@ # monthNames + +Returns all months names. diff --git a/date/monthNames.test.ts b/date/monthNames.test.ts index 0a888d04..1abda834 100644 --- a/date/monthNames.test.ts +++ b/date/monthNames.test.ts @@ -3,7 +3,22 @@ import monthNames from "./monthNames.ts"; describe("monthNames", () => { - it.skip("TODO", () => { - expect(monthNames()).toBeDefined(); + it("Returns all month names", () => { + const allMonthNames = [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December" + ]; + + expect(monthNames()).toEqual(allMonthNames); }); }); diff --git a/date/monthNames.ts b/date/monthNames.ts index c870f165..909169e7 100644 --- a/date/monthNames.ts +++ b/date/monthNames.ts @@ -1,4 +1,4 @@ -export default [ +export default (): string[] => [ "January", "February", "March", diff --git a/date/startOfDay.json b/date/startOfDay.json index 952fa86a..ef50d412 100644 --- a/date/startOfDay.json +++ b/date/startOfDay.json @@ -1,6 +1,6 @@ { "name": "startOfDay", - "description": "TODO: Fill short description here.", + "description": "Calculates start of a day in provided date.", "signature": "TODO: Fill type signature here.", "examples": [ { diff --git a/date/startOfDay.md b/date/startOfDay.md index e257769b..ca44b63a 100644 --- a/date/startOfDay.md +++ b/date/startOfDay.md @@ -1 +1,3 @@ # startOfDay + +Calculates start of a day in provided date. diff --git a/date/startOfDay.test.ts b/date/startOfDay.test.ts index ca6f59c1..a0da3058 100644 --- a/date/startOfDay.test.ts +++ b/date/startOfDay.test.ts @@ -3,7 +3,22 @@ import startOfDay from "./startOfDay.ts"; describe("startOfDay", () => { - it.skip("TODO", () => { - expect(startOfDay()).toBeDefined(); + const date = new Date("2018-12-31T13:54:33.232Z"); + + it("should handle timezone offsets", () => { + const localOffset = -300; + + const start = startOfDay(date, localOffset); + const expectedUTC = new Date("2018-12-31T05:00:00.000Z"); + const expectedLocal = new Date("2018-12-31T00:00:00.000-05:00"); + + expect(start).toEqual(expectedUTC); + expect(start).toEqual(expectedLocal); + }); + + it("should handle default local date time display", () => { + const localOffset = 0; + + expect(startOfDay(date)).toEqual(startOfDay(date, localOffset)); }); }); diff --git a/date/startOfDay.ts b/date/startOfDay.ts index 82289a4b..fb9c411c 100644 --- a/date/startOfDay.ts +++ b/date/startOfDay.ts @@ -1,6 +1,10 @@ import toLocalDateTime from "./toLocalDateTime"; -export default (date, timezoneOffset = 0, local = true) => { +export default ( + date: string | Date, + timezoneOffset = 0, + local = true +): Date => { const newDate = new Date(date); newDate.setHours(0, 0, 0, 0);