test: join service

This commit is contained in:
Ibrahima G. Coulibaly
2024-06-23 15:49:45 +01:00
parent d1552b224e
commit bfdc2cce95
3 changed files with 136 additions and 34 deletions

102
.idea/workspace.xml generated
View File

@@ -4,7 +4,8 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="refactor: tool options components">
<list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="fix: join text service">
<change afterPath="$PROJECT_DIR$/src/pages/string/join/string-join.test.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/pages/string/join/service.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/string/join/service.ts" afterDir="false" />
</list>
@@ -35,32 +36,34 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
&quot;ASKED_SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;main&quot;,
&quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;,
&quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Users/HP/IdeaProjects/omni-tools/src/pages/string/split&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;npm.dev.executor&quot;: &quot;Run&quot;,
&quot;npm.prebuild.executor&quot;: &quot;Run&quot;,
&quot;prettierjs.PrettierConfiguration.Package&quot;: &quot;C:\\Users\\HP\\IdeaProjects\\omni-tools\\node_modules\\prettier&quot;,
&quot;project.structure.last.edited&quot;: &quot;Problems&quot;,
&quot;project.structure.proportion&quot;: &quot;0.0&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.2&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;settings.typescriptcompiler&quot;,
&quot;ts.external.directory.path&quot;: &quot;C:\\Users\\HP\\IdeaProjects\\omni-tools\\node_modules\\typescript\\lib&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ASKED_ADD_EXTERNAL_FILES": "true",
"ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"Vitest.mergeText.executor": "Run",
"Vitest.mergeText.should merge lines and preserve blank lines when deleteBlankLines is false.executor": "Run",
"git-widget-placeholder": "main",
"ignore.virus.scanning.warn.message": "true",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "C:/Users/HP/IdeaProjects/omni-tools/src/pages/string/split",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"npm.dev.executor": "Run",
"npm.prebuild.executor": "Run",
"prettierjs.PrettierConfiguration.Package": "C:\\Users\\HP\\IdeaProjects\\omni-tools\\node_modules\\prettier",
"project.structure.last.edited": "Problems",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.2",
"settings.editor.selected.configurable": "settings.typescriptcompiler",
"ts.external.directory.path": "C:\\Users\\HP\\IdeaProjects\\omni-tools\\node_modules\\typescript\\lib",
"vue.rearranger.settings.migration": "true"
}
}</component>
}]]></component>
<component name="ReactDesignerToolWindowState">
<option name="myId2Visible">
<map>
@@ -84,7 +87,34 @@
<recent name="C:\Users\HP\IdeaProjects\omni-tools\src\tools" />
</key>
</component>
<component name="RunManager" selected="npm.dev">
<component name="RunManager" selected="Vitest.mergeText">
<configuration name="mergeText" type="JavaScriptTestRunnerVitest" temporary="true" nameIsGenerated="true">
<node-interpreter value="project" />
<vitest-package value="$PROJECT_DIR$/node_modules/vitest" />
<working-dir value="$PROJECT_DIR$" />
<vitest-options value="--run" />
<envs />
<scope-kind value="SUITE" />
<test-file value="$PROJECT_DIR$/src/pages/string/join/string-join.test.ts" />
<test-names>
<test-name value="mergeText" />
</test-names>
<method v="2" />
</configuration>
<configuration name="mergeText.should merge lines and preserve blank lines when deleteBlankLines is false" type="JavaScriptTestRunnerVitest" temporary="true" nameIsGenerated="true">
<node-interpreter value="project" />
<vitest-package value="$PROJECT_DIR$/node_modules/vitest" />
<working-dir value="$PROJECT_DIR$" />
<vitest-options value="--run" />
<envs />
<scope-kind value="TEST" />
<test-file value="$PROJECT_DIR$/src/pages/string/join/string-join.test.ts" />
<test-names>
<test-name value="mergeText" />
<test-name value="should merge lines and preserve blank lines when deleteBlankLines is false" />
</test-names>
<method v="2" />
</configuration>
<configuration name="dev" type="js.build_tools.npm" temporary="true" nameIsGenerated="true">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
@@ -107,6 +137,8 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Vitest.mergeText" />
<item itemvalue="Vitest.mergeText.should merge lines and preserve blank lines when deleteBlankLines is false" />
<item itemvalue="npm.dev" />
<item itemvalue="npm.prebuild" />
</list>
@@ -129,7 +161,7 @@
<workItem from="1719006951159" duration="2377000" />
<workItem from="1719021128819" duration="3239000" />
<workItem from="1719083989394" duration="7971000" />
<workItem from="1719092003308" duration="12297000" />
<workItem from="1719092003308" duration="13183000" />
</task>
<task id="LOCAL-00001" summary="feat: use vite and ts">
<option name="closed" value="true" />
@@ -363,7 +395,15 @@
<option name="project" value="LOCAL" />
<updated>1719152057230</updated>
</task>
<option name="localTasksCounter" value="30" />
<task id="LOCAL-00030" summary="fix: join text service">
<option name="closed" value="true" />
<created>1719153044395</created>
<option name="number" value="00030" />
<option name="presentableId" value="LOCAL-00030" />
<option name="project" value="LOCAL" />
<updated>1719153044395</updated>
</task>
<option name="localTasksCounter" value="31" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@@ -384,7 +424,6 @@
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<option name="CHECK_NEW_TODO" value="false" />
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="feat: use vite and ts" />
<MESSAGE value="feat: initial commit" />
<MESSAGE value="feat: example tools" />
<MESSAGE value="feat: Split string" />
@@ -409,7 +448,8 @@
<MESSAGE value="chore: tools by category" />
<MESSAGE value="feat: copy and import file" />
<MESSAGE value="refactor: tool options components" />
<option name="LAST_COMMIT_MESSAGE" value="refactor: tool options components" />
<MESSAGE value="fix: join text service" />
<option name="LAST_COMMIT_MESSAGE" value="fix: join text service" />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />

View File

@@ -4,9 +4,7 @@ export function mergeText(
deleteTrailingSpaces: boolean = true,
joinCharacter: string = ''
): string {
const lines = text.split('\n');
let processedLines: string[] = lines;
let processedLines: string[] = text.split('\n');
if (deleteTrailingSpaces) {
processedLines = processedLines.map((line) => line.trimEnd());
}

View File

@@ -0,0 +1,64 @@
import { describe, it, expect } from 'vitest';
import { mergeText } from './service';
describe('mergeText', () => {
it('should merge lines with default settings (delete blank lines, delete trailing spaces, join with empty string)', () => {
const input = 'line1 \n \nline2\nline3 \n\nline4';
const expected = 'line1line2line3line4';
expect(mergeText(input)).toBe(expected);
});
it('should merge lines and preserve blank lines when deleteBlankLines is false', () => {
const input = 'line1 \n \nline2\nline3 \n\nline4';
const expected = 'line1line2line3line4';
expect(mergeText(input, false, true, '')).toBe(expected);
});
it('should merge lines and preserve trailing spaces when deleteTrailingSpaces is false', () => {
const input = 'line1 \n \nline2\nline3 \n\nline4';
const expected = 'line1 line2line3 line4';
expect(mergeText(input, true, false)).toBe(expected);
});
it('should merge lines, preserve blank lines and trailing spaces when both deleteBlankLines and deleteTrailingSpaces are false', () => {
const input = 'line1 \n \nline2\nline3 \n\nline4';
const expected = 'line1 line2line3 line4';
expect(mergeText(input, false, false)).toBe(expected);
});
it('should merge lines with a specified joinCharacter', () => {
const input = 'line1 \n \nline2\nline3 \n\nline4';
const expected = 'line1 line2 line3 line4';
expect(mergeText(input, true, true, ' ')).toBe(expected);
});
it('should handle empty input', () => {
const input = '';
const expected = '';
expect(mergeText(input)).toBe(expected);
});
it('should handle input with only blank lines', () => {
const input = ' \n \n\n';
const expected = '';
expect(mergeText(input)).toBe(expected);
});
it('should handle input with only trailing spaces', () => {
const input = 'line1 \nline2 \nline3 ';
const expected = 'line1line2line3';
expect(mergeText(input)).toBe(expected);
});
it('should handle single line input', () => {
const input = 'single line';
const expected = 'single line';
expect(mergeText(input)).toBe(expected);
});
it('should join lines with new line character when joinCharacter is set to "\\n"', () => {
const input = 'line1 \n \nline2\nline3 \n\nline4';
const expected = 'line1\nline2\nline3\nline4';
expect(mergeText(input, true, true, '\n')).toBe(expected);
});
});