@naturalcycles/js-lib 15.63.0 → 15.64.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,29 +1,29 @@
1
- import { type Comparator, type Mapper, type SortDirection, type SortOptions, type StringMap } from '../types.js';
1
+ import type { Comparator, Mapper, SortDirection, SortOptions, StringMap } from '../types.js';
2
2
  declare class Comparators {
3
3
  /**
4
4
  * Good for numbers.
5
5
  */
6
- numericAsc(a: number, b: number): number;
7
- numericDesc(a: number, b: number): number;
8
- localeAsc(a: string, b: string): number;
9
- localeDesc(a: string, b: string): number;
10
- by<T, COMPARE_TYPE extends string | number>(mapper: Mapper<T, COMPARE_TYPE>, opt?: ComparatorByOptions): Comparator<T>;
11
- updatedAsc(a: {
6
+ numericAsc(this: void, a: number, b: number): number;
7
+ numericDesc(this: void, a: number, b: number): number;
8
+ localeAsc(this: void, a: string, b: string): number;
9
+ localeDesc(this: void, a: string, b: string): number;
10
+ by<T, COMPARE_TYPE extends string | number>(this: void, mapper: Mapper<T, COMPARE_TYPE>, opt?: ComparatorByOptions): Comparator<T>;
11
+ updatedAsc(this: void, a: {
12
12
  updated: number;
13
13
  }, b: {
14
14
  updated: number;
15
15
  }): number;
16
- updatedDesc(a: {
16
+ updatedDesc(this: void, a: {
17
17
  updated: number;
18
18
  }, b: {
19
19
  updated: number;
20
20
  }): number;
21
- createdAsc(a: {
21
+ createdAsc(this: void, a: {
22
22
  created: number;
23
23
  }, b: {
24
24
  created: number;
25
25
  }): number;
26
- createdDesc(a: {
26
+ createdDesc(this: void, a: {
27
27
  created: number;
28
28
  }, b: {
29
29
  created: number;
@@ -1,4 +1,4 @@
1
- import { _stringMapValues, } from '../types.js';
1
+ import { _stringMapValues } from '../types.js';
2
2
  class Comparators {
3
3
  /**
4
4
  * Good for numbers.
package/dist/bot.model.js CHANGED
@@ -1,4 +1,5 @@
1
- export var BotReason;
1
+ export { BotReason };
2
+ var BotReason;
2
3
  (function (BotReason) {
3
4
  BotReason[BotReason["NoNavigator"] = 1] = "NoNavigator";
4
5
  BotReason[BotReason["NoUserAgent"] = 2] = "NoUserAgent";
@@ -43,6 +43,7 @@ export class AdminService {
43
43
  stopListening() {
44
44
  if (isServerSide())
45
45
  return;
46
+ // oxlint-disable-next-line typescript/unbound-method
46
47
  document.removeEventListener('keydown', this.keydownListener);
47
48
  this.listening = false;
48
49
  }
@@ -8,7 +8,6 @@ export async function loadGTag(gtagId, enabled = true) {
8
8
  return;
9
9
  globalThis.dataLayer ||= [];
10
10
  globalThis.gtag ||= function gtag() {
11
- // biome-ignore lint/complexity/noArguments: ok
12
11
  globalThis.dataLayer.push(arguments);
13
12
  };
14
13
  globalThis.gtag('js', new Date());
@@ -40,7 +39,6 @@ export function loadHotjar(hjid) {
40
39
  h.hj =
41
40
  h.hj ||
42
41
  function hj() {
43
- // biome-ignore lint/complexity/noArguments: ok
44
42
  ;
45
43
  (h.hj.q = h.hj.q || []).push(arguments);
46
44
  };
@@ -35,7 +35,7 @@ export class TranslationService {
35
35
  */
36
36
  async loadLocale(locale) {
37
37
  const locales = Array.isArray(locale) ? locale : [locale];
38
- await pMap(locales, async (locale) => {
38
+ await pMap(locales, async locale => {
39
39
  if (this.locales[locale])
40
40
  return; // already loaded
41
41
  this.locales[locale] = await this.cfg.translationLoader.load(locale);
@@ -16,8 +16,8 @@ export interface TopBarOptions {
16
16
  }
17
17
  export declare const topbar: {
18
18
  config(opts: TopBarOptions): void;
19
- set(show: boolean, opts?: TopBarOptions): void;
20
- show(opts?: TopBarOptions): void;
21
- progress(to: number | string): any;
19
+ set(show: boolean, opts?: TopBarOptions | undefined): void;
20
+ show(opts?: TopBarOptions | undefined): void;
21
+ progress(to: string | number): any;
22
22
  hide(): void;
23
23
  };
@@ -208,6 +208,15 @@ export declare class LocalTime {
208
208
  * `1984-06-21 17:56`
209
209
  */
210
210
  toPretty(seconds?: boolean): IsoDateTime;
211
+ /**
212
+ * Returns e.g: `1984-06-21T17:56:21Z`
213
+ *
214
+ * IsoDateTime "format" is ambiguous.
215
+ * Normally what we mean by it is `1984-06-21T17:56:21` (without Z).
216
+ * But sometimes you need Z.
217
+ * So, when you need Z - use this function.
218
+ */
219
+ toISODateTimeWithZ(): IsoDateTime;
211
220
  /**
212
221
  * Returns e.g: `1984-06-21T17:56:21`
213
222
  */
@@ -2,7 +2,8 @@ import { _assert } from '../error/assert.js';
2
2
  import { localDate } from './localDate.js';
3
3
  import { _ms } from './time.util.js';
4
4
  import { WallTime } from './wallTime.js';
5
- export var ISODayOfWeek;
5
+ export { ISODayOfWeek };
6
+ var ISODayOfWeek;
6
7
  (function (ISODayOfWeek) {
7
8
  ISODayOfWeek[ISODayOfWeek["MONDAY"] = 1] = "MONDAY";
8
9
  ISODayOfWeek[ISODayOfWeek["TUESDAY"] = 2] = "TUESDAY";
@@ -568,6 +569,17 @@ export class LocalTime {
568
569
  // const s = this.$date.toISOString()
569
570
  // return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16)
570
571
  }
572
+ /**
573
+ * Returns e.g: `1984-06-21T17:56:21Z`
574
+ *
575
+ * IsoDateTime "format" is ambiguous.
576
+ * Normally what we mean by it is `1984-06-21T17:56:21` (without Z).
577
+ * But sometimes you need Z.
578
+ * So, when you need Z - use this function.
579
+ */
580
+ toISODateTimeWithZ() {
581
+ return (this.toISODateTime() + 'Z');
582
+ }
571
583
  /**
572
584
  * Returns e.g: `1984-06-21T17:56:21`
573
585
  */
@@ -46,7 +46,7 @@ export const _AsyncMemo = (opt) => (target, key, descriptor) => {
46
46
  // console.log('return promise', promiseCache.size)
47
47
  return promise;
48
48
  }
49
- promise = cache.get(cacheKey).then(async (value) => {
49
+ promise = cache.get(cacheKey).then(async value => {
50
50
  if (value !== MISS) {
51
51
  // console.log('hit', promiseCache.size)
52
52
  promiseCache.delete(cacheKey);
@@ -55,7 +55,7 @@ export const _AsyncMemo = (opt) => (target, key, descriptor) => {
55
55
  // Miss
56
56
  // console.log('miss', promiseCache.size)
57
57
  return await onMiss();
58
- }, async (err) => {
58
+ }, async err => {
59
59
  // Log the cache error and proceed "as cache Miss"
60
60
  logger.error(err);
61
61
  return await onMiss();
@@ -16,8 +16,7 @@ import type { BackendErrorResponseObject, ErrorData, ErrorObject } from './error
16
16
  *
17
17
  * Since 2024-07-10 it no longer sets `userFriendly: true` by default.
18
18
  */
19
- export declare function _assert(condition: any, // will be evaluated as Boolean
20
- message?: string, errorData?: ErrorData): asserts condition;
19
+ export declare function _assert(condition: any, message?: string, errorData?: ErrorData): asserts condition;
21
20
  /**
22
21
  * Like _assert(), but prints more helpful error message.
23
22
  * API is similar to Node's assert.equals().
@@ -1,9 +1,10 @@
1
+ export { ErrorMode };
1
2
  /**
2
3
  * Allows to define error-controlling behaviour for batch operations.
3
4
  *
4
5
  * @default is THROW_IMMEDIATELY in most cases
5
6
  */
6
- export var ErrorMode;
7
+ var ErrorMode;
7
8
  (function (ErrorMode) {
8
9
  /**
9
10
  * Usually a default behaviour, similar as "exit early".
@@ -1,5 +1,5 @@
1
1
  import { ErrorMode } from '../error/errorMode.js';
2
- import { type CommonLogger, type CommonLogLevel } from '../log/commonLogger.js';
2
+ import type { CommonLogger, CommonLogLevel } from '../log/commonLogger.js';
3
3
  import type { AsyncFunction, PositiveInteger } from '../types.js';
4
4
  export interface PGradualQueueCfg {
5
5
  concurrency: PositiveInteger;
@@ -1,5 +1,5 @@
1
1
  import { ErrorMode } from '../error/errorMode.js';
2
- import { createCommonLoggerAtLevel, } from '../log/commonLogger.js';
2
+ import { createCommonLoggerAtLevel } from '../log/commonLogger.js';
3
3
  import { pDefer } from './pDefer.js';
4
4
  /**
5
5
  * A queue similar to PQueue that gradually increases concurrency from 1 to the configured
@@ -1,6 +1,6 @@
1
1
  import { ErrorMode } from '../error/errorMode.js';
2
2
  import type { CommonLogger } from '../log/commonLogger.js';
3
- import { type AbortableAsyncMapper } from '../types.js';
3
+ import type { AbortableAsyncMapper } from '../types.js';
4
4
  export interface PMapOptions {
5
5
  /**
6
6
  * Number of concurrently pending promises returned by `mapper`.
@@ -77,7 +77,7 @@ export async function pMap(iterable, mapper, opt = {}) {
77
77
  }
78
78
  resolvingCount++;
79
79
  Promise.resolve(nextItem)
80
- .then(async (element) => await mapper(element, i))
80
+ .then(async element => await mapper(element, i))
81
81
  .then(value => {
82
82
  if (value === END) {
83
83
  isSettled = true;
@@ -1,5 +1,5 @@
1
1
  import { ErrorMode } from '../error/errorMode.js';
2
- import { type CommonLogger, type CommonLogLevel } from '../log/commonLogger.js';
2
+ import type { CommonLogger, CommonLogLevel } from '../log/commonLogger.js';
3
3
  import type { AsyncFunction, PositiveInteger } from '../types.js';
4
4
  export interface PQueueCfg {
5
5
  concurrency: PositiveInteger;
@@ -1,5 +1,5 @@
1
1
  import { ErrorMode } from '../error/errorMode.js';
2
- import { createCommonLoggerAtLevel, } from '../log/commonLogger.js';
2
+ import { createCommonLoggerAtLevel } from '../log/commonLogger.js';
3
3
  import { pDefer } from './pDefer.js';
4
4
  /**
5
5
  * Inspired by: https://github.com/sindresorhus/p-queue
@@ -29,7 +29,6 @@ export async function pTimeout(fn, opt) {
29
29
  }
30
30
  const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt;
31
31
  const fakeError = opt.fakeError || new Error('TimeoutError');
32
- // biome-ignore lint/suspicious/noAsyncPromiseExecutor: ok
33
32
  return await new Promise(async (resolve, reject) => {
34
33
  // Prepare the timeout timer
35
34
  const timer = setTimeout(() => {
@@ -12,7 +12,7 @@ export type Simplify<T> = {
12
12
  @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
13
13
  @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
14
14
  */
15
- export type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? true : false;
15
+ export type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => (G extends U ? 1 : 2) ? true : false;
16
16
  /**
17
17
  * Filter out keys from an object.
18
18
  * Returns `never` if `Exclude` is strictly equal to `Key`.
@@ -14,7 +14,7 @@ import type { AppError } from '../error/error.util.js';
14
14
  *
15
15
  * @experimental
16
16
  */
17
- export type ValidationFunction<IN, OUT, ERR extends AppError> = (input: IN, opt?: ValidationFunctionOptions) => ValidationFunctionResult<OUT, ERR>;
17
+ export type ValidationFunction<OUT, ERR extends AppError> = (input: unknown, opt?: ValidationFunctionOptions) => ValidationFunctionResult<OUT, ERR>;
18
18
  export type ValidationFunctionResult<OUT, ERR extends AppError> = [err: ERR | null, output: OUT];
19
19
  export interface ValidationFunctionOptions {
20
20
  /**
@@ -2,10 +2,10 @@ import type { ZodError, ZodType } from 'zod';
2
2
  import type { ErrorData } from '../error/error.model.js';
3
3
  import { AppError } from '../error/error.util.js';
4
4
  import type { ValidationFunction, ValidationFunctionResult } from '../validation/validation.js';
5
- export declare function getZodValidationFunction<T>(schema: ZodType<T>): ValidationFunction<T, T, ZodValidationError>;
6
- export declare function zIsValid<T>(value: T, schema: ZodType<T>): boolean;
7
- export declare function zValidate<T>(value: T, schema: ZodType<T>): T;
8
- export declare function zSafeValidate<T>(input: T, schema: ZodType<T>): ValidationFunctionResult<T, ZodValidationError>;
5
+ export declare function getZodValidationFunction<T>(schema: ZodType<T>): ValidationFunction<T, ZodValidationError>;
6
+ export declare function zIsValid<T>(value: unknown, schema: ZodType<T>): boolean;
7
+ export declare function zValidate<T>(value: unknown, schema: ZodType<T>): T;
8
+ export declare function zSafeValidate<T>(input: unknown, schema: ZodType<T>): ValidationFunctionResult<T, ZodValidationError>;
9
9
  export interface ZodValidationErrorData extends ErrorData {
10
10
  }
11
11
  export declare class ZodValidationError extends AppError<ZodValidationErrorData> {
@@ -17,7 +17,9 @@ export function zValidate(value, schema) {
17
17
  throw err;
18
18
  return data;
19
19
  }
20
- export function zSafeValidate(input, schema) {
20
+ export function zSafeValidate(input, schema
21
+ // inputName?: string,
22
+ ) {
21
23
  const r = schema.safeParse(input);
22
24
  if (r.success) {
23
25
  return [null, r.data];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.63.0",
4
+ "version": "15.64.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -21,9 +21,10 @@
21
21
  "@types/crypto-js": "^4",
22
22
  "@types/node": "^25",
23
23
  "@types/semver": "^7",
24
+ "@typescript/native-preview": "7.0.0-dev.20260201.1",
24
25
  "crypto-js": "^4",
25
26
  "dayjs": "^1",
26
- "@naturalcycles/dev-lib": "20.27.0"
27
+ "@naturalcycles/dev-lib": "18.4.2"
27
28
  },
28
29
  "exports": {
29
30
  ".": "./dist/index.js",
package/src/array/sort.ts CHANGED
@@ -1,33 +1,28 @@
1
- import {
2
- _stringMapValues,
3
- type Comparator,
4
- type Mapper,
5
- type SortDirection,
6
- type SortOptions,
7
- type StringMap,
8
- } from '../types.js'
1
+ import { _stringMapValues } from '../types.js'
2
+ import type { Comparator, Mapper, SortDirection, SortOptions, StringMap } from '../types.js'
9
3
 
10
4
  class Comparators {
11
5
  /**
12
6
  * Good for numbers.
13
7
  */
14
- numericAsc(a: number, b: number): number {
8
+ numericAsc(this: void, a: number, b: number): number {
15
9
  return a - b
16
10
  }
17
11
 
18
- numericDesc(a: number, b: number): number {
12
+ numericDesc(this: void, a: number, b: number): number {
19
13
  return b - a
20
14
  }
21
15
 
22
- localeAsc(a: string, b: string): number {
16
+ localeAsc(this: void, a: string, b: string): number {
23
17
  return a.localeCompare(b)
24
18
  }
25
19
 
26
- localeDesc(a: string, b: string): number {
20
+ localeDesc(this: void, a: string, b: string): number {
27
21
  return -a.localeCompare(b)
28
22
  }
29
23
 
30
24
  by<T, COMPARE_TYPE extends string | number>(
25
+ this: void,
31
26
  mapper: Mapper<T, COMPARE_TYPE>,
32
27
  opt: ComparatorByOptions = {},
33
28
  ): Comparator<T> {
@@ -43,19 +38,19 @@ class Comparators {
43
38
  }
44
39
  }
45
40
 
46
- updatedAsc(a: { updated: number }, b: { updated: number }): number {
41
+ updatedAsc(this: void, a: { updated: number }, b: { updated: number }): number {
47
42
  return a.updated - b.updated
48
43
  }
49
44
 
50
- updatedDesc(a: { updated: number }, b: { updated: number }): number {
45
+ updatedDesc(this: void, a: { updated: number }, b: { updated: number }): number {
51
46
  return b.updated - a.updated
52
47
  }
53
48
 
54
- createdAsc(a: { created: number }, b: { created: number }): number {
49
+ createdAsc(this: void, a: { created: number }, b: { created: number }): number {
55
50
  return a.created - b.created
56
51
  }
57
52
 
58
- createdDesc(a: { created: number }, b: { created: number }): number {
53
+ createdDesc(this: void, a: { created: number }, b: { created: number }): number {
59
54
  return b.created - a.created
60
55
  }
61
56
  }
@@ -103,6 +103,7 @@ export class AdminService {
103
103
 
104
104
  stopListening(): void {
105
105
  if (isServerSide()) return
106
+ // oxlint-disable-next-line typescript/unbound-method
106
107
  document.removeEventListener('keydown', this.keydownListener)
107
108
  this.listening = false
108
109
  }
@@ -17,7 +17,6 @@ export async function loadGTag(gtagId: string, enabled = true): Promise<void> {
17
17
 
18
18
  globalThis.dataLayer ||= []
19
19
  globalThis.gtag ||= function gtag() {
20
- // biome-ignore lint/complexity/noArguments: ok
21
20
  globalThis.dataLayer.push(arguments)
22
21
  }
23
22
  globalThis.gtag('js', new Date())
@@ -53,7 +52,6 @@ export function loadHotjar(hjid: number): void {
53
52
  h.hj =
54
53
  h.hj ||
55
54
  function hj() {
56
- // biome-ignore lint/complexity/noArguments: ok
57
55
  ;(h.hj.q = h.hj.q || []).push(arguments)
58
56
  }
59
57
  h._hjSettings = { hjid, hjsv: 6 }
@@ -686,6 +686,18 @@ export class LocalTime {
686
686
  // return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16)
687
687
  }
688
688
 
689
+ /**
690
+ * Returns e.g: `1984-06-21T17:56:21Z`
691
+ *
692
+ * IsoDateTime "format" is ambiguous.
693
+ * Normally what we mean by it is `1984-06-21T17:56:21` (without Z).
694
+ * But sometimes you need Z.
695
+ * So, when you need Z - use this function.
696
+ */
697
+ toISODateTimeWithZ(): IsoDateTime {
698
+ return (this.toISODateTime() + 'Z') as IsoDateTime
699
+ }
700
+
689
701
  /**
690
702
  * Returns e.g: `1984-06-21T17:56:21`
691
703
  */
@@ -1,9 +1,6 @@
1
1
  import { ErrorMode } from '../error/errorMode.js'
2
- import {
3
- type CommonLogger,
4
- type CommonLogLevel,
5
- createCommonLoggerAtLevel,
6
- } from '../log/commonLogger.js'
2
+ import { createCommonLoggerAtLevel } from '../log/commonLogger.js'
3
+ import type { CommonLogger, CommonLogLevel } from '../log/commonLogger.js'
7
4
  import type { AsyncFunction, PositiveInteger } from '../types.js'
8
5
  import type { DeferredPromise } from './pDefer.js'
9
6
  import { pDefer } from './pDefer.js'
@@ -1,6 +1,7 @@
1
1
  import { ErrorMode } from '../error/errorMode.js'
2
2
  import type { CommonLogger } from '../log/commonLogger.js'
3
- import { type AbortableAsyncMapper, END, SKIP } from '../types.js'
3
+ import { END, SKIP } from '../types.js'
4
+ import type { AbortableAsyncMapper } from '../types.js'
4
5
 
5
6
  export interface PMapOptions {
6
7
  /**
@@ -1,9 +1,6 @@
1
1
  import { ErrorMode } from '../error/errorMode.js'
2
- import {
3
- type CommonLogger,
4
- type CommonLogLevel,
5
- createCommonLoggerAtLevel,
6
- } from '../log/commonLogger.js'
2
+ import { createCommonLoggerAtLevel } from '../log/commonLogger.js'
3
+ import type { CommonLogger, CommonLogLevel } from '../log/commonLogger.js'
7
4
  import type { AsyncFunction, PositiveInteger } from '../types.js'
8
5
  import type { DeferredPromise } from './pDefer.js'
9
6
  import { pDefer } from './pDefer.js'
@@ -73,7 +73,6 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
73
73
  const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt
74
74
  const fakeError = opt.fakeError || new Error('TimeoutError')
75
75
 
76
- // biome-ignore lint/suspicious/noAsyncPromiseExecutor: ok
77
76
  return await new Promise(async (resolve, reject) => {
78
77
  // Prepare the timeout timer
79
78
  const timer = setTimeout(() => {
@@ -15,8 +15,8 @@ import type { AppError } from '../error/error.util.js'
15
15
  *
16
16
  * @experimental
17
17
  */
18
- export type ValidationFunction<IN, OUT, ERR extends AppError> = (
19
- input: IN,
18
+ export type ValidationFunction<OUT, ERR extends AppError> = (
19
+ input: unknown,
20
20
  opt?: ValidationFunctionOptions,
21
21
  ) => ValidationFunctionResult<OUT, ERR>
22
22
 
@@ -7,26 +7,26 @@ import type { ValidationFunction, ValidationFunctionResult } from '../validation
7
7
 
8
8
  export function getZodValidationFunction<T>(
9
9
  schema: ZodType<T>,
10
- ): ValidationFunction<T, T, ZodValidationError> {
10
+ ): ValidationFunction<T, ZodValidationError> {
11
11
  return (input, opt) => {
12
12
  _assert(!opt?.mutateInput, 'mutateInput=true is not yet supported with Zod')
13
13
  return zSafeValidate(input, schema)
14
14
  }
15
15
  }
16
16
 
17
- export function zIsValid<T>(value: T, schema: ZodType<T>): boolean {
17
+ export function zIsValid<T>(value: unknown, schema: ZodType<T>): boolean {
18
18
  const { success } = schema.safeParse(value)
19
19
  return success
20
20
  }
21
21
 
22
- export function zValidate<T>(value: T, schema: ZodType<T>): T {
22
+ export function zValidate<T>(value: unknown, schema: ZodType<T>): T {
23
23
  const [err, data] = zSafeValidate(value, schema)
24
24
  if (err) throw err
25
25
  return data
26
26
  }
27
27
 
28
28
  export function zSafeValidate<T>(
29
- input: T,
29
+ input: unknown,
30
30
  schema: ZodType<T>,
31
31
  // inputName?: string,
32
32
  ): ValidationFunctionResult<T, ZodValidationError> {
@@ -35,7 +35,7 @@ export function zSafeValidate<T>(
35
35
  return [null, r.data]
36
36
  }
37
37
 
38
- return [new ZodValidationError(r.error, input, schema), r.data ?? input]
38
+ return [new ZodValidationError(r.error, input, schema), r.data ?? (input as T)]
39
39
  }
40
40
 
41
41
  export interface ZodValidationErrorData extends ErrorData {}