@naturalcycles/js-lib 15.97.0 → 15.98.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 { Class, UnixTimestamp } from '../types.js';
1
+ import type { Class, NonEmptyArray, UnixTimestamp } from '../types.js';
2
2
  import type { BackendErrorResponseObject, ErrorData, ErrorObject } from './error.model.js';
3
3
  /**
4
4
  * Evaluates the `condition` (casts it to Boolean).
@@ -43,6 +43,12 @@ export declare function _assertIsErrorObject<DATA_TYPE extends ErrorData = Error
43
43
  export declare function _assertIsBackendErrorResponseObject<DATA_TYPE extends ErrorData = ErrorData>(obj: any): asserts obj is BackendErrorResponseObject<DATA_TYPE>;
44
44
  export declare function _assertIsString(v: any, message?: string): asserts v is string;
45
45
  export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
46
+ /**
47
+ * Asserts that the array has at least one element, narrowing it to NonEmptyArray.
48
+ * Readonly arrays are narrowed to `Readonly<NonEmptyArray<T>>`.
49
+ */
50
+ export declare function _assertIsNonEmptyArray<T>(arr: T[], message?: string): asserts arr is NonEmptyArray<T>;
51
+ export declare function _assertIsNonEmptyArray<T>(arr: readonly T[], message?: string): asserts arr is Readonly<NonEmptyArray<T>>;
46
52
  export declare function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T;
47
53
  /**
48
54
  * Casts an arbitrary number as UnixTimestamp.
@@ -91,6 +91,11 @@ export function _assertIsString(v, message) {
91
91
  export function _assertIsNumber(v, message) {
92
92
  _assertTypeOf(v, 'number', message);
93
93
  }
94
+ export function _assertIsNonEmptyArray(arr, message) {
95
+ if (!arr.length) {
96
+ throw new AssertionError(message || 'Expected non-empty array');
97
+ }
98
+ }
94
99
  export function _assertTypeOf(v, expectedType, message) {
95
100
  if (typeof v !== expectedType) {
96
101
  const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.97.0",
4
+ "version": "15.98.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -14,7 +14,7 @@
14
14
  }
15
15
  },
16
16
  "devDependencies": {
17
- "@naturalcycles/dev-lib": "20.68.0",
17
+ "@naturalcycles/dev-lib": "0.0.0",
18
18
  "@types/node": "^26",
19
19
  "@types/semver": "^7",
20
20
  "typescript": "^7",
@@ -1,6 +1,6 @@
1
1
  import { _deepEquals } from '../object/deepEquals.js'
2
2
  import { _stringify } from '../string/stringify.js'
3
- import type { Class, UnixTimestamp } from '../types.js'
3
+ import type { Class, NonEmptyArray, UnixTimestamp } from '../types.js'
4
4
  import type { BackendErrorResponseObject, ErrorData, ErrorObject } from './error.model.js'
5
5
  import { _isBackendErrorResponseObject, _isErrorObject, AssertionError } from './error.util.js'
6
6
 
@@ -134,6 +134,24 @@ export function _assertIsNumber(v: any, message?: string): asserts v is number {
134
134
  _assertTypeOf<number>(v, 'number', message)
135
135
  }
136
136
 
137
+ /**
138
+ * Asserts that the array has at least one element, narrowing it to NonEmptyArray.
139
+ * Readonly arrays are narrowed to `Readonly<NonEmptyArray<T>>`.
140
+ */
141
+ export function _assertIsNonEmptyArray<T>(
142
+ arr: T[],
143
+ message?: string,
144
+ ): asserts arr is NonEmptyArray<T>
145
+ export function _assertIsNonEmptyArray<T>(
146
+ arr: readonly T[],
147
+ message?: string,
148
+ ): asserts arr is Readonly<NonEmptyArray<T>>
149
+ export function _assertIsNonEmptyArray(arr: readonly unknown[], message?: string): void {
150
+ if (!arr.length) {
151
+ throw new AssertionError(message || 'Expected non-empty array')
152
+ }
153
+ }
154
+
137
155
  export function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T {
138
156
  if (typeof v !== expectedType) {
139
157
  const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`