@naturalcycles/js-lib 15.66.0 → 15.68.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.
@@ -197,6 +197,8 @@ export declare function _mapToObject<T, V>(array: Iterable<T>, mapper: (item: T)
197
197
  * Based on: https://stackoverflow.com/a/12646864/4919972
198
198
  */
199
199
  export declare function _shuffle<T>(array: T[], opt?: MutateOptions): T[];
200
+ export declare function _firstLast<T>(array: readonly T[]): [first: T, last: T];
201
+ export declare function _firstLastOrUndefined<T>(array: readonly T[]): [first: T, last: T] | undefined;
200
202
  /**
201
203
  * Returns last item of non-empty array.
202
204
  * Throws if array is empty.
@@ -363,6 +363,16 @@ export function _shuffle(array, opt = {}) {
363
363
  }
364
364
  return a;
365
365
  }
366
+ export function _firstLast(array) {
367
+ if (!array.length)
368
+ throw new Error('_firstLast called on empty array');
369
+ return [array[0], array[array.length - 1]];
370
+ }
371
+ export function _firstLastOrUndefined(array) {
372
+ if (!array.length)
373
+ return;
374
+ return [array[0], array[array.length - 1]];
375
+ }
366
376
  /**
367
377
  * Returns last item of non-empty array.
368
378
  * Throws if array is empty.
@@ -19,7 +19,9 @@ export interface KeySortedMapOptions<K> {
19
19
  *
20
20
  * @experimental
21
21
  */
22
- export declare class KeySortedMap<K, V> implements Map<K, V> {
22
+ export interface KeySortedMap<K, V> extends Map<K, V> {
23
+ }
24
+ export declare class KeySortedMap<K, V> {
23
25
  #private;
24
26
  private readonly map;
25
27
  constructor(entries?: [K, V][], opt?: KeySortedMapOptions<K>);
@@ -52,7 +54,7 @@ export declare class KeySortedMap<K, V> implements Map<K, V> {
52
54
  entries(): MapIterator<[K, V]>;
53
55
  [Symbol.iterator](): MapIterator<[K, V]>;
54
56
  toString(): string;
55
- [Symbol.toStringTag]: string;
57
+ readonly [Symbol.toStringTag] = "KeySortedMap";
56
58
  /**
57
59
  * Zero-allocation callbacks over sorted data (faster than spreading to arrays).
58
60
  */
@@ -1,15 +1,5 @@
1
1
  import { _assert } from '../error/index.js';
2
- /**
3
- * Maintains sorted array of keys.
4
- * Sorts **on insertion**, not on retrieval.
5
- *
6
- * - set(): O(log n) search + O(n) splice only when inserting a NEW key
7
- * - get/has: O(1)
8
- * - delete: O(log n) search + O(n) splice if present
9
- * - iteration: O(n) over pre-sorted keys (no sorting at iteration time)
10
- *
11
- * @experimental
12
- */
2
+ // oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
13
3
  export class KeySortedMap {
14
4
  map;
15
5
  #sortedKeys;
@@ -14,7 +14,9 @@ export interface LazyKeySortedMapOptions<K> {
14
14
  *
15
15
  * @experimental
16
16
  */
17
- export declare class LazyKeySortedMap<K, V> implements Map<K, V> {
17
+ export interface LazyKeySortedMap<K, V> extends Map<K, V> {
18
+ }
19
+ export declare class LazyKeySortedMap<K, V> {
18
20
  #private;
19
21
  private readonly map;
20
22
  private readonly maybeSortedKeys;
@@ -48,7 +50,7 @@ export declare class LazyKeySortedMap<K, V> implements Map<K, V> {
48
50
  values(): MapIterator<V>;
49
51
  entries(): MapIterator<[K, V]>;
50
52
  [Symbol.iterator](): MapIterator<[K, V]>;
51
- [Symbol.toStringTag]: string;
53
+ readonly [Symbol.toStringTag] = "KeySortedMap";
52
54
  /**
53
55
  * Zero-allocation callbacks over sorted data (faster than spreading to arrays).
54
56
  */
@@ -1,10 +1,5 @@
1
1
  import { _assert } from '../error/index.js';
2
- /**
3
- * Maintains sorted array of keys.
4
- * Sorts **data access**, not on insertion.
5
- *
6
- * @experimental
7
- */
2
+ // oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
8
3
  export class LazyKeySortedMap {
9
4
  map;
10
5
  maybeSortedKeys;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.66.0",
4
+ "version": "15.68.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -426,6 +426,16 @@ export function _shuffle<T>(array: T[], opt: MutateOptions = {}): T[] {
426
426
  return a
427
427
  }
428
428
 
429
+ export function _firstLast<T>(array: readonly T[]): [first: T, last: T] {
430
+ if (!array.length) throw new Error('_firstLast called on empty array')
431
+ return [array[0]!, array[array.length - 1]!]
432
+ }
433
+
434
+ export function _firstLastOrUndefined<T>(array: readonly T[]): [first: T, last: T] | undefined {
435
+ if (!array.length) return
436
+ return [array[0]!, array[array.length - 1]!]
437
+ }
438
+
429
439
  /**
430
440
  * Returns last item of non-empty array.
431
441
  * Throws if array is empty.
@@ -22,7 +22,11 @@ export interface KeySortedMapOptions<K> {
22
22
  *
23
23
  * @experimental
24
24
  */
25
- export class KeySortedMap<K, V> implements Map<K, V> {
25
+ // oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
26
+ export interface KeySortedMap<K, V> extends Map<K, V> {}
27
+
28
+ // oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
29
+ export class KeySortedMap<K, V> {
26
30
  private readonly map: Map<K, V>
27
31
  readonly #sortedKeys: K[]
28
32
 
@@ -140,7 +144,7 @@ export class KeySortedMap<K, V> implements Map<K, V> {
140
144
  return 'abc'
141
145
  }
142
146
 
143
- [Symbol.toStringTag] = 'KeySortedMap'
147
+ readonly [Symbol.toStringTag] = 'KeySortedMap'
144
148
 
145
149
  /**
146
150
  * Zero-allocation callbacks over sorted data (faster than spreading to arrays).
@@ -148,7 +152,7 @@ export class KeySortedMap<K, V> implements Map<K, V> {
148
152
  forEach(cb: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
149
153
  const { map } = this
150
154
  for (const k of this.#sortedKeys) {
151
- cb.call(thisArg, map.get(k)!, k, this)
155
+ cb.call(thisArg, map.get(k)!, k, this as unknown as Map<K, V>)
152
156
  }
153
157
  }
154
158
 
@@ -17,7 +17,11 @@ export interface LazyKeySortedMapOptions<K> {
17
17
  *
18
18
  * @experimental
19
19
  */
20
- export class LazyKeySortedMap<K, V> implements Map<K, V> {
20
+ // oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
21
+ export interface LazyKeySortedMap<K, V> extends Map<K, V> {}
22
+
23
+ // oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
24
+ export class LazyKeySortedMap<K, V> {
21
25
  private readonly map: Map<K, V>
22
26
  private readonly maybeSortedKeys: K[]
23
27
  private keysAreSorted = false
@@ -117,7 +121,7 @@ export class LazyKeySortedMap<K, V> implements Map<K, V> {
117
121
  return this.entries()
118
122
  }
119
123
 
120
- [Symbol.toStringTag] = 'KeySortedMap'
124
+ readonly [Symbol.toStringTag] = 'KeySortedMap'
121
125
 
122
126
  /**
123
127
  * Zero-allocation callbacks over sorted data (faster than spreading to arrays).
@@ -125,7 +129,7 @@ export class LazyKeySortedMap<K, V> implements Map<K, V> {
125
129
  forEach(cb: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
126
130
  const { map } = this
127
131
  for (const k of this.getSortedKeys()) {
128
- cb.call(thisArg, map.get(k)!, k, this)
132
+ cb.call(thisArg, map.get(k)!, k, this as unknown as Map<K, V>)
129
133
  }
130
134
  }
131
135