npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tau-switcher

v1.0.0

Published

A tool to define tabs and switch between them.

Downloads

3

Readme

Tau Switcher

A tool to define tabs and switch between them.

Usage

Require or import package first

// cjs
const { TabSwitcher, CycleSwitcher, BooleanSwitcher } = require('tau-switcher')

// esm
import { TabSwitcher, CycleSwitcher, BooleanSwitcher } from 'tau-switcher'

Tab Switcher

Create a switcher and use it to switch between tabs

const ts = new TabSwitcher()
ts.getTab() // null

ts.switch('a')
ts.getTab() // 'a'
ts.switch('a') // switch back to null
ts.getTab() // null

ts.switch('b')
ts.isTab(['a', 'b']) // true
ts.isTab('b') // true
ts.isTab(['c', 'd']) // false
ts.isTab('c') // false

ts.setTab('c')
ts.getTab() // 'c'

ts.reset()
ts.getTab() // null

Cycle Switcher

Use CycleSwitcher to create a switcher with pre-define tabs, then you can use #goNext, #goPrev, #goFirst, #goLast to switch tabs.

const cs = new CycleSwitcher({ tabs: ['a', 'b', 'c'] })
cs.getTab() // null

cs.goFirst()
cs.getTab() // 'a'
cs.goNext()
cs.getTab() // 'b'
cs.goNext()
cs.getTab() // 'c'
cs.goNext()
cs.getTab() // 'a'
cs.goPrev()
cs.getTab() // 'c'
cs.goPrev()
cs.getTab() // 'b'
cs.goPrev()
cs.getTab() // 'a'
cs.goLast()
cs.getTab() // 'c'

Use #isFirst, #isLast, #currentIndex to get the current tab information.

cs.setTab('a')
cs.getTab() // 'a'
cs.isFirst // true
cs.isLast // false
cs.currentIndex // 0

cs.setTab(null)
cs.getTab() // null
cs.isFirst // false
cs.isLast // false
cs.currentIndex // -1

You can disable the cycle behavior by given a parameter disableCycle: true when creating a cycle switcher, then the current tab would not be changed if you try to go to the next tab from the last tab or go to the previous tab from the first tab.

const cs = new CycleSwitcher({ tabs: ['a', 'b', 'c'], disableCycle: true })
cs.goFirst()
cs.getTab() // 'a'
cs.goPrev()
cs.getTab() // 'a'
cs.goLast()
cs.getTab() // 'c'
cs.goNext()
cs.getTab() // 'c'

CycleSwither has #switch, #getTab, #isTab as well, but the given tab must be pre-defined or null, otherwise the current tab would be not changed.

cs.switch('b')
cs.getTab() // 'b'
cs.switch('invalid_tab')
cs.getTab() // 'b'

Boolean Switcher

BooleanSwitcher has only two tabs - true and false. The #switch don't receive any tab parameter and just switch the current boolean value to the opposite one.

const bs = new BooleanSwitcher()
bs.getTab() // false

bs.switch()
bs.getTab() // true
bs.switch()
bs.getTab() // false
bs.switchOn()
bs.getTab() // true
bs.switchOff()
bs.getTab() // false

bs.setTab(true)
bs.isTab(true) // true
bs.isTab(false) // false

bs.setTab(true)
bs.getTab() // true
bs.setTab(false)
bs.getTab() // false

bs.reset()
bs.getTab() // false

BooleanSwitcher would try to covert (by !!tab ) tabs to boolean values before using them.

bs.setTab('a')
bs.getTab() // true
bs.setTab(null)
bs.getTab() // false

The initial tab

All switchers have a initial tab by default(tab/cycle switchers are null, boolean switchers are false). When doing switch-back or reset, the current tab would be set to the initial one. You can use the initTab parameter to change the initial tab when create a switcher.

const ts = new TabSwitcher({ initTab: 'a' })
ts.getTab() // 'a'
const cs = new CycleSwitcher({ tabs: ['a', 'b', 'c'], initTab: 'a' })
cs.getTab() // 'a'
const bs = new BooleanSwitcher({ initTab: true })
bs.getTab() // true

Test

npm test

Authors

Sibevin Wang

Copyright

Copyright (c) 2019 Sibevin Wang. Released under the MIT license.