Input and output number separators
npm install @flourish/number-localizationFlourish module for setting the numeric separators used in a template. Used in conjunction with Flourish number formatter.
To install the module, type:
`` shell`
npm install @flourish/number-localization
To add the module's settings block, include the following in a templates' template.yml file:
` yml`
- property: localization
import: "@flourish/number-localization"
Include a (usually empty) object in the state declaration:
` js`
var state = {
/ Other declarations of default values /
localization: {},
/ Any further state properties /
}
Make sure an instance of the module is initialised somewhere in the code before draw is called:
` js`
import initLocalization from "@flourish/number-localization";;
/ Other imports and code /
var localization = initLocalization(state.localization);
/ Other initialisation code, eg Flourish-number-formatter instances /
export { localization/, etc / };
This will add two properties to state.localization (if they don't already exist), input_decimal_separator (defaults to ".") and output_separators (defaults to ",."). The exported localization object contains two methods, getParser and getFormatterFunction.
The getParser method returns a function for parsing text into numbers based on the value of input_decimal_separator at the time it is called;
` js`
import { localization } from "init.js";
var parser = localization.getParser();
["1.2", "-4,000", "$326k"].map(parser) // Returns [1.2, -4000, 326] for input_decimal_separator === "."
// The above would return [12, -4, 326] for input_decimal_separator === ","
The getFormatterFunction method creates a locale function based on the value of output_separators at the time it is called. It is meant to be passed on to instances of Flourish number formatter:`js``
import { localization, getXAxisFormatter, getYAxisFormatter } from "init.js";
var localeFunction = localization.getFormatterFunction();
var xFormat = getXAxisFormatter(localeFunction);
var yFormat = getYAxisFormatter(localeFunction);