index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_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 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 (unit) {
  59. return wrapper(input * unitToMS[prettyUnit(unit)], this);
  60. }
  61. if (typeof input === 'number') {
  62. this.$ms = input;
  63. this.parseFromMilliseconds();
  64. return this;
  65. }
  66. if (typeof input === 'object') {
  67. Object.keys(input).forEach(function (k) {
  68. _this.$d[prettyUnit(k)] = input[k];
  69. });
  70. this.calMilliseconds();
  71. return this;
  72. }
  73. if (typeof input === 'string') {
  74. var d = input.match(durationRegex);
  75. if (d) {
  76. this.$d.years = d[2];
  77. this.$d.months = d[3];
  78. this.$d.weeks = d[4];
  79. this.$d.days = d[5];
  80. this.$d.hours = d[6];
  81. this.$d.minutes = d[7];
  82. this.$d.seconds = d[8];
  83. this.calMilliseconds();
  84. return this;
  85. }
  86. }
  87. return this;
  88. }
  89. var _proto = Duration.prototype;
  90. _proto.calMilliseconds = function calMilliseconds() {
  91. var _this2 = this;
  92. this.$ms = Object.keys(this.$d).reduce(function (total, unit) {
  93. return total + (_this2.$d[unit] || 0) * unitToMS[unit];
  94. }, 0);
  95. };
  96. _proto.parseFromMilliseconds = function parseFromMilliseconds() {
  97. var $ms = this.$ms;
  98. this.$d.years = roundNumber($ms / MILLISECONDS_A_YEAR);
  99. $ms %= MILLISECONDS_A_YEAR;
  100. this.$d.months = roundNumber($ms / MILLISECONDS_A_MONTH);
  101. $ms %= MILLISECONDS_A_MONTH;
  102. this.$d.days = roundNumber($ms / MILLISECONDS_A_DAY);
  103. $ms %= MILLISECONDS_A_DAY;
  104. this.$d.hours = roundNumber($ms / MILLISECONDS_A_HOUR);
  105. $ms %= MILLISECONDS_A_HOUR;
  106. this.$d.minutes = roundNumber($ms / MILLISECONDS_A_MINUTE);
  107. $ms %= MILLISECONDS_A_MINUTE;
  108. this.$d.seconds = roundNumber($ms / MILLISECONDS_A_SECOND);
  109. $ms %= MILLISECONDS_A_SECOND;
  110. this.$d.milliseconds = $ms;
  111. };
  112. _proto.toISOString = function toISOString() {
  113. var Y = getNumberUnitFormat(this.$d.years, 'Y');
  114. var M = getNumberUnitFormat(this.$d.months, 'M');
  115. var days = +this.$d.days || 0;
  116. if (this.$d.weeks) {
  117. days += this.$d.weeks * 7;
  118. }
  119. var D = getNumberUnitFormat(days, 'D');
  120. var H = getNumberUnitFormat(this.$d.hours, 'H');
  121. var m = getNumberUnitFormat(this.$d.minutes, 'M');
  122. var seconds = this.$d.seconds || 0;
  123. if (this.$d.milliseconds) {
  124. seconds += this.$d.milliseconds / 1000;
  125. }
  126. var S = getNumberUnitFormat(seconds, 'S');
  127. var negativeMode = Y.negative || M.negative || D.negative || H.negative || m.negative || S.negative;
  128. var T = H.format || m.format || S.format ? 'T' : '';
  129. var P = negativeMode ? '-' : '';
  130. var result = P + "P" + Y.format + M.format + D.format + T + H.format + m.format + S.format;
  131. return result === 'P' || result === '-P' ? 'P0D' : result;
  132. };
  133. _proto.toJSON = function toJSON() {
  134. return this.toISOString();
  135. };
  136. _proto.format = function format(formatStr) {
  137. var str = formatStr || 'YYYY-MM-DDTHH:mm:ss';
  138. var matches = {
  139. Y: this.$d.years,
  140. YY: $u.s(this.$d.years, 2, '0'),
  141. YYYY: $u.s(this.$d.years, 4, '0'),
  142. M: this.$d.months,
  143. MM: $u.s(this.$d.months, 2, '0'),
  144. D: this.$d.days,
  145. DD: $u.s(this.$d.days, 2, '0'),
  146. H: this.$d.hours,
  147. HH: $u.s(this.$d.hours, 2, '0'),
  148. m: this.$d.minutes,
  149. mm: $u.s(this.$d.minutes, 2, '0'),
  150. s: this.$d.seconds,
  151. ss: $u.s(this.$d.seconds, 2, '0'),
  152. SSS: $u.s(this.$d.milliseconds, 3, '0')
  153. };
  154. return str.replace(REGEX_FORMAT, function (match, $1) {
  155. return $1 || String(matches[match]);
  156. });
  157. };
  158. _proto.as = function as(unit) {
  159. return this.$ms / unitToMS[prettyUnit(unit)];
  160. };
  161. _proto.get = function get(unit) {
  162. var base = this.$ms;
  163. var pUnit = prettyUnit(unit);
  164. if (pUnit === 'milliseconds') {
  165. base %= 1000;
  166. } else if (pUnit === 'weeks') {
  167. base = roundNumber(base / unitToMS[pUnit]);
  168. } else {
  169. base = this.$d[pUnit];
  170. }
  171. return base === 0 ? 0 : base; // a === 0 will be true on both 0 and -0
  172. };
  173. _proto.add = function add(input, unit, isSubtract) {
  174. var another;
  175. if (unit) {
  176. another = input * unitToMS[prettyUnit(unit)];
  177. } else if (isDuration(input)) {
  178. another = input.$ms;
  179. } else {
  180. another = wrapper(input, this).$ms;
  181. }
  182. return wrapper(this.$ms + another * (isSubtract ? -1 : 1), this);
  183. };
  184. _proto.subtract = function subtract(input, unit) {
  185. return this.add(input, unit, true);
  186. };
  187. _proto.locale = function locale(l) {
  188. var that = this.clone();
  189. that.$l = l;
  190. return that;
  191. };
  192. _proto.clone = function clone() {
  193. return wrapper(this.$ms, this);
  194. };
  195. _proto.humanize = function humanize(withSuffix) {
  196. return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix);
  197. };
  198. _proto.milliseconds = function milliseconds() {
  199. return this.get('milliseconds');
  200. };
  201. _proto.asMilliseconds = function asMilliseconds() {
  202. return this.as('milliseconds');
  203. };
  204. _proto.seconds = function seconds() {
  205. return this.get('seconds');
  206. };
  207. _proto.asSeconds = function asSeconds() {
  208. return this.as('seconds');
  209. };
  210. _proto.minutes = function minutes() {
  211. return this.get('minutes');
  212. };
  213. _proto.asMinutes = function asMinutes() {
  214. return this.as('minutes');
  215. };
  216. _proto.hours = function hours() {
  217. return this.get('hours');
  218. };
  219. _proto.asHours = function asHours() {
  220. return this.as('hours');
  221. };
  222. _proto.days = function days() {
  223. return this.get('days');
  224. };
  225. _proto.asDays = function asDays() {
  226. return this.as('days');
  227. };
  228. _proto.weeks = function weeks() {
  229. return this.get('weeks');
  230. };
  231. _proto.asWeeks = function asWeeks() {
  232. return this.as('weeks');
  233. };
  234. _proto.months = function months() {
  235. return this.get('months');
  236. };
  237. _proto.asMonths = function asMonths() {
  238. return this.as('months');
  239. };
  240. _proto.years = function years() {
  241. return this.get('years');
  242. };
  243. _proto.asYears = function asYears() {
  244. return this.as('years');
  245. };
  246. return Duration;
  247. }();
  248. export default (function (option, Dayjs, dayjs) {
  249. $d = dayjs;
  250. $u = dayjs().$utils();
  251. dayjs.duration = function (input, unit) {
  252. var $l = dayjs.locale();
  253. return wrapper(input, {
  254. $l: $l
  255. }, unit);
  256. };
  257. dayjs.isDuration = isDuration;
  258. var oldAdd = Dayjs.prototype.add;
  259. var oldSubtract = Dayjs.prototype.subtract;
  260. Dayjs.prototype.add = function (value, unit) {
  261. if (isDuration(value)) value = value.asMilliseconds();
  262. return oldAdd.bind(this)(value, unit);
  263. };
  264. Dayjs.prototype.subtract = function (value, unit) {
  265. if (isDuration(value)) value = value.asMilliseconds();
  266. return oldSubtract.bind(this)(value, unit);
  267. };
  268. });