Skip to content

Commit

Permalink
Merge branch 'main' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jul 5, 2024
2 parents e57726e + c8fc978 commit 4affda1
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 4 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions src/dates/index.ts
Original file line number Diff line number Diff line change
@@ -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";
21 changes: 21 additions & 0 deletions src/dates/isOver18.ts
Original file line number Diff line number Diff line change
@@ -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;
};
3 changes: 3 additions & 0 deletions src/dates/startOfDay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const startOfDay = (day: Date): Date => {
return new Date(day.getFullYear(), day.getMonth(), day.getDate());
};
4 changes: 4 additions & 0 deletions src/dates/startOfNextMonth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const startOfNextMonth = () => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth() + 1, 1);
};
8 changes: 8 additions & 0 deletions src/dates/startOfNextWeek.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const startOfNextWeek = () => {
const now = new Date();
return new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 7 - now.getDay()
);
};
8 changes: 8 additions & 0 deletions src/dates/startOfThisWeek.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const startOfThisWeek = () => {
const now = new Date();
return new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() - now.getDay()
);
};
6 changes: 6 additions & 0 deletions src/dates/startOfToday.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { startOfDay } from "./startOfDay";

export const startOfToday = (): Date => {
const now = new Date();
return startOfDay(now);
};
4 changes: 4 additions & 0 deletions src/dates/startOfTomorrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const startOfTomorrow = () => {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
};
10 changes: 10 additions & 0 deletions src/helpers/setUrlSearchParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/setUrlSearchParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Maybe } from "../types";

export const setUrlSearchParams = (
currentURL: string,
searchParams: Record<string, string | number | boolean> = {}
searchParams: Record<string, Maybe<string | number | boolean>> = {}
) => {
const isRelativeUrl = currentURL.startsWith("/");
const url = new URL(
Expand All @@ -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());
});

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./checks";
export * from "./dates";
export * from "./formatters";
export * from "./helpers";
export * from "./math";
Expand Down
1 change: 1 addition & 0 deletions src/math/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
3 changes: 3 additions & 0 deletions src/math/isStrictlyBetween.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isStrictlyBetween = (value: number, min: number, max: number) => {
return value > min && value < max;
};
2 changes: 2 additions & 0 deletions src/types/Date.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/types/HashMap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PlainKey } from "./Object";

// I don't like the Dict keyword, but it's a possibility...
export type HashMap<T> = Record<PlainKey, T>;
export type HashMap<T = any> = Record<PlainKey, T>;
export type NumberMap = Record<PlainKey, number>;
export type StringMap = Record<PlainKey, string>;
export type BoolMap = Record<PlainKey, boolean>;
Expand Down

0 comments on commit 4affda1

Please sign in to comment.