Skip to content

Commit

Permalink
feat(table): border options
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Nov 17, 2024
1 parent 3014ea3 commit 303f82f
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 38 deletions.
44 changes: 38 additions & 6 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -10309,7 +10309,13 @@ Sort values in natural order.
<summary>Type Definition</summary>

```typescript
function naturalSort<T extends any[]>(arr: T): T;
namespace naturalSort {
interface INaturalSort {
<T extends any[]>(arr: T): T;
comparator(a: any, b: any): number;
}
}
const naturalSort: naturalSort.INaturalSort;
```

</details>
Expand Down Expand Up @@ -13168,15 +13174,41 @@ Output table string.
<summary>Type Definition</summary>

```typescript
function table(rows: Array<string[]>): 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<string[]>;
}
function table(
rows: Array<string[]>,
options?: table.IOptions
): string;
```

</details>

|Name |Desc |
|------|------------|
|rows |Table data |
|return|Table string|
|Name |Desc |
|-------|-------------|
|rows |Table data |
|options|Table options|
|return |Table string |

```javascript
table([
Expand Down
36 changes: 34 additions & 2 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10302,7 +10302,13 @@ ms(60000); // -> '1m'
<summary>类型定义</summary>

```typescript
function naturalSort<T extends any[]>(arr: T): T;
namespace naturalSort {
interface INaturalSort {
<T extends any[]>(arr: T): T;
comparator(a: any, b: any): number;
}
}
const naturalSort: naturalSort.INaturalSort;
```

</details>
Expand Down Expand Up @@ -13159,14 +13165,40 @@ swap(arr, 0, 1); // -> [2, 1]
<summary>类型定义</summary>

```typescript
function table(rows: Array<string[]>): 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<string[]>;
}
function table(
rows: Array<string[]>,
options?: table.IOptions
): string;
```

</details>

|参数名|说明|
|-----|---|
|rows|表格数据|
|options|选项|
|返回值|表格字符串|

```javascript
Expand Down
1 change: 1 addition & 0 deletions i18n/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
|参数名|说明|
|-----|---|
|rows|表格数据|
|options|选项|
|返回值|表格字符串|
3 changes: 2 additions & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -6412,7 +6412,8 @@
"strWidth",
"map",
"repeat",
"cloneDeep"
"cloneDeep",
"defaults"
],
"description": "Output table string.",
"env": [
Expand Down
45 changes: 35 additions & 10 deletions src/table.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,16 +22,40 @@
*/

/* typescript
* export declare function table(rows: Array<string[]>): 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<string[]>;
* }
* export declare function table(
* rows: Array<string[]>,
* 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);
Expand Down
69 changes: 50 additions & 19 deletions test/table.js
Original file line number Diff line number Diff line change
@@ -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 │
└─────────────────────────────────┘
`
]);
});

0 comments on commit 303f82f

Please sign in to comment.