mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-14 09:57:01 -04:00
feat(list-converter): a small converter who deals with column based data and do some stuff with it (#387)
* feat(list-converter): a small converter who deals with column based data and do some stuff with it * Update src/tools/list-converter/index.ts * Update src/tools/list-converter/index.ts * Update src/tools/list-converter/index.ts --------- Co-authored-by: Corentin THOMASSET <corentin.thomasset74@gmail.com> fix(list-format): fix e2e
This commit is contained in:
parent
ce3150c65d
commit
83a7b3bae9
9 changed files with 441 additions and 139 deletions
27
src/tools/list-converter/list-converter.models.ts
Normal file
27
src/tools/list-converter/list-converter.models.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import _ from 'lodash';
|
||||
import { byOrder } from '@/utils/array';
|
||||
import type { ConvertOptions } from './list-converter.types';
|
||||
|
||||
export { convert };
|
||||
|
||||
const whenever =
|
||||
<T, R>(condition: boolean, fn: (value: T) => R) =>
|
||||
(value: T) =>
|
||||
condition ? fn(value) : value;
|
||||
|
||||
function convert(list: string, options: ConvertOptions): string {
|
||||
const lineBreak = options.keepLineBreaks ? '\n' : '';
|
||||
|
||||
return _.chain(list)
|
||||
.thru(whenever(options.lowerCase, (text) => text.toLowerCase()))
|
||||
.split('\n')
|
||||
.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))
|
||||
.without('')
|
||||
.map((p) => options.itemPrefix + p + options.itemSuffix)
|
||||
.join(options.separator + lineBreak)
|
||||
.thru((text) => [options.listPrefix, text, options.listSuffix].join(lineBreak))
|
||||
.value();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue