From 3831c482b3c8aa30ffc2ba6a8664ea4c8256d795 Mon Sep 17 00:00:00 2001 From: Orlando Groppo Date: Mon, 8 Apr 2024 16:55:41 +0100 Subject: [PATCH 1/6] isStrictlyBetween (#16) --- CHANGELOG.md | 6 ++++++ README.md | 25 ++++++++++++++----------- package.json | 2 +- src/math/index.ts | 3 ++- src/math/isStrictlyBetween.ts | 3 +++ 5 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 src/math/isStrictlyBetween.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d8b2c46..c490402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.49.0 + +### Minor Changes + +- isStrictlyBetween + ## 0.48.1 ### Patch Changes diff --git a/README.md b/README.md index ebe5510..d13f000 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ Contributions always welcome! ### Validators -- `isArray()` -- `isBoolean()` +- `isArray()` +- `isBoolean()` - `isBrowser()` to detect if you are on the browser - `isBuffer()` if it's a buffer - `isClient()` same as isBrowser @@ -26,20 +26,20 @@ Contributions always welcome! - `isEmptyArray()` checks if the array has no items - `isEmptyObject()` checks if the object has no keys - `isFile()` if it's a file -- `isFunction()` +- `isFunction()` - `isJsDate()` if it's a **valid** javascript's Date - - `isFutureDate()` - - `isPastDate()` + - `isFutureDate()` + - `isPastDate()` - `isStringDate()` also checks if the string passed is a **valid** date - `isKey()` is a real key of an object - `isLastIndex()` is the index is the last item of array - `isNotEmptyString()` must have some text, checks for spaces-only - `isNumber()` if the arg is number, and also usable (no infinity) - `isInt()` if it's an integer - - `isEven()` - - `isOdd()` - - `isPositiveInt()` - - `isNegativeInt()` + - `isEven()` + - `isOdd()` + - `isPositiveInt()` + - `isNegativeInt()` - `isNumeric()` if string is representing a number - ⭐ `isObject()` if it's a js plain Object - `isPromise()` if it's a promise @@ -49,12 +49,15 @@ Contributions always welcome! - ⭐ `isSame()` Compare if dates, functions, arrays, objects or anything else are the same - `isServer()` if you are on the server - `isString()` -- `isURL()` +- `isURL()` - `isUUID()` if it's a valid UUID ### Math - `average()` +- `isBetween()` +- `isOutside()` +- `isStrictlyBetween()` - `max()` - `min()` - `multiply()` @@ -112,7 +115,7 @@ Contributions always welcome! These functions are optimized for low entropy random data generation useful for Unit Testing, Storybook, Pass real validations, Reverse hacking, Penetration testing... -- `randomAddress()` +- `randomAddress()` - `randomAlphaNumericCode()` - ⭐ `randomArrayItem()` now supporting non-uniform distribution - `randomBankAccount()` diff --git a/package.json b/package.json index f9690f4..e7c2e15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.48.1", + "version": "0.49.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/math/index.ts b/src/math/index.ts index dc11e28..f4648e3 100644 --- a/src/math/index.ts +++ b/src/math/index.ts @@ -1,8 +1,9 @@ export * from "./average"; export * from "./isBetween"; +export * from "./isOutside"; +export * from "./isStrictlyBetween"; export * from "./max"; export * from "./min"; export * from "./multiply"; -export * from "./isOutside"; export * from "./percentageChange"; export * from "./sum"; 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; +}; From 68d64a02070f1c66f9ce13be84db5d39629fbaa4 Mon Sep 17 00:00:00 2001 From: Orlando Date: Sat, 27 Apr 2024 00:01:46 +0100 Subject: [PATCH 2/6] Dates --- CHANGELOG.md | 6 ++++++ README.md | 9 +++++++++ package.json | 2 +- src/dates/index.ts | 6 ++++++ src/dates/isOver18.ts | 21 +++++++++++++++++++++ src/dates/startOfNextMonth.ts | 4 ++++ src/dates/startOfNextWeek.ts | 8 ++++++++ src/dates/startOfThisWeek.ts | 8 ++++++++ src/dates/startOfToday.ts | 4 ++++ src/dates/startOfTomorrow.ts | 4 ++++ src/index.ts | 1 + src/types/Date.ts | 2 ++ src/types/HashMap.ts | 2 +- 13 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/dates/index.ts create mode 100644 src/dates/isOver18.ts create mode 100644 src/dates/startOfNextMonth.ts create mode 100644 src/dates/startOfNextWeek.ts create mode 100644 src/dates/startOfThisWeek.ts create mode 100644 src/dates/startOfToday.ts create mode 100644 src/dates/startOfTomorrow.ts 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; From 049d252a1acd7ca5c686d4e96acb9fd38bea5a71 Mon Sep 17 00:00:00 2001 From: Orlando Date: Fri, 10 May 2024 19:06:48 +0100 Subject: [PATCH 3/6] startOfDay --- CHANGELOG.md | 6 ++++++ package.json | 4 ++-- src/dates/index.ts | 1 + src/dates/startOfDay.ts | 3 +++ src/dates/startOfToday.ts | 6 ++++-- 5 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 src/dates/startOfDay.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ca7767e..f439d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.51.0 + +### Minor Changes + +- startOfDay + ## 0.50.0 ### Minor Changes diff --git a/package.json b/package.json index 6894d99..3e1fba2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.50.0", + "version": "0.51.0", "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 index 401ff3c..0655ab1 100644 --- a/src/dates/index.ts +++ b/src/dates/index.ts @@ -2,5 +2,6 @@ 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/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/startOfToday.ts b/src/dates/startOfToday.ts index bc78965..5a43a03 100644 --- a/src/dates/startOfToday.ts +++ b/src/dates/startOfToday.ts @@ -1,4 +1,6 @@ -export const startOfToday = () => { +import { startOfDay } from "./startOfDay"; + +export const startOfToday = (): Date => { const now = new Date(); - return new Date(now.getFullYear(), now.getMonth(), now.getDate()); + return startOfDay(now); }; From f32422c3db77136d012b664f0a859dacf7316651 Mon Sep 17 00:00:00 2001 From: Orlando Date: Fri, 14 Jun 2024 18:42:12 +0100 Subject: [PATCH 4/6] setUrlSearchParams fix --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/helpers/setUrlSearchParams.test.ts | 6 ++++++ src/helpers/setUrlSearchParams.ts | 5 ++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f439d3e..eaaaf46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.51.1 + +### Patch Changes + +- setUrlSearchParams fix + ## 0.51.0 ### Minor Changes diff --git a/package.json b/package.json index 3e1fba2..c70cc85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.51.0", + "version": "0.51.1", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/helpers/setUrlSearchParams.test.ts b/src/helpers/setUrlSearchParams.test.ts index 2e18bc6..1a4ba0c 100644 --- a/src/helpers/setUrlSearchParams.test.ts +++ b/src/helpers/setUrlSearchParams.test.ts @@ -2,6 +2,12 @@ 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?")).toBe("/signin"); 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()); }); From a572bc925a641589f7b85823b312cd5b968e10af Mon Sep 17 00:00:00 2001 From: Orlando Date: Fri, 14 Jun 2024 21:18:08 +0100 Subject: [PATCH 5/6] extra test --- src/helpers/setUrlSearchParams.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/helpers/setUrlSearchParams.test.ts b/src/helpers/setUrlSearchParams.test.ts index 1a4ba0c..7bdf128 100644 --- a/src/helpers/setUrlSearchParams.test.ts +++ b/src/helpers/setUrlSearchParams.test.ts @@ -10,6 +10,9 @@ describe("setUrlSearchParams", () => { 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?in")).toBe("/signin?in"); expect(setUrlSearchParams("/signin?in#sec")).toBe("/signin?in#sec"); From c8fc97884771df19028999541320e6e158518684 Mon Sep 17 00:00:00 2001 From: Orlando Date: Sun, 16 Jun 2024 22:01:50 +0100 Subject: [PATCH 6/6] empty obj --- src/helpers/setUrlSearchParams.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/helpers/setUrlSearchParams.test.ts b/src/helpers/setUrlSearchParams.test.ts index 7bdf128..e1c46cf 100644 --- a/src/helpers/setUrlSearchParams.test.ts +++ b/src/helpers/setUrlSearchParams.test.ts @@ -13,6 +13,7 @@ describe("setUrlSearchParams", () => { 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");