@sohanemon/utils 5.1.5 → 5.1.7

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.
@@ -1,5 +1,6 @@
1
1
  export * from './cookie';
2
2
  export * from './object';
3
- export * from './shield';
4
3
  export * from './poll';
4
+ export * from './schedule';
5
+ export * from './shield';
5
6
  export * from './utils';
@@ -1,5 +1,6 @@
1
1
  export * from './cookie';
2
2
  export * from './object';
3
- export * from './shield';
4
3
  export * from './poll';
4
+ export * from './schedule';
5
+ export * from './shield';
5
6
  export * from './utils';
@@ -0,0 +1,11 @@
1
+ type Task = () => Promise<void> | void;
2
+ interface ScheduleOpts {
3
+ retry?: number;
4
+ delay?: number;
5
+ }
6
+ /**
7
+ * Runs a function asynchronously in the background.
8
+ * Returns immediately, retries on failure if configured.
9
+ */
10
+ export declare function schedule(task: Task, options?: ScheduleOpts): void;
11
+ export {};
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Runs a function asynchronously in the background.
3
+ * Returns immediately, retries on failure if configured.
4
+ */
5
+ export function schedule(task, options = {}) {
6
+ const { retry = 0, delay = 0 } = options;
7
+ const attempt = async (triesLeft) => {
8
+ try {
9
+ await task();
10
+ }
11
+ catch (err) {
12
+ console.log('⚡[schedule.ts] err:', err);
13
+ if (triesLeft > 0) {
14
+ console.log(`⚡[schedule.ts] Retrying in ${delay}ms...`);
15
+ setTimeout(() => attempt(triesLeft - 1), delay);
16
+ }
17
+ }
18
+ };
19
+ // Schedule immediately
20
+ setTimeout(() => attempt(retry), 0);
21
+ }
@@ -3,12 +3,131 @@ export type BUFFER<T> = T;
3
3
  export type IMPLIES<T, U> = T extends U ? true : false;
4
4
  export type XOR_Binary<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
5
5
  export type XNOR_Binary<T, U> = (T & U) | (Without<T, U> & Without<U, T>);
6
+ /**
7
+ * Computes a type-level AND (all must true) for a tuple of types.
8
+ *
9
+ * Truth table for 3 arguments:
10
+ *
11
+ * A B C = AND
12
+ * 1 1 1 = 1
13
+ * 1 1 0 = 0
14
+ * 1 0 1 = 0
15
+ * 1 0 0 = 0
16
+ * 0 1 1 = 0
17
+ * 0 1 0 = 0
18
+ * 0 0 1 = 0
19
+ * 0 0 0 = 0
20
+ *
21
+ * @template T - Tuple of boolean-like types (1/0)
22
+ */
6
23
  export type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F & AND<R> : F : unknown;
24
+ /**
25
+ * Computes a type-level OR (At least one) for a tuple of types.
26
+ *
27
+ * Truth table for 3 arguments:
28
+ *
29
+ * A B C = OR
30
+ * 1 1 1 = 1
31
+ * 1 1 0 = 1
32
+ * 1 0 1 = 1
33
+ * 1 0 0 = 1
34
+ * 0 1 1 = 1
35
+ * 0 1 0 = 1
36
+ * 0 0 1 = 1
37
+ * 0 0 0 = 0
38
+ *
39
+ * @template T - Tuple of boolean-like types (1/0)
40
+ */
7
41
  export type OR<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F | OR<R> : F : never;
42
+ /**
43
+ * Computes a type-level XOR (only one/odd) for a tuple of types.
44
+ *
45
+ * Truth table for 3 arguments:
46
+ *
47
+ * A B C = XOR
48
+ * 1 1 1 = 1
49
+ * 1 1 0 = 0
50
+ * 1 0 1 = 0
51
+ * 1 0 0 = 1
52
+ * 0 1 1 = 0
53
+ * 0 1 0 = 1
54
+ * 0 0 1 = 1
55
+ * 0 0 0 = 0
56
+ *
57
+ * @template T - Tuple of boolean-like types (1/0)
58
+ */
8
59
  export type XOR<T extends any[]> = T extends [infer F, ...infer R] ? R extends [infer S, ...infer Rest] ? XOR<[XOR_Binary<F, S>, ...Rest]> : F : never;
60
+ /**
61
+ * Computes a type-level XNOR (All or None true) for a tuple of types.
62
+ *
63
+ * Truth table for 3 arguments:
64
+ *
65
+ * A B C = XNOR
66
+ * 1 1 1 = 0
67
+ * 1 1 0 = 1
68
+ * 1 0 1 = 1
69
+ * 1 0 0 = 0
70
+ * 0 1 1 = 1
71
+ * 0 1 0 = 0
72
+ * 0 0 1 = 0
73
+ * 0 0 0 = 1
74
+ *
75
+ * @template T - Tuple of boolean-like types (1/0)
76
+ */
9
77
  export type XNOR<T extends any[]> = T extends [infer F, ...infer R] ? R extends [infer S, ...infer Rest] ? XNOR<[XNOR_Binary<F, S>, ...Rest]> : F : never;
78
+ /**
79
+ * Computes a type-level NOT for a tuple of types.
80
+ *
81
+ * Truth table for 3 arguments:
82
+ *
83
+ * A B C = NOT
84
+ * 1 1 1 = 0
85
+ * 1 1 0 = 0
86
+ * 1 0 1 = 0
87
+ * 1 0 0 = 0
88
+ * 0 1 1 = 0
89
+ * 0 1 0 = 0
90
+ * 0 0 1 = 0
91
+ * 0 0 0 = 1
92
+ *
93
+ * @template T - Tuple of boolean-like types (1/0)
94
+ */
10
95
  export type NOT<T> = {
11
96
  [P in keyof T]?: never;
12
97
  };
98
+ /**
99
+ * Computes a type-level NAND for a tuple of types.
100
+ *
101
+ * Truth table for 3 arguments:
102
+ *
103
+ * A B C = NAND
104
+ * 1 1 1 = 0
105
+ * 1 1 0 = 1
106
+ * 1 0 1 = 1
107
+ * 1 0 0 = 1
108
+ * 0 1 1 = 1
109
+ * 0 1 0 = 1
110
+ * 0 0 1 = 1
111
+ * 0 0 0 = 1
112
+ *
113
+ * @template T - Tuple of boolean-like types (1/0)
114
+ */
13
115
  export type NAND<T extends any[]> = NOT<AND<T>>;
116
+ /**
117
+ * Computes a type-level NOR for a tuple of types.
118
+ *
119
+ * Truth table for 3 arguments:
120
+ *
121
+ * A B C = NOR
122
+ * 1 1 1 = 0
123
+ * 1 1 0 = 0
124
+ * 1 0 1 = 0
125
+ * 1 0 0 = 0
126
+ * 0 1 1 = 0
127
+ * 0 1 0 = 0
128
+ * 0 0 1 = 0
129
+ * 0 0 0 = 1
130
+ *
131
+ * @template T - Tuple of boolean-like types (1/0)
132
+ */
14
133
  export type NOR<T extends any[]> = NOT<OR<T>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.1.5",
3
+ "version": "5.1.7",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",