Skip to content

Commit

Permalink
Add TypeScript definition (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 9, 2019
1 parent ff5a75b commit ea423b2
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
90 changes: 90 additions & 0 deletions index.d.ts
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;
37 changes: 37 additions & 0 deletions index.test-d.ts
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());
}
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"cycle",
Expand All @@ -34,7 +35,8 @@
"iterator"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ cycled.previous();

## API

### `cycled = new Cycled(input)`
### `cycled = new Cycled(array)`

Initiates an array subclass with the methods documented below. Since it's an array, you can use all the normal array methods on it.

#### input
#### array

Type: `Array`

The array to wrap.

### cycled

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.
Expand Down

0 comments on commit ea423b2

Please sign in to comment.