-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(action): correctly apply bitmask for supported features
- Loading branch information
Showing
4 changed files
with
82 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Push an element to an array if it does not exist | ||
export function pushIfNotExist<T>(array: T[], element: T) { | ||
if (!array.includes(element)) { | ||
array.push(element); | ||
} | ||
} | ||
|
||
function compareBitmask( | ||
supported: number, | ||
required: (number | number[])[], | ||
): boolean { | ||
return required.some((req) => { | ||
if (Array.isArray(req)) { | ||
return req.every((r) => (supported & r) !== 0); | ||
} else { | ||
return (supported & req) !== 0; | ||
} | ||
}); | ||
} | ||
|
||
export function isFeatureSupported( | ||
bitmask: number, | ||
required: (number | number[])[], | ||
): boolean { | ||
if (required.length === 0) { | ||
return true; | ||
} | ||
return compareBitmask(bitmask, required); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { | ||
isFeatureSupported, | ||
pushIfNotExist, | ||
} from '../../../../src/editor/components/idSelector/utils'; | ||
|
||
describe('utils', function () { | ||
describe('pushIfNotExist', function () { | ||
it('should push element to array if it does not exist', function () { | ||
const array = [1, 2, 3]; | ||
pushIfNotExist(array, 4); | ||
expect(array).to.include(4); | ||
}); | ||
|
||
it('should not push element to array if it already exists', function () { | ||
const array = [1, 2, 3]; | ||
pushIfNotExist(array, 3); | ||
expect(array).to.have.lengthOf(3); | ||
}); | ||
}); | ||
|
||
describe('isFeatureSupported', function () { | ||
it('should return true if required array is empty', function () { | ||
expect(isFeatureSupported(0b1010, [])).to.be.true; | ||
}); | ||
|
||
it('should return true if bitmask supports all required features', function () { | ||
expect(isFeatureSupported(0b1010, [0b0010, 0b1000])).to.be.true; | ||
}); | ||
|
||
it('should return false if bitmask does not support any required features', function () { | ||
expect(isFeatureSupported(0b1010, [0b0100, 0b0001])).to.be.false; | ||
}); | ||
|
||
it('should return true if bitmask supports any features ', function () { | ||
expect(isFeatureSupported(0b1111, [[0b1000, 0b0100], 0b1000])).to.be | ||
.true; | ||
}); | ||
|
||
it('should return false if bitmask does not support all features in nested arrays', function () { | ||
expect(isFeatureSupported(0b1010, [[0b1000, 0b0100]])).to.be.false; | ||
}); | ||
}); | ||
}); |