This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(object): ♻️ Major reorganization (#112)
- Loading branch information
Showing
22 changed files
with
183 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@terminal-nerds/snippets-object": minor | ||
--- | ||
|
||
♻️ Major reorganization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/* MODULES */ | ||
export * from "./keys/keys.ts"; | ||
export * from "./merge/merge.ts"; | ||
export * from "./schema/schema.ts"; | ||
export * from "./misc/index.ts"; | ||
export * from "./schema/index.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./sub/merge.ts"; | ||
export * from "./sub/omit.ts"; | ||
export * from "./sub/pick.ts"; | ||
export * from "./sub/rename.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { SAMPLE_PRIMITIVES } from "@terminal-nerds/snippets-test/sample"; | ||
import { returns, throws } from "@terminal-nerds/snippets-test/unit"; | ||
import { describe, it } from "vitest"; | ||
import { ZodError } from "zod"; | ||
|
||
import { omitObjectEntries } from "./omit.ts"; | ||
|
||
describe("omitObjectEntries(object, keys)", () => { | ||
it(throws(ZodError).on(`invalid object values`).samples(SAMPLE_PRIMITIVES), ({ expect }) => { | ||
// @ts-expect-error Testing | ||
expect(() => omitObjectEntries(SAMPLE_PRIMITIVES)).toThrowError(ZodError); | ||
}); | ||
|
||
it( | ||
returns({ b: 2 }) | ||
.on(`sample object`) | ||
.with({ keys: ["a"] }), | ||
({ expect }) => { | ||
expect(omitObjectEntries({ a: 1, b: 2 }, ["a"])).toEqual({ b: 2 }); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { validateObject } from "../../schema/sub/native.ts"; | ||
|
||
export function omitObjectEntries<const O extends object, K extends keyof O>(object: O, keys: K[]): Omit<O, K> { | ||
validateObject(object); | ||
|
||
const keysSet = new Set(keys); | ||
|
||
return Object.fromEntries( | ||
(Object.entries(object) as Array<[K, O[K]]>).filter(([key]) => !keysSet.has(key)), | ||
) as unknown as Omit<O, K>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { SAMPLE_PRIMITIVES } from "@terminal-nerds/snippets-test/sample"; | ||
import { returns, throws } from "@terminal-nerds/snippets-test/unit"; | ||
import { describe, it } from "vitest"; | ||
import { ZodError } from "zod"; | ||
|
||
import { pickObjectEntries } from "./pick.ts"; | ||
|
||
describe("pickObjectEntries(object, keys)", () => { | ||
it(throws(ZodError).on(`invalid object values`).samples(SAMPLE_PRIMITIVES), ({ expect }) => { | ||
// @ts-expect-error Testing | ||
expect(() => pickObjectEntries(SAMPLE_PRIMITIVES)).toThrowError(ZodError); | ||
}); | ||
|
||
it( | ||
returns({ a: 1 }) | ||
.on(`sample object`) | ||
.with({ keys: ["a"] }), | ||
({ expect }) => { | ||
expect(pickObjectEntries({ a: 1, b: 2 }, ["a"])).toEqual({ a: 1 }); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { validateObject } from "../../schema/sub/native.ts"; | ||
|
||
export function pickObjectEntries<const O extends object, K extends keyof O>(object: O, keys: K[]): Pick<O, K> { | ||
validateObject(object); | ||
|
||
const keysSet = new Set(keys); | ||
|
||
return Object.fromEntries((Object.entries(object) as Array<[K, O[K]]>).filter(([key]) => keysSet.has(key))) as Pick< | ||
O, | ||
K | ||
>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { z } from "zod"; | ||
|
||
import { type ObjectKey, SCHEMA_OBJECT_KEY, validateObjectKey } from "../../schema/sub/key.ts"; | ||
import { validateObject } from "../../schema/sub/native.ts"; | ||
|
||
/* prettier-ignore */ | ||
const SCHEMA_RENAMER = z.function() | ||
.args(SCHEMA_OBJECT_KEY, z.number().optional()) | ||
.returns(SCHEMA_OBJECT_KEY); | ||
|
||
export function renameObjectKeys<OldKey extends ObjectKey, Value, NewKey extends ObjectKey>( | ||
object: Record<OldKey, Value>, | ||
renamer: (key: OldKey, index: number) => NewKey, | ||
): Record<NewKey, Value> { | ||
validateObject(object); | ||
SCHEMA_RENAMER.parse(renamer); | ||
|
||
const handleRename = (key: OldKey, index: number) => { | ||
const renamedKey = renamer(key, index); | ||
|
||
validateObjectKey(renamedKey); | ||
|
||
return renamedKey; | ||
}; | ||
|
||
return Object.fromEntries( | ||
(Object.entries(object) as Array<[OldKey, Value]>).map(([key, value], index) => [ | ||
handleRename(key, index), | ||
value, | ||
]), | ||
) as Record<NewKey, Value>; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./sub/empty.ts"; | ||
export * from "./sub/native.ts"; |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { EmptyObject as ObjectEmpty } from "type-fest/source/empty-object"; | ||
import { z } from "zod"; | ||
|
||
export const EMPTY_OBJECT_SCHEMA = z.record(z.undefined()); | ||
|
||
export type { EmptyObject as ObjectEmpty } from "type-fest/source/empty-object"; | ||
|
||
export function isObjectEmpty(value: object): value is ObjectEmpty { | ||
return EMPTY_OBJECT_SCHEMA.safeParse(value).success; | ||
} | ||
|
||
export function validateEmptyObject(value: object): asserts value is ObjectEmpty { | ||
EMPTY_OBJECT_SCHEMA.parse(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { z } from "zod"; | ||
|
||
export const SCHEMA_OBJECT_KEY = z.number().or(z.string()).or(z.symbol()); | ||
|
||
export type ObjectKey = number | string | symbol; | ||
|
||
export function isValidObjectKey(key: unknown): key is ObjectKey { | ||
return SCHEMA_OBJECT_KEY.safeParse(key).success; | ||
} | ||
|
||
export function validateObjectKey(key: unknown): asserts key is ObjectKey { | ||
SCHEMA_OBJECT_KEY.parse(key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { z } from "zod"; | ||
|
||
export const SCHEMA_OBJECT = z.object({}); | ||
|
||
export function isObject(value: unknown): value is object { | ||
return SCHEMA_OBJECT.safeParse(value).success; | ||
} | ||
|
||
export function validateObject(value: unknown): asserts value is object { | ||
SCHEMA_OBJECT.parse(value); | ||
} |