Skip to content

Commit

Permalink
Bugfixed setter in @lazy decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
mlhaufe committed Oct 26, 2020
1 parent 010dd9d commit ed00c0e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@final-hill/class-tools",
"version": "1.2.0",
"version": "1.2.1",
"description": "Class Tools provides a number of utility functions and decorators to ease the use of features commonly found in functional languages",
"main": "dist/index.js",
"scripts": {
Expand Down
31 changes: 31 additions & 0 deletions src/lazy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('@lazy', () => {
void foo.bar;
void foo.bar;

expect(foo.bar).toBeDefined();
expect(Counter.usage).toBe(1);

Counter.usage = 99;
Expand All @@ -64,4 +65,34 @@ describe('@lazy', () => {

expect(Counter.usage).toBe(100);
});

test('@lazy getter only', () => {
class Foo {
private _leftThunk: () => number;
private _rightThunk: () => number;

constructor(
left: () => number,
right: () => number
) {
this._leftThunk = left;
this._rightThunk = right;
}

@lazy
get left(): number {
return this._leftThunk();
}

@lazy
get right(): number {
return this._rightThunk();
}
}

const alt = new Foo(() => 10, () => 20);

expect(alt.left).toBe(10);
expect(alt.right).toBe(20);
});
});
5 changes: 3 additions & 2 deletions src/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ function lazy(
/**
* The new setter
* @param {this} this - The target
* @param {any} value -
* @param {any} value - The value
* @returns {any} - The assigned value
*/
function set(this: typeof target, value: any): any {
_private = value;
return _private = value;
}

return { get, set };
Expand Down

0 comments on commit ed00c0e

Please sign in to comment.