gajus / table

Formats data into a string table.
Other
899 stars 77 forks source link

[feat] Support justify alignment #168

Closed nam-hle closed 3 years ago

nam-hle commented 3 years ago

This PR adds the justify alignment feature (#167). The solution is to calculate the number array of adding spaces, which has a length that is equal to the number of continuous space sequences and has a sum is equal to the available width. To be prettier, the array should be a non-decreasing one, and the difference between the maximum and minimum value is not greater than 1.

Example with total width = 10:

alignString('a b c')     = 'a    b   c'
alignString('a b c d')   = 'a  b  c  d'
alignString('a b c d e') = 'a  b c d e'

The issue is when the string is too short (e.g., the last line of a paragraph), I propose to fallback to left align if we have to add more than 03 spaces in one place (instead of calculating more information about the surrounding lines). So:

alignString('a b c', 11, 'justify')     = 'a    b    c'
alignString('a b c', 12, 'justify')     = 'a b c       '

Here is the demo from moonMission file with justify the last column:

image
coveralls commented 3 years ago

Pull Request Test Coverage Report for Build 330


Totals Coverage Status
Change from base Build 325: 0.0%
Covered Lines: 363
Relevant Lines: 363

💛 - Coveralls
gajus commented 3 years ago

These PRs are great!

gajus commented 3 years ago

These PRs are great!

gajus commented 3 years ago

:tada: This PR is included in version 6.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: