state-toggle
Advanced tools
Comparing version 2.0.1 to 2.0.2
/** | ||
* Create a toggle, which when entering toggles `key` on `ctx` (or `this`, if `ctx` | ||
* is not given) to `!initial`, and when exiting, sets `key` on the context back to | ||
* the value it had before entering. | ||
* @callback Exit | ||
* Exit function. | ||
* @returns {void} | ||
*/ | ||
/** | ||
* @callback Enter | ||
* Enter function. | ||
* @returns {Exit} | ||
* Exit function. | ||
*/ | ||
/** | ||
* Create a toggle, which when entering toggles `key` on `ctx` (or `this`, if | ||
* `ctx` is not given) to `!initial`, and when exiting, sets `key` on the | ||
* context back to the value it had before entering. | ||
* | ||
* @param {string} key | ||
* Key to toggle | ||
* @param {boolean} state | ||
* @param {Record<string, any>} [ctx] | ||
* @returns {() => () => void} | ||
* Default state. | ||
* @param {Record<string|number|symbol, unknown>} [ctx] | ||
* Record to toggle on (default: `this` of `enter`). | ||
* @returns {Enter} | ||
* Enter function. | ||
*/ | ||
export function stateToggle(key: string, state: boolean, ctx?: Record<string, any>): () => () => void; | ||
export function stateToggle( | ||
key: string, | ||
state: boolean, | ||
ctx?: Record<string | number | symbol, unknown> | undefined | ||
): Enter | ||
/** | ||
* Exit function. | ||
*/ | ||
export type Exit = () => void | ||
/** | ||
* Enter function. | ||
*/ | ||
export type Enter = () => Exit |
33
index.js
/** | ||
* Create a toggle, which when entering toggles `key` on `ctx` (or `this`, if `ctx` | ||
* is not given) to `!initial`, and when exiting, sets `key` on the context back to | ||
* the value it had before entering. | ||
* @callback Exit | ||
* Exit function. | ||
* @returns {void} | ||
*/ | ||
/** | ||
* @callback Enter | ||
* Enter function. | ||
* @returns {Exit} | ||
* Exit function. | ||
*/ | ||
/** | ||
* Create a toggle, which when entering toggles `key` on `ctx` (or `this`, if | ||
* `ctx` is not given) to `!initial`, and when exiting, sets `key` on the | ||
* context back to the value it had before entering. | ||
* | ||
* @param {string} key | ||
* Key to toggle | ||
* @param {boolean} state | ||
* @param {Record<string, any>} [ctx] | ||
* @returns {() => () => void} | ||
* Default state. | ||
* @param {Record<string|number|symbol, unknown>} [ctx] | ||
* Record to toggle on (default: `this` of `enter`). | ||
* @returns {Enter} | ||
* Enter function. | ||
*/ | ||
@@ -17,7 +34,7 @@ export function stateToggle(key, state, ctx) { | ||
* @this {Record<string, any>} | ||
* @returns {() => void} | ||
* @returns {Exit} | ||
*/ | ||
function enter() { | ||
var context = ctx || this | ||
var current = context[key] | ||
const context = ctx || this | ||
const current = context[key] | ||
@@ -24,0 +41,0 @@ context[key] = !state |
{ | ||
"name": "state-toggle", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Enter/exit a state", | ||
@@ -30,20 +30,18 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.52.0" | ||
}, | ||
"scripts": { | ||
"prepublishOnly": "npm run build", | ||
"prepack": "npm run build && npm run format", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"prebuild": "rimraf \"*.d.ts\"", | ||
"build": "tsc", | ||
"test-api": "node test", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
@@ -59,7 +57,3 @@ "prettier": { | ||
"xo": { | ||
"prettier": true, | ||
"esnext": false, | ||
"ignores": [ | ||
"state-toggle.js" | ||
] | ||
"prettier": true | ||
}, | ||
@@ -70,3 +64,9 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
# state-toggle | ||
[![Build][build-badge]][build] | ||
[![Coverage][coverage-badge]][coverage] | ||
[![Downloads][downloads-badge]][downloads] | ||
[![Size][size-badge]][size] | ||
**Stability: Legacy**. | ||
This package is no longer recommended for use. | ||
It’s still covered by semantic-versioning guarantees and not yet deprecated, but | ||
use of this package should be avoided. | ||
Please write some better code for yourself! | ||
Enter/exit a state. | ||
Legacy [documentation for this package][docs] is still available in Git. | ||
## Install | ||
[npm][]: | ||
```sh | ||
npm install state-toggle | ||
``` | ||
## Use | ||
```js | ||
import {stateToggle} from 'state-toggle' | ||
var ctx = {on: false} | ||
var enter = stateToggle('on', ctx.on, ctx) | ||
var exit | ||
// Entering: | ||
exit = enter() | ||
console.log(ctx.on) // => true | ||
// Exiting: | ||
exit() | ||
console.log(ctx.on) // => false | ||
``` | ||
## API | ||
`state-toggle` exports the following identifier: `stateToggle`. | ||
There is no default export. | ||
### `toggle(key, initial[, ctx])` | ||
Create a toggle, which when entering toggles `key` on `ctx` (or `this`, if `ctx` | ||
is not given) to `!initial`, and when exiting, sets `key` on the context back to | ||
the value it had before entering. | ||
###### Returns | ||
`Function` — [`enter`][enter]. | ||
### `enter()` | ||
Enter the state. | ||
###### Context | ||
If no `ctx` was given to `toggle`, the context object (`this`) of `enter()` is | ||
used to toggle. | ||
###### Returns | ||
`Function` — [`exit`][exit]. | ||
### `exit()` | ||
Exit the state, reverting `key` to the value it had before entering. | ||
## License | ||
@@ -74,20 +17,2 @@ | ||
[build-badge]: https://github.com/wooorm/state-toggle/workflows/main/badge.svg | ||
[build]: https://github.com/wooorm/state-toggle/actions | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/state-toggle.svg | ||
[coverage]: https://codecov.io/github/wooorm/state-toggle | ||
[downloads-badge]: https://img.shields.io/npm/dm/state-toggle.svg | ||
[downloads]: https://www.npmjs.com/package/state-toggle | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/state-toggle.svg | ||
[size]: https://bundlephobia.com/result?p=state-toggle | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[license]: license | ||
@@ -97,4 +22,2 @@ | ||
[enter]: #enter | ||
[exit]: #exit | ||
[docs]: https://github.com/wooorm/state-toggle/tree/063d450 |
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
8
83
5266
22