12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- type allergyMap = { [key: number]: string };
- let allergies:allergyMap = {
- 1: 'eggs',
- 2: 'peanuts',
- 4: 'shellfish',
- 8: 'strawberries',
- 16: 'tomatoes',
- 32: 'chocolate',
- 64: 'pollen',
- 128: 'cats',
- };
- export class Allergies {
- private ids = 0;
- constructor(allergenIndex: number) {
- allergenIndex &= 255;
- let i = 1;
- while (i <= 128) {
- if ((i & allergenIndex) !== 0) {
- this.ids |= i;
- }
- i <<= 1;
- }
- }
- public list(): string[] {
- let i = 1;
- let res: string[] = [];
- while (i <= 128) {
- if ((i & this.ids) !== 0) {
- res.push(allergies[i])
- }
- i <<= 1;
- }
- return res;
- }
- public allergicTo(allergen: string): boolean {
- return this.list().indexOf(allergen) >= 0;
- }
- }
|