@vkontakte/vk-qr 2.1.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/svg.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { QrCode } from './qr';
2
- import { QrOptions } from './types';
1
+ import type { QrCode } from './qr.ts';
2
+ import type { RequiredQrOptions } from './types.ts';
3
3
 
4
4
  const MULTI = 1;
5
5
  const QR_BORDER = 7;
@@ -12,7 +12,13 @@ const SMALL_QR_SIZE = 25;
12
12
  * for white or true for black. The top left corner has the coordinates (x=0, y=0).
13
13
  * If the given coordinates are out of bounds, then false (white) is returned.
14
14
  */
15
- const getPixel = (x: number, y: number, size: number, modules: boolean[][], isLogoShowed: boolean) => {
15
+ const getPixel = (
16
+ x: number,
17
+ y: number,
18
+ size: number,
19
+ modules: boolean[][],
20
+ isLogoShowed: boolean,
21
+ ) => {
16
22
  if (x < QR_BORDER && y < QR_BORDER) {
17
23
  return false;
18
24
  }
@@ -48,15 +54,21 @@ const getPixel = (x: number, y: number, size: number, modules: boolean[][], isLo
48
54
  return 0 <= x && x < size && 0 <= y && y < size && modules[y][x];
49
55
  }
50
56
 
51
- return modules[y] && modules[y][x];
57
+ return modules[y]?.[x];
52
58
  };
53
59
 
54
- const getNeighbors = (x: number, y: number, size: number, modules: boolean[][], isLogoShowed = true) => ({
60
+ const getNeighbors = (
61
+ x: number,
62
+ y: number,
63
+ size: number,
64
+ modules: boolean[][],
65
+ isLogoShowed = true,
66
+ ) => ({
55
67
  l: getPixel(x - 1, y, size, modules, isLogoShowed),
56
68
  r: getPixel(x + 1, y, size, modules, isLogoShowed),
57
69
  t: getPixel(x, y - 1, size, modules, isLogoShowed),
58
70
  b: getPixel(x, y + 1, size, modules, isLogoShowed),
59
- current: getPixel(x, y, size, modules, isLogoShowed)
71
+ current: getPixel(x, y, size, modules, isLogoShowed),
60
72
  });
61
73
 
62
74
  /**
@@ -64,7 +76,7 @@ const getNeighbors = (x: number, y: number, size: number, modules: boolean[][],
64
76
  * @param qrCode QrCode instance
65
77
  * @param options Convertation options
66
78
  */
67
- export const convertSegmentsToSvgString = (qrCode: QrCode, options: Required<QrOptions>): string => {
79
+ export const convertSegmentsToSvgString = (qrCode: QrCode, options: RequiredQrOptions): string => {
68
80
  if (typeof options.qrSize !== 'number') {
69
81
  throw new Error('Size should be a number');
70
82
  }
@@ -111,8 +123,10 @@ export const convertSegmentsToSvgString = (qrCode: QrCode, options: Required<QrO
111
123
  let selector = '';
112
124
 
113
125
  if (neighbors.current) {
114
- selector = !selector && !neighbors.l && !neighbors.r && !neighbors.t && !neighbors.b ? 'empty' : '';
115
- selector = (!selector && neighbors.l && neighbors.r) || (neighbors.t && neighbors.b) ? 'rect' : '';
126
+ selector =
127
+ !selector && !neighbors.l && !neighbors.r && !neighbors.t && !neighbors.b ? 'empty' : '';
128
+ selector =
129
+ (!selector && neighbors.l && neighbors.r) || (neighbors.t && neighbors.b) ? 'rect' : '';
116
130
  if (!selector) {
117
131
  selector += neighbors.l ? 'l' : neighbors.r ? 'r' : '';
118
132
  selector += neighbors.t ? 't' : neighbors.b ? 'b' : '';
@@ -168,12 +182,14 @@ export const convertSegmentsToSvgString = (qrCode: QrCode, options: Required<QrO
168
182
  }
169
183
 
170
184
  const pointPosition = (qrCode.size - QR_BORDER) * options.incTileSize;
171
- parts.push(`<use fill-rule="evenodd" transform="translate(0,0)" xlink:href="#point-${options.suffix}"/>`);
172
185
  parts.push(
173
- `<use fill-rule="evenodd" transform="translate(${pointPosition},0)" xlink:href="#point-${options.suffix}"/>`
186
+ `<use fill-rule="evenodd" transform="translate(0,0)" xlink:href="#point-${options.suffix}"/>`,
187
+ );
188
+ parts.push(
189
+ `<use fill-rule="evenodd" transform="translate(${pointPosition},0)" xlink:href="#point-${options.suffix}"/>`,
174
190
  );
175
191
  parts.push(
176
- `<use fill-rule="evenodd" transform="translate(0,${pointPosition})" xlink:href="#point-${options.suffix}"/>`
192
+ `<use fill-rule="evenodd" transform="translate(0,${pointPosition})" xlink:href="#point-${options.suffix}"/>`,
177
193
  );
178
194
 
179
195
  if (options.isShowLogo) {
package/src/types.ts CHANGED
@@ -3,59 +3,63 @@ export type QrOptions = {
3
3
  * Size of QR code
4
4
  * @default 128
5
5
  */
6
- qrSize?: number;
6
+ qrSize?: number | undefined;
7
7
 
8
8
  /**
9
9
  * Class name of root SVG element
10
10
  */
11
- className?: string;
11
+ className?: string | undefined;
12
12
 
13
13
  /**
14
14
  * Show VK logo in center of QR code
15
15
  * @default false
16
16
  */
17
- isShowLogo?: boolean;
17
+ isShowLogo?: boolean | undefined;
18
18
 
19
19
  /**
20
20
  * Show QR background
21
21
  * @default false
22
22
  */
23
- isShowBackground?: boolean;
23
+ isShowBackground?: boolean | undefined;
24
24
 
25
25
  /**
26
26
  * QR code background HEX color. Works if `isShowBackground` is enabled.
27
27
  * @default "#ffffff"
28
28
  */
29
- backgroundColor?: string;
29
+ backgroundColor?: string | undefined;
30
30
 
31
31
  /**
32
32
  * QR code HEX color
33
33
  */
34
- foregroundColor?: string;
34
+ foregroundColor?: string | undefined;
35
35
 
36
36
  /**
37
37
  * Color of logo
38
38
  * @default "#000000"
39
39
  */
40
- logoColor?: string;
40
+ logoColor?: string | undefined;
41
41
 
42
42
  /**
43
43
  * Reference to logo as a reference IRI
44
44
  */
45
- logoData?: string | null;
45
+ logoData?: string | null | undefined;
46
46
 
47
47
  /**
48
48
  * SVG elements id postfix
49
49
  */
50
- suffix?: string;
50
+ suffix?: string | undefined;
51
51
 
52
52
  /**
53
53
  * ECC Level
54
54
  */
55
- ecc?: number
55
+ ecc?: number | undefined;
56
56
 
57
57
  /**
58
58
  * Custom INC_TILE_SIZE
59
59
  */
60
- incTileSize?: number
60
+ incTileSize?: number | undefined;
61
61
  };
62
+
63
+ type NotUndefined<T> = { [k in keyof T]-?: Exclude<T[k], undefined> };
64
+
65
+ export type RequiredQrOptions = NotUndefined<QrOptions>;