"use strict";

require("core-js/modules/es.string.replace");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRandomIntInclusive = getRandomIntInclusive;
exports.versionCompare = versionCompare;
exports.default = exports.arrayShuffle = exports.arrayFlatten = exports.arrayRemove = exports.toHHMMSS = void 0;

// jshint -W116
const toHHMMSS = function (v, includeHour) {
  if (v === null || v === undefined || Number.isNaN(v)) return includeHour ? '--:--:--' : '--:--';
  let seconds = Math.floor(v),
      hours = Math.floor(seconds / 3600);
  seconds -= hours * 3600;
  let minutes = Math.floor(seconds / 60);
  seconds -= minutes * 60;

  if (hours < 10) {
    hours = '0' + hours;
  }

  if (minutes < 10 && includeHour) {
    minutes = '0' + minutes;
  }

  if (seconds < 10) {
    seconds = '0' + seconds;
  }

  let rv = '';
  if (includeHour && hours > 0) rv += hours + ':';else if (!includeHour) minutes = minutes + hours * 60;
  return rv + minutes + ':' + seconds;
};

exports.toHHMMSS = toHHMMSS;

const arrayRemove = function (arr, item) {
  let index = arr.indexOf(item);
  if (index >= 0) arr.splice(index, 1);
  return this;
};
/*eslint no-extend-native: "off" */


exports.arrayRemove = arrayRemove;

Array.prototype.flatten = function () {
  let arr = this;
  return arr.reduce(function (flat, toFlatten) {
    return flat.concat(Array.isArray(toFlatten) ? toFlatten.flatten() : toFlatten);
  }, []);
};

Array.prototype.unique = function (stripEmpty) {
  let a = [];

  for (let i = 0, l = this.length; i < l; i++) {
    if (stripEmpty && (this[i] === null || this[i] === undefined)) continue;

    if (a.indexOf(this[i]) === -1) {
      a.push(this[i]);
    }
  }

  return a;
};

const arrayFlatten = function (arr) {
  return arr.reduce(function (flat, toFlatten) {
    return flat.concat(Array.isArray(toFlatten) ? toFlatten.flatten() : toFlatten);
  }, []);
};

exports.arrayFlatten = arrayFlatten;

const arrayShuffle = function (array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    let temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }

  return array;
}; // string stuff from the old - TODO:[JS] get rid of prototype overrides...
// from http://snipplr.com/view.php?codeview&id=30964


exports.arrayShuffle = arrayShuffle;

String.prototype.hexDecode = function () {
  let r = '';

  for (let i = 0; i < this.length; i += 2) {
    r += unescape('%' + this.substr(i, 2));
  }

  return r;
};

String.prototype.hexEncode = function () {
  let r = '';
  let i = 0;
  let h;

  while (i < this.length) {
    h = this.charCodeAt(i++).toString(16);

    while (h.length < 2) {
      /* h = h ? */
    }

    r += h;
  }

  return r;
};

String.prototype.stripLinks = function () {
  let txt = this;
  txt = txt.replace(/<a\b[^>]*>/gi, '');
  txt = txt.replace(/<\/a>/gi, '');
  return txt;
};

if (!String.prototype.startsWith) {
  String.prototype.startsWith = function (searchString, position) {
    position = position || 0;
    return this.substr(position, searchString.length) === searchString;
  };
}

String.prototype.format = String.prototype.f = function () {
  let s = this,
      i = arguments.length;

  while (i--) {
    s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
  }

  return s;
};

String.prototype.f = String.prototype.format;

String.prototype.sentences = function () {
  let str = this || ''; // from http://stackoverflow.com/questions/18914629/split-string-into-sentences-in-javascript

  let rv1 = str.replace(/([.?!])\s*(?=[A-Z])/g, '$1|').split('|');
  let rv2 = []; // abreviations can f this up, so clean it a bit.

  for (let i = 0; i < rv1.length; ++i) {
    let s = '';

    while (s.length < 20 && i < rv1.length) {
      s += rv1[i];
      if (s.length < 20) ++i;
    }

    s.replace('. .', '.');
    s = s.trim();
    if (s.length) rv2.push(s);
  }

  return rv2;
}; // Returns a random integer between min (included) and max (included)
// Using Math.round() will give you a non-uniform distribution!


function getRandomIntInclusive(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function versionCompare(v1, v2, options) {
  let lexicographical = options && options.lexicographical,
      zeroExtend = options && options.zeroExtend,
      v1parts = v1.split('.'),
      v2parts = v2.split('.');

  function isValidPart(x) {
    return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
  }

  if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
    return NaN;
  }

  if (zeroExtend) {
    while (v1parts.length < v2parts.length) v1parts.push('0');

    while (v2parts.length < v1parts.length) v2parts.push('0');
  }

  if (!lexicographical) {
    v1parts = v1parts.map(Number);
    v2parts = v2parts.map(Number);
  }

  for (let i = 0; i < v1parts.length; ++i) {
    if (v2parts.length === i) {
      return 1;
    }

    if (v1parts[i] === v2parts[i]) {
      continue;
    } else if (v1parts[i] > v2parts[i]) {
      return 1;
    } else {
      return -1;
    }
  }

  if (v1parts.length !== v2parts.length) {
    return -1;
  }

  return 0;
}

var _default = {
  versionCompare,
  getRandomIntInclusive,
  arrayShuffle,
  toHHMMSS,
  arrayRemove,
  arrayFlatten
};
exports.default = _default;