-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff5a75b
commit ea423b2
Showing
4 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
declare class Cycled<T> extends Array<T> { | ||
/** | ||
Initiates an array subclass with the methods documented below. | ||
Since it's an array, you can use all the normal array methods on it. | ||
The instance is an iterable that will cycle through the array. | ||
It will cycle through the number of elements equaling the length of the array from the current index. | ||
@example | ||
``` | ||
import Cycled = require('cycled'); | ||
const numberCycle = new Cycled([1,2,3,4,5]); | ||
console.log(...numberCycle); | ||
//=> 1 2 3 4 5 | ||
class TabComponent { | ||
constructor(views) { | ||
this.activeView = views[0]; | ||
this.views = new Cycled(views); | ||
} | ||
setActiveView(view) { | ||
this.activeView = view; | ||
this.views.index = this.views.indexOf(view); | ||
} | ||
nextView() { | ||
setActiveView(this.views.next()); | ||
} | ||
previousView() { | ||
setActiveView(this.views.previous()); | ||
} | ||
} | ||
const tabs = new TabComponent([ | ||
'Homepage', | ||
'Blog', | ||
'Projects', | ||
'Contact' | ||
]); | ||
// … | ||
nextButton.addEventListener('click', () => { | ||
tabs.nextView(); | ||
}); | ||
``` | ||
*/ | ||
constructor(array: T[]); | ||
|
||
/** | ||
Get or set the current index. | ||
*/ | ||
index: number; | ||
|
||
/** | ||
Returns the current item. | ||
*/ | ||
current(): T; | ||
|
||
/** | ||
Returns the next item. | ||
*/ | ||
next(): T; | ||
|
||
/** | ||
Returns the previous item. | ||
*/ | ||
previous(): T; | ||
|
||
/** | ||
Returns the item by going the given amount of `steps` through the array. For example, calling `step(2)` is like calling `next()` twice. You go backward by specifying a negative number. | ||
*/ | ||
step(steps: number): T; | ||
|
||
/** | ||
Returns an iterable that will cycle through the array indefinitely. | ||
*/ | ||
indefinitely(): IterableIterator<T>; | ||
|
||
/** | ||
Returns an iterable that will cycle through the array backward indefinitely. | ||
*/ | ||
indefinitelyReversed(): IterableIterator<T>; | ||
} | ||
|
||
export = Cycled; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {expectType} from 'tsd'; | ||
import Cycled = require('.'); | ||
|
||
const cycled = new Cycled([1, 2, 3]); | ||
expectType<Cycled<number>>(cycled); | ||
expectType<number>(cycled.index); | ||
cycled.index = 1; | ||
expectType<number>(cycled.current()); | ||
expectType<number>(cycled.next()); | ||
expectType<number>(cycled.previous()); | ||
expectType<number>(cycled.step(10)); | ||
expectType<IterableIterator<number>>(cycled.indefinitely()); | ||
expectType<IterableIterator<number>>(cycled.indefinitelyReversed()); | ||
expectType<number[]>([...cycled]); | ||
|
||
class TabComponent { | ||
views: Cycled<string>; | ||
activeView: string; | ||
|
||
constructor(views: string[]) { | ||
this.activeView = views[0]; | ||
this.views = new Cycled(views); | ||
} | ||
|
||
setActiveView(view: string) { | ||
this.activeView = view; | ||
this.views.index = this.views.indexOf(view); | ||
} | ||
|
||
nextView() { | ||
this.setActiveView(this.views.next()); | ||
} | ||
|
||
previousView() { | ||
this.setActiveView(this.views.previous()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters