@zayne-labs/toolkit-type-helpers 0.9.45 → 0.9.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,19 +32,21 @@ type AnyNumber = number & Record<never, never>;
32
32
  type LiteralUnion<TUnion extends TBase, TBase = string> = TUnion | (TBase & Record<never, never>);
33
33
 
34
34
  type ArrayOrObject = Record<number | string | symbol, unknown> | unknown[];
35
- type WriteableVariantUnion = "deep" | "shallow";
35
+ type WriteableLevel = "deep" | "shallow";
36
36
  /**
37
37
  * Makes all properties in an object type writeable (removes readonly modifiers).
38
38
  * Supports both shallow and deep modes, and handles special cases like arrays, tuples, and unions.
39
39
  * @template TObject - The object type to make writeable
40
40
  * @template TVariant - The level of writeable transformation ("shallow" | "deep")
41
41
  */
42
- type Writeable<TObject, TVariant extends WriteableVariantUnion = "shallow"> = TObject extends readonly [...infer TTupleItems] ? [
42
+ type Writeable<TObject, TLevel extends WriteableLevel = "shallow"> = TObject extends readonly [
43
+ ...infer TTupleItems
44
+ ] ? [
43
45
  ...{
44
- [Index in keyof TTupleItems]: TVariant extends "deep" ? Writeable<TTupleItems[Index], "deep"> : TTupleItems[Index];
46
+ [Index in keyof TTupleItems]: TLevel extends "deep" ? Writeable<TTupleItems[Index], "deep"> : TTupleItems[Index];
45
47
  }
46
48
  ] : TObject extends ArrayOrObject ? {
47
- -readonly [Key in keyof TObject]: TVariant extends "deep" ? Writeable<TObject[Key], "deep"> : TObject[Key];
49
+ -readonly [Key in keyof TObject]: TLevel extends "deep" ? Writeable<TObject[Key], "deep"> : TObject[Key];
48
50
  } : TObject;
49
51
 
50
52
  type ErrorMessages<TType extends number | string | symbol = never> = Partial<Record<"$all" | number | symbol | (AnyString | TType), unknown>> | null;
@@ -75,7 +77,8 @@ type MergeTypes<TArrayOfTypes extends unknown[], TAccumulator = NonNullable<unkn
75
77
  * @template TMergedProperties Merged properties from all types
76
78
  */
77
79
  type UnionDiscriminator<TArrayOfTypes extends unknown[], TErrorMessages extends ErrorMessages<keyof MergeTypes<TArrayOfTypes>> = never, TAccumulator = never, TMergedProperties = MergeTypes<TArrayOfTypes>> = TArrayOfTypes extends [infer TFirstType, ...infer TRest] ? UnionDiscriminator<TRest, TErrorMessages, TAccumulator | AllowOnlyFirst<TFirstType, TMergedProperties, TErrorMessages>, TMergedProperties> : TAccumulator;
78
- type ExtractUnion<TObject, TVariant extends "keys" | "values" = "values"> = TObject extends Array<infer TUnion> | ReadonlyArray<infer TUnion> | Set<infer TUnion> ? TUnion : TObject extends Record<infer TKeys, infer TValues> ? TVariant extends "keys" ? TKeys : Prettify<Writeable<TValues, "deep">> : never;
80
+ type UnionVariant = "keys" | "values";
81
+ type ExtractUnion<TObject, TVariant extends UnionVariant = "values"> = TObject extends Array<infer TUnion> | ReadonlyArray<infer TUnion> | Set<infer TUnion> ? TUnion : TObject extends Record<infer TKeys, infer TValues> ? TVariant extends "keys" ? TKeys : Prettify<Writeable<TValues, "deep">> : never;
79
82
  type UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : never) extends (param: infer TParam) => void ? TParam : never;
80
83
 
81
84
  declare const isString: (value: unknown) => value is string;
@@ -112,7 +115,28 @@ type AssertFn = {
112
115
  };
113
116
  declare const assert: AssertFn;
114
117
 
115
- declare const defineEnum: <const TValue>(value: TValue) => Prettify<Writeable<TValue>>;
116
- declare const defineEnumDeep: <const TValue>(value: TValue) => Prettify<Writeable<TValue, "deep">>;
118
+ type DefineEnumOptions = {
119
+ unionVariant?: UnionVariant;
120
+ writeableLevel?: WriteableLevel;
121
+ };
122
+ declare const defineEnum: <const TValue extends object, TOptions extends DefineEnumOptions = DefineEnumOptions>(value: TValue, _options?: TOptions) => Prettify<Writeable<TValue, TOptions["writeableLevel"] extends WriteableLevel ? TOptions["writeableLevel"] : "shallow">> & {
123
+ "~inferredUnion": ExtractUnion<TValue, TOptions["unionVariant"] extends UnionVariant ? TOptions["unionVariant"] : "values">;
124
+ };
125
+ type DefineEnumDeepOptions = Pick<DefineEnumOptions, "unionVariant">;
126
+ declare const defineEnumDeep: <const TValue extends object, TOptions extends DefineEnumDeepOptions = DefineEnumDeepOptions>(value: TValue, _options?: TOptions) => (Writeable<TValue, (TOptions & {
127
+ writeableLevel: "deep";
128
+ })["writeableLevel"] extends WriteableLevel ? (TOptions & {
129
+ writeableLevel: "deep";
130
+ })["writeableLevel"] : "shallow"> extends infer T ? { [Key in keyof T]: Writeable<TValue, (TOptions & {
131
+ writeableLevel: "deep";
132
+ })["writeableLevel"] extends WriteableLevel ? (TOptions & {
133
+ writeableLevel: "deep";
134
+ })["writeableLevel"] : "shallow">[Key]; } : never) & {
135
+ "~inferredUnion": ExtractUnion<TValue, (TOptions & {
136
+ writeableLevel: "deep";
137
+ })["unionVariant"] extends UnionVariant ? (TOptions & {
138
+ writeableLevel: "deep";
139
+ })["unionVariant"] : "values">;
140
+ };
117
141
 
118
- export { type AnyAsyncFunction, type AnyFunction, type AnyNumber, type AnyObject, type AnyString, AssertionError, type Awaitable, type CallbackFn, type EmptyObject, type ExtractUnion, type LiteralUnion, type NonEmptyArray, type NonFalsy, type Prettify, type PrettyOmit, type PrettyPick, type SelectorFn, type UnionDiscriminator, type UnionToIntersection, type UnknownObject, type UnknownObjectWithAnyKey, type UnmaskType, type Writeable, type WriteableVariantUnion, assert, assertDefined, assertENV, defineEnum, defineEnumDeep, hasObjectPrototype, isArray, isAsyncFunction, isBoolean, isFile, isFormData, isFunction, isIterable, isNumber, isObject, isObjectAndNotArray, isPlainObject, isString, isSymbol };
142
+ export { type AnyAsyncFunction, type AnyFunction, type AnyNumber, type AnyObject, type AnyString, AssertionError, type Awaitable, type CallbackFn, type EmptyObject, type ExtractUnion, type LiteralUnion, type NonEmptyArray, type NonFalsy, type Prettify, type PrettyOmit, type PrettyPick, type SelectorFn, type UnionDiscriminator, type UnionToIntersection, type UnionVariant, type UnknownObject, type UnknownObjectWithAnyKey, type UnmaskType, type Writeable, type WriteableLevel, assert, assertDefined, assertENV, defineEnum, defineEnumDeep, hasObjectPrototype, isArray, isAsyncFunction, isBoolean, isFile, isFormData, isFunction, isIterable, isNumber, isObject, isObjectAndNotArray, isPlainObject, isString, isSymbol };
package/dist/esm/index.js CHANGED
@@ -69,11 +69,11 @@ var assert = (input, messageOrOptions) => {
69
69
  };
70
70
 
71
71
  // src/enum.ts
72
- var defineEnum = (value) => {
72
+ var defineEnum = (value, _options) => {
73
73
  return value;
74
74
  };
75
- var defineEnumDeep = (value) => {
76
- return value;
75
+ var defineEnumDeep = (value, _options) => {
76
+ return defineEnum(value);
77
77
  };
78
78
 
79
79
  export { AssertionError, assert, assertDefined, assertENV, defineEnum, defineEnumDeep, hasObjectPrototype, isArray, isAsyncFunction, isBoolean, isFile, isFormData, isFunction, isIterable, isNumber, isObject, isObjectAndNotArray, isPlainObject, isString, isSymbol };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/guard.ts","../../src/assert.ts","../../src/enum.ts"],"names":[],"mappings":";AAOO,IAAM,QAAW,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEtD,IAAM,QAAW,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEtD,IAAM,QAAW,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEtD,IAAM,SAAY,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEvD,IAAM,OAAU,GAAA,CAAS,KAAsC,KAAA,KAAA,CAAM,QAAQ,KAAK;AAE5E,IAAA,UAAA,GAAa,CAAC,KAAA,KAAmB,KAAiB,YAAA;AAExD,IAAM,WAAW,CAAC,KAAA,KAAmB,OAAO,KAAA,KAAU,YAAY,KAAU,KAAA;AAEtE,IAAA,mBAAA,GAAsB,CAA0B,KAAqC,KAAA;AACjG,EAAA,OAAO,QAAS,CAAA,KAAK,CAAK,IAAA,CAAC,QAAQ,KAAK,CAAA;AACzC;AAEa,IAAA,kBAAA,GAAqB,CAAC,KAAm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AA;AAEf,IAAA,KAAA,CAAM,UAAU,CAAG,EAAA,MAAM,CAAK,EAAA,EAAA,OAAO,KAAK,OAAO,CAAA;AAAA;AAEnD;AAEa,IAAA,aAAA,GAAgB,CAAS,KAAkB,KAAA;AACvD,EAAA,IAAI,SAAS,IAAM,EAAA;AAClB,IAAA,MAAM,IAAI,cAAA,CAAe,CAAwB,qBAAA,EAAA,KAAyB,CAAI,EAAA,CAAA,CAAA;AAAA;AAG/E,EAAO,OAAA,KAAA;AACR;AAEa,IAAA,SAAA,GAAY,CAAC,QAAA,EAA8B,OAAqB,KAAA;AAC5E,EAAA,IAAI,aAAa,MAAW,EAAA;AAC3B,IAAM,MAAA,IAAI,eAAe,OAAO,CAAA;AAAA;AAGjC,EAAO,OAAA,QAAA;AACR;AAea,IAAA,MAAA,GAAmB,CAAC,KAAA,EAAgB,gBAA8C,KAAA;AAC9F,EAAI,IAAA,KAAA,KAAU,KAAS,IAAA,KAAA,IAAS,IAAM,EAAA;AACrC,IAAA,MAAM,OAAU,GAAA,QAAA,CAAS,gBAAgB,CAAA,GAAI,mBAAmB,gBAAkB,EAAA,OAAA;AAElF,IAAM,MAAA,IAAI,eAAe,OAAO,CAAA;AAAA;AAElC;;;AC7Ca,IAAA,UAAA,GAAa,CAAe,KAAkB,KAAA;AAC1D,EAAO,OAAA,KAAA;AACR;AAEa,IAAA,cAAA,GAAiB,CAAe,KAAkB,KAAA;AAC9D,EAAO,OAAA,KAAA;AACR","file":"index.js","sourcesContent":["import type {\n\tAnyAsyncFunction,\n\tAnyFunction,\n\tUnknownObject,\n\tUnknownObjectWithAnyKey,\n} from \"./type-utils/common\";\n\nexport const isString = (value: unknown) => typeof value === \"string\";\n\nexport const isNumber = (value: unknown) => typeof value === \"number\";\n\nexport const isSymbol = (value: unknown) => typeof value === \"symbol\";\n\nexport const isBoolean = (value: unknown) => typeof value === \"boolean\";\n\nexport const isArray = <TArray>(value: unknown): value is TArray[] => Array.isArray(value);\n\nexport const isFormData = (value: unknown) => value instanceof FormData;\n\nexport const isObject = (value: unknown) => typeof value === \"object\" && value !== null;\n\nexport const isObjectAndNotArray = <TObject = UnknownObject>(value: unknown): value is TObject => {\n\treturn isObject(value) && !isArray(value);\n};\n\nexport const hasObjectPrototype = (value: unknown) => {\n\treturn Object.prototype.toString.call(value) === \"[object Object]\";\n};\n\n/**\n * @description Copied from TanStack Query's isPlainObject\n * @see https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L321\n */\nexport const isPlainObject = <TPlainObject extends UnknownObjectWithAnyKey = UnknownObject>(\n\tvalue: unknown\n): value is TPlainObject => {\n\tif (!hasObjectPrototype(value)) {\n\t\treturn false;\n\t}\n\n\t// If has no constructor\n\tconst constructor = (value as object | undefined)?.constructor;\n\tif (constructor === undefined) {\n\t\treturn true;\n\t}\n\n\t// If has modified prototype\n\tconst prototype = constructor.prototype as object;\n\tif (!hasObjectPrototype(prototype)) {\n\t\treturn false;\n\t}\n\n\t// If constructor does not have an Object-specific method\n\tif (!Object.hasOwn(prototype, \"isPrototypeOf\")) {\n\t\treturn false;\n\t}\n\n\t// Handles Objects created by Object.create(<arbitrary prototype>)\n\tif (Object.getPrototypeOf(value) !== Object.prototype) {\n\t\treturn false;\n\t}\n\n\t// It's probably a plain object at this point\n\treturn true;\n};\n\nexport const isFunction = <TFunction extends AnyFunction>(value: unknown): value is TFunction => {\n\treturn typeof value === \"function\";\n};\n\nexport const isAsyncFunction = <TAsyncFunction extends AnyAsyncFunction>(\n\tvalue: unknown\n): value is TAsyncFunction => {\n\treturn isFunction(value) && value.constructor.name === \"AsyncFunction\";\n};\n\nexport const isFile = (value: unknown): value is File => value instanceof File;\n\nexport const isIterable = <TIterable>(obj: object): obj is Iterable<TIterable> => Symbol.iterator in obj;\n","import { isString } from \"./guard\";\n\nexport class AssertionError extends Error {\n\toverride name = \"AssertionError\";\n\n\tconstructor(message?: string) {\n\t\tconst prefix = \"Assertion failed\";\n\n\t\tsuper(message ? `${prefix}: ${message}` : message);\n\t}\n}\n\nexport const assertDefined = <TValue>(value: TValue) => {\n\tif (value == null) {\n\t\tthrow new AssertionError(`The value passed is \"${value as null | undefined}!\"`);\n\t}\n\n\treturn value;\n};\n\nexport const assertENV = (variable: string | undefined, message?: string) => {\n\tif (variable === undefined) {\n\t\tthrow new AssertionError(message);\n\t}\n\n\treturn variable;\n};\n\ntype AssertOptions = {\n\tmessage: string;\n};\n\ntype AssertFn = {\n\t(condition: boolean, messageOrOptions?: string | AssertOptions): asserts condition;\n\n\t<TValue>(\n\t\tvalue: TValue,\n\t\tmessageOrOptions?: string | AssertOptions\n\t): asserts value is NonNullable<TValue>;\n};\n\nexport const assert: AssertFn = (input: unknown, messageOrOptions?: string | AssertOptions) => {\n\tif (input === false || input == null) {\n\t\tconst message = isString(messageOrOptions) ? messageOrOptions : messageOrOptions?.message;\n\n\t\tthrow new AssertionError(message);\n\t}\n};\n","import type { Prettify, Writeable } from \"./type-utils\";\n\nexport const defineEnum = <const TValue>(value: TValue) => {\n\treturn value as Prettify<Writeable<TValue>>;\n};\n\nexport const defineEnumDeep = <const TValue>(value: TValue) => {\n\treturn value as Prettify<Writeable<TValue, \"deep\">>;\n};\n"]}
1
+ {"version":3,"sources":["../../src/guard.ts","../../src/assert.ts","../../src/enum.ts"],"names":[],"mappings":";AAOO,IAAM,QAAW,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEtD,IAAM,QAAW,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEtD,IAAM,QAAW,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEtD,IAAM,SAAY,GAAA,CAAC,KAAmB,KAAA,OAAO,KAAU,KAAA;AAEvD,IAAM,OAAU,GAAA,CAAS,KAAsC,KAAA,KAAA,CAAM,QAAQ,KAAK;AAE5E,IAAA,UAAA,GAAa,CAAC,KAAA,KAAmB,KAAiB,YAAA;AAExD,IAAM,WAAW,CAAC,KAAA,KAAmB,OAAO,KAAA,KAA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";\n\nexport const isString = (value: unknown) => typeof value === \"string\";\n\nexport const isNumber = (value: unknown) => typeof value === \"number\";\n\nexport const isSymbol = (value: unknown) => typeof value === \"symbol\";\n\nexport const isBoolean = (value: unknown) => typeof value === \"boolean\";\n\nexport const isArray = <TArray>(value: unknown): value is TArray[] => Array.isArray(value);\n\nexport const isFormData = (value: unknown) => value instanceof FormData;\n\nexport const isObject = (value: unknown) => typeof value === \"object\" && value !== null;\n\nexport const isObjectAndNotArray = <TObject = UnknownObject>(value: unknown): value is TObject => {\n\treturn isObject(value) && !isArray(value);\n};\n\nexport const hasObjectPrototype = (value: unknown) => {\n\treturn Object.prototype.toString.call(value) === \"[object Object]\";\n};\n\n/**\n * @description Copied from TanStack Query's isPlainObject\n * @see https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L321\n */\nexport const isPlainObject = <TPlainObject extends UnknownObjectWithAnyKey = UnknownObject>(\n\tvalue: unknown\n): value is TPlainObject => {\n\tif (!hasObjectPrototype(value)) {\n\t\treturn false;\n\t}\n\n\t// If has no constructor\n\tconst constructor = (value as object | undefined)?.constructor;\n\tif (constructor === undefined) {\n\t\treturn true;\n\t}\n\n\t// If has modified prototype\n\tconst prototype = constructor.prototype as object;\n\tif (!hasObjectPrototype(prototype)) {\n\t\treturn false;\n\t}\n\n\t// If constructor does not have an Object-specific method\n\tif (!Object.hasOwn(prototype, \"isPrototypeOf\")) {\n\t\treturn false;\n\t}\n\n\t// Handles Objects created by Object.create(<arbitrary prototype>)\n\tif (Object.getPrototypeOf(value) !== Object.prototype) {\n\t\treturn false;\n\t}\n\n\t// It's probably a plain object at this point\n\treturn true;\n};\n\nexport const isFunction = <TFunction extends AnyFunction>(value: unknown): value is TFunction => {\n\treturn typeof value === \"function\";\n};\n\nexport const isAsyncFunction = <TAsyncFunction extends AnyAsyncFunction>(\n\tvalue: unknown\n): value is TAsyncFunction => {\n\treturn isFunction(value) && value.constructor.name === \"AsyncFunction\";\n};\n\nexport const isFile = (value: unknown): value is File => value instanceof File;\n\nexport const isIterable = <TIterable>(obj: object): obj is Iterable<TIterable> => Symbol.iterator in obj;\n","import { isString } from \"./guard\";\n\nexport class AssertionError extends Error {\n\toverride name = \"AssertionError\";\n\n\tconstructor(message?: string) {\n\t\tconst prefix = \"Assertion failed\";\n\n\t\tsuper(message ? `${prefix}: ${message}` : message);\n\t}\n}\n\nexport const assertDefined = <TValue>(value: TValue) => {\n\tif (value == null) {\n\t\tthrow new AssertionError(`The value passed is \"${value as null | undefined}!\"`);\n\t}\n\n\treturn value;\n};\n\nexport const assertENV = (variable: string | undefined, message?: string) => {\n\tif (variable === undefined) {\n\t\tthrow new AssertionError(message);\n\t}\n\n\treturn variable;\n};\n\ntype AssertOptions = {\n\tmessage: string;\n};\n\ntype AssertFn = {\n\t(condition: boolean, messageOrOptions?: string | AssertOptions): asserts condition;\n\n\t<TValue>(\n\t\tvalue: TValue,\n\t\tmessageOrOptions?: string | AssertOptions\n\t): asserts value is NonNullable<TValue>;\n};\n\nexport const assert: AssertFn = (input: unknown, messageOrOptions?: string | AssertOptions) => {\n\tif (input === false || input == null) {\n\t\tconst message = isString(messageOrOptions) ? messageOrOptions : messageOrOptions?.message;\n\n\t\tthrow new AssertionError(message);\n\t}\n};\n","import type { ExtractUnion, Prettify, UnionVariant, Writeable, WriteableLevel } from \"./type-utils\";\n\ntype DefineEnumOptions = {\n\tunionVariant?: UnionVariant;\n\twriteableLevel?: WriteableLevel;\n};\n\nexport const defineEnum = <\n\tconst TValue extends object,\n\tTOptions extends DefineEnumOptions = DefineEnumOptions,\n>(\n\tvalue: TValue,\n\t_options?: TOptions\n) => {\n\ttype UnionProp = {\n\t\t\"~inferredUnion\": ExtractUnion<\n\t\t\tTValue,\n\t\t\tTOptions[\"unionVariant\"] extends UnionVariant ? TOptions[\"unionVariant\"] : \"values\"\n\t\t>;\n\t};\n\n\treturn value as Prettify<\n\t\tWriteable<\n\t\t\tTValue,\n\t\t\tTOptions[\"writeableLevel\"] extends WriteableLevel ? TOptions[\"writeableLevel\"] : \"shallow\"\n\t\t>\n\t>\n\t\t& UnionProp;\n};\n\ntype DefineEnumDeepOptions = Pick<DefineEnumOptions, \"unionVariant\">;\n\nexport const defineEnumDeep = <\n\tconst TValue extends object,\n\tTOptions extends DefineEnumDeepOptions = DefineEnumDeepOptions,\n>(\n\tvalue: TValue,\n\t_options?: TOptions\n) => {\n\treturn defineEnum<TValue, TOptions & { writeableLevel: \"deep\" }>(value);\n};\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zayne-labs/toolkit-type-helpers",
3
3
  "type": "module",
4
- "version": "0.9.45",
4
+ "version": "0.9.46",
5
5
  "description": "A collection of utility functions, types and composables used by my other projects. Nothing too fancy but can be useful.",
6
6
  "author": "Ryan Zayne",
7
7
  "license": "MIT",