Motivation
I was supposed to create a list of the days of the week from which an event would happen and I needed something simple. I didn't find anything that pleased me, so I noticed that the binaries could help me, and I hope they help you too, this was all very new to me. , I did not believe this would be possible only with NxN relations to solve my problem, so with the help of colleagues and friends, we identified that operator BITWISE would solve our problem with the days of the week and a special holiday day.
How Works
The library is given a number that is a sum of the active binaries relative to the table.
Table.
The days of the week are represented by each column of the table, the values being Sunday = 1, Monday = 2, Tuesday = 4, Wednesday = 8 ... From this it is possible to use the bitwise operator to divide this integer, thus extracting the active days of the week.
https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
Install
$ npm install --save weekjs
ES6
;;
Browser
WeeksJS!
Tables
Functions | Description |
---|---|
getDaysOfWeek() |
return { "day" : "monday", value: 1 } |
intToWeek( sum, options ) |
Pass as the sum of your binaries and receive an object representing the days of the week { "sunday" : true, monday:true,.. } |
weekToInt( object || array ) |
Search the binary values of the entered days and perform a sum. int |
intToWeek | Description |
---|---|
sum |
The binary sum of the selected days |
options.array |
Returns the Object Result changed to Array[Object] |
weekToInt | Description |
---|---|
object |
{ { "monday": true, "holiday": true} } |
array |
["monday", "holiday"] |
Use case (lang: PT-br)
Olá Ônibus - Application for transport public in Campo Mourão City.
End, Thank you for getting here Thanks Google translator