-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQRCodeText.ts
61 lines (49 loc) · 1.34 KB
/
QRCodeText.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import QRCodeRaw from './QRCodeRaw';
import type { OptionsType as ParentOptionsType } from './QRCodeRaw';
export type OptionsType = ParentOptionsType & {
blackSymbol: string;
whiteSymbol: string;
};
const DEFAULT_OPTIONS = {
blackSymbol: '▓▓',
whiteSymbol: ' ',
};
export default class QRCodeText extends QRCodeRaw {
blackSymbol: string;
whiteSymbol: string;
qrCodeText: string | undefined | null;
constructor(value: string, options: Partial<OptionsType> = {}) {
super(value, options);
const params = { ...DEFAULT_OPTIONS, ...options };
this.blackSymbol = params.blackSymbol;
this.whiteSymbol = params.whiteSymbol;
}
_clearCache(): void {
super._clearCache();
this.qrCodeText = null;
}
toString(): null | string {
if (this.qrCodeText) {
return this.qrCodeText;
}
const dataSize = this.getDataSize();
if (!dataSize) {
return null;
}
const data = this.getData();
// FIXME eh?
if (data === null) {
return null;
}
const symbols: string[] = [];
for (let y = 0; y < dataSize; y += 1) {
for (let x = 0; x < dataSize; x += 1) {
const isBlack = data[y][x];
symbols.push(isBlack ? this.blackSymbol : this.whiteSymbol);
}
symbols.push('\n');
}
this.qrCodeText = symbols.join('');
return this.qrCodeText;
}
}