-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72a16a5
commit 2ed41ac
Showing
11 changed files
with
315 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,3 @@ | ||
class OriginalInputHandler { | ||
private charLimit = 4500; | ||
|
||
constructor() { | ||
this.initializeEventListeners(); | ||
} | ||
|
||
private initializeEventListeners() { | ||
window.onload = () => { | ||
const originalElement = document.getElementById("original"); | ||
const charLimitElement = document.getElementById("char_limit"); | ||
|
||
originalElement?.addEventListener("input", this.handleInput); | ||
charLimitElement?.addEventListener("change", this.handleInput); | ||
}; | ||
} | ||
|
||
private handleInput = (): void => { | ||
const originalElement = document.getElementById("original") as HTMLInputElement | null; | ||
const charLimitElement = document.getElementById("char_limit") as HTMLInputElement | null; | ||
|
||
if (!originalElement || !charLimitElement) return; | ||
|
||
this.charLimit = Math.max(Number(charLimitElement.value), 1000); | ||
charLimitElement.value = this.charLimit.toString(); | ||
|
||
const source = originalElement.value; | ||
if (!source) return; | ||
|
||
const convertedText = this.processText(source); | ||
this.displayConvertedText(convertedText); | ||
} | ||
|
||
private processText(text: string): string { | ||
const replacements: [RegExp, string | ((substring: string) => string)][] = [ | ||
[/-\n/g, ""], // Removes hyphen followed by a newline | ||
[/\n/g, " "], // Replaces newlines with spaces | ||
[/- /g, ""], // Removes hyphens followed by a space | ||
[/Fig\. /g, "Fig."], // Formats abbreviation for "Figure" | ||
[/Figs\. /g, "Figs."], // Formats abbreviation for "Figures" | ||
[/No\. /g, "No."], // Formats abbreviation for "Number" | ||
[/Prof\. /g, "Prof."], // Formats abbreviation for "Professor" | ||
[/Eq\. /g, "Eq."], // Formats abbreviation for "Equation" | ||
[/et al\. /g, "et al."], // Formats "et al." | ||
[/Dr\. /g, "Dr."], // Formats abbreviation for "Doctor" | ||
[/e\.g\. /g, "e.g."], // Formats "e.g." | ||
[/i\.e\. /g, "i.e."], // Formats "i.e." | ||
[/Sec\. /g, "Sec."], // Formats abbreviation for "Section" | ||
[/Sect\. /g, "Sect."], // Formats abbreviation for "Section" | ||
[/2\.4 GHz/g, "2.4GHz"], // Formats specific frequency value | ||
[/[IVXLCDM]+\.\s/g, match => match.trim() + ". "], // Formats Roman numerals followed by a period | ||
[/\.\d+(?= [A-Z])/g, match => "[" + match + "]. "], // Formats decimal numbers followed by an uppercase letter | ||
[/\.\d+,\d+(?= [A-Z])/g, match => "[" + match + "]. "], // Formats numbers with commas | ||
[/\.\d+–\d+(?= [A-Z])/g, match => "[" + match + "]. "] // Formats number ranges | ||
|
||
]; | ||
|
||
let processedText = text; | ||
replacements.forEach(([regex, replacement]) => { | ||
if (typeof replacement === "function") { | ||
processedText = processedText.replace(regex, replacement); | ||
} else { | ||
processedText = processedText.replace(regex, replacement); | ||
} | ||
}); | ||
|
||
const sentences = processedText.split(". ").map(str => `${str}.\n`); | ||
const formattedText = this.splitIntoColumns(sentences); | ||
|
||
return this.createHtmlForColumns(formattedText); | ||
} | ||
|
||
|
||
private splitIntoColumns(sentences: string[]): string[][] { | ||
let charCount = 0; | ||
let results: string[][] = []; | ||
let currentColumn: string[] = []; | ||
|
||
sentences.forEach(sentence => { | ||
if (charCount + sentence.length > this.charLimit) { | ||
results.push(currentColumn); | ||
currentColumn = []; | ||
charCount = 0; | ||
} | ||
|
||
currentColumn.push(sentence); | ||
charCount += sentence.length; | ||
}); | ||
|
||
if (currentColumn.length > 0) { | ||
results.push(currentColumn); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
private createHtmlForColumns(columns: string[][]): string { | ||
return columns.map((column, index) => | ||
`<li class="list-group-item"> | ||
<label for="text_area_${index}"> | ||
No.${index}, Number of characters : ${column.join("").length} | ||
</label> | ||
<textarea class="form-control" id="text_area_${index}">${column.join("")}</textarea> | ||
</li>` | ||
).join(""); | ||
} | ||
|
||
private displayConvertedText(html: string): void { | ||
const convertedElement = document.getElementById("converted"); | ||
if (!convertedElement) return; | ||
|
||
convertedElement.innerHTML = html; | ||
} | ||
} | ||
import { OriginalInputHandler } from "./lib/original_input_handler" | ||
|
||
new OriginalInputHandler(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function createHtmlForColumns(columns: string[][]): string { | ||
return columns.map((column, index) => | ||
`<li class="list-group-item"> | ||
<label for="text_area_${index}"> | ||
No.${index}, Number of characters : ${column.join("").length} | ||
</label> | ||
<textarea class="form-control" id="text_area_${index}">${column.join("\n")}</textarea> | ||
</li>` | ||
).join(""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {splitIntoColumns} from './split_into_columns' | ||
|
||
export function formatAndSplitTextIntoColumns( | ||
text: string, | ||
charLimit: number, | ||
): string[][] { | ||
const replacements: [RegExp, string | ((substring: string) => string)][] = [ | ||
[/-\n/g, ''], // Ensure hyphen followed by newline is completely removed | ||
[/\n/g, " "], // Replaces newlines with spaces | ||
[/- /g, ""], // Removes hyphens followed by a space | ||
[/Fig\. /g, "Fig."], // Formats abbreviation for "Figure" | ||
[/Figs\. /g, "Figs."], // Formats abbreviation for "Figures" | ||
[/No\. /g, "No."], // Formats abbreviation for "Number" | ||
[/Prof\. /g, "Prof."], // Formats abbreviation for "Professor" | ||
[/Eq\. /g, "Eq."], // Formats abbreviation for "Equation" | ||
[/et al\. /g, "et al."], // Formats "et al." | ||
[/Dr\. /g, "Dr."], // Formats abbreviation for "Doctor" | ||
[/e\.g\. /g, "e.g."], // Formats "e.g." | ||
[/i\.e\. /g, "i.e."], // Formats "i.e." | ||
[/Sec\. /g, "Sec."], // Formats abbreviation for "Section" | ||
[/Sect\. /g, "Sect."], // Formats abbreviation for "Section" | ||
[/2\.4 GHz/g, "2.4GHz"], // Formats specific frequency value | ||
[/I\. /g, "I."], | ||
[/II\. /g, "II."], | ||
[/III\. /g, "III."], | ||
[/IV\. /g, "IV."], | ||
[/V\. /g, "V."], | ||
[/VI\. /g, "VI."], | ||
[/VII\. /g, "VII."], | ||
[/VIII\. /g, "VIII."], | ||
[/IX\. /g, "IX."], | ||
[/X\. /g, "X."], | ||
[/\.\d+,\d+(?= [A-Z])/g, match => "[" + match + "]. "], // Formats numbers with commas | ||
[/\.\d+-\d+(?= [A-Z])/g, match => "[" + match + "]. "], // Formats number ranges | ||
]; | ||
|
||
// Process replacements | ||
let processedText = text; | ||
replacements.forEach(([regex, replacement]) => { | ||
processedText = processedText.replace(regex, (match) => typeof replacement === "function" ? replacement(match) : replacement); | ||
}); | ||
|
||
// Split the processed text into sentences | ||
const sentences = processedText.split(/(?<=\.)\s/); | ||
|
||
// Use the splitIntoColumns function to split the sentences into columns | ||
return splitIntoColumns(sentences, charLimit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {formatAndSplitTextIntoColumns} from './format_and_split_text_Into_columns' | ||
import {createHtmlForColumns} from './create_html_for_columns' | ||
|
||
export class OriginalInputHandler { | ||
private charLimit = 4500; | ||
|
||
constructor() { | ||
this.initializeEventListeners(); | ||
} | ||
|
||
private initializeEventListeners() { | ||
window.onload = () => { | ||
const originalElement = document.getElementById("original"); | ||
const charLimitElement = document.getElementById("char_limit"); | ||
|
||
originalElement?.addEventListener("input", this.handleInput); | ||
charLimitElement?.addEventListener("change", this.handleInput); | ||
}; | ||
} | ||
|
||
private handleInput = (): void => { | ||
const originalElement = document.getElementById("original") as HTMLInputElement | null; | ||
const charLimitElement = document.getElementById("char_limit") as HTMLInputElement | null; | ||
|
||
if (!originalElement || !charLimitElement) return; | ||
|
||
this.charLimit = Math.max(Number(charLimitElement.value), 1000); | ||
charLimitElement.value = this.charLimit.toString(); | ||
|
||
const source = originalElement.value; | ||
if (!source) return; | ||
|
||
const convertedText = formatAndSplitTextIntoColumns( | ||
source, | ||
this.charLimit, | ||
); | ||
const html = createHtmlForColumns(convertedText) | ||
this.displayConvertedText(html); | ||
} | ||
|
||
private displayConvertedText(html: string): void { | ||
const convertedElement = document.getElementById("converted"); | ||
if (!convertedElement) return; | ||
|
||
convertedElement.innerHTML = html; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export function splitIntoColumns( | ||
sentences: string[], | ||
charLimit: number, | ||
): string[][] { | ||
let charCount = 0; | ||
let results: string[][] = []; | ||
let currentColumn: string[] = []; | ||
|
||
sentences.forEach(sentence => { | ||
// Check if adding this sentence exceeds charLimit or if the sentence itself exceeds charLimit | ||
if (charCount + sentence.length > charLimit || sentence.length > charLimit) { | ||
// Push currentColumn to results if it's not empty | ||
if (currentColumn.length > 0) { | ||
results.push(currentColumn); | ||
currentColumn = []; | ||
} | ||
|
||
// If the sentence itself exceeds charLimit, it should be in its own column | ||
if (sentence.length > charLimit) { | ||
results.push([sentence]); | ||
// Reset charCount as the long sentence is placed in its own column | ||
charCount = 0; | ||
return; // Continue to next sentence | ||
} | ||
} | ||
|
||
currentColumn.push(sentence); | ||
charCount += sentence.length; | ||
}); | ||
|
||
// Push the last column if not empty | ||
if (currentColumn.length > 0) { | ||
results.push(currentColumn); | ||
} | ||
|
||
return results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { createHtmlForColumns } from '../create_html_for_columns' | ||
|
||
describe('createHtmlForColumns', () => { | ||
test('should return an empty string for an empty array', () => { | ||
const result = createHtmlForColumns([]); | ||
expect(result).toBe(''); | ||
}); | ||
|
||
test('should handle a single-element array', () => { | ||
const columns = [['Hello']]; | ||
const expected = | ||
`<li class="list-group-item"> | ||
<label for="text_area_0"> | ||
No.0, Number of characters : 5 | ||
</label> | ||
<textarea class="form-control" id="text_area_0">Hello</textarea> | ||
</li>`; | ||
const result = createHtmlForColumns(columns); | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('should handle a single-element array', () => { | ||
const columns = [['Hello.', 'Hello']]; | ||
const expected = | ||
`<li class="list-group-item"> | ||
<label for="text_area_0"> | ||
No.0, Number of characters : 11 | ||
</label> | ||
<textarea class="form-control" id="text_area_0">Hello.\nHello</textarea> | ||
</li>`; | ||
const result = createHtmlForColumns(columns); | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('should handle multiple elements in array', () => { | ||
const columns = [['Hello'], ['World']]; | ||
const expected = | ||
`<li class="list-group-item"> | ||
<label for="text_area_0"> | ||
No.0, Number of characters : 5 | ||
</label> | ||
<textarea class="form-control" id="text_area_0">Hello</textarea> | ||
</li><li class="list-group-item"> | ||
<label for="text_area_1"> | ||
No.1, Number of characters : 5 | ||
</label> | ||
<textarea class="form-control" id="text_area_1">World</textarea> | ||
</li>`; | ||
const result = createHtmlForColumns(columns); | ||
expect(result).toBe(expected); | ||
}); | ||
}); |
Oops, something went wrong.