@prismamedia/ts-memoize

3.0.0 • Public • Published

Typescript Memoize class method/getter decorator

npm version github actions status code style: prettier

It's almost a copy/paste of https://github.com/darrylhodgins/typescript-memoize, without the legacy part

Usage

import { doNotMemoize, Memoize } from '@prismamedia/ts-memoize';

class Foo {
  @Memoize()
  public get myCachedGetter(): string {
    return 'OK';
  }

  @Memoize()
  public myCachedMethod(): string {
    return 'OK';
  }

  // If not provided, the "hashFunction" takes the first argument
  @Memoize()
  public myCachedMethod(input: string): string {
    return input;
  }

  // The "hashFunction" can return anything valid as a "Map" key
  @Memoize((a: number, b: number) => [a, b].join('|'))
  public myCachedMethod(a: number, b: number): number {
    return a + b;
  }

  // The "hashFunction" can return the Symbol "doNotMemoize" in order to not memoize the result
  @Memoize((a: number, b: number) => a === b ? doNotMemoize : [a, b].join('|'))
  public myMaybeCachedMethod(a: number, b: number): number {
    return a + b;
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @prismamedia/ts-memoize

Weekly Downloads

4

Version

3.0.0

License

MIT

Unpacked Size

47.8 kB

Total Files

19

Last publish

Collaborators

  • prismamedia
  • yvann