@naturalcycles/js-lib 14.272.0 → 14.273.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.
- package/dist/datetime/localTime.d.ts +6 -6
- package/dist/datetime/localTime.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/math/accumulatingAverage.d.ts +20 -0
- package/dist/math/accumulatingAverage.js +36 -0
- package/dist/types.d.ts +1 -1
- package/dist-esm/datetime/localTime.js +2 -0
- package/dist-esm/index.js +1 -0
- package/dist-esm/math/accumulatingAverage.js +32 -0
- package/package.json +1 -1
- package/src/datetime/localTime.ts +8 -6
- package/src/index.ts +1 -0
- package/src/math/accumulatingAverage.ts +32 -0
- package/src/types.ts +1 -1
|
@@ -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:
|
|
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?:
|
|
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?:
|
|
83
|
+
getUTCOffsetHours(tz?: IANATimezone): NumberOfHours;
|
|
84
84
|
/**
|
|
85
85
|
* Returns e.g `-05:00` for New_York winter time.
|
|
86
86
|
*/
|
|
87
|
-
getUTCOffsetString(tz:
|
|
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;
|
|
@@ -304,7 +304,7 @@ declare class LocalTimeFactory {
|
|
|
304
304
|
* Returns the IANA timezone e.g `Europe/Stockholm`.
|
|
305
305
|
* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
306
306
|
*/
|
|
307
|
-
getTimezone():
|
|
307
|
+
getTimezone(): IANATimezone;
|
|
308
308
|
/**
|
|
309
309
|
* Returns true if passed IANA timezone is valid/supported.
|
|
310
310
|
* E.g `Europe/Stockholm` is valid, but `Europe/Stockholm2` is not.
|
|
@@ -853,6 +853,8 @@ class LocalTimeFactory {
|
|
|
853
853
|
* consider caching the Intl.supportedValuesOf values as Set and reuse that.
|
|
854
854
|
*/
|
|
855
855
|
isTimezoneValid(tz) {
|
|
856
|
+
if (tz === 'UTC')
|
|
857
|
+
return true; // we deliberately consider UTC a valid timezone, while it's mostly used in testing
|
|
856
858
|
return Intl.supportedValuesOf('timeZone').includes(tz);
|
|
857
859
|
}
|
|
858
860
|
sort(items, dir = 'asc', mutate = false) {
|
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
|
|
205
|
+
export type IANATimezone = Branded<string, 'IANATimezone'>;
|
|
206
206
|
/**
|
|
207
207
|
* Branded UnixTimestamp in seconds.
|
|
208
208
|
* Extends (compatible with) `number`.
|
|
@@ -849,6 +849,8 @@ class LocalTimeFactory {
|
|
|
849
849
|
* consider caching the Intl.supportedValuesOf values as Set and reuse that.
|
|
850
850
|
*/
|
|
851
851
|
isTimezoneValid(tz) {
|
|
852
|
+
if (tz === 'UTC')
|
|
853
|
+
return true; // we deliberately consider UTC a valid timezone, while it's mostly used in testing
|
|
852
854
|
return Intl.supportedValuesOf('timeZone').includes(tz);
|
|
853
855
|
}
|
|
854
856
|
sort(items, dir = 'asc', mutate = false) {
|
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,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:
|
|
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?:
|
|
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?:
|
|
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:
|
|
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 ? '-' : '+'
|
|
@@ -982,8 +983,8 @@ class LocalTimeFactory {
|
|
|
982
983
|
* Returns the IANA timezone e.g `Europe/Stockholm`.
|
|
983
984
|
* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
984
985
|
*/
|
|
985
|
-
getTimezone():
|
|
986
|
-
return Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
986
|
+
getTimezone(): IANATimezone {
|
|
987
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone as IANATimezone
|
|
987
988
|
}
|
|
988
989
|
|
|
989
990
|
/**
|
|
@@ -994,6 +995,7 @@ class LocalTimeFactory {
|
|
|
994
995
|
* consider caching the Intl.supportedValuesOf values as Set and reuse that.
|
|
995
996
|
*/
|
|
996
997
|
isTimezoneValid(tz: string): boolean {
|
|
998
|
+
if (tz === 'UTC') return true // we deliberately consider UTC a valid timezone, while it's mostly used in testing
|
|
997
999
|
return Intl.supportedValuesOf('timeZone').includes(tz)
|
|
998
1000
|
}
|
|
999
1001
|
|
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
|
|
263
|
+
export type IANATimezone = Branded<string, 'IANATimezone'>
|
|
264
264
|
|
|
265
265
|
/**
|
|
266
266
|
* Branded UnixTimestamp in seconds.
|