UNPKG

persian-vue-timeago

Version:

persian local timeago

112 lines (96 loc) 3.55 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.VueTimeago = factory()); }(this, function () { 'use strict'; var MINUTE = 60; var HOUR = MINUTE * 60; var DAY = HOUR * 24; var WEEK = DAY * 7; var MONTH = DAY * 30; var YEAR = DAY * 365; function pluralOrSingular(data, locale) { if (data === 'just now') { return locale; } var count = Math.round(data); if (Array.isArray(locale)) { return count > 1 ? locale[1].replace(/%s/, count) : locale[0].replace(/%s/, count); } return locale.replace(/%s/, count); } function formatTime(time) { var d = new Date(time); return d.toLocaleString(); } function install(Vue) { var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; var _ref$name = _ref.name; var name = _ref$name === undefined ? 'timeago' : _ref$name; var _ref$locale = _ref.locale; var locale = _ref$locale === undefined ? 'en-US' : _ref$locale; var _ref$locales = _ref.locales; var locales = _ref$locales === undefined ? null : _ref$locales; if (!locales || Object.keys(locales).length === 0) { throw new TypeError('Expected locales to have at lease one locale.'); } var VueTimeago = { props: { since: { required: true, coerce: function coerce(val) { return new Date(val).getTime(); } }, locale: String, maxTime: Number, autoUpdate: Number, format: Function }, template: '<span v-text="timeago"></span>', data: function data() { return { now: new Date().getTime() }; }, computed: { currentLocale: function currentLocale() { var current = locales[this.locale || locale]; if (!current) { return locales[locale]; } return current; }, timeago: function timeago() { var seconds = this.now / 1000 - this.since / 1000; if (this.maxTime && seconds > this.maxTime) { clearInterval(this.interval); return this.format ? this.format(this.since) : formatTime(this.since); } var ret = seconds <= 5 ? pluralOrSingular('just now', this.currentLocale[0]) : seconds < MINUTE ? pluralOrSingular(seconds, this.currentLocale[1]) : seconds < HOUR ? pluralOrSingular(seconds / MINUTE, this.currentLocale[2]) : seconds < DAY ? pluralOrSingular(seconds / HOUR, this.currentLocale[3]) : seconds < WEEK ? pluralOrSingular(seconds / DAY, this.currentLocale[4]) : seconds < MONTH ? pluralOrSingular(seconds / WEEK, this.currentLocale[5]) : seconds < YEAR ? pluralOrSingular(seconds / MONTH, this.currentLocale[6]) : pluralOrSingular(seconds / YEAR, this.currentLocale[7]); return ret; } }, ready: function ready() { if (this.autoUpdate) { this.update(); } }, methods: { update: function update() { var _this = this; var period = this.autoUpdate * 1000; this.interval = setInterval(function () { _this.now = new Date().getTime(); }, period); } }, beforeDestroy: function beforeDestroy() { clearInterval(this.interval); this.interval = null; } }; Vue.component(name, VueTimeago); } return install; }));