PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/moment-range/1.0.6/moment-range.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 321 lines | 133 code | 52 blank | 136 comment | 54 complexity | 7d382433a3ed3eac6bade60744717a18 MD5 | raw file
  1. (function(root, factory) {
  2. if(typeof exports === 'object') {
  3. module.exports = factory(require('moment'));
  4. }
  5. else if(typeof define === 'function' && define.amd) {
  6. define(['moment'], factory);
  7. }
  8. else {
  9. root.moment = factory(root.moment);
  10. }
  11. }(this, function(moment) {
  12. var DateRange, INTERVALS;
  13. INTERVALS = {
  14. year: true,
  15. month: true,
  16. week: true,
  17. day: true,
  18. hour: true,
  19. minute: true,
  20. second: true
  21. };
  22. /**
  23. * DateRange class to store ranges and query dates.
  24. * @typedef {!Object}
  25. *
  26. */
  27. DateRange = (function() {
  28. /**
  29. * DateRange instance.
  30. *
  31. * @param {(Moment|Date)} start Start of interval
  32. * @param {(Moment|Date)} end End of interval
  33. *
  34. * @constructor
  35. *
  36. */
  37. function DateRange(start, end) {
  38. this.start = moment(start);
  39. this.end = moment(end);
  40. }
  41. /**
  42. * Determine if the current interval contains a given moment/date/range.
  43. *
  44. * @param {(Moment|Date|DateRange)} other Date to check
  45. *
  46. * @return {!boolean}
  47. *
  48. */
  49. DateRange.prototype.contains = function(other) {
  50. if (other instanceof DateRange) {
  51. return this.start <= other.start && this.end >= other.end;
  52. } else {
  53. return (this.start <= other && other <= this.end);
  54. }
  55. };
  56. /**
  57. * @private
  58. *
  59. */
  60. DateRange.prototype._by_string = function(interval, hollaback) {
  61. var current, _results;
  62. current = moment(this.start);
  63. _results = [];
  64. while (this.contains(current)) {
  65. hollaback.call(this, current.clone());
  66. _results.push(current.add(1, interval));
  67. }
  68. return _results;
  69. };
  70. /**
  71. * @private
  72. *
  73. */
  74. DateRange.prototype._by_range = function(range_interval, hollaback) {
  75. var i, l, _i, _results;
  76. l = Math.floor(this / range_interval);
  77. if (l === Infinity) {
  78. return this;
  79. }
  80. _results = [];
  81. for (i = _i = 0; 0 <= l ? _i <= l : _i >= l; i = 0 <= l ? ++_i : --_i) {
  82. _results.push(hollaback.call(this, moment(this.start.valueOf() + range_interval.valueOf() * i)));
  83. }
  84. return _results;
  85. };
  86. /**
  87. * Determine if the current date range overlaps a given date range.
  88. *
  89. * @param {!DateRange} range Date range to check
  90. *
  91. * @return {!boolean}
  92. *
  93. */
  94. DateRange.prototype.overlaps = function(range) {
  95. return this.intersect(range) !== null;
  96. };
  97. /**
  98. * Determine the intersecting periods from one or more date ranges.
  99. *
  100. * @param {!DateRange} other A date range to intersect with this one
  101. *
  102. * @return {!DateRange|null}
  103. *
  104. */
  105. DateRange.prototype.intersect = function(other) {
  106. var _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
  107. if (((this.start <= (_ref1 = other.start) && _ref1 < (_ref = this.end)) && _ref < other.end)) {
  108. return new DateRange(other.start, this.end);
  109. } else if (((other.start < (_ref3 = this.start) && _ref3 < (_ref2 = other.end)) && _ref2 <= this.end)) {
  110. return new DateRange(this.start, other.end);
  111. } else if (((other.start < (_ref5 = this.start) && _ref5 < (_ref4 = this.end)) && _ref4 < other.end)) {
  112. return this;
  113. } else if (((this.start <= (_ref7 = other.start) && _ref7 < (_ref6 = other.end)) && _ref6 <= this.end)) {
  114. return other;
  115. } else {
  116. return null;
  117. }
  118. /**
  119. * Merge date ranges if they intersect.
  120. *
  121. * @param {!DateRange} other A date range to add to this one
  122. *
  123. * @return {!DateRange|null}
  124. *
  125. */
  126. };
  127. DateRange.prototype.add = function(other) {
  128. if (this.overlaps(other)) {
  129. return new DateRange(moment.min(this.start, other.start), moment.max(this.end, other.end));
  130. } else {
  131. return null;
  132. }
  133. };
  134. /**
  135. * Subtract one range from another.
  136. *
  137. * @param {!DateRange} other A date range to substract from this one
  138. *
  139. * @return {!DateRange[]}
  140. *
  141. */
  142. DateRange.prototype.subtract = function(other) {
  143. var _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
  144. if (this.intersect(other) === null) {
  145. return [this];
  146. } else if (((other.start <= (_ref1 = this.start) && _ref1 < (_ref = this.end)) && _ref <= other.end)) {
  147. return [];
  148. } else if (((other.start <= (_ref3 = this.start) && _ref3 < (_ref2 = other.end)) && _ref2 < this.end)) {
  149. return [new DateRange(other.end, this.end)];
  150. } else if (((this.start < (_ref5 = other.start) && _ref5 < (_ref4 = this.end)) && _ref4 <= other.end)) {
  151. return [new DateRange(this.start, other.start)];
  152. } else if (((this.start < (_ref7 = other.start) && _ref7 < (_ref6 = other.end)) && _ref6 < this.end)) {
  153. return [new DateRange(this.start, other.start), new DateRange(other.end, this.end)];
  154. }
  155. };
  156. /**
  157. * Iterate over the date range by a given date range, executing a function
  158. * for each sub-range.
  159. *
  160. * @param {(!DateRange|String)} range Date range to be used for iteration
  161. * or shorthand string (shorthands:
  162. * http://momentjs.com/docs/#/manipulating/add/)
  163. * @param {!function(Moment)} hollaback Function to execute for each sub-range
  164. *
  165. * @return {!boolean}
  166. *
  167. */
  168. DateRange.prototype.by = function(range, hollaback) {
  169. if (typeof range === 'string') {
  170. this._by_string(range, hollaback);
  171. } else {
  172. this._by_range(range, hollaback);
  173. }
  174. return this;
  175. };
  176. /**
  177. * Date range in milliseconds. Allows basic coercion math of date ranges.
  178. *
  179. * @return {!number}
  180. *
  181. */
  182. DateRange.prototype.valueOf = function() {
  183. return this.end - this.start;
  184. };
  185. /**
  186. * Date range toDate
  187. *
  188. * @return {!Array}
  189. *
  190. */
  191. DateRange.prototype.toDate = function() {
  192. return [this.start.toDate(), this.end.toDate()];
  193. };
  194. /**
  195. * Determine if this date range is the same as another.
  196. *
  197. * @param {!DateRange} other Another date range to compare to
  198. *
  199. * @return {!boolean}
  200. *
  201. */
  202. DateRange.prototype.isSame = function(other) {
  203. return this.start.isSame(other.start) && this.end.isSame(other.end);
  204. };
  205. /**
  206. * The difference of the end vs start.
  207. *
  208. * @param {number} unit Unit of difference, if no unit is passed in
  209. * milliseconds are returned. E.g.: `"days"`,
  210. * `"months"`, etc...
  211. *
  212. * @return {!number}
  213. *
  214. */
  215. DateRange.prototype.diff = function(unit) {
  216. if (unit == null) {
  217. unit = void 0;
  218. }
  219. return this.end.diff(this.start, unit);
  220. };
  221. return DateRange;
  222. })();
  223. /**
  224. * Build a date range.
  225. *
  226. * @param {(Moment|Date)} start Start of range
  227. * @param {(Moment|Date)} end End of range
  228. *
  229. * @this {Moment}
  230. *
  231. * @return {!DateRange}
  232. *
  233. */
  234. moment.fn.range = function(start, end) {
  235. if (start in INTERVALS) {
  236. return new DateRange(moment(this).startOf(start), moment(this).endOf(start));
  237. } else {
  238. return new DateRange(start, end);
  239. }
  240. };
  241. /**
  242. * Build a date range.
  243. *
  244. * @param {(Moment|Date)} start Start of range
  245. * @param {(Moment|Date)} end End of range
  246. *
  247. * @this {Moment}
  248. *
  249. * @return {!DateRange}
  250. *
  251. */
  252. moment.range = function(start, end) {
  253. return new DateRange(start, end);
  254. };
  255. /**
  256. * Check if the current moment is within a given date range.
  257. *
  258. * @param {!DateRange} range Date range to check
  259. *
  260. * @this {Moment}
  261. *
  262. * @return {!boolean}
  263. *
  264. */
  265. moment.fn.within = function(range) {
  266. return range.contains(this._d);
  267. };
  268. return moment;
  269. }));