From 28e912e0b66c856ae2480a0669544dcc4b9ebee3 Mon Sep 17 00:00:00 2001 From: zetavg Date: Thu, 11 Jan 2024 00:59:43 +0800 Subject: [PATCH] update splitToTwoLines again to keeps number with unit in a whole --- App/app/consts/chars.ts | 4 ++++ App/app/features/label-printers/print-utils.test.ts | 5 +++++ App/app/features/label-printers/print-utils.ts | 5 +++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/App/app/consts/chars.ts b/App/app/consts/chars.ts index a9301a18..227a1448 100644 --- a/App/app/consts/chars.ts +++ b/App/app/consts/chars.ts @@ -28,4 +28,8 @@ export const UNITS = [ '公升', ]; +export const NUMBER_WITH_OPTIONAL_UNIT_REGEX = new RegExp( + `^([0-9,.]+) ?(${UNITS.join('|')})?$`, +); + export const OPERATION_SYMBOLS = ['×', 'x']; diff --git a/App/app/features/label-printers/print-utils.test.ts b/App/app/features/label-printers/print-utils.test.ts index ac6a98e4..c2254826 100644 --- a/App/app/features/label-printers/print-utils.test.ts +++ b/App/app/features/label-printers/print-utils.test.ts @@ -74,6 +74,11 @@ describe('splitToTwoLines', () => { '10 mm x 20 cm', ]); + expect(splitToTwoLines('Paper 10mm × 20cm', 12)).toStrictEqual([ + 'Paper', + '10mm × 20cm', + ]); + expect(splitToTwoLines('Paper 10 × 20 cm', 12)).toStrictEqual([ 'Paper', '10 × 20 cm', diff --git a/App/app/features/label-printers/print-utils.ts b/App/app/features/label-printers/print-utils.ts index 8aac3a59..d6eeba40 100644 --- a/App/app/features/label-printers/print-utils.ts +++ b/App/app/features/label-printers/print-utils.ts @@ -2,6 +2,7 @@ import { Platform } from 'react-native'; import { LINE_SPLITTING_PUNCTUATION_REGEX, + NUMBER_WITH_OPTIONAL_UNIT_REGEX, OPERATION_SYMBOLS, PUNCTUATION_REGEX, T_PUNCTUATION_REGEX, @@ -55,12 +56,12 @@ function* generateSubstrings(str: string, splitRegex?: RegExp) { !UNITS.includes(words[wordsPtr + 1]) && !( OPERATION_SYMBOLS.includes(words[wordsPtr + 1]) && - (words[wordsPtr].match(/^[0-9.]+$/) || + (words[wordsPtr].match(NUMBER_WITH_OPTIONAL_UNIT_REGEX) || UNITS.includes(words[wordsPtr])) ) && !( OPERATION_SYMBOLS.includes(words[wordsPtr]) && - (words[wordsPtr + 1] || '').match(/^[0-9.]+$/) + (words[wordsPtr + 1] || '').match(NUMBER_WITH_OPTIONAL_UNIT_REGEX) ) ) { yield subStr;