@byloth/core 2.0.0-rc.9 → 2.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.
Files changed (47) hide show
  1. package/dist/core.js +4087 -621
  2. package/dist/core.js.map +1 -1
  3. package/dist/core.umd.cjs +2 -2
  4. package/dist/core.umd.cjs.map +1 -1
  5. package/package.json +17 -13
  6. package/src/core/types.ts +41 -0
  7. package/src/helpers.ts +11 -2
  8. package/src/index.ts +12 -9
  9. package/src/models/aggregators/aggregated-async-iterator.ts +920 -21
  10. package/src/models/aggregators/aggregated-iterator.ts +838 -22
  11. package/src/models/aggregators/reduced-iterator.ts +827 -11
  12. package/src/models/aggregators/types.ts +153 -10
  13. package/src/models/callbacks/callable-object.ts +42 -6
  14. package/src/models/callbacks/index.ts +2 -2
  15. package/src/models/callbacks/publisher.ts +160 -4
  16. package/src/models/callbacks/switchable-callback.ts +230 -23
  17. package/src/models/callbacks/types.ts +16 -0
  18. package/src/models/exceptions/core.ts +132 -3
  19. package/src/models/exceptions/index.ts +405 -13
  20. package/src/models/index.ts +4 -8
  21. package/src/models/iterators/smart-async-iterator.ts +827 -22
  22. package/src/models/iterators/smart-iterator.ts +755 -20
  23. package/src/models/iterators/types.ts +268 -9
  24. package/src/models/json/json-storage.ts +508 -110
  25. package/src/models/json/types.ts +10 -1
  26. package/src/models/promises/deferred-promise.ts +85 -5
  27. package/src/models/promises/index.ts +1 -3
  28. package/src/models/promises/smart-promise.ts +272 -4
  29. package/src/models/promises/timed-promise.ts +43 -1
  30. package/src/models/promises/types.ts +84 -2
  31. package/src/models/timers/clock.ts +109 -19
  32. package/src/models/timers/countdown.ts +176 -21
  33. package/src/models/timers/game-loop.ts +266 -0
  34. package/src/models/timers/index.ts +2 -1
  35. package/src/models/types.ts +6 -5
  36. package/src/utils/async.ts +43 -0
  37. package/src/utils/curve.ts +85 -0
  38. package/src/utils/date.ts +204 -10
  39. package/src/utils/dom.ts +16 -2
  40. package/src/utils/index.ts +3 -2
  41. package/src/utils/iterator.ts +200 -17
  42. package/src/utils/math.ts +55 -3
  43. package/src/utils/random.ts +139 -2
  44. package/src/utils/string.ts +11 -0
  45. package/src/models/game-loop.ts +0 -83
  46. package/src/models/promises/long-running-task.ts +0 -294
  47. package/src/models/promises/thenable.ts +0 -97
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byloth/core",
3
- "version": "2.0.0-rc.9",
3
+ "version": "2.0.1",
4
4
  "description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
5
5
  "keywords": [
6
6
  "Core",
@@ -31,27 +31,31 @@
31
31
  "dist",
32
32
  "src"
33
33
  ],
34
- "main": "./dist/core.umd.cjs",
35
- "module": "./dist/core.js",
34
+ "main": "dist/core.umd.cjs",
35
+ "module": "dist/core.js",
36
36
  "exports": {
37
37
  ".": {
38
38
  "import": {
39
- "default": "./dist/core.js",
40
- "types": "./src/index.ts"
39
+ "types": "./src/index.ts",
40
+ "default": "./dist/core.js"
41
41
  },
42
42
  "require": {
43
- "default": "./dist/core.umd.cjs",
44
- "types": "./src/index.ts"
43
+ "types": "./src/index.ts",
44
+ "default": "./dist/core.umd.cjs"
45
45
  }
46
46
  }
47
47
  },
48
- "types": "./src/index.ts",
48
+ "types": "src/index.ts",
49
49
  "devDependencies": {
50
- "@byloth/eslint-config-typescript": "^3.0.3",
51
- "@types/node": "^22.10.1",
50
+ "@byloth/eslint-config-typescript": "^3.1.0",
51
+ "@eslint/compat": "^1.2.7",
52
+ "@types/node": "^22.13.11",
53
+ "eslint": "^9.23.0",
52
54
  "husky": "^9.1.7",
53
- "typescript": "^5.7.2",
54
- "vite": "^5.4.11"
55
+ "jsdom": "^26.0.0",
56
+ "typescript": "^5.8.2",
57
+ "vite": "^6.2.2",
58
+ "vitest": "^3.0.9"
55
59
  },
56
60
  "scripts": {
57
61
  "dev": "vite",
@@ -59,7 +63,7 @@
59
63
  "preview": "vite preview",
60
64
  "typecheck": "tsc",
61
65
  "lint": "eslint .",
62
- "test": "vitest",
66
+ "test": "vitest run",
63
67
  "ci": "pnpm install --frozen-lockfile"
64
68
  }
65
69
  }
package/src/core/types.ts CHANGED
@@ -1,5 +1,46 @@
1
+ /**
2
+ * A utility type that allows to define a class constructor of a specific type.
3
+ * Is the counterpart of the native `InstanceType` utility type.
4
+ *
5
+ * ```ts
6
+ * function factory<T extends object>(Factory: Constructor<T>): T { [...] }
7
+ *
8
+ * const instance: MyObject = factory(MyObject);
9
+ * ```
10
+ */
1
11
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
12
  export type Constructor<T extends object, P extends unknown[] = any[]> = new (...args: P) => T;
3
13
 
14
+ /**
15
+ * A type that represents the return value of `setInterval` function,
16
+ * indipendently from the platform it's currently running on.
17
+ *
18
+ * For instance, in a browser environment, it's a `number` value representing the interval ID.
19
+ * In a Node.js environment, on the other hand, it's an object of type `NodeJS.Timeout`.
20
+ *
21
+ * This allows to seamlessly use the same code in both environments, without having to deal with the differences:
22
+ *
23
+ * ```ts
24
+ * const intervalId: Interval = setInterval(() => { [...] }, 1_000);
25
+ *
26
+ * clearInterval(intervalId);
27
+ * ```
28
+ */
4
29
  export type Interval = ReturnType<typeof setInterval>;
30
+
31
+ /**
32
+ * A type that represents the return value of `setTimeout` function,
33
+ * indipendently from the platform it's currently running on.
34
+ *
35
+ * For instance, in a browser environment, it's a `number` value representing the timeout ID.
36
+ * In a Node.js environment, on the other hand, it's an object of type `NodeJS.Timeout`.
37
+ *
38
+ * This allows to seamlessly use the same code in both environments, without having to deal with the differences:
39
+ *
40
+ * ```ts
41
+ * const timeoutId: Timeout = setTimeout(() => { [...] }, 1_000);
42
+ *
43
+ * clearTimeout(timeoutId);
44
+ * ```
45
+ */
5
46
  export type Timeout = ReturnType<typeof setTimeout>;
package/src/helpers.ts CHANGED
@@ -1,10 +1,19 @@
1
1
  /* eslint-disable @typescript-eslint/ban-ts-comment */
2
2
 
3
+ /**
4
+ * An utility constant that indicates whether the current environment is a browser.
5
+ */
3
6
  // @ts-ignore
4
7
  export const isBrowser = ((typeof window !== "undefined") && (typeof window.document !== "undefined"));
5
8
 
9
+ /**
10
+ * An utility constant that indicates whether the current environment is a Node.js runtime.
11
+ */
6
12
  // @ts-ignore
7
- export const isNode = ((typeof process !== "undefined") && (process.versions?.node));
13
+ export const isNode = ((typeof process !== "undefined") && !!(process.versions?.node));
8
14
 
15
+ /**
16
+ * An utility constant that indicates whether the current environment is a Web Worker.
17
+ */
9
18
  // @ts-ignore
10
- export const isWebWorker = ((typeof self === "object") && (self.constructor?.name === "DedicatedWorkerGlobalScope"));
19
+ export const isWorker = ((typeof self === "object") && (self.constructor?.name === "DedicatedWorkerGlobalScope"));
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
- export const VERSION = "2.0.0-rc.9";
1
+ export const VERSION = "2.0.1";
2
2
 
3
3
  export type { Constructor, Interval, Timeout } from "./core/types.js";
4
4
 
5
- export { isBrowser, isNode, isWebWorker } from "./helpers.js";
5
+ export { isBrowser, isNode, isWorker } from "./helpers.js";
6
6
 
7
7
  export {
8
8
  AggregatedIterator,
@@ -11,6 +11,7 @@ export {
11
11
  Clock,
12
12
  Countdown,
13
13
  DeferredPromise,
14
+ EnvironmentException,
14
15
  Exception,
15
16
  FatalErrorException,
16
17
  FileException,
@@ -19,7 +20,6 @@ export {
19
20
  GameLoop,
20
21
  JSONStorage,
21
22
  KeyException,
22
- LongRunningTask,
23
23
  NotImplementedException,
24
24
  NetworkException,
25
25
  PermissionException,
@@ -32,7 +32,6 @@ export {
32
32
  SmartAsyncIterator,
33
33
  SmartPromise,
34
34
  SwitchableCallback,
35
- Thenable,
36
35
  TimeoutException,
37
36
  TimedPromise,
38
37
  TypeException,
@@ -42,7 +41,11 @@ export {
42
41
 
43
42
  export type {
44
43
  AsyncGeneratorFunction,
44
+ AsyncIteratee,
45
45
  AsyncIteratorLike,
46
+ AsyncKeyedIteratee,
47
+ AsyncKeyedReducer,
48
+ AsyncReducer,
46
49
  Callback,
47
50
  FulfilledHandler,
48
51
  GeneratorFunction,
@@ -53,23 +56,20 @@ export type {
53
56
  JSONValue,
54
57
  KeyedIteratee,
55
58
  KeyedReducer,
56
- KeyedTypeGuardIteratee,
57
- LongRunningTaskOptions,
59
+ KeyedTypeGuardPredicate,
58
60
  MaybeAsyncKeyedIteratee,
59
61
  MaybeAsyncKeyedReducer,
60
- MaybeAsyncKeyedTypeGuardIteratee,
61
62
  MaybeAsyncGeneratorFunction,
62
63
  MaybeAsyncIteratee,
63
64
  MaybeAsyncIteratorLike,
64
65
  MaybeAsyncReducer,
65
- MaybeAsyncTypeGuardIteratee,
66
66
  MaybePromise,
67
67
  PromiseExecutor,
68
68
  PromiseRejecter,
69
69
  PromiseResolver,
70
70
  Reducer,
71
71
  RejectedHandler,
72
- TypeGuardIteratee
72
+ TypeGuardPredicate
73
73
 
74
74
  } from "./models/types.js";
75
75
 
@@ -78,12 +78,14 @@ export {
78
78
  capitalize,
79
79
  chain,
80
80
  count,
81
+ Curve,
81
82
  delay,
82
83
  dateDifference,
83
84
  dateRange,
84
85
  dateRound,
85
86
  TimeUnit,
86
87
  enumerate,
88
+ getWeek,
87
89
  hash,
88
90
  loadScript,
89
91
  nextAnimationFrame,
@@ -92,6 +94,7 @@ export {
92
94
  shuffle,
93
95
  sum,
94
96
  unique,
97
+ WeekDay,
95
98
  yieldToEventLoop,
96
99
  zip
97
100