
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
extension-methods
Advanced tools
This library allows you to create extension methods for a given object the fatest way
With this library, you can create 'proxy references' for your objects and access many methods that actually doesn't exists in them! This is pretty useful in the following scenarios:
Depending on the number of methods you'll proxy through extension-methods, you can achieve a 99% faster operation than a simple new Class()
First, you need to obtain your extension object, like this:
import { getExtender } from 'extension-methods';
export interface MyObjectExtended {
concatFoo(): MyObjectExtended;
concatBar(): MyObjectExtended;
}
export interface MyObject {
value: string;
}
export const myObjectExtension = getExtender({
concatFoo(this: MyObject) {
return extend({ value: `${this.value}_foo` }, myObjectExtension);
}
concatBar(this: MyObject) {
return extend({ value: `${this.value}_bar` }, myObjectExtension);
}
}
);
Now, take a look in the extend function called in the above code: this is the one who applies the extension!
The first parameter must be the definition of this, as it will be a reference to the object being extended, so, you can access it and do whatever you want!
So, when you need to extend methods in some object, just do it like this:
import { extend } from 'extension-methods';
import { myObjectExtension, MyObject, MyObjectExtended } from './my-object-extension';
...
...
const extended: MyObjectExtended = extend({ value: 'my string' }, myObjectExtension);
console.log(extended.concatFoo().value);
console.log(extended.concatBar().value);
console.log(extended.concatFoo().concatBar().value);
// Result:
// my string_foo
// my string_bar
// my string_foo_bar
Look that does methods actually are not present in the extended const (or in the original object), but extension-methods make it be accessible at runtime, making the call to extend being much faster!
Also, in the Extender implementation, the return of concatFoo and concatBar also applies the extend function to the result, which creates a fluent interface for this use, resulting in the chained call as you can see above!
All of that, with less overload possible!
You can also create extended version of Classes that will create instances of such classes with the exactly same benefits of an extended object. To achieve this, just use the method extendClass:
import * as examplePackage from './example-class';
import { extendClass, getExtender } from 'extension-methods';
declare module './example-class.spec' {
interface ExampleClass {
method1(): number;
}
}
const extender = getExtender({
method1(this: ExampleClass) {
return this.someProperty * 3;
},
});
export const ExtendedClass = extendClass(ExampleClass, extender);
Notice that an interface with the same name of the class is declared in the same module from where the class is imported. Doing this will make the new methods visible anywhere this code is imported.
Now, import your ExtendedClass where you want to use it!
import { ExtendedClass } './extended-class';
const test = new ExtendedClass();
console.log(test.method1());
And that's it, it'll just work!
FAQs
This library allows you to create extension methods for a given object the fatest way
The npm package extension-methods receives a total of 121 weekly downloads. As such, extension-methods popularity was classified as not popular.
We found that extension-methods demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.