@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 +11 -0
- package/src/models/beautify.type.ts +3 -0
- package/src/models/constructors/abstact-constructor.type.ts +5 -0
- package/src/models/constructors/constructor.type.ts +8 -0
- package/src/models/contains-char.ts +9 -0
- package/src/models/decorators/accessor-decorator.type.ts +17 -0
- package/src/models/decorators/class-decorator.type.ts +6 -0
- package/src/models/decorators/field-decorator.type.ts +5 -0
- package/src/models/decorators/method-decorator.type.ts +6 -0
- package/src/models/dictionary.type.ts +3 -0
- package/src/models/function.type.ts +86 -0
- package/src/models/index.ts +15 -0
- package/src/models/mutable.type.ts +3 -0
- package/src/models/positive-integer.type.ts +8 -0
- package/src/models/require-one.type.ts +6 -0
- package/src/models/stack/stack.model.ts +56 -0
- package/src/models/tuple-of-length.type.ts +11 -0
- package/src/public-api.ts +1 -0
- package/tsconfig.json +3 -0
- package/vite.config.ts +4 -0
package/package.json
ADDED
|
@@ -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,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,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,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
package/vite.config.ts
ADDED