Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/omer73364/toggle-item-in-array
A JavaScript package to toggle a value in an array with an optional validation function
https://github.com/omer73364/toggle-item-in-array
add array javascript method npm package remove toggle
Last synced: 9 days ago
JSON representation
A JavaScript package to toggle a value in an array with an optional validation function
- Host: GitHub
- URL: https://github.com/omer73364/toggle-item-in-array
- Owner: omer73364
- Created: 2021-04-05T10:23:33.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-16T13:33:37.000Z (almost 3 years ago)
- Last Synced: 2024-05-30T02:08:50.402Z (8 months ago)
- Topics: add, array, javascript, method, npm, package, remove, toggle
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# toggle-item-in-array
A JavaScript package to toggle a value in an array with an optional validation function
# toggle function adds or removes a value from an array
## it accepts three parameters:
1) an array
2) a value
3) a funcion to validate the value (optional parameter)
if the array actually includes the value then it removes the value from the array
if not then it adds the value to the array
## usage:### NPM:
download this library:
npm i toggle-item-in-array --save
then import it:
import { toggle } from 'toggle-item-in-array';
then you can use it as a function by "toggle" name
### HTML:include the library in your html page
then you can use it as a function by "toggle" name
## example:let arr = [12]
toggle(arr,5) // will add 5 to arr
console.log(arr) // [12,5]
toggle(arr,12) // will remove 12 from arr
console.log(arr) // [5]
toggle(arr,'bad',(v)=>v!=='bad') // will not add because the value is not valid
console.log(arr) // [5]