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

View File

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