@zelgadis87/utils-core 5.0.1 → 5.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@zelgadis87/utils-core",
4
- "version": "5.0.1",
4
+ "version": "5.0.3",
5
5
  "author": "Zelgadis87",
6
6
  "license": "ISC",
7
7
  "private": false,
@@ -22,15 +22,15 @@
22
22
  "@stylistic/eslint-plugin-ts": "4.2.0",
23
23
  "@typescript-eslint/eslint-plugin": "8.26.1",
24
24
  "@typescript-eslint/parser": "8.26.1",
25
- "@vitest/coverage-v8": "3.0.8",
26
- "@vitest/ui": "3.0.8",
27
- "esbuild": "0.19.2",
25
+ "@vitest/coverage-v8": "3.2.4",
26
+ "@vitest/ui": "3.2.4",
27
+ "esbuild": "0.25.8",
28
28
  "eslint": "9.22.0",
29
- "nx": "21.1.2",
29
+ "nx": "21.3.5",
30
30
  "rimraf": "6.0.1",
31
- "typescript": "5.8.2",
31
+ "typescript": "5.8.3",
32
32
  "typescript-eslint": "8.26.1",
33
- "vitest": "3.0.8"
33
+ "vitest": "3.2.4"
34
34
  },
35
35
  "dependencies": {
36
36
  "small-date": "^2.0.1"
@@ -1,6 +1,6 @@
1
1
  import { randomNumberInInterval } from "../utils/random.js";
2
2
  import TimeDuration from "./TimeDuration.js";
3
- import TimeUnit from "./TimeUnit.js";
3
+ import { TimeUnit } from "./TimeUnit.js";
4
4
 
5
5
  function randomize( unit: TimeUnit ) {
6
6
  return ( a: number, b: number ) => {
@@ -1,4 +1,12 @@
1
- import TimeUnit from "./TimeUnit";
1
+ import { TimeUnit } from "./TimeUnit";
2
+
3
+ export type TTimeInUnits = {
4
+ milliseconds: number;
5
+ seconds: number;
6
+ minutes: number;
7
+ hours: number;
8
+ days: number;
9
+ }
2
10
 
3
11
  export default abstract class TimeBase<T> {
4
12
 
@@ -13,22 +21,37 @@ export default abstract class TimeBase<T> {
13
21
 
14
22
  protected abstract create( value: number, unit: TimeUnit ): T;
15
23
 
24
+ /**
25
+ * Total number of milliseconds, rounded down.
26
+ */
16
27
  public get ms(): number {
17
28
  return Math.floor( this._ms / TimeUnit.MILLISECONDS.multiplier );
18
29
  }
19
30
 
31
+ /**
32
+ * Total number of seconds, rounded down.
33
+ */
20
34
  public get seconds(): number {
21
35
  return Math.floor( this._ms / TimeUnit.SECONDS.multiplier );
22
36
  }
23
37
 
38
+ /**
39
+ * Total number of minutes, rounded down.
40
+ */
24
41
  public get minutes(): number {
25
42
  return Math.floor( this._ms / TimeUnit.MINUTES.multiplier );
26
43
  }
27
44
 
45
+ /**
46
+ * Total number of hours, rounded down.
47
+ */
28
48
  public get hours(): number {
29
49
  return Math.floor( this._ms / TimeUnit.HOURS.multiplier );
30
50
  }
31
51
 
52
+ /**
53
+ * Total number of days, rounded down.
54
+ */
32
55
  public get days(): number {
33
56
  return Math.floor( this._ms / TimeUnit.DAYS.multiplier );
34
57
  }
@@ -85,20 +108,21 @@ export default abstract class TimeBase<T> {
85
108
  return this._ms / unit.multiplier;
86
109
  }
87
110
 
88
- protected toUnits(): { days: number, hours: number, minutes: number, seconds: number } {
111
+ protected toUnits(): TTimeInUnits {
89
112
  return {
90
- days: Math.floor( ( this._ms / TimeUnit.DAYS.multiplier ) ),
91
- hours: Math.floor( ( this._ms / TimeUnit.HOURS.multiplier ) % 24 ),
92
- minutes: Math.floor( ( this._ms / TimeUnit.MINUTES.multiplier ) % 60 ),
93
- seconds: Math.floor( ( this._ms / TimeUnit.SECONDS.multiplier ) % 60 ),
113
+ milliseconds: this.ms % TimeUnit.SECONDS.multiplier,
114
+ seconds: Math.floor( this.seconds % 60 ),
115
+ minutes: Math.floor( this.minutes % 60 ),
116
+ hours: Math.floor( this.hours % 24 ),
117
+ days: Math.floor( this.days ),
94
118
  };
95
119
  }
96
120
 
97
- protected static toMs( units: { days?: number, hours?: number, minutes?: number, seconds?: number, ms?: number } ): number {
121
+ protected static unitsToMs( units: Partial<TTimeInUnits> ): number {
98
122
  if ( !units )
99
123
  throw new Error( 'Invalid units given' );
100
124
  let ms = 0
101
- ms += units.ms ?? 0 * TimeUnit.MILLISECONDS.multiplier;
125
+ ms += ( units.milliseconds ?? 0 ) * TimeUnit.MILLISECONDS.multiplier;
102
126
  ms += ( units.seconds ?? 0 ) * TimeUnit.SECONDS.multiplier;
103
127
  ms += ( units.minutes ?? 0 ) * TimeUnit.MINUTES.multiplier;
104
128
  ms += ( units.hours ?? 0 ) * TimeUnit.HOURS.multiplier;
@@ -1,9 +1,9 @@
1
1
  import Deferred, { DeferredCanceledError, ICancelable, ICancelablePromise } from "../async/Deferred.js";
2
2
  import { TComparisonResult } from "../sorting/_index.js";
3
3
  import { TIntervalHandle, TMaybe, TTimeoutHandle, ensureNonNegativeNumber, ensurePositiveNumber, pad } from "../utils/_index.js";
4
- import TimeBase from "./TimeBase";
4
+ import TimeBase, { type TTimeInUnits } from "./TimeBase";
5
5
  import TimeInstant from "./TimeInstant";
6
- import TimeUnit from "./TimeUnit";
6
+ import { TimeUnit } from "./TimeUnit";
7
7
 
8
8
  export class TimeDuration extends TimeBase<TimeDuration> {
9
9
 
@@ -183,6 +183,10 @@ export class TimeDuration extends TimeBase<TimeDuration> {
183
183
  return this.ms > 0;
184
184
  }
185
185
 
186
+ public override toUnits() {
187
+ return super.toUnits();
188
+ }
189
+
186
190
  /**
187
191
  * This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
188
192
  */
@@ -198,7 +202,7 @@ export class TimeDuration extends TimeBase<TimeDuration> {
198
202
  const match = humanTime.trim().match( /^(?:([0-9]+)d|)\s*(?:([0-9]+)h|)\s*(?:([0-9]+)m|)\s*(?:([0-9]+)s|)$/ );
199
203
  if ( match ) {
200
204
  const [ _, days, hours, minutes, seconds ] = match;
201
- return new TimeDuration( TimeBase.toMs( { days: Number( days ?? 0 ), hours: Number( hours ?? 0 ), minutes: Number( minutes ?? 0 ), seconds: Number( seconds ?? 0 ) } ), TimeUnit.MILLISECONDS );
205
+ return new TimeDuration( TimeBase.unitsToMs( { days: Number( days ?? 0 ), hours: Number( hours ?? 0 ), minutes: Number( minutes ?? 0 ), seconds: Number( seconds ?? 0 ) } ), TimeUnit.MILLISECONDS );
202
206
  }
203
207
  throw new Error( 'Failed to parse time: ' + humanTime );
204
208
  }
@@ -263,6 +267,10 @@ export class TimeDuration extends TimeBase<TimeDuration> {
263
267
  return TimeDuration.ms( ms );
264
268
  }
265
269
 
270
+ public static fromUnits( parameters: Partial<TTimeInUnits> ): TimeDuration {
271
+ return TimeDuration.ms( TimeBase.unitsToMs( parameters ) );
272
+ }
273
+
266
274
  }
267
275
 
268
276
  export type TPredefinedTimeDuration = 0 | 50 | 100 | 150 | 200 | 250 | 300 | 500 | 1000 | 1500 | 2000 | 2500 | 5000;
@@ -4,7 +4,7 @@ import { TComparisonResult } from "../sorting/_index.js";
4
4
  import TimeBase from "./TimeBase";
5
5
  import TimeDuration from "./TimeDuration";
6
6
  import { TTimeInstantBuilder, TTimeInstantCreationParameters, createTimeInstantFromParameters, timeInstantBuilder } from "./TimeInstantBuilder.js";
7
- import TimeUnit from "./TimeUnit";
7
+ import { TimeUnit } from "./TimeUnit";
8
8
  import { TDayOfMonth, TDayOfWeek, TIso8601DateString, TIso8601DateUtcString, TMonth, TWeekNumber } from "./types";
9
9
 
10
10
  export class TimeInstant extends TimeBase<TimeInstant> {
@@ -1,5 +1,5 @@
1
1
 
2
- export default class TimeUnit {
2
+ export class TimeUnit {
3
3
 
4
4
  private constructor(
5
5
  public readonly multiplier: number
@@ -29,15 +29,10 @@ export default class TimeUnit {
29
29
  return this.toUnit( value, TimeUnit.DAYS );
30
30
  }
31
31
 
32
- public toWeeks( value: number ): number {
33
- return this.toUnit( value, TimeUnit.WEEKS );
34
- }
35
-
36
32
  public static readonly MILLISECONDS = new TimeUnit( 1 );
37
33
  public static readonly SECONDS = new TimeUnit( 1000 * TimeUnit.MILLISECONDS.multiplier );
38
34
  public static readonly MINUTES = new TimeUnit( 60 * TimeUnit.SECONDS.multiplier );
39
35
  public static readonly HOURS = new TimeUnit( 60 * TimeUnit.MINUTES.multiplier );
40
36
  public static readonly DAYS = new TimeUnit( 24 * TimeUnit.HOURS.multiplier );
41
- public static readonly WEEKS = new TimeUnit( 7 * TimeUnit.DAYS.multiplier );
42
37
 
43
38
  }
@@ -1,9 +1,10 @@
1
1
 
2
2
  export { default as RandomTimeDuration } from './RandomTimeDuration.js';
3
- export { TPredefinedTimeDuration, TValidTimeDuration, TimeDuration, isAllowedTimeDuration } from './TimeDuration.js';
3
+ export * from './TimeBase.js';
4
+ export { isAllowedTimeDuration, TimeDuration, TPredefinedTimeDuration, TValidTimeDuration } from './TimeDuration.js';
4
5
  export { default as TimeFrequency } from './TimeFrequency.js';
5
6
  export * from './TimeInstant.js';
6
7
  export * from './TimeRange.js';
7
- export { default as TimeUnit } from './TimeUnit.js';
8
+ export * from './TimeUnit.js';
8
9
  export * from './types.js';
9
10
 
@@ -17,3 +17,16 @@ export function isError( e: unknown ): e is Error {
17
17
  return e instanceof Error;
18
18
  }
19
19
 
20
+ export function getMessageFromError( error: Error ): string {
21
+ const maybeCause = error.cause ? getMessageFromError( asError( error.cause ) ) : null;
22
+ const cause = maybeCause ? `\ncaused by: ${maybeCause}` : '';
23
+ return `${error.name}: ${error.message}${cause}`;
24
+ }
25
+
26
+ export function getStackFromError( error: Error ): string {
27
+ const maybeCause = error.cause ? getStackFromError( asError( error.cause ) ) : null;
28
+ const cause = maybeCause ? `\ncaused by: ${maybeCause}` : '';
29
+ const stack = error.stack && error.stack.includes( error.message ) ? error.stack : error.message + ' ' + ( error.stack ?? '' );
30
+ return `${error.name}: ${stack}${cause}`;
31
+ }
32
+
@@ -0,0 +1,4 @@
1
+ export * from './constant.js';
2
+ export * from './iff.js';
3
+ export * from './predicateBuilder.js';
4
+
@@ -0,0 +1,25 @@
1
+
2
+ import { constantTrue, type TPredicate } from "../functions.ts";
3
+
4
+ export class PredicateBuilder<T> {
5
+
6
+ private _currentPredicate: TPredicate<T> = constantTrue;
7
+
8
+ public and( predicate: TPredicate<T> ) {
9
+ const curPredicate = this._currentPredicate.bind( undefined );
10
+ const newPredicate = ( t: T ) => curPredicate( t ) && predicate( t );
11
+ this._currentPredicate = newPredicate;
12
+ return this;
13
+ }
14
+
15
+ public or( predicate: TPredicate<T> ) {
16
+ const newPredicate = ( t: T ) => this._currentPredicate( t ) || predicate( t );
17
+ this._currentPredicate = newPredicate;
18
+ return this;
19
+ }
20
+
21
+ public toPredicate() {
22
+ return this._currentPredicate;
23
+ }
24
+
25
+ }
@@ -1,8 +1,5 @@
1
- import { identity } from './_index.js';
2
- import { constantTrue } from './functions.js';
3
-
4
- export * from './functions/constant.js';
5
- export * from './functions/iff.js';
1
+ import { constantTrue, identity } from './functions/constant.js';
2
+ export * from './functions/_index.js';
6
3
 
7
4
  export type TFunction<A, R> = ( a: A ) => R;
8
5
  export type TTransformer<A, R> = TFunction<A, R>;
@@ -38,3 +38,13 @@ export type TWithRequiredProperty<T, K extends keyof T> = T & {
38
38
  * Given a type T, make all fields required instead of optional.
39
39
  */
40
40
  export type TWithRequiredProperties<T> = Required<T>;
41
+
42
+ /**
43
+ * Type that forces a record to contain a few properties, but allows for extraneous properties.
44
+ */
45
+ export type TWithExtras<R extends Record<any, any>, T extends Record<any, any> = Record<string, unknown>> = R & T;
46
+
47
+ /**
48
+ * Types that improves the human readability of a Typescript type.
49
+ */
50
+ export type TPrettify<T> = { [ K in keyof T ]: T[ K ]; } & {};