Skip to content

Commit

Permalink
テストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
takanotume24 committed Jan 19, 2024
1 parent 72a16a5 commit 2ed41ac
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 118 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true,
}
3 changes: 2 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ module.exports = {
// watchman: true,

roots: [
"<rootDir>/test"
"<rootDir>/src/lib/test",
"<rootDir>/src/helper/test"
],

transform: {
Expand Down
6 changes: 3 additions & 3 deletions test/helper.test.ts → src/helper/test/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Helper } from "../src/helper/helper"
import { Type } from "../src/type/type"
import { Helper } from "../helper"
import { Type } from "../../type/type"

describe("Helper.delete_last_period()", () => {
test("テストが正常に動作している", () => {
Expand Down Expand Up @@ -113,4 +113,4 @@ describe("Helper.delete_last_empty_string()", () => {
expect(result).toStrictEqual(correct)
})
}
)
)
115 changes: 1 addition & 114 deletions src/index.ts
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();
10 changes: 10 additions & 0 deletions src/lib/create_html_for_columns.ts
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("");
}
48 changes: 48 additions & 0 deletions src/lib/format_and_split_text_Into_columns.ts
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);
}
47 changes: 47 additions & 0 deletions src/lib/original_input_handler.ts
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;
}
}
37 changes: 37 additions & 0 deletions src/lib/split_into_columns.ts
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;
}
52 changes: 52 additions & 0 deletions src/lib/test/create_html_for_columns.test.ts
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);
});
});
Loading

0 comments on commit 2ed41ac

Please sign in to comment.