55[ ![ Downloads] [ downloads-badge ]] [ downloads ]
66[ ![ Size] [ size-badge ]] [ size ]
77
8- [ ` Array#forEach() ` ] [ foreach ] with the possibility to change the next position .
8+ [ ` Array#forEach() ` ] [ foreach ] but it’s possible to define where to move to next .
99
10- ## Install
10+ ## Contents
11+
12+ * [ What is this?] ( #what-is-this )
13+ * [ When should I use this?] ( #when-should-i-use-this )
14+ * [ Install] ( #install )
15+ * [ Use] ( #use )
16+ * [ API] ( #api )
17+ * [ ` arrayIterate(values, callbackFn[, thisArg]) ` ] ( #arrayiteratevalues-callbackfn-thisarg )
18+ * [ Types] ( #types )
19+ * [ Compatibility] ( #compatibility )
20+ * [ Security] ( #security )
21+ * [ Contribute] ( #contribute )
22+ * [ License] ( #license )
23+
24+ ## What is this?
25+
26+ A tiny package that works just like ` forEach ` , with one small difference.
1127
12- This package is ESM only: Node 12+ is needed to use it and it must be ` import ` ed
13- instead of ` require ` d.
28+ ## When should I use this?
1429
15- [ npm] [ ] :
30+ You can use this if for some weird reason—like I did—you have to sometimes
31+ skip a few places ahead or backwards when moving through an array.
32+
33+ ## Install
34+
35+ This package is [ ESM only] [ esm ] .
36+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
1637
1738``` sh
1839npm install array-iterate
1940```
2041
21- ## Use
42+ In Deno with [ Skypack ] [ ] :
2243
2344``` js
24- import {arrayIterate } from ' array-iterate'
45+ import {arrayIterate } from ' https://cdn.skypack.dev/array-iterate@2?dts'
46+ ```
2547
26- var isFirst = true
27- var thisArg = {hello: ' world' }
48+ In browsers with [ Skypack] [ ] :
49+
50+ ``` html
51+ <script type =" module" >
52+ import {arrayIterate } from ' https://cdn.skypack.dev/array-iterate@2?min'
53+ </script >
54+ ```
2855
29- arrayIterate ([ 1 , 2 , 3 , 4 ], callbackFn, thisArg)
56+ ## Use
3057
31- function callbackFn ( value , index , values ) {
32- console . log ( this , value, index, values)
58+ ``` js
59+ import { arrayIterate } from ' array-iterate '
3360
34- if (isFirst && index + 1 === values .length ) {
35- isFirst = false
36- return 0
37- }
38- }
61+ let first = true
62+
63+ arrayIterate (
64+ [1 , 2 , 3 , 4 ],
65+ (value , index , values ) => {
66+ console .log (this , value, index, values)
67+
68+ // Repeat once.
69+ if (first && index + 1 === values .length ) {
70+ first = false
71+ return 0
72+ }
73+ },
74+ {hello: ' world' }
75+ )
3976```
4077
4178Yields:
@@ -58,37 +95,63 @@ There is no default export.
5895
5996### ` arrayIterate(values, callbackFn[, thisArg]) `
6097
61- Perform the specified action for each element in an array, so works just like
62- [ ` Array#forEach() ` ] [ foreach ] .
63- When ` callbackFn ` returns a ` number ` , moves to the element at that index next.
98+ Perform the specified action for each element in an array (just like
99+ [ ` Array#forEach() ` ] [ foreach ] ).
100+ When ` callbackFn ` returns a ` number ` , moves to the element at that index
101+ next.
64102
65103###### Parameters
66104
67- * ` values ` (` Array ` -like thing )
68- — Values to iterate over
69- * ` callbackFn ` ([ ` Function ` ] [ callback ] )
70- — A function that accepts up to three arguments
105+ * ` values ` (` Array<*> ` )
106+ — values to iterate over
107+ * ` callbackFn ` (` Function ` )
108+ — function called for each element, can return the ` index ` to move to next
71109* ` thisArg ` (` * ` , optional)
72- — An object to which the this keyword can refer in ` callbackFn ` .
73- If ` thisArg ` is omitted, ` undefined ` is used as the ` this ` value
110+ — optional object assigned as ` this ` in ` callbackFn `
111+
112+ ###### Returns
113+
114+ ` undefined ` .
74115
75116#### ` function callbackFn(value, index, values) `
76117
77118Callback given to ` iterate ` .
78119
79120###### Parameters
80121
81- * ` value ` (` * ` ) — Current iteration
82- * ` index ` (` number ` ) — Position of ` value ` in ` values `
83- * ` values ` (` Array ` -like thing) — Currently iterated over
122+ * ` this ` (` * ` )
123+ — context object when given as ` thisArg ` to ` arrayIterate ` or ` undefined `
124+ * ` value ` (` * ` )
125+ — element in array
126+ * ` index ` (` number ` )
127+ — index of ` value ` in ` values `
128+ * ` values ` (` Array.<*> ` )
129+ — list
84130
85- ###### Context
131+ ###### Returns
86132
87- ` thisArg ` when given to ` arrayIterate ` or ` undefined `
133+ ` number ` or ` undefined ` — the ` index ` to move to next.
88134
89- ###### Returns
135+ ## Types
136+
137+ This package is fully typed with [ TypeScript] [ ] .
138+ There is also a ` CallbackFn ` type export that represents the callback function.
139+
140+ ## Compatibility
141+
142+ This package is at least compatible with all maintained versions of Node.js.
143+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
144+ It also works in Deno and modern browsers.
145+
146+ ## Security
90147
91- ` number ` (optional) — Position to go to next.
148+ This package is safe, assuming that you don’t create an infinite loop
149+ by keeping on repeating.
150+
151+ ## Contribute
152+
153+ Yes please!
154+ See [ How to Contribute to Open Source] [ contribute ] .
92155
93156## License
94157
@@ -114,10 +177,16 @@ Callback given to `iterate`.
114177
115178[ npm ] : https://docs.npmjs.com/cli/install
116179
180+ [ skypack ] : https://www.skypack.dev
181+
117182[ license ] : license
118183
119184[ author ] : https://wooorm.com
120185
121- [ foreach ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
186+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
122187
123- [ callback ] : #function-callbackfnvalue-index-values
188+ [ typescript ] : https://www.typescriptlang.org
189+
190+ [ contribute ] : https://opensource.guide/how-to-contribute/
191+
192+ [ foreach ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
0 commit comments