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.

55 lines
932 B

3 years ago
3 years ago
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. first(): void {
  27. this.step = this.minStep
  28. }
  29. last(): void {
  30. this.step = this.maxStep
  31. }
  32. hasNext(): boolean {
  33. return this.step !== this.maxStep
  34. }
  35. hasPrev(): boolean {
  36. return this.step !== this.minStep
  37. }
  38. isFirst(): boolean {
  39. return this.step === this.minStep
  40. }
  41. isLast(): boolean {
  42. return this.step === this.maxStep
  43. }
  44. }