diff --git a/README.md b/README.md index b4e2b621..39a4d41a 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,12 +216,18 @@ Zips given arrays together with the given function. #### dateDiff +Calculates the difference in milliseconds between two dates. + #### dateInRange +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 @@ -224,6 +236,8 @@ Zips given arrays together with the given function. #### endOfDay +Calculates end of a day in provided date. + #### formatDate #### formatDateTime @@ -248,6 +262,8 @@ Zips given arrays together with the given function. #### monthNames +Returns all months names. + #### offsetByBit #### parseHourMinutePair @@ -256,6 +272,8 @@ Zips given arrays together with the given function. #### startOfDay +Calculates start of a day in provided date. + #### subtractDays #### toDate 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..08b92a1a 100644 --- a/date/README.md +++ b/date/README.md @@ -4,12 +4,18 @@ # dateDiff +Calculates the difference in milliseconds between two dates. + # dateInRange +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 @@ -18,6 +24,8 @@ # endOfDay +Calculates end of a day in provided date. + # formatDate # formatDateTime @@ -42,6 +50,8 @@ # monthNames +Returns all months names. + # offsetByBit # parseHourMinutePair @@ -50,6 +60,8 @@ # startOfDay +Calculates start of a day in provided date. + # subtractDays # toDate 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); diff --git a/date/dateInRange.json b/date/dateInRange.json index 35f60ade..bf5236e3 100644 --- a/date/dateInRange.json +++ b/date/dateInRange.json @@ -1,6 +1,6 @@ { "name": "dateInRange", - "description": "TODO: Fill short description here.", + "description": "Checks if provided date is within the range.", "signature": "TODO: Fill type signature here.", "examples": [ { diff --git a/date/dateInRange.md b/date/dateInRange.md index 87f3e12c..226f3cc7 100644 --- a/date/dateInRange.md +++ b/date/dateInRange.md @@ -1 +1,3 @@ # dateInRange + +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 + ); }); }); 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(); 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.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 bed95fea..3bd70300 100644 --- a/date/daysInMonths.test.ts +++ b/date/daysInMonths.test.ts @@ -3,7 +3,45 @@ 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..6a6b17b7 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, 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);