@zayne-labs/toolkit-type-helpers 0.8.22

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.
@@ -0,0 +1,63 @@
1
+ type Prettify<TObject> = NonNullable<unknown> & {
2
+ [Key in keyof TObject]: TObject[Key];
3
+ };
4
+ type UnmaskType<TType> = {
5
+ _: TType;
6
+ }["_"];
7
+ type PrettyOmit<TObject, Key extends keyof TObject> = Prettify<Omit<TObject, Key>>;
8
+ type PrettyPick<TObject, Key extends keyof TObject> = Prettify<Pick<TObject, Key>>;
9
+ type CallbackFn<in TParams, out TResult = void> = (...params: TParams[]) => TResult;
10
+ type SelectorFn<TStore, TResult> = (state: TStore) => TResult;
11
+ type WriteableLevel = "deep" | "shallow";
12
+ type Writeable<TObject, TType extends WriteableLevel = "shallow"> = {
13
+ -readonly [key in keyof TObject]: TType extends "shallow" ? TObject[key] : TType extends "deep" ? TObject[key] extends object ? Writeable<TObject[key], TType> : TObject[key] : never;
14
+ };
15
+ type InferEnum<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;
16
+ type NonEmptyArray<TArrayItem> = [TArrayItem, ...TArrayItem[]];
17
+ type AnyObject = UnmaskType<Record<string, unknown>>;
18
+ type AnyFunction<TResult = any> = UnmaskType<(...args: any[]) => TResult>;
19
+ type AnyAsyncFunction<TResult = any> = UnmaskType<(...args: any[]) => Promise<TResult>>;
20
+ /**
21
+ * - This types allows for adding arbitrary literal types, while still provided autocomplete for defaults.
22
+ * - Usually intersection with "{}" or "NonNullable<unknown>" would make it work fine, but the "ignore" property with never type will help make it appear at the bottom of intellisense list and errors because of never .
23
+ * @see [typescript issue](https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609)
24
+ */
25
+ type AnyString = string & Record<never, never>;
26
+ /**
27
+ * - This types allows for adding arbitrary literal types, while still provided autocomplete for defaults.
28
+ * - Usually intersection with "{}" or "NonNullable<unknown>" would make it work fine, but the "ignore" property with never type will help make it appear at the bottom of intellisense list and errors because of never .
29
+ * @see [typescript issue](https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609)
30
+ */
31
+ type AnyNumber = number & Record<never, never>;
32
+ type LiteralUnion<TUnion extends TBase, TBase = string> = TUnion | (TBase & Record<never, never>);
33
+
34
+ declare const isString: (value: unknown) => value is string;
35
+ declare const isBoolean: (value: unknown) => value is boolean;
36
+ declare const isArray: <TArray>(value: unknown) => value is TArray[];
37
+ declare const isFormData: (value: unknown) => value is FormData;
38
+ declare const isObject: (value: unknown) => value is object;
39
+ declare const isObjectAndNotArray: <TObject = Record<string, unknown>>(value: unknown) => value is TObject;
40
+ declare const isPlainObject: <TObject extends AnyObject>(value: unknown) => value is TObject;
41
+ declare const isFunction: <TFunction extends AnyFunction>(value: unknown) => value is TFunction;
42
+ declare const isAsyncFunction: <TAsyncFunction extends AnyAsyncFunction>(value: unknown) => value is TAsyncFunction;
43
+ declare const isFile: (value: unknown) => value is File;
44
+ declare const isIterable: <TIterable>(obj: object) => obj is Iterable<TIterable>;
45
+
46
+ declare class AssertionError extends Error {
47
+ name: string;
48
+ constructor(message?: string);
49
+ }
50
+ declare const assertDefined: <TValue>(value: TValue) => NonNullable<TValue>;
51
+ declare const assertENV: (variable: string | undefined, message?: string) => string;
52
+ type AssertOptions = {
53
+ message: string;
54
+ };
55
+ type AssertFn = {
56
+ (condition: boolean, messageOrOptions?: string | AssertOptions): asserts condition;
57
+ <TValue>(value: TValue, messageOrOptions?: string | AssertOptions): asserts value is NonNullable<TValue>;
58
+ };
59
+ declare const assert: AssertFn;
60
+
61
+ declare const defineEnum: <const TValue, TWriteableLevel extends WriteableLevel = "shallow">(value: TValue) => Prettify<Writeable<TValue, TWriteableLevel>>;
62
+
63
+ export { type AnyAsyncFunction, type AnyFunction, type AnyNumber, type AnyObject, type AnyString, AssertionError, type CallbackFn, type InferEnum, type LiteralUnion, type NonEmptyArray, type Prettify, type PrettyOmit, type PrettyPick, type SelectorFn, type UnmaskType, type Writeable, type WriteableLevel, assert, assertDefined, assertENV, defineEnum, isArray, isAsyncFunction, isBoolean, isFile, isFormData, isFunction, isIterable, isObject, isObjectAndNotArray, isPlainObject, isString };
@@ -0,0 +1,58 @@
1
+ // src/guard.ts
2
+ var isString = (value) => typeof value === "string";
3
+ var isBoolean = (value) => typeof value === "boolean";
4
+ var isArray = (value) => Array.isArray(value);
5
+ var isFormData = (value) => value instanceof FormData;
6
+ var isObject = (value) => typeof value === "object" && value !== null;
7
+ var isObjectAndNotArray = (value) => {
8
+ return isObject(value) && !isArray(value);
9
+ };
10
+ var isPlainObject = (value) => {
11
+ if (!isObject(value)) {
12
+ return false;
13
+ }
14
+ const prototype = Object.getPrototypeOf(value);
15
+ return (prototype == null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value);
16
+ };
17
+ var isFunction = (value) => {
18
+ return typeof value === "function";
19
+ };
20
+ var isAsyncFunction = (value) => {
21
+ return isFunction(value) && value.constructor.name === "AsyncFunction";
22
+ };
23
+ var isFile = (value) => value instanceof File;
24
+ var isIterable = (obj) => Symbol.iterator in obj;
25
+
26
+ // src/assert.ts
27
+ var AssertionError = class extends Error {
28
+ name = "AssertionError";
29
+ constructor(message) {
30
+ const prefix = "Assertion failed";
31
+ super(message ? `${prefix}: ${message}` : message);
32
+ }
33
+ };
34
+ var assertDefined = (value) => {
35
+ if (value == null) {
36
+ throw new AssertionError(`The value passed is "${value}!"`);
37
+ }
38
+ return value;
39
+ };
40
+ var assertENV = (variable, message) => {
41
+ if (variable === void 0) {
42
+ throw new AssertionError(message);
43
+ }
44
+ return variable;
45
+ };
46
+ var assert = (input, messageOrOptions) => {
47
+ if (input === false || input == null) {
48
+ const message = isString(messageOrOptions) ? messageOrOptions : messageOrOptions?.message;
49
+ throw new AssertionError(message);
50
+ }
51
+ };
52
+
53
+ // src/common.ts
54
+ var defineEnum = (value) => value;
55
+
56
+ export { AssertionError, assert, assertDefined, assertENV, defineEnum, isArray, isAsyncFunction, isBoolean, isFile, isFormData, isFunction, isIterable, isObject, isObjectAndNotArray, isPlainObject, isString };
57
+ //# sourceMappingURL=index.js.map
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/guard.ts","../../src/assert.ts","../../src/common.ts"],"names":[],"mappings":";AAEO,IAAM,QAAW,GAAA,CAAC,KAAoC,KAAA,OAAO,KAAU,KAAA;AAEvE,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,CAClC,KACsB,KAAA;AACtB,EAAA,OAAO,QAAS,CAAA,KAAK,CAAK,IAAA,CAAC,QAAQ,KAAK,CAAA;AACzC;AAEa,IAAA,aAAA,GAAgB,CAA4B,KAAqC,KAAA;AAC7F,EAAI,IAAA,CAAC,QAAS,CAAA,KAAK,CAAG,EAAA;AACrB,IAAO,OAAA,KAAA;AAAA;AAGR,EAAM,MAAA,SAAA,GAAY,MAAO,CAAA,cAAA,CAAe,KAAK,CAAA;AAG7C,EAAA,OAAA,CACE,SAAa,IAAA,IAAA,IAAQ,SAAc,KAAA,MAAA,CAAO,SAAa,IAAA,MAAA,CAAO,cAAe,CAAA,SAAS,CAAM,KAAA,IAAA,KAC7F,EAAE,MAAA,CAAO,WAAe,IAAA,KAAA,CAAA;AAE1B;AAEa,IAAA,UAAA,GAAa,CAAgC,KAAuC,KAAA;AAChG,EAAA,OAAO,OAAO,KAAU,K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","file":"index.js","sourcesContent":["import type { AnyAsyncFunction, AnyFunction, AnyObject } from \"./type-utils\";\n\nexport const isString = (value: unknown): value is string => typeof value === \"string\";\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 = Record<string, unknown>>(\n\tvalue: unknown\n): value is TObject => {\n\treturn isObject(value) && !isArray(value);\n};\n\nexport const isPlainObject = <TObject extends AnyObject>(value: unknown): value is TObject => {\n\tif (!isObject(value)) {\n\t\treturn false;\n\t}\n\n\tconst prototype = Object.getPrototypeOf(value) as unknown;\n\n\t// == Check if it's a plain object\n\treturn (\n\t\t(prototype == null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) &&\n\t\t!(Symbol.toStringTag in value)\n\t);\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, WriteableLevel } from \"./type-utils\";\n\nexport const defineEnum = <const TValue, TWriteableLevel extends WriteableLevel = \"shallow\">(\n\tvalue: TValue\n) => value as Prettify<Writeable<TValue, TWriteableLevel>>;\n"]}
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@zayne-labs/toolkit-type-helpers",
3
+ "type": "module",
4
+ "version": "0.8.22",
5
+ "description": "A collection of utility functions, types and composables used by my other projects. Nothing too fancy but can be useful.",
6
+ "author": "Ryan Zayne",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/zayne-labs/toolkit#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/zayne-labs/toolkit.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/zayne-labs/toolkit/issues"
15
+ },
16
+ "keywords": [
17
+ "utilities",
18
+ "types",
19
+ "hooks"
20
+ ],
21
+ "sideEffects": false,
22
+ "exports": {
23
+ ".": "./dist/esm/index.js"
24
+ },
25
+ "main": "./dist/esm/index.js",
26
+ "module": "./dist/esm/index.js",
27
+ "types": "./dist/esm/index.d.ts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "engines": {
32
+ "node": ">=18.x"
33
+ },
34
+ "devDependencies": {
35
+ "@arethetypeswrong/cli": "^0.17.2",
36
+ "@changesets/cli": "^2.27.11",
37
+ "@size-limit/esbuild-why": "^11.1.6",
38
+ "@size-limit/preset-small-lib": "^11.1.6",
39
+ "@total-typescript/ts-reset": "^0.6.1",
40
+ "@types/node": "^22.10.3",
41
+ "@zayne-labs/tsconfig": "0.2.1",
42
+ "clsx": "^2.1.1",
43
+ "concurrently": "^9.1.2",
44
+ "cross-env": "^7.0.3",
45
+ "publint": "^0.2.12",
46
+ "size-limit": "^11.1.6",
47
+ "terser": "^5.37.0",
48
+ "tsup": "^8.3.5",
49
+ "typescript": "^5.7.2"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public",
53
+ "registry": "https://registry.npmjs.org/",
54
+ "provenance": true
55
+ },
56
+ "size-limit": [
57
+ {
58
+ "path": "./src/index.ts",
59
+ "limit": "500 b"
60
+ }
61
+ ],
62
+ "scripts": {
63
+ "build": "tsup",
64
+ "build:dev": "cross-env NODE_ENV=development tsup",
65
+ "build:test": "concurrently --prefix-colors \"yellow.bold,#7da4f8.bold,magenta\" --names PUBLINT,TSUP 'pnpm:lint:publint' 'pnpm:build:dev'",
66
+ "lint:attw": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
67
+ "lint:check-types": "tsc --pretty --incremental -p tsconfig.json",
68
+ "lint:publint": "publint --strict .",
69
+ "lint:size": "size-limit",
70
+ "release": "pnpm publish --no-git-checks",
71
+ "test:release": "pnpx pkg-pr-new publish"
72
+ }
73
+ }