Skip to content

Commit

Permalink
fix: add general use methods
Browse files Browse the repository at this point in the history
  • Loading branch information
snickbit committed May 16, 2023
1 parent f43b5fb commit fc5c217
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/cycle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {arrayShuffle, isString} from '@snickbit/utilities'
import {arrayShuffle, isString, JSONStringify} from '@snickbit/utilities'
import * as presets from './presets'

export type Preset = keyof typeof presets
export type Preset = string & keyof typeof presets

export class Cycle<T = any> {
protected started: boolean
Expand Down Expand Up @@ -92,10 +92,26 @@ export class Cycle<T = any> {
shuffle() {
this.items = arrayShuffle(this.items)
}

values() {
return [...this.items]
}

indexes() {
return Object.keys(this.items).map(Number)
}

toJSON() {
return this.values()
}

toString() {
return JSONStringify(this.toJSON())
}
}

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

0 comments on commit fc5c217

Please sign in to comment.