<commit_before>///<reference path="../reference.ts" />

module Plottable {

  interface TimeFilterFormat {
    format: string;
    filter: (d: any) => any;
  }

  export class Formatters {

    public static currency(precision = 2, symbol = "$", prefix = true, onlyShowUnchanged = true) {
      var fixedFormatter = Formatters.fixed(precision);
      return function(d: any) {
        var formattedValue = fixedFormatter(Math.abs(d));
        if (onlyShowUnchanged && Formatters._valueChanged(Math.abs(d), formattedValue)) {
          return "";
        }
        if (formattedValue !== "") {
          if (prefix) {
            formattedValue = symbol + formattedValue;
          } else {
            formattedValue += symbol;
          }

          if (d < 0) {
            formattedValue = "-" + formattedValue;
          }
        }
        return formattedValue;
      };
    }

    public static fixed(precision = 3, onlyShowUnchanged = true) {
      Formatters.verifyPrecision(precision);
      return function(d: any) {
        var formattedValue = (<number> d).toFixed(precision);
        if (onlyShowUnchanged && Formatters._valueChanged(d, formattedValue)) {
          return "";
        }
        return formattedValue;
      };
    }

    public static general(precision = 3, onlyShowUnchanged = true) {
      Formatters.verifyPrecision(precision);
      return function(d: any) {
        if (typeof d === "number") {
          var multiplier = Math.pow(10, precision);
          var formattedValue = String(Math.round(d * multiplier) / multiplier);
          if (onlyShowUnchanged && Formatters._valueChanged(d, formattedValue)) {
            return "";
          }
          return formattedValue;
        } else {
          return String(d);
        }
      };
    }

    public static identity() {
      return function(d: any) {
        return String(d);
      };
    }

    public static percentage(precision = 0, onlyShowUnchanged = true) {
      var fixedFormatter = Formatters.fixed(precision);
      return function(d: any) {
        var formattedValue = fixedFormatter(d * 100);
        if (onlyShowUnchanged && Formatters._valueChanged(d * 100, formattedValue)) {
          return "";
        }
        if (formattedValue !== "") {
          formattedValue += "%";
        }
        return formattedValue;
      };
    }

    public static siSuffix(precision = 3) {
      Formatters.verifyPrecision(precision);
      return function(d: any) {
        return d3.format("." + precision + "s")(d);
      };
    }

    public static time() {

      var numFormats = 8;

      // these defaults were taken from d3
      // https://github.com/mbostock/d3/wiki/Time-Formatting#format_multi
      var timeFormat: { [index: number]: TimeFilterFormat } = {};

      timeFormat[0] = {
        format: ".%L",
        filter: (d: any) => d.getMilliseconds() !== 0
      };
      timeFormat[1] = {
        format: ":%S",
        filter: (d: any) => d.getSeconds() !== 0
      };
      timeFormat[2] = {
        format: "%I:%M",
        filter: (d: any) => d.getMinutes() !== 0
      };
      timeFormat[3] = {
        format: "%I %p",
        filter: (d: any) => d.getHours() !== 0
      };
      timeFormat[4] = {
        format: "%a %d",
        filter: (d: any) => d.getDay() !== 0 && d.getDate() !== 1
      };<commit_msg>Adding jsdoc for formatters<commit_after>///<reference path="../reference.ts" />

module Plottable {

  interface TimeFilterFormat {
    format: string;
    filter: (d: any) => any;
  }

  export class Formatters {

    /**
     * Creates a formatter for currency values.
     *
     * @param {number} [precision] The number of decimal places to show.
     * @param {String} [symbol] The currency symbol to use.
     * @param {boolean} [prefix] Whether to prepend or append the currency symbol.
     * @param {boolean} [onlyShowUnchanged] Whether to return a value if value changes after formatting.
     *
     * @returns {function} A formatter for currency values.
     */
    public static currency(precision = 2, symbol = "$", prefix = true, onlyShowUnchanged = true) {
      var fixedFormatter = Formatters.fixed(precision);
      return function(d: any) {
        var formattedValue = fixedFormatter(Math.abs(d));
        if (onlyShowUnchanged && Formatters._valueChanged(Math.abs(d), formattedValue)) {
          return "";
        }
        if (formattedValue !== "") {
          if (prefix) {
            formattedValue = symbol + formattedValue;
          } else {
            formattedValue += symbol;
          }

          if (d < 0) {
            formattedValue = "-" + formattedValue;
          }
        }
        return formattedValue;
      };
    }

    /**
     * Creates a formatter that displays exactly [precision] decimal places.
     *
     * @param {number} [precision] The number of decimal places to show.
     * @param {boolean} [onlyShowUnchanged] Whether to return a value if value changes after formatting.
     *
     * @returns {function} A formatter that displays exactly [precision] decimal places.
     */
    public static fixed(precision = 3, onlyShowUnchanged = true) {
      Formatters.verifyPrecision(precision);
      return function(d: any) {
        var formattedValue = (<number> d).toFixed(precision);
        if (onlyShowUnchanged && Formatters._valueChanged(d, formattedValue)) {
          return "";
        }
        return formattedValue;
      };
    }

    /**
     * Creates a formatter that formats numbers to show no more than
     * [precision] decimal places. All other values are stringified.
     *
     * @param {number} [precision] The number of decimal places to show.
     * @param {boolean} [onlyShowUnchanged] Whether to return a value if value changes after formatting.
     *
     * @returns {function} A formatter for general values.
     */
    public static general(precision = 3, onlyShowUnchanged = true) {
      Formatters.verifyPrecision(precision);
      return function(d: any) {
        if (typeof d === "number") {
          var multiplier = Math.pow(10, precision);
          var formattedValue = String(Math.round(d * multiplier) / multiplier);
          if (onlyShowUnchanged && Formatters._valueChanged(d, formattedValue)) {
            return "";
          }
          return formattedValue;
        } else {
          return String(d);
        }
      };
    }

    /**
     * Creates a formatter that stringifies its input.
     *
     * @returns {function} A formatter that stringifies its input.
     */
    public static identity() {
      return function(d: any) {
        return String(d);
      };
    }

    /**
     * Creates a formatter for percentage values.
     * Multiplies the input by 100 and appends "%".
     *
     * @param {number} [precision] The number of decimal places to show.
     * @param {boolean} [onlyShowUnchanged] Whether to return a value if value changes after formatting.
     *
     * @returns {function} A formatter for percentage values.
     */
    public static percentage(precision = 0, onlyShowUnchanged = true) {
      var fixedFormatter = Formatters.fixed(precision);
      return function(d: any) {
        var formattedValue = fixedFormatter(d * 100);
        if (onlyShowUnchanged && Formatters._valueChanged(d * 100, formattedValue)) {
          return "";
        }
        if (formattedValue !== "") {
          formattedValue += "%";
        }
        return formattedValue;
      };
    }

    /**
     * Creates a formatter for values that displays [precision] significant figures.
     *
     * @param {number} [precision] The number of significant figures to show.
     *
     * @returns {function} A formatter for SI values.
     */
    public static siSuffix(precision = 3) {
      Formatters.verifyPrecision(precision);
      return function(d: any) {
        return d3.format("." + precision + "s")(d);
      };
    }

    /**
     * Creates a formatter that displays [precision] dates.
     *
     * @returns {function} A formatter for time/date values.
     */
    public static time() {

      var numFormats = 8;

      // these defaults were taken from d3
      // https://github.com/mbostock/d3/wiki/Time-Formatting#format_multi
      var timeFormat: { [index: number]: TimeFilterFormat } = {};

      timeFormat[0] = {
        format: ".%L",
        filter: (d: any) => d.getMilliseconds() !== 0
      };
      timeFormat[1] = {
        format: ":%S",
        filter: (d: any) => d.getSeconds() !== 0
      };
      timeFormat[2] = {
        format: "%I:%M",
        filter: (d: any) => d.getMinutes() !== 0
      };
      timeFormat[3] = {
        format: "%I %p",
        filter: (d: any) => d.getHours() !== 0
      };
      timeFormat[4] = {
        format: "%a %d",
        filter: (d: any) => d.getDay() !== 0 && d.getDate() !== 1
      };
{'fraction_non_alphanumeric': 0.12644328729907175, 'fraction_numerical': 0.006905139234774734, 'mean_word_length': 2.134090102873359, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7114762', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2448, 'n_words': 684}