Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/glensc/node-cached_property
A cached_property decorator for Node and TypeScript
https://github.com/glensc/node-cached_property
javascript typescript
Last synced: about 2 months ago
JSON representation
A cached_property decorator for Node and TypeScript
- Host: GitHub
- URL: https://github.com/glensc/node-cached_property
- Owner: glensc
- License: bsd-3-clause
- Created: 2022-02-04T17:21:23.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-31T07:35:03.000Z (over 1 year ago)
- Last Synced: 2024-10-14T11:38:04.544Z (3 months ago)
- Topics: javascript, typescript
- Language: TypeScript
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cached_property for Node and TypeScript
A `@cached_property` decorator for Node.
This package implements `@cached_property` for Node,
inspired by Python 3.8 [functools.cached_property].The package also includes TypeScript typing.
[functools.cached_property]: https://docs.python.org/3/library/functools.html#functools.cached_property
# Usage
```shell
yarn add cached_property
``````ts
import { cached_property } from "cached_property";
```# Example
```ts
class CachedPropertyTest {
private readonly multiplier: number;constructor() {
this.multiplier = -Math.random() * 1000;
}@cached_property
public get seconds(): number {
return Math.random() * this.multiplier;
}@cached_property
public get more_seconds(): number {
return Math.random() * this.multiplier;
}
}const t1 = new CachedPropertyTest();
const t2 = new CachedPropertyTest();console.log('t1', t1);
console.log('t2', t2);console.log(t1.seconds);
console.log(t1.seconds);
console.log(t1.more_seconds);console.log(t2.seconds);
console.log(t2.seconds);
console.log(t2.more_seconds);
```prints:
```
t1 CachedPropertyTest { multiplier: -744.8363910755569 }
t2 CachedPropertyTest { multiplier: -740.5825190870856 }
-577.7473504371724
-577.7473504371724
-377.27881344400447
-139.17929406315005
-139.17929406315005
-648.857438477043
```