Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

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

License

Notifications You must be signed in to change notification settings

nberlette/decorators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

107 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@decorators

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


Packages

Creates aliases for existing class members, with support for methods, getters, setters, auto-accessors, and fields. Static members are also supported, along with #private members (in environments with support for ES2022 class fields).

Important

When working with private members, the @alias decorator must beß applied inside of the same enclosing class that the member is declared in. This is due to the way that private members are scoped in JavaScript.

Simplifies stack traces for improved debugging, improves code readability for a more maintainable codebase, and reduces the boilerplate typically associated with aliasing class members in TypeScript/JavaScript.

Install

Deno

deno add @decorators/alias

NPM

npx jsr add @decorators/alias

Usage

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.

Install

Deno

deno add @decorators/bind

NPM

npx jsr add @decorators/bind

Usage

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.

Install

Deno

deno add @decorators/lru

NPM

npx jsr add @decorators/lru

Usage

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)

Collection of type guard functions, decorator function signatures, decorator factory signatures, and other utility types for working with both Stage 3 and Legacy Decorators (Stage 2 / experimentalDecorators).

Install

Deno

deno add @decorators/types

NPM

npx jsr add @decorators/types

Usage

import {
  type AnyDecoratorArguments,
  type AnyDecoratorReturn,
  isDecoratorArguments,
  isLegacyDecoratorArguments,
} from "@decorators/types";

function toStringTag<Args extends AnyDecoratorArguments>(
  value: string,
): (...args: Args) => AnyDecoratorReturn<Args>;
// deno-lint-ignore no-explicit-any
function toStringTag(value: string): (...args: any[]) => any {
  return (...args) => {
    if (isDecoratorArguments(args)) {
      const [target, context] = args;
      if (context.kind !== "class") {
        throw new TypeError(
          `@toStringTag cannot decorate ${context.kind}s - it can only be used on the class itself.`,
        );
      }
      context.addInitializer(function () {
        Object.defineProperty(this.prototype, Symbol.toStringTag, {
          value,
          configurable: true,
        });
      });
    } else if (isLegacyDecoratorArguments(args)) {
      const [target] = args;
      Object.defineProperty(target.prototype, Symbol.toStringTag, {
        value,
        configurable: true,
      });
    } else {
      throw new TypeError("@toStringTag received invalid arguments");
    }
  };
}

// this decorator factory works in TS 4.x and 5.x without issue:
@toStringTag("Foo")
class Foo {
  // ...
}

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.

github · issues · jsr · docs · contributing

Sponsor this project

Packages

No packages published