All files / src utils.js

76.47% Statements 13/17
50% Branches 4/8
66.66% Functions 6/9
76.47% Lines 13/17

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        1x           38x       1x           2x                                 5x   5x         1x                                             96x 37x     59x       1x                   12x                       14x  
// @ts-check
 
import { get, isEmpty, isNil, random, replace } from "lodash";
import { DateTime } from "luxon";
let id = 0;
 
/**
 * @returns {string}
 */
export function genId() {
  return "b-" + (++id);
}
 
/** @type {string} */
var _currentUrl = window.location.origin + window.location.pathname;
 
/**
 * @return {string}
 */
export function currentUrl() {
  return _currentUrl;
}
 
/**
 * @param {string} url
 */
export function setCurrentUrl(url) {
  _currentUrl = url;
}
 
/**
 * @brief get webpack public path ('/dist')
 * @param  {string} url
 * @return {string}
 */
export function publicPath(url) {
  /* eslint-disable-next-line camelcase */ /* @ts-ignore */
  Eif (typeof __webpack_public_path__ !== "undefined") {
    /* eslint-disable-next-line camelcase */ /* @ts-ignore */
    return __webpack_public_path__ + url;
  }
  return url;
}
 
const templateRegex = /{([^\}]*)}/g;
/**
 * @brief compile a string template
 * @details can't use "template" from lodash
 * (with { interpolate: /{([^\}]*)}/g }), since it uses eval.
 * @details uses "{word}" for parts to be replaced
 * @param  {string} source format string
 * @param  {{ [key:string]: any }} repl replacement object
 * @param  {boolean} keep whereas to keep unmatched braces
 * @return {string}
 */
export function format(source, repl, keep = false) {
  return replace(source, templateRegex, function(match, key) {
    return get(repl, key, keep ? match : "");
  });
}
 
/**
 * @param {number} timestamp
 * @param {luxon.DateTimeOptions} [options]
 * @return {DateTime}
 */
export function getDateTime(timestamp, options = undefined) {
  if (timestamp > 32503676400) { /* year 3000 --> ms timestamp */
    return DateTime.fromMillis(timestamp, options);
  }
  else {
    return DateTime.fromSeconds(timestamp, options);
  }
}
 
export const utils = {
  get, isEmpty, isNil, currentUrl, setCurrentUrl, publicPath, format, random,
  getDateTime
};
 
/**
 * @param {Vue.VueConstructor<any>} Vue
 */
function installUtils(Vue) {
  // @ts-ignore
  Vue.prototype.$butils = utils;
}
 
export default {
  install: installUtils
};
 
/**
 * @brief function used to automatically and correctly infer types in mixins
 * @template V, Data, Methods, Computed, PropNames, SetupBindings, Mixin, Extends
 * @type {V.mixinMaker<V, Data, Methods, Computed, PropNames, SetupBindings, Mixin, Extends>}}
 */
export function mixinMaker(m) { return /** @type {any} */ (m); }