@naturalcycles/js-lib 14.271.2 → 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.
@@ -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;
@@ -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(): string;
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;
@@ -1,9 +1,18 @@
1
1
  /**
2
2
  * Converts the first character of string to upper case and the remaining to lower case.
3
+ * Returns a type-safe capitalized string.
3
4
  */
4
- export declare function _capitalize(s?: string): string;
5
- export declare function _upperFirst(s?: string): string;
6
- export declare function _lowerFirst(s: string): string;
5
+ export declare function _capitalize(s?: string): Capitalize<string>;
6
+ /**
7
+ * Convert a string to a type-safe uppercase string.
8
+ */
9
+ export declare function _toUpperCase(s: string): Uppercase<string>;
10
+ /**
11
+ * Convert a string to a type-safe lowercase string.
12
+ */
13
+ export declare function _toLowercase(s: string): Lowercase<string>;
14
+ export declare function _upperFirst(s?: string): Capitalize<string>;
15
+ export declare function _lowerFirst(s: string): Uncapitalize<string>;
7
16
  /**
8
17
  * Like String.split(), but with limit, returning the tail together with last element.
9
18
  *
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._capitalize = _capitalize;
4
+ exports._toUpperCase = _toUpperCase;
5
+ exports._toLowercase = _toLowercase;
4
6
  exports._upperFirst = _upperFirst;
5
7
  exports._lowerFirst = _lowerFirst;
6
8
  exports._split = _split;
@@ -15,15 +17,28 @@ exports._substringBetweenLast = _substringBetweenLast;
15
17
  exports._nl2br = _nl2br;
16
18
  /**
17
19
  * Converts the first character of string to upper case and the remaining to lower case.
20
+ * Returns a type-safe capitalized string.
18
21
  */
19
22
  function _capitalize(s = '') {
20
- return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
23
+ return (s.charAt(0).toUpperCase() + s.slice(1).toLowerCase());
24
+ }
25
+ /**
26
+ * Convert a string to a type-safe uppercase string.
27
+ */
28
+ function _toUpperCase(s) {
29
+ return s.toUpperCase();
30
+ }
31
+ /**
32
+ * Convert a string to a type-safe lowercase string.
33
+ */
34
+ function _toLowercase(s) {
35
+ return s.toLowerCase();
21
36
  }
22
37
  function _upperFirst(s = '') {
23
- return s.charAt(0).toUpperCase() + s.slice(1);
38
+ return (s.charAt(0).toUpperCase() + s.slice(1));
24
39
  }
25
40
  function _lowerFirst(s) {
26
- return s.charAt(0).toLowerCase() + s.slice(1);
41
+ return (s.charAt(0).toLowerCase() + s.slice(1));
27
42
  }
28
43
  /**
29
44
  * Like String.split(), but with limit, returning the tail together with last element.
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`.
@@ -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
+ }
@@ -1,14 +1,27 @@
1
1
  /**
2
2
  * Converts the first character of string to upper case and the remaining to lower case.
3
+ * Returns a type-safe capitalized string.
3
4
  */
4
5
  export function _capitalize(s = '') {
5
- return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
6
+ return (s.charAt(0).toUpperCase() + s.slice(1).toLowerCase());
7
+ }
8
+ /**
9
+ * Convert a string to a type-safe uppercase string.
10
+ */
11
+ export function _toUpperCase(s) {
12
+ return s.toUpperCase();
13
+ }
14
+ /**
15
+ * Convert a string to a type-safe lowercase string.
16
+ */
17
+ export function _toLowercase(s) {
18
+ return s.toLowerCase();
6
19
  }
7
20
  export function _upperFirst(s = '') {
8
- return s.charAt(0).toUpperCase() + s.slice(1);
21
+ return (s.charAt(0).toUpperCase() + s.slice(1));
9
22
  }
10
23
  export function _lowerFirst(s) {
11
- return s.charAt(0).toLowerCase() + s.slice(1);
24
+ return (s.charAt(0).toLowerCase() + s.slice(1));
12
25
  }
13
26
  /**
14
27
  * Like String.split(), but with limit, returning the tail together with last element.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.271.2",
3
+ "version": "14.273.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build-esm-cjs",
@@ -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 ? '-' : '+'
@@ -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(): string {
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
+ }
@@ -1,16 +1,31 @@
1
1
  /**
2
2
  * Converts the first character of string to upper case and the remaining to lower case.
3
+ * Returns a type-safe capitalized string.
3
4
  */
4
- export function _capitalize(s = ''): string {
5
- return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase()
5
+ export function _capitalize(s = ''): Capitalize<string> {
6
+ return (s.charAt(0).toUpperCase() + s.slice(1).toLowerCase()) as Capitalize<string>
6
7
  }
7
8
 
8
- export function _upperFirst(s = ''): string {
9
- return s.charAt(0).toUpperCase() + s.slice(1)
9
+ /**
10
+ * Convert a string to a type-safe uppercase string.
11
+ */
12
+ export function _toUpperCase(s: string): Uppercase<string> {
13
+ return s.toUpperCase() as Uppercase<string>
14
+ }
15
+
16
+ /**
17
+ * Convert a string to a type-safe lowercase string.
18
+ */
19
+ export function _toLowercase(s: string): Lowercase<string> {
20
+ return s.toLowerCase() as Lowercase<string>
21
+ }
22
+
23
+ export function _upperFirst(s = ''): Capitalize<string> {
24
+ return (s.charAt(0).toUpperCase() + s.slice(1)) as Capitalize<string>
10
25
  }
11
26
 
12
- export function _lowerFirst(s: string): string {
13
- return s.charAt(0).toLowerCase() + s.slice(1)
27
+ export function _lowerFirst(s: string): Uncapitalize<string> {
28
+ return (s.charAt(0).toLowerCase() + s.slice(1)) as Uncapitalize<string>
14
29
  }
15
30
 
16
31
  /**
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.