Skip to content

Commit

Permalink
print-utils: update splitToTwoLines
Browse files Browse the repository at this point in the history
  • Loading branch information
zetavg committed Jan 10, 2024
1 parent 28e912e commit 699bce9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
30 changes: 28 additions & 2 deletions App/app/consts/chars.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
export const PUNCTUATION_REGEX = /[ ,/() ,。/、()]| - /;
export const LINE_SPLITTING_PUNCTUATION_REGEX = /[,(,。/、(]| - | \/ /;
export const LEFT_PARENTHESES = ['(', '('];

export const RIGHT_PARENTHESES = [')', ')'];

export const PUNCTUATIONS = [
' ',
',',
'/',
' ',
',',
'。',
'/',
'、',
' - ',
...LEFT_PARENTHESES,
...RIGHT_PARENTHESES,
];

export const PUNCTUATION_REGEX = new RegExp(
`${PUNCTUATIONS.map(p =>
p.replace('/', '\\/').replace('(', '\\(').replace(')', '\\)'),
).join('|')}`,
);

export const LINE_SPLITTING_PUNCTUATION_REGEX = /[,(),。/、()]| - | \/ /;
export const LINE_SPLITTING_PUNCTUATION_REGEX_2 = new RegExp(
`${LINE_SPLITTING_PUNCTUATION_REGEX.source}| (?=\\d)`,
);
export const T_PUNCTUATION_REGEX = /[ ,-/ ,。/]/;

export const UNITS = [
Expand Down
22 changes: 22 additions & 0 deletions App/app/features/label-printers/print-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ describe('splitToTwoLines', () => {
).toStrictEqual(['Storage Bag for', 'ABC-123 and DEF-456']);
});

it('does not break parentheses', async () => {
expect(splitToTwoLines('Hello (Hello World)', 12)).toStrictEqual([
'Hello',
'(Hello World)',
]);

expect(splitToTwoLines('Hello (Hello World)', 18)).toStrictEqual([
'Hello',
'(Hello World)',
]);

expect(splitToTwoLines('Hello (Hello World)', 7)).toStrictEqual([
'Hello',
'(Hello World)',
]);
});

it('keeps number with unit in a whole', async () => {
expect(splitToTwoLines('Rope 10 m', 8)).toStrictEqual(['Rope', '10 m']);

Expand All @@ -84,6 +101,11 @@ describe('splitToTwoLines', () => {
'10 × 20 cm',
]);

expect(splitToTwoLines('Paper (White) Bag 10 × 20 cm', 20)).toStrictEqual([
'Paper (White) Bag',
'10 × 20 cm',
]);

expect(splitToTwoLines('Paper 10 ×', 12)).toStrictEqual(['Paper 10 ×', '']);

expect(splitToTwoLines('Paper × 3', 5)).toStrictEqual(['Paper', '× 3']);
Expand Down
7 changes: 6 additions & 1 deletion App/app/features/label-printers/print-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Platform } from 'react-native';

import {
LEFT_PARENTHESES,
LINE_SPLITTING_PUNCTUATION_REGEX,
LINE_SPLITTING_PUNCTUATION_REGEX_2,
NUMBER_WITH_OPTIONAL_UNIT_REGEX,
OPERATION_SYMBOLS,
PUNCTUATION_REGEX,
RIGHT_PARENTHESES,
T_PUNCTUATION_REGEX,
UNITS,
} from '@app/consts/chars';
Expand Down Expand Up @@ -53,6 +56,8 @@ function* generateSubstrings(str: string, splitRegex?: RegExp) {
subStr += str[i];
if (subStr.endsWith(words[wordsPtr])) {
if (
!LEFT_PARENTHESES.some(p => subStr.endsWith(p)) &&
!RIGHT_PARENTHESES.some(p => str.slice(i + 1).startsWith(p)) &&
!UNITS.includes(words[wordsPtr + 1]) &&
!(
OPERATION_SYMBOLS.includes(words[wordsPtr + 1]) &&
Expand Down Expand Up @@ -100,7 +105,7 @@ export function splitToTwoLines(
let line1 = '';
for (const substring of generateSubstrings(
str,
strContainsPunctuation ? LINE_SPLITTING_PUNCTUATION_REGEX : undefined,
strContainsPunctuation ? LINE_SPLITTING_PUNCTUATION_REGEX_2 : undefined,
)) {
if (countChars(substring) > line1MaxWidth) break;

Expand Down

0 comments on commit 699bce9

Please sign in to comment.