@naturalcycles/js-lib 14.272.0 → 14.274.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.
@@ -1,4 +1,4 @@
1
- import type { Inclusiveness, IsoDate, IsoDateTime, MonthId, NumberOfHours, NumberOfMinutes, SortDirection, UnixTimestamp, UnixTimestampMillis } from '../types';
1
+ import type { IANATimezone, Inclusiveness, IsoDate, IsoDateTime, MonthId, NumberOfHours, NumberOfMinutes, SortDirection, UnixTimestamp, UnixTimestampMillis } from '../types';
2
2
  import { LocalDate } from './localDate';
3
3
  import { WallTime } from './wallTime';
4
4
  export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
@@ -54,7 +54,7 @@ export declare class LocalTime {
54
54
  *
55
55
  * @experimental
56
56
  */
57
- inTimezone(tz: string): WallTime;
57
+ inTimezone(tz: IANATimezone): WallTime;
58
58
  /**
59
59
  * UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
60
60
  * to the local time to get UTC time.
@@ -69,7 +69,7 @@ export declare class LocalTime {
69
69
  *
70
70
  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
71
71
  */
72
- getUTCOffsetMinutes(tz?: string): NumberOfMinutes;
72
+ getUTCOffsetMinutes(tz?: IANATimezone): NumberOfMinutes;
73
73
  /**
74
74
  * Same as getUTCOffsetMinutes, but rounded to hours.
75
75
  *
@@ -80,11 +80,11 @@ export declare class LocalTime {
80
80
  * If timezone (tz) is specified, e.g `America/New_York`,
81
81
  * it will return the UTC offset for that timezone.
82
82
  */
83
- getUTCOffsetHours(tz?: string): NumberOfHours;
83
+ getUTCOffsetHours(tz?: IANATimezone): NumberOfHours;
84
84
  /**
85
85
  * Returns e.g `-05:00` for New_York winter time.
86
86
  */
87
- getUTCOffsetString(tz: string): string;
87
+ getUTCOffsetString(tz: IANATimezone): string;
88
88
  get(unit: LocalTimeUnit): number;
89
89
  set(unit: LocalTimeUnit, v: number, mutate?: boolean): LocalTime;
90
90
  get year(): number;
@@ -220,6 +220,7 @@ export declare class LocalTime {
220
220
  * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
221
221
  */
222
222
  toISOTime(seconds?: boolean): string;
223
+ toWallTime(): WallTime;
223
224
  /**
224
225
  * Returns e.g: `19840621_1705`
225
226
  */
@@ -304,7 +305,7 @@ declare class LocalTimeFactory {
304
305
  * Returns the IANA timezone e.g `Europe/Stockholm`.
305
306
  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
306
307
  */
307
- getTimezone(): string;
308
+ getTimezone(): IANATimezone;
308
309
  /**
309
310
  * Returns true if passed IANA timezone is valid/supported.
310
311
  * E.g `Europe/Stockholm` is valid, but `Europe/Stockholm2` is not.
@@ -607,6 +607,9 @@ class LocalTime {
607
607
  // !! Not using toISOString(), as it returns time in UTC, not in local timezone (unexpected!)
608
608
  // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
609
609
  }
610
+ toWallTime() {
611
+ return new wallTime_1.WallTime(this.toDateTimeObject());
612
+ }
610
613
  /**
611
614
  * Returns e.g: `19840621_1705`
612
615
  */
@@ -853,6 +856,8 @@ class LocalTimeFactory {
853
856
  * consider caching the Intl.supportedValuesOf values as Set and reuse that.
854
857
  */
855
858
  isTimezoneValid(tz) {
859
+ if (tz === 'UTC')
860
+ return true; // we deliberately consider UTC a valid timezone, while it's mostly used in testing
856
861
  return Intl.supportedValuesOf('timeZone').includes(tz);
857
862
  }
858
863
  sort(items, dir = 'asc', mutate = false) {
@@ -1,5 +1,6 @@
1
- import { IsoDate } from '../types';
2
- import { DateTimeObject } from './localTime';
1
+ import { IsoDate, IsoDateTime } from '../types';
2
+ import { LocalDate } from './localDate';
3
+ import { DateTimeObject, LocalTime } from './localTime';
3
4
  /**
4
5
  * Representation of a "time on the wall clock",
5
6
  * which means "local time, regardless of timezone".
@@ -17,12 +18,26 @@ export declare class WallTime implements DateTimeObject {
17
18
  minute: number;
18
19
  second: number;
19
20
  constructor(obj: DateTimeObject);
21
+ toLocalDate(): LocalDate;
22
+ /**
23
+ * Example:
24
+ * WallTime is 1984-06-21 17:56:21
25
+ * .toLocalTime() will return a LocalTime Date instance
26
+ * holding that time in the local timezone.
27
+ */
28
+ toLocalTime(): LocalTime;
29
+ toJSON(): IsoDateTime;
30
+ toString(): IsoDateTime;
20
31
  /**
21
32
  * Returns e.g: `1984-06-21 17:56:21`
22
33
  * or (if seconds=false):
23
34
  * `1984-06-21 17:56`
24
35
  */
25
36
  toPretty(seconds?: boolean): string;
37
+ /**
38
+ * Returns e.g: `1984-06-21T17:56:21`
39
+ */
40
+ toISODateTime(): IsoDateTime;
26
41
  /**
27
42
  * Returns e.g: `1984-06-21`, only the date part of DateTime
28
43
  */
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WallTime = void 0;
4
+ const localDate_1 = require("./localDate");
5
+ const localTime_1 = require("./localTime");
4
6
  /**
5
7
  * Representation of a "time on the wall clock",
6
8
  * which means "local time, regardless of timezone".
@@ -14,6 +16,24 @@ class WallTime {
14
16
  constructor(obj) {
15
17
  Object.assign(this, obj);
16
18
  }
19
+ toLocalDate() {
20
+ return new localDate_1.LocalDate(this.year, this.month, this.day);
21
+ }
22
+ /**
23
+ * Example:
24
+ * WallTime is 1984-06-21 17:56:21
25
+ * .toLocalTime() will return a LocalTime Date instance
26
+ * holding that time in the local timezone.
27
+ */
28
+ toLocalTime() {
29
+ return localTime_1.localTime.fromDateTimeObject(this);
30
+ }
31
+ toJSON() {
32
+ return this.toISODateTime();
33
+ }
34
+ toString() {
35
+ return this.toISODateTime();
36
+ }
17
37
  /**
18
38
  * Returns e.g: `1984-06-21 17:56:21`
19
39
  * or (if seconds=false):
@@ -22,6 +42,12 @@ class WallTime {
22
42
  toPretty(seconds = true) {
23
43
  return this.toISODate() + ' ' + this.toISOTime(seconds);
24
44
  }
45
+ /**
46
+ * Returns e.g: `1984-06-21T17:56:21`
47
+ */
48
+ toISODateTime() {
49
+ return (this.toISODate() + 'T' + this.toISOTime());
50
+ }
25
51
  /**
26
52
  * Returns e.g: `1984-06-21`, only the date part of DateTime
27
53
  */
package/dist/index.d.ts CHANGED
@@ -50,6 +50,7 @@ export * from './json-schema/jsonSchema.model';
50
50
  export * from './json-schema/jsonSchema.util';
51
51
  export * from './json-schema/jsonSchemaBuilder';
52
52
  export * from './log/commonLogger';
53
+ export * from './math/accumulatingAverage';
53
54
  export * from './math/math.util';
54
55
  export * from './math/sma';
55
56
  export * from './math/stack.util';
package/dist/index.js CHANGED
@@ -54,6 +54,7 @@ tslib_1.__exportStar(require("./json-schema/jsonSchema.model"), exports);
54
54
  tslib_1.__exportStar(require("./json-schema/jsonSchema.util"), exports);
55
55
  tslib_1.__exportStar(require("./json-schema/jsonSchemaBuilder"), exports);
56
56
  tslib_1.__exportStar(require("./log/commonLogger"), exports);
57
+ tslib_1.__exportStar(require("./math/accumulatingAverage"), exports);
57
58
  tslib_1.__exportStar(require("./math/math.util"), exports);
58
59
  tslib_1.__exportStar(require("./math/sma"), exports);
59
60
  tslib_1.__exportStar(require("./math/stack.util"), exports);
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Container that allows to accumulate average of a set of numbers,
3
+ * without the need to store all of them in memory.
4
+ *
5
+ * @experimental
6
+ */
7
+ export declare class AccumulatingAverage {
8
+ total: number;
9
+ count: number;
10
+ /**
11
+ * Returns the current average.
12
+ * Returns 0 if no values have been added.
13
+ */
14
+ get average(): number;
15
+ /**
16
+ * Adds a new value.
17
+ */
18
+ add(value: number): void;
19
+ reset(): void;
20
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccumulatingAverage = void 0;
4
+ /**
5
+ * Container that allows to accumulate average of a set of numbers,
6
+ * without the need to store all of them in memory.
7
+ *
8
+ * @experimental
9
+ */
10
+ class AccumulatingAverage {
11
+ constructor() {
12
+ this.total = 0;
13
+ this.count = 0;
14
+ }
15
+ /**
16
+ * Returns the current average.
17
+ * Returns 0 if no values have been added.
18
+ */
19
+ get average() {
20
+ if (this.count === 0)
21
+ return 0;
22
+ return this.total / this.count;
23
+ }
24
+ /**
25
+ * Adds a new value.
26
+ */
27
+ add(value) {
28
+ this.total += value;
29
+ this.count++;
30
+ }
31
+ reset() {
32
+ this.total = 0;
33
+ this.count = 0;
34
+ }
35
+ }
36
+ exports.AccumulatingAverage = AccumulatingAverage;
package/dist/types.d.ts CHANGED
@@ -202,7 +202,7 @@ export type MonthId = string;
202
202
  *
203
203
  * @example 'America/New_York'
204
204
  */
205
- export type IanaTimezone = Branded<string, 'IanaTimezone'>;
205
+ export type IANATimezone = Branded<string, 'IANATimezone'>;
206
206
  /**
207
207
  * Branded UnixTimestamp in seconds.
208
208
  * Extends (compatible with) `number`.
@@ -604,6 +604,9 @@ export class LocalTime {
604
604
  // !! Not using toISOString(), as it returns time in UTC, not in local timezone (unexpected!)
605
605
  // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
606
606
  }
607
+ toWallTime() {
608
+ return new WallTime(this.toDateTimeObject());
609
+ }
607
610
  /**
608
611
  * Returns e.g: `19840621_1705`
609
612
  */
@@ -849,6 +852,8 @@ class LocalTimeFactory {
849
852
  * consider caching the Intl.supportedValuesOf values as Set and reuse that.
850
853
  */
851
854
  isTimezoneValid(tz) {
855
+ if (tz === 'UTC')
856
+ return true; // we deliberately consider UTC a valid timezone, while it's mostly used in testing
852
857
  return Intl.supportedValuesOf('timeZone').includes(tz);
853
858
  }
854
859
  sort(items, dir = 'asc', mutate = false) {
@@ -1,3 +1,5 @@
1
+ import { LocalDate } from './localDate';
2
+ import { localTime } from './localTime';
1
3
  /**
2
4
  * Representation of a "time on the wall clock",
3
5
  * which means "local time, regardless of timezone".
@@ -11,6 +13,24 @@ export class WallTime {
11
13
  constructor(obj) {
12
14
  Object.assign(this, obj);
13
15
  }
16
+ toLocalDate() {
17
+ return new LocalDate(this.year, this.month, this.day);
18
+ }
19
+ /**
20
+ * Example:
21
+ * WallTime is 1984-06-21 17:56:21
22
+ * .toLocalTime() will return a LocalTime Date instance
23
+ * holding that time in the local timezone.
24
+ */
25
+ toLocalTime() {
26
+ return localTime.fromDateTimeObject(this);
27
+ }
28
+ toJSON() {
29
+ return this.toISODateTime();
30
+ }
31
+ toString() {
32
+ return this.toISODateTime();
33
+ }
14
34
  /**
15
35
  * Returns e.g: `1984-06-21 17:56:21`
16
36
  * or (if seconds=false):
@@ -19,6 +39,12 @@ export class WallTime {
19
39
  toPretty(seconds = true) {
20
40
  return this.toISODate() + ' ' + this.toISOTime(seconds);
21
41
  }
42
+ /**
43
+ * Returns e.g: `1984-06-21T17:56:21`
44
+ */
45
+ toISODateTime() {
46
+ return (this.toISODate() + 'T' + this.toISOTime());
47
+ }
22
48
  /**
23
49
  * Returns e.g: `1984-06-21`, only the date part of DateTime
24
50
  */
package/dist-esm/index.js CHANGED
@@ -50,6 +50,7 @@ export * from './json-schema/jsonSchema.model';
50
50
  export * from './json-schema/jsonSchema.util';
51
51
  export * from './json-schema/jsonSchemaBuilder';
52
52
  export * from './log/commonLogger';
53
+ export * from './math/accumulatingAverage';
53
54
  export * from './math/math.util';
54
55
  export * from './math/sma';
55
56
  export * from './math/stack.util';
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Container that allows to accumulate average of a set of numbers,
3
+ * without the need to store all of them in memory.
4
+ *
5
+ * @experimental
6
+ */
7
+ export class AccumulatingAverage {
8
+ constructor() {
9
+ this.total = 0;
10
+ this.count = 0;
11
+ }
12
+ /**
13
+ * Returns the current average.
14
+ * Returns 0 if no values have been added.
15
+ */
16
+ get average() {
17
+ if (this.count === 0)
18
+ return 0;
19
+ return this.total / this.count;
20
+ }
21
+ /**
22
+ * Adds a new value.
23
+ */
24
+ add(value) {
25
+ this.total += value;
26
+ this.count++;
27
+ }
28
+ reset() {
29
+ this.total = 0;
30
+ this.count = 0;
31
+ }
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.272.0",
3
+ "version": "14.274.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build-esm-cjs",
@@ -21,16 +21,17 @@
21
21
  },
22
22
  "devDependencies": {
23
23
  "@naturalcycles/bench-lib": "^3",
24
- "@naturalcycles/dev-lib": "^15",
24
+ "@naturalcycles/dev-lib": "^17",
25
25
  "@naturalcycles/nodejs-lib": "^13",
26
26
  "@naturalcycles/time-lib": "^3",
27
27
  "@types/crypto-js": "^4",
28
28
  "@types/node": "^22",
29
29
  "@types/semver": "^7",
30
+ "@vitest/coverage-v8": "^3",
30
31
  "crypto-js": "^4",
31
- "jest": "^29",
32
- "prettier": "^3",
32
+ "tsx": "^4.19.3",
33
33
  "vitepress": "^1",
34
+ "vitest": "^3",
34
35
  "vue": "^3"
35
36
  },
36
37
  "files": [
@@ -1,6 +1,7 @@
1
1
  import { _assert } from '../error/assert'
2
2
  import { _ms } from '../time/time.util'
3
3
  import type {
4
+ IANATimezone,
4
5
  Inclusiveness,
5
6
  IsoDate,
6
7
  IsoDateTime,
@@ -101,7 +102,7 @@ export class LocalTime {
101
102
  *
102
103
  * @experimental
103
104
  */
104
- inTimezone(tz: string): WallTime {
105
+ inTimezone(tz: IANATimezone): WallTime {
105
106
  const d = new Date(this.$date.toLocaleString('en-US', { timeZone: tz }))
106
107
  return new WallTime({
107
108
  year: d.getFullYear(),
@@ -127,7 +128,7 @@ export class LocalTime {
127
128
  *
128
129
  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
129
130
  */
130
- getUTCOffsetMinutes(tz?: string): NumberOfMinutes {
131
+ getUTCOffsetMinutes(tz?: IANATimezone): NumberOfMinutes {
131
132
  if (tz) {
132
133
  // based on: https://stackoverflow.com/a/53652131/4919972
133
134
  const nowTime = this.$date.getTime()
@@ -148,14 +149,14 @@ export class LocalTime {
148
149
  * If timezone (tz) is specified, e.g `America/New_York`,
149
150
  * it will return the UTC offset for that timezone.
150
151
  */
151
- getUTCOffsetHours(tz?: string): NumberOfHours {
152
+ getUTCOffsetHours(tz?: IANATimezone): NumberOfHours {
152
153
  return Math.round(this.getUTCOffsetMinutes(tz) / 60)
153
154
  }
154
155
 
155
156
  /**
156
157
  * Returns e.g `-05:00` for New_York winter time.
157
158
  */
158
- getUTCOffsetString(tz: string): string {
159
+ getUTCOffsetString(tz: IANATimezone): string {
159
160
  const minutes = this.getUTCOffsetMinutes(tz)
160
161
  const hours = Math.trunc(minutes / 60)
161
162
  const sign = hours < 0 ? '-' : '+'
@@ -726,6 +727,10 @@ export class LocalTime {
726
727
  // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
727
728
  }
728
729
 
730
+ toWallTime(): WallTime {
731
+ return new WallTime(this.toDateTimeObject())
732
+ }
733
+
729
734
  /**
730
735
  * Returns e.g: `19840621_1705`
731
736
  */
@@ -982,8 +987,8 @@ class LocalTimeFactory {
982
987
  * Returns the IANA timezone e.g `Europe/Stockholm`.
983
988
  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
984
989
  */
985
- getTimezone(): string {
986
- return Intl.DateTimeFormat().resolvedOptions().timeZone
990
+ getTimezone(): IANATimezone {
991
+ return Intl.DateTimeFormat().resolvedOptions().timeZone as IANATimezone
987
992
  }
988
993
 
989
994
  /**
@@ -994,6 +999,7 @@ class LocalTimeFactory {
994
999
  * consider caching the Intl.supportedValuesOf values as Set and reuse that.
995
1000
  */
996
1001
  isTimezoneValid(tz: string): boolean {
1002
+ if (tz === 'UTC') return true // we deliberately consider UTC a valid timezone, while it's mostly used in testing
997
1003
  return Intl.supportedValuesOf('timeZone').includes(tz)
998
1004
  }
999
1005
 
@@ -1,5 +1,6 @@
1
- import { IsoDate } from '../types'
2
- import { DateTimeObject } from './localTime'
1
+ import { IsoDate, IsoDateTime } from '../types'
2
+ import { LocalDate } from './localDate'
3
+ import { DateTimeObject, LocalTime, localTime } from './localTime'
3
4
 
4
5
  /**
5
6
  * Representation of a "time on the wall clock",
@@ -22,6 +23,28 @@ export class WallTime implements DateTimeObject {
22
23
  Object.assign(this, obj)
23
24
  }
24
25
 
26
+ toLocalDate(): LocalDate {
27
+ return new LocalDate(this.year, this.month, this.day)
28
+ }
29
+
30
+ /**
31
+ * Example:
32
+ * WallTime is 1984-06-21 17:56:21
33
+ * .toLocalTime() will return a LocalTime Date instance
34
+ * holding that time in the local timezone.
35
+ */
36
+ toLocalTime(): LocalTime {
37
+ return localTime.fromDateTimeObject(this)
38
+ }
39
+
40
+ toJSON(): IsoDateTime {
41
+ return this.toISODateTime()
42
+ }
43
+
44
+ toString(): IsoDateTime {
45
+ return this.toISODateTime()
46
+ }
47
+
25
48
  /**
26
49
  * Returns e.g: `1984-06-21 17:56:21`
27
50
  * or (if seconds=false):
@@ -31,6 +54,13 @@ export class WallTime implements DateTimeObject {
31
54
  return this.toISODate() + ' ' + this.toISOTime(seconds)
32
55
  }
33
56
 
57
+ /**
58
+ * Returns e.g: `1984-06-21T17:56:21`
59
+ */
60
+ toISODateTime(): IsoDateTime {
61
+ return (this.toISODate() + 'T' + this.toISOTime()) as IsoDateTime
62
+ }
63
+
34
64
  /**
35
65
  * Returns e.g: `1984-06-21`, only the date part of DateTime
36
66
  */
package/src/index.ts CHANGED
@@ -50,6 +50,7 @@ export * from './json-schema/jsonSchema.model'
50
50
  export * from './json-schema/jsonSchema.util'
51
51
  export * from './json-schema/jsonSchemaBuilder'
52
52
  export * from './log/commonLogger'
53
+ export * from './math/accumulatingAverage'
53
54
  export * from './math/math.util'
54
55
  export * from './math/sma'
55
56
  export * from './math/stack.util'
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Container that allows to accumulate average of a set of numbers,
3
+ * without the need to store all of them in memory.
4
+ *
5
+ * @experimental
6
+ */
7
+ export class AccumulatingAverage {
8
+ total = 0
9
+ count = 0
10
+
11
+ /**
12
+ * Returns the current average.
13
+ * Returns 0 if no values have been added.
14
+ */
15
+ get average(): number {
16
+ if (this.count === 0) return 0
17
+ return this.total / this.count
18
+ }
19
+
20
+ /**
21
+ * Adds a new value.
22
+ */
23
+ add(value: number): void {
24
+ this.total += value
25
+ this.count++
26
+ }
27
+
28
+ reset(): void {
29
+ this.total = 0
30
+ this.count = 0
31
+ }
32
+ }
package/src/types.ts CHANGED
@@ -260,7 +260,7 @@ export type MonthId = string
260
260
  *
261
261
  * @example 'America/New_York'
262
262
  */
263
- export type IanaTimezone = Branded<string, 'IanaTimezone'>
263
+ export type IANATimezone = Branded<string, 'IANATimezone'>
264
264
 
265
265
  /**
266
266
  * Branded UnixTimestamp in seconds.