diff --git a/CHANGELOG.md b/CHANGELOG.md index c490402..ca7767e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.50.0 + +### Minor Changes + +- dates + ## 0.49.0 ### Minor Changes diff --git a/README.md b/README.md index d13f000..5010e83 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,15 @@ Contributions always welcome! - `isURL()` - `isUUID()` if it's a valid UUID +### Dates + +- `isOver18()` +- `startOfNextMonth()` +- `startOfNextWeek()` +- `startOfThisWeek()` +- `startOfToday()` +- `startOfTomorrow()` + ### Math - `average()` diff --git a/package.json b/package.json index e7c2e15..6894d99 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.49.0", + "version": "0.50.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/dates/index.ts b/src/dates/index.ts new file mode 100644 index 0000000..401ff3c --- /dev/null +++ b/src/dates/index.ts @@ -0,0 +1,6 @@ +export * from "./isOver18"; +export * from "./startOfNextMonth"; +export * from "./startOfNextWeek"; +export * from "./startOfThisWeek"; +export * from "./startOfToday"; +export * from "./startOfTomorrow"; diff --git a/src/dates/isOver18.ts b/src/dates/isOver18.ts new file mode 100644 index 0000000..ed5f8ac --- /dev/null +++ b/src/dates/isOver18.ts @@ -0,0 +1,21 @@ +import { parseDate } from "../helpers"; +import { DateLike } from "../types"; + +export const isOver18 = (birthDate: DateLike) => { + const now = new Date(); + const birth = parseDate(birthDate); + if (!birth) return false; + + const age = now.getFullYear() - birth.getFullYear(); + if (age > 18) return true; + if (age < 18) return false; + if (age === 18) { + if (now.getMonth() > birth.getMonth()) return true; + if (now.getMonth() < birth.getMonth()) return false; + if (now.getMonth() === birth.getMonth()) { + if (now.getDate() >= birth.getDate()) return true; + if (now.getDate() < birth.getDate()) return false; + } + } + return false; +}; diff --git a/src/dates/startOfNextMonth.ts b/src/dates/startOfNextMonth.ts new file mode 100644 index 0000000..e5277b6 --- /dev/null +++ b/src/dates/startOfNextMonth.ts @@ -0,0 +1,4 @@ +export const startOfNextMonth = () => { + const now = new Date(); + return new Date(now.getFullYear(), now.getMonth() + 1, 1); +}; diff --git a/src/dates/startOfNextWeek.ts b/src/dates/startOfNextWeek.ts new file mode 100644 index 0000000..c3f977d --- /dev/null +++ b/src/dates/startOfNextWeek.ts @@ -0,0 +1,8 @@ +export const startOfNextWeek = () => { + const now = new Date(); + return new Date( + now.getFullYear(), + now.getMonth(), + now.getDate() + 7 - now.getDay() + ); +}; diff --git a/src/dates/startOfThisWeek.ts b/src/dates/startOfThisWeek.ts new file mode 100644 index 0000000..adf2b41 --- /dev/null +++ b/src/dates/startOfThisWeek.ts @@ -0,0 +1,8 @@ +export const startOfThisWeek = () => { + const now = new Date(); + return new Date( + now.getFullYear(), + now.getMonth(), + now.getDate() - now.getDay() + ); +}; diff --git a/src/dates/startOfToday.ts b/src/dates/startOfToday.ts new file mode 100644 index 0000000..bc78965 --- /dev/null +++ b/src/dates/startOfToday.ts @@ -0,0 +1,4 @@ +export const startOfToday = () => { + const now = new Date(); + return new Date(now.getFullYear(), now.getMonth(), now.getDate()); +}; diff --git a/src/dates/startOfTomorrow.ts b/src/dates/startOfTomorrow.ts new file mode 100644 index 0000000..76077ab --- /dev/null +++ b/src/dates/startOfTomorrow.ts @@ -0,0 +1,4 @@ +export const startOfTomorrow = () => { + const now = new Date(); + return new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1); +}; diff --git a/src/index.ts b/src/index.ts index 742819e..e75f2bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from "./checks"; +export * from "./dates"; export * from "./formatters"; export * from "./helpers"; export * from "./math"; diff --git a/src/types/Date.ts b/src/types/Date.ts index b2d1fb0..ea6e726 100644 --- a/src/types/Date.ts +++ b/src/types/Date.ts @@ -1,6 +1,8 @@ export type DateLike = Date | string | number; export type Datey = Date | string; +export type ISODate = string; + export type DateRange = { startDate: DateLike; endDate: DateLike; diff --git a/src/types/HashMap.ts b/src/types/HashMap.ts index edc33d3..9314b8d 100644 --- a/src/types/HashMap.ts +++ b/src/types/HashMap.ts @@ -1,7 +1,7 @@ import { PlainKey } from "./Object"; // I don't like the Dict keyword, but it's a possibility... -export type HashMap = Record; +export type HashMap = Record; export type NumberMap = Record; export type StringMap = Record; export type BoolMap = Record;