Formatting and parsing

function O.numberFormatter(format)

Returns a function which takes a single numeric argument, and returns a string representation according to format.

If a function is passed to this function, it will return the function unaltered. This is so APIs can take format strings or custom formatters, and pass them through O.numberFormatter() to get a formatter function.

The format string uses the same formats as Java’s java.text.DecimalFormat.

For performance, call this once to create a formatter which is called many times.

  let formatter = O.numberFormatter("#,##0.00");
  let string = formatter(123456);
  // Reuse formatter to format other numbers.

function O.dateParser(format)

Returns a function which takes a single string argument, and returns a Date parsed according to format.

The format string uses the same formats as Java’s java.text.SimpleDateFormat.

If the string passed to the parser does not match the format, null is returned.

For performance, call this once to create a parser which is called many times.

  let parser = O.dateParser("yyyy-MM-dd");
  let date = parser("2018-02-12");
  // Reuse parser for any other dates.