Skip to content

nberlette/decorators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@decorators

Monorepo for packages published under the @decorators/* scope on JSR.


Packages

Create aliases between class members and simplify stack traces.

import { alias } from "@decorators/alias";

class Foo {
  // alias() can be used to create multiple aliases from one original member
  @alias("qux", "nurp")
  bar(): string {
    return "baz";
  }

  // declare the aliased members to avoid compilation errors
  declare qux: Foo["bar"];
  declare nurp: Foo["bar"];

  // or, use @alias.for on the alias itself and pass it the original member name.
  @alias.for("bar")
  baz(): string {
    return this.bar();
  }
}

const foo = new Foo();

console.assert(foo.bar === "baz"); // OK
console.assert(foo.bar === foo.baz); // OK
console.assert(foo.qux === foo.bar); // OK
console.assert(foo.nurp === foo.bar); // OK

Bind methods, getters, and setters to the appropriate context object, with support for static members and inheritance.

import { bind } from "@decorators/bind";

class Foo {
  @bind
  bar(): Foo {
    return this;
  }

  @bind
  static self(): typeof Foo {
    return this;
  }
}
const { self } = Foo, { bar } = new Foo();

console.log(self === Foo); // true
console.log(bar() instanceof Foo); // true

Highly configurable LRU cache decorator for class methods, with support for TTL, max size, custom key generation, pre- and post-processing, lifecycle event handlers, and much more.

import { lru } from "@decorators/lru";

class BasicExample {
  @lru({ maxSize: 64, ttl: 1000 })
  memoized(arg1: string, arg2: number): string {
    return `${arg1}-${arg2}`;
  }
}

const example = new BasicExample();
console.log(example.memoizedMethod("foo", 42)); // "foo-42"
console.log(example.memoizedMethod("foo", 42)); // "foo-42" (cached)

Contributing

Contributions are warmly welcomed! Please read the Contributing Guide for details on our code of conduct, and the process for submitting pull requests.

If you find a bug, please open an issue and we will get to it as soon as possible. Alternatively, if you feel up to fixing it yourself, please create the issue anyways (so we can track it) and submit a pull request with the fix!


Further Reading


MIT © Nicholas Berlette. All rights reserved.