Skip to content

Commit

Permalink
improve docs a little
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Aug 14, 2024
1 parent d595f49 commit f7f1f98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/array/toggle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _.toggle(gods, 'ra') // => ['zeus', 'loki']
_.toggle(gods, 'vishnu') // => ['ra', 'zeus', 'loki', 'vishnu']
```

### toggle(list, item, identity)
### toggle(list, item, toKey)

You can pass an optional `toKey` function to determine the identity of non-primitive values. Helpful when working with more complex data types.

Expand All @@ -34,7 +34,7 @@ _.toggle(gods, ra, g => g.name) // => [zeus, loki]
_.toggle(gods, vishnu, g => g.name) // => [ra, zeus, loki, vishnu]
```

### toggle(list, item, identity, options)
### toggle(list, item, toKey, options)

By default, toggle will append the item if it does not exist. If you need to prepend the item instead you can override the `strategy` in the options argument.

Expand Down
23 changes: 16 additions & 7 deletions src/array/toggle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
/**
* If the item matching the condition already exists in the list it
* will be removed. If it does not it will be added.
* Either adds or removes an item from an array, based on whether it
* already exists in the array. If multiple items match the given
* `item`, all matching items will be removed.
*
* Note that the given `array` is *not mutated*. A copy of the array
* is returned with the given item either added or removed.
*
* - **"toKey" parameter**
* - You may define a `toKey` callback, which is a function that
* converts an item into a value that can be checked for equality.
* When called with the given `item`, an index of -1 will be passed.
*
* - **"strategy" option**
* - You may define a `strategy` option, which determines where the
* item should be added in the array.
*
* @see https://radashi-org.github.io/reference/array/toggle
* @example
Expand All @@ -23,11 +36,7 @@
export function toggle<T>(
array: readonly T[],
item: T,
/**
* Converts an item of type T item into a value that can be checked
* for equality. For an item idx will be -1.
*/
toKey?: null | ((item: T, idx: number) => number | string | symbol),
toKey?: ((item: T, idx: number) => number | string | symbol) | null,
options?: {
strategy?: 'prepend' | 'append'
},
Expand Down

0 comments on commit f7f1f98

Please sign in to comment.