@arabig/origin 1.0.0

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,139 @@
1
+ /**
2
+ * @license
3
+ * Copyright Salar Arab All Rights Reserved.
4
+ *
5
+ * @author Salar Arab <salar_arab@outlook.com>
6
+ */
7
+ /**
8
+ * Represents a union of the given type, `undefined` and `null`.
9
+ * @typeParam `T` The type that the union is based on
10
+ */
11
+ type AoNullish<T> = T | null | undefined;
12
+ /**
13
+ * Represents a discriminated union of the passed type parameter's possible enteries
14
+ * based on its keys.
15
+ * @typeParam `T` The type that the union is based on
16
+ */
17
+ type AoEntryUnion<T> = {
18
+ [K in keyof T]: {
19
+ key: K;
20
+ value: T[K];
21
+ };
22
+ }[keyof T];
23
+ /**
24
+ * Represents a key-value pair in form of a two elements tuple that its first element
25
+ * is the key and the second one is the value.
26
+ */
27
+ type AoEntry<T> = [keyof T, T[keyof T]];
28
+ /**
29
+ * Represents an `object` with loose or typed structure base on given type parameter.
30
+ * @typeParam `T` The type that determines the structure of the resulting `object`.
31
+ * If omitted, a complete loose structure will be considered
32
+ */
33
+ type AoObject<T extends Record<any, any> = any> = T extends any[] ? never : T extends (...p: any[]) => any ? never : unknown extends T ? Record<any, any> : {
34
+ [K in keyof T]: T[K];
35
+ };
36
+ /**
37
+ * Represents an `object` that includes a loose minimal structure base on passed type
38
+ * parameter.
39
+ * @typeParam `T` The `object` based that forms the minimal structure, if omitted
40
+ * a complete loose structure will be considered
41
+ */
42
+ type AoLooseObject<T extends AoObject = AoObject> = AoObject & T;
43
+ /**
44
+ * Represents a `string` literal union of the given `enum` values.
45
+ * @typeParam `T`The `enum` type
46
+ * @typeParam `TDefault` The type that determines the default case of the `enum` values union
47
+ */
48
+ type AoEnumValue<T extends string, TDefault extends string = never> = `${T}` | TDefault;
49
+ /**
50
+ * Represents an optional `string` literal union of the given `enum` values.
51
+ * @typeParam `T`The `enum` type
52
+ */
53
+ type AoEnumOptionalValue<T extends string> = `${T}` | String;
54
+ /**
55
+ * Represents either a typed union of specific keys or a plain primitive key
56
+ * base on given type parameter.
57
+ * @typeParam `T` The type that determines the keys union or a primitive key if omitted
58
+ */
59
+ type AoKey<T = unknown> = unknown extends T ? string | number | symbol : keyof T;
60
+ /**
61
+ * Represents either a typed union of specific keys or a `string` key
62
+ * base on given type parameter.
63
+ * @typeParam `T` The type that determines the keys union or a string key if omitted
64
+ */
65
+ type AoStringKey<T = unknown> = unknown extends T ? string : Exclude<keyof T, number | symbol>;
66
+ /**
67
+ * Represents either a typed union of specific keys or a plain primitive key
68
+ * base on given type parameter for all nested types.
69
+ * @typeParam `T` The type that determines the keys union or a primitive key if omitted
70
+ * @remarks Using this type for deeply nested types can prove slow compilation time.
71
+ */
72
+ type AoDeepKey<T = unknown> = unknown extends T ? string | number | symbol : {
73
+ [K in keyof T]: T[K] extends object ? K | AoDeepKey<T[K]> : K;
74
+ }[keyof T];
75
+ /**
76
+ * Represents either a typed union of specific keys or a `string` key
77
+ * base on given type parameter for all nested types.
78
+ * @typeParam `T` The type that determines the keys union or a `string` key if omitted
79
+ * @remarks Using this type for deeply nested types can prove slow compilation time.
80
+ */
81
+ type AoDeepStringKey<T = unknown> = unknown extends T ? string : Exclude<{
82
+ [K in keyof T]: T[K] extends object ? K | AoDeepKey<T[K]> : K;
83
+ }[keyof T], number | symbol>;
84
+
85
+ /**
86
+ * @license
87
+ * Copyright Salar Arab All Rights Reserved.
88
+ *
89
+ * @author Salar Arab <salar_arab@outlook.com>
90
+ */
91
+ /**
92
+ * Represents a union of JavaScript primitive data types.
93
+ * @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Primitive | Primitive}
94
+ */
95
+ type AoPrimitive = string | number | boolean | bigint | undefined | null | symbol;
96
+ /**
97
+ * Represents a union of JavaScript falsy values.
98
+ * @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy | Falsy}
99
+ */
100
+ type AoFalsy = undefined | null | false | 0 | -0 | 0n | "";
101
+ /**
102
+ * Represents an object that is a CSS declaration block, and exposes style information
103
+ * and various style-related methods and properties.
104
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration | CSSStyleDeclaration}
105
+ */
106
+ type AoStyle = Partial<CSSStyleDeclaration>;
107
+ /**
108
+ * Represents a numeric `string` type
109
+ */
110
+ type AoDigit = `${number}`;
111
+ /**
112
+ * Represents a union of the `typeof` keyword result values
113
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof | typeof}
114
+ */
115
+ type AoTypeOf = 'string' | 'number' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function' | 'bigint';
116
+
117
+ /**
118
+ * @license
119
+ * Copyright Agin Company All Rights Reserved
120
+ *
121
+ * @author Salar Arab <salar.arab.70@gmail.com>
122
+ */
123
+ /**
124
+ * Checks whether the passed type is only assignable to a minimal unbound JS `object`.
125
+ * @typeParam `T` The type to be checked
126
+ */
127
+ type AoIsObject<T> = T extends any[] | Function | ((...p: any[]) => any) ? never : T extends Record<any, any> ? T : never;
128
+ /**
129
+ * Checks whether the passed type is only assignable to a minimal unbound JS `array`.
130
+ * @typeParam `T` The type to be checked
131
+ */
132
+ type AoIsArray<T> = T extends any[] ? T : never;
133
+ /**
134
+ * Checks whether the passed type is only assignable to a minimal callable JS `function`.
135
+ * @typeParam `T` The type to be checked
136
+ */
137
+ type AoIsFunction<T> = T extends (...p: any[]) => any ? T : never;
138
+
139
+ export type { AoDeepKey, AoDeepStringKey, AoDigit, AoEntry, AoEntryUnion, AoEnumOptionalValue, AoEnumValue, AoFalsy, AoIsArray, AoIsFunction, AoIsObject, AoKey, AoLooseObject, AoNullish, AoObject, AoPrimitive, AoStringKey, AoStyle, AoTypeOf };
@@ -0,0 +1,145 @@
1
+ /**
2
+ * @license
3
+ * Copyright Salar Arab All Rights Reserved.
4
+ *
5
+ * @author Salar Arab <salar_arab@outlook.com>
6
+ */
7
+ /**
8
+ * Represents a uniquely created token-like entity which can be associated with a
9
+ * corresponding value.
10
+ *
11
+ * @remarks
12
+ * Each `AoToken` instance encapsulates a unique `Symbol` identifier, ensuring
13
+ * collision-free keying even across modules. The generic type parameter `T` allows
14
+ * consumers to associate a specific value type with the token, enabling type-safe
15
+ * retrieval from collections.
16
+ *
17
+ * This pattern is especially useful in scenarios like dependency injection,
18
+ * metadata registries, or strongly typed key-value stores.
19
+ *
20
+ * @typeParam `T` The type of value associated with this token.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const BASE_URL_TK = new AoToken<string>("website base URL");
25
+ *
26
+ * const collection = new Map();
27
+ *
28
+ * function getValue<T>(token: AoToken<T>): T | null {
29
+ return collection.get(token) ?? null;
30
+ }
31
+ *
32
+ * const value = getValue(BASE_URL_TK); //inferred as string | null
33
+ * ```
34
+ */
35
+ declare class AoToken<T> {
36
+ /**
37
+ * @remarks Do not use this property in the inheritance, since it reserved for
38
+ * semantic usage of `T` type parameter.
39
+ */
40
+ protected $implicitType?: T;
41
+ private readonly _identifier;
42
+ private readonly _description;
43
+ /**
44
+ * Creates a new instance with optional given description.
45
+ * @param description An optional string description
46
+ */
47
+ constructor(description?: string | null);
48
+ /**
49
+ * @readonly The `symbol` base identifier which is unique for each intance
50
+ * @remarks It is recommended to use this property for identification or comparsion
51
+ * purposes.
52
+ */
53
+ get identifier(): symbol;
54
+ /**
55
+ * @readonly The description about this token instance.
56
+ * @remarks It is not recommended to use this property for identification or
57
+ * comparsion purposes, since it's just a simple description about the token instance.
58
+ * Use the `identifier` property for this matter.
59
+ */
60
+ get description(): string;
61
+ /**
62
+ * Compares the given `AoToken` with the current instance.
63
+ * @param comparable The comparing `AoToken` instance
64
+ * @returns The comparsion result as `boolean`
65
+ */
66
+ equals(comparable: AoToken<unknown>): boolean;
67
+ }
68
+
69
+ /**
70
+ * @license
71
+ * Copyright Salar Arab All Rights Reserved.
72
+ *
73
+ * @author Salar Arab <salar_arab@outlook.com>
74
+ */
75
+
76
+ /**
77
+ * Represents a type-safe collection that stores and retrieves values using unique,
78
+ * typed tokens.
79
+ *
80
+ * This class utilizes an abstract data structure similar to `Map`, but replaces generic keys
81
+ * with {@link AoToken} instances to ensure both **uniqueness** and **type safety**. Each token
82
+ * carries a generic type parameter that enforces the expected value type at compile time,
83
+ * preventing mismatches and enhancing developer experience.
84
+ *
85
+ * Ideal for use cases such as dependency registries, metadata stores, or scoped configuration
86
+ * systems where strong typing and collision-free keying are critical.
87
+ *
88
+ * @see {@link https://en.wikipedia.org/wiki/Abstract_data_type | Abstract data type}
89
+ *
90
+ * @typeParam `TValue` Determines the storeable value type range
91
+ *
92
+ * @remarks
93
+ * - Tokens are guaranteed to be unique via their internal `Symbol` identifier.
94
+ * - Values are type-checked against the token’s generic parameter.
95
+ * - This structure improves safety and clarity in large-scale, modular applications.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const data = { meta: "data" };
100
+ *
101
+ * const DATA_TOKEN = new AoToken<{ meta: "data" }>();
102
+ *
103
+ * const collection = new AoVault().store(DATA_TOKEN, data);
104
+ *
105
+ * const value = collection.retrieve(DATA_TOKEN); //inferred as { meta: "data" } | null;
106
+ * ```
107
+ */
108
+ declare class AoVault<TValue = any> {
109
+ private readonly _vault;
110
+ /**
111
+ * Creates a new instance with optional initial entries.
112
+ * @param entries The optional initial entries as token-value `tuples`
113
+ */
114
+ constructor(entries?: [AoToken<TValue>, TValue][]);
115
+ /**
116
+ * Checks if an associated entry exists for the given token.
117
+ * @param token An instance of {@link AoToken} as querying token
118
+ * @returns The result of the query as `boolean`
119
+ */
120
+ exists(token: AoToken<TValue>): boolean;
121
+ /**
122
+ * Stores the given entry base on {@link AoToken}.
123
+ * @typeParam `T` The type of storing value that must be assignable to {@link AoVault} `TValue` generic type
124
+ * @param token An instance of {@link AoToken} as querying token
125
+ * @param value The storing value
126
+ * @returns The current instance of {@link AoVault}
127
+ */
128
+ store<T extends TValue>(token: AoToken<T>, value: T): AoVault<TValue>;
129
+ /**
130
+ * Retrieves the associated value base on given {@link AoToken}.
131
+ * @typeParam `T` The type of retrieving value that must be assignable to {@link AoVault} `TValue` generic type
132
+ * @param token An instance of {@link AoToken} as querying token
133
+ * @returns The associated value
134
+ */
135
+ retrieve<T extends TValue>(token: AoToken<T>): T | null;
136
+ /**
137
+ * Deletes the associated entry base on given {@link AoToken}.
138
+ * @typeParam `T` The type of deleting value that must be assignable to {@link AoVault} `TValue` generic type
139
+ * @param token An instance of {@link AoToken} as querying token
140
+ * @returns The associated value before deleting the entry
141
+ */
142
+ delete<T extends TValue>(token: AoToken<T>): T | null;
143
+ }
144
+
145
+ export { AoToken, AoVault };