|
1 | | -export interface State { |
2 | | - get(key: string): string; |
3 | | - set(key: string, value: any): void; |
4 | | -} |
| 1 | +export default class Contract { |
| 2 | + private state: { value: number } = { value: 0 }; |
5 | 3 |
|
6 | | -export function reset(state: State) { |
7 | | - const newValue = 0; |
8 | | - state.set('value', newValue); |
9 | | - return newValue |
10 | | -} |
11 | | -export function inc(state: State, { x }: { x: number }) { |
12 | | - const oldValue = Number(state.get('value')) || 0; |
13 | | - const newValue = oldValue + x; |
14 | | - state.set('value', newValue); |
15 | | - return newValue |
16 | | -} |
17 | | -export function dec(state: State, { x }: { x: number }) { |
18 | | - const oldValue = Number(state.get('value')) || 0; |
19 | | - const newValue = oldValue - x; |
20 | | - state.set('value', newValue); |
21 | | - return newValue |
22 | | -} |
| 4 | + constructor() { } |
| 5 | + |
| 6 | + reset(): number { |
| 7 | + this.state.value = 0; |
| 8 | + return this.state.value; |
| 9 | + } |
| 10 | + |
| 11 | + init(): number { |
| 12 | + this.state.value = 0; |
| 13 | + return this.state.value; |
| 14 | + } |
| 15 | + |
| 16 | + inc(): number { |
| 17 | + const oldValue = this.state.value || 0; |
| 18 | + const newValue = oldValue + 1; |
| 19 | + this.state.value = newValue; |
| 20 | + return this.state.value; |
| 21 | + } |
| 22 | + |
| 23 | + dec(): number { |
| 24 | + const oldValue = this.state.value ?? 0; |
| 25 | + const newValue = oldValue - 1; |
| 26 | + this.state.value = newValue; |
| 27 | + return newValue; |
| 28 | + } |
| 29 | + |
| 30 | + read(): number { |
| 31 | + return this.state.value; |
| 32 | + } |
23 | 33 |
|
24 | | -export function read(state: State) { |
25 | | - return state.get('value'); |
26 | 34 | } |
0 commit comments