-
Notifications
You must be signed in to change notification settings - Fork 571
docs: point alias docs to canonical function #1871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,3 @@ | ||
| # each (Lodash Compatibility) | ||
|
|
||
| ::: warning Use `Array.prototype.forEach` | ||
|
|
||
| This `each` function operates slowly due to complex type processing and support for various collection types. | ||
|
|
||
| Instead, use the faster and more modern `Array.prototype.forEach`. | ||
|
|
||
| ::: | ||
|
|
||
| Performs an iteration operation on each element of an array or object. | ||
|
|
||
| ```typescript | ||
| const result = each(collection, iteratee); | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### `each(collection, iteratee?)` | ||
|
|
||
| Iterates through each element of an array, object, or string and executes the given function. For arrays, it iterates in index order; for objects, it iterates through enumerable properties. | ||
|
|
||
| ```typescript | ||
| import { each } from 'es-toolkit/compat'; | ||
|
|
||
| // Iterate array | ||
| each([1, 2, 3], (value, index) => console.log(value, index)); | ||
| // Logs: 1 0, 2 1, 3 2 | ||
|
|
||
| // Iterate object | ||
| each({ a: 1, b: 2 }, (value, key) => console.log(key, value)); | ||
| // Logs: 'a' 1, 'b' 2 | ||
|
|
||
| // Iterate string | ||
| each('hello', (char, index) => console.log(char, index)); | ||
| // Logs: 'h' 0, 'e' 1, 'l' 2, 'l' 3, 'o' 4 | ||
| ``` | ||
|
|
||
| If the function returns `false`, iteration stops. | ||
|
|
||
| ```typescript | ||
| import { each } from 'es-toolkit/compat'; | ||
|
|
||
| each([1, 2, 3, 4], value => { | ||
| console.log(value); | ||
| return value !== 2; // Stop at 2 | ||
| }); | ||
| // Logs: 1, 2 | ||
| ``` | ||
|
|
||
| #### Parameters | ||
|
|
||
| - `collection` (`ArrayLike<T> | Record<any, any> | string | null | undefined`): The collection to iterate over. | ||
| - `iteratee` (`(item: any, index: any, collection: any) => unknown`, optional): The function to execute for each element. Default is the `identity` function. | ||
|
|
||
| #### Returns | ||
|
|
||
| (`ArrayLike<T> | Record<any, any> | string | null | undefined`): Returns the original collection. | ||
| `each` is an alias of [`forEach`](./forEach.md). See the [`forEach`](./forEach.md) documentation for details. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ forEach(collection, callback); | |
|
|
||
| ## Usage | ||
|
|
||
| ### `forEach(collection, callback)` | ||
| ### `forEach(collection, callback?)` | ||
|
|
||
| Use `forEach` when you want to iterate over all elements of an array or object and execute a callback function for each element. The iteration stops if the callback returns `false`. | ||
|
|
||
|
|
@@ -76,11 +76,15 @@ forEach(undefined, value => { | |
| }); | ||
| ``` | ||
|
|
||
| #### Alias | ||
|
|
||
| `each` | ||
|
|
||
|
Comment on lines
+79
to
+82
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also added an Alias section to the I'm not sure whether this is the best place for the section. If you think there's a more appropriate location, please let me know! |
||
| #### Parameters | ||
|
|
||
| - `collection` (`ArrayLike<T> | Record<string, unknown> | null | undefined`): The array or object to iterate over. | ||
| - `callback` (`(value: T, index: number | string, collection: any) => void | false`): The function to execute for each element. Returns `false` to stop iteration. | ||
| - `collection` (`ArrayLike<T> | Record<any, any> | string | null | undefined`): The array or object to iterate over. | ||
| - `callback` (`(item: any, index: any, collection: any) => unknown`, optional): The function to execute for each element. Default is the `identity` function. | ||
|
Comment on lines
+85
to
+86
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noticed that the types documented in |
||
|
|
||
| #### Returns | ||
|
|
||
| (`T`): Returns the original collection that was iterated over. | ||
| (`ArrayLike<T> | Record<any, any> | string | null | undefined`): Returns the original collection. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the detailed documentation from the aliased function.
Using
eachandforEachas an example, they provide the same functionality, so I think their documentation should remain consistent as well. If both documents contain detailed explanations, any future changes will need to be made in both places.If one of them is updated without the other being reviewed, we could easily end up in a situation where two aliases provide different documentation for the same behavior.
For that reason, I removed the detailed description from
eachand had it refer to theforEachdocumentation instead.Previously, both functions had detailed documentation. Was there a particular reason for that?