Code Monkey home page Code Monkey logo

bitweiser's Introduction

Bitweiser

A custom React Hooks library that gives you pretty state handling. Store your boolean states in number. Based on javascript bitwise.

Basic

on
0 -> 1
1 -> 1

off
0 -> 0
1 -> 0

check
0 -> 0
1 -> 1

toggle
0 -> 1
1 -> 0

invert
1001 -> 0110

Usage

import React, { useState } from 'react';
import { bitweiser } from 'bitweiser';

function Example() {
    
    // predefine some states
    const
        burger = 1 << 0, // 00000000 00000000 00000000 00000001
        search = 1 << 1; // 00000000 00000000 00000000 00000010
        
    const initialState = 0;
    // or const initialState = (burger | search);
    
    // state handling api
    const {
        state,
        setState,
        check,
        on,
        off,
        toggle,
        invert,
    } = bitweiser(useState(initialState));
    
    const myMapping = {
        [burger | search]: 'menu and search are opened',
        [burger]: 'only menu is opened',
        [search]: 'only search is opened',
    };
    
    return (
        <div>
            <div onClick={() => toggle(burger)}>toggle menu</div>
            <div onClick={() => off(burger)}>close menu</div>
            <div onClick={() => on(burger)}>open menu</div>
            <div onClick={() => on(burger | search)}>open all</div>
            <div onClick={() => off(burger)}>close menu</div>
            <div onClick={() => invert()}>invert state</div>
            <div onClick={() => setState(0)}>close all</div>
            <div onClick={() => setState(burger)}>close all and opened menu is the next state</div>
            
            <div>menu is {check(burger) ? 'opened' : 'closed'}</div>
            <div>menu or search is {check(burger | search) ? 'opened' : 'closed'}</div>
            <div>menu or search is {check(burger) || check(search) ? 'opened' : 'closed'}</div>
            <div>menu and search are {check(burger) && check(search) ? 'opened' : 'closed'}</div>
            
            <div>mapping example: {myMapping[state]}</div>
        </div>
    );
}

License

MIT © Alexey Ozerov

bitweiser's People

Contributors

13luck avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.