Skip to content

Commit

Permalink
Merge pull request #65 from rabbitholegg/add_recursion_to_bitmask
Browse files Browse the repository at this point in the history
  • Loading branch information
Quazia authored Jan 18, 2024
2 parents f200c56 + 938af45 commit 77d76e4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/empty-pans-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk": minor
---

Adds numerical operator support to bitmask operator
12 changes: 12 additions & 0 deletions src/filter/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ describe('parser', () => {
expect(handleBitmask(context, filter)).toBe(false)
})

test('applies the bitmask and compares the result to the provided value with numerical operators', () => {
const context = 0b1100
const filter = { bitmask: 0b0100, value: { $gte: 0b0100n } }
expect(handleBitmask(context, filter)).toBe(true)
})

test('returns false when the masked context does not equal the provided value with numerical operators', () => {
const context = 0b1100
const filter = { bitmask: 0b1100, value: { $gte: 0b11100n } }
expect(handleBitmask(context, filter)).toBe(false)
})

test('correctly handles large numbers', () => {
const context = '0x123456789abcdef0123456789abcdef0123456789abcdef'
const filter = {
Expand Down
3 changes: 3 additions & 0 deletions src/filter/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ export const handleRegex = (context: any, filter: string): boolean => {
*/
export const handleBitmask = (context: any, filter: BitmaskFilter): boolean => {
const maskedContext = BigInt(context) & BigInt(filter.bitmask)
if (typeof filter.value === 'object') {
return apply(maskedContext as any, filter.value as FilterObject)
}
return maskedContext === BigInt(filter.value)
}

Expand Down
2 changes: 1 addition & 1 deletion src/filter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type FilterObject = {
}
export type BitmaskFilter = {
bitmask: bigint | number | string
value: bigint | number | string
value: bigint | number | string | NumericOperator
}
export type Filter = Primitive | FilterObject | FilterArray | Abi
export type FilterArray = Filter[]
Expand Down

0 comments on commit 77d76e4

Please sign in to comment.