Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add handling for sub-regions that only consist of "a" #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

csandman
Copy link
Member

@csandman csandman commented Nov 9, 2024

Currently, there is an issue with the inline number replacement in strings consisting of more than just numbers, where the word "a" is always replaced with a "1", even if it's on it's own. This has been mentioned a few times in issues on the original project:

This behavior really doesn't make sense, and it's already handled for cases with only the word "a" (in the existing code, the standalone word "a" isn't replaced with anything). So This is meant to fix the rest of the cases where the word has no neighboring numbers to make it into an actual number.

I added some new test cases around this to ensure it works as expected.

// This is an existing test case that already passed
it("a", () => {
  expect(wtn("a")).to.equal("a");
});

// This case also already passed
it("A", () => {
  expect(wtn("A")).to.equal("A");
});

// Previously, these two cases would all result in the "a"s being replaced with "1"
it("A crab cake", () => {
  expect(wtn("A crab cake")).to.equal("A crab cake");
});

it("A cat and a mouse", () => {
  expect(wtn("A cat and a mouse")).to.equal("A cat and a mouse");
});

// This is a valid case where you'd expect the "a" to mean "one hundred"
it("A hundred cats and dogs", () => {
  expect(wtn("A hundred cats and dogs")).to.equal("100 cats and dogs");
});

Besides the main change, I made some more cleanup changes to make the code more readable, and organized some files.

@csandman csandman changed the title fix: Add handling for regions that only consist of "a" fix: Add handling for sub-regions that only consist of "a" Nov 9, 2024
Comment on lines +103 to +108
if (!checkBlacklist(region.tokens)) {
const length = region.end - region.start + 1;
const replaceWith = getNumber(region).toString();
replaced = splice(replaced, region.start + offset, length, replaceWith);
offset -= length - replaceWith.length;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the change that actually fixed the issue.

Comment on lines -369 to -377
if (end !== start) {
acc.push({
start,
end: end - 1,
value: unfuzzyChunk,
lowerCaseValue: unfuzzyChunk.toLowerCase(),
type: getTokenType(unfuzzyChunk),
});
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was so convoluted. This was basically going through the whole flow, just to finally check if the start index and end index are the same (meaning the string is empty), where as you can just check that at the beginning or filter them out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant