npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@krauters/monkey-patcher

v0.3.0

Published

Monkey Patcher is a package for modifying the behavior of functions or objects at runtime, without altering original code.

Downloads

507

Readme

Code Size Commits per Month Contributors Forks GitHub Stars Install Size GitHub Issues Last Commit License npm version Open PRs Repo Size Version visitors

@krauters/monkey-patcher

MonkeyPatcher is a powerful TypeScript utility designed to help developers extend class prototypes by adding new methods under a specified namespace. This approach enhances the flexibility and extensibility of codebases without altering the original class implementations. By encapsulating additional functionalities within a clear namespace, MonkeyPatcher promotes organized and maintainable code patching.

While monkey patching can be incredibly useful, it's crucial to avoid falling into common anti-patterns. Always ensure that the added functionalities are well-organized and do not interfere with existing methods, and please group them into reasonable namespaces to make their usage more intuitive.

npm install @krauters/monkey-patcher

Usage

To use MonkeyPatcher, first define the new methods you want to add to a target class and extend the class's interface with a unique namespace. Then, create a MonkeyPatcher instance with the class prototype and namespace, apply the patch, and access the new methods through the designated namespace on class instances.

// Usage Example: Patching String with MonkeyPatcher

import { MonkeyPatcher } from 'monkey-patcher'

// 1. Define new methods to add to String
const stringMethods = {
	reverse(this: String): string {
		return this.split('').reverse().join('')
	},
	capitalize(this: String): string {
		if (this.length === 0) return ''
		return this.charAt(0).toUpperCase() + this.slice(1)
	},
}

// 2. Extend the String interface to include the new namespace
declare global {
	interface String {
		monkey-patcher: {
			reverse(): string
			capitalize(): string
		}
	}
}

// 3. Create a MonkeyPatcher instance with the desired namespace
const stringPatcher = new MonkeyPatcher(String.prototype, 'monkey-patcher')

// 4. Patch the String class with the new methods
stringPatcher.patch(stringMethods)

// 5. Use the patched methods
const str = new String('hello world')

// Access existing method
console.log(str.toUpperCase()) // Output: HELLO WORLD

// Use patched methods under the 'monkey-patcher' namespace
console.log(str.monkey-patcher.reverse()) // Output: dlrow olleh
console.log(str.monkey-patcher.capitalize()) // Output: Hello world

Husky

Husky helps manage Git hooks easily, automating things like running tests or linting before a commit is made. This ensures your code is in good shape.

Pre-commit hooks run scripts before a commit is finalized to catch issues or enforce standards. With Husky, setting up these hooks across your team becomes easy, keeping your codebase clean and consistent.

Our Custom Pre-Commit Hook

This project uses a custom pre-commit hook to run npm run bundle. This ensures that our bundled assets are always up to date before any commit (which is especially important for TypeScript GitHub Actions). Husky automates this, so no commits will go through without a fresh bundle, keeping everything streamlined.

Contributing

The goal of this project is to continually evolve and improve its core features, making it more efficient and easier to use. Development happens openly here on GitHub, and we’re thankful to the community for contributing bug fixes, enhancements, and fresh ideas. Whether you're fixing a small bug or suggesting a major improvement, your input is invaluable.

License

This project is licensed under the ISC License. Please see the LICENSE file for more details.

🥂 Thanks Contributors

Thanks for spending time on this project.