fix: style

This commit is contained in:
Ibrahima G. Coulibaly
2024-07-09 23:38:29 +01:00
parent e4c726c7b5
commit 7b021d0770
36 changed files with 1107 additions and 955 deletions

31
.idea/workspace.xml generated
View File

@@ -4,13 +4,8 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="feat: find most popular ui"> <list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="fix: misc">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/options/SelectWithDesc.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/options/SelectWithDesc.tsx" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/result/ToolTextResult.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/result/ToolTextResult.tsx" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/pages/list/find-most-popular/find-most-popular.service.test.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/list/find-most-popular/find-most-popular.service.test.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/pages/list/sort/index.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/list/sort/index.tsx" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/utils/string.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/utils/string.ts" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -206,15 +201,7 @@
<workItem from="1719492452780" duration="8000" /> <workItem from="1719492452780" duration="8000" />
<workItem from="1719496624579" duration="6148000" /> <workItem from="1719496624579" duration="6148000" />
<workItem from="1720542757452" duration="5355000" /> <workItem from="1720542757452" duration="5355000" />
<workItem from="1720557527691" duration="1135000" /> <workItem from="1720557527691" duration="3245000" />
</task>
<task id="LOCAL-00041" summary="ci: fix">
<option name="closed" value="true" />
<created>1719185893875</created>
<option name="number" value="00041" />
<option name="presentableId" value="LOCAL-00041" />
<option name="project" value="LOCAL" />
<updated>1719185893875</updated>
</task> </task>
<task id="LOCAL-00042" summary="ci: fix"> <task id="LOCAL-00042" summary="ci: fix">
<option name="closed" value="true" /> <option name="closed" value="true" />
@@ -600,7 +587,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1720546921899</updated> <updated>1720546921899</updated>
</task> </task>
<option name="localTasksCounter" value="90" /> <task id="LOCAL-00090" summary="fix: misc">
<option name="closed" value="true" />
<created>1720558690146</created>
<option name="number" value="00090" />
<option name="presentableId" value="LOCAL-00090" />
<option name="project" value="LOCAL" />
<updated>1720558690147</updated>
</task>
<option name="localTasksCounter" value="91" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@@ -658,7 +653,6 @@
<MESSAGE value="feat: change gif speed" /> <MESSAGE value="feat: change gif speed" />
<MESSAGE value="chore: remove unused deps" /> <MESSAGE value="chore: remove unused deps" />
<MESSAGE value="style: lint" /> <MESSAGE value="style: lint" />
<MESSAGE value="fix: misc" />
<MESSAGE value="fix: radio and list sort init" /> <MESSAGE value="fix: radio and list sort init" />
<MESSAGE value="chore: formik updateField" /> <MESSAGE value="chore: formik updateField" />
<MESSAGE value="ci: run e2e tests" /> <MESSAGE value="ci: run e2e tests" />
@@ -669,7 +663,8 @@
<MESSAGE value="chore: idea config" /> <MESSAGE value="chore: idea config" />
<MESSAGE value="feat: sort list" /> <MESSAGE value="feat: sort list" />
<MESSAGE value="feat: find most popular ui" /> <MESSAGE value="feat: find most popular ui" />
<option name="LAST_COMMIT_MESSAGE" value="feat: find most popular ui" /> <MESSAGE value="fix: misc" />
<option name="LAST_COMMIT_MESSAGE" value="fix: misc" />
</component> </component>
<component name="XSLT-Support.FileAssociations.UIState"> <component name="XSLT-Support.FileAssociations.UIState">
<expand /> <expand />

View File

@@ -3,5 +3,5 @@ a {
} }
a:hover { a:hover {
color: #030362 color: #030362;
} }

View File

@@ -44,7 +44,7 @@ function displayFormater(
dict: { [key: string]: number }, dict: { [key: string]: number },
displayFormat: DisplayFormat displayFormat: DisplayFormat
): string[] { ): string[] {
let formattedOutput: string[] = []; const formattedOutput: string[] = [];
const total = Object.values(dict).reduce((acc, val) => acc + val, 0); const total = Object.values(dict).reduce((acc, val) => acc + val, 0);
switch (displayFormat) { switch (displayFormat) {

View File

@@ -5,43 +5,106 @@ import { TopItemsList } from './service';
describe('TopItemsList Function', () => { describe('TopItemsList Function', () => {
test('should return unique items ignoring case sensitivity', () => { test('should return unique items ignoring case sensitivity', () => {
const input = 'apple,banana,Apple,orange,Banana,apple'; const input = 'apple,banana,Apple,orange,Banana,apple';
const result = TopItemsList('symbol', ',', '\n', input, true, true, false, true); const result = TopItemsList(
'symbol',
',',
'\n',
input,
true,
true,
false,
true
);
expect(result).toBe('orange'); expect(result).toBe('orange');
}); });
test('should return unique items considering case sensitivity', () => { test('should return unique items considering case sensitivity', () => {
const input = 'apple,banana,Apple,orange,Banana,apple'; const input = 'apple,banana,Apple,orange,Banana,apple';
const result = TopItemsList('symbol', ',', '\n', input, true, true, true, true); const result = TopItemsList(
'symbol',
',',
'\n',
input,
true,
true,
true,
true
);
expect(result).toBe('banana\nApple\norange\nBanana'); expect(result).toBe('banana\nApple\norange\nBanana');
}); });
test('should return all unique items ignoring case sensitivity', () => { test('should return all unique items ignoring case sensitivity', () => {
const input = 'apple,banana,Apple,orange,Banana,apple'; const input = 'apple,banana,Apple,orange,Banana,apple';
const result = TopItemsList('symbol', ',', '\n', input, true, true, false, false); const result = TopItemsList(
'symbol',
',',
'\n',
input,
true,
true,
false,
false
);
expect(result).toBe('apple\nbanana\norange'); expect(result).toBe('apple\nbanana\norange');
}); });
test('should return all unique items considering case sensitivity', () => { test('should return all unique items considering case sensitivity', () => {
const input = 'apple,banana,Apple,orange,Banana,apple'; const input = 'apple,banana,Apple,orange,Banana,apple';
const result = TopItemsList('symbol', ',', '\n', input, true, true, true, false); const result = TopItemsList(
'symbol',
',',
'\n',
input,
true,
true,
true,
false
);
expect(result).toBe('apple\nbanana\nApple\norange\nBanana'); expect(result).toBe('apple\nbanana\nApple\norange\nBanana');
}); });
test('should handle empty items deletion', () => { test('should handle empty items deletion', () => {
const input = 'apple,,banana, ,orange'; const input = 'apple,,banana, ,orange';
const result = TopItemsList('symbol', ',', '\n', input, true, true, false, false); const result = TopItemsList(
'symbol',
',',
'\n',
input,
true,
true,
false,
false
);
expect(result).toBe('apple\nbanana\norange'); expect(result).toBe('apple\nbanana\norange');
}); });
test('should handle trimming items', () => { test('should handle trimming items', () => {
const input = ' apple , banana , orange '; const input = ' apple , banana , orange ';
const result = TopItemsList('symbol', ',', '\n', input, false, false, false, false); const result = TopItemsList(
'symbol',
',',
'\n',
input,
false,
false,
false,
false
);
expect(result).toBe(' apple \n banana \n orange '); expect(result).toBe(' apple \n banana \n orange ');
}); });
test('should handle regex split', () => { test('should handle regex split', () => {
const input = 'apple banana orange'; const input = 'apple banana orange';
const result = TopItemsList('regex', '\\s+', '\n', input, false, false, false, false); const result = TopItemsList(
'regex',
'\\s+',
'\n',
input,
false,
false,
false,
false
);
expect(result).toBe('apple\nbanana\norange'); expect(result).toBe('apple\nbanana\norange');
}); });
}); });

View File

@@ -37,22 +37,28 @@ export function TopItemsList(
array = input.split(splitSeparator); array = input.split(splitSeparator);
break; break;
case 'regex': case 'regex':
array = input.split(new RegExp(splitSeparator)).filter(item => item !== ''); array = input
.split(new RegExp(splitSeparator))
.filter((item) => item !== '');
break; break;
} }
// Trim items if required // Trim items if required
if (trimItems) { if (trimItems) {
array = array.map(item => item.trim()); array = array.map((item) => item.trim());
} }
// Delete empty items after initial split // Delete empty items after initial split
if (deleteEmptyItems) { if (deleteEmptyItems) {
array = array.filter(item => item !== ''); array = array.filter((item) => item !== '');
} }
// Format the output with desired format // Format the output with desired format
const uniqueListItems = uniqueListBuilder(array, caseSensitive, absolutelyUnique); const uniqueListItems = uniqueListBuilder(
array,
caseSensitive,
absolutelyUnique
);
return uniqueListItems.join(joinSeparator); return uniqueListItems.join(joinSeparator);
} }

View File

@@ -4,7 +4,7 @@ import { groupList, SplitOperatorType } from './service';
describe('groupList', () => { describe('groupList', () => {
it('splits by symbol, groups, pads, and formats correctly', () => { it('splits by symbol, groups, pads, and formats correctly', () => {
const input = "a,b,c,d,e,f,g,h,i,j"; const input = 'a,b,c,d,e,f,g,h,i,j';
const splitOperatorType: SplitOperatorType = 'symbol'; const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ','; const splitSeparator = ',';
const groupNumber = 3; const groupNumber = 3;
@@ -16,7 +16,7 @@ describe('groupList', () => {
const padNonFullGroup = true; const padNonFullGroup = true;
const paddingChar = 'x'; const paddingChar = 'x';
const expectedOutput = "[a-b-c] | [d-e-f] | [g-h-i] | [j-x-x]"; const expectedOutput = '[a-b-c] | [d-e-f] | [g-h-i] | [j-x-x]';
const result = groupList( const result = groupList(
splitOperatorType, splitOperatorType,
@@ -36,7 +36,7 @@ describe('groupList', () => {
}); });
it('handles regex split, no padding, and formats correctly', () => { it('handles regex split, no padding, and formats correctly', () => {
const input = "a1b2c3d4e5f6g7h8i9j"; const input = 'a1b2c3d4e5f6g7h8i9j';
const splitOperatorType: SplitOperatorType = 'regex'; const splitOperatorType: SplitOperatorType = 'regex';
const splitSeparator = '\\d'; const splitSeparator = '\\d';
const groupNumber = 4; const groupNumber = 4;
@@ -47,7 +47,7 @@ describe('groupList', () => {
const deleteEmptyItems = true; const deleteEmptyItems = true;
const padNonFullGroup = false; const padNonFullGroup = false;
const expectedOutput = "(a,b,c,d) / (e,f,g,h) / (i,j)"; const expectedOutput = '(a,b,c,d) / (e,f,g,h) / (i,j)';
const result = groupList( const result = groupList(
splitOperatorType, splitOperatorType,
@@ -66,7 +66,7 @@ describe('groupList', () => {
}); });
it('handles empty items removal and padd the last group with a z', () => { it('handles empty items removal and padd the last group with a z', () => {
const input = "a,,b,,c,,d,,e,,"; const input = 'a,,b,,c,,d,,e,,';
const splitOperatorType: SplitOperatorType = 'symbol'; const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ','; const splitSeparator = ',';
const groupNumber = 2; const groupNumber = 2;
@@ -78,7 +78,7 @@ describe('groupList', () => {
const padNonFullGroup = true; const padNonFullGroup = true;
const paddingChar = 'z'; const paddingChar = 'z';
const expectedOutput = "<a:b> & <c:d> & <e:z>"; const expectedOutput = '<a:b> & <c:d> & <e:z>';
const result = groupList( const result = groupList(
splitOperatorType, splitOperatorType,

View File

@@ -1,11 +1,7 @@
export type SplitOperatorType = 'symbol' | 'regex'; export type SplitOperatorType = 'symbol' | 'regex';
// function that split the array into an array of subarray of desired length // function that split the array into an array of subarray of desired length
function groupMaker( function groupMaker(array: string[], groupNumber: number): string[][] {
array: string[],
groupNumber: number,
): string[][] {
const result: string[][] = []; const result: string[][] = [];
for (let i = 0; i < array.length; i += groupNumber) { for (let i = 0; i < array.length; i += groupNumber) {
result.push(array.slice(i, i + groupNumber)); result.push(array.slice(i, i + groupNumber));
@@ -18,7 +14,7 @@ function groupFiller(
array: string[][], array: string[][],
groupNumber: number, groupNumber: number,
padNonFullGroup: boolean, padNonFullGroup: boolean,
paddingChar: string = '', paddingChar: string = ''
): string[][] { ): string[][] {
if (padNonFullGroup) { if (padNonFullGroup) {
const lastSubArray: string[] = array[array.length - 1]; const lastSubArray: string[] = array[array.length - 1];
@@ -30,7 +26,6 @@ function groupFiller(
array[array.length - 1] = lastSubArray; array[array.length - 1] = lastSubArray;
} }
return array; return array;
} }
// function that join with the item separator and wrap with left and right each subArray of the Array // function that join with the item separator and wrap with left and right each subArray of the Array
@@ -38,14 +33,13 @@ function groupJoinerAndWrapper(
array: string[][], array: string[][],
itemSeparator: string = '', itemSeparator: string = '',
leftWrap: string = '', leftWrap: string = '',
rightWrap: string = '', rightWrap: string = ''
): string[] { ): string[] {
return array.map(subArray => { return array.map((subArray) => {
return leftWrap + subArray.join(itemSeparator) + rightWrap; return leftWrap + subArray.join(itemSeparator) + rightWrap;
}); });
} }
export function groupList( export function groupList(
splitOperatorType: SplitOperatorType, splitOperatorType: SplitOperatorType,
splitSeparator: string, splitSeparator: string,
@@ -57,8 +51,7 @@ export function groupList(
groupSeparator: string, groupSeparator: string,
deleteEmptyItems: boolean, deleteEmptyItems: boolean,
padNonFullGroup: boolean, padNonFullGroup: boolean,
paddingChar: string = '', paddingChar: string = ''
): string { ): string {
let array: string[]; let array: string[];
let splitedArray: string[][]; let splitedArray: string[][];
@@ -74,19 +67,28 @@ export function groupList(
} }
// delete empty items after intial split // delete empty items after intial split
if (deleteEmptyItems) { if (deleteEmptyItems) {
array = array.filter(item => item !== ''); array = array.filter((item) => item !== '');
} }
// split the input into an array of subArray with the desired length // split the input into an array of subArray with the desired length
splitedArray = groupMaker(array, groupNumber); splitedArray = groupMaker(array, groupNumber);
// fill the last subArray is PadNonFullGroup is enabled // fill the last subArray is PadNonFullGroup is enabled
fullSplitedArray = groupFiller(splitedArray, groupNumber, padNonFullGroup, paddingChar); fullSplitedArray = groupFiller(
splitedArray,
groupNumber,
padNonFullGroup,
paddingChar
);
// get the list of formated subArray with the item separator and left and right wrapper // get the list of formated subArray with the item separator and left and right wrapper
result = groupJoinerAndWrapper(fullSplitedArray, itemSeparator, leftWrap, rightWrap); result = groupJoinerAndWrapper(
fullSplitedArray,
itemSeparator,
leftWrap,
rightWrap
);
// finnaly join the group separator before returning // finnaly join the group separator before returning
return result.join(groupSeparator); return result.join(groupSeparator);
} }

View File

@@ -4,7 +4,7 @@ export function reverseList(
splitOperatorType: SplitOperatorType, splitOperatorType: SplitOperatorType,
splitSeparator: string, splitSeparator: string,
joinSeparator: string = '\n', joinSeparator: string = '\n',
input: string, input: string
): string { ): string {
let array: string[] = []; let array: string[] = [];
switch (splitOperatorType) { switch (splitOperatorType) {
@@ -12,7 +12,9 @@ export function reverseList(
array = input.split(splitSeparator); array = input.split(splitSeparator);
break; break;
case 'regex': case 'regex':
array = input.split(new RegExp(splitSeparator)).filter(item => item !== ''); array = input
.split(new RegExp(splitSeparator))
.filter((item) => item !== '');
break; break;
} }

View File

@@ -1,8 +1,5 @@
import { expect, describe, it } from 'vitest'; import { expect, describe, it } from 'vitest';
import { import { SplitOperatorType, rotateList } from './service';
SplitOperatorType,
rotateList
} from './service';
describe('rotate function', () => { describe('rotate function', () => {
it('should rotate right side if right is set to true', () => { it('should rotate right side if right is set to true', () => {
@@ -20,10 +17,10 @@ describe('rotate function', () => {
joinSeparator, joinSeparator,
right, right,
step); step
);
expect(result).toBe('mango apple pineaple lemon orange'); expect(result).toBe('mango apple pineaple lemon orange');
}); });
it('should rotate left side if right is set to true', () => { it('should rotate left side if right is set to true', () => {
@@ -41,10 +38,10 @@ describe('rotate function', () => {
joinSeparator, joinSeparator,
right, right,
step); step
);
expect(result).toBe('pineaple lemon orange mango apple'); expect(result).toBe('pineaple lemon orange mango apple');
}); });
it('should rotate left side with 2 step if right is set to true', () => { it('should rotate left side with 2 step if right is set to true', () => {
@@ -62,10 +59,10 @@ describe('rotate function', () => {
joinSeparator, joinSeparator,
right, right,
step); step
);
expect(result).toBe('lemon orange mango apple pineaple'); expect(result).toBe('lemon orange mango apple pineaple');
}); });
it('should raise an error if step is negative', () => { it('should raise an error if step is negative', () => {
@@ -83,9 +80,9 @@ describe('rotate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
right, right,
step); step
);
}).toThrowError('Rotation step must be greater than zero.'); }).toThrowError('Rotation step must be greater than zero.');
}); });
it('should raise an error if step is undefined', () => { it('should raise an error if step is undefined', () => {
@@ -101,10 +98,8 @@ describe('rotate function', () => {
input, input,
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
right); right
);
}).toThrowError('Rotation step contains non-digits.'); }).toThrowError('Rotation step contains non-digits.');
}); });
});
})

View File

@@ -1,10 +1,7 @@
import { isNumber } from 'utils/string'; import { isNumber } from 'utils/string';
export type SplitOperatorType = 'symbol' | 'regex'; export type SplitOperatorType = 'symbol' | 'regex';
function rotateArray( function rotateArray(array: string[], step: number, right: boolean): string[] {
array: string[],
step: number,
right: boolean): string[] {
const length = array.length; const length = array.length;
// Normalize the step to be within the bounds of the array length // Normalize the step to be within the bounds of the array length
@@ -12,10 +9,14 @@ function rotateArray(
if (right) { if (right) {
// Rotate right // Rotate right
return array.slice(-normalizedPositions).concat(array.slice(0, -normalizedPositions)); return array
.slice(-normalizedPositions)
.concat(array.slice(0, -normalizedPositions));
} else { } else {
// Rotate left // Rotate left
return array.slice(normalizedPositions).concat(array.slice(0, normalizedPositions)); return array
.slice(normalizedPositions)
.concat(array.slice(0, normalizedPositions));
} }
} }
@@ -25,7 +26,7 @@ export function rotateList(
splitSeparator: string, splitSeparator: string,
joinSeparator: string, joinSeparator: string,
right: boolean, right: boolean,
step?: number, step?: number
): string { ): string {
let array: string[]; let array: string[];
let rotatedArray: string[]; let rotatedArray: string[];
@@ -39,10 +40,10 @@ export function rotateList(
} }
if (step !== undefined) { if (step !== undefined) {
if (step <= 0) { if (step <= 0) {
throw new Error("Rotation step must be greater than zero."); throw new Error('Rotation step must be greater than zero.');
} }
rotatedArray = rotateArray(array, step, right); rotatedArray = rotateArray(array, step, right);
return rotatedArray.join(joinSeparator); return rotatedArray.join(joinSeparator);
} }
throw new Error("Rotation step contains non-digits.") throw new Error('Rotation step contains non-digits.');
} }

View File

@@ -2,7 +2,7 @@ export type SplitOperatorType = 'symbol' | 'regex';
// function that randomize the array // function that randomize the array
function shuffleArray(array: string[]): string[] { function shuffleArray(array: string[]): string[] {
let shuffledArray = array.slice(); // Create a copy of the array const shuffledArray = array.slice(); // Create a copy of the array
for (let i = shuffledArray.length - 1; i > 0; i--) { for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1)); const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
@@ -15,7 +15,7 @@ export function shuffleList(
input: string, input: string,
splitSeparator: string, splitSeparator: string,
joinSeparator: string, joinSeparator: string,
length?: number, // "?" is to handle the case the user let the input blank length?: number // "?" is to handle the case the user let the input blank
): string { ): string {
let array: string[]; let array: string[];
let shuffledArray: string[]; let shuffledArray: string[];
@@ -30,7 +30,7 @@ export function shuffleList(
shuffledArray = shuffleArray(array); shuffledArray = shuffleArray(array);
if (length !== undefined) { if (length !== undefined) {
if (length <= 0) { if (length <= 0) {
throw new Error("Length value must be a positive number."); throw new Error('Length value must be a positive number.');
} }
return shuffledArray.slice(0, length).join(joinSeparator); return shuffledArray.slice(0, length).join(joinSeparator);
} }

View File

@@ -1,11 +1,7 @@
import { expect, describe, it } from 'vitest'; import { expect, describe, it } from 'vitest';
import { import { shuffleList, SplitOperatorType } from './service';
shuffleList,
SplitOperatorType
} from './service';
describe('shuffle function', () => { describe('shuffle function', () => {
it('should be a 4 length list if no length value defined ', () => { it('should be a 4 length list if no length value defined ', () => {
const input: string = 'apple, pineaple, lemon, orange'; const input: string = 'apple, pineaple, lemon, orange';
const splitOperatorType: SplitOperatorType = 'symbol'; const splitOperatorType: SplitOperatorType = 'symbol';
@@ -90,5 +86,4 @@ describe('shuffle function', () => {
console.log(result); console.log(result);
expect(result).toBe(''); expect(result).toBe('');
}); });
});
})

View File

@@ -6,7 +6,7 @@ export function truncateList(
splitSeparator: string, splitSeparator: string,
joinSeparator: string, joinSeparator: string,
end: boolean, end: boolean,
length?: number, length?: number
): string { ): string {
let array: string[]; let array: string[];
let truncatedArray: string[]; let truncatedArray: string[];
@@ -20,11 +20,12 @@ export function truncateList(
} }
if (length !== undefined) { if (length !== undefined) {
if (length < 0) { if (length < 0) {
throw new Error("Length value must be a positive number.") throw new Error('Length value must be a positive number.');
} }
truncatedArray = end ? array.slice(0, length) : array.slice(array.length - length, array.length); truncatedArray = end
? array.slice(0, length)
: array.slice(array.length - length, array.length);
return truncatedArray.join(joinSeparator); return truncatedArray.join(joinSeparator);
} }
throw new Error("Length value isn't a value number."); throw new Error("Length value isn't a value number.");
} }

View File

@@ -1,9 +1,6 @@
import { expect, describe, it } from 'vitest'; import { expect, describe, it } from 'vitest';
import { import { SplitOperatorType, truncateList } from './service';
SplitOperatorType,
truncateList,
} from './service';
describe('truncate function', () => { describe('truncate function', () => {
it('should remove at the end (one element) if end is set to true', () => { it('should remove at the end (one element) if end is set to true', () => {
@@ -20,10 +17,10 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length); length
);
expect(result).toBe('apple pineaple lemon'); expect(result).toBe('apple pineaple lemon');
}); });
it('should return 3 elements from the start if end is set to true', () => { it('should return 3 elements from the start if end is set to true', () => {
@@ -40,10 +37,10 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length); length
);
expect(result).toBe('apple pineaple lemon'); expect(result).toBe('apple pineaple lemon');
}); });
it('should return 3 elements from the start if end is set to true', () => { it('should return 3 elements from the start if end is set to true', () => {
@@ -60,10 +57,10 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length); length
);
expect(result).toBe('apple pineaple lemon'); expect(result).toBe('apple pineaple lemon');
}); });
it('should return 3 elements from the end if end is set to true', () => { it('should return 3 elements from the end if end is set to true', () => {
@@ -80,10 +77,10 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length); length
);
expect(result).toBe('lemon orange mango'); expect(result).toBe('lemon orange mango');
}); });
it('should return a void string if length is set to 0', () => { it('should return a void string if length is set to 0', () => {
@@ -100,10 +97,10 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length); length
);
expect(result).toBe(''); expect(result).toBe('');
}); });
it('should return an element (first) string if length is set to 1 and end is set to true', () => { it('should return an element (first) string if length is set to 1 and end is set to true', () => {
@@ -120,10 +117,10 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length); length
);
expect(result).toBe('apple'); expect(result).toBe('apple');
}); });
it('should return an element (last) string if length is set to 1 and end is set to false', () => { it('should return an element (last) string if length is set to 1 and end is set to false', () => {
@@ -140,10 +137,10 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length); length
);
expect(result).toBe('mango'); expect(result).toBe('mango');
}); });
it('should throw an error if the length value is negative', () => { it('should throw an error if the length value is negative', () => {
@@ -161,8 +158,9 @@ describe('truncate function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end, end,
length) length
}).toThrow("Length value must be a positive number."); );
}).toThrow('Length value must be a positive number.');
}); });
it('should throw an error if the length value is left blank', () => { it('should throw an error if the length value is left blank', () => {
@@ -178,8 +176,8 @@ describe('truncate function', () => {
input, input,
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
end) end
);
}).toThrow("Length value isn't a value number."); }).toThrow("Length value isn't a value number.");
}); });
});
})

View File

@@ -63,7 +63,7 @@ export function unwrapList(
} }
// trim items if needed // trim items if needed
if (trimItems) { if (trimItems) {
unwrappedArray = unwrappedArray.map(item => item.trim()); unwrappedArray = unwrappedArray.map((item) => item.trim());
} }
return unwrappedArray.join(joinSeparator); return unwrappedArray.join(joinSeparator);
} }

View File

@@ -3,68 +3,168 @@ import { unwrapList } from './service';
describe('unwrapList function', () => { describe('unwrapList function', () => {
it('should unwrap elements correctly with symbol split', () => { it('should unwrap elements correctly with symbol split', () => {
const input = "##Hello##\n##World##"; const input = '##Hello##\n##World##';
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello World"); 'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
}); });
it('should unwrap elements correctly with regex split', () => { it('should unwrap elements correctly with regex split', () => {
const input = "##Hello##||##World##"; const input = '##Hello##||##World##';
const result = unwrapList('regex', input, '\\|\\|', ' ', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello World"); 'regex',
input,
'\\|\\|',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
}); });
it('should handle multiple levels of unwrapping', () => { it('should handle multiple levels of unwrapping', () => {
const input = "###Hello###"; const input = '###Hello###';
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello"); 'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello');
}); });
it('should handle single level of unwrapping', () => { it('should handle single level of unwrapping', () => {
const input = "###Hello###"; const input = '###Hello###';
const result = unwrapList('symbol', input, '\n', ' ', true, false, true, '#', '#'); const result = unwrapList(
expect(result).toBe("##Hello##"); 'symbol',
input,
'\n',
' ',
true,
false,
true,
'#',
'#'
);
expect(result).toBe('##Hello##');
}); });
it('should delete empty items', () => { it('should delete empty items', () => {
const input = "##Hello##\n\n##World##"; const input = '##Hello##\n\n##World##';
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello World"); 'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
}); });
it('should keep empty items if deleteEmptyItems is false', () => { it('should keep empty items if deleteEmptyItems is false', () => {
const input = "##Hello##\n\n##World##"; const input = '##Hello##\n\n##World##';
const result = unwrapList('symbol', input, '\n', ' ', false, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello World"); 'symbol',
input,
'\n',
' ',
false,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
}); });
it('should trim items', () => { it('should trim items', () => {
const input = "## Hello ##\n## World ##"; const input = '## Hello ##\n## World ##';
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello World"); 'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
}); });
it('should handle no left or right unwrapping', () => { it('should handle no left or right unwrapping', () => {
const input = "Hello\nWorld"; const input = 'Hello\nWorld';
const result = unwrapList('symbol', input, '\n', ' ', true, true, true); const result = unwrapList('symbol', input, '\n', ' ', true, true, true);
expect(result).toBe("Hello World"); expect(result).toBe('Hello World');
}); });
it('should handle mixed levels of unwrapping', () => { it('should handle mixed levels of unwrapping', () => {
const input = "###Hello##\n#World###"; const input = '###Hello##\n#World###';
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello World"); 'symbol',
input,
'\n',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World');
}); });
it('should handle complex regex split', () => { it('should handle complex regex split', () => {
const input = "##Hello##||###World###||####Test####"; const input = '##Hello##||###World###||####Test####';
const result = unwrapList('regex', input, '\\|\\|', ' ', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello World Test"); 'regex',
input,
'\\|\\|',
' ',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello World Test');
}); });
it('should handle different joinSeparator', () => { it('should handle different joinSeparator', () => {
const input = "##Hello##\n##World##"; const input = '##Hello##\n##World##';
const result = unwrapList('symbol', input, '\n', '-', true, true, true, '#', '#'); const result = unwrapList(
expect(result).toBe("Hello-World"); 'symbol',
input,
'\n',
'-',
true,
true,
true,
'#',
'#'
);
expect(result).toBe('Hello-World');
}); });
}); });

View File

@@ -11,8 +11,7 @@ export function wrapList(
joinSeparator: string, joinSeparator: string,
deleteEmptyItems: boolean, deleteEmptyItems: boolean,
left: string = '', left: string = '',
right: string = '', right: string = ''
): string { ): string {
let array: string[]; let array: string[];
let wrappedArray: string[]; let wrappedArray: string[];

View File

@@ -1,8 +1,5 @@
import { expect, describe, it } from 'vitest'; import { expect, describe, it } from 'vitest';
import { import { SplitOperatorType, wrapList } from './service';
SplitOperatorType,
wrapList
} from './service';
describe('wrap function', () => { describe('wrap function', () => {
it('should return the same input if no left and right are blanked', () => { it('should return the same input if no left and right are blanked', () => {
@@ -12,7 +9,6 @@ describe('wrap function', () => {
const joinSeparator = ', '; const joinSeparator = ', ';
const deleteEmptyItems = false; const deleteEmptyItems = false;
const result = wrapList( const result = wrapList(
splitOperatorType, splitOperatorType,
input, input,
@@ -22,7 +18,6 @@ describe('wrap function', () => {
); );
expect(result).toBe('apple, pineaple, lemon, orange, mango'); expect(result).toBe('apple, pineaple, lemon, orange, mango');
}); });
it('should append to left if defined', () => { it('should append to left if defined', () => {
@@ -39,10 +34,12 @@ describe('wrap function', () => {
splitSeparator, splitSeparator,
joinSeparator, joinSeparator,
deleteEmptyItems, deleteEmptyItems,
left); left
);
expect(result).toBe('the apple, the pineaple, the lemon, the orange, the mango');
expect(result).toBe(
'the apple, the pineaple, the lemon, the orange, the mango'
);
}); });
it('should append to right if defined', () => { it('should append to right if defined', () => {
@@ -61,7 +58,8 @@ describe('wrap function', () => {
joinSeparator, joinSeparator,
deleteEmptyItems, deleteEmptyItems,
left, left,
right); right
);
expect(result).toBe('applez, pineaplez, lemonz, orangez, mangoz'); expect(result).toBe('applez, pineaplez, lemonz, orangez, mangoz');
}); });
@@ -82,10 +80,10 @@ describe('wrap function', () => {
joinSeparator, joinSeparator,
deleteEmptyItems, deleteEmptyItems,
left, left,
right); right
);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz'); expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
}); });
it('should append to both side if both defined and not delete empty items', () => { it('should append to both side if both defined and not delete empty items', () => {
@@ -104,13 +102,12 @@ describe('wrap function', () => {
joinSeparator, joinSeparator,
deleteEmptyItems, deleteEmptyItems,
left, left,
right); right
);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz, Kz'); expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz, Kz');
}); });
it('should append to both side if both defined and delete empty items', () => { it('should append to both side if both defined and delete empty items', () => {
const input: string = 'apple, pineaple, lemon, , orange, mango'; const input: string = 'apple, pineaple, lemon, , orange, mango';
const splitOperatorType: SplitOperatorType = 'symbol'; const splitOperatorType: SplitOperatorType = 'symbol';
@@ -127,11 +124,9 @@ describe('wrap function', () => {
joinSeparator, joinSeparator,
deleteEmptyItems, deleteEmptyItems,
left, left,
right); right
);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz'); expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
}); });
});
})