@olenbetong/synergi-react 4.1.1 → 4.1.3

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.
Files changed (59) hide show
  1. package/es/components/ImageEditor/plugins/cropPlugin/useCropTool.js +3 -1
  2. package/es/components/ImageEditor/useEdgeDetection.d.ts +17 -2
  3. package/es/components/ImageEditor/useEdgeDetection.js +241 -61
  4. package/es/components/Toolbar/index.js +20 -1
  5. package/package.json +3 -3
  6. package/src/chrome/AppShell/index.tsx +29 -29
  7. package/src/chrome/openReport.ts +34 -34
  8. package/src/colorScheme.ts +81 -81
  9. package/src/components/DateNavigator/index.tsx +186 -186
  10. package/src/components/FilePond.tsx +312 -312
  11. package/src/components/ImageEditor/ImageEditor.tsx +343 -343
  12. package/src/components/ImageEditor/index.ts +9 -9
  13. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  14. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  15. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -403
  16. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  17. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  18. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  19. package/src/components/ImageEditor/useEdgeDetection.ts +319 -127
  20. package/src/components/ImageEditor/useImageHistory.ts +197 -197
  21. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  22. package/src/components/LinkedCardList/index.scss +95 -95
  23. package/src/components/MergeProgressDialog/index.tsx +249 -249
  24. package/src/components/PageBanner/index.tsx +80 -80
  25. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  26. package/src/components/ProgressBar/index.scss +96 -96
  27. package/src/components/SplitContainer/index.tsx +333 -333
  28. package/src/components/Toolbar/index.tsx +232 -212
  29. package/src/designsystemet-web-types.d.ts +24 -24
  30. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  31. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  32. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  33. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  34. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  35. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  36. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  37. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  38. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  39. package/src/elements/ElementLookup.tsx +123 -123
  40. package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
  41. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
  42. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
  43. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
  44. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
  45. package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
  46. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
  47. package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
  48. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  49. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  50. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  51. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  52. package/src/elements/ProjectSelect.tsx +130 -130
  53. package/src/elements/index.ts +15 -15
  54. package/src/filter/index.ts +34 -34
  55. package/src/i18n.ts +13 -13
  56. package/src/index.ts +77 -77
  57. package/src/mui/MuiThemeProvider.tsx +174 -174
  58. package/src/mui/index.ts +1 -1
  59. package/src/test/setup.ts +8 -8
@@ -1,103 +1,103 @@
1
- import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "../decoder";
2
- import { getLocalizedString } from "../../../../i18n.js";
3
-
4
- /**
5
- * Minimal typing of the Barcode Detection API. It is not part of the TypeScript DOM lib because
6
- * only Chromium-based browsers implement it.
7
- */
8
- export type NativeDetectedBarcode = {
9
- rawValue: string;
10
- format: string;
11
- cornerPoints: Array<{ x: number; y: number }>;
12
- boundingBox: DOMRectReadOnly;
13
- };
14
-
15
- type NativeBarcodeDetector = {
16
- detect(source: ImageBitmapSource): Promise<NativeDetectedBarcode[]>;
17
- };
18
-
19
- type NativeBarcodeDetectorConstructor = {
20
- new (options?: { formats?: string[] }): NativeBarcodeDetector;
21
- getSupportedFormats(): Promise<string[]>;
22
- };
23
-
24
- const DATA_MATRIX = "data_matrix";
25
-
26
- function getNativeDetector(): NativeBarcodeDetectorConstructor | undefined {
27
- return (globalThis as { BarcodeDetector?: NativeBarcodeDetectorConstructor }).BarcodeDetector;
28
- }
29
-
30
- /**
31
- * Whether the browser can detect DataMatrix codes natively. Chrome on desktop exposes the
32
- * `BarcodeDetector` class without any formats, so the class alone is not enough.
33
- */
34
- export async function isNativeDecoderAvailable(): Promise<boolean> {
35
- try {
36
- let detector = getNativeDetector();
37
- if (!detector) return false;
38
-
39
- let formats = await detector.getSupportedFormats();
40
-
41
- return formats.includes(DATA_MATRIX);
42
- } catch {
43
- return false;
44
- }
45
- }
46
-
47
- export function mapNativeResult(result: NativeDetectedBarcode): DecodedBarcode {
48
- let points = result.cornerPoints;
49
- let { left, top, right, bottom } = result.boundingBox;
50
- let corners: DecodedBarcode["corners"] =
51
- points.length >= 4
52
- ? [
53
- { x: points[0]!.x, y: points[0]!.y },
54
- { x: points[1]!.x, y: points[1]!.y },
55
- { x: points[2]!.x, y: points[2]!.y },
56
- { x: points[3]!.x, y: points[3]!.y },
57
- ]
58
- : [
59
- { x: left, y: top },
60
- { x: right, y: top },
61
- { x: right, y: bottom },
62
- { x: left, y: bottom },
63
- ];
64
-
65
- return { text: result.rawValue, format: result.format, corners };
66
- }
67
-
68
- /**
69
- * Decoder backed by the browser's own Barcode Detection API (ML Kit on Android). Nothing to
70
- * download, but only available where the browser supports it — check `isNativeDecoderAvailable`
71
- * first and fall back to another decoder when it returns false.
72
- */
73
- export function createNativeDecoder(): DecoderFactory {
74
- return async () => {
75
- let Detector = getNativeDetector();
76
-
77
- if (!Detector || !(await isNativeDecoderAvailable())) {
78
- throw new Error(getLocalizedString("This browser cannot detect barcodes natively"));
79
- }
80
-
81
- let detector = new Detector({ formats: [DATA_MATRIX] });
82
-
83
- return {
84
- name: "native",
85
- async decodeFrame(frame) {
86
- let results = await detector.detect(frame);
87
-
88
- return results.map(mapNativeResult);
89
- },
90
- async decodeImage(file) {
91
- let bitmap = await createImageBitmap(file);
92
-
93
- try {
94
- let results = await detector.detect(bitmap);
95
-
96
- return results.map(mapNativeResult);
97
- } finally {
98
- bitmap.close();
99
- }
100
- },
101
- } satisfies BarcodeDecoder;
102
- };
103
- }
1
+ import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "../decoder";
2
+ import { getLocalizedString } from "../../../../i18n.js";
3
+
4
+ /**
5
+ * Minimal typing of the Barcode Detection API. It is not part of the TypeScript DOM lib because
6
+ * only Chromium-based browsers implement it.
7
+ */
8
+ export type NativeDetectedBarcode = {
9
+ rawValue: string;
10
+ format: string;
11
+ cornerPoints: Array<{ x: number; y: number }>;
12
+ boundingBox: DOMRectReadOnly;
13
+ };
14
+
15
+ type NativeBarcodeDetector = {
16
+ detect(source: ImageBitmapSource): Promise<NativeDetectedBarcode[]>;
17
+ };
18
+
19
+ type NativeBarcodeDetectorConstructor = {
20
+ new (options?: { formats?: string[] }): NativeBarcodeDetector;
21
+ getSupportedFormats(): Promise<string[]>;
22
+ };
23
+
24
+ const DATA_MATRIX = "data_matrix";
25
+
26
+ function getNativeDetector(): NativeBarcodeDetectorConstructor | undefined {
27
+ return (globalThis as { BarcodeDetector?: NativeBarcodeDetectorConstructor }).BarcodeDetector;
28
+ }
29
+
30
+ /**
31
+ * Whether the browser can detect DataMatrix codes natively. Chrome on desktop exposes the
32
+ * `BarcodeDetector` class without any formats, so the class alone is not enough.
33
+ */
34
+ export async function isNativeDecoderAvailable(): Promise<boolean> {
35
+ try {
36
+ let detector = getNativeDetector();
37
+ if (!detector) return false;
38
+
39
+ let formats = await detector.getSupportedFormats();
40
+
41
+ return formats.includes(DATA_MATRIX);
42
+ } catch {
43
+ return false;
44
+ }
45
+ }
46
+
47
+ export function mapNativeResult(result: NativeDetectedBarcode): DecodedBarcode {
48
+ let points = result.cornerPoints;
49
+ let { left, top, right, bottom } = result.boundingBox;
50
+ let corners: DecodedBarcode["corners"] =
51
+ points.length >= 4
52
+ ? [
53
+ { x: points[0]!.x, y: points[0]!.y },
54
+ { x: points[1]!.x, y: points[1]!.y },
55
+ { x: points[2]!.x, y: points[2]!.y },
56
+ { x: points[3]!.x, y: points[3]!.y },
57
+ ]
58
+ : [
59
+ { x: left, y: top },
60
+ { x: right, y: top },
61
+ { x: right, y: bottom },
62
+ { x: left, y: bottom },
63
+ ];
64
+
65
+ return { text: result.rawValue, format: result.format, corners };
66
+ }
67
+
68
+ /**
69
+ * Decoder backed by the browser's own Barcode Detection API (ML Kit on Android). Nothing to
70
+ * download, but only available where the browser supports it — check `isNativeDecoderAvailable`
71
+ * first and fall back to another decoder when it returns false.
72
+ */
73
+ export function createNativeDecoder(): DecoderFactory {
74
+ return async () => {
75
+ let Detector = getNativeDetector();
76
+
77
+ if (!Detector || !(await isNativeDecoderAvailable())) {
78
+ throw new Error(getLocalizedString("This browser cannot detect barcodes natively"));
79
+ }
80
+
81
+ let detector = new Detector({ formats: [DATA_MATRIX] });
82
+
83
+ return {
84
+ name: "native",
85
+ async decodeFrame(frame) {
86
+ let results = await detector.detect(frame);
87
+
88
+ return results.map(mapNativeResult);
89
+ },
90
+ async decodeImage(file) {
91
+ let bitmap = await createImageBitmap(file);
92
+
93
+ try {
94
+ let results = await detector.detect(bitmap);
95
+
96
+ return results.map(mapNativeResult);
97
+ } finally {
98
+ bitmap.close();
99
+ }
100
+ },
101
+ } satisfies BarcodeDecoder;
102
+ };
103
+ }
@@ -1,95 +1,95 @@
1
- import type { ReaderOptions, ReadResult } from "zxing-wasm/reader";
2
- import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "../decoder";
3
-
4
- export type ZxingDecoderOptions = {
5
- /**
6
- * URL of `zxing_reader.wasm`. When omitted, zxing-wasm fetches the binary matching its own version
7
- * from the jsDelivr CDN.
8
- */
9
- wasmUrl?: string;
10
- /** Reader options for live camera frames. Merged over the defaults. */
11
- frameOptions?: ReaderOptions;
12
- /** Reader options for still images. Merged over the defaults. */
13
- imageOptions?: ReaderOptions;
14
- };
15
-
16
- const defaultFrameOptions: ReaderOptions = {
17
- formats: ["DataMatrix"],
18
- tryHarder: false,
19
- tryRotate: true,
20
- tryInvert: false,
21
- tryDownscale: true,
22
- maxNumberOfSymbols: 4,
23
- };
24
-
25
- const defaultImageOptions: ReaderOptions = {
26
- formats: ["DataMatrix"],
27
- tryHarder: true,
28
- tryRotate: true,
29
- tryInvert: true,
30
- tryDownscale: true,
31
- maxNumberOfSymbols: 8,
32
- };
33
-
34
- export function mapZxingResult(result: ReadResult): DecodedBarcode {
35
- let { topLeft, topRight, bottomRight, bottomLeft } = result.position;
36
-
37
- return {
38
- text: result.text,
39
- format: result.format,
40
- corners: [
41
- { x: topLeft.x, y: topLeft.y },
42
- { x: topRight.x, y: topRight.y },
43
- { x: bottomRight.x, y: bottomRight.y },
44
- { x: bottomLeft.x, y: bottomLeft.y },
45
- ],
46
- };
47
- }
48
-
49
- /**
50
- * Decoder backed by zxing-cpp compiled to WebAssembly (`zxing-wasm`). Free, runs everywhere
51
- * WebAssembly does, including iOS Safari. The module is imported when the factory runs, so apps
52
- * that never open the scanner do not download it.
53
- */
54
- export function createZxingDecoder(options: ZxingDecoderOptions = {}): DecoderFactory {
55
- let frameOptions = { ...defaultFrameOptions, ...options.frameOptions };
56
- let imageOptions = { ...defaultImageOptions, ...options.imageOptions };
57
-
58
- return async () => {
59
- if (typeof WebAssembly === "undefined") {
60
- throw new Error("WebAssembly is not supported");
61
- }
62
-
63
- let zxing = await import("zxing-wasm/reader");
64
- let { wasmUrl } = options;
65
-
66
- if (wasmUrl) {
67
- zxing.prepareZXingModule({
68
- overrides: {
69
- locateFile: (path: string, prefix: string) => (path.endsWith(".wasm") ? wasmUrl : prefix + path),
70
- },
71
- });
72
- }
73
-
74
- // Instantiate up front so a broken download surfaces as a scanner error instead of on the first frame.
75
- await zxing.prepareZXingModule({ fireImmediately: true });
76
-
77
- return {
78
- name: "zxing",
79
- async decodeFrame(frame) {
80
- let context = frame.getContext("2d", { willReadFrequently: true });
81
- if (!context) return [];
82
-
83
- let imageData = context.getImageData(0, 0, frame.width, frame.height);
84
- let results = await zxing.readBarcodes(imageData, frameOptions);
85
-
86
- return results.filter((result) => result.isValid).map(mapZxingResult);
87
- },
88
- async decodeImage(file) {
89
- let results = await zxing.readBarcodes(file, imageOptions);
90
-
91
- return results.filter((result) => result.isValid).map(mapZxingResult);
92
- },
93
- } satisfies BarcodeDecoder;
94
- };
95
- }
1
+ import type { ReaderOptions, ReadResult } from "zxing-wasm/reader";
2
+ import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "../decoder";
3
+
4
+ export type ZxingDecoderOptions = {
5
+ /**
6
+ * URL of `zxing_reader.wasm`. When omitted, zxing-wasm fetches the binary matching its own version
7
+ * from the jsDelivr CDN.
8
+ */
9
+ wasmUrl?: string;
10
+ /** Reader options for live camera frames. Merged over the defaults. */
11
+ frameOptions?: ReaderOptions;
12
+ /** Reader options for still images. Merged over the defaults. */
13
+ imageOptions?: ReaderOptions;
14
+ };
15
+
16
+ const defaultFrameOptions: ReaderOptions = {
17
+ formats: ["DataMatrix"],
18
+ tryHarder: false,
19
+ tryRotate: true,
20
+ tryInvert: false,
21
+ tryDownscale: true,
22
+ maxNumberOfSymbols: 4,
23
+ };
24
+
25
+ const defaultImageOptions: ReaderOptions = {
26
+ formats: ["DataMatrix"],
27
+ tryHarder: true,
28
+ tryRotate: true,
29
+ tryInvert: true,
30
+ tryDownscale: true,
31
+ maxNumberOfSymbols: 8,
32
+ };
33
+
34
+ export function mapZxingResult(result: ReadResult): DecodedBarcode {
35
+ let { topLeft, topRight, bottomRight, bottomLeft } = result.position;
36
+
37
+ return {
38
+ text: result.text,
39
+ format: result.format,
40
+ corners: [
41
+ { x: topLeft.x, y: topLeft.y },
42
+ { x: topRight.x, y: topRight.y },
43
+ { x: bottomRight.x, y: bottomRight.y },
44
+ { x: bottomLeft.x, y: bottomLeft.y },
45
+ ],
46
+ };
47
+ }
48
+
49
+ /**
50
+ * Decoder backed by zxing-cpp compiled to WebAssembly (`zxing-wasm`). Free, runs everywhere
51
+ * WebAssembly does, including iOS Safari. The module is imported when the factory runs, so apps
52
+ * that never open the scanner do not download it.
53
+ */
54
+ export function createZxingDecoder(options: ZxingDecoderOptions = {}): DecoderFactory {
55
+ let frameOptions = { ...defaultFrameOptions, ...options.frameOptions };
56
+ let imageOptions = { ...defaultImageOptions, ...options.imageOptions };
57
+
58
+ return async () => {
59
+ if (typeof WebAssembly === "undefined") {
60
+ throw new Error("WebAssembly is not supported");
61
+ }
62
+
63
+ let zxing = await import("zxing-wasm/reader");
64
+ let { wasmUrl } = options;
65
+
66
+ if (wasmUrl) {
67
+ zxing.prepareZXingModule({
68
+ overrides: {
69
+ locateFile: (path: string, prefix: string) => (path.endsWith(".wasm") ? wasmUrl : prefix + path),
70
+ },
71
+ });
72
+ }
73
+
74
+ // Instantiate up front so a broken download surfaces as a scanner error instead of on the first frame.
75
+ await zxing.prepareZXingModule({ fireImmediately: true });
76
+
77
+ return {
78
+ name: "zxing",
79
+ async decodeFrame(frame) {
80
+ let context = frame.getContext("2d", { willReadFrequently: true });
81
+ if (!context) return [];
82
+
83
+ let imageData = context.getImageData(0, 0, frame.width, frame.height);
84
+ let results = await zxing.readBarcodes(imageData, frameOptions);
85
+
86
+ return results.filter((result) => result.isValid).map(mapZxingResult);
87
+ },
88
+ async decodeImage(file) {
89
+ let results = await zxing.readBarcodes(file, imageOptions);
90
+
91
+ return results.filter((result) => result.isValid).map(mapZxingResult);
92
+ },
93
+ } satisfies BarcodeDecoder;
94
+ };
95
+ }
@@ -1,118 +1,118 @@
1
- import { describe, expect, it } from "vitest";
2
- import { mapDbrResult } from "./decoders/dbr";
3
- import { mapNativeResult, type NativeDetectedBarcode } from "./decoders/native";
4
- import { mapZxingResult } from "./decoders/zxing";
5
- import { createDuplicateFilter } from "./duplicateFilter";
6
-
7
- const GS = "";
8
- const GIAI = `8004${"7043210"}${GS}${"12345"}`;
9
-
10
- describe("result mapping", () => {
11
- it("maps a zxing result to corners in reading order", () => {
12
- let mapped = mapZxingResult({
13
- text: GIAI,
14
- format: "DataMatrix",
15
- isValid: true,
16
- position: {
17
- topLeft: { x: 10, y: 20 },
18
- topRight: { x: 110, y: 20 },
19
- bottomRight: { x: 110, y: 120 },
20
- bottomLeft: { x: 10, y: 120 },
21
- },
22
- } as Parameters<typeof mapZxingResult>[0]);
23
-
24
- expect(mapped.text).toBe(GIAI);
25
- expect(mapped.format).toBe("DataMatrix");
26
- expect(mapped.corners).toEqual([
27
- { x: 10, y: 20 },
28
- { x: 110, y: 20 },
29
- { x: 110, y: 120 },
30
- { x: 10, y: 120 },
31
- ]);
32
- });
33
-
34
- it("maps a native result from its corner points", () => {
35
- let result: NativeDetectedBarcode = {
36
- rawValue: GIAI,
37
- format: "data_matrix",
38
- cornerPoints: [
39
- { x: 1, y: 2 },
40
- { x: 3, y: 2 },
41
- { x: 3, y: 4 },
42
- { x: 1, y: 4 },
43
- ],
44
- boundingBox: { left: 0, top: 0, right: 0, bottom: 0 } as DOMRectReadOnly,
45
- };
46
-
47
- expect(mapNativeResult(result)).toEqual({
48
- text: GIAI,
49
- format: "data_matrix",
50
- corners: [
51
- { x: 1, y: 2 },
52
- { x: 3, y: 2 },
53
- { x: 3, y: 4 },
54
- { x: 1, y: 4 },
55
- ],
56
- });
57
- });
58
-
59
- it("falls back to the bounding box when a native result has no corner points", () => {
60
- let result: NativeDetectedBarcode = {
61
- rawValue: GIAI,
62
- format: "data_matrix",
63
- cornerPoints: [],
64
- boundingBox: { left: 5, top: 6, right: 15, bottom: 16 } as DOMRectReadOnly,
65
- };
66
-
67
- expect(mapNativeResult(result).corners).toEqual([
68
- { x: 5, y: 6 },
69
- { x: 15, y: 6 },
70
- { x: 15, y: 16 },
71
- { x: 5, y: 16 },
72
- ]);
73
- });
74
-
75
- it("maps a Dynamsoft result from its localization result", () => {
76
- let mapped = mapDbrResult({
77
- BarcodeText: GIAI,
78
- BarcodeFormat: 134217728,
79
- LocalizationResult: { X1: 1, Y1: 2, X2: 3, Y2: 2, X3: 3, Y3: 4, X4: 1, Y4: 4 },
80
- });
81
-
82
- expect(mapped.text).toBe(GIAI);
83
- expect(mapped.corners).toEqual([
84
- { x: 1, y: 2 },
85
- { x: 3, y: 2 },
86
- { x: 3, y: 4 },
87
- { x: 1, y: 4 },
88
- ]);
89
- });
90
- });
91
-
92
- describe("createDuplicateFilter", () => {
93
- it("reports a code once while it stays in view", () => {
94
- let isNew = createDuplicateFilter(1000);
95
-
96
- expect(isNew("a", 0)).toBe(true);
97
- expect(isNew("a", 100)).toBe(false);
98
- expect(isNew("a", 900)).toBe(false);
99
- // Still in view: every sighting refreshes the timer.
100
- expect(isNew("a", 1500)).toBe(false);
101
- });
102
-
103
- it("reports a code again once it has been out of view for the forget time", () => {
104
- let isNew = createDuplicateFilter(1000);
105
-
106
- expect(isNew("a", 0)).toBe(true);
107
- expect(isNew("a", 1001)).toBe(true);
108
- });
109
-
110
- it("tracks codes independently", () => {
111
- let isNew = createDuplicateFilter(1000);
112
-
113
- expect(isNew("a", 0)).toBe(true);
114
- expect(isNew("b", 0)).toBe(true);
115
- expect(isNew("a", 10)).toBe(false);
116
- expect(isNew("b", 10)).toBe(false);
117
- });
118
- });
1
+ import { describe, expect, it } from "vitest";
2
+ import { mapDbrResult } from "./decoders/dbr";
3
+ import { mapNativeResult, type NativeDetectedBarcode } from "./decoders/native";
4
+ import { mapZxingResult } from "./decoders/zxing";
5
+ import { createDuplicateFilter } from "./duplicateFilter";
6
+
7
+ const GS = "";
8
+ const GIAI = `8004${"7043210"}${GS}${"12345"}`;
9
+
10
+ describe("result mapping", () => {
11
+ it("maps a zxing result to corners in reading order", () => {
12
+ let mapped = mapZxingResult({
13
+ text: GIAI,
14
+ format: "DataMatrix",
15
+ isValid: true,
16
+ position: {
17
+ topLeft: { x: 10, y: 20 },
18
+ topRight: { x: 110, y: 20 },
19
+ bottomRight: { x: 110, y: 120 },
20
+ bottomLeft: { x: 10, y: 120 },
21
+ },
22
+ } as Parameters<typeof mapZxingResult>[0]);
23
+
24
+ expect(mapped.text).toBe(GIAI);
25
+ expect(mapped.format).toBe("DataMatrix");
26
+ expect(mapped.corners).toEqual([
27
+ { x: 10, y: 20 },
28
+ { x: 110, y: 20 },
29
+ { x: 110, y: 120 },
30
+ { x: 10, y: 120 },
31
+ ]);
32
+ });
33
+
34
+ it("maps a native result from its corner points", () => {
35
+ let result: NativeDetectedBarcode = {
36
+ rawValue: GIAI,
37
+ format: "data_matrix",
38
+ cornerPoints: [
39
+ { x: 1, y: 2 },
40
+ { x: 3, y: 2 },
41
+ { x: 3, y: 4 },
42
+ { x: 1, y: 4 },
43
+ ],
44
+ boundingBox: { left: 0, top: 0, right: 0, bottom: 0 } as DOMRectReadOnly,
45
+ };
46
+
47
+ expect(mapNativeResult(result)).toEqual({
48
+ text: GIAI,
49
+ format: "data_matrix",
50
+ corners: [
51
+ { x: 1, y: 2 },
52
+ { x: 3, y: 2 },
53
+ { x: 3, y: 4 },
54
+ { x: 1, y: 4 },
55
+ ],
56
+ });
57
+ });
58
+
59
+ it("falls back to the bounding box when a native result has no corner points", () => {
60
+ let result: NativeDetectedBarcode = {
61
+ rawValue: GIAI,
62
+ format: "data_matrix",
63
+ cornerPoints: [],
64
+ boundingBox: { left: 5, top: 6, right: 15, bottom: 16 } as DOMRectReadOnly,
65
+ };
66
+
67
+ expect(mapNativeResult(result).corners).toEqual([
68
+ { x: 5, y: 6 },
69
+ { x: 15, y: 6 },
70
+ { x: 15, y: 16 },
71
+ { x: 5, y: 16 },
72
+ ]);
73
+ });
74
+
75
+ it("maps a Dynamsoft result from its localization result", () => {
76
+ let mapped = mapDbrResult({
77
+ BarcodeText: GIAI,
78
+ BarcodeFormat: 134217728,
79
+ LocalizationResult: { X1: 1, Y1: 2, X2: 3, Y2: 2, X3: 3, Y3: 4, X4: 1, Y4: 4 },
80
+ });
81
+
82
+ expect(mapped.text).toBe(GIAI);
83
+ expect(mapped.corners).toEqual([
84
+ { x: 1, y: 2 },
85
+ { x: 3, y: 2 },
86
+ { x: 3, y: 4 },
87
+ { x: 1, y: 4 },
88
+ ]);
89
+ });
90
+ });
91
+
92
+ describe("createDuplicateFilter", () => {
93
+ it("reports a code once while it stays in view", () => {
94
+ let isNew = createDuplicateFilter(1000);
95
+
96
+ expect(isNew("a", 0)).toBe(true);
97
+ expect(isNew("a", 100)).toBe(false);
98
+ expect(isNew("a", 900)).toBe(false);
99
+ // Still in view: every sighting refreshes the timer.
100
+ expect(isNew("a", 1500)).toBe(false);
101
+ });
102
+
103
+ it("reports a code again once it has been out of view for the forget time", () => {
104
+ let isNew = createDuplicateFilter(1000);
105
+
106
+ expect(isNew("a", 0)).toBe(true);
107
+ expect(isNew("a", 1001)).toBe(true);
108
+ });
109
+
110
+ it("tracks codes independently", () => {
111
+ let isNew = createDuplicateFilter(1000);
112
+
113
+ expect(isNew("a", 0)).toBe(true);
114
+ expect(isNew("b", 0)).toBe(true);
115
+ expect(isNew("a", 10)).toBe(false);
116
+ expect(isNew("b", 10)).toBe(false);
117
+ });
118
+ });
@@ -1,21 +1,21 @@
1
- /**
2
- * Suppresses repeated reads of a code that stays in view. A value is reported once, and again only
3
- * after it has been out of view for `forgetTime` milliseconds — every sighting refreshes the timer,
4
- * matching the `duplicateForgetTime` behaviour of the Dynamsoft scanner the apps were built around.
5
- */
6
- export function createDuplicateFilter(forgetTime: number) {
7
- let lastSeen = new Map<string, number>();
8
-
9
- return function isNew(text: string, now: number): boolean {
10
- let previous = lastSeen.get(text);
11
- lastSeen.set(text, now);
12
-
13
- if (lastSeen.size > 64) {
14
- for (let [key, seen] of lastSeen) {
15
- if (now - seen > forgetTime) lastSeen.delete(key);
16
- }
17
- }
18
-
19
- return previous === undefined || now - previous > forgetTime;
20
- };
21
- }
1
+ /**
2
+ * Suppresses repeated reads of a code that stays in view. A value is reported once, and again only
3
+ * after it has been out of view for `forgetTime` milliseconds — every sighting refreshes the timer,
4
+ * matching the `duplicateForgetTime` behaviour of the Dynamsoft scanner the apps were built around.
5
+ */
6
+ export function createDuplicateFilter(forgetTime: number) {
7
+ let lastSeen = new Map<string, number>();
8
+
9
+ return function isNew(text: string, now: number): boolean {
10
+ let previous = lastSeen.get(text);
11
+ lastSeen.set(text, now);
12
+
13
+ if (lastSeen.size > 64) {
14
+ for (let [key, seen] of lastSeen) {
15
+ if (now - seen > forgetTime) lastSeen.delete(key);
16
+ }
17
+ }
18
+
19
+ return previous === undefined || now - previous > forgetTime;
20
+ };
21
+ }