You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
829 B

3 years ago
  1. export class StepCounter {
  2. private step: number
  3. constructor(
  4. private readonly minStep: number = 1,
  5. private readonly maxStep: number = 10
  6. ) {
  7. this.step = 1
  8. }
  9. static valueOf(
  10. minStep: number = 1, maxStep: number = 10
  11. ): StepCounter {
  12. return new StepCounter(minStep, maxStep)
  13. }
  14. get count(): number {
  15. return this.step
  16. }
  17. set count(val: number) {
  18. this.step = val
  19. }
  20. next(): void {
  21. this.step = Math.min(this.step + 1, this.maxStep)
  22. }
  23. prev(): void {
  24. this.step = Math.max(this.step - 1, this.minStep)
  25. }
  26. hasNext(): boolean {
  27. return this.step !== this.maxStep
  28. }
  29. hasPrev(): boolean {
  30. return this.step !== this.minStep
  31. }
  32. isFirst(): boolean {
  33. return this.step === this.minStep
  34. }
  35. isLast(): boolean {
  36. return this.step === this.maxStep
  37. }
  38. }