From 81e0023c17d648d182e991f2fc25691928fc33d2 Mon Sep 17 00:00:00 2001 From: Ou Date: Wed, 9 Oct 2024 15:29:39 +0800 Subject: [PATCH] fix: dayjs duration --- package.json | 9 +++++- patches/dayjs.patch | 70 +++++++++++++++++++++++++++++++++++++++++++++ pnpm-lock.yaml | 20 +++++++++++-- 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 patches/dayjs.patch diff --git a/package.json b/package.json index 5eabee2..664a96d 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "cheerio": "^1.0.0", "clsx": "^2.1.1", "consola": "^3.2.3", - "dayjs": "^1.11.13", + "dayjs": "1.11.13", "db0": "npm:@ourongxing/db0@latest", "fast-xml-parser": "^4.5.0", "favicons-scraper": "^1.3.2", @@ -62,6 +62,7 @@ "eslint-plugin-react-refresh": "^0.4.12", "lint-staged": "^15.2.10", "mlly": "^1.7.2", + "mockdate": "^3.0.5", "nitropack": "^2.9.7", "simple-git-hooks": "^2.11.1", "tsx": "^4.19.1", @@ -75,7 +76,13 @@ "vitest": "^2.1.1", "wrangler": "^3.79.0" }, + "pnpm": { + "patchedDependencies": { + "dayjs": "patches/dayjs.patch" + } + }, "resolutions": { + "dayjs": "1.11.13", "db0": "npm:@ourongxing/db0@latest" }, "simple-git-hooks": { diff --git a/patches/dayjs.patch b/patches/dayjs.patch new file mode 100644 index 0000000..384923b --- /dev/null +++ b/patches/dayjs.patch @@ -0,0 +1,70 @@ +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) } } })); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 68c3a1e..e35be4a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,8 +5,14 @@ settings: excludeLinksFromLockfile: false overrides: + dayjs: 1.11.13 db0: npm:@ourongxing/db0@latest +patchedDependencies: + dayjs: + hash: vxjypqxmsykboavgqknf3tdbfa + path: patches/dayjs.patch + importers: .: @@ -45,8 +51,8 @@ importers: specifier: ^3.2.3 version: 3.2.3 dayjs: - specifier: ^1.11.13 - version: 1.11.13 + specifier: 1.11.13 + version: 1.11.13(patch_hash=vxjypqxmsykboavgqknf3tdbfa) db0: specifier: npm:@ourongxing/db0@latest version: '@ourongxing/db0@0.1.5(libsql@0.4.5)' @@ -141,6 +147,9 @@ importers: mlly: specifier: ^1.7.2 version: 1.7.2 + mockdate: + specifier: ^3.0.5 + version: 3.0.5 nitropack: specifier: ^2.9.7 version: 2.9.7(encoding@0.1.13)(libsql@0.4.5)(magicast@0.3.5) @@ -3615,6 +3624,9 @@ packages: mlly@1.7.2: resolution: {integrity: sha512-tN3dvVHYVz4DhSXinXIk7u9syPYaJvio118uomkovAtWBT+RdbP6Lfh/5Lvo519YMmwBafwlh20IPTXIStscpA==} + mockdate@3.0.5: + resolution: {integrity: sha512-iniQP4rj1FhBdBYS/+eQv7j1tadJ9lJtdzgOpvsOHng/GbcDh2Fhdeq+ZRldrPYdXvCyfFUmFeEwEGXZB5I/AQ==} + mri@1.2.0: resolution: {integrity: sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==} engines: {node: '>=4'} @@ -6965,7 +6977,7 @@ snapshots: data-uri-to-buffer@2.0.2: {} - dayjs@1.11.13: {} + dayjs@1.11.13(patch_hash=vxjypqxmsykboavgqknf3tdbfa): {} debug@2.6.9: dependencies: @@ -8394,6 +8406,8 @@ snapshots: pkg-types: 1.2.0 ufo: 1.5.4 + mockdate@3.0.5: {} + mri@1.2.0: {} mrmime@2.0.0: {}