diff --git a/CHANGELOG.md b/CHANGELOG.md index d8b2c46..eaaaf46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,29 @@ # deverything +## 0.51.1 + +### Patch Changes + +- setUrlSearchParams fix + +## 0.51.0 + +### Minor Changes + +- startOfDay + +## 0.50.0 + +### Minor Changes + +- dates + +## 0.49.0 + +### Minor Changes + +- isStrictlyBetween + ## 0.48.1 ### Patch Changes diff --git a/README.md b/README.md index 79a0fa9..ddd2600 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,21 @@ Contributions always welcome! - `isURL()` - `isUUID()` if it's a valid UUID +### Dates + +- `isOver18()` +- `startOfNextMonth()` +- `startOfNextWeek()` +- `startOfThisWeek()` +- `startOfToday()` +- `startOfTomorrow()` + ### Math - `average()` +- `isBetween()` +- `isOutside()` +- `isStrictlyBetween()` - `max()` - `min()` - `multiply()` diff --git a/package.json b/package.json index f9690f4..c70cc85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.48.1", + "version": "0.51.1", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", @@ -17,7 +17,7 @@ "lint": "TIMING=1 eslint src --fix", "prepublish": "pnpm test && pnpm build", "test": "jest", - "bump": "pnpm changeset && pnpm changeset version" + "release": "pnpm changeset && pnpm changeset version" }, "repository": { "type": "git", diff --git a/src/dates/index.ts b/src/dates/index.ts new file mode 100644 index 0000000..0655ab1 --- /dev/null +++ b/src/dates/index.ts @@ -0,0 +1,7 @@ +export * from "./isOver18"; +export * from "./startOfNextMonth"; +export * from "./startOfNextWeek"; +export * from "./startOfThisWeek"; +export * from "./startOfDay"; +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/startOfDay.ts b/src/dates/startOfDay.ts new file mode 100644 index 0000000..663db48 --- /dev/null +++ b/src/dates/startOfDay.ts @@ -0,0 +1,3 @@ +export const startOfDay = (day: Date): Date => { + return new Date(day.getFullYear(), day.getMonth(), day.getDate()); +}; 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..5a43a03 --- /dev/null +++ b/src/dates/startOfToday.ts @@ -0,0 +1,6 @@ +import { startOfDay } from "./startOfDay"; + +export const startOfToday = (): Date => { + const now = new Date(); + return startOfDay(now); +}; 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/helpers/setUrlSearchParams.test.ts b/src/helpers/setUrlSearchParams.test.ts index 2e18bc6..e1c46cf 100644 --- a/src/helpers/setUrlSearchParams.test.ts +++ b/src/helpers/setUrlSearchParams.test.ts @@ -2,8 +2,18 @@ import { expect, describe, test } from "@jest/globals"; import { setUrlSearchParams } from "./setUrlSearchParams"; describe("setUrlSearchParams", () => { + test("no nullish", () => { + expect( + setUrlSearchParams("/signin?token#hash", { n: null, u: undefined, z: 0 }) + ).toBe("/signin?token=&z=0#hash"); + }); + test("relative url", () => { expect(setUrlSearchParams("/signin")).toBe("/signin"); + expect(setUrlSearchParams("/signin/", { ok: true })).toBe( + "/signin/?ok=true" + ); + expect(setUrlSearchParams("/signin/", {})).toBe("/signin/"); expect(setUrlSearchParams("/signin?")).toBe("/signin"); expect(setUrlSearchParams("/signin?in")).toBe("/signin?in"); expect(setUrlSearchParams("/signin?in#sec")).toBe("/signin?in#sec"); diff --git a/src/helpers/setUrlSearchParams.ts b/src/helpers/setUrlSearchParams.ts index e07b45f..567ed36 100644 --- a/src/helpers/setUrlSearchParams.ts +++ b/src/helpers/setUrlSearchParams.ts @@ -1,6 +1,8 @@ +import { Maybe } from "../types"; + export const setUrlSearchParams = ( currentURL: string, - searchParams: Record = {} + searchParams: Record> = {} ) => { const isRelativeUrl = currentURL.startsWith("/"); const url = new URL( @@ -9,6 +11,7 @@ export const setUrlSearchParams = ( ); Object.entries(searchParams).forEach(([paramKey, paramValue]) => { + if (paramValue === null || paramValue === undefined) return; url.searchParams.set(paramKey, paramValue.toString()); }); 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/math/index.ts b/src/math/index.ts index 0e844d0..2e086cc 100644 --- a/src/math/index.ts +++ b/src/math/index.ts @@ -1,6 +1,7 @@ export * from "./average"; export * from "./isBetween"; export * from "./isOutside"; +export * from "./isStrictlyBetween"; export * from "./max"; export * from "./min"; export * from "./multiply"; diff --git a/src/math/isStrictlyBetween.ts b/src/math/isStrictlyBetween.ts new file mode 100644 index 0000000..4d932eb --- /dev/null +++ b/src/math/isStrictlyBetween.ts @@ -0,0 +1,3 @@ +export const isStrictlyBetween = (value: number, min: number, max: number) => { + return value > min && value < max; +}; 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;