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" />
|
<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 />
|
||||||
|
@@ -3,5 +3,5 @@ a {
|
|||||||
}
|
}
|
||||||
|
|
||||||
a:hover {
|
a:hover {
|
||||||
color: #030362
|
color: #030362;
|
||||||
}
|
}
|
||||||
|
@@ -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) {
|
||||||
|
@@ -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');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -2,57 +2,63 @@ export type SplitOperatorType = 'symbol' | 'regex';
|
|||||||
|
|
||||||
// Function that builds the unique items array handling caseSensitive and absolutelyUnique options
|
// Function that builds the unique items array handling caseSensitive and absolutelyUnique options
|
||||||
function uniqueListBuilder(
|
function uniqueListBuilder(
|
||||||
array: string[],
|
array: string[],
|
||||||
caseSensitive: boolean,
|
caseSensitive: boolean,
|
||||||
absolutelyUnique: boolean
|
absolutelyUnique: boolean
|
||||||
): string[] {
|
): string[] {
|
||||||
const dict: { [key: string]: number } = {};
|
const dict: { [key: string]: number } = {};
|
||||||
for (const item of array) {
|
for (const item of array) {
|
||||||
const key = caseSensitive ? item : item.toLowerCase();
|
const key = caseSensitive ? item : item.toLowerCase();
|
||||||
dict[key] = (dict[key] || 0) + 1;
|
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)) {
|
return Object.keys(dict);
|
||||||
if (value > 1) {
|
|
||||||
delete dict[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Object.keys(dict);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TopItemsList(
|
export function TopItemsList(
|
||||||
splitOperatorType: SplitOperatorType,
|
splitOperatorType: SplitOperatorType,
|
||||||
splitSeparator: string,
|
splitSeparator: string,
|
||||||
joinSeparator: string = '\n',
|
joinSeparator: string = '\n',
|
||||||
input: string,
|
input: string,
|
||||||
deleteEmptyItems: boolean,
|
deleteEmptyItems: boolean,
|
||||||
trimItems: boolean,
|
trimItems: boolean,
|
||||||
caseSensitive: boolean,
|
caseSensitive: boolean,
|
||||||
absolutelyUnique: boolean
|
absolutelyUnique: boolean
|
||||||
): string {
|
): string {
|
||||||
let array: string[];
|
let array: string[];
|
||||||
switch (splitOperatorType) {
|
switch (splitOperatorType) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
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
|
||||||
break;
|
.split(new RegExp(splitSeparator))
|
||||||
}
|
.filter((item) => item !== '');
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
@@ -3,97 +3,97 @@ import { expect, describe, it } from 'vitest';
|
|||||||
import { groupList, SplitOperatorType } from './service';
|
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;
|
||||||
const itemSeparator = '-';
|
const itemSeparator = '-';
|
||||||
const leftWrap = '[';
|
const leftWrap = '[';
|
||||||
const rightWrap = ']';
|
const rightWrap = ']';
|
||||||
const groupSeparator = ' | ';
|
const groupSeparator = ' | ';
|
||||||
const deleteEmptyItems = false;
|
const deleteEmptyItems = false;
|
||||||
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,
|
||||||
splitSeparator,
|
splitSeparator,
|
||||||
input,
|
input,
|
||||||
groupNumber,
|
groupNumber,
|
||||||
itemSeparator,
|
itemSeparator,
|
||||||
leftWrap,
|
leftWrap,
|
||||||
rightWrap,
|
rightWrap,
|
||||||
groupSeparator,
|
groupSeparator,
|
||||||
deleteEmptyItems,
|
deleteEmptyItems,
|
||||||
padNonFullGroup,
|
padNonFullGroup,
|
||||||
paddingChar
|
paddingChar
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toBe(expectedOutput);
|
expect(result).toBe(expectedOutput);
|
||||||
});
|
});
|
||||||
|
|
||||||
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;
|
||||||
const itemSeparator = ',';
|
const itemSeparator = ',';
|
||||||
const leftWrap = '(';
|
const leftWrap = '(';
|
||||||
const rightWrap = ')';
|
const rightWrap = ')';
|
||||||
const groupSeparator = ' / ';
|
const groupSeparator = ' / ';
|
||||||
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,
|
||||||
splitSeparator,
|
splitSeparator,
|
||||||
input,
|
input,
|
||||||
groupNumber,
|
groupNumber,
|
||||||
itemSeparator,
|
itemSeparator,
|
||||||
leftWrap,
|
leftWrap,
|
||||||
rightWrap,
|
rightWrap,
|
||||||
groupSeparator,
|
groupSeparator,
|
||||||
deleteEmptyItems,
|
deleteEmptyItems,
|
||||||
padNonFullGroup
|
padNonFullGroup
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toBe(expectedOutput);
|
expect(result).toBe(expectedOutput);
|
||||||
});
|
});
|
||||||
|
|
||||||
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;
|
||||||
const itemSeparator = ':';
|
const itemSeparator = ':';
|
||||||
const leftWrap = '<';
|
const leftWrap = '<';
|
||||||
const rightWrap = '>';
|
const rightWrap = '>';
|
||||||
const groupSeparator = ' & ';
|
const groupSeparator = ' & ';
|
||||||
const deleteEmptyItems = true;
|
const deleteEmptyItems = true;
|
||||||
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,
|
||||||
splitSeparator,
|
splitSeparator,
|
||||||
input,
|
input,
|
||||||
groupNumber,
|
groupNumber,
|
||||||
itemSeparator,
|
itemSeparator,
|
||||||
leftWrap,
|
leftWrap,
|
||||||
rightWrap,
|
rightWrap,
|
||||||
groupSeparator,
|
groupSeparator,
|
||||||
deleteEmptyItems,
|
deleteEmptyItems,
|
||||||
padNonFullGroup,
|
padNonFullGroup,
|
||||||
paddingChar
|
paddingChar
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toBe(expectedOutput);
|
expect(result).toBe(expectedOutput);
|
||||||
});
|
});
|
||||||
});
|
});
|
@@ -1,92 +1,94 @@
|
|||||||
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[],
|
const result: string[][] = [];
|
||||||
groupNumber: number,
|
for (let i = 0; i < array.length; i += groupNumber) {
|
||||||
): string[][] {
|
result.push(array.slice(i, i + groupNumber));
|
||||||
const result: string[][] = [];
|
}
|
||||||
for (let i = 0; i < array.length; i += groupNumber) {
|
return result;
|
||||||
result.push(array.slice(i, i + groupNumber));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// function use to handle the case paddingNonFullGroup is enable
|
// function use to handle the case paddingNonFullGroup is enable
|
||||||
function groupFiller(
|
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];
|
||||||
if (lastSubArray.length < groupNumber) {
|
if (lastSubArray.length < groupNumber) {
|
||||||
for (let i = lastSubArray.length; i < groupNumber; i++) {
|
for (let i = lastSubArray.length; i < groupNumber; i++) {
|
||||||
lastSubArray.push(paddingChar);
|
lastSubArray.push(paddingChar);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
array[array.length - 1] = lastSubArray;
|
|
||||||
}
|
}
|
||||||
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 that join with the item separator and wrap with left and right each subArray of the Array
|
||||||
function groupJoinerAndWrapper(
|
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,
|
||||||
input: string,
|
input: string,
|
||||||
groupNumber: number,
|
groupNumber: number,
|
||||||
itemSeparator: string = '',
|
itemSeparator: string = '',
|
||||||
leftWrap: string = '',
|
leftWrap: string = '',
|
||||||
rightWrap: string = '',
|
rightWrap: string = '',
|
||||||
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[][];
|
||||||
let fullSplitedArray: string[][];
|
let fullSplitedArray: string[][];
|
||||||
let result: string[];
|
let result: string[];
|
||||||
switch (splitOperatorType) {
|
switch (splitOperatorType) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
array = input.split(splitSeparator);
|
array = input.split(splitSeparator);
|
||||||
break;
|
break;
|
||||||
case 'regex':
|
case 'regex':
|
||||||
array = input.split(new RegExp(splitSeparator));
|
array = input.split(new RegExp(splitSeparator));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,27 +2,27 @@ import { expect, describe, it } from 'vitest';
|
|||||||
import { reverseList } from './service';
|
import { reverseList } from './service';
|
||||||
|
|
||||||
describe('reverseList Function', () => {
|
describe('reverseList Function', () => {
|
||||||
test('should reverse items split by symbol', () => {
|
test('should reverse items split by symbol', () => {
|
||||||
const input = 'apple,banana,orange';
|
const input = 'apple,banana,orange';
|
||||||
const result = reverseList('symbol', ',', '\n', input);
|
const result = reverseList('symbol', ',', '\n', input);
|
||||||
expect(result).toBe('orange\nbanana\napple');
|
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 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';
|
type SplitOperatorType = 'symbol' | 'regex';
|
||||||
|
|
||||||
export function reverseList(
|
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) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
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
|
||||||
break;
|
.split(new RegExp(splitSeparator))
|
||||||
}
|
.filter((item) => item !== '');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
const reversedList = array.reverse();
|
const reversedList = array.reverse();
|
||||||
return reversedList.join(joinSeparator);
|
return reversedList.join(joinSeparator);
|
||||||
}
|
}
|
||||||
|
@@ -1,110 +1,105 @@
|
|||||||
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', () => {
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
const splitSeparator = ', ';
|
const splitSeparator = ', ';
|
||||||
const joinSeparator = ' ';
|
const joinSeparator = ' ';
|
||||||
const step = 1;
|
const step = 1;
|
||||||
const right = true;
|
const right = true;
|
||||||
|
|
||||||
const result = rotateList(
|
const result = rotateList(
|
||||||
splitOperatorType,
|
splitOperatorType,
|
||||||
input,
|
input,
|
||||||
splitSeparator,
|
splitSeparator,
|
||||||
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', () => {
|
||||||
|
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 result = rotateList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator,
|
||||||
const step = 1;
|
right,
|
||||||
const right = false;
|
|
||||||
|
|
||||||
const result = rotateList(
|
step
|
||||||
splitOperatorType,
|
);
|
||||||
input,
|
|
||||||
splitSeparator,
|
|
||||||
joinSeparator,
|
|
||||||
right,
|
|
||||||
|
|
||||||
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', () => {
|
step
|
||||||
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(
|
expect(result).toBe('lemon orange mango apple pineaple');
|
||||||
splitOperatorType,
|
});
|
||||||
input,
|
|
||||||
splitSeparator,
|
|
||||||
joinSeparator,
|
|
||||||
right,
|
|
||||||
|
|
||||||
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', () => {
|
expect(() => {
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
rotateList(
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
splitOperatorType,
|
||||||
const splitSeparator = ', ';
|
input,
|
||||||
const joinSeparator = ' ';
|
splitSeparator,
|
||||||
const step = -2;
|
joinSeparator,
|
||||||
const right = false;
|
right
|
||||||
|
);
|
||||||
expect(() => {
|
}).toThrowError('Rotation step contains non-digits.');
|
||||||
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.');
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
})
|
|
||||||
|
@@ -1,48 +1,49 @@
|
|||||||
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[],
|
const length = array.length;
|
||||||
step: number,
|
|
||||||
right: boolean): string[] {
|
|
||||||
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
|
||||||
const normalizedPositions = ((step % length) + length) % length;
|
const normalizedPositions = ((step % length) + length) % length;
|
||||||
|
|
||||||
if (right) {
|
if (right) {
|
||||||
// Rotate right
|
// Rotate right
|
||||||
return array.slice(-normalizedPositions).concat(array.slice(0, -normalizedPositions));
|
return array
|
||||||
} else {
|
.slice(-normalizedPositions)
|
||||||
// Rotate left
|
.concat(array.slice(0, -normalizedPositions));
|
||||||
return array.slice(normalizedPositions).concat(array.slice(0, normalizedPositions));
|
} else {
|
||||||
}
|
// Rotate left
|
||||||
|
return array
|
||||||
|
.slice(normalizedPositions)
|
||||||
|
.concat(array.slice(0, normalizedPositions));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function rotateList(
|
export function rotateList(
|
||||||
splitOperatorType: SplitOperatorType,
|
splitOperatorType: SplitOperatorType,
|
||||||
input: string,
|
input: string,
|
||||||
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[];
|
||||||
switch (splitOperatorType) {
|
switch (splitOperatorType) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
array = input.split(splitSeparator);
|
array = input.split(splitSeparator);
|
||||||
break;
|
break;
|
||||||
case 'regex':
|
case 'regex':
|
||||||
array = input.split(new RegExp(splitSeparator));
|
array = input.split(new RegExp(splitSeparator));
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
if (step !== undefined) {
|
||||||
|
if (step <= 0) {
|
||||||
|
throw new Error('Rotation step must be greater than zero.');
|
||||||
}
|
}
|
||||||
if (step !== undefined) {
|
rotatedArray = rotateArray(array, step, right);
|
||||||
if (step <= 0) {
|
return rotatedArray.join(joinSeparator);
|
||||||
throw new Error("Rotation step must be greater than zero.");
|
}
|
||||||
}
|
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 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]];
|
||||||
}
|
}
|
||||||
return shuffledArray;
|
return shuffledArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function shuffleList(
|
export function shuffleList(
|
||||||
splitOperatorType: SplitOperatorType,
|
splitOperatorType: SplitOperatorType,
|
||||||
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[];
|
||||||
switch (splitOperatorType) {
|
switch (splitOperatorType) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
array = input.split(splitSeparator);
|
array = input.split(splitSeparator);
|
||||||
break;
|
break;
|
||||||
case 'regex':
|
case 'regex':
|
||||||
array = input.split(new RegExp(splitSeparator));
|
array = input.split(new RegExp(splitSeparator));
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
shuffledArray = shuffleArray(array);
|
||||||
|
if (length !== undefined) {
|
||||||
|
if (length <= 0) {
|
||||||
|
throw new Error('Length value must be a positive number.');
|
||||||
}
|
}
|
||||||
shuffledArray = shuffleArray(array);
|
return shuffledArray.slice(0, length).join(joinSeparator);
|
||||||
if (length !== undefined) {
|
}
|
||||||
if (length <= 0) {
|
return shuffledArray.join(joinSeparator);
|
||||||
throw new Error("Length value must be a positive number.");
|
|
||||||
}
|
|
||||||
return shuffledArray.slice(0, length).join(joinSeparator);
|
|
||||||
}
|
|
||||||
return shuffledArray.join(joinSeparator);
|
|
||||||
}
|
}
|
@@ -1,94 +1,89 @@
|
|||||||
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 ', () => {
|
||||||
|
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 result = shuffleList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator
|
||||||
|
);
|
||||||
|
expect(result.split(joinSeparator).length).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
const result = shuffleList(
|
it('should be a 2 length list if length value is set to 2', () => {
|
||||||
splitOperatorType,
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
input,
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
splitSeparator,
|
const splitSeparator = ', ';
|
||||||
joinSeparator
|
const joinSeparator = ' ';
|
||||||
);
|
const length = 2;
|
||||||
expect(result.split(joinSeparator).length).toBe(4);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be a 2 length list if length value is set to 2', () => {
|
const result = shuffleList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator,
|
||||||
const length = 2;
|
length
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result.split(joinSeparator).length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
const result = shuffleList(
|
it('should be a 4 length list if length value is set to 99', () => {
|
||||||
splitOperatorType,
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
input,
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
splitSeparator,
|
const splitSeparator = ', ';
|
||||||
joinSeparator,
|
const joinSeparator = ' ';
|
||||||
length
|
const length = 99;
|
||||||
);
|
|
||||||
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 result = shuffleList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator,
|
||||||
const length = 99;
|
length
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result.split(joinSeparator).length).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
const result = shuffleList(
|
it('should include a random element if length value is undefined', () => {
|
||||||
splitOperatorType,
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
input,
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
splitSeparator,
|
const splitSeparator = ', ';
|
||||||
joinSeparator,
|
const joinSeparator = ' ';
|
||||||
length
|
|
||||||
);
|
|
||||||
console.log(result);
|
|
||||||
expect(result.split(joinSeparator).length).toBe(4);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should include a random element if length value is undefined', () => {
|
const result = shuffleList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator,
|
||||||
|
length
|
||||||
|
);
|
||||||
|
console.log(result);
|
||||||
|
expect(result.split(joinSeparator)).toContain('apple');
|
||||||
|
});
|
||||||
|
|
||||||
const result = shuffleList(
|
it('should return empty string if input is empty', () => {
|
||||||
splitOperatorType,
|
const input: string = '';
|
||||||
input,
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
splitSeparator,
|
const splitSeparator = ', ';
|
||||||
joinSeparator,
|
const joinSeparator = ' ';
|
||||||
length
|
|
||||||
);
|
|
||||||
console.log(result);
|
|
||||||
expect(result.split(joinSeparator)).toContain('apple');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return empty string if input is empty', () => {
|
const result = shuffleList(
|
||||||
const input: string = '';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator,
|
||||||
|
length
|
||||||
const result = shuffleList(
|
);
|
||||||
splitOperatorType,
|
console.log(result);
|
||||||
input,
|
expect(result).toBe('');
|
||||||
splitSeparator,
|
});
|
||||||
joinSeparator,
|
});
|
||||||
length
|
|
||||||
);
|
|
||||||
console.log(result);
|
|
||||||
expect(result).toBe('');
|
|
||||||
});
|
|
||||||
|
|
||||||
})
|
|
||||||
|
@@ -1,30 +1,31 @@
|
|||||||
export type SplitOperatorType = 'symbol' | 'regex';
|
export type SplitOperatorType = 'symbol' | 'regex';
|
||||||
|
|
||||||
export function truncateList(
|
export function truncateList(
|
||||||
splitOperatorType: SplitOperatorType,
|
splitOperatorType: SplitOperatorType,
|
||||||
input: string,
|
input: string,
|
||||||
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[];
|
||||||
switch (splitOperatorType) {
|
switch (splitOperatorType) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
array = input.split(splitSeparator);
|
array = input.split(splitSeparator);
|
||||||
break;
|
break;
|
||||||
case 'regex':
|
case 'regex':
|
||||||
array = input.split(new RegExp(splitSeparator));
|
array = input.split(new RegExp(splitSeparator));
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
if (length !== undefined) {
|
||||||
|
if (length < 0) {
|
||||||
|
throw new Error('Length value must be a positive number.');
|
||||||
}
|
}
|
||||||
if (length !== undefined) {
|
truncatedArray = end
|
||||||
if (length < 0) {
|
? array.slice(0, length)
|
||||||
throw new Error("Length value must be a positive number.")
|
: array.slice(array.length - length, array.length);
|
||||||
}
|
return truncatedArray.join(joinSeparator);
|
||||||
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.");
|
||||||
}
|
|
||||||
throw new Error("Length value isn't a value number.");
|
|
||||||
|
|
||||||
}
|
}
|
@@ -1,185 +1,183 @@
|
|||||||
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', () => {
|
||||||
const input: string = 'apple, pineaple, lemon, orange';
|
const input: string = 'apple, pineaple, lemon, orange';
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
const splitSeparator = ', ';
|
const splitSeparator = ', ';
|
||||||
const joinSeparator = ' ';
|
const joinSeparator = ' ';
|
||||||
const end = true;
|
const end = true;
|
||||||
const length = 3;
|
const length = 3;
|
||||||
|
|
||||||
const result = truncateList(
|
const result = truncateList(
|
||||||
splitOperatorType,
|
splitOperatorType,
|
||||||
input,
|
input,
|
||||||
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', () => {
|
||||||
|
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 result = truncateList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator,
|
||||||
const end = true;
|
end,
|
||||||
const length = 3;
|
length
|
||||||
|
);
|
||||||
|
|
||||||
const result = truncateList(
|
expect(result).toBe('apple pineaple lemon');
|
||||||
splitOperatorType,
|
});
|
||||||
input,
|
|
||||||
splitSeparator,
|
|
||||||
joinSeparator,
|
|
||||||
end,
|
|
||||||
length);
|
|
||||||
|
|
||||||
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', () => {
|
expect(result).toBe('apple pineaple lemon');
|
||||||
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(
|
it('should return 3 elements from the end if end is set to true', () => {
|
||||||
splitOperatorType,
|
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||||
input,
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
splitSeparator,
|
const splitSeparator = ', ';
|
||||||
joinSeparator,
|
const joinSeparator = ' ';
|
||||||
end,
|
const end = false;
|
||||||
length);
|
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', () => {
|
it('should return a void string if length is set to 0', () => {
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
const splitSeparator = ', ';
|
const splitSeparator = ', ';
|
||||||
const joinSeparator = ' ';
|
const joinSeparator = ' ';
|
||||||
const end = false;
|
const end = false;
|
||||||
const length = 3;
|
const length = 0;
|
||||||
|
|
||||||
const result = truncateList(
|
const result = truncateList(
|
||||||
splitOperatorType,
|
splitOperatorType,
|
||||||
input,
|
input,
|
||||||
splitSeparator,
|
splitSeparator,
|
||||||
joinSeparator,
|
joinSeparator,
|
||||||
end,
|
end,
|
||||||
length);
|
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 result = truncateList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ' ';
|
joinSeparator,
|
||||||
const end = false;
|
end,
|
||||||
const length = 0;
|
length
|
||||||
|
);
|
||||||
|
|
||||||
const result = truncateList(
|
expect(result).toBe('apple');
|
||||||
splitOperatorType,
|
});
|
||||||
input,
|
|
||||||
splitSeparator,
|
|
||||||
joinSeparator,
|
|
||||||
end,
|
|
||||||
length);
|
|
||||||
|
|
||||||
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', () => {
|
expect(result).toBe('mango');
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
});
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
|
||||||
const splitSeparator = ', ';
|
|
||||||
const joinSeparator = ' ';
|
|
||||||
const end = true;
|
|
||||||
const length = 1;
|
|
||||||
|
|
||||||
const result = truncateList(
|
it('should throw an error if the length value is negative', () => {
|
||||||
splitOperatorType,
|
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||||
input,
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
splitSeparator,
|
const splitSeparator = ', ';
|
||||||
joinSeparator,
|
const joinSeparator = ' ';
|
||||||
end,
|
const end = false;
|
||||||
length);
|
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', () => {
|
expect(() => {
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
truncateList(
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
splitOperatorType,
|
||||||
const splitSeparator = ', ';
|
input,
|
||||||
const joinSeparator = ' ';
|
splitSeparator,
|
||||||
const end = false;
|
joinSeparator,
|
||||||
const length = 1;
|
end
|
||||||
|
);
|
||||||
const result = truncateList(
|
}).toThrow("Length value isn't a value number.");
|
||||||
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.");
|
|
||||||
});
|
|
||||||
|
|
||||||
})
|
|
||||||
|
@@ -1,69 +1,69 @@
|
|||||||
export type SplitOperatorType = 'symbol' | 'regex';
|
export type SplitOperatorType = 'symbol' | 'regex';
|
||||||
|
|
||||||
function leftUnwrap(
|
function leftUnwrap(
|
||||||
row: string,
|
row: string,
|
||||||
left: string = '',
|
left: string = '',
|
||||||
multiLevel: boolean
|
multiLevel: boolean
|
||||||
): string {
|
): string {
|
||||||
if (left === '') return row; // Prevent infinite loop if left is an empty string
|
if (left === '') return row; // Prevent infinite loop if left is an empty string
|
||||||
while (row.startsWith(left)) {
|
while (row.startsWith(left)) {
|
||||||
row = row.slice(left.length);
|
row = row.slice(left.length);
|
||||||
if (!multiLevel) {
|
if (!multiLevel) {
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return row;
|
}
|
||||||
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
function rightUnwrap(
|
function rightUnwrap(
|
||||||
row: string,
|
row: string,
|
||||||
right: string = '',
|
right: string = '',
|
||||||
multiLevel: boolean
|
multiLevel: boolean
|
||||||
): string {
|
): string {
|
||||||
if (right === '') return row; // Prevent infinite loop if right is an empty string
|
if (right === '') return row; // Prevent infinite loop if right is an empty string
|
||||||
while (row.endsWith(right)) {
|
while (row.endsWith(right)) {
|
||||||
row = row.slice(0, row.length - right.length);
|
row = row.slice(0, row.length - right.length);
|
||||||
if (!multiLevel) {
|
if (!multiLevel) {
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return row;
|
}
|
||||||
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unwrapList(
|
export function unwrapList(
|
||||||
splitOperatorType: SplitOperatorType,
|
splitOperatorType: SplitOperatorType,
|
||||||
input: string,
|
input: string,
|
||||||
splitSeparator: string,
|
splitSeparator: string,
|
||||||
joinSeparator: string,
|
joinSeparator: string,
|
||||||
deleteEmptyItems: boolean,
|
deleteEmptyItems: boolean,
|
||||||
multiLevel: boolean,
|
multiLevel: boolean,
|
||||||
trimItems: boolean,
|
trimItems: boolean,
|
||||||
left: string = '',
|
left: string = '',
|
||||||
right: string = ''
|
right: string = ''
|
||||||
): string {
|
): string {
|
||||||
let array: string[];
|
let array: string[];
|
||||||
let unwrappedArray: string[] = [];
|
let unwrappedArray: string[] = [];
|
||||||
switch (splitOperatorType) {
|
switch (splitOperatorType) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
array = input.split(splitSeparator);
|
array = input.split(splitSeparator);
|
||||||
break;
|
break;
|
||||||
case 'regex':
|
case 'regex':
|
||||||
array = input.split(new RegExp(splitSeparator));
|
array = input.split(new RegExp(splitSeparator));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (deleteEmptyItems) {
|
if (deleteEmptyItems) {
|
||||||
array = array.filter(Boolean);
|
array = array.filter(Boolean);
|
||||||
}
|
}
|
||||||
|
|
||||||
// for each element of array unwrap left side then right side and push the result to a final array
|
// for each element of array unwrap left side then right side and push the result to a final array
|
||||||
for (let row of array) {
|
for (let row of array) {
|
||||||
row = leftUnwrap(row, left, multiLevel);
|
row = leftUnwrap(row, left, multiLevel);
|
||||||
row = rightUnwrap(row, right, multiLevel);
|
row = rightUnwrap(row, right, multiLevel);
|
||||||
unwrappedArray.push(row);
|
unwrappedArray.push(row);
|
||||||
}
|
}
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
|
@@ -2,69 +2,169 @@ import { expect, describe, it } from 'vitest';
|
|||||||
import { unwrapList } from './service';
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
@@ -1,32 +1,31 @@
|
|||||||
export type SplitOperatorType = 'symbol' | 'regex';
|
export type SplitOperatorType = 'symbol' | 'regex';
|
||||||
|
|
||||||
function wrap(array: string[], left: string, right: string): string[] {
|
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(
|
export function wrapList(
|
||||||
splitOperatorType: SplitOperatorType,
|
splitOperatorType: SplitOperatorType,
|
||||||
input: string,
|
input: string,
|
||||||
splitSeparator: string,
|
splitSeparator: string,
|
||||||
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[];
|
||||||
switch (splitOperatorType) {
|
switch (splitOperatorType) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
array = input.split(splitSeparator);
|
array = input.split(splitSeparator);
|
||||||
break;
|
break;
|
||||||
case 'regex':
|
case 'regex':
|
||||||
array = input.split(new RegExp(splitSeparator));
|
array = input.split(new RegExp(splitSeparator));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (deleteEmptyItems) {
|
if (deleteEmptyItems) {
|
||||||
array = array.filter(Boolean);
|
array = array.filter(Boolean);
|
||||||
}
|
}
|
||||||
wrappedArray = wrap(array, left, right);
|
wrappedArray = wrap(array, left, right);
|
||||||
return wrappedArray.join(joinSeparator);
|
return wrappedArray.join(joinSeparator);
|
||||||
}
|
}
|
||||||
|
@@ -1,137 +1,132 @@
|
|||||||
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', () => {
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
const splitSeparator = ', ';
|
const splitSeparator = ', ';
|
||||||
const joinSeparator = ', ';
|
const joinSeparator = ', ';
|
||||||
const deleteEmptyItems = false;
|
const deleteEmptyItems = false;
|
||||||
|
|
||||||
|
const result = wrapList(
|
||||||
|
splitOperatorType,
|
||||||
|
input,
|
||||||
|
splitSeparator,
|
||||||
|
joinSeparator,
|
||||||
|
deleteEmptyItems
|
||||||
|
);
|
||||||
|
|
||||||
const result = wrapList(
|
expect(result).toBe('apple, pineaple, lemon, orange, mango');
|
||||||
splitOperatorType,
|
});
|
||||||
input,
|
|
||||||
splitSeparator,
|
|
||||||
joinSeparator,
|
|
||||||
deleteEmptyItems
|
|
||||||
);
|
|
||||||
|
|
||||||
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', () => {
|
expect(result).toBe(
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
'the apple, the pineaple, the lemon, the orange, the mango'
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
);
|
||||||
const splitSeparator = ', ';
|
});
|
||||||
const joinSeparator = ', ';
|
|
||||||
const left = 'the ';
|
|
||||||
const deleteEmptyItems = false;
|
|
||||||
|
|
||||||
const result = wrapList(
|
it('should append to right if defined', () => {
|
||||||
splitOperatorType,
|
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||||
input,
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
splitSeparator,
|
const splitSeparator = ', ';
|
||||||
joinSeparator,
|
const joinSeparator = ', ';
|
||||||
deleteEmptyItems,
|
const left = '';
|
||||||
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', () => {
|
it('should append to both side if both defined', () => {
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango';
|
const input: string = 'apple, pineaple, lemon, orange, mango';
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
const splitOperatorType: SplitOperatorType = 'symbol';
|
||||||
const splitSeparator = ', ';
|
const splitSeparator = ', ';
|
||||||
const joinSeparator = ', ';
|
const joinSeparator = ', ';
|
||||||
const left = '';
|
const deleteEmptyItems = false;
|
||||||
const right = 'z';
|
const left = 'K';
|
||||||
const deleteEmptyItems = false;
|
const right = 'z';
|
||||||
|
|
||||||
const result = wrapList(
|
const result = wrapList(
|
||||||
splitOperatorType,
|
splitOperatorType,
|
||||||
input,
|
input,
|
||||||
splitSeparator,
|
splitSeparator,
|
||||||
joinSeparator,
|
joinSeparator,
|
||||||
deleteEmptyItems,
|
deleteEmptyItems,
|
||||||
left,
|
left,
|
||||||
right);
|
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', () => {
|
it('should append to both side if both defined and not 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';
|
||||||
const splitSeparator = ', ';
|
const splitSeparator = ', ';
|
||||||
const joinSeparator = ', ';
|
const joinSeparator = ', ';
|
||||||
const deleteEmptyItems = false;
|
const deleteEmptyItems = false;
|
||||||
const left = 'K';
|
const left = 'K';
|
||||||
const right = 'z';
|
const right = 'z';
|
||||||
|
|
||||||
const result = wrapList(
|
const result = wrapList(
|
||||||
splitOperatorType,
|
splitOperatorType,
|
||||||
input,
|
input,
|
||||||
splitSeparator,
|
splitSeparator,
|
||||||
joinSeparator,
|
joinSeparator,
|
||||||
deleteEmptyItems,
|
deleteEmptyItems,
|
||||||
left,
|
left,
|
||||||
right);
|
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 result = wrapList(
|
||||||
const input: string = 'apple, pineaple, lemon, orange, mango, ';
|
splitOperatorType,
|
||||||
const splitOperatorType: SplitOperatorType = 'symbol';
|
input,
|
||||||
const splitSeparator = ', ';
|
splitSeparator,
|
||||||
const joinSeparator = ', ';
|
joinSeparator,
|
||||||
const deleteEmptyItems = false;
|
deleteEmptyItems,
|
||||||
const left = 'K';
|
left,
|
||||||
const right = 'z';
|
right
|
||||||
|
);
|
||||||
|
|
||||||
const result = wrapList(
|
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
|
||||||
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');
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
})
|
|
||||||
|
Reference in New Issue
Block a user