@xaendar/common 0.0.1

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.
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@xaendar/common",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "author": "Kaitenjo",
6
+ "license": "MIT",
7
+ "description": "A library containing common utils and types",
8
+ "peerDependencies": {
9
+ "vite": "^8.0.4"
10
+ }
11
+ }
@@ -0,0 +1,3 @@
1
+ export type Beautify<T extends Object> = {
2
+ [K in keyof T]: T[K]
3
+ } & {}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A type representing an abstract constructor function for a given type T.
3
+ * This type can be used to define abstract classes.
4
+ */
5
+ export type AbstractConstructor<T extends Object = Object> = abstract new (...args: any[]) => T;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A type representing a constructor function for a given type T.
3
+ * This type can be used to define classes or factory functions
4
+ */
5
+ export type Constructor<
6
+ T extends Object = Object,
7
+ Statics extends Record<string, unknown> = {}
8
+ > = (new (...args: any[]) => T) & Statics;
@@ -0,0 +1,9 @@
1
+ export type ContainsChar<
2
+ String extends string,
3
+ Contains extends string
4
+ > =
5
+ String extends `${infer First}${infer Rest}`
6
+ ? First extends Contains
7
+ ? true
8
+ : ContainsChar<Rest, Contains>
9
+ : false;
@@ -0,0 +1,17 @@
1
+ export type AccessorDecorator<
2
+ Class extends Object,
3
+ Field = unknown,
4
+ ReturnTypeField = Field
5
+ > = (
6
+ value: ClassAccessorDecoratorValue<Field>,
7
+ context: ClassAccessorDecoratorContext<Class, ReturnTypeField>
8
+ ) => {
9
+ get?: () => ReturnTypeField;
10
+ set?: (value: ReturnTypeField) => void;
11
+ init?: (initialValue: Field) => ReturnTypeField;
12
+ } | void;
13
+
14
+ export type ClassAccessorDecoratorValue<Field = unknown> = {
15
+ get?: () => Field;
16
+ set: (value: Field) => void;
17
+ };
@@ -0,0 +1,6 @@
1
+ import { Constructor } from '../constructors/constructor.type';
2
+
3
+ export type ClassDecorator<
4
+ T extends Object,
5
+ Statics extends { [key: string]: any } = { [key: string]: any }
6
+ > = (klass: Constructor<T, Statics>, context: ClassDecoratorContext) => void
@@ -0,0 +1,5 @@
1
+ export type FieldDecorator<
2
+ Class extends Object,
3
+ Field,
4
+ ReturnType = Field
5
+ > = (field: undefined, context: ClassFieldDecoratorContext<Class, Field>) => ((value: Field) => ReturnType);
@@ -0,0 +1,6 @@
1
+ import { Function } from '../function.type';
2
+
3
+ export type MethodDecorator<
4
+ T extends Object,
5
+ Method extends Function
6
+ > = (value: Method, context: ClassMethodDecoratorContext<T, Method>) => Method | void;
@@ -0,0 +1,3 @@
1
+ export type Dictionary<Key extends string | number, Value> = {
2
+ [K in Key]?: Value
3
+ }
@@ -0,0 +1,86 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /**
3
+ * A function type that can accept any number of arguments and return any type.
4
+ */
5
+ export type Function<
6
+ Arguments extends any[] = any[],
7
+ ReturnType = void
8
+ > = Arguments extends Array<any>
9
+ ? (...args: Arguments) => ReturnType
10
+ : () => ReturnType;
11
+
12
+ /**
13
+ * A function type that can accept any number of arguments and return a promise of any type.
14
+ */
15
+ export type AsyncFunction<
16
+ Arguments extends any[] = any[],
17
+ ReturnType = void
18
+ > = Arguments extends Array<any>
19
+ ? (...args: Arguments) => Promise<ReturnType>
20
+ : () => Promise<ReturnType>;
21
+
22
+ /**
23
+ * A function type that accepts no arguments and returns a value of the specified type.
24
+ * Also known as a 'thunk' in functional programming.
25
+ * @template ReturnType - The return type of the function (defaults to void)
26
+ * @example
27
+ * type GetUser = FunctionNoArgs<User>;
28
+ * const getUser: GetUser = () => ({ id: 1, name: 'John' });
29
+ */
30
+ export type NoArgsFunction <ReturnType = void> = () => ReturnType;
31
+
32
+ /**
33
+ * An async function type that accepts no arguments and returns a promise of the specified type.
34
+ * @template ReturnType - The type that the promise resolves to (defaults to void)
35
+ * @example
36
+ * type FetchUser = AsyncFunctionNoArgs<User>;
37
+ * const fetchUser: FetchUser = async () => await api.getUser();
38
+ */
39
+ export type NoArgsAsyncFunction <ReturnType = void> = () => Promise<ReturnType>;
40
+
41
+ /**
42
+ * A function type that accepts arguments but always returns void.
43
+ */
44
+ export type NoArgsVoidFunction = () => void;
45
+
46
+ /**
47
+ * An async function type that accepts arguments but always resolves to void.
48
+ */
49
+ export type NoArgsAsyncVoidFunction = () => Promise<void>;
50
+
51
+ /**
52
+ * A function type that accepts arguments but always returns void.
53
+ * Typically used for callbacks, event handlers, or side-effect functions.
54
+ * @template Arguments - The types of the function arguments
55
+ * @example
56
+ * type OnUserChange = VoidFunction<[user: User]>;
57
+ * const handleUserChange: OnUserChange = (user) => console.log(user);
58
+ */
59
+ export type VoidFunction<Arguments extends any[] = any[]> = (...args: Arguments) => void;
60
+
61
+ /**
62
+ * An async function type that accepts arguments but always resolves to void.
63
+ * Typically used for async callbacks or side-effect functions.
64
+ * @template Arguments - The types of the function arguments
65
+ * @example
66
+ * type OnUserChangeAsync = AsyncVoidFunction<[user: User]>;
67
+ * const handleUserChangeAsync: OnUserChangeAsync = async (user) => await api.logChange(user);
68
+ */
69
+ export type AsyncVoidFunction<Arguments extends any[] = any[]> = (...args: Arguments) => Promise<void>;
70
+
71
+ /**
72
+ * A constructor function type that can accept any number of arguments and return an instance of any type.
73
+ */
74
+ export type ConstructorFunction<
75
+ T extends object = object,
76
+ Statics extends Record<string, unknown> | undefined = undefined
77
+ > = (new (...args: any[]) => T) & (Statics extends undefined ? object : Statics);
78
+
79
+ /**
80
+ * An abstract constructor function type that can accept any number of arguments
81
+ * and return an instance of the specified type.
82
+ */
83
+ export type AbstractConstructorFunction<
84
+ T extends object = object,
85
+ Statics extends Record<string, unknown> | undefined = undefined
86
+ > = (abstract new (...args: any[]) => T) & (Statics extends undefined ? object : Statics);
@@ -0,0 +1,15 @@
1
+ export * from './beautify.type';
2
+ export * from './constructors/abstact-constructor.type';
3
+ export * from './constructors/constructor.type';
4
+ export * from './contains-char';
5
+ export * from './decorators/accessor-decorator.type';
6
+ export * from './decorators/class-decorator.type';
7
+ export * from './decorators/field-decorator.type';
8
+ export * from './decorators/method-decorator.type';
9
+ export * from './dictionary.type';
10
+ export * from './function.type';
11
+ export * from './mutable.type';
12
+ export * from './positive-integer.type';
13
+ export * from './require-one.type';
14
+ export * from './stack/stack.model';
15
+ export * from './tuple-of-length.type';
@@ -0,0 +1,3 @@
1
+ export type Mutable<T extends Object> = {
2
+ -readonly [P in keyof T]: T[P];
3
+ };
@@ -0,0 +1,8 @@
1
+ import { ContainsChar } from './contains-char';
2
+
3
+ export type PositiveInteger<Value extends number> =
4
+ ContainsChar<`${Value}`, '-'> extends true
5
+ ? never
6
+ : ContainsChar<`${Value}`, '.'> extends true
7
+ ? never
8
+ : Value;
@@ -0,0 +1,6 @@
1
+ export type RequireOne<T extends Object, Keys extends keyof T = keyof T> =
2
+ Pick<T, Exclude<keyof T, Keys>> &
3
+ {
4
+ [K in Keys]-?: Required<Pick<T, K>> &
5
+ Partial<Pick<T, Exclude<Keys, K>>>
6
+ }[Keys];
@@ -0,0 +1,56 @@
1
+ /**
2
+ * A generic LIFO (Last In, First Out) stack data structure.
3
+ *
4
+ * @template T - The type of elements held in the stack. Defaults to `unknown`.
5
+ */
6
+ export class Stack<T = unknown> {
7
+ /**
8
+ * Internal array holding the stack elements, ordered from bottom to top.
9
+ */
10
+ private _elements = new Array<T>;
11
+
12
+ [index: number]: T;
13
+
14
+ constructor() {
15
+ return new Proxy(this, {
16
+ get(target, prop) {
17
+ if (typeof prop === 'string') {
18
+ return isNaN(Number(prop)) ? (target as unknown as { [prop]: unknown })[prop] : target._elements[Number(prop)];
19
+ }
20
+ }
21
+ });
22
+ }
23
+
24
+ /**
25
+ * The number of elements currently in the stack.
26
+ */
27
+ public get length(): number {
28
+ return this._elements.length;
29
+ }
30
+
31
+ /**
32
+ * A shallow copy of the elements in the stack, ordered from bottom to top.
33
+ */
34
+ public get values(): T[] {
35
+ return [...this._elements];
36
+ }
37
+
38
+ /**
39
+ * Removes and returns the top element of the stack.
40
+ *
41
+ * @returns The top element, or `undefined` if the stack is empty.
42
+ */
43
+ public pop(): T | undefined {
44
+ return this._elements.pop();
45
+ }
46
+
47
+ /**
48
+ * Pushes an element onto the top of the stack.
49
+ *
50
+ * @param element - The element to push.
51
+ * @returns The new length of the stack.
52
+ */
53
+ public push(element: T): number {
54
+ return this._elements.push(element);
55
+ }
56
+ }
@@ -0,0 +1,11 @@
1
+ import { PositiveInteger } from './positive-integer.type';
2
+
3
+ export type TupleOfLength<
4
+ Length extends number,
5
+ TupleType = number,
6
+ Acc extends TupleType[] = []
7
+ > = PositiveInteger<Length> extends never
8
+ ? never
9
+ : Acc['length'] extends Length
10
+ ? Acc
11
+ : TupleOfLength<Length, TupleType, [...Acc, TupleType]>
@@ -0,0 +1 @@
1
+ export * from './models';
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { defineConfig } from 'vite';
2
+ import getViteConfig from '../../vite-config';
3
+
4
+ export default defineConfig(getViteConfig('@xaendar/common', __dirname));