avoidwork / filesize.js

JavaScript library to generate a human readable String describing the file size
https://filesizejs.com
BSD 3-Clause "New" or "Revised" License
1.61k stars 97 forks source link

Add support for localeOptions option: allow customization of i18n output #102

Closed christopherscott closed 5 years ago

christopherscott commented 5 years ago

This PR adds support for passing an additional parameter localeOptions that is used when an explicit locale is passed to allow for further customization of the output. Defaults to {}.

This is particularly useful for places where filesize is highly dynamic/animated and there is a desire to enforce a number of fixed digits, even in cases where the decimal would be zero:

Example without (notice the left-shift of the digits when no zero decimal): Sep-11-2019 12-23-00

Example with: Sep-11-2019 12-23-18

Usage:

filesize(1024, {
  locale: "en-us",
  localeOptions: { minimumFractionDigits: 1 }
})

// => "1.0 KB"

The options are from ECMA-402, and are documented on MDN.

avoidwork commented 5 years ago

Thanks! I'll prep a release