Skip to content

Commit f5c5905

Browse files
committed
add built utils
1 parent aa21929 commit f5c5905

File tree

1 file changed

+259
-0
lines changed

1 file changed

+259
-0
lines changed

lib/utils/index.js

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
'use strict';
2+
3+
exports.__esModule = true;
4+
exports.animate = exports.withImmutableProps = exports.scrollbarSize = exports.ScrollSpeed = exports.keyCodes = undefined;
5+
6+
var _withPropsOnChange2 = require('recompose/withPropsOnChange');
7+
8+
var _withPropsOnChange3 = _interopRequireDefault(_withPropsOnChange2);
9+
10+
exports.getMonth = getMonth;
11+
exports.getWeek = getWeek;
12+
exports.getWeeksInMonth = getWeeksInMonth;
13+
exports.emptyFn = emptyFn;
14+
exports.sanitizeDate = sanitizeDate;
15+
exports.getDateString = getDateString;
16+
exports.getMonthsForYear = getMonthsForYear;
17+
exports.debounce = debounce;
18+
exports.range = range;
19+
20+
var _animate = require('./animate');
21+
22+
Object.defineProperty(exports, 'animate', {
23+
enumerable: true,
24+
get: function get() {
25+
return _interopRequireDefault(_animate).default;
26+
}
27+
});
28+
29+
var _scrollbarSize = require('dom-helpers/util/scrollbarSize');
30+
31+
var _scrollbarSize2 = _interopRequireDefault(_scrollbarSize);
32+
33+
var _get_days_in_month = require('date-fns/get_days_in_month');
34+
35+
var _get_days_in_month2 = _interopRequireDefault(_get_days_in_month);
36+
37+
var _get_day = require('date-fns/get_day');
38+
39+
var _get_day2 = _interopRequireDefault(_get_day);
40+
41+
var _is_after = require('date-fns/is_after');
42+
43+
var _is_after2 = _interopRequireDefault(_is_after);
44+
45+
var _is_before = require('date-fns/is_before');
46+
47+
var _is_before2 = _interopRequireDefault(_is_before);
48+
49+
var _is_same_day = require('date-fns/is_same_day');
50+
51+
var _is_same_day2 = _interopRequireDefault(_is_same_day);
52+
53+
var _end_of_day = require('date-fns/end_of_day');
54+
55+
var _end_of_day2 = _interopRequireDefault(_end_of_day);
56+
57+
var _start_of_day = require('date-fns/start_of_day');
58+
59+
var _start_of_day2 = _interopRequireDefault(_start_of_day);
60+
61+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
62+
63+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
64+
65+
var keyCodes = exports.keyCodes = {
66+
command: 91,
67+
control: 17,
68+
down: 40,
69+
enter: 13,
70+
escape: 27,
71+
left: 37,
72+
right: 39,
73+
shift: 16,
74+
up: 38
75+
};
76+
77+
/**
78+
* Given a year and a month, returns the rows for that month to be iterated over
79+
* @param {Number} year - the year number
80+
* @param {Number} month - the index of the month
81+
* @param {Number} weekStartsOn - the index of the first day of the week (from 0 to 6)
82+
* @return {Object} - Returns the first day of the month and the rows of that month
83+
*/
84+
function getMonth(year, month, weekStartsOn) {
85+
var rows = [];
86+
var monthDate = new Date(year, month, 1);
87+
var daysInMonth = (0, _get_days_in_month2.default)(monthDate);
88+
var weekEndsOn = getEndOfWeekIndex(weekStartsOn);
89+
90+
var dow = (0, _get_day2.default)(new Date(year, month, 1));
91+
var week = 0;
92+
93+
for (var day = 1; day <= daysInMonth; day++) {
94+
if (!rows[week]) {
95+
rows[week] = [];
96+
}
97+
98+
rows[week].push(day);
99+
100+
if (dow === weekEndsOn) {
101+
week++;
102+
}
103+
104+
dow = dow < 6 ? dow + 1 : 0;
105+
}
106+
107+
return {
108+
date: monthDate,
109+
rows: rows
110+
};
111+
}
112+
113+
function getWeek(yearStart, date, weekStartsOn) {
114+
var yearStartDate = typeof yearStart === 'number' ? new Date(yearStart, 0, 1) // 1st Jan of the Year
115+
: yearStart;
116+
117+
return Math.ceil((Math.round((date - yearStartDate) / (60 * 60 * 24 * 1000)) + yearStartDate.getDay() + 1 - weekStartsOn) / 7);
118+
}
119+
120+
/**
121+
* Get the number of weeks in a given month to be able to calculate the height of that month
122+
* @param {Number} year - the year number
123+
* @param {Number} month - the index of the month
124+
* @param {Number} weekStartsOn - the index of the first day of the week (from 0 to 6)
125+
* @return {Number} - Returns the number of weeks for the given month
126+
*/
127+
function getWeeksInMonth(month) {
128+
var year = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date().getFullYear();
129+
var weekStartsOn = arguments[2];
130+
var isLastDisplayedMonth = arguments[3];
131+
132+
var weekEndsOn = getEndOfWeekIndex(weekStartsOn);
133+
134+
var firstOfMonth = new Date(year, month, 1);
135+
var firstWeekNumber = getWeek(year, firstOfMonth, weekStartsOn);
136+
137+
var lastOfMonth = new Date(year, month + 1, 0); // Last date of the Month
138+
var lastWeekNumber = getWeek(year, lastOfMonth, weekStartsOn);
139+
140+
var rowCount = lastWeekNumber - firstWeekNumber;
141+
142+
// If the last week contains 7 days, we need to add an extra row
143+
if (lastOfMonth.getDay() === weekEndsOn || isLastDisplayedMonth) {
144+
rowCount++;
145+
}
146+
147+
return rowCount;
148+
}
149+
150+
/**
151+
* Helper to find out what day the week ends on given the localized start of the week
152+
* @param {Number} weekStartsOn - the index of the first day of the week (from 0 to 6)
153+
* @return {Number} - Returns the index of the day the week ends on
154+
*/
155+
function getEndOfWeekIndex(weekStartsOn) {
156+
var weekEndsOn = weekStartsOn === 0 ? 6 : weekStartsOn - 1;
157+
158+
return weekEndsOn;
159+
}
160+
161+
var ScrollSpeed = exports.ScrollSpeed = function () {
162+
function ScrollSpeed() {
163+
var _this = this;
164+
165+
_classCallCheck(this, ScrollSpeed);
166+
167+
this.clear = function () {
168+
_this.lastPosition = null;
169+
_this.delta = 0;
170+
};
171+
}
172+
173+
ScrollSpeed.prototype.getScrollSpeed = function getScrollSpeed(scrollOffset) {
174+
if (this.lastPosition != null) {
175+
this.delta = scrollOffset - this.lastPosition;
176+
}
177+
this.lastPosition = scrollOffset;
178+
179+
clearTimeout(this._timeout);
180+
this._timeout = setTimeout(this.clear, 50);
181+
182+
return this.delta;
183+
};
184+
185+
return ScrollSpeed;
186+
}();
187+
188+
var scrollbarSize = exports.scrollbarSize = (0, _scrollbarSize2.default)();
189+
190+
function emptyFn() {
191+
/* no-op */
192+
}
193+
194+
function sanitizeDate(date, _ref) {
195+
var _ref$disabledDates = _ref.disabledDates,
196+
disabledDates = _ref$disabledDates === undefined ? [] : _ref$disabledDates,
197+
_ref$disabledDays = _ref.disabledDays,
198+
disabledDays = _ref$disabledDays === undefined ? [] : _ref$disabledDays,
199+
minDate = _ref.minDate,
200+
maxDate = _ref.maxDate;
201+
202+
// Selected date should not be disabled or outside the selectable range
203+
if (!date || disabledDates.some(function (disabledDate) {
204+
return (0, _is_same_day2.default)(disabledDate, date);
205+
}) || disabledDays && disabledDays.indexOf((0, _get_day2.default)(date)) !== -1 || minDate && (0, _is_before2.default)(date, (0, _start_of_day2.default)(minDate)) || maxDate && (0, _is_after2.default)(date, (0, _end_of_day2.default)(maxDate))) {
206+
return null;
207+
}
208+
209+
return date;
210+
}
211+
212+
function getDateString(year, month, date) {
213+
return year + '-' + ('0' + (month + 1)).slice(-2) + '-' + ('0' + date).slice(-2);
214+
}
215+
216+
function getMonthsForYear(year) {
217+
var day = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
218+
219+
return Array.apply(null, Array(12)).map(function (val, index) {
220+
var constrainedDay = Math.min((0, _get_days_in_month2.default)(new Date(year, index, 1)), day);
221+
return new Date(year, index, constrainedDay);
222+
});
223+
}
224+
225+
var withImmutableProps = exports.withImmutableProps = function withImmutableProps(props) {
226+
return (0, _withPropsOnChange3.default)(function () {
227+
return false;
228+
}, props);
229+
};
230+
231+
function debounce(callback, wait) {
232+
var _this2 = this;
233+
234+
var timeout = null;
235+
var callbackArgs = null;
236+
237+
var later = function later() {
238+
return callback.apply(_this2, callbackArgs);
239+
};
240+
241+
return function () {
242+
callbackArgs = arguments;
243+
clearTimeout(timeout);
244+
timeout = setTimeout(later, wait);
245+
};
246+
}
247+
248+
function range(start, stop) {
249+
var step = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
250+
251+
var length = Math.max(Math.ceil((stop - start) / step), 0);
252+
var range = Array(length);
253+
254+
for (var i = 0; i < length; i++, start += step) {
255+
range[i] = start;
256+
}
257+
258+
return range;
259+
};

0 commit comments

Comments
 (0)