@@ -24,14 +24,6 @@ export function numberInRangeInclusive(
2424 return value >= min && value <= max ;
2525}
2626
27- export function createCounter ( ) {
28- let i = 0 ;
29- return ( ) => {
30- i ++ ;
31- return i ;
32- } ;
33- }
34-
3527/**
3628 * Returns whether an element has a Left-to-Right directionality.
3729 */
@@ -153,19 +145,25 @@ export function isElement(node: unknown): node is Element {
153145 return node instanceof Node && node . nodeType === Node . ELEMENT_NODE ;
154146}
155147
156- export function getElementsFromEventPath < T extends Element > ( event : Event ) {
157- return event . composedPath ( ) . filter ( ( item ) => isElement ( item ) ) as T [ ] ;
158- }
159-
148+ export function findElementFromEventPath < K extends keyof HTMLElementTagNameMap > (
149+ predicate : K ,
150+ event : Event
151+ ) : HTMLElementTagNameMap [ K ] | undefined ;
160152export function findElementFromEventPath < T extends Element > (
161153 predicate : string | ( ( element : Element ) => boolean ) ,
162154 event : Event
155+ ) : T | undefined ;
156+ export function findElementFromEventPath (
157+ predicate : string | ( ( element : Element ) => boolean ) ,
158+ event : Event
163159) {
164160 const func = isString ( predicate )
165161 ? ( e : Element ) => e . matches ( predicate )
166162 : ( e : Element ) => predicate ( e ) ;
167163
168- return getElementsFromEventPath ( event ) . find ( func ) as T | undefined ;
164+ return Iterator . from ( event . composedPath ( ) ) . find (
165+ ( item ) => isElement ( item ) && func ( item )
166+ ) as Element | undefined ;
169167}
170168
171169export function first < T > ( arr : T [ ] ) {
@@ -181,44 +179,27 @@ export function modulo(n: number, d: number) {
181179}
182180
183181/**
184- * Creates an array of `n` elements from a given iterator.
185- *
186- */
187- export function take < T > ( iterable : IterableIterator < T > , n : number ) {
188- const result : T [ ] = [ ] ;
189- let i = 0 ;
190- let current = iterable . next ( ) ;
191-
192- while ( i < n && ! current . done ) {
193- result . push ( current . value ) ;
194- current = iterable . next ( ) ;
195- i ++ ;
196- }
197-
198- return result ;
199- }
200-
201- /**
202- * Splits an array into chunks of length `size` and returns a generator
203- * yielding each chunk.
204- * The last chunk may contain less than `size` elements.
182+ * Splits an array into chunks of a specified size and returns a generator that yields each chunk.
205183 *
206184 * @example
207185 * ```typescript
208- * const arr = [0,1,2,3,4,5,6,7,8,9];
209- *
210- * Array.from(chunk(arr, 2)) // [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
211- * Array.from(chunk(arr, 3)) // [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
212- * Array.from(chunk([], 3)) // []
213- * Array.from(chunk(arr, -3)) // Error
186+ * [...chunk([1, 2, 3, 4, 5], 2)]; // [[1, 2], [3, 4], [5]]
214187 * ```
188+ *
189+ * @throws If the `size` parameter is not a safe integer greater than or equal to 1.
215190 */
216- export function * chunk < T > ( arr : T [ ] , size : number ) {
217- if ( size < 1 ) {
191+ export function * chunk < T > ( arr : T [ ] , size : number ) : Generator < T [ ] > {
192+ if ( ! Number . isSafeInteger ( size ) || size < 1 ) {
218193 throw new Error ( 'size must be an integer >= 1' ) ;
219194 }
220- for ( let i = 0 ; i < arr . length ; i += size ) {
221- yield arr . slice ( i , i + size ) ;
195+
196+ const iterator = Iterator . from ( arr ) ;
197+ const length = arr . length ;
198+ let i = 0 ;
199+
200+ while ( i < length ) {
201+ yield iterator . take ( size ) . toArray ( ) ;
202+ i += size ;
222203 }
223204}
224205
0 commit comments