index.js 6.2 KB

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