Skip to content

Commit

Permalink
feat(List Converter): add remove prefix/suffix capability
Browse files Browse the repository at this point in the history
Fix #702
  • Loading branch information
sharevb committed Sep 21, 2024
1 parent 318fb6e commit 56d779e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/tools/list-converter/list-converter.models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('list-converter', () => {
removeDuplicates: true,
itemPrefix: '"',
itemSuffix: '"',
removeItemPrefix: '',
removeItemSuffix: '',
listPrefix: '',
listSuffix: '',
reverseList: false,
Expand All @@ -36,6 +38,8 @@ describe('list-converter', () => {
removeDuplicates: true,
itemPrefix: '',
itemSuffix: '',
removeItemPrefix: '',
removeItemSuffix: '',
listPrefix: '',
listSuffix: '',
reverseList: false,
Expand All @@ -52,6 +56,8 @@ describe('list-converter', () => {
trimItems: true,
itemPrefix: '<li>',
itemSuffix: '</li>',
removeItemPrefix: '',
removeItemSuffix: '',
listPrefix: '<ul>',
listSuffix: '</ul>',
keepLineBreaks: true,
Expand All @@ -72,5 +78,32 @@ describe('list-converter', () => {
</ul>`;
expect(convert(input, options)).toEqual(expected);
});

it('should remove prefix and suffix', () => {
const options: ConvertOptions = {
separator: '',
trimItems: true,
itemPrefix: '',
itemSuffix: '',
removeItemPrefix: '\<li\>',
removeItemSuffix: '\</li\>',
listPrefix: '',
listSuffix: '',
keepLineBreaks: true,
lowerCase: false,
removeDuplicates: false,
reverseList: false,
sortList: null,
};
const input = `
<li>1</li>
<li>2</li>
<li>3</li>
`;
const expected = `1
2
3`;
expect(convert(input, options)).toEqual(expected);
});
});
});
2 changes: 2 additions & 0 deletions src/tools/list-converter/list-converter.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function convert(list: string, options: ConvertOptions): string {
.thru(whenever(!_.isNull(options.sortList), parts => parts.sort(byOrder({ order: options.sortList }))))
.map(whenever(options.trimItems, _.trim))
.without('')
.map(p => options.removeItemPrefix ? p.replace(new RegExp(`^${options.removeItemPrefix}`, 'g'), '') : p)
.map(p => options.removeItemSuffix ? p.replace(new RegExp(`${options.removeItemSuffix}$`, 'g'), '') : p)
.map(p => options.itemPrefix + p + options.itemSuffix)
.join(options.separator + lineBreak)
.thru(text => [options.listPrefix, text, options.listSuffix].join(lineBreak))
Expand Down
2 changes: 2 additions & 0 deletions src/tools/list-converter/list-converter.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface ConvertOptions {
trimItems: boolean
itemPrefix: string
itemSuffix: string
removeItemPrefix: string
removeItemSuffix: string
listPrefix: string
listSuffix: string
reverseList: boolean
Expand Down
15 changes: 15 additions & 0 deletions src/tools/list-converter/list-converter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const conversionConfig = useStorage<ConvertOptions>('list-converter:conversionCo
keepLineBreaks: false,
itemPrefix: '',
itemSuffix: '',
removeItemPrefix: '',
removeItemSuffix: '',
listPrefix: '',
listSuffix: '',
reverseList: false,
Expand Down Expand Up @@ -85,6 +87,19 @@ function transformer(value: string) {
placeholder=","
/>

<n-form-item label="Unwrap item" label-placement="left" label-width="120" :show-feedback="false" mb-2>
<c-input-text
v-model:value="conversionConfig.removeItemPrefix"
placeholder="Remove item prefix regex"
test-id="removeItemPrefix"
/>
<c-input-text
v-model:value="conversionConfig.removeItemSuffix"
placeholder="Remove item suffix regex"
test-id="removeItemSuffix"
/>
</n-form-item>

<n-form-item label="Wrap item" label-placement="left" label-width="120" :show-feedback="false" mb-2>
<c-input-text
v-model:value="conversionConfig.itemPrefix"
Expand Down

0 comments on commit 56d779e

Please sign in to comment.