{"version":3,"file":"datetime.util.mjs","sources":["../../src/lib/utils/datetime.util.js"],"sourcesContent":["/*\nCopyright 2019 Iguazio Systems Ltd.\n\nLicensed under the Apache License, Version 2.0 (the \"License\") with\nan addition restriction as set forth herein. You may not use this\nfile except in compliance with the License. You may obtain a copy of\nthe License at http://www.apache.org/licenses/LICENSE-2.0.\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\nimplied. See the License for the specific language governing\npermissions and limitations under the License.\n\nIn addition, you may not use the software for any purposes that are\nillegal under applicable law, and the grant of the foregoing license\nunder the Apache 2.0 license is conditioned upon your compliance with\nsuch restriction.\n*/\nimport moment from 'moment'\n\nexport function getSupportedLocale() {\n  const SUPPORTED_LOCALES = new Set(['en-GB', 'en-US'])\n\n  const userLocales = navigator.languages || [navigator.language]\n\n  // browser always returns locales in descending order of preference\n  const match = userLocales.find(locale => SUPPORTED_LOCALES.has(locale))\n\n  return match || 'en-US'\n}\n\nexport const supportedLocale = getSupportedLocale()\n\nconst normalizeDatetimeString = datetime => {\n  if (\n    typeof datetime === 'string' &&\n    /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}/.test(datetime) &&\n    !/Z$|[+-]\\d{2}:?\\d{2}$/.test(datetime)\n  ) {\n    return datetime + 'Z'\n  }\n\n  return datetime\n}\n\nexport const formatDatetime = (\n  datetime,\n  invalidDateMessage,\n  options = {\n    year: 'numeric',\n    month: 'short',\n    day: 'numeric',\n    hour: '2-digit',\n    minute: '2-digit',\n    second: '2-digit'\n  },\n  locale = supportedLocale\n) => {\n  if (!datetime) {\n    return invalidDateMessage\n  }\n\n  const date = new Date(normalizeDatetimeString(datetime))\n\n  if (isNaN(date)) {\n    return invalidDateMessage\n  }\n\n  return typeof date !== 'object' || !(date instanceof Date) || isNaN(date)\n    ? invalidDateMessage\n    : new Intl.DateTimeFormat(locale, {\n        numberingSystem: 'latn',\n        calendar: 'gregory',\n        ...options\n      }).format(date)\n}\n\nexport const getFormatTime = time => {\n  const [hour, minute] = time.split(':')\n  if (!minute) {\n    return {\n      hour: '0',\n      minute: '0'\n    }\n  }\n  return {\n    hour: hour.replace(/_/g, '0'),\n    minute: minute.replace(/_/g, '0')\n  }\n}\n\nexport const getTimeElapsedByDate = creationDate => {\n  moment.updateLocale('en', {\n    relativeTime: {\n      future: 'in %s',\n      past: '%s ago',\n      s: 'a few seconds',\n      ss: '%d seconds',\n      m: 'a minute',\n      mm: '%d minutes',\n      h: 'an hour',\n      hh: '%d hours',\n      d: 'a day',\n      dd: '%d days',\n      w: 'a week',\n      ww: '%d weeks',\n      M: 'a month',\n      MM: '%d months',\n      y: 'a year',\n      yy: '%d years'\n    }\n  })\n\n  const time = moment.utc(creationDate)\n\n  return time.fromNow()\n}\n\nexport const getDateAndTimeByFormat = (date, dateFormat) => {\n  return moment(date).format(dateFormat)\n}\n\nexport const sortListByDate = (list = [], field, isAscending = true) => {\n  return [...list].sort((prevElem, nextElem) => {\n    const prev = Date.parse(prevElem[field])\n    const next = Date.parse(nextElem[field])\n\n    return isAscending ? prev - next : next - prev\n  })\n}\n"],"names":["getSupportedLocale","SUPPORTED_LOCALES","locale","supportedLocale","normalizeDatetimeString","datetime","formatDatetime","invalidDateMessage","options","date","getFormatTime","time","hour","minute","getTimeElapsedByDate","creationDate","moment","getDateAndTimeByFormat","dateFormat","sortListByDate","list","field","isAscending","prevElem","nextElem","prev","next"],"mappings":";AAqBO,SAASA,IAAqB;AACnC,QAAMC,IAAoB,oBAAI,IAAI,CAAC,SAAS,OAAO,CAAC;AAOpD,UALoB,UAAU,aAAa,CAAC,UAAU,QAAQ,GAGpC,KAAK,CAAAC,MAAUD,EAAkB,IAAIC,CAAM,CAAC,KAEtD;AAClB;AAEY,MAACC,IAAkBH,EAAkB,GAE3CI,IAA0B,CAAAC,MAE5B,OAAOA,KAAa,YACpB,iCAAiC,KAAKA,CAAQ,KAC9C,CAAC,u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