index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK } from '../../constant';
  2. var MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365;
  3. var MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30;
  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 Duration = /*#__PURE__*/function () {
  27. function Duration(input, unit, locale) {
  28. var _this = this;
  29. this.$d = {};
  30. this.$l = locale;
  31. if (unit) {
  32. return wrapper(input * unitToMS[prettyUnit(unit)], this);
  33. }
  34. if (typeof input === 'number') {
  35. this.$ms = input;
  36. this.parseFromMilliseconds();
  37. return this;
  38. }
  39. if (typeof input === 'object') {
  40. Object.keys(input).forEach(function (k) {
  41. _this.$d[prettyUnit(k)] = input[k];
  42. });
  43. this.calMilliseconds();
  44. return this;
  45. }
  46. if (typeof input === 'string') {
  47. var d = input.match(durationRegex);
  48. if (d) {
  49. this.$d.years = d[2];
  50. this.$d.months = d[3];
  51. this.$d.weeks = d[4];
  52. this.$d.days = d[5];
  53. this.$d.hours = d[6];
  54. this.$d.minutes = d[7];
  55. this.$d.seconds = d[8];
  56. this.calMilliseconds();
  57. return this;
  58. }
  59. }
  60. return this;
  61. }
  62. var _proto = Duration.prototype;
  63. _proto.calMilliseconds = function calMilliseconds() {
  64. var _this2 = this;
  65. this.$ms = Object.keys(this.$d).reduce(function (total, unit) {
  66. return total + (_this2.$d[unit] || 0) * (unitToMS[unit] || 1);
  67. }, 0);
  68. };
  69. _proto.parseFromMilliseconds = function parseFromMilliseconds() {
  70. var $ms = this.$ms;
  71. this.$d.years = Math.floor($ms / MILLISECONDS_A_YEAR);
  72. $ms %= MILLISECONDS_A_YEAR;
  73. this.$d.months = Math.floor($ms / MILLISECONDS_A_MONTH);
  74. $ms %= MILLISECONDS_A_MONTH;
  75. this.$d.days = Math.floor($ms / MILLISECONDS_A_DAY);
  76. $ms %= MILLISECONDS_A_DAY;
  77. this.$d.hours = Math.floor($ms / MILLISECONDS_A_HOUR);
  78. $ms %= MILLISECONDS_A_HOUR;
  79. this.$d.minutes = Math.floor($ms / MILLISECONDS_A_MINUTE);
  80. $ms %= MILLISECONDS_A_MINUTE;
  81. this.$d.seconds = Math.floor($ms / MILLISECONDS_A_SECOND);
  82. $ms %= MILLISECONDS_A_SECOND;
  83. this.$d.milliseconds = $ms;
  84. };
  85. _proto.toISOString = function toISOString() {
  86. var Y = this.$d.years ? this.$d.years + "Y" : '';
  87. var M = this.$d.months ? this.$d.months + "M" : '';
  88. var days = +this.$d.days || 0;
  89. if (this.$d.weeks) {
  90. days += this.$d.weeks * 7;
  91. }
  92. var D = days ? days + "D" : '';
  93. var H = this.$d.hours ? this.$d.hours + "H" : '';
  94. var m = this.$d.minutes ? this.$d.minutes + "M" : '';
  95. var seconds = this.$d.seconds || 0;
  96. if (this.$d.milliseconds) {
  97. seconds += this.$d.milliseconds / 1000;
  98. }
  99. var S = seconds ? seconds + "S" : '';
  100. var T = H || m || S ? 'T' : '';
  101. var result = "P" + Y + M + D + T + H + m + S;
  102. return result === 'P' ? 'P0D' : result;
  103. };
  104. _proto.toJSON = function toJSON() {
  105. return this.toISOString();
  106. };
  107. _proto.as = function as(unit) {
  108. return this.$ms / (unitToMS[prettyUnit(unit)] || 1);
  109. };
  110. _proto.get = function get(unit) {
  111. var base = this.$ms;
  112. var pUnit = prettyUnit(unit);
  113. if (pUnit === 'milliseconds') {
  114. base %= 1000;
  115. } else if (pUnit === 'weeks') {
  116. base = Math.floor(base / unitToMS[pUnit]);
  117. } else {
  118. base = this.$d[pUnit];
  119. }
  120. return base;
  121. };
  122. _proto.add = function add(input, unit, isSubtract) {
  123. var another;
  124. if (unit) {
  125. another = input * unitToMS[prettyUnit(unit)];
  126. } else if (isDuration(input)) {
  127. another = input.$ms;
  128. } else {
  129. another = wrapper(input, this).$ms;
  130. }
  131. return wrapper(this.$ms + another * (isSubtract ? -1 : 1), this);
  132. };
  133. _proto.subtract = function subtract(input, unit) {
  134. return this.add(input, unit, true);
  135. };
  136. _proto.locale = function locale(l) {
  137. var that = this.clone();
  138. that.$l = l;
  139. return that;
  140. };
  141. _proto.clone = function clone() {
  142. return wrapper(this.$ms, this);
  143. };
  144. _proto.humanize = function humanize(withSuffix) {
  145. return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix);
  146. };
  147. _proto.milliseconds = function milliseconds() {
  148. return this.get('milliseconds');
  149. };
  150. _proto.asMilliseconds = function asMilliseconds() {
  151. return this.as('milliseconds');
  152. };
  153. _proto.seconds = function seconds() {
  154. return this.get('seconds');
  155. };
  156. _proto.asSeconds = function asSeconds() {
  157. return this.as('seconds');
  158. };
  159. _proto.minutes = function minutes() {
  160. return this.get('minutes');
  161. };
  162. _proto.asMinutes = function asMinutes() {
  163. return this.as('minutes');
  164. };
  165. _proto.hours = function hours() {
  166. return this.get('hours');
  167. };
  168. _proto.asHours = function asHours() {
  169. return this.as('hours');
  170. };
  171. _proto.days = function days() {
  172. return this.get('days');
  173. };
  174. _proto.asDays = function asDays() {
  175. return this.as('days');
  176. };
  177. _proto.weeks = function weeks() {
  178. return this.get('weeks');
  179. };
  180. _proto.asWeeks = function asWeeks() {
  181. return this.as('weeks');
  182. };
  183. _proto.months = function months() {
  184. return this.get('months');
  185. };
  186. _proto.asMonths = function asMonths() {
  187. return this.as('months');
  188. };
  189. _proto.years = function years() {
  190. return this.get('years');
  191. };
  192. _proto.asYears = function asYears() {
  193. return this.as('years');
  194. };
  195. return Duration;
  196. }();
  197. export default (function (option, Dayjs, dayjs) {
  198. $d = dayjs;
  199. $u = dayjs().$utils();
  200. dayjs.duration = function (input, unit) {
  201. var $l = dayjs.locale();
  202. return wrapper(input, {
  203. $l: $l
  204. }, unit);
  205. };
  206. dayjs.isDuration = isDuration;
  207. });