This commit is contained in:
Timothy J. Baek
2024-10-19 16:18:14 -07:00
parent 73b33c3781
commit 953a8285f7
4 changed files with 42 additions and 21 deletions
+23 -8
View File
@@ -273,20 +273,35 @@ export const findWordIndices = (text) => {
};
export const removeLastWordFromString = (inputString, wordString) => {
// Split the string into an array of words
const words = inputString.split(' ');
console.log('inputString', inputString);
// Split the string by newline characters to handle lines separately
const lines = inputString.split('\n');
console.log(words.at(-1), wordString);
// Take the last line to operate only on it
const lastLine = lines.pop();
// Split the last line into an array of words
const words = lastLine.split(' ');
// Conditional to check for the last word removal
if (words.at(-1) === wordString || (wordString === '' && words.at(-1) === '\\#')) {
words.pop();
words.pop(); // Remove last word if condition is satisfied
}
// Join the remaining words back into a string
let resultString = words.join(' ');
if (resultString !== '') {
resultString += ' ';
// Join the remaining words back into a string and handle space correctly
let updatedLastLine = words.join(' ');
// Add a trailing space to the updated last line if there are still words
if (updatedLastLine !== '') {
updatedLastLine += ' ';
}
// Combine the lines together again, placing the updated last line back in
const resultString = [...lines, updatedLastLine].join('\n');
// Return the final string
console.log('resultString', resultString);
return resultString;
};