@naturalcycles/js-lib 15.99.0 → 15.100.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.
@@ -369,8 +369,7 @@ export function _shuffle(array, opt = {}) {
369
369
  return a;
370
370
  }
371
371
  export function _firstLast(array) {
372
- if (!array.length)
373
- throw new Error('_firstLast called on empty array');
372
+ _assert(array.length, '_firstLast called on empty array');
374
373
  return [array[0], array[array.length - 1]];
375
374
  }
376
375
  export function _firstLastOrUndefined(array) {
@@ -383,8 +382,7 @@ export function _firstLastOrUndefined(array) {
383
382
  * Throws if array is empty.
384
383
  */
385
384
  export function _last(array) {
386
- if (!array.length)
387
- throw new Error('_last called on empty array');
385
+ _assert(array.length, '_last called on empty array');
388
386
  return array[array.length - 1];
389
387
  }
390
388
  /**
@@ -398,8 +396,7 @@ export function _lastOrUndefined(array) {
398
396
  * Throws if array is empty.
399
397
  */
400
398
  export function _first(array) {
401
- if (!array.length)
402
- throw new Error('_first called on empty array');
399
+ _assert(array.length, '_first called on empty array');
403
400
  return array[0];
404
401
  }
405
402
  /**
@@ -482,8 +479,7 @@ export function _minBy(array, mapper) {
482
479
  return min;
483
480
  }
484
481
  export function _minMax(array) {
485
- if (!array.length)
486
- throw new Error('_minMax called on empty array');
482
+ _assert(array.length, '_minMax called on empty array');
487
483
  const result = _minMaxOrUndefined(array);
488
484
  _assert(result !== undefined, '_minBy returned undefined');
489
485
  return result;
@@ -510,8 +506,7 @@ export function _minMaxOrUndefined(array) {
510
506
  return [min, max];
511
507
  }
512
508
  export function _minMaxBy(array, mapper) {
513
- if (!array.length)
514
- throw new Error('_minMaxBy called on empty array');
509
+ _assert(array.length, '_minMaxBy called on empty array');
515
510
  const result = _minMaxByOrUndefined(array, mapper);
516
511
  _assert(result !== undefined, '_minMaxBy returned undefined');
517
512
  return result;
@@ -1,3 +1,4 @@
1
+ import { _assert } from '../error/assert.js';
1
2
  import { _shuffle } from './array.util.js';
2
3
  /**
3
4
  * Better Array.
@@ -16,8 +17,7 @@ export class Array2 extends Array {
16
17
  return this[0];
17
18
  }
18
19
  first() {
19
- if (!this.length)
20
- throw new Error('Array.first called on empty array');
20
+ _assert(this.length, 'Array.first called on empty array');
21
21
  return this[0];
22
22
  }
23
23
  lastOrUndefined() {
@@ -25,8 +25,7 @@ export class Array2 extends Array {
25
25
  }
26
26
  last() {
27
27
  const { length } = this;
28
- if (!length)
29
- throw new Error('Array.last called on empty array');
28
+ _assert(length, 'Array.last called on empty array');
30
29
  return this[length - 1];
31
30
  }
32
31
  uniq() {
@@ -1,3 +1,5 @@
1
+ // `_assert` and friends are implemented by throwing in an if
2
+ // oxlint-disable nc/prefer-assert
1
3
  import { _deepEquals } from '../object/deepEquals.js';
2
4
  import { _stringify } from '../string/stringify.js';
3
5
  import { _isBackendErrorResponseObject, _isErrorObject, AssertionError } from './error.util.js';
@@ -4,7 +4,7 @@
4
4
  import { abortSignalAnyOrUndefined, abortSignalTimeoutOrUndefined } from '../abort.js';
5
5
  import { _ms, _since } from '../datetime/time.util.js';
6
6
  import { isServerSide } from '../env.js';
7
- import { _assertErrorClassOrRethrow, _assertIsError } from '../error/assert.js';
7
+ import { _assert, _assertErrorClassOrRethrow, _assertIsError } from '../error/assert.js';
8
8
  import { _anyToError, _anyToErrorObject, _errorDataAppend, _errorLikeToErrorObject, HttpRequestError, UnexpectedPassError, } from '../error/error.util.js';
9
9
  import { consoleLogger } from '../log/commonLogger.js';
10
10
  import { _clamp } from '../number/number.util.js';
@@ -454,10 +454,8 @@ export class Fetcher {
454
454
  }
455
455
  else if (responseType === 'readableStream') {
456
456
  res.body = res.fetchResponse.body;
457
- if (res.body === null) {
458
- // Error is to be handled upstream
459
- throw new Error('fetchResponse.body is null');
460
- }
457
+ // Error is to be handled upstream
458
+ _assert(res.body !== null, 'fetchResponse.body is null');
461
459
  }
462
460
  res.retryStatus.retryStopped = true;
463
461
  if (req.logResponse) {
@@ -1,3 +1,4 @@
1
+ import { _assert } from '../error/assert.js';
1
2
  /**
2
3
  * Like Set, but serializes to JSON as an array.
3
4
  *
@@ -20,8 +21,7 @@ export class Set2 extends Set {
20
21
  return this;
21
22
  }
22
23
  first() {
23
- if (!this.size)
24
- throw new Error('Set.first called on empty set');
24
+ _assert(this.size, 'Set.first called on empty set');
25
25
  return this.firstOrUndefined();
26
26
  }
27
27
  firstOrUndefined() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.99.0",
4
+ "version": "15.100.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": "0.0.0",
17
+ "@naturalcycles/dev-lib": "20.70.0",
18
18
  "@types/node": "^26",
19
19
  "@types/semver": "^7",
20
20
  "typescript": "^7",
@@ -435,7 +435,7 @@ export function _shuffle<T>(array: T[], opt: MutateOptions = {}): T[] {
435
435
  }
436
436
 
437
437
  export function _firstLast<T>(array: readonly T[]): [first: T, last: T] {
438
- if (!array.length) throw new Error('_firstLast called on empty array')
438
+ _assert(array.length, '_firstLast called on empty array')
439
439
  return [array[0]!, array[array.length - 1]!]
440
440
  }
441
441
 
@@ -449,7 +449,7 @@ export function _firstLastOrUndefined<T>(array: readonly T[]): [first: T, last:
449
449
  * Throws if array is empty.
450
450
  */
451
451
  export function _last<T>(array: readonly T[]): T {
452
- if (!array.length) throw new Error('_last called on empty array')
452
+ _assert(array.length, '_last called on empty array')
453
453
  return array[array.length - 1]!
454
454
  }
455
455
 
@@ -465,7 +465,7 @@ export function _lastOrUndefined<T>(array: readonly T[]): T | undefined {
465
465
  * Throws if array is empty.
466
466
  */
467
467
  export function _first<T>(array: readonly T[]): T {
468
- if (!array.length) throw new Error('_first called on empty array')
468
+ _assert(array.length, '_first called on empty array')
469
469
  return array[0]!
470
470
  }
471
471
 
@@ -561,7 +561,7 @@ export function _minBy<T>(array: readonly T[], mapper: Mapper<T, number | string
561
561
  }
562
562
 
563
563
  export function _minMax<T>(array: readonly T[]): [min: NonNullable<T>, max: NonNullable<T>] {
564
- if (!array.length) throw new Error('_minMax called on empty array')
564
+ _assert(array.length, '_minMax called on empty array')
565
565
  const result = _minMaxOrUndefined(array)
566
566
  _assert(result !== undefined, '_minBy returned undefined')
567
567
  return result
@@ -592,7 +592,7 @@ export function _minMaxBy<T>(
592
592
  array: readonly T[],
593
593
  mapper: Mapper<T, number | string | undefined>,
594
594
  ): [min: NonNullable<T>, max: NonNullable<T>] {
595
- if (!array.length) throw new Error('_minMaxBy called on empty array')
595
+ _assert(array.length, '_minMaxBy called on empty array')
596
596
  const result = _minMaxByOrUndefined(array, mapper)
597
597
  _assert(result !== undefined, '_minMaxBy returned undefined')
598
598
  return result
@@ -1,3 +1,4 @@
1
+ import { _assert } from '../error/assert.js'
1
2
  import { _shuffle } from './array.util.js'
2
3
 
3
4
  /**
@@ -20,7 +21,7 @@ export class Array2<T> extends Array<T> {
20
21
  }
21
22
 
22
23
  first(): T {
23
- if (!this.length) throw new Error('Array.first called on empty array')
24
+ _assert(this.length, 'Array.first called on empty array')
24
25
  return this[0]!
25
26
  }
26
27
 
@@ -30,7 +31,7 @@ export class Array2<T> extends Array<T> {
30
31
 
31
32
  last(): T {
32
33
  const { length } = this
33
- if (!length) throw new Error('Array.last called on empty array')
34
+ _assert(length, 'Array.last called on empty array')
34
35
  return this[length - 1]!
35
36
  }
36
37
 
@@ -1,3 +1,5 @@
1
+ // `_assert` and friends are implemented by throwing in an if
2
+ // oxlint-disable nc/prefer-assert
1
3
  import { _deepEquals } from '../object/deepEquals.js'
2
4
  import { _stringify } from '../string/stringify.js'
3
5
  import type { Class, NonEmptyArray, UnixTimestamp } from '../types.js'
@@ -6,7 +6,7 @@ import type { ReadableStream as WebReadableStream } from 'node:stream/web'
6
6
  import { abortSignalAnyOrUndefined, abortSignalTimeoutOrUndefined } from '../abort.js'
7
7
  import { _ms, _since } from '../datetime/time.util.js'
8
8
  import { isServerSide } from '../env.js'
9
- import { _assertErrorClassOrRethrow, _assertIsError } from '../error/assert.js'
9
+ import { _assert, _assertErrorClassOrRethrow, _assertIsError } from '../error/assert.js'
10
10
  import type { ErrorLike, ErrorObject } from '../error/error.model.js'
11
11
  import {
12
12
  _anyToError,
@@ -560,10 +560,8 @@ export class Fetcher {
560
560
  } else if (responseType === 'readableStream') {
561
561
  res.body = res.fetchResponse.body
562
562
 
563
- if (res.body === null) {
564
- // Error is to be handled upstream
565
- throw new Error('fetchResponse.body is null')
566
- }
563
+ // Error is to be handled upstream
564
+ _assert(res.body !== null, 'fetchResponse.body is null')
567
565
  }
568
566
 
569
567
  res.retryStatus.retryStopped = true
@@ -1,3 +1,5 @@
1
+ import { _assert } from '../error/assert.js'
2
+
1
3
  /**
2
4
  * Like Set, but serializes to JSON as an array.
3
5
  *
@@ -22,7 +24,7 @@ export class Set2<T = any> extends Set<T> {
22
24
  }
23
25
 
24
26
  first(): T {
25
- if (!this.size) throw new Error('Set.first called on empty set')
27
+ _assert(this.size, 'Set.first called on empty set')
26
28
  return this.firstOrUndefined()!
27
29
  }
28
30