Skip to content

Instantly share code, notes, and snippets.

@allex
Created February 27, 2020 04:45
Show Gist options
  • Save allex/459aaa91c9b584e127f3f45890026303 to your computer and use it in GitHub Desktop.
Save allex/459aaa91c9b584e127f3f45890026303 to your computer and use it in GitHub Desktop.
// Uses bit operation to reduce your codes (by allex)
// GistID: 459aaa91c9b584e127f3f45890026303
const ADD = 4
const UPDATE = 2
const DELETE = 1
const checkFlag = (v, mask) => (v & mask) === mask
const print = (v, desc) => {
const s = `
value => ${v} (${desc})
ADD: ${checkFlag(v, ADD)}, UPDATE: ${checkFlag(v, UPDATE)}, DELETE: ${checkFlag(v, DELETE)}
`
console.log(s)
}
// (default) add, update, delete
let flg = ADD | UPDATE | DELETE
print(flg, 'add, update, delete')
// add & update
flg = ADD | UPDATE
print(flg, 'add & update')
// add & delete
flg = ADD | DELETE
print(flg, 'add & delete')
// update & delete
flg = UPDATE | DELETE
print(flg, 'update & delete')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment