From 303f82fbf8d12eae9604ef7796d5ea162dd95332 Mon Sep 17 00:00:00 2001 From: surunzi Date: Sun, 17 Nov 2024 08:42:48 +0800 Subject: [PATCH] feat(table): border options --- DOC.md | 44 +++++++++++++++++++++++++++----- DOC_CN.md | 36 +++++++++++++++++++++++++-- i18n/table.md | 1 + index.json | 3 ++- src/table.js | 45 +++++++++++++++++++++++++-------- test/table.js | 69 +++++++++++++++++++++++++++++++++++++-------------- 6 files changed, 160 insertions(+), 38 deletions(-) diff --git a/DOC.md b/DOC.md index 98e9cf0e..d3c8f9d9 100644 --- a/DOC.md +++ b/DOC.md @@ -10309,7 +10309,13 @@ Sort values in natural order. Type Definition ```typescript -function naturalSort(arr: T): T; +namespace naturalSort { + interface INaturalSort { + (arr: T): T; + comparator(a: any, b: any): number; + } +} +const naturalSort: naturalSort.INaturalSort; ``` @@ -13168,15 +13174,41 @@ Output table string. Type Definition ```typescript -function table(rows: Array): string; +namespace table { + interface IOptions { + border?: { + topBody?: string; + topJoin?: string; + topLeft?: string; + topRight?: string; + bottomBody?: string; + bottomJoin?: string; + bottomLeft?: string; + bottomRight?: string; + bodyLeft?: string; + bodyRight?: string; + bodyJoin?: string; + joinBody?: string; + joinLeft?: string; + joinRight?: string; + joinJoin?: string; + }; + } + function parse(table: string, options?: IOptions): Array; +} +function table( + rows: Array, + options?: table.IOptions +): string; ``` -|Name |Desc | -|------|------------| -|rows |Table data | -|return|Table string| +|Name |Desc | +|-------|-------------| +|rows |Table data | +|options|Table options| +|return |Table string | ```javascript table([ diff --git a/DOC_CN.md b/DOC_CN.md index e753bbf8..f8dcefc1 100644 --- a/DOC_CN.md +++ b/DOC_CN.md @@ -10302,7 +10302,13 @@ ms(60000); // -> '1m' 类型定义 ```typescript -function naturalSort(arr: T): T; +namespace naturalSort { + interface INaturalSort { + (arr: T): T; + comparator(a: any, b: any): number; + } +} +const naturalSort: naturalSort.INaturalSort; ``` @@ -13159,7 +13165,32 @@ swap(arr, 0, 1); // -> [2, 1] 类型定义 ```typescript -function table(rows: Array): string; +namespace table { + interface IOptions { + border?: { + topBody?: string; + topJoin?: string; + topLeft?: string; + topRight?: string; + bottomBody?: string; + bottomJoin?: string; + bottomLeft?: string; + bottomRight?: string; + bodyLeft?: string; + bodyRight?: string; + bodyJoin?: string; + joinBody?: string; + joinLeft?: string; + joinRight?: string; + joinJoin?: string; + }; + } + function parse(table: string, options?: IOptions): Array; +} +function table( + rows: Array, + options?: table.IOptions +): string; ``` @@ -13167,6 +13198,7 @@ function table(rows: Array): string; |参数名|说明| |-----|---| |rows|表格数据| +|options|选项| |返回值|表格字符串| ```javascript diff --git a/i18n/table.md b/i18n/table.md index 01d104e0..36da6cd5 100644 --- a/i18n/table.md +++ b/i18n/table.md @@ -5,4 +5,5 @@ |参数名|说明| |-----|---| |rows|表格数据| +|options|选项| |返回值|表格字符串| diff --git a/index.json b/index.json index b8076569..50c44850 100644 --- a/index.json +++ b/index.json @@ -6412,7 +6412,8 @@ "strWidth", "map", "repeat", - "cloneDeep" + "cloneDeep", + "defaults" ], "description": "Output table string.", "env": [ diff --git a/src/table.js b/src/table.js index cb400181..9331c004 100644 --- a/src/table.js +++ b/src/table.js @@ -1,9 +1,10 @@ /* Output table string. * - * |Name |Desc | - * |------|------------| - * |rows |Table data | - * |return|Table string| + * |Name |Desc | + * |-------|-------------| + * |rows |Table data | + * |options|Table options| + * |return |Table string | */ /* example @@ -21,16 +22,40 @@ */ /* typescript - * export declare function table(rows: Array): string; + * export declare namespace table { + * interface IOptions { + * border?: { + * topBody?: string; + * topJoin?: string; + * topLeft?: string; + * topRight?: string; + * bottomBody?: string; + * bottomJoin?: string; + * bottomLeft?: string; + * bottomRight?: string; + * bodyLeft?: string; + * bodyRight?: string; + * bodyJoin?: string; + * joinBody?: string; + * joinLeft?: string; + * joinRight?: string; + * joinJoin?: string; + * }; + * } + * function parse(table: string, options?: IOptions): Array; + * } + * export declare function table( + * rows: Array, + * options?: table.IOptions + * ): string; */ -_('each strWidth map repeat cloneDeep'); +_('each strWidth map repeat cloneDeep defaults'); -exports = function(rows) { +exports = function(rows, options = {}) { rows = cloneDeep(rows); - const options = { - border: defBorder - }; + options.border = options.border || {}; + defaults(options.border, defBorder); options.columns = getColumns(rows); padData(rows, options); diff --git a/test/table.js b/test/table.js index ca67d30a..37254e28 100644 --- a/test/table.js +++ b/test/table.js @@ -1,19 +1,50 @@ -test([ - [ - ['', 'firstName', 'lastName'], - ['daughter', 'Emily', 'Smith'], - ['father', 'John', 'Smith'], - ['mother', 'Jane', 'Smith'] - ], - util.stripIndent` - ┌──────────┬───────────┬──────────┐ - │ │ firstName │ lastName │ - ├──────────┼───────────┼──────────┤ - │ daughter │ Emily │ Smith │ - ├──────────┼───────────┼──────────┤ - │ father │ John │ Smith │ - ├──────────┼───────────┼──────────┤ - │ mother │ Jane │ Smith │ - └──────────┴───────────┴──────────┘ - ` -]); +const stripIndent = util.stripIndent; + +const data = [ + ['', 'firstName', 'lastName'], + ['daughter', 'Emily', 'Smith'], + ['father', 'John', 'Smith'], + ['mother', 'Jane', 'Smith'] +]; + +it('stringify', () => { + test([ + data, + stripIndent` + ┌──────────┬───────────┬──────────┐ + │ │ firstName │ lastName │ + ├──────────┼───────────┼──────────┤ + │ daughter │ Emily │ Smith │ + ├──────────┼───────────┼──────────┤ + │ father │ John │ Smith │ + ├──────────┼───────────┼──────────┤ + │ mother │ Jane │ Smith │ + └──────────┴───────────┴──────────┘ + ` + ]); +}); + +it('custom table borders', () => { + test([ + data, + { + border: { + topJoin: '─', + bodyJoin: ' ', + joinJoin: '─', + bottomJoin: '─' + } + }, + stripIndent` + ┌─────────────────────────────────┐ + │ firstName lastName │ + ├─────────────────────────────────┤ + │ daughter Emily Smith │ + ├─────────────────────────────────┤ + │ father John Smith │ + ├─────────────────────────────────┤ + │ mother Jane Smith │ + └─────────────────────────────────┘ + ` + ]); +});