Comparing version 2.0.1 to 2.0.2
@@ -7,11 +7,10 @@ /** | ||
* | ||
* @template {object} T | ||
* @param {T} context Context object | ||
* @param {string | number} name Field where the original method lives | ||
* @param {unknown} context Context object | ||
* @param {string|number} name Field where the original method lives | ||
* @param {Function} callback Interceptor | ||
*/ | ||
export function cept<T extends unknown>( | ||
context: T, | ||
export function cept( | ||
context: unknown, | ||
name: string | number, | ||
callback: Function | ||
): () => void |
11
index.js
@@ -7,11 +7,11 @@ /** | ||
* | ||
* @template {object} T | ||
* @param {T} context Context object | ||
* @param {string | number} name Field where the original method lives | ||
* @param {unknown} context Context object | ||
* @param {string|number} name Field where the original method lives | ||
* @param {Function} callback Interceptor | ||
*/ | ||
export function cept(context, name, callback) { | ||
/** @type {unknown} */ | ||
var original = context[name] | ||
// @ts-expect-error: assume indexable. | ||
const original = context[name] | ||
// @ts-expect-error: assume it was a function. | ||
context[name] = callback | ||
@@ -25,4 +25,5 @@ | ||
function stop() { | ||
// @ts-expect-error: this is fine. | ||
context[name] = original | ||
} | ||
} |
{ | ||
"name": "cept", | ||
"version": "2.0.1", | ||
"description": "Intercept method calls", | ||
"version": "2.0.2", | ||
"description": "Legacy package to intercept method calls", | ||
"license": "MIT", | ||
@@ -32,19 +32,17 @@ "keywords": [ | ||
"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": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"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" | ||
@@ -61,7 +59,3 @@ }, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -68,0 +62,0 @@ "remarkConfig": { |
# cept | ||
[![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 use a [`assert.CallTracker`][calltracker] or so. | ||
Intercept method calls. | ||
Legacy [documentation for this package][docs] is still available in Git. | ||
## Install | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
[npm][]: | ||
```sh | ||
npm install cept | ||
``` | ||
## Use | ||
```js | ||
import {cept} from 'cept' | ||
// Intercept: | ||
var stop = cept(console, 'log', noop) | ||
// From now on `console.log` is ignored: | ||
console.log('foo bar baz') | ||
// No longer ignored! | ||
stop() | ||
function noop() {} | ||
``` | ||
## API | ||
This package exports the following identifiers: `cept`. | ||
There is no default export. | ||
### `stop = cept(context, key, callback)` | ||
Intercept all calls to `context[key]` (e.g., `console.log`). | ||
From now on, when the original method would be called, `callback` is used | ||
instead. | ||
Calling `stop` reverts everything back to normal. | ||
###### Parameters | ||
* `context` (`Object`) — Context object | ||
* `key` (`string`) — Field where the original method lives | ||
* `callback` (`Function`) - Interceptor | ||
###### Returns | ||
`Function` — Used to revert back to the previous function. | ||
## License | ||
@@ -66,22 +17,8 @@ | ||
[build-badge]: https://github.com/wooorm/cept/workflows/main/badge.svg | ||
[license]: license | ||
[build]: https://github.com/wooorm/cept/actions | ||
[author]: https://wooorm.com | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/cept.svg | ||
[docs]: https://github.com/wooorm/cept/tree/365fba5 | ||
[coverage]: https://codecov.io/github/wooorm/cept | ||
[downloads-badge]: https://img.shields.io/npm/dm/cept.svg | ||
[downloads]: https://www.npmjs.com/package/cept | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/cept.svg | ||
[size]: https://bundlephobia.com/result?p=cept | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[license]: license | ||
[author]: https://wooorm.com | ||
[calltracker]: https://nodejs.org/api/assert.html#class-assertcalltracker |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8
4630
24