diff --git a/types/cycled/cycled-tests.ts b/types/cycled/cycled-tests.ts new file mode 100644 index 0000000000..f5945be629 --- /dev/null +++ b/types/cycled/cycled-tests.ts @@ -0,0 +1,53 @@ +import Cycled = require('cycled'); + +const cycled = new Cycled([1, 2, 3]); +// $ExpectType Cycled +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 Iterator +cycled.indefinitely(); + +// $ExpectType Iterator +cycled.indefinitelyReversed(); + +// $ExpectType number[] +[...cycled]; + +class TabComponent { + views: Cycled; + 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()); + } +} diff --git a/types/cycled/index.d.ts b/types/cycled/index.d.ts new file mode 100644 index 0000000000..a0940934b8 --- /dev/null +++ b/types/cycled/index.d.ts @@ -0,0 +1,58 @@ +// Type definitions for cycled 1.0 +// Project: https://github.com/sindresorhus/cycled#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export = Cycled; + +// tslint:disable:jsdoc-format +declare class Cycled extends 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. + * + * 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. + * ``` +const numberCycle = new Cycled([1,2,3,4,5]); + +console.log(...numberCycle); +//=> 1 2 3 4 5 +``` + * + * @param input + */ + constructor(input: 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. + * @param steps + */ + step(steps: number): T; + /** + * Returns an iterable that will cycle through the array indefinitely. + */ + indefinitely(): Iterator; + /** + * Returns an iterable that will cycle through the array backward indefinitely. + */ + indefinitelyReversed(): Iterator; +} diff --git a/types/cycled/tsconfig.json b/types/cycled/tsconfig.json new file mode 100644 index 0000000000..a8fb15e11e --- /dev/null +++ b/types/cycled/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cycled-tests.ts" + ] +} diff --git a/types/cycled/tslint.json b/types/cycled/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/cycled/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }