Skip to content

Commit

Permalink
feat: rewrite bitmasks as a function
Browse files Browse the repository at this point in the history
BREAKING CHANGE

Replaces the BitMask class with the bitMask function.
  • Loading branch information
Arcath committed May 5, 2023
1 parent 15803fc commit 6c0e2e5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 102 deletions.
95 changes: 0 additions & 95 deletions src/classes/bit-mask.ts

This file was deleted.

14 changes: 7 additions & 7 deletions src/classes/bit-mask.spec.ts → src/functions/bit-mask.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BitMask} from './bit-mask'
import {bitMask} from './bit-mask'

const READ = 'read'
const WRITE = 'write'
Expand All @@ -10,7 +10,7 @@ const Bits: Permission[] = [READ, WRITE, EXECUTE]

describe('BitMask', () => {
it('should set values', () => {
const mask = new BitMask(0, Bits)
const mask = bitMask(0, Bits)

expect(mask.get(READ)).toBe(false)

Expand All @@ -26,7 +26,7 @@ describe('BitMask', () => {
})

it('should work with arrays', () => {
const mask = new BitMask(0, Bits)
const mask = bitMask(0, Bits)

expect(mask.asArray()).toHaveLength(0)

Expand All @@ -35,7 +35,7 @@ describe('BitMask', () => {
expect(mask.asArray()).toHaveLength(1)
expect(mask.asIndexArray()).toStrictEqual([0])

const mask2 = new BitMask(0, Bits)
const mask2 = bitMask(0, Bits)

mask2.fromIndexArray([1])

Expand All @@ -44,15 +44,15 @@ describe('BitMask', () => {
})

it('should have default values', () => {
const mask = new BitMask()
const mask = bitMask()

expect(mask.value).toBe(0)
expect(mask.value()).toBe(0)

//eslint-disable-next-line
expect((mask as any).get('test')).toBe(false)

//eslint-disable-next-line
;(mask as any).set('test')
expect(mask.value).toBe(0)
expect(mask.value()).toBe(0)
})
})
70 changes: 70 additions & 0 deletions src/functions/bit-mask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export const bitMask = <Bit>(initialValue: number = 0, bits: Bit[] = []) => {
let value = initialValue

const _get = (bit: number) => {
return !!((1 << bit) & value)
}

const _set = (bit: number, newValue: boolean) => {
if (newValue) {
if (!_get(bit)) {
value = (1 << bit) ^ value
}
} else if (_get(bit)) {
value = (1 << bit) ^ value
}
}

const get = (name: Bit) => {
const index = bits.indexOf(name)

if (index !== -1) {
return _get(index)
}

return false
}

const set = (name: Bit, value: boolean) => {
const index = bits.indexOf(name)

if (index !== -1) {
_set(index, value)
}
}

const asArray = () => {
return bits.filter(bit => {
return get(bit)
})
}

const asIndexArray = () => {
return asArray().map(bit => {
return bits.indexOf(bit)
})
}

const fromIndexArray = (indexs: number[]) => {
bits.forEach((bit, index) => {
if (indexs.includes(index)) {
set(bit, true)
} else {
set(bit, false)
}
})
}

return {
get,
set,
_get,
_set,
value: () => value,
asArray,
asIndexArray,
fromIndexArray
}
}

// {bitMask}

0 comments on commit 6c0e2e5

Please sign in to comment.