mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 14:09:31 +02:00
fix: style
This commit is contained in:
31
.idea/workspace.xml
generated
31
.idea/workspace.xml
generated
@@ -4,13 +4,8 @@
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<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$/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>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@@ -206,15 +201,7 @@
|
||||
<workItem from="1719492452780" duration="8000" />
|
||||
<workItem from="1719496624579" duration="6148000" />
|
||||
<workItem from="1720542757452" duration="5355000" />
|
||||
<workItem from="1720557527691" duration="1135000" />
|
||||
</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>
|
||||
<workItem from="1720557527691" duration="3245000" />
|
||||
</task>
|
||||
<task id="LOCAL-00042" summary="ci: fix">
|
||||
<option name="closed" value="true" />
|
||||
@@ -600,7 +587,15 @@
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1720546921899</updated>
|
||||
</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 />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
@@ -658,7 +653,6 @@
|
||||
<MESSAGE value="feat: change gif speed" />
|
||||
<MESSAGE value="chore: remove unused deps" />
|
||||
<MESSAGE value="style: lint" />
|
||||
<MESSAGE value="fix: misc" />
|
||||
<MESSAGE value="fix: radio and list sort init" />
|
||||
<MESSAGE value="chore: formik updateField" />
|
||||
<MESSAGE value="ci: run e2e tests" />
|
||||
@@ -669,7 +663,8 @@
|
||||
<MESSAGE value="chore: idea config" />
|
||||
<MESSAGE value="feat: sort list" />
|
||||
<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 name="XSLT-Support.FileAssociations.UIState">
|
||||
<expand />
|
||||
|
@@ -3,5 +3,5 @@ a {
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #030362
|
||||
color: #030362;
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ function displayFormater(
|
||||
dict: { [key: string]: number },
|
||||
displayFormat: DisplayFormat
|
||||
): string[] {
|
||||
let formattedOutput: string[] = [];
|
||||
const formattedOutput: string[] = [];
|
||||
const total = Object.values(dict).reduce((acc, val) => acc + val, 0);
|
||||
|
||||
switch (displayFormat) {
|
||||
|
@@ -5,43 +5,106 @@ import { TopItemsList } from './service';
|
||||
describe('TopItemsList Function', () => {
|
||||
test('should return unique items ignoring case sensitivity', () => {
|
||||
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');
|
||||
});
|
||||
|
||||
test('should return unique items considering case sensitivity', () => {
|
||||
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');
|
||||
});
|
||||
|
||||
test('should return all unique items ignoring case sensitivity', () => {
|
||||
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');
|
||||
});
|
||||
|
||||
test('should return all unique items considering case sensitivity', () => {
|
||||
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');
|
||||
});
|
||||
|
||||
test('should handle empty items deletion', () => {
|
||||
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');
|
||||
});
|
||||
|
||||
test('should handle trimming items', () => {
|
||||
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 ');
|
||||
});
|
||||
|
||||
test('should handle regex split', () => {
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
@@ -2,57 +2,63 @@ export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
// Function that builds the unique items array handling caseSensitive and absolutelyUnique options
|
||||
function uniqueListBuilder(
|
||||
array: string[],
|
||||
caseSensitive: boolean,
|
||||
absolutelyUnique: boolean
|
||||
array: string[],
|
||||
caseSensitive: boolean,
|
||||
absolutelyUnique: boolean
|
||||
): string[] {
|
||||
const dict: { [key: string]: number } = {};
|
||||
for (const item of array) {
|
||||
const key = caseSensitive ? item : item.toLowerCase();
|
||||
dict[key] = (dict[key] || 0) + 1;
|
||||
const dict: { [key: string]: number } = {};
|
||||
for (const item of array) {
|
||||
const key = caseSensitive ? item : item.toLowerCase();
|
||||
dict[key] = (dict[key] || 0) + 1;
|
||||
}
|
||||
if (absolutelyUnique) {
|
||||
for (const [key, value] of Object.entries(dict)) {
|
||||
if (value > 1) {
|
||||
delete dict[key];
|
||||
}
|
||||
}
|
||||
if (absolutelyUnique) {
|
||||
for (const [key, value] of Object.entries(dict)) {
|
||||
if (value > 1) {
|
||||
delete dict[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return Object.keys(dict);
|
||||
}
|
||||
return Object.keys(dict);
|
||||
}
|
||||
|
||||
export function TopItemsList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string = '\n',
|
||||
input: string,
|
||||
deleteEmptyItems: boolean,
|
||||
trimItems: boolean,
|
||||
caseSensitive: boolean,
|
||||
absolutelyUnique: boolean
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string = '\n',
|
||||
input: string,
|
||||
deleteEmptyItems: boolean,
|
||||
trimItems: boolean,
|
||||
caseSensitive: boolean,
|
||||
absolutelyUnique: boolean
|
||||
): string {
|
||||
let array: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator)).filter(item => item !== '');
|
||||
break;
|
||||
}
|
||||
let array: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input
|
||||
.split(new RegExp(splitSeparator))
|
||||
.filter((item) => item !== '');
|
||||
break;
|
||||
}
|
||||
|
||||
// Trim items if required
|
||||
if (trimItems) {
|
||||
array = array.map(item => item.trim());
|
||||
}
|
||||
// Trim items if required
|
||||
if (trimItems) {
|
||||
array = array.map((item) => item.trim());
|
||||
}
|
||||
|
||||
// Delete empty items after initial split
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter(item => item !== '');
|
||||
}
|
||||
// Delete empty items after initial split
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter((item) => item !== '');
|
||||
}
|
||||
|
||||
// Format the output with desired format
|
||||
const uniqueListItems = uniqueListBuilder(array, caseSensitive, absolutelyUnique);
|
||||
// Format the output with desired format
|
||||
const uniqueListItems = uniqueListBuilder(
|
||||
array,
|
||||
caseSensitive,
|
||||
absolutelyUnique
|
||||
);
|
||||
|
||||
return uniqueListItems.join(joinSeparator);
|
||||
return uniqueListItems.join(joinSeparator);
|
||||
}
|
||||
|
@@ -3,97 +3,97 @@ import { expect, describe, it } from 'vitest';
|
||||
import { groupList, SplitOperatorType } from './service';
|
||||
|
||||
describe('groupList', () => {
|
||||
it('splits by symbol, groups, pads, and formats correctly', () => {
|
||||
const input = "a,b,c,d,e,f,g,h,i,j";
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ',';
|
||||
const groupNumber = 3;
|
||||
const itemSeparator = '-';
|
||||
const leftWrap = '[';
|
||||
const rightWrap = ']';
|
||||
const groupSeparator = ' | ';
|
||||
const deleteEmptyItems = false;
|
||||
const padNonFullGroup = true;
|
||||
const paddingChar = 'x';
|
||||
it('splits by symbol, groups, pads, and formats correctly', () => {
|
||||
const input = 'a,b,c,d,e,f,g,h,i,j';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ',';
|
||||
const groupNumber = 3;
|
||||
const itemSeparator = '-';
|
||||
const leftWrap = '[';
|
||||
const rightWrap = ']';
|
||||
const groupSeparator = ' | ';
|
||||
const deleteEmptyItems = false;
|
||||
const padNonFullGroup = true;
|
||||
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(
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
input,
|
||||
groupNumber,
|
||||
itemSeparator,
|
||||
leftWrap,
|
||||
rightWrap,
|
||||
groupSeparator,
|
||||
deleteEmptyItems,
|
||||
padNonFullGroup,
|
||||
paddingChar
|
||||
);
|
||||
const result = groupList(
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
input,
|
||||
groupNumber,
|
||||
itemSeparator,
|
||||
leftWrap,
|
||||
rightWrap,
|
||||
groupSeparator,
|
||||
deleteEmptyItems,
|
||||
padNonFullGroup,
|
||||
paddingChar
|
||||
);
|
||||
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
it('handles regex split, no padding, and formats correctly', () => {
|
||||
const input = "a1b2c3d4e5f6g7h8i9j";
|
||||
const splitOperatorType: SplitOperatorType = 'regex';
|
||||
const splitSeparator = '\\d';
|
||||
const groupNumber = 4;
|
||||
const itemSeparator = ',';
|
||||
const leftWrap = '(';
|
||||
const rightWrap = ')';
|
||||
const groupSeparator = ' / ';
|
||||
const deleteEmptyItems = true;
|
||||
const padNonFullGroup = false;
|
||||
it('handles regex split, no padding, and formats correctly', () => {
|
||||
const input = 'a1b2c3d4e5f6g7h8i9j';
|
||||
const splitOperatorType: SplitOperatorType = 'regex';
|
||||
const splitSeparator = '\\d';
|
||||
const groupNumber = 4;
|
||||
const itemSeparator = ',';
|
||||
const leftWrap = '(';
|
||||
const rightWrap = ')';
|
||||
const groupSeparator = ' / ';
|
||||
const deleteEmptyItems = true;
|
||||
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(
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
input,
|
||||
groupNumber,
|
||||
itemSeparator,
|
||||
leftWrap,
|
||||
rightWrap,
|
||||
groupSeparator,
|
||||
deleteEmptyItems,
|
||||
padNonFullGroup
|
||||
);
|
||||
const result = groupList(
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
input,
|
||||
groupNumber,
|
||||
itemSeparator,
|
||||
leftWrap,
|
||||
rightWrap,
|
||||
groupSeparator,
|
||||
deleteEmptyItems,
|
||||
padNonFullGroup
|
||||
);
|
||||
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
|
||||
it('handles empty items removal and padd the last group with a z', () => {
|
||||
const input = "a,,b,,c,,d,,e,,";
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ',';
|
||||
const groupNumber = 2;
|
||||
const itemSeparator = ':';
|
||||
const leftWrap = '<';
|
||||
const rightWrap = '>';
|
||||
const groupSeparator = ' & ';
|
||||
const deleteEmptyItems = true;
|
||||
const padNonFullGroup = true;
|
||||
const paddingChar = 'z';
|
||||
it('handles empty items removal and padd the last group with a z', () => {
|
||||
const input = 'a,,b,,c,,d,,e,,';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ',';
|
||||
const groupNumber = 2;
|
||||
const itemSeparator = ':';
|
||||
const leftWrap = '<';
|
||||
const rightWrap = '>';
|
||||
const groupSeparator = ' & ';
|
||||
const deleteEmptyItems = true;
|
||||
const padNonFullGroup = true;
|
||||
const paddingChar = 'z';
|
||||
|
||||
const expectedOutput = "<a:b> & <c:d> & <e:z>";
|
||||
const expectedOutput = '<a:b> & <c:d> & <e:z>';
|
||||
|
||||
const result = groupList(
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
input,
|
||||
groupNumber,
|
||||
itemSeparator,
|
||||
leftWrap,
|
||||
rightWrap,
|
||||
groupSeparator,
|
||||
deleteEmptyItems,
|
||||
padNonFullGroup,
|
||||
paddingChar
|
||||
);
|
||||
const result = groupList(
|
||||
splitOperatorType,
|
||||
splitSeparator,
|
||||
input,
|
||||
groupNumber,
|
||||
itemSeparator,
|
||||
leftWrap,
|
||||
rightWrap,
|
||||
groupSeparator,
|
||||
deleteEmptyItems,
|
||||
padNonFullGroup,
|
||||
paddingChar
|
||||
);
|
||||
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
});
|
@@ -1,92 +1,94 @@
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
|
||||
// function that split the array into an array of subarray of desired length
|
||||
function groupMaker(
|
||||
array: string[],
|
||||
groupNumber: number,
|
||||
): string[][] {
|
||||
const result: string[][] = [];
|
||||
for (let i = 0; i < array.length; i += groupNumber) {
|
||||
result.push(array.slice(i, i + groupNumber));
|
||||
}
|
||||
return result;
|
||||
function groupMaker(array: string[], groupNumber: number): string[][] {
|
||||
const result: string[][] = [];
|
||||
for (let i = 0; i < array.length; i += groupNumber) {
|
||||
result.push(array.slice(i, i + groupNumber));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// function use to handle the case paddingNonFullGroup is enable
|
||||
function groupFiller(
|
||||
array: string[][],
|
||||
groupNumber: number,
|
||||
padNonFullGroup: boolean,
|
||||
paddingChar: string = '',
|
||||
array: string[][],
|
||||
groupNumber: number,
|
||||
padNonFullGroup: boolean,
|
||||
paddingChar: string = ''
|
||||
): string[][] {
|
||||
if (padNonFullGroup) {
|
||||
const lastSubArray: string[] = array[array.length - 1];
|
||||
if (lastSubArray.length < groupNumber) {
|
||||
for (let i = lastSubArray.length; i < groupNumber; i++) {
|
||||
lastSubArray.push(paddingChar);
|
||||
}
|
||||
}
|
||||
array[array.length - 1] = lastSubArray;
|
||||
if (padNonFullGroup) {
|
||||
const lastSubArray: string[] = array[array.length - 1];
|
||||
if (lastSubArray.length < groupNumber) {
|
||||
for (let i = lastSubArray.length; i < groupNumber; i++) {
|
||||
lastSubArray.push(paddingChar);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
|
||||
array[array.length - 1] = lastSubArray;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// function that join with the item separator and wrap with left and right each subArray of the Array
|
||||
function groupJoinerAndWrapper(
|
||||
array: string[][],
|
||||
itemSeparator: string = '',
|
||||
leftWrap: string = '',
|
||||
rightWrap: string = '',
|
||||
array: string[][],
|
||||
itemSeparator: string = '',
|
||||
leftWrap: string = '',
|
||||
rightWrap: string = ''
|
||||
): string[] {
|
||||
return array.map(subArray => {
|
||||
return leftWrap + subArray.join(itemSeparator) + rightWrap;
|
||||
});
|
||||
return array.map((subArray) => {
|
||||
return leftWrap + subArray.join(itemSeparator) + rightWrap;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function groupList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
input: string,
|
||||
groupNumber: number,
|
||||
itemSeparator: string = '',
|
||||
leftWrap: string = '',
|
||||
rightWrap: string = '',
|
||||
groupSeparator: string,
|
||||
deleteEmptyItems: boolean,
|
||||
padNonFullGroup: boolean,
|
||||
paddingChar: string = '',
|
||||
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
input: string,
|
||||
groupNumber: number,
|
||||
itemSeparator: string = '',
|
||||
leftWrap: string = '',
|
||||
rightWrap: string = '',
|
||||
groupSeparator: string,
|
||||
deleteEmptyItems: boolean,
|
||||
padNonFullGroup: boolean,
|
||||
paddingChar: string = ''
|
||||
): string {
|
||||
let array: string[];
|
||||
let splitedArray: string[][];
|
||||
let fullSplitedArray: string[][];
|
||||
let result: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
// delete empty items after intial split
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter(item => item !== '');
|
||||
}
|
||||
let array: string[];
|
||||
let splitedArray: string[][];
|
||||
let fullSplitedArray: string[][];
|
||||
let result: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
// delete empty items after intial split
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter((item) => item !== '');
|
||||
}
|
||||
|
||||
// split the input into an array of subArray with the desired length
|
||||
splitedArray = groupMaker(array, groupNumber);
|
||||
// split the input into an array of subArray with the desired length
|
||||
splitedArray = groupMaker(array, groupNumber);
|
||||
|
||||
// fill the last subArray is PadNonFullGroup is enabled
|
||||
fullSplitedArray = groupFiller(splitedArray, groupNumber, padNonFullGroup, paddingChar);
|
||||
// fill the last subArray is PadNonFullGroup is enabled
|
||||
fullSplitedArray = groupFiller(
|
||||
splitedArray,
|
||||
groupNumber,
|
||||
padNonFullGroup,
|
||||
paddingChar
|
||||
);
|
||||
|
||||
// get the list of formated subArray with the item separator and left and right wrapper
|
||||
result = groupJoinerAndWrapper(fullSplitedArray, itemSeparator, leftWrap, rightWrap);
|
||||
// get the list of formated subArray with the item separator and left and right wrapper
|
||||
result = groupJoinerAndWrapper(
|
||||
fullSplitedArray,
|
||||
itemSeparator,
|
||||
leftWrap,
|
||||
rightWrap
|
||||
);
|
||||
|
||||
// finnaly join the group separator before returning
|
||||
return result.join(groupSeparator);
|
||||
// finnaly join the group separator before returning
|
||||
return result.join(groupSeparator);
|
||||
}
|
||||
|
||||
|
@@ -2,27 +2,27 @@ import { expect, describe, it } from 'vitest';
|
||||
import { reverseList } from './service';
|
||||
|
||||
describe('reverseList Function', () => {
|
||||
test('should reverse items split by symbol', () => {
|
||||
const input = 'apple,banana,orange';
|
||||
const result = reverseList('symbol', ',', '\n', input);
|
||||
expect(result).toBe('orange\nbanana\napple');
|
||||
});
|
||||
|
||||
test('should reverse items split by regex', () => {
|
||||
const input = 'apple banana orange';
|
||||
const result = reverseList('regex', '\\s+', '\n', input);
|
||||
expect(result).toBe('orange\nbanana\napple');
|
||||
});
|
||||
|
||||
test('should handle empty input', () => {
|
||||
const input = '';
|
||||
const result = reverseList('symbol', ',', '\n', input);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
test('should handle join separator', () => {
|
||||
const input = 'apple,banana,orange';
|
||||
const result = reverseList('symbol', ',', ', ', input);
|
||||
expect(result).toBe('orange, banana, apple');
|
||||
});
|
||||
test('should reverse items split by symbol', () => {
|
||||
const input = 'apple,banana,orange';
|
||||
const result = reverseList('symbol', ',', '\n', input);
|
||||
expect(result).toBe('orange\nbanana\napple');
|
||||
});
|
||||
|
||||
test('should reverse items split by regex', () => {
|
||||
const input = 'apple banana orange';
|
||||
const result = reverseList('regex', '\\s+', '\n', input);
|
||||
expect(result).toBe('orange\nbanana\napple');
|
||||
});
|
||||
|
||||
test('should handle empty input', () => {
|
||||
const input = '';
|
||||
const result = reverseList('symbol', ',', '\n', input);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
test('should handle join separator', () => {
|
||||
const input = 'apple,banana,orange';
|
||||
const result = reverseList('symbol', ',', ', ', input);
|
||||
expect(result).toBe('orange, banana, apple');
|
||||
});
|
||||
});
|
||||
|
@@ -1,21 +1,23 @@
|
||||
type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
export function reverseList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string = '\n',
|
||||
input: string,
|
||||
splitOperatorType: SplitOperatorType,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string = '\n',
|
||||
input: string
|
||||
): string {
|
||||
let array: string[] = [];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator)).filter(item => item !== '');
|
||||
break;
|
||||
}
|
||||
let array: string[] = [];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input
|
||||
.split(new RegExp(splitSeparator))
|
||||
.filter((item) => item !== '');
|
||||
break;
|
||||
}
|
||||
|
||||
const reversedList = array.reverse();
|
||||
return reversedList.join(joinSeparator);
|
||||
const reversedList = array.reverse();
|
||||
return reversedList.join(joinSeparator);
|
||||
}
|
||||
|
@@ -1,110 +1,105 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import {
|
||||
SplitOperatorType,
|
||||
rotateList
|
||||
} from './service';
|
||||
import { SplitOperatorType, rotateList } from './service';
|
||||
|
||||
describe('rotate function', () => {
|
||||
it('should rotate right side if right is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = 1;
|
||||
const right = true;
|
||||
it('should rotate right side if right is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = 1;
|
||||
const right = true;
|
||||
|
||||
const result = rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right,
|
||||
const result = rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
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', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = 1;
|
||||
const right = false;
|
||||
|
||||
it('should rotate left side if right is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = 1;
|
||||
const right = false;
|
||||
const result = rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right,
|
||||
|
||||
const result = rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
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', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = 2;
|
||||
const right = false;
|
||||
|
||||
});
|
||||
const result = rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right,
|
||||
|
||||
it('should rotate left side with 2 step if right is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = 2;
|
||||
const right = false;
|
||||
step
|
||||
);
|
||||
|
||||
const result = rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right,
|
||||
expect(result).toBe('lemon orange mango apple pineaple');
|
||||
});
|
||||
|
||||
step);
|
||||
it('should raise an error if step is negative', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = -2;
|
||||
const right = false;
|
||||
|
||||
expect(result).toBe('lemon orange mango apple pineaple');
|
||||
expect(() => {
|
||||
rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right,
|
||||
step
|
||||
);
|
||||
}).toThrowError('Rotation step must be greater than zero.');
|
||||
});
|
||||
|
||||
});
|
||||
it('should raise an error if step is undefined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const right = false;
|
||||
|
||||
it('should raise an error if step is negative', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const step = -2;
|
||||
const right = false;
|
||||
|
||||
expect(() => {
|
||||
rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right,
|
||||
step);
|
||||
}).toThrowError('Rotation step must be greater than zero.');
|
||||
|
||||
});
|
||||
|
||||
it('should raise an error if step is undefined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const right = false;
|
||||
|
||||
expect(() => {
|
||||
rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right);
|
||||
}).toThrowError('Rotation step contains non-digits.');
|
||||
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
expect(() => {
|
||||
rotateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
right
|
||||
);
|
||||
}).toThrowError('Rotation step contains non-digits.');
|
||||
});
|
||||
});
|
||||
|
@@ -1,48 +1,49 @@
|
||||
import { isNumber } from 'utils/string';
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
function rotateArray(
|
||||
array: string[],
|
||||
step: number,
|
||||
right: boolean): string[] {
|
||||
const length = array.length;
|
||||
function rotateArray(array: string[], step: number, right: boolean): string[] {
|
||||
const length = array.length;
|
||||
|
||||
// Normalize the step to be within the bounds of the array length
|
||||
const normalizedPositions = ((step % length) + length) % length;
|
||||
// Normalize the step to be within the bounds of the array length
|
||||
const normalizedPositions = ((step % length) + length) % length;
|
||||
|
||||
if (right) {
|
||||
// Rotate right
|
||||
return array.slice(-normalizedPositions).concat(array.slice(0, -normalizedPositions));
|
||||
} else {
|
||||
// Rotate left
|
||||
return array.slice(normalizedPositions).concat(array.slice(0, normalizedPositions));
|
||||
}
|
||||
if (right) {
|
||||
// Rotate right
|
||||
return array
|
||||
.slice(-normalizedPositions)
|
||||
.concat(array.slice(0, -normalizedPositions));
|
||||
} else {
|
||||
// Rotate left
|
||||
return array
|
||||
.slice(normalizedPositions)
|
||||
.concat(array.slice(0, normalizedPositions));
|
||||
}
|
||||
}
|
||||
|
||||
export function rotateList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
right: boolean,
|
||||
step?: number,
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
right: boolean,
|
||||
step?: number
|
||||
): string {
|
||||
let array: string[];
|
||||
let rotatedArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
let array: string[];
|
||||
let rotatedArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
if (step !== undefined) {
|
||||
if (step <= 0) {
|
||||
throw new Error('Rotation step must be greater than zero.');
|
||||
}
|
||||
if (step !== undefined) {
|
||||
if (step <= 0) {
|
||||
throw new Error("Rotation step must be greater than zero.");
|
||||
}
|
||||
rotatedArray = rotateArray(array, step, right);
|
||||
return rotatedArray.join(joinSeparator);
|
||||
}
|
||||
throw new Error("Rotation step contains non-digits.")
|
||||
rotatedArray = rotateArray(array, step, right);
|
||||
return rotatedArray.join(joinSeparator);
|
||||
}
|
||||
throw new Error('Rotation step contains non-digits.');
|
||||
}
|
@@ -2,37 +2,37 @@ export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
// function that randomize the array
|
||||
function shuffleArray(array: string[]): string[] {
|
||||
let shuffledArray = array.slice(); // Create a copy of the array
|
||||
for (let i = shuffledArray.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
|
||||
}
|
||||
return shuffledArray;
|
||||
const shuffledArray = array.slice(); // Create a copy of the array
|
||||
for (let i = shuffledArray.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
|
||||
}
|
||||
return shuffledArray;
|
||||
}
|
||||
|
||||
export function shuffleList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
length?: number, // "?" is to handle the case the user let the input blank
|
||||
) : string {
|
||||
let array: string[];
|
||||
let shuffledArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
length?: number // "?" is to handle the case the user let the input blank
|
||||
): string {
|
||||
let array: string[];
|
||||
let shuffledArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
shuffledArray = shuffleArray(array);
|
||||
if (length !== undefined) {
|
||||
if (length <= 0) {
|
||||
throw new Error('Length value must be a positive number.');
|
||||
}
|
||||
shuffledArray = shuffleArray(array);
|
||||
if (length !== undefined) {
|
||||
if (length <= 0) {
|
||||
throw new Error("Length value must be a positive number.");
|
||||
}
|
||||
return shuffledArray.slice(0, length).join(joinSeparator);
|
||||
}
|
||||
return shuffledArray.join(joinSeparator);
|
||||
return shuffledArray.slice(0, length).join(joinSeparator);
|
||||
}
|
||||
return shuffledArray.join(joinSeparator);
|
||||
}
|
@@ -1,94 +1,89 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import {
|
||||
shuffleList,
|
||||
SplitOperatorType
|
||||
} from './service';
|
||||
import { shuffleList, SplitOperatorType } from './service';
|
||||
|
||||
describe('shuffle function', () => {
|
||||
it('should be a 4 length list if no length value defined ', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
|
||||
it('should be a 4 length list if no length value defined ', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator
|
||||
);
|
||||
expect(result.split(joinSeparator).length).toBe(4);
|
||||
});
|
||||
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator
|
||||
);
|
||||
expect(result.split(joinSeparator).length).toBe(4);
|
||||
});
|
||||
it('should be a 2 length list if length value is set to 2', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const length = 2;
|
||||
|
||||
it('should be a 2 length list if length value is set to 2', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const length = 2;
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result.split(joinSeparator).length).toBe(2);
|
||||
});
|
||||
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result.split(joinSeparator).length).toBe(2);
|
||||
});
|
||||
it('should be a 4 length list if length value is set to 99', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const length = 99;
|
||||
|
||||
it('should be a 4 length list if length value is set to 99', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const length = 99;
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result.split(joinSeparator).length).toBe(4);
|
||||
});
|
||||
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result.split(joinSeparator).length).toBe(4);
|
||||
});
|
||||
it('should include a random element if length value is undefined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
|
||||
it('should include a random element if length value is undefined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result.split(joinSeparator)).toContain('apple');
|
||||
});
|
||||
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result.split(joinSeparator)).toContain('apple');
|
||||
});
|
||||
it('should return empty string if input is empty', () => {
|
||||
const input: string = '';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
|
||||
it('should return empty string if input is empty', () => {
|
||||
const input: string = '';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
})
|
||||
const result = shuffleList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
length
|
||||
);
|
||||
console.log(result);
|
||||
expect(result).toBe('');
|
||||
});
|
||||
});
|
||||
|
@@ -1,30 +1,31 @@
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
export function truncateList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
end: boolean,
|
||||
length?: number,
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
end: boolean,
|
||||
length?: number
|
||||
): string {
|
||||
let array: string[];
|
||||
let truncatedArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
let array: string[];
|
||||
let truncatedArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
if (length !== undefined) {
|
||||
if (length < 0) {
|
||||
throw new Error('Length value must be a positive number.');
|
||||
}
|
||||
if (length !== undefined) {
|
||||
if (length < 0) {
|
||||
throw new Error("Length value must be a positive number.")
|
||||
}
|
||||
truncatedArray = end ? array.slice(0, length) : array.slice(array.length - length, array.length);
|
||||
return truncatedArray.join(joinSeparator);
|
||||
}
|
||||
throw new Error("Length value isn't a value number.");
|
||||
|
||||
truncatedArray = end
|
||||
? array.slice(0, length)
|
||||
: array.slice(array.length - length, array.length);
|
||||
return truncatedArray.join(joinSeparator);
|
||||
}
|
||||
throw new Error("Length value isn't a value number.");
|
||||
}
|
@@ -1,185 +1,183 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
|
||||
import {
|
||||
SplitOperatorType,
|
||||
truncateList,
|
||||
} from './service';
|
||||
import { SplitOperatorType, truncateList } from './service';
|
||||
|
||||
describe('truncate function', () => {
|
||||
it('should remove at the end (one element) if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
it('should remove at the end (one element) if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
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', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
|
||||
it('should return 3 elements from the start if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length
|
||||
);
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
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', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
|
||||
});
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length
|
||||
);
|
||||
|
||||
it('should return 3 elements from the start if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 3;
|
||||
expect(result).toBe('apple pineaple lemon');
|
||||
});
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
it('should return 3 elements from the end if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 3;
|
||||
|
||||
expect(result).toBe('apple pineaple lemon');
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length
|
||||
);
|
||||
|
||||
});
|
||||
expect(result).toBe('lemon orange mango');
|
||||
});
|
||||
|
||||
it('should return 3 elements from the end if end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 3;
|
||||
it('should return a void string if length is set to 0', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 0;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length
|
||||
);
|
||||
|
||||
expect(result).toBe('lemon orange mango');
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
});
|
||||
it('should return an element (first) string if length is set to 1 and end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 1;
|
||||
|
||||
it('should return a void string if length is set to 0', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 0;
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length
|
||||
);
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
expect(result).toBe('apple');
|
||||
});
|
||||
|
||||
expect(result).toBe('');
|
||||
it('should return an element (last) string if length is set to 1 and end is set to false', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 1;
|
||||
|
||||
});
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length
|
||||
);
|
||||
|
||||
it('should return an element (first) string if length is set to 1 and end is set to true', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = true;
|
||||
const length = 1;
|
||||
expect(result).toBe('mango');
|
||||
});
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
it('should throw an error if the length value is negative', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = -5;
|
||||
|
||||
expect(result).toBe('apple');
|
||||
expect(() => {
|
||||
truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length
|
||||
);
|
||||
}).toThrow('Length value must be a positive number.');
|
||||
});
|
||||
|
||||
});
|
||||
it('should throw an error if the length value is left blank', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
|
||||
it('should return an element (last) string if length is set to 1 and end is set to false', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = 1;
|
||||
|
||||
const result = truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length);
|
||||
|
||||
expect(result).toBe('mango');
|
||||
|
||||
});
|
||||
|
||||
it('should throw an error if the length value is negative', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
const length = -5;
|
||||
|
||||
expect(() => {
|
||||
truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end,
|
||||
length)
|
||||
}).toThrow("Length value must be a positive number.");
|
||||
});
|
||||
|
||||
it('should throw an error if the length value is left blank', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ' ';
|
||||
const end = false;
|
||||
|
||||
expect(() => {
|
||||
truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end)
|
||||
}).toThrow("Length value isn't a value number.");
|
||||
});
|
||||
|
||||
})
|
||||
expect(() => {
|
||||
truncateList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
end
|
||||
);
|
||||
}).toThrow("Length value isn't a value number.");
|
||||
});
|
||||
});
|
||||
|
@@ -1,69 +1,69 @@
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
function leftUnwrap(
|
||||
row: string,
|
||||
left: string = '',
|
||||
multiLevel: boolean
|
||||
row: string,
|
||||
left: string = '',
|
||||
multiLevel: boolean
|
||||
): string {
|
||||
if (left === '') return row; // Prevent infinite loop if left is an empty string
|
||||
while (row.startsWith(left)) {
|
||||
row = row.slice(left.length);
|
||||
if (!multiLevel) {
|
||||
break;
|
||||
}
|
||||
if (left === '') return row; // Prevent infinite loop if left is an empty string
|
||||
while (row.startsWith(left)) {
|
||||
row = row.slice(left.length);
|
||||
if (!multiLevel) {
|
||||
break;
|
||||
}
|
||||
return row;
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
function rightUnwrap(
|
||||
row: string,
|
||||
right: string = '',
|
||||
multiLevel: boolean
|
||||
row: string,
|
||||
right: string = '',
|
||||
multiLevel: boolean
|
||||
): string {
|
||||
if (right === '') return row; // Prevent infinite loop if right is an empty string
|
||||
while (row.endsWith(right)) {
|
||||
row = row.slice(0, row.length - right.length);
|
||||
if (!multiLevel) {
|
||||
break;
|
||||
}
|
||||
if (right === '') return row; // Prevent infinite loop if right is an empty string
|
||||
while (row.endsWith(right)) {
|
||||
row = row.slice(0, row.length - right.length);
|
||||
if (!multiLevel) {
|
||||
break;
|
||||
}
|
||||
return row;
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
export function unwrapList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
deleteEmptyItems: boolean,
|
||||
multiLevel: boolean,
|
||||
trimItems: boolean,
|
||||
left: string = '',
|
||||
right: string = ''
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
deleteEmptyItems: boolean,
|
||||
multiLevel: boolean,
|
||||
trimItems: boolean,
|
||||
left: string = '',
|
||||
right: string = ''
|
||||
): string {
|
||||
let array: string[];
|
||||
let unwrappedArray: string[] = [];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter(Boolean);
|
||||
}
|
||||
let array: string[];
|
||||
let unwrappedArray: string[] = [];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter(Boolean);
|
||||
}
|
||||
|
||||
// for each element of array unwrap left side then right side and push the result to a final array
|
||||
for (let row of array) {
|
||||
row = leftUnwrap(row, left, multiLevel);
|
||||
row = rightUnwrap(row, right, multiLevel);
|
||||
unwrappedArray.push(row);
|
||||
}
|
||||
// trim items if needed
|
||||
if (trimItems) {
|
||||
unwrappedArray = unwrappedArray.map(item => item.trim());
|
||||
}
|
||||
return unwrappedArray.join(joinSeparator);
|
||||
// for each element of array unwrap left side then right side and push the result to a final array
|
||||
for (let row of array) {
|
||||
row = leftUnwrap(row, left, multiLevel);
|
||||
row = rightUnwrap(row, right, multiLevel);
|
||||
unwrappedArray.push(row);
|
||||
}
|
||||
// trim items if needed
|
||||
if (trimItems) {
|
||||
unwrappedArray = unwrappedArray.map((item) => item.trim());
|
||||
}
|
||||
return unwrappedArray.join(joinSeparator);
|
||||
}
|
||||
|
@@ -2,69 +2,169 @@ import { expect, describe, it } from 'vitest';
|
||||
import { unwrapList } from './service';
|
||||
|
||||
describe('unwrapList function', () => {
|
||||
it('should unwrap elements correctly with symbol split', () => {
|
||||
const input = "##Hello##\n##World##";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
it('should unwrap elements correctly with symbol split', () => {
|
||||
const input = '##Hello##\n##World##';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
' ',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should unwrap elements correctly with regex split', () => {
|
||||
const input = "##Hello##||##World##";
|
||||
const result = unwrapList('regex', input, '\\|\\|', ' ', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
it('should unwrap elements correctly with regex split', () => {
|
||||
const input = '##Hello##||##World##';
|
||||
const result = unwrapList(
|
||||
'regex',
|
||||
input,
|
||||
'\\|\\|',
|
||||
' ',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should handle multiple levels of unwrapping', () => {
|
||||
const input = "###Hello###";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello");
|
||||
});
|
||||
it('should handle multiple levels of unwrapping', () => {
|
||||
const input = '###Hello###';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
' ',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello');
|
||||
});
|
||||
|
||||
it('should handle single level of unwrapping', () => {
|
||||
const input = "###Hello###";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, false, true, '#', '#');
|
||||
expect(result).toBe("##Hello##");
|
||||
});
|
||||
it('should handle single level of unwrapping', () => {
|
||||
const input = '###Hello###';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
' ',
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('##Hello##');
|
||||
});
|
||||
|
||||
it('should delete empty items', () => {
|
||||
const input = "##Hello##\n\n##World##";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
it('should delete empty items', () => {
|
||||
const input = '##Hello##\n\n##World##';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
' ',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should keep empty items if deleteEmptyItems is false', () => {
|
||||
const input = "##Hello##\n\n##World##";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', false, true, true, '#', '#');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
it('should keep empty items if deleteEmptyItems is false', () => {
|
||||
const input = '##Hello##\n\n##World##';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
' ',
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should trim items', () => {
|
||||
const input = "## Hello ##\n## World ##";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
it('should trim items', () => {
|
||||
const input = '## Hello ##\n## World ##';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
' ',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should handle no left or right unwrapping', () => {
|
||||
const input = "Hello\nWorld";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, true, true);
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
it('should handle no left or right unwrapping', () => {
|
||||
const input = 'Hello\nWorld';
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, true, true);
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should handle mixed levels of unwrapping', () => {
|
||||
const input = "###Hello##\n#World###";
|
||||
const result = unwrapList('symbol', input, '\n', ' ', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
it('should handle mixed levels of unwrapping', () => {
|
||||
const input = '###Hello##\n#World###';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
' ',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should handle complex regex split', () => {
|
||||
const input = "##Hello##||###World###||####Test####";
|
||||
const result = unwrapList('regex', input, '\\|\\|', ' ', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello World Test");
|
||||
});
|
||||
it('should handle complex regex split', () => {
|
||||
const input = '##Hello##||###World###||####Test####';
|
||||
const result = unwrapList(
|
||||
'regex',
|
||||
input,
|
||||
'\\|\\|',
|
||||
' ',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello World Test');
|
||||
});
|
||||
|
||||
it('should handle different joinSeparator', () => {
|
||||
const input = "##Hello##\n##World##";
|
||||
const result = unwrapList('symbol', input, '\n', '-', true, true, true, '#', '#');
|
||||
expect(result).toBe("Hello-World");
|
||||
});
|
||||
it('should handle different joinSeparator', () => {
|
||||
const input = '##Hello##\n##World##';
|
||||
const result = unwrapList(
|
||||
'symbol',
|
||||
input,
|
||||
'\n',
|
||||
'-',
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
'#',
|
||||
'#'
|
||||
);
|
||||
expect(result).toBe('Hello-World');
|
||||
});
|
||||
});
|
@@ -1,32 +1,31 @@
|
||||
export type SplitOperatorType = 'symbol' | 'regex';
|
||||
|
||||
function wrap(array: string[], left: string, right: string): string[] {
|
||||
return array.map((element) => left + element + right);
|
||||
return array.map((element) => left + element + right);
|
||||
}
|
||||
|
||||
export function wrapList(
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
deleteEmptyItems: boolean,
|
||||
left: string = '',
|
||||
right: string = '',
|
||||
|
||||
splitOperatorType: SplitOperatorType,
|
||||
input: string,
|
||||
splitSeparator: string,
|
||||
joinSeparator: string,
|
||||
deleteEmptyItems: boolean,
|
||||
left: string = '',
|
||||
right: string = ''
|
||||
): string {
|
||||
let array: string[];
|
||||
let wrappedArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter(Boolean);
|
||||
}
|
||||
wrappedArray = wrap(array, left, right);
|
||||
return wrappedArray.join(joinSeparator);
|
||||
let array: string[];
|
||||
let wrappedArray: string[];
|
||||
switch (splitOperatorType) {
|
||||
case 'symbol':
|
||||
array = input.split(splitSeparator);
|
||||
break;
|
||||
case 'regex':
|
||||
array = input.split(new RegExp(splitSeparator));
|
||||
break;
|
||||
}
|
||||
if (deleteEmptyItems) {
|
||||
array = array.filter(Boolean);
|
||||
}
|
||||
wrappedArray = wrap(array, left, right);
|
||||
return wrappedArray.join(joinSeparator);
|
||||
}
|
||||
|
@@ -1,137 +1,132 @@
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import {
|
||||
SplitOperatorType,
|
||||
wrapList
|
||||
} from './service';
|
||||
import { SplitOperatorType, wrapList } from './service';
|
||||
|
||||
describe('wrap function', () => {
|
||||
it('should return the same input if no left and right are blanked', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = false;
|
||||
it('should return the same input if no left and right are blanked', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = false;
|
||||
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems
|
||||
);
|
||||
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems
|
||||
);
|
||||
expect(result).toBe('apple, pineaple, lemon, orange, mango');
|
||||
});
|
||||
|
||||
expect(result).toBe('apple, pineaple, lemon, orange, mango');
|
||||
it('should append to left if defined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const left = 'the ';
|
||||
const deleteEmptyItems = false;
|
||||
|
||||
});
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left
|
||||
);
|
||||
|
||||
it('should append to left if defined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const left = 'the ';
|
||||
const deleteEmptyItems = false;
|
||||
expect(result).toBe(
|
||||
'the apple, the pineaple, the lemon, the orange, the mango'
|
||||
);
|
||||
});
|
||||
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left);
|
||||
it('should append to right if defined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const left = '';
|
||||
const right = 'z';
|
||||
const deleteEmptyItems = false;
|
||||
|
||||
expect(result).toBe('the apple, the pineaple, the lemon, the orange, the mango');
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right
|
||||
);
|
||||
|
||||
});
|
||||
expect(result).toBe('applez, pineaplez, lemonz, orangez, mangoz');
|
||||
});
|
||||
|
||||
it('should append to right if defined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const left = '';
|
||||
const right = 'z';
|
||||
const deleteEmptyItems = false;
|
||||
it('should append to both side if both defined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = false;
|
||||
const left = 'K';
|
||||
const right = 'z';
|
||||
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right);
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right
|
||||
);
|
||||
|
||||
expect(result).toBe('applez, pineaplez, lemonz, orangez, mangoz');
|
||||
});
|
||||
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
|
||||
});
|
||||
|
||||
it('should append to both side if both defined', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = false;
|
||||
const left = 'K';
|
||||
const right = 'z';
|
||||
it('should append to both side if both defined and not delete empty items', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango, ';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = false;
|
||||
const left = 'K';
|
||||
const right = 'z';
|
||||
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right);
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right
|
||||
);
|
||||
|
||||
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
|
||||
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz, Kz');
|
||||
});
|
||||
|
||||
});
|
||||
it('should append to both side if both defined and delete empty items', () => {
|
||||
const input: string = 'apple, pineaple, lemon, , orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = true;
|
||||
const left = 'K';
|
||||
const right = 'z';
|
||||
|
||||
it('should append to both side if both defined and not delete empty items', () => {
|
||||
const input: string = 'apple, pineaple, lemon, orange, mango, ';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = false;
|
||||
const left = 'K';
|
||||
const right = 'z';
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right
|
||||
);
|
||||
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right);
|
||||
|
||||
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz, Kz');
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('should append to both side if both defined and delete empty items', () => {
|
||||
const input: string = 'apple, pineaple, lemon, , orange, mango';
|
||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||
const splitSeparator = ', ';
|
||||
const joinSeparator = ', ';
|
||||
const deleteEmptyItems = true;
|
||||
const left = 'K';
|
||||
const right = 'z';
|
||||
|
||||
const result = wrapList(
|
||||
splitOperatorType,
|
||||
input,
|
||||
splitSeparator,
|
||||
joinSeparator,
|
||||
deleteEmptyItems,
|
||||
left,
|
||||
right);
|
||||
|
||||
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
|
||||
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user