Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 (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. }
  134. var S = getNumberUnitFormat(seconds, 'S');
  135. var negativeMode = Y.negative || M.negative || D.negative || H.negative || m.negative || S.negative;
  136. var T = H.format || m.format || S.format ? 'T' : '';
  137. var P = negativeMode ? '-' : '';
  138. var result = P + "P" + Y.format + M.format + D.format + T + H.format + m.format + S.format;
  139. return result === 'P' || result === '-P' ? 'P0D' : result;
  140. };
  141. _proto.toJSON = function toJSON() {
  142. return this.toISOString();
  143. };
  144. _proto.format = function format(formatStr) {
  145. var str = formatStr || 'YYYY-MM-DDTHH:mm:ss';
  146. var matches = {
  147. Y: this.$d.years,
  148. YY: $u.s(this.$d.years, 2, '0'),
  149. YYYY: $u.s(this.$d.years, 4, '0'),
  150. M: this.$d.months,
  151. MM: $u.s(this.$d.months, 2, '0'),
  152. D: this.$d.days,
  153. DD: $u.s(this.$d.days, 2, '0'),
  154. H: this.$d.hours,
  155. HH: $u.s(this.$d.hours, 2, '0'),
  156. m: this.$d.minutes,
  157. mm: $u.s(this.$d.minutes, 2, '0'),
  158. s: this.$d.seconds,
  159. ss: $u.s(this.$d.seconds, 2, '0'),
  160. SSS: $u.s(this.$d.milliseconds, 3, '0')
  161. };
  162. return str.replace(REGEX_FORMAT, function (match, $1) {
  163. return $1 || String(matches[match]);
  164. });
  165. };
  166. _proto.as = function as(unit) {
  167. return this.$ms / unitToMS[prettyUnit(unit)];
  168. };
  169. _proto.get = function get(unit) {
  170. var base = this.$ms;
  171. var pUnit = prettyUnit(unit);
  172. if (pUnit === 'milliseconds') {
  173. base %= 1000;
  174. } else if (pUnit === 'weeks') {
  175. base = roundNumber(base / unitToMS[pUnit]);
  176. } else {
  177. base = this.$d[pUnit];
  178. }
  179. return base === 0 ? 0 : base; // a === 0 will be true on both 0 and -0
  180. };
  181. _proto.add = function add(input, unit, isSubtract) {
  182. var another;
  183. if (unit) {
  184. another = input * unitToMS[prettyUnit(unit)];
  185. } else if (isDuration(input)) {
  186. another = input.$ms;
  187. } else {
  188. another = wrapper(input, this).$ms;
  189. }
  190. return wrapper(this.$ms + another * (isSubtract ? -1 : 1), this);
  191. };
  192. _proto.subtract = function subtract(input, unit) {
  193. return this.add(input, unit, true);
  194. };
  195. _proto.locale = function locale(l) {
  196. var that = this.clone();
  197. that.$l = l;
  198. return that;
  199. };
  200. _proto.clone = function clone() {
  201. return wrapper(this.$ms, this);
  202. };
  203. _proto.humanize = function humanize(withSuffix) {
  204. return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix);
  205. };
  206. _proto.milliseconds = function milliseconds() {
  207. return this.get('milliseconds');
  208. };
  209. _proto.asMilliseconds = function asMilliseconds() {
  210. return this.as('milliseconds');
  211. };
  212. _proto.seconds = function seconds() {
  213. return this.get('seconds');
  214. };
  215. _proto.asSeconds = function asSeconds() {
  216. return this.as('seconds');
  217. };
  218. _proto.minutes = function minutes() {
  219. return this.get('minutes');
  220. };
  221. _proto.asMinutes = function asMinutes() {
  222. return this.as('minutes');
  223. };
  224. _proto.hours = function hours() {
  225. return this.get('hours');
  226. };
  227. _proto.asHours = function asHours() {
  228. return this.as('hours');
  229. };
  230. _proto.days = function days() {
  231. return this.get('days');
  232. };
  233. _proto.asDays = function asDays() {
  234. return this.as('days');
  235. };
  236. _proto.weeks = function weeks() {
  237. return this.get('weeks');
  238. };
  239. _proto.asWeeks = function asWeeks() {
  240. return this.as('weeks');
  241. };
  242. _proto.months = function months() {
  243. return this.get('months');
  244. };
  245. _proto.asMonths = function asMonths() {
  246. return this.as('months');
  247. };
  248. _proto.years = function years() {
  249. return this.get('years');
  250. };
  251. _proto.asYears = function asYears() {
  252. return this.as('years');
  253. };
  254. return Duration;
  255. }();
  256. export default (function (option, Dayjs, dayjs) {
  257. $d = dayjs;
  258. $u = dayjs().$utils();
  259. dayjs.duration = function (input, unit) {
  260. var $l = dayjs.locale();
  261. return wrapper(input, {
  262. $l: $l
  263. }, unit);
  264. };
  265. dayjs.isDuration = isDuration;
  266. var oldAdd = Dayjs.prototype.add;
  267. var oldSubtract = Dayjs.prototype.subtract;
  268. Dayjs.prototype.add = function (value, unit) {
  269. if (isDuration(value)) value = value.asMilliseconds();
  270. return oldAdd.bind(this)(value, unit);
  271. };
  272. Dayjs.prototype.subtract = function (value, unit) {
  273. if (isDuration(value)) value = value.asMilliseconds();
  274. return oldSubtract.bind(this)(value, unit);
  275. };
  276. });