@naturalcycles/js-lib 14.246.1 → 14.246.2

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.
@@ -88,7 +88,7 @@ async function pMap(iterable, mapper, opt = {}) {
88
88
  ret[i] = value;
89
89
  resolvingCount--;
90
90
  next();
91
- }, err => {
91
+ }, (err) => {
92
92
  if (errorMode === __1.ErrorMode.THROW_IMMEDIATELY) {
93
93
  isSettled = true;
94
94
  reject(err);
@@ -66,7 +66,7 @@ class PQueue {
66
66
  if (!resolveOnStart)
67
67
  fn.defer.resolve(result);
68
68
  })
69
- .catch(err => {
69
+ .catch((err) => {
70
70
  this.cfg.logger.error(err);
71
71
  if (resolveOnStart)
72
72
  return;
@@ -1,6 +1,6 @@
1
1
  import type { ErrorData } from '../error/error.model';
2
2
  import { TimeoutError } from '../error/error.util';
3
- import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types';
3
+ import { AnyAsyncFunction, NumberOfMilliseconds } from '../types';
4
4
  export interface PTimeoutOptions {
5
5
  /**
6
6
  * Timeout in milliseconds.
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pTimeoutFn = pTimeoutFn;
4
4
  exports.pTimeout = pTimeout;
5
5
  const error_util_1 = require("../error/error.util");
6
+ const types_1 = require("../types");
6
7
  /**
7
8
  * Decorates a Function with a timeout.
8
9
  * Returns a decorated Function.
@@ -44,6 +45,7 @@ async function pTimeout(fn, opt) {
44
45
  resolve(onTimeout(err));
45
46
  }
46
47
  catch (err) {
48
+ (0, types_1._typeCast)(err);
47
49
  // keep original stack
48
50
  err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
49
51
  reject((0, error_util_1._errorDataAppend)(err, opt.errorData));
package/dist/semver.d.ts CHANGED
@@ -34,13 +34,13 @@ export declare class Semver {
34
34
  * returns 0 if they are equal
35
35
  * returns -1 if this < other
36
36
  */
37
- cmp(other: SemverInput): -1 | 0 | 1;
37
+ compare(other: SemverInput): -1 | 0 | 1;
38
38
  toJSON: () => string;
39
39
  toString(): string;
40
40
  }
41
41
  declare class SemverFactory {
42
- of(input: SemverInput): Semver;
43
- parseOrNull(input: SemverInputNullable): Semver | null;
42
+ fromInput(input: SemverInput): Semver;
43
+ fromInputOrUndefined(input: SemverInputNullable): Semver | undefined;
44
44
  /**
45
45
  * Returns the highest (max) Semver from the array, or undefined if the array is empty.
46
46
  */
package/dist/semver.js CHANGED
@@ -24,11 +24,11 @@ const is_util_1 = require("./is.util");
24
24
  class Semver {
25
25
  constructor(tokens) {
26
26
  this.tokens = tokens;
27
- this.isAfter = (other) => this.cmp(other) > 0;
28
- this.isSameOrAfter = (other) => this.cmp(other) >= 0;
29
- this.isBefore = (other) => this.cmp(other) < 0;
30
- this.isSameOrBefore = (other) => this.cmp(other) <= 0;
31
- this.isSame = (other) => this.cmp(other) === 0;
27
+ this.isAfter = (other) => this.compare(other) > 0;
28
+ this.isSameOrAfter = (other) => this.compare(other) >= 0;
29
+ this.isBefore = (other) => this.compare(other) < 0;
30
+ this.isSameOrBefore = (other) => this.compare(other) <= 0;
31
+ this.isSame = (other) => this.compare(other) === 0;
32
32
  this.toJSON = () => this.toString();
33
33
  }
34
34
  get major() {
@@ -45,8 +45,8 @@ class Semver {
45
45
  * returns 0 if they are equal
46
46
  * returns -1 if this < other
47
47
  */
48
- cmp(other) {
49
- const { tokens } = exports.semver2.of(other);
48
+ compare(other) {
49
+ const { tokens } = exports.semver2.fromInput(other);
50
50
  for (let i = 0; i < 3; i++) {
51
51
  if (this.tokens[i] < tokens[i])
52
52
  return -1;
@@ -61,16 +61,16 @@ class Semver {
61
61
  }
62
62
  exports.Semver = Semver;
63
63
  class SemverFactory {
64
- of(input) {
65
- const s = this.parseOrNull(input);
66
- (0, assert_1._assert)(s !== null, `Cannot parse "${input}" into Semver`, {
64
+ fromInput(input) {
65
+ const s = this.fromInputOrUndefined(input);
66
+ (0, assert_1._assert)(s, `Cannot parse "${input}" into Semver`, {
67
67
  input,
68
68
  });
69
69
  return s;
70
70
  }
71
- parseOrNull(input) {
71
+ fromInputOrUndefined(input) {
72
72
  if (!input)
73
- return null;
73
+ return;
74
74
  if (input instanceof Semver)
75
75
  return input;
76
76
  const t = input.split('.');
@@ -89,7 +89,9 @@ class SemverFactory {
89
89
  max(items) {
90
90
  const items2 = items.filter(is_util_1._isTruthy);
91
91
  (0, assert_1._assert)(items2.length, 'semver.max called on empty array');
92
- return items2.map(i => this.of(i)).reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
92
+ return items2
93
+ .map(i => this.fromInput(i))
94
+ .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
93
95
  }
94
96
  /**
95
97
  * Returns the lowest (min) Semver from the array, or undefined if the array is empty.
@@ -105,7 +107,7 @@ class SemverFactory {
105
107
  const items2 = items.filter(is_util_1._isTruthy);
106
108
  (0, assert_1._assert)(items2.length, 'semver.min called on empty array');
107
109
  return items2
108
- .map(i => this.of(i))
110
+ .map(i => this.fromInput(i))
109
111
  .reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
110
112
  }
111
113
  /**
@@ -113,11 +115,11 @@ class SemverFactory {
113
115
  */
114
116
  sort(items, dir = 'asc', mutate = false) {
115
117
  const mod = dir === 'desc' ? -1 : 1;
116
- return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
118
+ return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
117
119
  }
118
120
  }
119
121
  const semverFactory = new SemverFactory();
120
- exports.semver2 = semverFactory.of.bind(semverFactory);
122
+ exports.semver2 = semverFactory.fromInput.bind(semverFactory);
121
123
  // The line below is the blackest of black magic I have ever written in 2024.
122
124
  // And probably 2023 as well.
123
125
  Object.setPrototypeOf(exports.semver2, semverFactory);
@@ -20,7 +20,6 @@ class MissingValueError extends Error {
20
20
  super(`Missing a value for ${key ? `the placeholder: ${key}` : 'a placeholder'}`);
21
21
  this.key = key;
22
22
  this.name = 'MissingValueError';
23
- this.key = key;
24
23
  }
25
24
  }
26
25
  exports.MissingValueError = MissingValueError;
@@ -85,7 +85,7 @@ export async function pMap(iterable, mapper, opt = {}) {
85
85
  ret[i] = value;
86
86
  resolvingCount--;
87
87
  next();
88
- }, err => {
88
+ }, (err) => {
89
89
  if (errorMode === ErrorMode.THROW_IMMEDIATELY) {
90
90
  isSettled = true;
91
91
  reject(err);
@@ -63,7 +63,7 @@ export class PQueue {
63
63
  if (!resolveOnStart)
64
64
  fn.defer.resolve(result);
65
65
  })
66
- .catch(err => {
66
+ .catch((err) => {
67
67
  this.cfg.logger.error(err);
68
68
  if (resolveOnStart)
69
69
  return;
@@ -1,4 +1,5 @@
1
1
  import { _errorDataAppend, TimeoutError } from '../error/error.util';
2
+ import { _typeCast } from '../types';
2
3
  /**
3
4
  * Decorates a Function with a timeout.
4
5
  * Returns a decorated Function.
@@ -40,6 +41,7 @@ export async function pTimeout(fn, opt) {
40
41
  resolve(onTimeout(err));
41
42
  }
42
43
  catch (err) {
44
+ _typeCast(err);
43
45
  // keep original stack
44
46
  err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
45
47
  reject(_errorDataAppend(err, opt.errorData));
@@ -20,11 +20,11 @@ import { _isTruthy } from './is.util';
20
20
  export class Semver {
21
21
  constructor(tokens) {
22
22
  this.tokens = tokens;
23
- this.isAfter = (other) => this.cmp(other) > 0;
24
- this.isSameOrAfter = (other) => this.cmp(other) >= 0;
25
- this.isBefore = (other) => this.cmp(other) < 0;
26
- this.isSameOrBefore = (other) => this.cmp(other) <= 0;
27
- this.isSame = (other) => this.cmp(other) === 0;
23
+ this.isAfter = (other) => this.compare(other) > 0;
24
+ this.isSameOrAfter = (other) => this.compare(other) >= 0;
25
+ this.isBefore = (other) => this.compare(other) < 0;
26
+ this.isSameOrBefore = (other) => this.compare(other) <= 0;
27
+ this.isSame = (other) => this.compare(other) === 0;
28
28
  this.toJSON = () => this.toString();
29
29
  }
30
30
  get major() {
@@ -41,8 +41,8 @@ export class Semver {
41
41
  * returns 0 if they are equal
42
42
  * returns -1 if this < other
43
43
  */
44
- cmp(other) {
45
- const { tokens } = semver2.of(other);
44
+ compare(other) {
45
+ const { tokens } = semver2.fromInput(other);
46
46
  for (let i = 0; i < 3; i++) {
47
47
  if (this.tokens[i] < tokens[i])
48
48
  return -1;
@@ -56,16 +56,16 @@ export class Semver {
56
56
  }
57
57
  }
58
58
  class SemverFactory {
59
- of(input) {
60
- const s = this.parseOrNull(input);
61
- _assert(s !== null, `Cannot parse "${input}" into Semver`, {
59
+ fromInput(input) {
60
+ const s = this.fromInputOrUndefined(input);
61
+ _assert(s, `Cannot parse "${input}" into Semver`, {
62
62
  input,
63
63
  });
64
64
  return s;
65
65
  }
66
- parseOrNull(input) {
66
+ fromInputOrUndefined(input) {
67
67
  if (!input)
68
- return null;
68
+ return;
69
69
  if (input instanceof Semver)
70
70
  return input;
71
71
  const t = input.split('.');
@@ -84,7 +84,9 @@ class SemverFactory {
84
84
  max(items) {
85
85
  const items2 = items.filter(_isTruthy);
86
86
  _assert(items2.length, 'semver.max called on empty array');
87
- return items2.map(i => this.of(i)).reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
87
+ return items2
88
+ .map(i => this.fromInput(i))
89
+ .reduce((max, item) => (max.isSameOrAfter(item) ? max : item));
88
90
  }
89
91
  /**
90
92
  * Returns the lowest (min) Semver from the array, or undefined if the array is empty.
@@ -100,7 +102,7 @@ class SemverFactory {
100
102
  const items2 = items.filter(_isTruthy);
101
103
  _assert(items2.length, 'semver.min called on empty array');
102
104
  return items2
103
- .map(i => this.of(i))
105
+ .map(i => this.fromInput(i))
104
106
  .reduce((min, item) => (min.isSameOrBefore(item) ? min : item));
105
107
  }
106
108
  /**
@@ -108,11 +110,11 @@ class SemverFactory {
108
110
  */
109
111
  sort(items, dir = 'asc', mutate = false) {
110
112
  const mod = dir === 'desc' ? -1 : 1;
111
- return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
113
+ return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
112
114
  }
113
115
  }
114
116
  const semverFactory = new SemverFactory();
115
- export const semver2 = semverFactory.of.bind(semverFactory);
117
+ export const semver2 = semverFactory.fromInput.bind(semverFactory);
116
118
  // The line below is the blackest of black magic I have ever written in 2024.
117
119
  // And probably 2023 as well.
118
120
  Object.setPrototypeOf(semver2, semverFactory);
@@ -16,7 +16,6 @@ export class MissingValueError extends Error {
16
16
  super(`Missing a value for ${key ? `the placeholder: ${key}` : 'a placeholder'}`);
17
17
  this.key = key;
18
18
  this.name = 'MissingValueError';
19
- this.key = key;
20
19
  }
21
20
  }
22
21
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.246.1",
3
+ "version": "14.246.2",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "devDependencies": {
19
19
  "@naturalcycles/bench-lib": "^3.0.0",
20
- "@naturalcycles/dev-lib": "^13.0.1",
20
+ "@naturalcycles/dev-lib": "^14.0.0",
21
21
  "@naturalcycles/nodejs-lib": "^13.0.1",
22
22
  "@naturalcycles/time-lib": "^3.5.1",
23
23
  "@types/crypto-js": "^4.1.1",
@@ -127,7 +127,7 @@ export async function pMap<IN, OUT>(
127
127
  resolvingCount--
128
128
  next()
129
129
  },
130
- err => {
130
+ (err: Error) => {
131
131
  if (errorMode === ErrorMode.THROW_IMMEDIATELY) {
132
132
  isSettled = true
133
133
  reject(err)
@@ -124,7 +124,7 @@ export class PQueue {
124
124
  .then(result => {
125
125
  if (!resolveOnStart) fn.defer.resolve(result)
126
126
  })
127
- .catch(err => {
127
+ .catch((err: Error) => {
128
128
  this.cfg.logger.error(err)
129
129
  if (resolveOnStart) return
130
130
 
@@ -1,6 +1,6 @@
1
1
  import type { ErrorData } from '../error/error.model'
2
2
  import { _errorDataAppend, TimeoutError } from '../error/error.util'
3
- import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types'
3
+ import { _typeCast, AnyAsyncFunction, NumberOfMilliseconds } from '../types'
4
4
 
5
5
  export interface PTimeoutOptions {
6
6
  /**
@@ -84,6 +84,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
84
84
  try {
85
85
  resolve(onTimeout(err))
86
86
  } catch (err: any) {
87
+ _typeCast<Error>(err)
87
88
  // keep original stack
88
89
  err.stack = fakeError.stack!.replace('Error: TimeoutError', err.name + ': ' + err.message)
89
90
  reject(_errorDataAppend(err, opt.errorData))
@@ -98,7 +99,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
98
99
  try {
99
100
  resolve(await fn())
100
101
  } catch (err) {
101
- reject(err)
102
+ reject(err as Error)
102
103
  } finally {
103
104
  clearTimeout(timer)
104
105
  }
package/src/semver.ts CHANGED
@@ -36,19 +36,19 @@ export class Semver {
36
36
  return this.tokens[2]
37
37
  }
38
38
 
39
- isAfter = (other: SemverInput): boolean => this.cmp(other) > 0
40
- isSameOrAfter = (other: SemverInput): boolean => this.cmp(other) >= 0
41
- isBefore = (other: SemverInput): boolean => this.cmp(other) < 0
42
- isSameOrBefore = (other: SemverInput): boolean => this.cmp(other) <= 0
43
- isSame = (other: SemverInput): boolean => this.cmp(other) === 0
39
+ isAfter = (other: SemverInput): boolean => this.compare(other) > 0
40
+ isSameOrAfter = (other: SemverInput): boolean => this.compare(other) >= 0
41
+ isBefore = (other: SemverInput): boolean => this.compare(other) < 0
42
+ isSameOrBefore = (other: SemverInput): boolean => this.compare(other) <= 0
43
+ isSame = (other: SemverInput): boolean => this.compare(other) === 0
44
44
 
45
45
  /**
46
46
  * Returns 1 if this > other
47
47
  * returns 0 if they are equal
48
48
  * returns -1 if this < other
49
49
  */
50
- cmp(other: SemverInput): -1 | 0 | 1 {
51
- const { tokens } = semver2.of(other)
50
+ compare(other: SemverInput): -1 | 0 | 1 {
51
+ const { tokens } = semver2.fromInput(other)
52
52
  for (let i = 0; i < 3; i++) {
53
53
  if (this.tokens[i]! < tokens[i]!) return -1
54
54
  if (this.tokens[i]! > tokens[i]!) return 1
@@ -64,18 +64,18 @@ export class Semver {
64
64
  }
65
65
 
66
66
  class SemverFactory {
67
- of(input: SemverInput): Semver {
68
- const s = this.parseOrNull(input)
67
+ fromInput(input: SemverInput): Semver {
68
+ const s = this.fromInputOrUndefined(input)
69
69
 
70
- _assert(s !== null, `Cannot parse "${input}" into Semver`, {
70
+ _assert(s, `Cannot parse "${input}" into Semver`, {
71
71
  input,
72
72
  })
73
73
 
74
74
  return s
75
75
  }
76
76
 
77
- parseOrNull(input: SemverInputNullable): Semver | null {
78
- if (!input) return null
77
+ fromInputOrUndefined(input: SemverInputNullable): Semver | undefined {
78
+ if (!input) return
79
79
  if (input instanceof Semver) return input
80
80
 
81
81
  const t = input.split('.')
@@ -96,7 +96,9 @@ class SemverFactory {
96
96
  max(items: SemverInputNullable[]): Semver {
97
97
  const items2 = items.filter(_isTruthy)
98
98
  _assert(items2.length, 'semver.max called on empty array')
99
- return items2.map(i => this.of(i)).reduce((max, item) => (max.isSameOrAfter(item) ? max : item))
99
+ return items2
100
+ .map(i => this.fromInput(i))
101
+ .reduce((max, item) => (max.isSameOrAfter(item) ? max : item))
100
102
  }
101
103
 
102
104
  /**
@@ -114,7 +116,7 @@ class SemverFactory {
114
116
  const items2 = items.filter(_isTruthy)
115
117
  _assert(items2.length, 'semver.min called on empty array')
116
118
  return items2
117
- .map(i => this.of(i))
119
+ .map(i => this.fromInput(i))
118
120
  .reduce((min, item) => (min.isSameOrBefore(item) ? min : item))
119
121
  }
120
122
 
@@ -123,7 +125,7 @@ class SemverFactory {
123
125
  */
124
126
  sort(items: Semver[], dir: SortDirection = 'asc', mutate = false): Semver[] {
125
127
  const mod = dir === 'desc' ? -1 : 1
126
- return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod)
128
+ return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod)
127
129
  }
128
130
  }
129
131
 
@@ -133,7 +135,7 @@ interface SemverFn extends SemverFactory {
133
135
 
134
136
  const semverFactory = new SemverFactory()
135
137
 
136
- export const semver2 = semverFactory.of.bind(semverFactory) as SemverFn
138
+ export const semver2 = semverFactory.fromInput.bind(semverFactory) as SemverFn
137
139
 
138
140
  // The line below is the blackest of black magic I have ever written in 2024.
139
141
  // And probably 2023 as well.
@@ -18,7 +18,6 @@ export class MissingValueError extends Error {
18
18
  constructor(public key: any) {
19
19
  super(`Missing a value for ${key ? `the placeholder: ${key}` : 'a placeholder'}`)
20
20
  this.name = 'MissingValueError'
21
- this.key = key
22
21
  }
23
22
  }
24
23