Skip to content

Commit

Permalink
Fixed tags removal from notes with carriage returns (closes #942)
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Nov 12, 2023
1 parent 2df9d1c commit d8f9ce7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -94,15 +95,20 @@ public static String removeTags(String text, List<Tag> tagsToRemove) {
if (StringUtils.isEmpty(text)) {
return text;
}
String[] textCopy = new String[]{text};
from(tagsToRemove).forEach(tagToRemove -> textCopy[0] = removeTag(textCopy[0], tagToRemove));
return textCopy[0];
var textCopy = new AtomicReference<>(text);
from(tagsToRemove).forEach(tagToRemove -> textCopy.set(removeTag(textCopy.get(), tagToRemove)));
return textCopy.get();
}

private static String removeTag(String textCopy, Tag tagToRemove) {
return from(textCopy.split(" "))
var spaceProcessed = tokenizeAndRemoveTag(textCopy, " ", tagToRemove);
return tokenizeAndRemoveTag(spaceProcessed, "\n", tagToRemove);
}

private static String tokenizeAndRemoveTag(String text, String separator, Tag tagToRemove) {
return from(text.split(separator))
.map(word -> removeTagFromWord(word, tagToRemove))
.reduce((s, s2) -> s + " " + s2)
.reduce((s, s2) -> s + separator + s2)
.toBlocking()
.singleOrDefault("")
.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@

public class TagsHelperTest {

private static Tag TAG1 = new Tag("#mixed", 1);
private static Tag TAG2 = new Tag("#tags", 1);
private static Tag TAG3 = new Tag("#tag", 1);
private static Tag TAG4 = new Tag("#numberedAfter123", 1);
private static Tag TAG_INVALID = new Tag("#123numbered", 1);
private static final Tag TAG1 = new Tag("#mixed", 1);
private static final Tag TAG2 = new Tag("#tags", 1);
private static final Tag TAG3 = new Tag("#tag", 1);
private static final Tag TAG4 = new Tag("#numberedAfter123", 1);
private static final Tag TAG_INVALID = new Tag("#123numbered", 1);

private Note note;

Expand Down Expand Up @@ -129,7 +129,7 @@ public void TestTagWithComma() {
}

@Test
public void removeTags_specialCharsKeeped () {
public void removeTags_specialCharsKept () {
String text = "<>[],-.(){}!?\n\t text";
String testString = text + " " + TAG1.getText();

Expand All @@ -138,6 +138,16 @@ public void removeTags_specialCharsKeeped () {
assertEquals(text, result);
}

@Test
public void removeTags_mixedCarriageReturns () {
var text = "some text\n" + TAG1.getText() + " other following text ending with " + TAG2.getText();
var expected = "some text\n other following text ending with " + TAG2.getText();

var result = TagsHelper.removeTags(text, singletonList(TAG1));

assertEquals(expected, result);
}

@Test
public void removeTagFromWord() {
String word = TAG3 + "(and";
Expand Down

0 comments on commit d8f9ce7

Please sign in to comment.