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

@nejs/extension

v2.20.0

Published

A small library for extending existing JavaScript

Downloads

17

Readme

@nejs/extension

Overview

The Nyteshade Enterprises JavaScript library extension provides two primary classes for extending the usefulness of code that you do not wish to hack into the global namespace. There are two general approaches using this library

Extension

The Extension class allows you to replace an entire property on some parent or owning object. By default this is globalThis which is equivalent to global in nodejs and window in a browser.

This allows you to introduce entirely new objects or replace single properties on an existing Object. So, as an example, if you wanted to introduce a new global Descriptor object you might approach that in the following manner.

const DescriptorExtension = new Extension(class Descriptor {
  constructor(configurable = true, enumerable = true) {
    Object.assign(this, { configurable, enumerable })
  }

  makeAccessor(getter, setter) {
    delete this.writable
    delete this.value
    Object.assign(this, { get: getter, set: setter })
    return this
  }

  makeData(value, writable = true) {
    delete this.get
    delete this.set
    Object.assign(this, { value, writable })
    return this
  }
})

console.log(Descriptor) // undefined
DescriptorExtension.apply()
console.log(Descriptor) // [class Descriptor]
DescriptorExtension.revert()
console.log(Descriptor) // undefined

Patch

The Patch class, another core component of the @nejs/extension library, provides a versatile way to apply and revert modifications to properties or methods of an existing object. This is especially useful when you need to temporarily change the behavior of an object without permanently affecting its original state. Below are some examples demonstrating how to use the Patch class.

Basic Usage of Patch

In this example, we'll demonstrate how to patch an existing object's method:

import { Patch } from '@nejs/extension';

const myObject = {
  greet: () => "Hello, World!"
};

// Display original behavior
console.log(myObject.greet()); // "Hello, World!"

// Create a patch
const greetingPatch = new Patch(myObject, {
  greet: () => "Hello, Universe!"
});

// Apply the patch
greetingPatch.apply();
console.log(myObject.greet()); // "Hello, Universe!"

// Revert to original
greetingPatch.revert();
console.log(myObject.greet()); // "Hello, World!"

Patching with Conflict Resolution

The Patch class can handle conflicts gracefully when the property to be patched already exists on the target object. Here's an example:

const myCalculator = {
  add: (a, b) => a + b
};

// Original functionality
console.log(myCalculator.add(2, 3)); // 5

// Patch to change the behavior
const calculatorPatch = new Patch(myCalculator, {
  add: (a, b) => a * b // Changing addition to multiplication
});

calculatorPatch.apply();
console.log(myCalculator.add(2, 3)); // 6

// Revert the patch
calculatorPatch.revert();
console.log(myCalculator.add(2, 3)); // 5

Installation

To use @nejs/extension in your project, you can install it via npm:

npm install @nejs/extension

Contributing

Contributions to @nejs/extension are welcome! Please ensure that your contributions adhere to the following guidelines:

  • Write clear, readable, and maintainable code.
  • Ensure backward compatibility or provide a clear migration path.
  • Add unit tests for new features or bug fixes.
  • Update documentation to reflect changes in the codebase.

For more details, see the CONTRIBUTING.md file in the repository.

License

@nejs/extension is licensed under the MIT License.