index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK, REGEX_FORMAT } from '../../constant';
  2. var MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365;
  3. var MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12;
  4. 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)?)?$/;
  5. var unitToMS = {
  6. years: MILLISECONDS_A_YEAR,
  7. months: MILLISECONDS_A_MONTH,
  8. days: MILLISECONDS_A_DAY,
  9. hours: MILLISECONDS_A_HOUR,
  10. minutes: MILLISECONDS_A_MINUTE,
  11. seconds: MILLISECONDS_A_SECOND,
  12. milliseconds: 1,
  13. weeks: MILLISECONDS_A_WEEK
  14. };
  15. var isDuration = function isDuration(d) {
  16. return d instanceof Duration;
  17. }; // eslint-disable-line no-use-before-define
  18. var $d;
  19. var $u;
  20. var wrapper = function wrapper(input, instance, unit) {
  21. return new Duration(input, unit, instance.$l);
  22. }; // eslint-disable-line no-use-before-define
  23. var prettyUnit = function prettyUnit(unit) {
  24. return $u.p(unit) + "s";
  25. };
  26. var isNegative = function isNegative(number) {
  27. return number < 0;
  28. };
  29. var roundNumber = function roundNumber(number) {
  30. return isNegative(number) ? Math.ceil(number) : Math.floor(number);
  31. };
  32. var absolute = function absolute(number) {
  33. return Math.abs(number);
  34. };
  35. var getNumberUnitFormat = function getNumberUnitFormat(number, unit) {
  36. if (!number) {
  37. return {
  38. negative: false,
  39. format: ''
  40. };
  41. }
  42. if (isNegative(number)) {
  43. return {
  44. negative: true,
  45. format: "" + absolute(number) + unit
  46. };
  47. }
  48. return {
  49. negative: false,
  50. format: "" + number + unit
  51. };
  52. };
  53. var Duration = /*#__PURE__*/function () {
  54. function Duration(input, unit, locale) {
  55. var _this = this;
  56. this.$d = {};
  57. this.$l = locale;
  58. if (input === undefined) {
  59. this.$ms = 0;
  60. this.parseFromMilliseconds();
  61. }
  62. if (unit) {
  63. return wrapper(input * unitToMS[prettyUnit(unit)], this);
  64. }
  65. if (typeof input === 'number') {
  66. this.$ms = input;
  67. this.parseFromMilliseconds();
  68. return this;
  69. }
  70. if (typeof input === 'object') {
  71. Object.keys(input).forEach(function (k) {
  72. _this.$d[prettyUnit(k)] = input[k];
  73. });
  74. this.calMilliseconds();
  75. return this;
  76. }
  77. if (typeof input === 'string') {
  78. var d = input.match(durationRegex);
  79. if (d) {
  80. var properties = d.slice(2);
  81. var numberD = properties.map(function (value) {
  82. return value != null ? Number(value) : 0;
  83. });
  84. this.$d.years = numberD[0];
  85. this.$d.months = numberD[1];
  86. this.$d.weeks = numberD[2];
  87. this.$d.days = numberD[3];
  88. this.$d.hours = numberD[4];
  89. this.$d.minutes = numberD[5];
  90. this.$d.seconds = numberD[6];
  91. this.calMilliseconds();
  92. return this;
  93. }
  94. }
  95. return this;
  96. }
  97. var _proto = Duration.prototype;
  98. _proto.calMilliseconds = function calMilliseconds() {
  99. var _this2 = this;
  100. this.$ms = Object.keys(this.$d).reduce(function (total, unit) {
  101. return total + (_this2.$d[unit] || 0) * unitToMS[unit];
  102. }, 0);
  103. };
  104. _proto.parseFromMilliseconds = function parseFromMilliseconds() {
  105. var $ms = this.$ms;
  106. this.$d.years = roundNumber($ms / MILLISECONDS_A_YEAR);
  107. $ms %= MILLISECONDS_A_YEAR;
  108. this.$d.months = roundNumber($ms / MILLISECONDS_A_MONTH);
  109. $ms %= MILLISECONDS_A_MONTH;
  110. this.$d.days = roundNumber($ms / MILLISECONDS_A_DAY);
  111. $ms %= MILLISECONDS_A_DAY;
  112. this.$d.hours = roundNumber($ms / MILLISECONDS_A_HOUR);
  113. $ms %= MILLISECONDS_A_HOUR;
  114. this.$d.minutes = roundNumber($ms / MILLISECONDS_A_MINUTE);
  115. $ms %= MILLISECONDS_A_MINUTE;
  116. this.$d.seconds = roundNumber($ms / MILLISECONDS_A_SECOND);
  117. $ms %= MILLISECONDS_A_SECOND;
  118. this.$d.milliseconds = $ms;
  119. };
  120. _proto.toISOString = function toISOString() {
  121. var Y = getNumberUnitFormat(this.$d.years, 'Y');
  122. var M = getNumberUnitFormat(this.$d.months, 'M');
  123. var days = +this.$d.days || 0;
  124. if (this.$d.weeks) {
  125. days += this.$d.weeks * 7;
  126. }
  127. var D = getNumberUnitFormat(days, 'D');
  128. var H = getNumberUnitFormat(this.$d.hours, 'H');
  129. var m = getNumberUnitFormat(this.$d.minutes, 'M');
  130. var seconds = this.$d.seconds || 0;
  131. if (this.$d.milliseconds) {
  132. seconds += this.$d.milliseconds / 1000;
  133. seconds = Math.round(seconds * 1000) / 1000;
  134. }
  135. var S = getNumberUnitFormat(seconds, 'S');
  136. var negativeMode = Y.negative || M.negative || D.negative || H.negative || m.negative || S.negative;
  137. var T = H.format || m.format || S.format ? 'T' : '';
  138. var P = negativeMode ? '-' : '';
  139. var result = P + "P" + Y.format + M.format + D.format + T + H.format + m.format + S.format;
  140. return result === 'P' || result === '-P' ? 'P0D' : result;
  141. };
  142. _proto.toJSON = function toJSON() {
  143. return this.toISOString();
  144. };
  145. _proto.format = function format(formatStr) {
  146. var str = formatStr || 'YYYY-MM-DDTHH:mm:ss';
  147. var matches = {
  148. Y: this.$d.years,
  149. YY: $u.s(this.$d.years, 2, '0'),
  150. YYYY: $u.s(this.$d.years, 4, '0'),
  151. M: this.$d.months,
  152. MM: $u.s(this.$d.months, 2, '0'),
  153. D: this.$d.days,
  154. DD: $u.s(this.$d.days, 2, '0'),
  155. H: this.$d.hours,
  156. HH: $u.s(this.$d.hours, 2, '0'),
  157. m: this.$d.minutes,
  158. mm: $u.s(this.$d.minutes, 2, '0'),
  159. s: this.$d.seconds,
  160. ss: $u.s(this.$d.seconds, 2, '0'),
  161. SSS: $u.s(this.$d.milliseconds, 3, '0')
  162. };
  163. return str.replace(REGEX_FORMAT, function (match, $1) {
  164. return $1 || String(matches[match]);
  165. });
  166. };
  167. _proto.as = function as(unit) {
  168. return this.$ms / unitToMS[prettyUnit(unit)];
  169. };
  170. _proto.get = function get(unit) {
  171. var base = this.$ms;
  172. var pUnit = prettyUnit(unit);
  173. if (pUnit === 'milliseconds') {
  174. base %= 1000;
  175. } else if (pUnit === 'weeks') {
  176. base = roundNumber(base / unitToMS[pUnit]);
  177. } else {
  178. base = this.$d[pUnit];
  179. }
  180. return base || 0; // a === 0 will be true on both 0 and -0
  181. };
  182. _proto.add = function add(input, unit, isSubtract) {
  183. var another;
  184. if (unit) {
  185. another = input * unitToMS[prettyUnit(unit)];
  186. } else if (isDuration(input)) {
  187. another = input.$ms;
  188. } else {
  189. another = wrapper(input, this).$ms;
  190. }
  191. return wrapper(this.$ms + another * (isSubtract ? -1 : 1), this);
  192. };
  193. _proto.subtract = function subtract(input, unit) {
  194. return this.add(input, unit, true);
  195. };
  196. _proto.locale = function locale(l) {
  197. var that = this.clone();
  198. that.$l = l;
  199. return that;
  200. };
  201. _proto.clone = function clone() {
  202. return wrapper(this.$ms, this);
  203. };
  204. _proto.humanize = function humanize(withSuffix) {
  205. return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix);
  206. };
  207. _proto.valueOf = function valueOf() {
  208. return this.asMilliseconds();
  209. };
  210. _proto.milliseconds = function milliseconds() {
  211. return this.get('milliseconds');
  212. };
  213. _proto.asMilliseconds = function asMilliseconds() {
  214. return this.as('milliseconds');
  215. };
  216. _proto.seconds = function seconds() {
  217. return this.get('seconds');
  218. };
  219. _proto.asSeconds = function asSeconds() {
  220. return this.as('seconds');
  221. };
  222. _proto.minutes = function minutes() {
  223. return this.get('minutes');
  224. };
  225. _proto.asMinutes = function asMinutes() {
  226. return this.as('minutes');
  227. };
  228. _proto.hours = function hours() {
  229. return this.get('hours');
  230. };
  231. _proto.asHours = function asHours() {
  232. return this.as('hours');
  233. };
  234. _proto.days = function days() {
  235. return this.get('days');
  236. };
  237. _proto.asDays = function asDays() {
  238. return this.as('days');
  239. };
  240. _proto.weeks = function weeks() {
  241. return this.get('weeks');
  242. };
  243. _proto.asWeeks = function asWeeks() {
  244. return this.as('weeks');
  245. };
  246. _proto.months = function months() {
  247. return this.get('months');
  248. };
  249. _proto.asMonths = function asMonths() {
  250. return this.as('months');
  251. };
  252. _proto.years = function years() {
  253. return this.get('years');
  254. };
  255. _proto.asYears = function asYears() {
  256. return this.as('years');
  257. };
  258. return Duration;
  259. }();
  260. var manipulateDuration = function manipulateDuration(date, duration, k) {
  261. 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');
  262. };
  263. export default (function (option, Dayjs, dayjs) {
  264. $d = dayjs;
  265. $u = dayjs().$utils();
  266. dayjs.duration = function (input, unit) {
  267. var $l = dayjs.locale();
  268. return wrapper(input, {
  269. $l: $l
  270. }, unit);
  271. };
  272. dayjs.isDuration = isDuration;
  273. var oldAdd = Dayjs.prototype.add;
  274. var oldSubtract = Dayjs.prototype.subtract;
  275. Dayjs.prototype.add = function (value, unit) {
  276. if (isDuration(value)) {
  277. return manipulateDuration(this, value, 1);
  278. }
  279. return oldAdd.bind(this)(value, unit);
  280. };
  281. Dayjs.prototype.subtract = function (value, unit) {
  282. if (isDuration(value)) {
  283. return manipulateDuration(this, value, -1);
  284. }
  285. return oldSubtract.bind(this)(value, unit);
  286. };
  287. });