Repository navigation
Expand file tree
/
Copy pathhelpers.ts
More file actions
40 lines (34 loc) · 1.67 KB
/
Copy pathhelpers.ts
File metadata and controls
40 lines (34 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as definitions from "./definitions";
import type { Bit, BitField, ConsentState, ConsentStateCode, ConsentType } from "./types";
export function readCharValues(gcd: string): number[] {
return gcd
.slice(0, definitions.GCD_LENGTH)
.split("")
.map((char, position) => {
const value = definitions.ALPHABET.indexOf(char);
if (value < 0) throw new Error(`Invalid gcd character at position ${position}: "${char}"`);
return value;
});
}
export function readBits(value: number, offset: number, count: 1): Bit;
export function readBits(value: number, offset: number, count: 2): ConsentStateCode;
export function readBits(value: number, offset: number, count: number): number;
export function readBits(value: number, offset: number, count: number): number {
return (value >> offset) & ((1 << count) - 1);
}
export function readField(chars: number[], field: BitField<1>): Bit;
export function readField(chars: number[], field: BitField<2>): ConsentStateCode;
export function readField(chars: number[], { position, offset, bits }: BitField): number {
return readBits(chars[position], offset, bits);
}
export function readState(value: number, offset: number): ConsentState | undefined {
return definitions.STATE_BY_CODE[readBits(value, offset, definitions.CONSENT_CODE_BITS)];
}
export function mapConsentTypes<T>(build: (type: ConsentType) => T): Record<ConsentType, T> {
const result = {} as Record<ConsentType, T>;
for (const type of definitions.CONSENT_TYPES) result[type] = build(type);
return result;
}
export function toGranted(state: ConsentState | undefined): boolean | undefined {
return state === undefined ? undefined : state === "granted";
}