@naturalcycles/js-lib 14.210.0 → 14.212.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.
@@ -176,6 +176,11 @@ export declare function _last<T>(array: T[]): T;
176
176
  * Returns last item of the array (or undefined if array is empty).
177
177
  */
178
178
  export declare function _lastOrUndefined<T>(array: T[]): T | undefined;
179
+ /**
180
+ * Returns the first item of non-empty array.
181
+ * Throws if array is empty.
182
+ */
183
+ export declare function _first<T>(array: T[]): T;
179
184
  export declare function _minOrUndefined<T>(array: T[]): NonNullable<T> | undefined;
180
185
  /**
181
186
  * Filters out nullish values (undefined and null).
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._zip = exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersectsWith = exports._intersection = exports._countBy = exports._count = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortDescBy = exports._sortBy = exports._groupBy = exports._mapBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
3
+ exports._zip = exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._first = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersectsWith = exports._intersection = exports._countBy = exports._count = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortDescBy = exports._sortBy = exports._groupBy = exports._mapBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
4
4
  const is_util_1 = require("../is.util");
5
5
  const types_1 = require("../types");
6
6
  /**
@@ -333,6 +333,16 @@ function _lastOrUndefined(array) {
333
333
  return array[array.length - 1];
334
334
  }
335
335
  exports._lastOrUndefined = _lastOrUndefined;
336
+ /**
337
+ * Returns the first item of non-empty array.
338
+ * Throws if array is empty.
339
+ */
340
+ function _first(array) {
341
+ if (!array.length)
342
+ throw new Error('_first called on empty array');
343
+ return array[0];
344
+ }
345
+ exports._first = _first;
336
346
  function _minOrUndefined(array) {
337
347
  const a = array.filter(is_util_1._isNotNullish);
338
348
  if (!a.length)
@@ -26,7 +26,7 @@ const acceptByResponseType = {
26
26
  const defRetryOptions = {
27
27
  count: 2,
28
28
  timeout: 1000,
29
- timeoutMax: 30000,
29
+ timeoutMax: 30_000,
30
30
  timeoutMultiplier: 2,
31
31
  };
32
32
  /**
@@ -24,7 +24,7 @@ export declare const jsonSchema: {
24
24
  dateString(): JsonSchemaStringBuilder;
25
25
  object<T_4 extends AnyObject>(props: { [k in keyof T_4]: JsonSchemaAnyBuilder<T_4[k], JsonSchema<T_4[k]>>; }): JsonSchemaObjectBuilder<T_4>;
26
26
  rootObject<T_5 extends AnyObject>(props: { [k_1 in keyof T_5]: JsonSchemaAnyBuilder<T_5[k_1], JsonSchema<T_5[k_1]>>; }): JsonSchemaObjectBuilder<T_5>;
27
- array<ITEM = unknown>(itemSchema: JsonSchemaAnyBuilder<ITEM, JsonSchema<ITEM>>): JsonSchemaArrayBuilder<ITEM>;
27
+ array<ITEM = unknown>(itemSchema: JsonSchemaAnyBuilder<ITEM>): JsonSchemaArrayBuilder<ITEM>;
28
28
  tuple<T_6 extends any[] = unknown[]>(items: JsonSchemaAnyBuilder[]): JsonSchemaTupleBuilder<T_6>;
29
29
  oneOf<T_7 = unknown>(items: JsonSchemaAnyBuilder[]): JsonSchemaAnyBuilder<T_7, JsonSchemaOneOf<T_7>>;
30
30
  allOf<T_8 = unknown>(items: JsonSchemaAnyBuilder[]): JsonSchemaAnyBuilder<T_8, JsonSchemaAllOf<T_8>>;
@@ -329,7 +329,6 @@ function _set(obj, path, value) {
329
329
  a[c]
330
330
  : // No: create the key. Is the next key a potential array-index?
331
331
  (a[c] =
332
- // @ts-expect-error
333
332
  // eslint-disable-next-line
334
333
  Math.abs(path[i + 1]) >> 0 === +path[i + 1]
335
334
  ? [] // Yes: assign a new array object
@@ -123,7 +123,7 @@ function _stringify(obj, opt = {}) {
123
123
  if (s === undefined)
124
124
  return 'undefined';
125
125
  // Handle maxLen
126
- const { maxLen = 10000 } = opt;
126
+ const { maxLen = 10_000 } = opt;
127
127
  if (maxLen && s.length > maxLen) {
128
128
  s = s.slice(0, maxLen) + `... ${Math.ceil(s.length / 1024)} KB message truncated`;
129
129
  }
package/dist/types.d.ts CHANGED
@@ -177,6 +177,16 @@ export type NumberOfMilliseconds = number;
177
177
  * Same as `number`, but with semantic meaning that it's an Integer.
178
178
  */
179
179
  export type Integer = number;
180
+ /**
181
+ * Convenience type alias, that allows to write this:
182
+ *
183
+ * data: NullableNumber[]
184
+ *
185
+ * instead of this:
186
+ *
187
+ * data: (number | null)[]
188
+ */
189
+ export type NullableNumber = number | null;
180
190
  /**
181
191
  * Used as a compact representation of truthy value.
182
192
  * undefined ('' or other short falsy value) should be used as falsy value.
@@ -304,6 +304,15 @@ export function _last(array) {
304
304
  export function _lastOrUndefined(array) {
305
305
  return array[array.length - 1];
306
306
  }
307
+ /**
308
+ * Returns the first item of non-empty array.
309
+ * Throws if array is empty.
310
+ */
311
+ export function _first(array) {
312
+ if (!array.length)
313
+ throw new Error('_first called on empty array');
314
+ return array[0];
315
+ }
307
316
  export function _minOrUndefined(array) {
308
317
  const a = array.filter(_isNotNullish);
309
318
  if (!a.length)
@@ -306,7 +306,6 @@ export function _set(obj, path, value) {
306
306
  a[c]
307
307
  : // No: create the key. Is the next key a potential array-index?
308
308
  (a[c] =
309
- // @ts-expect-error
310
309
  // eslint-disable-next-line
311
310
  Math.abs(path[i + 1]) >> 0 === +path[i + 1]
312
311
  ? [] // Yes: assign a new array object
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.210.0",
3
+ "version": "14.212.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -13,7 +13,7 @@
13
13
  "zod": "^3.20.2"
14
14
  },
15
15
  "devDependencies": {
16
- "@naturalcycles/bench-lib": "^1.5.0",
16
+ "@naturalcycles/bench-lib": "^2.0.0",
17
17
  "@naturalcycles/dev-lib": "^13.0.1",
18
18
  "@naturalcycles/nodejs-lib": "^13.0.1",
19
19
  "@naturalcycles/time-lib": "^3.5.1",
@@ -365,6 +365,15 @@ export function _lastOrUndefined<T>(array: T[]): T | undefined {
365
365
  return array[array.length - 1]
366
366
  }
367
367
 
368
+ /**
369
+ * Returns the first item of non-empty array.
370
+ * Throws if array is empty.
371
+ */
372
+ export function _first<T>(array: T[]): T {
373
+ if (!array.length) throw new Error('_first called on empty array')
374
+ return array[0]!
375
+ }
376
+
368
377
  export function _minOrUndefined<T>(array: T[]): NonNullable<T> | undefined {
369
378
  const a = array.filter(_isNotNullish)
370
379
  if (!a.length) return
@@ -39,7 +39,7 @@ export function _rangeIterable(fromIncl: number, toExcl?: number, step = 1): Ite
39
39
 
40
40
  return Iterable2.of({
41
41
  *[Symbol.iterator]() {
42
- for (let i = fromIncl; i < toExcl!; i += step) {
42
+ for (let i = fromIncl; i < toExcl; i += step) {
43
43
  yield i
44
44
  }
45
45
  },
@@ -67,7 +67,7 @@ export function _rangeAsyncIterable(
67
67
 
68
68
  return AsyncIterable2.of({
69
69
  async *[Symbol.asyncIterator]() {
70
- for (let i = fromIncl; i < toExcl!; i += step) {
70
+ for (let i = fromIncl; i < toExcl; i += step) {
71
71
  yield i
72
72
  }
73
73
  },
@@ -360,7 +360,6 @@ export function _set<T extends AnyObject>(obj: T, path: PropertyPath, value: any
360
360
  a[c]
361
361
  : // No: create the key. Is the next key a potential array-index?
362
362
  (a[c] =
363
- // @ts-expect-error
364
363
  // eslint-disable-next-line
365
364
  Math.abs(path[i + 1]) >> 0 === +path[i + 1]
366
365
  ? [] // Yes: assign a new array object
@@ -37,7 +37,7 @@ function serializer(replacer?: Reviver, cycleReplacer?: Reviver): Reviver {
37
37
  ~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
38
38
  ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
39
39
  if (~stack.indexOf(value)) {
40
- value = cycleReplacer!.call(this, key, value)
40
+ value = cycleReplacer.call(this, key, value)
41
41
  }
42
42
  } else {
43
43
  stack.push(value)
package/src/types.ts CHANGED
@@ -235,6 +235,17 @@ export type NumberOfMilliseconds = number
235
235
  */
236
236
  export type Integer = number
237
237
 
238
+ /**
239
+ * Convenience type alias, that allows to write this:
240
+ *
241
+ * data: NullableNumber[]
242
+ *
243
+ * instead of this:
244
+ *
245
+ * data: (number | null)[]
246
+ */
247
+ export type NullableNumber = number | null
248
+
238
249
  /**
239
250
  * Used as a compact representation of truthy value.
240
251
  * undefined ('' or other short falsy value) should be used as falsy value.