├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export default class Cycled extends Array { 2 | /** 3 | Get or set the current index. 4 | */ 5 | index: number; 6 | 7 | /** 8 | Initiates an array subclass with the methods documented below. 9 | Since it's an array, you can use all the normal array methods on it. 10 | 11 | The instance is an iterable that will cycle through the array. 12 | It will cycle through the number of elements equaling the length of the array from the current index. 13 | 14 | @example 15 | ``` 16 | import Cycled from 'cycled'; 17 | 18 | const numberCycle = new Cycled([1, 2, 3, 4, 5]); 19 | 20 | console.log(...numberCycle); 21 | //=> 1 2 3 4 5 22 | 23 | class TabComponent { 24 | #activeView; 25 | #views; 26 | 27 | constructor(views) { 28 | this.#activeView = views[0]; 29 | this.#views = new Cycled(views); 30 | } 31 | 32 | setActiveView(view) { 33 | this.#activeView = view; 34 | this.#views.index = this.#views.indexOf(view); 35 | } 36 | 37 | nextView() { 38 | setActiveView(this.#views.next()); 39 | } 40 | 41 | previousView() { 42 | setActiveView(this.#views.previous()); 43 | } 44 | } 45 | 46 | const tabs = new TabComponent([ 47 | 'Homepage', 48 | 'Blog', 49 | 'Projects', 50 | 'Contact' 51 | ]); 52 | 53 | // … 54 | 55 | nextButton.addEventListener('click', () => { 56 | tabs.nextView(); 57 | }); 58 | ``` 59 | */ 60 | constructor(elements: readonly T[]); 61 | 62 | /** 63 | Returns the current item. 64 | */ 65 | current(): T; 66 | 67 | /** 68 | Returns the next item. 69 | */ 70 | next(): T; 71 | 72 | /** 73 | Returns the previous item. 74 | */ 75 | previous(): T; 76 | 77 | /** 78 | Returns the item by going the given amount of `steps` through the array. For example, calling `step(2)` is like calling `next()` twice. 79 | 80 | You go backward by specifying a negative number. 81 | */ 82 | step(steps: number): T; 83 | 84 | /** 85 | Returns the item that is located in the given amount of `steps` through the array. For example, calling `peek(2)` would get the item 2 items after the current one. 86 | 87 | You go backward by specifying a negative number. 88 | 89 | This method is similar to `.step()` but without changing the current item. 90 | */ 91 | peek(steps: number): T; 92 | 93 | /** 94 | Returns an iterable that will cycle through the array indefinitely. 95 | */ 96 | indefinitely(): IterableIterator; 97 | 98 | /** 99 | Returns an iterable that will cycle through the array backward indefinitely. 100 | */ 101 | indefinitelyReversed(): IterableIterator; 102 | } 103 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default class Cycled extends Array { 2 | #index = 0; 3 | 4 | constructor(array) { 5 | if (!Array.isArray(array)) { 6 | throw new TypeError('Expected an array'); 7 | } 8 | 9 | super(...array); 10 | } 11 | 12 | * [Symbol.iterator]() { 13 | let {length} = this; 14 | 15 | while (length-- > 0) { 16 | yield this.current(); 17 | this.index++; 18 | } 19 | } 20 | 21 | get index() { 22 | return this.#index; 23 | } 24 | 25 | set index(index) { 26 | this.#index = (this.length + (index % this.length)) % this.length; 27 | } 28 | 29 | step(steps) { 30 | this.#index = (this.length + this.#index + steps) % this.length; 31 | return this[this.#index]; 32 | } 33 | 34 | peek(steps) { 35 | return this[(this.length + this.#index + steps) % this.length]; 36 | } 37 | 38 | current() { 39 | return this.step(0); 40 | } 41 | 42 | next() { 43 | return this.step(1); 44 | } 45 | 46 | previous() { 47 | return this.step(-1); 48 | } 49 | 50 | * indefinitely() { 51 | while (true) { 52 | yield this.next(); 53 | } 54 | } 55 | 56 | * indefinitelyReversed() { 57 | while (true) { 58 | yield this.previous(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import Cycled from './index.js'; 3 | 4 | const cycled = new Cycled([1, 2, 3]); 5 | expectType>(cycled); 6 | expectType(cycled.index); 7 | cycled.index = 1; 8 | expectType(cycled.current()); 9 | expectType(cycled.next()); 10 | expectType(cycled.previous()); 11 | expectType(cycled.step(10)); 12 | expectType(cycled.peek(10)); 13 | expectType>(cycled.indefinitely()); 14 | expectType>(cycled.indefinitelyReversed()); 15 | expectType([...cycled]); 16 | 17 | class TabComponent { 18 | #activeView: string; 19 | #views: Cycled; 20 | 21 | constructor(views: readonly string[]) { 22 | this.#activeView = views[0]; 23 | this.#views = new Cycled(views); 24 | } 25 | 26 | setActiveView(view: string) { 27 | this.#activeView = view; 28 | this.#views.index = this.#views.indexOf(view); 29 | } 30 | 31 | nextView() { 32 | this.setActiveView(this.#views.next()); 33 | } 34 | 35 | previousView() { 36 | this.setActiveView(this.#views.previous()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cycled", 3 | "version": "2.0.0", 4 | "description": "Cycle through the items of an array", 5 | "license": "MIT", 6 | "repository": "sindresorhus/cycled", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "//test": "xo && ava && tsd", 20 | "test": "ava && tsd" 21 | }, 22 | "files": [ 23 | "index.js", 24 | "index.d.ts" 25 | ], 26 | "keywords": [ 27 | "cycle", 28 | "cycled", 29 | "iterable", 30 | "iterables", 31 | "array", 32 | "item", 33 | "next", 34 | "previous", 35 | "loop", 36 | "repeat", 37 | "value", 38 | "values", 39 | "iterator" 40 | ], 41 | "devDependencies": { 42 | "ava": "^3.15.0", 43 | "tsd": "^0.14.0", 44 | "xo": "^0.38.2" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # cycled 2 | 3 | > Cycle through the items of an array 4 | 5 | This package can be useful for cycling through tabs, images of slideshows, etc. 6 | 7 | ## Install 8 | 9 | ``` 10 | $ npm install cycled 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import Cycled from 'cycled'; 17 | 18 | const cycled = new Cycled([1, 2, 3]); 19 | 20 | cycled.current(); 21 | //=> 1 22 | 23 | cycled.next(); 24 | //=> 2 25 | 26 | cycled.next(); 27 | //=> 3 28 | 29 | cycled.next(); 30 | //=> 1 31 | 32 | cycled.previous(); 33 | //=> 3 34 | ``` 35 | 36 | ## API 37 | 38 | ### `cycled = new Cycled(array)` 39 | 40 | Initiates an array subclass with the methods documented below. Since it's an array, you can use all the normal array methods on it. 41 | 42 | #### array 43 | 44 | Type: `Array` 45 | 46 | The array to wrap. 47 | 48 | ### cycled 49 | 50 | 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. 51 | 52 | ```js 53 | import Cycled from 'cycled'; 54 | 55 | const numberCycle = new Cycled([1, 2, 3, 4, 5]); 56 | 57 | console.log(...numberCycle); 58 | //=> 1 2 3 4 5 59 | ``` 60 | 61 | #### current() 62 | 63 | Returns the current item. 64 | 65 | #### next() 66 | 67 | Returns the next item. 68 | 69 | #### previous() 70 | 71 | Returns the previous item. 72 | 73 | #### step(steps) 74 | 75 | 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. 76 | 77 | #### peek(steps) 78 | 79 | Returns the item that is located in the given amount of `steps` through the array. For example, calling `peek(2)` would get the item 2 items after the current one. You go backward by specifying a negative number. 80 | 81 | This method is similar to `.step()` but without changing the current item. 82 | 83 | #### index 84 | 85 | Get or set the current index. 86 | 87 | #### indefinitely() 88 | 89 | Returns an iterable that will cycle through the array indefinitely. 90 | 91 | #### indefinitelyReversed() 92 | 93 | Returns an iterable that will cycle through the array backward indefinitely. 94 | 95 | ## Example 96 | 97 | Here we create a simple tab component that can have the active view set or go forward/backward through the tabs. 98 | 99 | ```js 100 | import Cycled from 'cycled'; 101 | 102 | class TabComponent { 103 | #activeView; 104 | #views; 105 | 106 | constructor(views) { 107 | this.#activeView = views[0]; 108 | this.#views = new Cycled(views); 109 | } 110 | 111 | setActiveView(view) { 112 | this.#activeView = view; 113 | this.#views.index = this.views.indexOf(view); 114 | } 115 | 116 | nextView() { 117 | setActiveView(this.#views.next()); 118 | } 119 | 120 | previousView() { 121 | setActiveView(this.#views.previous()); 122 | } 123 | } 124 | 125 | const tabs = new TabComponent([ 126 | 'Homepage', 127 | 'Blog', 128 | 'Projects', 129 | 'Contact' 130 | ]); 131 | 132 | // … 133 | 134 | nextButton.addEventListener('click', () => { 135 | tabs.nextView(); 136 | }); 137 | ``` 138 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import Cycled from './index.js'; 3 | 4 | const fixture = [1, 2, 3]; 5 | 6 | test('.current()', t => { 7 | const cycled = new Cycled(fixture); 8 | t.is(cycled.current(), 1); 9 | cycled.next(); 10 | t.is(cycled.current(), 2); 11 | }); 12 | 13 | test('.next()', t => { 14 | const cycled = new Cycled(fixture); 15 | t.deepEqual([cycled.next(), cycled.next(), cycled.next(), cycled.next()], [2, 3, 1, 2]); 16 | }); 17 | 18 | test('.previous()', t => { 19 | const cycled = new Cycled(fixture); 20 | t.deepEqual([cycled.previous(), cycled.previous(), cycled.previous(), cycled.previous()], [3, 2, 1, 3]); 21 | }); 22 | 23 | test('.step()', t => { 24 | const cycled = new Cycled(fixture); 25 | t.is(cycled.step(2), 3); 26 | t.is(cycled.step(-2), 1); 27 | }); 28 | 29 | test('.peek()', t => { 30 | const cycled = new Cycled(fixture); 31 | t.is(cycled.peek(1), 2); 32 | t.is(cycled.peek(-2), 2); 33 | }); 34 | 35 | test('.index', t => { 36 | const cycled = new Cycled(fixture); 37 | t.is(cycled.index, 0); 38 | cycled.index = 2; 39 | t.is(cycled.index, 2); 40 | t.is(cycled.current(), 3); 41 | cycled.index = -7; 42 | t.is(cycled.index, 2); 43 | t.is(cycled.current(), 3); 44 | }); 45 | 46 | test('.indefinitely()', t => { 47 | const cycled = new Cycled(fixture); 48 | t.is(cycled.indefinitely().next().value, 2); 49 | }); 50 | 51 | test('.indefinitelyReversed()', t => { 52 | const cycled = new Cycled(fixture); 53 | t.is(cycled.indefinitelyReversed().next().value, 3); 54 | }); 55 | 56 | test('.indexOf()', t => { 57 | const cycled = new Cycled(fixture); 58 | t.is(cycled.indexOf(3), 2); 59 | }); 60 | 61 | test('iterable', t => { 62 | const cycled = new Cycled(fixture); 63 | t.is(cycled[Symbol.iterator]().next().value, 1); 64 | }); 65 | 66 | test('iterations on destructuring', t => { 67 | const cycled = new Cycled(fixture); 68 | t.deepEqual([...cycled], fixture); 69 | t.deepEqual([...cycled], fixture); 70 | cycled.next(); 71 | t.deepEqual([...cycled], [2, 3, 1]); 72 | }); 73 | --------------------------------------------------------------------------------