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

bittersweet-flags

v1.0.9

Published

This package aims to work with bit flags with ease

Downloads

521

Readme

This library was originally made as a personal tool, but I decided to design it as a shareable package. This library aims to help managing bit flags so it can be easier to anyone to work with.

You can install it by simply running:

npm install bittersweet-flags

or

yarn install bittersweet-flags

or

pnpm add bittersweet-flags

How to setup the optionsets?

  1. This library act as a centralized repository for all your bitflag-related objects, so the first step is to import the library and then create this "repository"
import createOptionset from 'bittersweet-flags

const optionset = createOptionset({
  weekdays: {
    sun: 1 << 0, // 1
    mon: 1 << 1, // 2
    tue: 1 << 2, // 4
    wed: 1 << 3, // 8
    thu: 1 << 4, // 16
    fri: 1 << 5, // 32
    sat: 1 << 6  // 64
  }
})
  1. Now that we created our optionset object, we can create separated instances for each object.
/**
 * Since each instance is meant to perform immutable operations
 * I recommend to export each instance separatelly and just
 * import it where you need it
 */
export const weekbits = optionset({ of: 'weekdays' })

What operations can we perform with the created optionsets?

  • Adding a value.
  • Removing a value.
  • Toggling a value (Add or Remove).
  • Get labels for a given value.
  • Get computed value for a given combination of keys.
  • Get the value of a given key.
  • Check if a given value contains a given flag.
  • Check if a given value contains all of the given flags (check exact many).
  • Get intersection [keys | values | key value pairs | key value objects] between two given values

for all the next examples we will be reusing the created variables to demonstrate each usage.

Adding a value.

const incompleteWorkingDaysFromDB = 30 // mon, the, wed, and thu (missing friday)
const workingDays = weekbits.add(incompleteWorkingDaysFromDB, 'fri') // 62

Removing a value

const workingDaysButWednesday = weekbits.remove(workingDays, 'wed') // 54

Toggling a value

const fullWorkingDaysAgain = weekbits.toggle(workingDays, 'wed') // 62
const workingDaysButWednesdayAgain = weekbits.toogle(fullWorkingDaysAgian, 'wed') // 54

Get Labels for a given value

const labelsForValueFromDB = weekbits.labelsFor(incompleteWorkingDaysFromDB) // ['mon', 'tue', 'wed', 'thu']
const labelsForWorkingDaysButWednesday = weekbits.labelsFor(workingDaysButWednesday) // ['mon', 'tue', 'thu', 'fri']

Get computed value from a combination of keys

const weekend = weekdaybits.valueFor(['sun', 'sat']) // 65

Get the value of a given key

const wednesday = weekdaybits.valueOf('wed') // 8

Check if a given value contains a given flag

const workingDaysContainsMonday = weekdaybits.contains(workingdays, 'mon') // true
const incompleteWorkingDaysFromDBContainsFriday = weekdaybits.contains(incompleteWorkingDaysFromDB, 'fri') // false

Check if a given value contains all of the given flags (check exact many)

const workingDaysContainsMonAndWed = weekdaybits.containsAll(workingDays, ['mon', 'wed']) // true
const incompleteworkingDaysContainsMonAndFri = weekdaybits.containsAll(incompleteWorkingDaysFromDB, ['mon', 'fri']) // false

Get intersection [keys | values | key value pairs | key value objects] between two given values

const workingDaysOneDayStep = weekdaybits.valueFor(['mon', 'wed', 'fri'])
const intersectionValues = weekdaybits.intersections(workingDays, workingDaysOneDayStep) // [2, 8, 32]
const intersectionKeys = weekdaybits.intersections(workingDays, workingDaysOneDayStep, 'keys') // ['mon', 'wed', 'fri']
const intersectionKeyValuePairs = weekdaybits.intersections(workingDays, workingDaysOneDayStep, 'key-value-pairs') // [['mon', 2], ['wed', 8], ['fri', 32]]
const intersectionKeyValueObjects = weekdaybits.intersections(workingDays, workingDaysOneDayStep, 'key-value-object') // {mon: 2, wed: 8, fri: 32}