Skip to content

Commit

Permalink
added toString() passthrough (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisChabot committed Apr 21, 2021
1 parent 2ca8c3f commit fa3a930
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ApiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export interface DynamicReaderBase<T> {
removeListener(lst: (v: T) => void): void;

readonly current: T;

/** gets string representation */
toString(): string;
}

/** Consumer API for a dynamic primitive. */
Expand Down
4 changes: 4 additions & 0 deletions src/dynamic/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export class DynamicBaseImpl<T> implements DynamicBase<T> {
}
}

toString(): string {
return `${this._value}`;
}

addListener(lst: (v: T) => void): void {
if (!this._notifier) {
this._notifier = [];
Expand Down
13 changes: 13 additions & 0 deletions tests/dynamicVal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ describe('Dynamic Value', () => {
expect(b).toBe(2);
expect(c).toBe(2);
});

it('stringizes correctly', () => {
const x = dynamic(12);
expect(`${x}`).toBe('12');
expect(`` + x).toBe('12');
});

it('valueof works', () => {
const x = dynamic(12) as any;
const y = x + 12;

expect(y).toBe(24);
});
});

describe('Dependant value', () => {
Expand Down

0 comments on commit fa3a930

Please sign in to comment.