mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 11:19:14 +08:00
71 lines
13 KiB
Diff
71 lines
13 KiB
Diff
diff --git a/esm/plugin/duration/index.js b/esm/plugin/duration/index.js
|
|
index a241d4b202e99c61467639a5756c586e0e50ceb7..9896d06941a0340fcde49641dfc8cb517d4ec400 100644
|
|
--- a/esm/plugin/duration/index.js
|
|
+++ b/esm/plugin/duration/index.js
|
|
@@ -1,6 +1,6 @@
|
|
import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK, REGEX_FORMAT } from '../../constant';
|
|
var MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365;
|
|
-var MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12;
|
|
+var MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30;
|
|
var durationRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/;
|
|
var unitToMS = {
|
|
years: MILLISECONDS_A_YEAR,
|
|
@@ -159,7 +159,6 @@ var Duration = /*#__PURE__*/function () {
|
|
|
|
if (this.$d.milliseconds) {
|
|
seconds += this.$d.milliseconds / 1000;
|
|
- seconds = Math.round(seconds * 1000) / 1000;
|
|
}
|
|
|
|
var S = getNumberUnitFormat(seconds, 'S');
|
|
@@ -213,7 +212,7 @@ var Duration = /*#__PURE__*/function () {
|
|
base = this.$d[pUnit];
|
|
}
|
|
|
|
- return base || 0; // a === 0 will be true on both 0 and -0
|
|
+ return base === 0 ? 0 : base; // a === 0 will be true on both 0 and -0
|
|
};
|
|
|
|
_proto.add = function add(input, unit, isSubtract) {
|
|
@@ -319,10 +318,6 @@ var Duration = /*#__PURE__*/function () {
|
|
return Duration;
|
|
}();
|
|
|
|
-var manipulateDuration = function manipulateDuration(date, duration, k) {
|
|
- return date.add(duration.years() * k, 'y').add(duration.months() * k, 'M').add(duration.days() * k, 'd').add(duration.hours() * k, 'h').add(duration.minutes() * k, 'm').add(duration.seconds() * k, 's').add(duration.milliseconds() * k, 'ms');
|
|
-};
|
|
-
|
|
export default (function (option, Dayjs, dayjs) {
|
|
$d = dayjs;
|
|
$u = dayjs().$utils();
|
|
@@ -339,18 +334,12 @@ export default (function (option, Dayjs, dayjs) {
|
|
var oldSubtract = Dayjs.prototype.subtract;
|
|
|
|
Dayjs.prototype.add = function (value, unit) {
|
|
- if (isDuration(value)) {
|
|
- return manipulateDuration(this, value, 1);
|
|
- }
|
|
-
|
|
+ if (isDuration(value)) value = value.asMilliseconds();
|
|
return oldAdd.bind(this)(value, unit);
|
|
};
|
|
|
|
Dayjs.prototype.subtract = function (value, unit) {
|
|
- if (isDuration(value)) {
|
|
- return manipulateDuration(this, value, -1);
|
|
- }
|
|
-
|
|
+ if (isDuration(value)) value = value.asMilliseconds();
|
|
return oldSubtract.bind(this)(value, unit);
|
|
};
|
|
});
|
|
\ No newline at end of file
|
|
diff --git a/plugin/duration.js b/plugin/duration.js
|
|
index 4578f0644c004c2ee0721e3c9f4543a6dbfbeee7..02992b29ebd4bd347b2903e8269358cb05ae70e0 100644
|
|
--- a/plugin/duration.js
|
|
+++ b/plugin/duration.js
|
|
@@ -1 +1 @@
|
|
-!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):(t="undefined"!=typeof globalThis?globalThis:t||self).dayjs_plugin_duration=s()}(this,(function(){"use strict";var t,s,n=1e3,i=6e4,e=36e5,r=864e5,o=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,u=31536e6,d=2628e6,a=/^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/,h={years:u,months:d,days:r,hours:e,minutes:i,seconds:n,milliseconds:1,weeks:6048e5},c=function(t){return t instanceof g},f=function(t,s,n){return new g(t,n,s.$l)},m=function(t){return s.p(t)+"s"},l=function(t){return t<0},$=function(t){return l(t)?Math.ceil(t):Math.floor(t)},y=function(t){return Math.abs(t)},v=function(t,s){return t?l(t)?{negative:!0,format:""+y(t)+s}:{negative:!1,format:""+t+s}:{negative:!1,format:""}},g=function(){function l(t,s,n){var i=this;if(this.$d={},this.$l=n,void 0===t&&(this.$ms=0,this.parseFromMilliseconds()),s)return f(t*h[m(s)],this);if("number"==typeof t)return this.$ms=t,this.parseFromMilliseconds(),this;if("object"==typeof t)return Object.keys(t).forEach((function(s){i.$d[m(s)]=t[s]})),this.calMilliseconds(),this;if("string"==typeof t){var e=t.match(a);if(e){var r=e.slice(2).map((function(t){return null!=t?Number(t):0}));return this.$d.years=r[0],this.$d.months=r[1],this.$d.weeks=r[2],this.$d.days=r[3],this.$d.hours=r[4],this.$d.minutes=r[5],this.$d.seconds=r[6],this.calMilliseconds(),this}}return this}var y=l.prototype;return y.calMilliseconds=function(){var t=this;this.$ms=Object.keys(this.$d).reduce((function(s,n){return s+(t.$d[n]||0)*h[n]}),0)},y.parseFromMilliseconds=function(){var t=this.$ms;this.$d.years=$(t/u),t%=u,this.$d.months=$(t/d),t%=d,this.$d.days=$(t/r),t%=r,this.$d.hours=$(t/e),t%=e,this.$d.minutes=$(t/i),t%=i,this.$d.seconds=$(t/n),t%=n,this.$d.milliseconds=t},y.toISOString=function(){var t=v(this.$d.years,"Y"),s=v(this.$d.months,"M"),n=+this.$d.days||0;this.$d.weeks&&(n+=7*this.$d.weeks);var i=v(n,"D"),e=v(this.$d.hours,"H"),r=v(this.$d.minutes,"M"),o=this.$d.seconds||0;this.$d.milliseconds&&(o+=this.$d.milliseconds/1e3,o=Math.round(1e3*o)/1e3);var u=v(o,"S"),d=t.negative||s.negative||i.negative||e.negative||r.negative||u.negative,a=e.format||r.format||u.format?"T":"",h=(d?"-":"")+"P"+t.format+s.format+i.format+a+e.format+r.format+u.format;return"P"===h||"-P"===h?"P0D":h},y.toJSON=function(){return this.toISOString()},y.format=function(t){var n=t||"YYYY-MM-DDTHH:mm:ss",i={Y:this.$d.years,YY:s.s(this.$d.years,2,"0"),YYYY:s.s(this.$d.years,4,"0"),M:this.$d.months,MM:s.s(this.$d.months,2,"0"),D:this.$d.days,DD:s.s(this.$d.days,2,"0"),H:this.$d.hours,HH:s.s(this.$d.hours,2,"0"),m:this.$d.minutes,mm:s.s(this.$d.minutes,2,"0"),s:this.$d.seconds,ss:s.s(this.$d.seconds,2,"0"),SSS:s.s(this.$d.milliseconds,3,"0")};return n.replace(o,(function(t,s){return s||String(i[t])}))},y.as=function(t){return this.$ms/h[m(t)]},y.get=function(t){var s=this.$ms,n=m(t);return"milliseconds"===n?s%=1e3:s="weeks"===n?$(s/h[n]):this.$d[n],s||0},y.add=function(t,s,n){var i;return i=s?t*h[m(s)]:c(t)?t.$ms:f(t,this).$ms,f(this.$ms+i*(n?-1:1),this)},y.subtract=function(t,s){return this.add(t,s,!0)},y.locale=function(t){var s=this.clone();return s.$l=t,s},y.clone=function(){return f(this.$ms,this)},y.humanize=function(s){return t().add(this.$ms,"ms").locale(this.$l).fromNow(!s)},y.valueOf=function(){return this.asMilliseconds()},y.milliseconds=function(){return this.get("milliseconds")},y.asMilliseconds=function(){return this.as("milliseconds")},y.seconds=function(){return this.get("seconds")},y.asSeconds=function(){return this.as("seconds")},y.minutes=function(){return this.get("minutes")},y.asMinutes=function(){return this.as("minutes")},y.hours=function(){return this.get("hours")},y.asHours=function(){return this.as("hours")},y.days=function(){return this.get("days")},y.asDays=function(){return this.as("days")},y.weeks=function(){return this.get("weeks")},y.asWeeks=function(){return this.as("weeks")},y.months=function(){return this.get("months")},y.asMonths=function(){return this.as("months")},y.years=function(){return this.get("years")},y.asYears=function(){return this.as("years")},l}(),p=function(t,s,n){return t.add(s.years()*n,"y").add(s.months()*n,"M").add(s.days()*n,"d").add(s.hours()*n,"h").add(s.minutes()*n,"m").add(s.seconds()*n,"s").add(s.milliseconds()*n,"ms")};return function(n,i,e){t=e,s=e().$utils(),e.duration=function(t,s){var n=e.locale();return f(t,{$l:n},s)},e.isDuration=c;var r=i.prototype.add,o=i.prototype.subtract;i.prototype.add=function(t,s){return c(t)?p(this,t,1):r.bind(this)(t,s)},i.prototype.subtract=function(t,s){return c(t)?p(this,t,-1):o.bind(this)(t,s)}}}));
|
|
\ No newline at end of file
|
|
+!function (t, s) { "object" == typeof exports && "undefined" != typeof module ? module.exports = s() : "function" == typeof define && define.amd ? define(s) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs_plugin_duration = s() }(this, (function () { "use strict"; var t, s, n = 1e3, i = 6e4, e = 36e5, r = 864e5, o = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, u = 31536e6, h = 2592e6, a = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/, d = { years: u, months: h, days: r, hours: e, minutes: i, seconds: n, milliseconds: 1, weeks: 6048e5 }, c = function (t) { return t instanceof g }, f = function (t, s, n) { return new g(t, n, s.$l) }, l = function (t) { return s.p(t) + "s" }, m = function (t) { return t < 0 }, $ = function (t) { return m(t) ? Math.ceil(t) : Math.floor(t) }, y = function (t) { return Math.abs(t) }, v = function (t, s) { return t ? m(t) ? { negative: !0, format: "" + y(t) + s } : { negative: !1, format: "" + t + s } : { negative: !1, format: "" } }, g = function () { function m(t, s, n) { var i = this; if (this.$d = {}, this.$l = n, void 0 === t && (this.$ms = 0, this.parseFromMilliseconds()), s) return f(t * d[l(s)], this); if ("number" == typeof t) return this.$ms = t, this.parseFromMilliseconds(), this; if ("object" == typeof t) return Object.keys(t).forEach((function (s) { i.$d[l(s)] = t[s] })), this.calMilliseconds(), this; if ("string" == typeof t) { var e = t.match(a); if (e) { var r = e.slice(2).map((function (t) { return null != t ? Number(t) : 0 })); return this.$d.years = r[0], this.$d.months = r[1], this.$d.weeks = r[2], this.$d.days = r[3], this.$d.hours = r[4], this.$d.minutes = r[5], this.$d.seconds = r[6], this.calMilliseconds(), this } } return this } var y = m.prototype; return y.calMilliseconds = function () { var t = this; this.$ms = Object.keys(this.$d).reduce((function (s, n) { return s + (t.$d[n] || 0) * d[n] }), 0) }, y.parseFromMilliseconds = function () { var t = this.$ms; this.$d.years = $(t / u), t %= u, this.$d.months = $(t / h), t %= h, this.$d.days = $(t / r), t %= r, this.$d.hours = $(t / e), t %= e, this.$d.minutes = $(t / i), t %= i, this.$d.seconds = $(t / n), t %= n, this.$d.milliseconds = t }, y.toISOString = function () { var t = v(this.$d.years, "Y"), s = v(this.$d.months, "M"), n = +this.$d.days || 0; this.$d.weeks && (n += 7 * this.$d.weeks); var i = v(n, "D"), e = v(this.$d.hours, "H"), r = v(this.$d.minutes, "M"), o = this.$d.seconds || 0; this.$d.milliseconds && (o += this.$d.milliseconds / 1e3); var u = v(o, "S"), h = t.negative || s.negative || i.negative || e.negative || r.negative || u.negative, a = e.format || r.format || u.format ? "T" : "", d = (h ? "-" : "") + "P" + t.format + s.format + i.format + a + e.format + r.format + u.format; return "P" === d || "-P" === d ? "P0D" : d }, y.toJSON = function () { return this.toISOString() }, y.format = function (t) { var n = t || "YYYY-MM-DDTHH:mm:ss", i = { Y: this.$d.years, YY: s.s(this.$d.years, 2, "0"), YYYY: s.s(this.$d.years, 4, "0"), M: this.$d.months, MM: s.s(this.$d.months, 2, "0"), D: this.$d.days, DD: s.s(this.$d.days, 2, "0"), H: this.$d.hours, HH: s.s(this.$d.hours, 2, "0"), m: this.$d.minutes, mm: s.s(this.$d.minutes, 2, "0"), s: this.$d.seconds, ss: s.s(this.$d.seconds, 2, "0"), SSS: s.s(this.$d.milliseconds, 3, "0") }; return n.replace(o, (function (t, s) { return s || String(i[t]) })) }, y.as = function (t) { return this.$ms / d[l(t)] }, y.get = function (t) { var s = this.$ms, n = l(t); return "milliseconds" === n ? s %= 1e3 : s = "weeks" === n ? $(s / d[n]) : this.$d[n], 0 === s ? 0 : s }, y.add = function (t, s, n) { var i; return i = s ? t * d[l(s)] : c(t) ? t.$ms : f(t, this).$ms, f(this.$ms + i * (n ? -1 : 1), this) }, y.subtract = function (t, s) { return this.add(t, s, !0) }, y.locale = function (t) { var s = this.clone(); return s.$l = t, s }, y.clone = function () { return f(this.$ms, this) }, y.humanize = function (s) { return t().add(this.$ms, "ms").locale(this.$l).fromNow(!s) }, y.valueOf = function () { return this.asMilliseconds() }, y.milliseconds = function () { return this.get("milliseconds") }, y.asMilliseconds = function () { return this.as("milliseconds") }, y.seconds = function () { return this.get("seconds") }, y.asSeconds = function () { return this.as("seconds") }, y.minutes = function () { return this.get("minutes") }, y.asMinutes = function () { return this.as("minutes") }, y.hours = function () { return this.get("hours") }, y.asHours = function () { return this.as("hours") }, y.days = function () { return this.get("days") }, y.asDays = function () { return this.as("days") }, y.weeks = function () { return this.get("weeks") }, y.asWeeks = function () { return this.as("weeks") }, y.months = function () { return this.get("months") }, y.asMonths = function () { return this.as("months") }, y.years = function () { return this.get("years") }, y.asYears = function () { return this.as("years") }, m }(); return function (n, i, e) { t = e, s = e().$utils(), e.duration = function (t, s) { var n = e.locale(); return f(t, { $l: n }, s) }, e.isDuration = c; var r = i.prototype.add, o = i.prototype.subtract; i.prototype.add = function (t, s) { return c(t) && (t = t.asMilliseconds()), r.bind(this)(t, s) }, i.prototype.subtract = function (t, s) { return c(t) && (t = t.asMilliseconds()), o.bind(this)(t, s) } } }));
|