Skip to content

Commit

Permalink
fix: improve type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
snickbit committed May 16, 2023
1 parent 396a63b commit f43b5fb
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions packages/cycle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import {arrayShuffle, isString} from '@snickbit/utilities'
import * as presets from './presets'

export class Cycle {
#started: boolean
export type Preset = keyof typeof presets

items: any[] = []
export class Cycle<T = any> {
protected started: boolean

index: number
protected items: T[] = []

constructor(items?: any[])
constructor(preset?: string)
constructor(itemsOrPreset?: any[] | string)
constructor(itemsOrPreset?: any[] | string) {
this.index = 0
protected index = 0

constructor(items?: T[])
constructor(preset?: Preset)
constructor(itemsOrPreset?: Preset | T[])
constructor(itemsOrPreset?: Preset | T[]) {
if (!itemsOrPreset) {
this.items = []
} else if (Array.isArray(itemsOrPreset)) {
this.items = [...itemsOrPreset]
} else if (isString(itemsOrPreset)) {
this.items = presets[itemsOrPreset as keyof typeof presets]
this.items = presets[itemsOrPreset] as T[]
} else {
throw new TypeError('Invalid type for items, expected array or string')
}
Expand Down Expand Up @@ -48,12 +49,12 @@ export class Cycle {
if (save) {
this.index = index
}
this.#started = true
this.started = true
return this.items[index]
}

next(save?: boolean) {
return this.getIndex(this.#started ? this.nextIndex : this.currentIndex, save)
return this.getIndex(this.started ? this.nextIndex : this.currentIndex, save)
}

prev(save?: boolean) {
Expand All @@ -76,15 +77,15 @@ export class Cycle {
return this.getIndex(index % this.items.length, save)
}

set(index: number, value: any) {
set(index: number, value: T) {
this.items[index] = value
}

push(value: any) {
push(value: T) {
this.items.push(value)
}

remove(value: any) {
remove(value: T) {
this.items.splice(this.items.indexOf(value), 1)
}

Expand All @@ -93,8 +94,8 @@ export class Cycle {
}
}

export function cycle(items?: any[]): Cycle
export function cycle(prefix?: string): Cycle
export function cycle(itemsOrPreset?: any[] | string) {
return new Cycle(itemsOrPreset)
export function cycle<T = any>(items?: T[]): Cycle
export function cycle(prefix?: Preset): Cycle
export function cycle<T = any>(itemsOrPreset?: T[] | string): Cycle<T> {
return new Cycle<T>(itemsOrPreset as any)
}

0 comments on commit f43b5fb

Please sign in to comment.