Skip to content

Commit

Permalink
update splitToTwoLines again to keeps number with unit in a whole
Browse files Browse the repository at this point in the history
zetavg committed Jan 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 04b831b commit 28e912e
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions App/app/consts/chars.ts
Original file line number Diff line number Diff line change
@@ -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'];
5 changes: 5 additions & 0 deletions App/app/features/label-printers/print-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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',
5 changes: 3 additions & 2 deletions App/app/features/label-printers/print-utils.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 28e912e

Please sign in to comment.