-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea61184
commit 3db99bd
Showing
8 changed files
with
121 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import type { | ||
Row, | ||
} from './types/internal'; | ||
import { | ||
normalizeString, | ||
} from './utils'; | ||
|
||
export default (rows: unknown[][]): Row[] => { | ||
return rows.map((cells) => { | ||
return cells.map(String); | ||
return cells.map((cell) => { | ||
return normalizeString(String(cell)); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import slice from 'slice-ansi'; | ||
import stripAnsi from 'strip-ansi'; | ||
import getBorderCharacters from './getBorderCharacters'; | ||
import type { | ||
BorderConfig, BorderUserConfig, | ||
} from './types/api'; | ||
|
||
/** | ||
* Converts Windows-style newline to Unix-style | ||
* | ||
* @internal | ||
*/ | ||
export const normalizeString = (input: string): string => { | ||
return input.replace(/\r\n/g, '\n'); | ||
}; | ||
|
||
/** | ||
* Splits ansi string by newlines | ||
* | ||
* @internal | ||
*/ | ||
export const splitAnsi = (input: string): string[] => { | ||
const lengths = stripAnsi(input).split('\n').map(({length}) => { | ||
return length; | ||
}); | ||
|
||
const result: string[] = []; | ||
let startIndex = 0; | ||
|
||
lengths.forEach((length) => { | ||
result.push(length === 0 ? '' : slice(input, startIndex, startIndex + length)); | ||
|
||
// Plus 1 for the newline character itself | ||
startIndex += length + 1; | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* Merges user provided border characters with the default border ("honeywell") characters. | ||
* | ||
* @internal | ||
*/ | ||
export const makeBorder = (border: BorderUserConfig | undefined): BorderConfig => { | ||
return { | ||
...getBorderCharacters('honeywell'), | ||
...border, | ||
}; | ||
}; | ||
|
||
/** | ||
* Groups the array into sub-arrays by sizes. | ||
* | ||
* @internal | ||
* @example | ||
* groupBySizes(['a', 'b', 'c', 'd', 'e'], [2, 1, 2]) = [ ['a', 'b'], ['c'], ['d', 'e'] ] | ||
*/ | ||
|
||
export const groupBySizes = <T>(array: T[], sizes: number[]): T[][] => { | ||
let startIndex = 0; | ||
|
||
return sizes.map((size) => { | ||
const group = array.slice(startIndex, startIndex + size); | ||
|
||
startIndex += size; | ||
|
||
return group; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters