
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
lazy-get-decorator
Advanced tools
Lazily evaluates a getter on an object and caches the returned value
Getter decorator that memoises the return value
npm install lazy-get-decorator
import {LazyGetter} from 'lazy-get-decorator';
class MyClass {
@LazyGetter(/* optional config */)
get foo(): number {
// ...
}
@LazyGetter(/* optional config */)
static get bar(): string {
// ...
}
}
All options are optional
Name | Type | Default | Description |
---|---|---|---|
global | boolean | false | If set to true, the lazy getter triggering on one class instance will end up saving the returned value on all class instances, current and future. Has no effect on static getters. |
select | <T, R>(this: T, output: R, self: T) => any | A function to determine whether we should save the results (returning a truthy value) or continue calling the original getter (returning a falsy value). The default behaviour is to always save the 1st result. |
The following example will save the value only if the counter is 10
or above. The selector executes after the getter
function, hence the offset of 1: the current value of 9 is returned and passed on to the result selector, but the
counter value is already incremented at this point.
class MyClass {
#counter = 0;
@LazyGetter({select: v => v === 9})
get timesAccessed(): number {
return this.#counter++;
}
}
The library's only goal is to be compatible with Typescript 5 decorators which, at the time of writing, use the 2022-03 stage 3 decorators proposal. If you need experimental TS decorators or can't use a suitable Babel transform just stick to v2 - there are no new features in this release.
Typescript has further gotchas depending on the compiler target you've set.
ES2022 and higher allows you to use a class' method as a result selector:
class MyClass {
static shouldMemoiseStatic = false;
shouldMemoiseInstance = true;
@LazyGetter({select: MyClass.bar})
static get foo() {}
@LazyGetter({select: MyClass.prototype.bar2})
get foo2() {}
static bar() {
return this.shouldMemoiseStatic;
}
bar2() {
return this.shouldMemoiseInstance;
}
}
Attempting to do this on ES2021 or lower will result in a runtime error as of Typescript 5.2.
makeNonConfigurable
option removedsetProto
option renamed to global
FAQs
Lazily evaluates a getter on an object and caches the returned value
The npm package lazy-get-decorator receives a total of 19,123 weekly downloads. As such, lazy-get-decorator popularity was classified as popular.
We found that lazy-get-decorator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.