@vkontakte/vk-qr 2.0.13 → 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/index.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { convertSegmentsToSvgString } from './svg';
2
- import { QrSegment, QrCode } from './qr';
3
- import { QrOptions } from './types';
4
- import Ecc = QrCode.Ecc;
1
+ import { convertSegmentsToSvgString, INC_TILE_SIZE } from './svg.ts';
2
+ import { QrSegment, QrCode, QrCodeEcc } from './qr.ts';
3
+ import type { QrOptions, RequiredQrOptions } from './types.ts';
5
4
 
6
5
  /** Default width and height of QR code */
7
6
  const DEFAULT_SIZE = 128;
@@ -20,7 +19,7 @@ const LOGO_COLOR_DEFAULT = '#07f';
20
19
  * @param text String to encode
21
20
  * @param options QR code options
22
21
  */
23
- function createQR(text: string, options?: QrOptions): string;
22
+ export function createQR(text: string, options?: QrOptions): string;
24
23
 
25
24
  /**
26
25
  * Legacy interface
@@ -30,16 +29,21 @@ function createQR(text: string, options?: QrOptions): string;
30
29
  * @param className SVG element class name
31
30
  * @param options Options of the QR code
32
31
  */
33
- function createQR(text: string, qrSize: number, className: string, options?: QrOptions): string;
32
+ export function createQR(
33
+ text: string,
34
+ qrSize: number,
35
+ className: string,
36
+ options?: QrOptions,
37
+ ): string;
34
38
 
35
39
  /**
36
40
  * Implementation
37
41
  */
38
- function createQR(
42
+ export function createQR(
39
43
  text: string,
40
44
  qrSizeOrOptions?: number | QrOptions,
41
45
  classNameLegacy?: string,
42
- legacyOptions?: QrOptions
46
+ legacyOptions?: QrOptions,
43
47
  ): string {
44
48
  if (typeof text !== 'string') {
45
49
  throw new TypeError('Enter text for encoding');
@@ -50,45 +54,54 @@ function createQR(
50
54
  ...(typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null ? qrSizeOrOptions : {}),
51
55
  ...(typeof legacyOptions === 'object' && legacyOptions !== null ? legacyOptions : {}),
52
56
  qrSize:
53
- typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null && typeof qrSizeOrOptions.qrSize === 'number'
57
+ typeof qrSizeOrOptions === 'object' &&
58
+ qrSizeOrOptions !== null &&
59
+ typeof qrSizeOrOptions.qrSize === 'number'
54
60
  ? qrSizeOrOptions.qrSize
55
61
  : (qrSizeOrOptions as number),
56
62
  className:
57
- typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null && typeof qrSizeOrOptions.className === 'string'
63
+ typeof qrSizeOrOptions === 'object' &&
64
+ qrSizeOrOptions !== null &&
65
+ typeof qrSizeOrOptions.className === 'string'
58
66
  ? qrSizeOrOptions.className
59
- : classNameLegacy
67
+ : classNameLegacy,
60
68
  };
61
69
 
62
70
  // ECC levels mapping
63
- const eccLevels = [
64
- QrCode.Ecc.LOW,
65
- QrCode.Ecc.MEDIUM,
66
- QrCode.Ecc.QUARTILE,
67
- QrCode.Ecc.HIGH
68
- ];
71
+ const eccLevels = [QrCodeEcc.LOW, QrCodeEcc.MEDIUM, QrCodeEcc.QUARTILE, QrCodeEcc.HIGH];
69
72
 
70
73
  // Fallback undefined options
71
- const fallbackOptions: Required<QrOptions> = {
74
+ const fallbackOptions: RequiredQrOptions = {
72
75
  qrSize: typeof options.qrSize === 'number' ? options.qrSize : DEFAULT_SIZE,
73
76
  className: typeof options.className === 'string' ? options.className : classNameLegacy || '',
74
77
  isShowLogo: !!options.isShowLogo || false,
75
78
  isShowBackground: !!options.isShowBackground || false,
76
- foregroundColor: typeof options.foregroundColor === 'string' ? options.foregroundColor : FOREGROUND_COLOR_DEFAULT,
77
- backgroundColor: typeof options.backgroundColor === 'string' ? options.backgroundColor : BACKGROUND_COLOR_DEFAULT,
79
+ foregroundColor:
80
+ typeof options.foregroundColor === 'string'
81
+ ? options.foregroundColor
82
+ : FOREGROUND_COLOR_DEFAULT,
83
+ backgroundColor:
84
+ typeof options.backgroundColor === 'string'
85
+ ? options.backgroundColor
86
+ : BACKGROUND_COLOR_DEFAULT,
78
87
  logoColor: typeof options.logoColor === 'string' ? options.logoColor : LOGO_COLOR_DEFAULT,
79
88
  suffix: options.suffix ? options.suffix.toString() : '0',
80
89
  logoData: typeof options.logoData === 'string' ? options.logoData : null,
81
- ecc: (typeof options.ecc === 'number' && eccLevels[options.ecc] ? options.ecc : 3),
90
+ ecc: typeof options.ecc === 'number' && eccLevels[options.ecc] ? options.ecc : 3,
91
+ incTileSize: typeof options.incTileSize === 'number' ? options.incTileSize : INC_TILE_SIZE,
82
92
  };
83
93
 
84
94
  // Code generation
85
95
  const segments: QrSegment[] = QrSegment.makeSegments(text);
86
- const qrCode: QrCode = QrCode.encodeSegments(segments, eccLevels[fallbackOptions.ecc], 1, 40, -1, true);
96
+ const qrCode: QrCode = QrCode.encodeSegments(
97
+ segments,
98
+ eccLevels[fallbackOptions.ecc],
99
+ 1,
100
+ 40,
101
+ -1,
102
+ true,
103
+ );
87
104
  const svgCode = convertSegmentsToSvgString(qrCode, fallbackOptions);
88
105
 
89
106
  return svgCode;
90
107
  }
91
-
92
- export default {
93
- createQR
94
- };