Skip to content

Commit

Permalink
1.11.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Dec 14, 2024
1 parent 020c591 commit 90eb2a6
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 1.11.2

### Patch Changes

- small patch on helper and 2 type renames

## 1.11.1

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ These functions are optimized for low entropy random data generation useful for

- `Coords`
- `DateLike`
- `DefinedKeys<>`
- `Defined<T>`
- `Dimensions`
- `HashMap<>`
- `BoolMap`
Expand All @@ -204,10 +204,10 @@ These functions are optimized for low entropy random data generation useful for
- `ObjectValue<>`
- `ObjectValues<>`
-`PlainObject` use this instead of `Record<,>` or `extends object`, also makes sure it's not an array
- `PickRequired<>`
- `PickDefined<T, K>`
- `PickRequired<T, K>`
- `PlainKey`
- `Point`
- `Required<>`
- `VoidFn`

## Development
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "1.11.1",
"version": "1.11.2",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
10 changes: 8 additions & 2 deletions src/helpers/setUrlSearchParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ describe("setUrlSearchParams", () => {
"/signin?in=&UPPER=CASE#sec"
);
expect(
setUrlSearchParams("/signin?in#sec", { and: "&", equals: "=" })
).toBe("/signin?in=&and=%26&equals=%3D#sec");
setUrlSearchParams("/signin?in#sec", {
and: "&",
equals: "=",
filter: { date: "2024" },
})
).toBe(
"/signin?in=&and=%26&equals=%3D&filter=%7B%22date%22%3A%222024%22%7D#sec"
);
});

test("ip", () => {
Expand Down
13 changes: 10 additions & 3 deletions src/helpers/setUrlSearchParams.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Maybe } from "../types";
import { Maybe, PlainObject } from "../types";
import { isObject } from "../validators";

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

Object.entries(searchParams).forEach(([paramKey, paramValue]) => {
if (paramValue === null || paramValue === undefined) return;
url.searchParams.set(paramKey, paramValue.toString());

if (isObject(paramValue))
url.searchParams.set(paramKey, JSON.stringify(paramValue));
else url.searchParams.set(paramKey, paramValue.toString());
});

if (isRelativeUrl) {
Expand Down
6 changes: 3 additions & 3 deletions src/types/DefinedKeys.ts → src/types/Defined.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* Makes all keys in required and removes undefined and null from the value types.
* Makes all keys required and removes undefined and null from the value types.
* @example
* type Example = {
* a: string;
* b: string | undefined;
* c?: string;
* d?: number | null;
* };
* type Result = DefinedKeys<Example>;
* type Result = Defined<Example>;
* {
* a: string,
* b: string,
* c: string,
* d: number
* }
*/
export type DefinedKeys<T> = {
export type Defined<T> = {
[P in keyof T]-?: NonNullable<T[P]>;
};
30 changes: 30 additions & 0 deletions src/types/PickDefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Defined } from "./Defined";

/**
* Makes picked keys required and defined.
* @example
* type Example = {
* a: string;
* b: string | undefined;
* c?: string;
* d?: number | null;
* };
* type Result = PickDefined<Example, "a" | "b" | "d">;
* {
* a: string;
* b: string | undefined;
* d: number | null;
* }
*/
export type PickDefined<T, K extends keyof T> = Pick<Defined<T>, K>;

// Test
// type Example = {
// a: string;
// b: string | undefined;
// c?: string;
// d?: number | null;
// e: never;
// };

// type Result = PickDefined<Example, "a" | "b" | "c" | "d" | "e">;
15 changes: 13 additions & 2 deletions src/types/PickRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@
* c?: string;
* d?: number | null;
* };
* type Result = PickRequired<Example>;
* type Result = PickRequired<Example, "a" | "b" | "d">;
* {
* a: string;
* b: string | undefined;
* d: number | null;
* }
*/
export type PickRequired<T, K extends keyof T> = Required<Pick<T, K>>;
export type PickRequired<T, K extends keyof T> = Pick<Required<T>, K>;

// Test
// type Example = {
// a: string;
// b: string | undefined;
// c?: string;
// d?: number | null;
// e: never;
// };

// type Result = PickRequired<Example, "a" | "b" | "c" | "d" | "e">;
4 changes: 3 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
export * from "./Coords";
export * from "./Date";
export * from "./DefinedKeys";
export * from "./Defined";
export * from "./Dimensions";
export * from "./Function";
export * from "./HashMap";
export * from "./Matrix";
export * from "./Maybe";
export * from "./NonUndefined";
export * from "./Object";
export * from "./PickDefined";
export * from "./PickRequired";
export * from "./PickRequired";
export * from "./Point";
export * from "./PrismaSelect";
Expand Down

0 comments on commit 90eb2a6

Please sign in to comment.