From 00f361361d759c3fd4b182e92ee58f253e16ec5d Mon Sep 17 00:00:00 2001 From: Nicholas Berlette Date: Wed, 19 Oct 2022 12:37:18 -0700 Subject: [PATCH] feat: add all.ts, _internal.ts, mod.d.ts --- _internal.ts | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ mod.d.ts | 12 +++++++ testing/all.ts | 6 ++++ 3 files changed, 116 insertions(+) create mode 100644 _internal.ts create mode 100644 mod.d.ts create mode 100644 testing/all.ts diff --git a/_internal.ts b/_internal.ts new file mode 100644 index 0000000..96e3092 --- /dev/null +++ b/_internal.ts @@ -0,0 +1,98 @@ +export { + filterEntries, + filterKeys, + isKeyOf, + isObject, + mapEntries, + mapKeys, + objectPick, + pick, +} from "./deps.ts"; + +export type Primitive = string | number | bigint | boolean | symbol; + +export type LiteralUnion< + Literal extends BaseType, + BaseType extends Primitive, +> = Literal | (BaseType & Record); + +export type JSONReplacerFn = (this: any, key: string, value: any) => any; + +export type JSONReplacerArray = (string | number)[]; + +export type JSONReviverFn = (this: any, key: string, value: any) => any; + +export type JsonValue = + | { [key: string]: JsonValue | undefined } + | JsonValue[] + | string + | number + | boolean + | null; + +export type { JsonValue as JSONValue }; + +export function objectMap< + Input extends Record, + CallbackFn extends (this: Input, value: V, key: K, array: [K, V][]) => V2, + K extends keyof Input, + V extends Input[K], + V2, +>(obj: Input, callbackfn: CallbackFn) { + const entries = Object.entries(obj) as [K, V][]; + const mappedEntries = entries.map( + ([k, v], _, a) => [k, callbackfn.bind(obj, v, k, a)], + ); + + return Object.fromEntries(mappedEntries) as ( + CallbackFn extends ((v: infer Val, k: infer P, array: [K, V][]) => infer O) + ? [O] extends [Entry] ? { [Key in P as OK]: OV } + : [O] extends [infer OV] ? { [Key in K]: OV } + : never + : never + ); +} + +export type Entry = + readonly [K, V]; + +export type Merge = T extends + infer U extends Record + ? { [K in keyof U]: Deep extends true ? Merge : U[K] } + : T; + +export type MergeTuples = [...A, ...B]; + +export type Tuple = ReadOnly extends true + ? readonly T[] + : T[]; + +export type LastInTuple = T extends + { readonly length: infer L extends number } ? [L] extends [0] ? undefined + : [unknown, ...T][L] + : never; + +export type FirstInTuple = T extends + { readonly length: infer L extends number } ? [L] extends [0] ? undefined + : T[0] + : never; + +export type KeysOfUnion = T extends T ? keyof T : never; + +export type Writable = T extends Record ? { + -readonly [K in keyof T]?: Deep extends true ? Writable : T[K]; + } + : T; + +export type Readonly = T extends Record ? { + readonly [K in keyof T]?: Deep extends true ? Readonly : T[K]; + } + : T; + +export type Optional = T extends Record + ? { [K in keyof T]?: Deep extends true ? Optional : T[K] } + : T; + +export type Required = T extends Record + ? { [K in keyof T]-?: Deep extends true ? Required : T[K] } + : T; diff --git a/mod.d.ts b/mod.d.ts new file mode 100644 index 0000000..9cbcac7 --- /dev/null +++ b/mod.d.ts @@ -0,0 +1,12 @@ +declare type JSONReplacerFn = (this: any, key: string, value: any) => any; + +declare type JSONReviverFn = (this: any, key: string, value: any) => any; +declare type JSONReviverArray = (string | number)[]; + +declare type JSONValue = + | { [key: string]: JSONValue } + | JSONValue[] + | string + | number + | boolean + | null; diff --git a/testing/all.ts b/testing/all.ts new file mode 100644 index 0000000..176c3df --- /dev/null +++ b/testing/all.ts @@ -0,0 +1,6 @@ +import "./asserts.ts"; +import "./bdd.ts"; +import "./chai.ts"; +import "./fc.ts"; +import "./mock.ts"; +import "./snapshot.ts";