
Security News
/Research
npm Phishing Email Targets Developers with Typosquatted Domain
A phishing attack targeted developers using a typosquatted npm domain (npnjs.com) to steal credentials via fake login pages - watch out for similar scams.
lazy-get-decorator
Advanced tools
Lazily evaluates a getter on an object and caches the returned value
Previously known as typescript-lazy-get-decorator.
/**
* Evaluate the getter function and cache the result
* @param {boolean} [setProto=false] Set the value on the class prototype as well. Only applies to non-static getters.
* @param {boolean} [makeNonConfigurable=false] Set to true to make the resolved property non-configurable
* @param {ResultSelectorFn} [resultSelector] A filter function that must return true for the value to cached
* @return A Typescript decorator function
*/
function LazyGetter(setProto?: boolean, makeNonConfigurable?: boolean, resultSelector?: (value: any) => boolean): MethodDecorator;
import {LazyGetter} from 'lazy-get-decorator';
class AClass {
@LazyGetter()
get lazyNoProto(): string {
console.log('Evaluating lazyNoProto');
return 'lazyNoProtoValue';
}
@LazyGetter(true)
get lazyWithProto(): string {
console.log('Evaluating lazyWithProto');
return 'lazyWithProtoValue';
}
}
const inst1 = new AClass();
console.log('==== inst 1 ====\n');
console.log(inst1.lazyNoProto);
console.log(inst1.lazyNoProto);
console.log(inst1.lazyWithProto);
console.log(inst1.lazyWithProto);
const inst2 = new AClass();
console.log('\n\n==== inst 2 ====\n');
console.log(inst2.lazyNoProto);
console.log(inst2.lazyNoProto);
console.log(inst2.lazyWithProto);
console.log(inst2.lazyWithProto);
Output:
==== inst 1 ====
Evaluating lazyNoProto
lazyNoProtoValue
lazyNoProtoValue
Evaluating lazyWithProto
lazyWithProtoValue
lazyWithProtoValue
==== inst 2 ====
Evaluating lazyNoProto
lazyNoProtoValue
lazyNoProtoValue
lazyWithProtoValue
lazyWithProtoValue
import {LazyGetter} from 'lazy-get-decorator';
class MyClass {
public readonly someCondition = 10;
@LazyGetter(false, false, (v: number) => v === 10)
public get prop1(): number {
// This will get cached
return this.someCondition;
}
@LazyGetter(false, false, (v: number) => v === 1)
public get prop2(): number {
// This won't get cached
return this.someCondition;
}
}
The cached value can be reset if the decorator does not modify the class prototype,
i.e. is not called as @LazyGetter(true)
:
import {LazyGetter} from 'lazy-get-decorator';
const instanceDec = LazyGetter();
const staticDec = LazyGetter();
class MyClass {
public instanceCount = 0;
public static staticCount = 0;
@instanceDec
public get count(): number {
return this.instanceCount++;
}
@staticDec
public static get count(): number {
return MyClass.staticCount++;
}
}
const inst = new MyClass();
console.log(inst.count); // 0
console.log(inst.count); // 0
instanceDec.reset(inst);
console.log(inst.count); // 1
console.log(MyClass.count); // 0
console.log(MyClass.count); // 0
staticDec.reset(MyClass);
console.log(MyClass.count); // 1
Resetting the decoration performs the following steps:
This means that any descriptor changes made by other decorators may be lost, therefore you
should ensure LazyGetter
is applied last if you intend on resetting it, i.e. place it
at the very top of your decorators list:
class MyClass {
@LazyGetter()
@decorator2
@decorator1
get getter() {
return 1;
}
}
FAQs
Lazily evaluates a getter on an object and caches the returned value
The npm package lazy-get-decorator receives a total of 16,906 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
/Research
A phishing attack targeted developers using a typosquatted npm domain (npnjs.com) to steal credentials via fake login pages - watch out for similar scams.
Security News
Knip hits 500 releases with v5.62.0, refining TypeScript config detection and updating plugins as monthly npm downloads approach 12M.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.