diff --git a/components.d.ts b/components.d.ts index 89f41f808..f1a030728 100644 --- a/components.d.ts +++ b/components.d.ts @@ -132,6 +132,7 @@ declare module '@vue/runtime-core' { NCode: typeof import('naive-ui')['NCode'] NCollapseTransition: typeof import('naive-ui')['NCollapseTransition'] NConfigProvider: typeof import('naive-ui')['NConfigProvider'] + NDivider: typeof import('naive-ui')['NDivider'] NEllipsis: typeof import('naive-ui')['NEllipsis'] NForm: typeof import('naive-ui')['NForm'] NFormItem: typeof import('naive-ui')['NFormItem'] @@ -144,6 +145,7 @@ declare module '@vue/runtime-core' { NMenu: typeof import('naive-ui')['NMenu'] NScrollbar: typeof import('naive-ui')['NScrollbar'] NSlider: typeof import('naive-ui')['NSlider'] + NSpace: typeof import('naive-ui')['NSpace'] NSwitch: typeof import('naive-ui')['NSwitch'] NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default'] OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default'] diff --git a/src/tools/list-converter/list-converter.models.test.ts b/src/tools/list-converter/list-converter.models.test.ts index d6c87773b..6229c5c75 100644 --- a/src/tools/list-converter/list-converter.models.test.ts +++ b/src/tools/list-converter/list-converter.models.test.ts @@ -6,19 +6,11 @@ describe('list-converter', () => { describe('convert', () => { it('should convert a given list', () => { const options: ConvertOptions = { - separator: ', ', + itemsSeparator: ', ', trimItems: true, removeDuplicates: true, itemPrefix: '"', itemSuffix: '"', - removeItemPrefix: '', - removeItemSuffix: '', - listPrefix: '', - listSuffix: '', - reverseList: false, - sortList: null, - lowerCase: false, - keepLineBreaks: false, }; const input = ` 1 @@ -33,38 +25,21 @@ describe('list-converter', () => { it('should return an empty value for an empty input', () => { const options: ConvertOptions = { - separator: ', ', + itemsSeparator: ', ', trimItems: true, removeDuplicates: true, - itemPrefix: '', - itemSuffix: '', - removeItemPrefix: '', - removeItemSuffix: '', - listPrefix: '', - listSuffix: '', - reverseList: false, - sortList: null, - lowerCase: false, - keepLineBreaks: false, }; expect(convert('', options)).toEqual(''); }); it('should keep line breaks', () => { const options: ConvertOptions = { - separator: '', trimItems: true, itemPrefix: '
  • ', itemSuffix: '
  • ', - removeItemPrefix: '', - removeItemSuffix: '', listPrefix: '', keepLineBreaks: true, - lowerCase: false, - removeDuplicates: false, - reverseList: false, - sortList: null, }; const input = ` 1 @@ -81,30 +56,61 @@ describe('list-converter', () => { it('should remove prefix and suffix', () => { const options: ConvertOptions = { - separator: '', trimItems: true, - itemPrefix: '', - itemSuffix: '', - removeItemPrefix: '\', - removeItemSuffix: '\', - listPrefix: '', - listSuffix: '', + removeItemPrefix: '
  • ', + removeItemSuffix: '
  • ', keepLineBreaks: true, - lowerCase: false, - removeDuplicates: false, - reverseList: false, - sortList: null, }; const input = `
  • 1
  • 2
  • 3
  • `; - const expected = ` -1 + const expected = `1 2 +3`; + expect(convert(input, options)).toEqual(expected); + }); + + it('should split by separator', () => { + const options: ConvertOptions = { + trimItems: true, + keepLineBreaks: true, + splitBySeparator: ',', + }; + const input = '1,2,3'; + const expected = `1 +2 +3`; + expect(convert(input, options)).toEqual(expected); + }); + + it('should sort by asc-num', () => { + const options: ConvertOptions = { + trimItems: true, + keepLineBreaks: true, + sortList: 'asc-num', + }; + const input = `3 +20 +1`; + const expected = `1 3 -`; +20`; + expect(convert(input, options)).toEqual(expected); + }); + it('should sort by desc', () => { + const options: ConvertOptions = { + trimItems: true, + keepLineBreaks: true, + sortList: 'desc', + }; + const input = `1 +20 +3`; + const expected = `3 +20 +1`; expect(convert(input, options)).toEqual(expected); }); }); diff --git a/src/tools/list-converter/list-converter.models.ts b/src/tools/list-converter/list-converter.models.ts index 5c047293c..9c88128bf 100644 --- a/src/tools/list-converter/list-converter.models.ts +++ b/src/tools/list-converter/list-converter.models.ts @@ -4,7 +4,7 @@ import { byOrder } from '@/utils/array'; export { convert }; -function whenever(condition: boolean, fn: (value: T) => R) { +function whenever(condition: boolean | undefined, fn: (value: T) => R) { return (value: T) => condition ? fn(value) : value; } @@ -14,16 +14,16 @@ function convert(list: string, options: ConvertOptions): string { return _.chain(list) .thru(whenever(options.lowerCase, text => text.toLowerCase())) - .split('\n') + .split(new RegExp(`[${options.splitBySeparator || ''}\n]`, 'g')) .thru(whenever(options.removeDuplicates, _.uniq)) .thru(whenever(options.reverseList, _.reverse)) - .thru(whenever(!_.isNull(options.sortList), parts => parts.sort(byOrder({ order: options.sortList })))) .map(whenever(options.trimItems, _.trim)) + .thru(whenever(!!options.sortList, parts => parts.sort(byOrder({ order: options.sortList })))) .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)) + .map(p => (options.itemPrefix || '') + p + (options.itemSuffix || '')) + .join((options.itemsSeparator || '') + lineBreak) + .thru(text => [options.listPrefix, text, options.listSuffix].filter(l => l).join(lineBreak)) .value(); } diff --git a/src/tools/list-converter/list-converter.types.ts b/src/tools/list-converter/list-converter.types.ts index 448f1a5a7..a6c099e88 100644 --- a/src/tools/list-converter/list-converter.types.ts +++ b/src/tools/list-converter/list-converter.types.ts @@ -1,17 +1,18 @@ -export type SortOrder = 'asc' | 'desc' | null; +export type SortOrder = null | 'asc' | 'desc' | 'asc-num' | 'desc-num'; export interface ConvertOptions { - lowerCase: boolean - trimItems: boolean - itemPrefix: string - itemSuffix: string - removeItemPrefix: string - removeItemSuffix: string - listPrefix: string - listSuffix: string - reverseList: boolean - sortList: SortOrder - removeDuplicates: boolean - separator: string - keepLineBreaks: boolean + lowerCase?: boolean + trimItems?: boolean + itemPrefix?: string + itemSuffix?: string + removeItemPrefix?: string + removeItemSuffix?: string + listPrefix?: string + listSuffix?: string + reverseList?: boolean + sortList?: SortOrder + removeDuplicates?: boolean + itemsSeparator?: string + splitBySeparator?: string + keepLineBreaks?: boolean } diff --git a/src/tools/list-converter/list-converter.vue b/src/tools/list-converter/list-converter.vue index 58c581880..550da926c 100644 --- a/src/tools/list-converter/list-converter.vue +++ b/src/tools/list-converter/list-converter.vue @@ -4,15 +4,25 @@ import { convert } from './list-converter.models'; import type { ConvertOptions } from './list-converter.types'; const sortOrderOptions = [ + { + label: 'No Sort', + value: null, + }, { label: 'Sort ascending', value: 'asc', - disabled: false, }, { label: 'Sort descending', value: 'desc', - disabled: false, + }, + { + label: 'Sort ascending (Numeric)', + value: 'asc-num', + }, + { + label: 'Sort descending (Numeric)', + value: 'desc-num', }, ]; @@ -29,7 +39,8 @@ const conversionConfig = useStorage('list-converter:conversionCo listSuffix: '', reverseList: false, sortList: null, - separator: ', ', + itemsSeparator: ', ', + splitBySeparator: '', }); function transformer(value: string) { @@ -41,7 +52,7 @@ function transformer(value: string) {
    -
    +
    @@ -62,7 +73,7 @@ function transformer(value: string) {
    -
    +
    + + @@ -125,7 +146,7 @@ function transformer(value: string) { />
    -
    +
    diff --git a/src/utils/array.ts b/src/utils/array.ts index 15b3506dc..3dd947c0a 100644 --- a/src/utils/array.ts +++ b/src/utils/array.ts @@ -1,7 +1,13 @@ export { byOrder }; -function byOrder({ order }: { order: 'asc' | 'desc' | null | undefined }) { +function byOrder({ order }: { order: 'asc' | 'desc' | 'asc-num' | 'desc-num' | null | undefined }) { return (a: string, b: string) => { + if (order === 'asc-num' || order === 'desc-num') { + const numOpt = { + numeric: true, + }; + return order === 'asc-num' ? a.localeCompare(b, undefined, numOpt) : b.localeCompare(a, undefined, numOpt); + } return order === 'asc' ? a.localeCompare(b) : b.localeCompare(a); }; }