Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/igorskyflyer/npm-hook
๐ช Hooks onto a JavaScript prototype, either extending or changing its behavior or replacing it completely. ๐บ
https://github.com/igorskyflyer/npm-hook
back-end biome function hook igorskyflyer javascript js method node npm prototype ts typescript utility vitest
Last synced: 2 months ago
JSON representation
๐ช Hooks onto a JavaScript prototype, either extending or changing its behavior or replacing it completely. ๐บ
- Host: GitHub
- URL: https://github.com/igorskyflyer/npm-hook
- Owner: igorskyflyer
- License: mit
- Created: 2024-07-08T17:44:30.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-07-21T17:07:42.000Z (6 months ago)
- Last Synced: 2024-09-22T20:53:13.919Z (4 months ago)
- Topics: back-end, biome, function, hook, igorskyflyer, javascript, js, method, node, npm, prototype, ts, typescript, utility, vitest
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@igor.dvlpr/hook
- Size: 71.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Hook
๐ช Hooks onto a JavaScript prototype, either extending or changing its
behavior or replacing it completely ๐บ
๐ Support further development
I work hard for every project, including this one and your support means a lot to me!
Consider buying me a coffee. โ
Thank you for supporting my efforts! ๐๐
@igorskyflyer
## ๐ Table of contents
- [Usage](#-usage)
- [API](#-api)
- [hook()](#hook-boolean)
- [Examples](#-examples)
- [Changelog](#-changelog)
- [License](#-license)
- [Related](#-related)
- [Author](#-author)---
## ๐ต๐ผ Usage
Install it by executing:
```shell
npm i "@igor.dvlpr/hook"
```
## ๐คน๐ผ API
> [!CAUTION]
> This package provides ways of modifying the native prototype(s) of built-in JavaScript objects, use it only if you know what you're doing and with **caution** as it may cause unexpected results!
>
### hook(): boolean
```ts
function hook(
proto: Prototype,
method: string,
handler: NativeMethodHook,
replace: boolean = false
): boolean
```Hooks onto a JavaScript prototype in order to extend, modify or completely replace a given method of it.
#### `proto`
A prototype, e.g. `Array.prototype`, `Number.prototype`, etc.
#### `method`
A method to hook onto, e.g. `push` of `Array.prototype`.
#### `handler`
A custom function to run when the hooked method is called.
The function has the following signature:```ts
(
this: Type,
native: Type[Method],
...args: any[]
) => ReturnType
```
`this` will be resolved to the provided prototype.
`native` is the native method that's being overridden, with its original signature.
`...args: any[]` all other arguments passed after the `native` method.
`ReturnType` the return type of the handler is the same as the `native` method is.
#### `replace`
A Boolean indicating whether the prototype method should be replaced completely.
Defaults to **false**.
Returns a Boolean whether the hooking onto was successful.
---
## โจ Examples
```ts
import { hook } from '@igor.dvlpr/hook'hook(Array.prototype, 'unshift', function (native, x) {
// any code can be here,
// not just owned by the prototype
// you're hooking/replacingnative(512)
this.push(x / 2)// must adhere to the original method's
// return type
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift#return_value
return this.length // returns 3
})const array: number[] = []
array.unshift(256)
console.log(array) // [512, 256, 128]
```---
## ๐ Changelog
๐ Changelog is available here: [CHANGELOG.md](https://github.com/igorskyflyer/npm-hook/blob/main/CHANGELOG.md).
---
## ๐ชช License
Licensed under the MIT license which is available here, [MIT license](https://github.com/igorskyflyer/npm-hook/blob/main/LICENSE).
---
## ๐งฌ Related
[@igor.dvlpr/jmap](https://www.npmjs.com/package/@igor.dvlpr/jmap)
> _๐ถ๏ธ Reads a JSON file into a Map. ๐ป_
[@igor.dvlpr/strip-html](https://www.npmjs.com/package/@igor.dvlpr/strip-html)
> _๐ฅ Removes HTML code from the given string. Can even extract text-only from the given an HTML string. โจ_
[@igor.dvlpr/comment-it](https://www.npmjs.com/package/@igor.dvlpr/comment-it)
> _๐ Formats the provided string as a comment, either a single or a multi line comment for the given programming language. ๐ป_
[@igor.dvlpr/normalized-string](https://www.npmjs.com/package/@igor.dvlpr/normalized-string)
> _๐ NormalizedString provides you with a String type with consistent line-endings, guaranteed. ๐ฎ_
[@igor.dvlpr/keppo](https://www.npmjs.com/package/@igor.dvlpr/keppo)
> _๐ก Parse, manage, compare and output SemVer-compatible version numbers. ๐ก_
---
### ๐จ๐ปโ๐ป Author
Created by **Igor Dimitrijeviฤ** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).