uodate wrap to handle empty items deletion and adding testcases for that feature

This commit is contained in:
Chesterkxng
2024-07-04 10:54:30 +00:00
parent 37c520d9ca
commit cc16fa5bb0
2 changed files with 59 additions and 1 deletions

View File

@@ -9,8 +9,10 @@ export function wrapList(
input: string,
splitSeparator: string,
joinSeparator: string,
deleteEmptyItems: boolean,
left: string = '',
right: string = ''
right: string = '',
): string {
let array: string[];
let wrappedArray: string[];
@@ -22,6 +24,9 @@ export function wrapList(
array = input.split(new RegExp(splitSeparator));
break;
}
if (deleteEmptyItems) {
array = array.filter(Boolean);
}
wrappedArray = wrap(array, left, right);
return wrappedArray.join(joinSeparator);
}

View File

@@ -10,6 +10,7 @@ describe('wrap function', () => {
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const deleteEmptyItems = false;
const result = wrapList(
@@ -17,6 +18,7 @@ describe('wrap function', () => {
input,
splitSeparator,
joinSeparator,
deleteEmptyItems
);
expect(result).toBe('apple, pineaple, lemon, orange, mango');
@@ -29,12 +31,14 @@ describe('wrap function', () => {
const splitSeparator = ', ';
const joinSeparator = ', ';
const left = 'the ';
const deleteEmptyItems = false;
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left);
expect(result).toBe('the apple, the pineaple, the lemon, the orange, the mango');
@@ -48,12 +52,14 @@ describe('wrap function', () => {
const joinSeparator = ', ';
const left = '';
const right = 'z';
const deleteEmptyItems = false;
const result = wrapList(
splitOperatorType,
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left,
right);
@@ -65,6 +71,7 @@ describe('wrap function', () => {
const splitOperatorType: SplitOperatorType = 'symbol';
const splitSeparator = ', ';
const joinSeparator = ', ';
const deleteEmptyItems = false;
const left = 'K';
const right = 'z';
@@ -73,6 +80,52 @@ describe('wrap function', () => {
input,
splitSeparator,
joinSeparator,
deleteEmptyItems,
left,
right);
expect(result).toBe('Kapplez, Kpineaplez, Klemonz, Korangez, Kmangoz');
});
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);
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);