@naturalcycles/js-lib 15.76.0 → 15.76.1

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.
@@ -218,14 +218,19 @@ export declare function _first<T>(array: readonly T[]): T;
218
218
  */
219
219
  export declare function _firstOrUndefined<T>(array: readonly T[]): T | undefined;
220
220
  /**
221
- * Returns the first item of an iterable (Set, Map.values(), generator, etc.),
222
- * or `undefined` if the iterable is empty.
221
+ * Returns the first item of a non-empty iterable (Set, Map.values(), generator, etc.).
222
+ * Throws if the iterable is empty.
223
223
  *
224
224
  * Avoids the `Array.from(iter)[0]` pattern that materialises the entire iterable.
225
225
  * `for...of` with an early return advances the iterator exactly once; if the
226
226
  * iterator implements `return()` (generators, etc.), it is invoked for cleanup.
227
227
  */
228
- export declare function _firstFromIterable<T>(iter: Iterable<T>): T | undefined;
228
+ export declare function _firstFromIterable<T>(iter: Iterable<T>): T;
229
+ /**
230
+ * Returns the first item of an iterable (or undefined if the iterable is empty).
231
+ * See `_firstFromIterable` for the iteration semantics.
232
+ */
233
+ export declare function _firstOrUndefinedFromIterable<T>(iter: Iterable<T>): T | undefined;
229
234
  export declare function _minOrUndefined<T>(array: readonly T[]): NonNullable<T> | undefined;
230
235
  /**
231
236
  * Filters out nullish values (undefined and null).
@@ -404,14 +404,23 @@ export function _firstOrUndefined(array) {
404
404
  return array[0];
405
405
  }
406
406
  /**
407
- * Returns the first item of an iterable (Set, Map.values(), generator, etc.),
408
- * or `undefined` if the iterable is empty.
407
+ * Returns the first item of a non-empty iterable (Set, Map.values(), generator, etc.).
408
+ * Throws if the iterable is empty.
409
409
  *
410
410
  * Avoids the `Array.from(iter)[0]` pattern that materialises the entire iterable.
411
411
  * `for...of` with an early return advances the iterator exactly once; if the
412
412
  * iterator implements `return()` (generators, etc.), it is invoked for cleanup.
413
413
  */
414
414
  export function _firstFromIterable(iter) {
415
+ for (const item of iter)
416
+ return item;
417
+ throw new Error('_firstFromIterable called on empty iterable');
418
+ }
419
+ /**
420
+ * Returns the first item of an iterable (or undefined if the iterable is empty).
421
+ * See `_firstFromIterable` for the iteration semantics.
422
+ */
423
+ export function _firstOrUndefinedFromIterable(iter) {
415
424
  for (const item of iter)
416
425
  return item;
417
426
  return undefined;
@@ -88,7 +88,8 @@ export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapp
88
88
  export declare function _mapObject<OUT = unknown, IN extends AnyObject = AnyObject>(obj: IN, mapper: ObjectMapper<IN, KeyValueTuple<string, any> | typeof SKIP>): OUT;
89
89
  export declare function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): keyof T | undefined;
90
90
  /**
91
- * Returns the first key of the object, or `undefined` if the object is empty.
91
+ * Returns the first key of a non-empty object.
92
+ * Throws if the object is empty.
92
93
  *
93
94
  * Performance-optimised: uses `for...in` with an early return to avoid
94
95
  * allocating the full `Object.keys(obj)` array. The `Object.hasOwn` filter
@@ -98,18 +99,34 @@ export declare function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<
98
99
  * objects (integer-like keys ascending first, then string keys in
99
100
  * insertion order).
100
101
  */
101
- export declare function _firstKey<T extends AnyObject>(obj: T): keyof T | undefined;
102
+ export declare function _firstKey<T extends AnyObject>(obj: T): keyof T;
102
103
  /**
103
- * Returns the first value of the object, or `undefined` if the object is empty.
104
+ * Returns the first key of the object (or undefined if the object is empty).
104
105
  * See `_firstKey` for the iteration-order contract.
105
106
  */
106
- export declare function _firstValue<T extends AnyObject>(obj: T): ValueOf<T> | undefined;
107
+ export declare function _firstKeyOrUndefined<T extends AnyObject>(obj: T): keyof T | undefined;
107
108
  /**
108
- * Returns the first [key, value] tuple of the object,
109
- * or `undefined` if the object is empty.
109
+ * Returns the first value of a non-empty object.
110
+ * Throws if the object is empty.
110
111
  * See `_firstKey` for the iteration-order contract.
111
112
  */
112
- export declare function _firstEntry<T extends AnyObject>(obj: T): [keyof T, ValueOf<T>] | undefined;
113
+ export declare function _firstValue<T extends AnyObject>(obj: T): ValueOf<T>;
114
+ /**
115
+ * Returns the first value of the object (or undefined if the object is empty).
116
+ * See `_firstKey` for the iteration-order contract.
117
+ */
118
+ export declare function _firstValueOrUndefined<T extends AnyObject>(obj: T): ValueOf<T> | undefined;
119
+ /**
120
+ * Returns the first [key, value] tuple of a non-empty object.
121
+ * Throws if the object is empty.
122
+ * See `_firstKey` for the iteration-order contract.
123
+ */
124
+ export declare function _firstEntry<T extends AnyObject>(obj: T): [keyof T, ValueOf<T>];
125
+ /**
126
+ * Returns the first [key, value] tuple of the object (or undefined if the object is empty).
127
+ * See `_firstKey` for the iteration-order contract.
128
+ */
129
+ export declare function _firstEntryOrUndefined<T extends AnyObject>(obj: T): [keyof T, ValueOf<T>] | undefined;
113
130
  export declare function _objectNullValuesToUndefined<T extends AnyObject>(obj: T, opt?: MutateOptions): T;
114
131
  /**
115
132
  * Deep copy object (by json parse/stringify, since it has unbeatable performance+simplicity combo).
@@ -192,7 +192,8 @@ export function _findKeyByValue(obj, v) {
192
192
  return Object.entries(obj).find(([_, value]) => value === v)?.[0];
193
193
  }
194
194
  /**
195
- * Returns the first key of the object, or `undefined` if the object is empty.
195
+ * Returns the first key of a non-empty object.
196
+ * Throws if the object is empty.
196
197
  *
197
198
  * Performance-optimised: uses `for...in` with an early return to avoid
198
199
  * allocating the full `Object.keys(obj)` array. The `Object.hasOwn` filter
@@ -203,6 +204,17 @@ export function _findKeyByValue(obj, v) {
203
204
  * insertion order).
204
205
  */
205
206
  export function _firstKey(obj) {
207
+ for (const k in obj) {
208
+ if (Object.hasOwn(obj, k))
209
+ return k;
210
+ }
211
+ throw new Error('_firstKey called on empty object');
212
+ }
213
+ /**
214
+ * Returns the first key of the object (or undefined if the object is empty).
215
+ * See `_firstKey` for the iteration-order contract.
216
+ */
217
+ export function _firstKeyOrUndefined(obj) {
206
218
  for (const k in obj) {
207
219
  if (Object.hasOwn(obj, k))
208
220
  return k;
@@ -210,10 +222,22 @@ export function _firstKey(obj) {
210
222
  return undefined;
211
223
  }
212
224
  /**
213
- * Returns the first value of the object, or `undefined` if the object is empty.
225
+ * Returns the first value of a non-empty object.
226
+ * Throws if the object is empty.
214
227
  * See `_firstKey` for the iteration-order contract.
215
228
  */
216
229
  export function _firstValue(obj) {
230
+ for (const k in obj) {
231
+ if (Object.hasOwn(obj, k))
232
+ return obj[k];
233
+ }
234
+ throw new Error('_firstValue called on empty object');
235
+ }
236
+ /**
237
+ * Returns the first value of the object (or undefined if the object is empty).
238
+ * See `_firstKey` for the iteration-order contract.
239
+ */
240
+ export function _firstValueOrUndefined(obj) {
217
241
  for (const k in obj) {
218
242
  if (Object.hasOwn(obj, k))
219
243
  return obj[k];
@@ -221,11 +245,22 @@ export function _firstValue(obj) {
221
245
  return undefined;
222
246
  }
223
247
  /**
224
- * Returns the first [key, value] tuple of the object,
225
- * or `undefined` if the object is empty.
248
+ * Returns the first [key, value] tuple of a non-empty object.
249
+ * Throws if the object is empty.
226
250
  * See `_firstKey` for the iteration-order contract.
227
251
  */
228
252
  export function _firstEntry(obj) {
253
+ for (const k in obj) {
254
+ if (Object.hasOwn(obj, k))
255
+ return [k, obj[k]];
256
+ }
257
+ throw new Error('_firstEntry called on empty object');
258
+ }
259
+ /**
260
+ * Returns the first [key, value] tuple of the object (or undefined if the object is empty).
261
+ * See `_firstKey` for the iteration-order contract.
262
+ */
263
+ export function _firstEntryOrUndefined(obj) {
229
264
  for (const k in obj) {
230
265
  if (Object.hasOwn(obj, k))
231
266
  return [k, obj[k]];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.76.0",
4
+ "version": "15.76.1",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -469,14 +469,23 @@ export function _firstOrUndefined<T>(array: readonly T[]): T | undefined {
469
469
  }
470
470
 
471
471
  /**
472
- * Returns the first item of an iterable (Set, Map.values(), generator, etc.),
473
- * or `undefined` if the iterable is empty.
472
+ * Returns the first item of a non-empty iterable (Set, Map.values(), generator, etc.).
473
+ * Throws if the iterable is empty.
474
474
  *
475
475
  * Avoids the `Array.from(iter)[0]` pattern that materialises the entire iterable.
476
476
  * `for...of` with an early return advances the iterator exactly once; if the
477
477
  * iterator implements `return()` (generators, etc.), it is invoked for cleanup.
478
478
  */
479
- export function _firstFromIterable<T>(iter: Iterable<T>): T | undefined {
479
+ export function _firstFromIterable<T>(iter: Iterable<T>): T {
480
+ for (const item of iter) return item
481
+ throw new Error('_firstFromIterable called on empty iterable')
482
+ }
483
+
484
+ /**
485
+ * Returns the first item of an iterable (or undefined if the iterable is empty).
486
+ * See `_firstFromIterable` for the iteration semantics.
487
+ */
488
+ export function _firstOrUndefinedFromIterable<T>(iter: Iterable<T>): T | undefined {
480
489
  for (const item of iter) return item
481
490
  return undefined
482
491
  }
@@ -243,7 +243,8 @@ export function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): key
243
243
  }
244
244
 
245
245
  /**
246
- * Returns the first key of the object, or `undefined` if the object is empty.
246
+ * Returns the first key of a non-empty object.
247
+ * Throws if the object is empty.
247
248
  *
248
249
  * Performance-optimised: uses `for...in` with an early return to avoid
249
250
  * allocating the full `Object.keys(obj)` array. The `Object.hasOwn` filter
@@ -253,7 +254,18 @@ export function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): key
253
254
  * objects (integer-like keys ascending first, then string keys in
254
255
  * insertion order).
255
256
  */
256
- export function _firstKey<T extends AnyObject>(obj: T): keyof T | undefined {
257
+ export function _firstKey<T extends AnyObject>(obj: T): keyof T {
258
+ for (const k in obj) {
259
+ if (Object.hasOwn(obj, k)) return k as keyof T
260
+ }
261
+ throw new Error('_firstKey called on empty object')
262
+ }
263
+
264
+ /**
265
+ * Returns the first key of the object (or undefined if the object is empty).
266
+ * See `_firstKey` for the iteration-order contract.
267
+ */
268
+ export function _firstKeyOrUndefined<T extends AnyObject>(obj: T): keyof T | undefined {
257
269
  for (const k in obj) {
258
270
  if (Object.hasOwn(obj, k)) return k as keyof T
259
271
  }
@@ -261,10 +273,22 @@ export function _firstKey<T extends AnyObject>(obj: T): keyof T | undefined {
261
273
  }
262
274
 
263
275
  /**
264
- * Returns the first value of the object, or `undefined` if the object is empty.
276
+ * Returns the first value of a non-empty object.
277
+ * Throws if the object is empty.
265
278
  * See `_firstKey` for the iteration-order contract.
266
279
  */
267
- export function _firstValue<T extends AnyObject>(obj: T): ValueOf<T> | undefined {
280
+ export function _firstValue<T extends AnyObject>(obj: T): ValueOf<T> {
281
+ for (const k in obj) {
282
+ if (Object.hasOwn(obj, k)) return obj[k] as ValueOf<T>
283
+ }
284
+ throw new Error('_firstValue called on empty object')
285
+ }
286
+
287
+ /**
288
+ * Returns the first value of the object (or undefined if the object is empty).
289
+ * See `_firstKey` for the iteration-order contract.
290
+ */
291
+ export function _firstValueOrUndefined<T extends AnyObject>(obj: T): ValueOf<T> | undefined {
268
292
  for (const k in obj) {
269
293
  if (Object.hasOwn(obj, k)) return obj[k] as ValueOf<T>
270
294
  }
@@ -272,11 +296,24 @@ export function _firstValue<T extends AnyObject>(obj: T): ValueOf<T> | undefined
272
296
  }
273
297
 
274
298
  /**
275
- * Returns the first [key, value] tuple of the object,
276
- * or `undefined` if the object is empty.
299
+ * Returns the first [key, value] tuple of a non-empty object.
300
+ * Throws if the object is empty.
301
+ * See `_firstKey` for the iteration-order contract.
302
+ */
303
+ export function _firstEntry<T extends AnyObject>(obj: T): [keyof T, ValueOf<T>] {
304
+ for (const k in obj) {
305
+ if (Object.hasOwn(obj, k)) return [k as keyof T, obj[k] as ValueOf<T>]
306
+ }
307
+ throw new Error('_firstEntry called on empty object')
308
+ }
309
+
310
+ /**
311
+ * Returns the first [key, value] tuple of the object (or undefined if the object is empty).
277
312
  * See `_firstKey` for the iteration-order contract.
278
313
  */
279
- export function _firstEntry<T extends AnyObject>(obj: T): [keyof T, ValueOf<T>] | undefined {
314
+ export function _firstEntryOrUndefined<T extends AnyObject>(
315
+ obj: T,
316
+ ): [keyof T, ValueOf<T>] | undefined {
280
317
  for (const k in obj) {
281
318
  if (Object.hasOwn(obj, k)) return [k as keyof T, obj[k] as ValueOf<T>]
282
319
  }