Skip to content

Commit

Permalink
fix: add pairs group check
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm committed Nov 29, 2024
1 parent 77a297f commit c7b9da5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ export {
export {WINDOW_WIDTH} from './parameters';

export {INITIALS, HEAD, TAIL, OTHER, HEAD_PAIR, TAIL_PAIR, OTHER_PAIR} from './abbreviations';

export {REGEXP_PAIRS} from './pairs';
export type {RegExpPair} from './pairs';
5 changes: 5 additions & 0 deletions src/constants/pairs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type RegExpPair = [() => RegExp, () => RegExp];

export const REGEXP_PAIRS: RegExpPair[] = [
[() => new RegExp('\\*\\*\\w', 'g'), () => new RegExp('\\w\\*\\*', 'g')],
];
35 changes: 34 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
rightStartsWithLowercase,
spaceBothSides,
} from './rules';
import {REGEXP_PAIRS, RegExpPair} from './constants';

// sides preprocessing before evaluation
const leftPreprocessor = lstChars(20);
Expand Down Expand Up @@ -51,6 +52,38 @@ const join = compose(joinCondition, zipWith<any, any, any>(call, sidesPreprocess
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const breaks = compose(breakCondition, zipWith<any, any, any>(call, sidesPreprocessors));

function isUnpairedStr(str: string, regExp: RegExpPair) {
const first = str?.match(regExp[0]()) ?? [];
const second = str?.match(regExp[1]()) ?? [];
return first.length === second.length;
}

function isUnpaired(str: string) {
for (const pair of REGEXP_PAIRS) {
if (isUnpairedStr(str, pair)) {
return true;
}
}
return false;
}

function groupPairs(parsed: string[]) {
let index = 0;

while (index < parsed.length - 1) {
const current = parsed[index];
const next = parsed[index + 1] || '';

if (isUnpaired(current)) {
parsed.splice(index, 2, current + next);
} else {
index++;
}
}

return parsed;
}

// sentences processing
export function sentenize(text: string): string[] {
const parts = text.split(/((?:\n\s*){2,})/);
Expand Down Expand Up @@ -80,5 +113,5 @@ export function sentenize(text: string): string[] {
}
}

return parsed;
return groupPairs(parsed);
}

0 comments on commit c7b9da5

Please sign in to comment.