All files / src filters.js

98.07% Statements 51/52
96.49% Branches 55/57
100% Functions 8/8
98.03% Lines 50/51

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                                51x 30x     21x     51x 6x   45x 9x   36x 6x   30x 30x   30x 30x                     57x 18x   39x 6x       33x                       16x 8x   8x 8x                         7x 3x   4x                               11x 1x   10x 1x   10x 10x 10x 10x 10x 1x   9x 5x     4x                         8x   4x   4x 4x 4x                 12x 12x 12x 12x 12x 12x 12x      
// @ts-check
 
import { assign, isFinite, isInteger, isNaN, isNil, isNumber, isString,
  pickBy, toString } from "lodash";
import { DateTime, Duration } from "luxon";
import { getDateTime } from "./utils";
 
/**
 * @param {string | number} bytes
 * @param {number} decimals
 * @returns {string}
 */
export function byteSize(bytes, decimals = 2) {
  // if bytes is a string that not represent a number, return as it is.
  /** @type {number} */
  let bytesFloat;
  if (isNumber(bytes)) {
    bytesFloat = bytes;
  }
  else {
    bytesFloat = parseFloat(bytes);
  }
 
  if (isNaN(bytesFloat) && isString(bytes)) {
    return bytes;
  }
  else if (isNaN(bytesFloat) || !isFinite(bytesFloat)) {
    return "#NaN";
  }
  else if (bytesFloat === 0) {
    return "0 Bytes";
  }
  const k = 1024; // Using by default Kibibyte
  const sizes = [ "Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "BiB" ]; // IEC notation
 
  const i = Math.floor(Math.log(bytesFloat) / Math.log(k));
  return parseFloat((bytesFloat / Math.pow(k, i)).toFixed(decimals)) + " " + sizes[i];
}
 
/**
 * @param {number} timestamp
 * @param {string} [format]
 * @param {luxon.DateTimeOptions} [options]
 * @returns {string|null}
 * @deprecated
 */
export function momentFilter(timestamp, format, options) {
  if (!Number.isFinite(timestamp)) {
    return "";
  }
  else if (isNil(format)) {
    return getDateTime(timestamp, options).set({ millisecond: 0 })
    .toISO({ suppressMilliseconds: true });
  }
  else {
    return getDateTime(timestamp, options).toFormat(format);
  }
}
 
/**
 * @details diplay the provided ms or seconds timestamp as a human readable date
 * @param {number} timestamp
 * @param {string} [format]
 * @param {luxon.DateTimeOptions} [options]
 * @return {string}
 */
export function dateTimeFilter(timestamp, format, options) {
  if (!Number.isFinite(timestamp)) {
    return "";
  }
  else if (isNil(format)) {
    return getDateTime(timestamp, options)
    .toLocaleString(DateTime.DATETIME_SHORT_WITH_SECONDS);
  }
  else E{
    return getDateTime(timestamp, options).toFormat(format);
  }
}
 
/**
 * @param {number} timestamp
 * @return {string}
 */
export function relativeDateTimeFilter(timestamp) {
  if (!Number.isFinite(timestamp)) {
    return "";
  }
  return getDateTime(timestamp).toRelative() || "";
}
 
/**
 * @param  {number} value
 * @param  {string} suffix
 * @param  {{ precision?: number, expLimit?: number|false, fixed: boolean }} [options]
 * @return {string}
 * @details
 *  - expLimit: value at which to switch to exponent notation (default: 10000)
 *  - precision: number of decimals (default: 3)
 *  - fixed: fixed number of decimals (precision required)
 *
 */
// eslint-disable-next-line complexity
function bigNumber(value, suffix = "", options = undefined) {
  if (isNil(value) || isNaN(value)) {
    return "-";
  }
  if (isString(value)) {
    value = Number.parseFloat(value);
  }
  const abs = Math.abs(value);
  const precision = options?.precision ?? 3;
  const expLimit = options?.expLimit ?? 10000;
  const fixed = options?.fixed ?? false;
  if (fixed) {
    return value.toFixed(precision) + suffix;
  }
  else if (expLimit === false || (abs <= expLimit && abs >= 1)) {
    return toString(Number.isInteger(value) ?
      value : Number.parseFloat(value.toFixed(precision))) + suffix;
  }
  return (value === 0) ? "0" : (value.toExponential(precision) + suffix);
}
 
/**
 * @param {number} ms
 * @param {luxon.DurationUnit[]} [units]
 * @param {Partial<luxon.DurationOptions & luxon.ToHumanDurationOptions> &
 *  { hideNulls: boolean }} [options]
 * @return {string}
 * @details
 *  - hideNulls: hide null units in the returned string if true (default: true)
 */
export function durationFilter(ms, units = [ "milliseconds" ], options) {
  if (!isInteger(ms) || ms < 0) { return ""; }
 
  const opts = assign({ hideNulls: true }, options);
 
  const values = Duration.fromMillis(ms, opts).shiftTo(...units).toObject();
  return Duration.fromObject(
    opts.hideNulls ? pickBy(values, (v) => (v > 0)) : values)
  .toHuman(opts);
}
 
export default {
  /**
   * @param {Vue.VueConstructor<Vue>} Vue
   */
  install(Vue) {
    Vue.filter("b-byteSize", byteSize);
    Vue.filter("b-moment", momentFilter);
    Vue.filter("b-luxon", momentFilter);
    Vue.filter("b-dateTime", dateTimeFilter);
    Vue.filter("b-duration", durationFilter);
    Vue.filter("b-relativeDateTime", relativeDateTimeFilter);
    Vue.filter("b-bigNumber", bigNumber);
  }
};