All Projects → sindresorhus → Cycled

sindresorhus / Cycled

Licence: mit
Cycle through the items of an array

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cycled

Do Not Disturb Cli
Control the macOS `Do Not Disturb` feature from the command-line
Stars: ✭ 205 (-12.77%)
Mutual labels:  npm-package
Bitcoin Chart Cli
Bitcoin chart for the terminal as command line util
Stars: ✭ 221 (-5.96%)
Mutual labels:  npm-package
Unstated Debug
Debug your Unstated containers with ease
Stars: ✭ 231 (-1.7%)
Mutual labels:  npm-package
Node Cli Boilerplate
Boilerplate to kickstart creating a Node.js command-line tool
Stars: ✭ 209 (-11.06%)
Mutual labels:  npm-package
Typy
Minimal JavaScript type checking library
Stars: ✭ 215 (-8.51%)
Mutual labels:  npm-package
Ts ci
✅ Continuous integration setup for TypeScript projects via GitHub Actions.
Stars: ✭ 225 (-4.26%)
Mutual labels:  npm-package
Aperture Node
Record the screen on macOS from Node.js
Stars: ✭ 199 (-15.32%)
Mutual labels:  npm-package
Stimulus Components
A modern Stimulus library delivering common JavaScript behaviors with a bunch of customizable controllers.
Stars: ✭ 234 (-0.43%)
Mutual labels:  npm-package
Pinyinlite
Lightweight and Lightning-Fast ⚡️ Pinyin Library for JavaScript
Stars: ✭ 218 (-7.23%)
Mutual labels:  npm-package
Node Virtualbox
A JavaScript Library for Interacting with VirtualBox
Stars: ✭ 231 (-1.7%)
Mutual labels:  npm-package
Kjbannerviewdemo
轮播图无限自动循环滚动、缩放布局、自带缓存加载读取、支持自定义继承、定制特定样式、动态图和网图混合轮播、支持在Storyboard和Xib中创建并配置其属性、多种滚动方向选择、多种分页控件选择等等
Stars: ✭ 206 (-12.34%)
Mutual labels:  cycle
Abort Controller
An implementation of WHATWG AbortController interface.
Stars: ✭ 213 (-9.36%)
Mutual labels:  npm-package
React Npm Boilerplate
Boilerplate for creating React Npm packages with ES2015
Stars: ✭ 226 (-3.83%)
Mutual labels:  npm-package
Netlify Cms
A Git-based CMS for Static Site Generators
Stars: ✭ 14,776 (+6187.66%)
Mutual labels:  npm-package
Pupa
Simple micro templating
Stars: ✭ 231 (-1.7%)
Mutual labels:  npm-package
Filter Console
Filter out unwanted `console.log()` output
Stars: ✭ 203 (-13.62%)
Mutual labels:  npm-package
Eslint Plugin Eslint Comments
Additional ESLint rules for directive comments of ESLint.
Stars: ✭ 221 (-5.96%)
Mutual labels:  npm-package
Terminal Image Cli
Display images in the terminal
Stars: ✭ 234 (-0.43%)
Mutual labels:  npm-package
Nodehun
The Hunspell binding for NodeJS that exposes as much of Hunspell as possible and also adds new features. Hunspell is a first class spellcheck library used by Google, Apple, and Mozilla.
Stars: ✭ 229 (-2.55%)
Mutual labels:  npm-package
Json2typescript
Convert JSON to TypeScript with secure type checking!
Stars: ✭ 230 (-2.13%)
Mutual labels:  npm-package

cycled

Cycle through the items of an array

This package can be useful for cycling through tabs, images of slideshows, etc.

Install

$ npm install cycled

Usage

const Cycled = require('cycled');

const cycled = new Cycled([1, 2, 3]);

cycled.current();
//=> 1

cycled.next();
//=> 2

cycled.next();
//=> 3

cycled.next();
//=> 1

cycled.previous();
//=> 3

API

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.

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.

const numberCycle = new Cycled([1, 2, 3, 4, 5]);

console.log(...numberCycle);
//=> 1 2 3 4 5

current()

Returns the current item.

next()

Returns the next item.

previous()

Returns the previous item.

step(steps)

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.

peek(steps)

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.

This method is similar to .step() but without changing the current item.

index

Get or set the current index.

indefinitely()

Returns an iterable that will cycle through the array indefinitely.

indefinitelyReversed()

Returns an iterable that will cycle through the array backward indefinitely.

Example

Here we create a simple tab component that can have the active view set or go forward/backward through the tabs.

const Cycled = require('cycled');

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();
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].