openexchangerates / accounting.js

A lightweight JavaScript library for number, money and currency formatting - fully localisable, zero dependencies.
http://openexchangerates.github.io/accounting.js
MIT License
4.95k stars 532 forks source link

Force specific string length in formatColumn (appending new values to existing column) #25

Open wjcrowcroft opened 12 years ago

wjcrowcroft commented 12 years ago

When new rows are added to a column in a table/list of values that has been formatted with formatColumn it would be great if the added values could be column-formatted along with the preceding values, otherwise this happens:

$     1
$    23
$ 4,567
$ 890   // this one was appended but not column-formatted

Specifying an option (eg. minLength : 7), which pads the string to that length, might be one way to tackle this.

A slightly more complicated alternative - could pass in a value or array of values to match it up to, for example:

var column = ["$     1", "$    23", "$ 4,567"];

accounting.formatColumn( [890], {
    join : column
});
// --> "$   890"

Or a combination of the two.

If anyone has ideas - please add.

trevtrich commented 7 years ago

@wjcrowcroft did you get this one figured out? I would think in this case you'd want to take your values and re-formatColumn on the entire list. This doesn't rely so much on stateful behavior.

Your alternative suggestion of forcing it to a specific padded width might run into issues when the numbers are larger than the padded width you specified.