All files / lib/util Formatter.ts

100% Statements 27/27
85.71% Branches 6/7
100% Functions 6/6
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                              1x 1x             1x   1x       1x 644x 644x 644x               1x         19246x 19246x   1x 7757x 7757x   1x 19x 19x   1x 7x 7x   1x 7x 7x 1x 1x   1x 1x      
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Base } from '~/straps';
 
/**
 * @name Formatter
 * @class
 * @private
 */
export const Formatter = Base.extend(
  // const FormatterBase = Base.extend(
  /** @lends Formatter# */ {
    /**
     * @param {Number} [precision=5] the amount of fractional digits
     */
    initialize: function (precision) {
      this.precision = Base.pick(precision, 5);
      this.multiplier = Math.pow(10, this.precision);
    },
 
    /**
     * Utility function for rendering numbers as strings at a precision of
     * up to the amount of fractional digits.
     *
     * @param {Number} num the number to be converted to a string
     */
    number: function (val) {
      // It would be nice to use Number#toFixed() instead, but it pads with 0,
      // unnecessarily consuming space.
      // If precision is >= 16, don't do anything at all, since that appears
      // to be the limit of the precision (it actually varies).
      return this.precision < 16 ? Math.round(val * this.multiplier) / this.multiplier : val;
    },
 
    pair: function (val1, val2, separator) {
      return this.number(val1) + (separator || ',') + this.number(val2);
    },
 
    point: function (val, separator) {
      return this.number(val.x) + (separator || ',') + this.number(val.y);
    },
 
    size: function (val, separator) {
      return this.number(val.width) + (separator || ',') + this.number(val.height);
    },
 
    rectangle: function (val, separator) {
      return this.point(val, separator) + (separator || ',') + this.size(val, separator);
    },
  }
);
 
ref.Formatter = Formatter;
Formatter.instance = new Formatter();
 
// export const Formatter = FormatterBase;