@naturalcycles/js-lib 15.16.2 → 15.17.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 { isServerSide } from '../index.js';
1
+ import { isServerSide } from '../env.js';
2
2
  import { loadScript } from './script.util.js';
3
3
  /**
4
4
  * Pass enabled = false to only init globalThis.gtag, but not load actual gtag script (e.g in dev mode).
@@ -1,4 +1,4 @@
1
- import type { CommonLogger } from '../log/index.js';
1
+ import type { CommonLogger } from '../log/commonLogger.js';
2
2
  /**
3
3
  * $r - result
4
4
  *
@@ -1,7 +1,7 @@
1
1
  import { _ms } from '../datetime/time.util.js';
2
- import { _assert } from '../error/index.js';
3
- import { SimpleMovingAverage } from '../math/index.js';
4
- import { _stringify } from '../string/index.js';
2
+ import { _assert } from '../error/assert.js';
3
+ import { SimpleMovingAverage } from '../math/sma.js';
4
+ import { _stringify } from '../string/stringify.js';
5
5
  import { _getArgsSignature, _getMethodSignature } from './decorator.util.js';
6
6
  /**
7
7
  * Console-logs when method had started, when it finished, time taken and if error happened.
@@ -1,2 +1,2 @@
1
- import type { PRetryOptions } from '../promise/index.js';
1
+ import type { PRetryOptions } from '../promise/pRetry.js';
2
2
  export declare function _Retry(opt?: PRetryOptions): MethodDecorator;
@@ -1,4 +1,4 @@
1
- import { pRetryFn } from '../promise/index.js';
1
+ import { pRetryFn } from '../promise/pRetry.js';
2
2
  // eslint-disable-next-line @typescript-eslint/naming-convention
3
3
  export function _Retry(opt = {}) {
4
4
  return (_target, _key, descriptor) => {
@@ -1,4 +1,4 @@
1
- import type { CommonLogger } from '../log/index.js';
1
+ import type { CommonLogger } from '../log/commonLogger.js';
2
2
  import type { AnyFunction } from '../types.js';
3
3
  export interface TryCatchOptions {
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { _since } from '../datetime/index.js';
1
+ import { _since } from '../datetime/time.util.js';
2
2
  import { _anyToError } from './error.util.js';
3
3
  /**
4
4
  * Decorates a function with "try/catch", so it'll never reject/throw.
package/dist/index.d.ts CHANGED
@@ -14,4 +14,5 @@ export * from './number/createDeterministicRandom.js';
14
14
  export * from './number/number.util.js';
15
15
  export * from './polyfill.js';
16
16
  export * from './unit/size.util.js';
17
+ export type * from './validation/validation.js';
17
18
  export * from './web.js';
@@ -1,4 +1,4 @@
1
- import { _uniq } from '../index.js';
1
+ import { _uniq } from '../array/array.util.js';
2
2
  import { _filterNullishValues } from '../object/object.util.js';
3
3
  /**
4
4
  * Merges s2 into s1 (mutates s1) and returns s1.
@@ -1,6 +1,5 @@
1
1
  import type { AnyObject, BaseDBEntity } from '../types.js';
2
- import { type JsonSchemaAllOf, type JsonSchemaArray, type JsonSchemaOneOf, type JsonSchemaTuple } from './index.js';
3
- import type { JsonSchema, JsonSchemaAny, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNull, JsonSchemaNumber, JsonSchemaObject, JsonSchemaRef, JsonSchemaString } from './jsonSchema.model.js';
2
+ import type { JsonSchema, JsonSchemaAllOf, JsonSchemaAny, JsonSchemaArray, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNull, JsonSchemaNumber, JsonSchemaObject, JsonSchemaOneOf, JsonSchemaRef, JsonSchemaString, JsonSchemaTuple } from './jsonSchema.model.js';
4
3
  export interface JsonSchemaBuilder<T = unknown> {
5
4
  build: () => JsonSchema<T>;
6
5
  }
@@ -1,7 +1,8 @@
1
1
  import { _uniq } from '../array/array.util.js';
2
- import { _deepCopy, _sortObject } from '../object/index.js';
3
- import { mergeJsonSchemaObjects, } from './index.js';
2
+ import { _deepCopy } from '../object/object.util.js';
3
+ import { _sortObject } from '../object/sortObject.js';
4
4
  import { JSON_SCHEMA_ORDER } from './jsonSchema.cnst.js';
5
+ import { mergeJsonSchemaObjects } from './jsonSchema.util.js';
5
6
  /**
6
7
  * Fluent (chainable) API to manually create Json Schemas.
7
8
  * Inspired by Joi
@@ -1,5 +1,5 @@
1
1
  import { _range } from '../array/range.js';
2
- import { _average, _percentile, _percentiles } from '../math/index.js';
2
+ import { _average, _percentile, _percentiles } from './math.util.js';
3
3
  /**
4
4
  * Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
5
5
  * Like an array of a fixed size. When it runs out of space - it starts writing on top of itself
@@ -1,5 +1,5 @@
1
- import { ErrorMode } from '../error/index.js';
2
- import type { CommonLogger } from '../log/index.js';
1
+ import { ErrorMode } from '../error/errorMode.js';
2
+ import type { CommonLogger } from '../log/commonLogger.js';
3
3
  import { type AbortableAsyncMapper } from '../types.js';
4
4
  export interface PMapOptions {
5
5
  /**
@@ -1,4 +1,4 @@
1
- import { ErrorMode } from '../error/index.js';
1
+ import { ErrorMode } from '../error/errorMode.js';
2
2
  import { END, SKIP } from '../types.js';
3
3
  /**
4
4
  * Forked from https://github.com/sindresorhus/p-map
@@ -1,5 +1,5 @@
1
- import { type ErrorData } from '../error/index.js';
2
- import type { CommonLogger } from '../log/index.js';
1
+ import type { ErrorData } from '../error/error.model.js';
2
+ import type { CommonLogger } from '../log/commonLogger.js';
3
3
  import type { AnyFunction } from '../types.js';
4
4
  export interface PRetryOptions {
5
5
  /**
@@ -1,5 +1,5 @@
1
- import { _since } from '../datetime/index.js';
2
- import { _errorDataAppend } from '../error/index.js';
1
+ import { _since } from '../datetime/time.util.js';
2
+ import { _errorDataAppend } from '../error/error.util.js';
3
3
  import { pDelay } from './pDelay.js';
4
4
  import { pTimeout } from './pTimeout.js';
5
5
  /**
@@ -0,0 +1,30 @@
1
+ import type { AppError } from '../error/error.util.js';
2
+ import type { ErrorDataTuple } from '../types.js';
3
+ /**
4
+ * Item to be validated.
5
+ * Can be null or undefined, which allows validation function to produce an error,
6
+ * if undefined/null are not accepted. But they might be accepted too, it depends
7
+ * on the schema (implementation detail of the ValidationFunction).
8
+ *
9
+ * Function returns a tuple of [err, item].
10
+ * In case of error it will be [err, null].
11
+ * In case of success it is [null, item].
12
+ *
13
+ * ValidationFunction may mutate the input item or not,
14
+ * depending on the implementation.
15
+ *
16
+ * @experimental
17
+ */
18
+ export type ValidationFunction<T, ERR extends AppError> = (item: T | null | undefined, opt?: ValidationFunctionOptions) => ErrorDataTuple<T, ERR>;
19
+ export interface ValidationFunctionOptions {
20
+ /**
21
+ * E.g User
22
+ * Used for error message printing.
23
+ */
24
+ itemName?: string;
25
+ /**
26
+ * E.g `12345678` (user id).
27
+ * Used for error message printing.
28
+ */
29
+ itemId?: string;
30
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.16.2",
4
+ "version": "15.17.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "zod": "^4"
@@ -18,19 +18,27 @@
18
18
  "./cfg/frontend/tsconfig.json": "./cfg/frontend/tsconfig.json",
19
19
  ".": "./dist/index.js",
20
20
  "./browser": "./dist/browser/index.js",
21
+ "./browser/*.js": "./dist/browser/*.js",
21
22
  "./datetime": "./dist/datetime/index.js",
23
+ "./datetime/*.js": "./dist/datetime/*.js",
22
24
  "./decorators": "./dist/decorators/index.js",
25
+ "./decorators/*.js": "./dist/decorators/*.js",
23
26
  "./deviceIdService": "./dist/deviceIdService.js",
24
27
  "./error": "./dist/error/index.js",
28
+ "./error/*.js": "./dist/error/*.js",
25
29
  "./http": "./dist/http/index.js",
26
30
  "./json-schema": "./dist/json-schema/index.js",
27
- "./log": "./dist/log/index.js",
31
+ "./log": "./dist/log/commonLogger.js",
28
32
  "./math": "./dist/math/index.js",
33
+ "./math/*.js": "./dist/math/*.js",
29
34
  "./object": "./dist/object/index.js",
35
+ "./object/*.js": "./dist/object/*.js",
30
36
  "./promise": "./dist/promise/index.js",
37
+ "./promise/*.js": "./dist/promise/*.js",
31
38
  "./nanoid": "./dist/nanoid.js",
32
39
  "./semver": "./dist/semver.js",
33
40
  "./string": "./dist/string/index.js",
41
+ "./string/*.js": "./dist/string/*.js",
34
42
  "./types": "./dist/types.js",
35
43
  "./zod": "./dist/zod/index.js"
36
44
  },
@@ -42,7 +50,6 @@
42
50
  "!src/**/*.test.*",
43
51
  "!src/**/__snapshots__"
44
52
  ],
45
- "main": "dist/index.js",
46
53
  "types": "dist/index.d.ts",
47
54
  "sideEffects": false,
48
55
  "engines": {
@@ -1,4 +1,4 @@
1
- import { isServerSide } from '../index.js'
1
+ import { isServerSide } from '../env.js'
2
2
  import { loadScript } from './script.util.js'
3
3
 
4
4
  declare global {
@@ -1,8 +1,8 @@
1
1
  import { _ms } from '../datetime/time.util.js'
2
- import { _assert } from '../error/index.js'
3
- import type { CommonLogger } from '../log/index.js'
4
- import { SimpleMovingAverage } from '../math/index.js'
5
- import { _stringify } from '../string/index.js'
2
+ import { _assert } from '../error/assert.js'
3
+ import type { CommonLogger } from '../log/commonLogger.js'
4
+ import { SimpleMovingAverage } from '../math/sma.js'
5
+ import { _stringify } from '../string/stringify.js'
6
6
  import { _getArgsSignature, _getMethodSignature } from './decorator.util.js'
7
7
 
8
8
  /**
@@ -1,5 +1,5 @@
1
- import type { PRetryOptions } from '../promise/index.js'
2
- import { pRetryFn } from '../promise/index.js'
1
+ import type { PRetryOptions } from '../promise/pRetry.js'
2
+ import { pRetryFn } from '../promise/pRetry.js'
3
3
 
4
4
  // eslint-disable-next-line @typescript-eslint/naming-convention
5
5
  export function _Retry(opt: PRetryOptions = {}): MethodDecorator {
@@ -1,5 +1,5 @@
1
- import { _since } from '../datetime/index.js'
2
- import type { CommonLogger } from '../log/index.js'
1
+ import { _since } from '../datetime/time.util.js'
2
+ import type { CommonLogger } from '../log/commonLogger.js'
3
3
  import type { AnyFunction, UnixTimestampMillis } from '../types.js'
4
4
  import { _anyToError } from './error.util.js'
5
5
 
package/src/index.ts CHANGED
@@ -14,4 +14,5 @@ export * from './number/createDeterministicRandom.js'
14
14
  export * from './number/number.util.js'
15
15
  export * from './polyfill.js'
16
16
  export * from './unit/size.util.js'
17
+ export type * from './validation/validation.js'
17
18
  export * from './web.js'
@@ -1,4 +1,4 @@
1
- import { _uniq } from '../index.js'
1
+ import { _uniq } from '../array/array.util.js'
2
2
  import { _filterNullishValues } from '../object/object.util.js'
3
3
  import type { AnyObject } from '../types.js'
4
4
  import type { JsonSchemaObject } from './jsonSchema.model.js'
@@ -1,26 +1,25 @@
1
1
  import { _uniq } from '../array/array.util.js'
2
- import { _deepCopy, _sortObject } from '../object/index.js'
2
+ import { _deepCopy } from '../object/object.util.js'
3
+ import { _sortObject } from '../object/sortObject.js'
3
4
  import type { AnyObject, BaseDBEntity } from '../types.js'
4
- import {
5
- type JsonSchemaAllOf,
6
- type JsonSchemaArray,
7
- type JsonSchemaOneOf,
8
- type JsonSchemaTuple,
9
- mergeJsonSchemaObjects,
10
- } from './index.js'
11
5
  import { JSON_SCHEMA_ORDER } from './jsonSchema.cnst.js'
12
6
  import type {
13
7
  JsonSchema,
8
+ JsonSchemaAllOf,
14
9
  JsonSchemaAny,
10
+ JsonSchemaArray,
15
11
  JsonSchemaBoolean,
16
12
  JsonSchemaConst,
17
13
  JsonSchemaEnum,
18
14
  JsonSchemaNull,
19
15
  JsonSchemaNumber,
20
16
  JsonSchemaObject,
17
+ JsonSchemaOneOf,
21
18
  JsonSchemaRef,
22
19
  JsonSchemaString,
20
+ JsonSchemaTuple,
23
21
  } from './jsonSchema.model.js'
22
+ import { mergeJsonSchemaObjects } from './jsonSchema.util.js'
24
23
 
25
24
  /* eslint-disable id-blacklist, @typescript-eslint/explicit-module-boundary-types */
26
25
 
@@ -1,5 +1,5 @@
1
1
  import { _range } from '../array/range.js'
2
- import { _average, _percentile, _percentiles } from '../math/index.js'
2
+ import { _average, _percentile, _percentiles } from './math.util.js'
3
3
 
4
4
  /**
5
5
  * Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
@@ -1,5 +1,5 @@
1
- import { ErrorMode } from '../error/index.js'
2
- import type { CommonLogger } from '../log/index.js'
1
+ import { ErrorMode } from '../error/errorMode.js'
2
+ import type { CommonLogger } from '../log/commonLogger.js'
3
3
  import { type AbortableAsyncMapper, END, SKIP } from '../types.js'
4
4
 
5
5
  export interface PMapOptions {
@@ -1,6 +1,7 @@
1
- import { _since } from '../datetime/index.js'
2
- import { _errorDataAppend, type ErrorData } from '../error/index.js'
3
- import type { CommonLogger } from '../log/index.js'
1
+ import { _since } from '../datetime/time.util.js'
2
+ import type { ErrorData } from '../error/error.model.js'
3
+ import { _errorDataAppend } from '../error/error.util.js'
4
+ import type { CommonLogger } from '../log/commonLogger.js'
4
5
  import type { AnyFunction, UnixTimestampMillis } from '../types.js'
5
6
  import { pDelay } from './pDelay.js'
6
7
  import { pTimeout } from './pTimeout.js'
@@ -0,0 +1,35 @@
1
+ import type { AppError } from '../error/error.util.js'
2
+ import type { ErrorDataTuple } from '../types.js'
3
+
4
+ /**
5
+ * Item to be validated.
6
+ * Can be null or undefined, which allows validation function to produce an error,
7
+ * if undefined/null are not accepted. But they might be accepted too, it depends
8
+ * on the schema (implementation detail of the ValidationFunction).
9
+ *
10
+ * Function returns a tuple of [err, item].
11
+ * In case of error it will be [err, null].
12
+ * In case of success it is [null, item].
13
+ *
14
+ * ValidationFunction may mutate the input item or not,
15
+ * depending on the implementation.
16
+ *
17
+ * @experimental
18
+ */
19
+ export type ValidationFunction<T, ERR extends AppError> = (
20
+ item: T | null | undefined,
21
+ opt?: ValidationFunctionOptions,
22
+ ) => ErrorDataTuple<T, ERR>
23
+
24
+ export interface ValidationFunctionOptions {
25
+ /**
26
+ * E.g User
27
+ * Used for error message printing.
28
+ */
29
+ itemName?: string
30
+ /**
31
+ * E.g `12345678` (user id).
32
+ * Used for error message printing.
33
+ */
34
+ itemId?: string
35
+ }
@@ -1 +0,0 @@
1
- export * from './commonLogger.js';
package/dist/log/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './commonLogger.js';
package/src/log/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './commonLogger.js'