@spaced-out/ui-design-system 0.1.110 → 0.1.111

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spaced-out/ui-design-system",
3
- "version": "0.1.110",
3
+ "version": "0.1.111",
4
4
  "main": "index.js",
5
5
  "description": "Sense UI components library",
6
6
  "author": {
@@ -1,236 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.wrangleMoment = exports.isStartOfRange = exports.isStartDateEndDateSame = exports.isEndOfRange = exports.inDateRange = exports.getTimezones = exports.getSubtractedDate = exports.getMonthStartDate = exports.getInitialDates = exports.getDaysInMonth = exports.getAvailableMonths = exports.getAddedDate = exports.generateAvailableYears = exports.formatIsoDate = exports.addTimezone = exports.WEEKDAYS = exports.NAVIGATION_ACTION = exports.MONTHS = exports.MARKERS = void 0;
7
- var _formatISO = _interopRequireDefault(require("date-fns/formatISO"));
8
- var _parseISO = _interopRequireDefault(require("date-fns/parseISO"));
9
- var _lodash = _interopRequireDefault(require("lodash"));
10
- var _momentTimezone = _interopRequireDefault(require("moment-timezone"));
11
- var _timezones = require("./timezones");
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- // $FlowFixMe - strict types for date-fns
15
-
16
- // $FlowFixMe - strict types for date-fns
17
-
18
- // $FlowFixMe[untyped-import]
19
-
20
- const NAVIGATION_ACTION = Object.freeze({
21
- NEXT: 'next',
22
- PREV: 'prev'
23
- });
24
- exports.NAVIGATION_ACTION = NAVIGATION_ACTION;
25
- const MARKERS = Object.freeze({
26
- DATE_RANGE_START: 'FIRST',
27
- DATE_RANGE_END: 'SECOND'
28
- });
29
- exports.MARKERS = MARKERS;
30
- const WEEKDAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
31
- exports.WEEKDAYS = WEEKDAYS;
32
- const MONTHS = [{
33
- key: '0',
34
- label: 'Jan'
35
- }, {
36
- key: '1',
37
- label: 'Feb'
38
- }, {
39
- key: '2',
40
- label: 'Mar'
41
- }, {
42
- key: '3',
43
- label: 'Apr'
44
- }, {
45
- key: '4',
46
- label: 'May'
47
- }, {
48
- key: '5',
49
- label: 'Jun'
50
- }, {
51
- key: '6',
52
- label: 'Jul'
53
- }, {
54
- key: '7',
55
- label: 'Aug'
56
- }, {
57
- key: '8',
58
- label: 'Sep'
59
- }, {
60
- key: '9',
61
- label: 'Oct'
62
- }, {
63
- key: '10',
64
- label: 'Nov'
65
- }, {
66
- key: '11',
67
- label: 'Dec'
68
- }];
69
- exports.MONTHS = MONTHS;
70
- const wrangleMoment = date => {
71
- if (date instanceof Date) {
72
- return date;
73
- } else if (!date) {
74
- return new Date();
75
- }
76
- return date instanceof _momentTimezone.default ? date.toDate() : (0, _parseISO.default)(date);
77
- };
78
- exports.wrangleMoment = wrangleMoment;
79
- const formatIsoDate = date => (0, _formatISO.default)(wrangleMoment(date), {
80
- representation: 'date'
81
- });
82
- exports.formatIsoDate = formatIsoDate;
83
- const isStartOfRange = (_ref, date) => {
84
- let {
85
- startDate
86
- } = _ref;
87
- return Boolean(startDate) && (0, _momentTimezone.default)(date).isSame((0, _momentTimezone.default)(startDate), 'd');
88
- };
89
- exports.isStartOfRange = isStartOfRange;
90
- const isEndOfRange = (_ref2, date) => {
91
- let {
92
- endDate
93
- } = _ref2;
94
- return Boolean(endDate) && (0, _momentTimezone.default)(date).isSame((0, _momentTimezone.default)(endDate), 'd');
95
- };
96
- exports.isEndOfRange = isEndOfRange;
97
- const inDateRange = (_ref3, date) => {
98
- let {
99
- startDate,
100
- endDate
101
- } = _ref3;
102
- if (startDate && endDate) {
103
- const momentDay = (0, _momentTimezone.default)(date);
104
- const momentStartDate = (0, _momentTimezone.default)(startDate);
105
- const momentEndDate = (0, _momentTimezone.default)(endDate);
106
- return momentDay.isBetween(momentStartDate, momentEndDate, null, '[]');
107
- }
108
- return false;
109
- };
110
- exports.inDateRange = inDateRange;
111
- const isStartDateEndDateSame = _ref4 => {
112
- let {
113
- startDate,
114
- endDate
115
- } = _ref4;
116
- if (startDate && endDate) {
117
- return (0, _momentTimezone.default)(startDate).isSame((0, _momentTimezone.default)(endDate), 'd');
118
- }
119
- return false;
120
- };
121
- exports.isStartDateEndDateSame = isStartDateEndDateSame;
122
- const getMonthAndYear = date => {
123
- const momentDate = date ? (0, _momentTimezone.default)(date) : (0, _momentTimezone.default)();
124
- return [momentDate.month(), momentDate.year()];
125
- };
126
- const getDaysInMonth = date => {
127
- const startWeek = (0, _momentTimezone.default)(date).startOf('month').startOf('week');
128
- const endWeek = (0, _momentTimezone.default)(date).endOf('month').endOf('week');
129
- const days = [],
130
- current = startWeek;
131
- while (current.isBefore(endWeek)) {
132
- days.push(current.clone().format('YYYY-MM-DD'));
133
- current.add(1, 'd');
134
- }
135
- const daysInChunks = _lodash.default.chunk(days, 7);
136
-
137
- // if total rows in calendar are 5 add one more week to the calendar
138
- if (daysInChunks.length === 5) {
139
- const nextWeek = getAddedDate(endWeek, WEEKDAYS.length, 'd');
140
- const extraDays = [];
141
- while (current.isSameOrBefore(nextWeek)) {
142
- extraDays.push(current.clone().format('YYYY-MM-DD'));
143
- current.add(1, 'd');
144
- }
145
- daysInChunks.push(extraDays);
146
- }
147
- return daysInChunks;
148
- };
149
- exports.getDaysInMonth = getDaysInMonth;
150
- const getAddedDate = (date, addCount, timeUnit) => formatIsoDate((0, _momentTimezone.default)(date).add(addCount, timeUnit));
151
- exports.getAddedDate = getAddedDate;
152
- const getSubtractedDate = (date, subtractCount, timeUnit) => formatIsoDate((0, _momentTimezone.default)(date).subtract(subtractCount, timeUnit));
153
- exports.getSubtractedDate = getSubtractedDate;
154
- const getMonthStartDate = date => formatIsoDate((0, _momentTimezone.default)(date).startOf('M'));
155
- exports.getMonthStartDate = getMonthStartDate;
156
- const addTimezone = (date, timezone) => _momentTimezone.default.tz(date, timezone).startOf('d').utc().toISOString();
157
- exports.addTimezone = addTimezone;
158
- const getTimezones = () => Object.keys(_timezones.TIMEZONES).reduce((menuOptions, key) => {
159
- menuOptions.push({
160
- key,
161
- label: _timezones.TIMEZONES[key]
162
- });
163
- return menuOptions;
164
- }, []);
165
- exports.getTimezones = getTimezones;
166
- const generateAvailableYears = (count, firstCalendarDate, secondCalendarDate, marker) => {
167
- const startYear = (0, _momentTimezone.default)().year() - count + 1;
168
- const firstCalendarYear = (0, _momentTimezone.default)(firstCalendarDate).add(1, 'M').year();
169
- const secondCalendarYear = (0, _momentTimezone.default)(secondCalendarDate).subtract(1, 'M').year();
170
- const isWithinRange = year => marker === MARKERS.DATE_RANGE_START ? year <= secondCalendarYear : year >= firstCalendarYear;
171
- return Array.from({
172
- length: count
173
- }, (_, index) => {
174
- const year = startYear + index;
175
- return {
176
- key: year.toString(),
177
- label: year.toString(),
178
- disabled: !isWithinRange(year)
179
- };
180
- }).filter(_ref5 => {
181
- let {
182
- disabled
183
- } = _ref5;
184
- return !disabled;
185
- });
186
- };
187
- exports.generateAvailableYears = generateAvailableYears;
188
- const getAvailableMonths = (Months, first, second, marker) => {
189
- const [firstMonth, firstYear] = getMonthAndYear(first);
190
- const [secondMonth, secondYear] = getMonthAndYear(second);
191
- const [currentMonth, currentYear] = getMonthAndYear();
192
- return Months.filter(month => {
193
- const isSameYear = firstYear === secondYear;
194
- const isMonthBeforeFirstMonth = month.key <= firstMonth;
195
- const isMonthAfterCurrentMonth = currentYear === secondYear && month.key > currentMonth;
196
- if (marker === MARKERS.DATE_RANGE_START) {
197
- return !(isSameYear && month.key >= secondMonth);
198
- } else {
199
- return !(isSameYear && isMonthBeforeFirstMonth || isMonthAfterCurrentMonth);
200
- }
201
- });
202
- };
203
- exports.getAvailableMonths = getAvailableMonths;
204
- const getInitialDates = selectedDateRange => {
205
- const today = formatIsoDate();
206
- const currentTimezone = _momentTimezone.default.tz.guess();
207
- const {
208
- startDate,
209
- endDate,
210
- timezone
211
- } = selectedDateRange;
212
- const isStartDateValid = (0, _momentTimezone.default)(startDate).isSameOrBefore(today);
213
- const isEndDateValid = (0, _momentTimezone.default)(endDate).isSameOrBefore(today);
214
- const dateRange = startDate && endDate && isStartDateValid && isEndDateValid && (0, _momentTimezone.default)(endDate).isSameOrAfter(startDate) ? {
215
- startDate,
216
- endDate
217
- } : {
218
- startDate: undefined,
219
- endDate: undefined
220
- };
221
- let startMonth = dateRange.startDate ? getMonthStartDate(dateRange.startDate) : getMonthStartDate(getSubtractedDate(today, 1, 'M'));
222
- const endMonth = dateRange.endDate ? getMonthStartDate(dateRange.endDate) : getMonthStartDate(today);
223
- if ((0, _momentTimezone.default)(startMonth).isSame((0, _momentTimezone.default)(endMonth), 'M')) {
224
- startMonth = getSubtractedDate(endMonth, 1, 'M');
225
- }
226
- const monthRange = {
227
- rangeStartMonth: startMonth,
228
- rangeEndMonth: endMonth
229
- };
230
- return {
231
- dateRange,
232
- monthRange,
233
- timezone: timezone ?? currentTimezone
234
- };
235
- };
236
- exports.getInitialDates = getInitialDates;
@@ -1,245 +0,0 @@
1
- // @flow strict
2
-
3
- // $FlowFixMe - strict types for date-fns
4
- import formatISO from 'date-fns/formatISO';
5
- // $FlowFixMe - strict types for date-fns
6
- import parseISO from 'date-fns/parseISO';
7
- import lodash from 'lodash';
8
- // $FlowFixMe[untyped-import]
9
- import moment from 'moment-timezone';
10
-
11
- import type {MenuOption} from '../Menu';
12
-
13
- import {TIMEZONES} from './timezones';
14
-
15
-
16
- export type TimeUnit = 'years' | 'months' | 'days' | 'd' | 'M' | 'y';
17
- export type DateRange = {startDate?: string, endDate?: string};
18
- export type DateRangeWithTimezone = {|
19
- ...DateRange,
20
- startDateUTC?: string,
21
- endDateUTC?: string,
22
- timezone?: string,
23
- |};
24
-
25
- type MonthRange = {rangeStartMonth: string, rangeEndMonth: string};
26
- type SelectedDateRangeTimezone = {
27
- dateRange: DateRange,
28
- monthRange: MonthRange,
29
- timezone: string,
30
- };
31
-
32
- export const NAVIGATION_ACTION = Object.freeze({
33
- NEXT: 'next',
34
- PREV: 'prev',
35
- });
36
- export const MARKERS = Object.freeze({
37
- DATE_RANGE_START: 'FIRST',
38
- DATE_RANGE_END: 'SECOND',
39
- });
40
- export const WEEKDAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
41
- export const MONTHS = [
42
- {key: '0', label: 'Jan'},
43
- {key: '1', label: 'Feb'},
44
- {key: '2', label: 'Mar'},
45
- {key: '3', label: 'Apr'},
46
- {key: '4', label: 'May'},
47
- {key: '5', label: 'Jun'},
48
- {key: '6', label: 'Jul'},
49
- {key: '7', label: 'Aug'},
50
- {key: '8', label: 'Sep'},
51
- {key: '9', label: 'Oct'},
52
- {key: '10', label: 'Nov'},
53
- {key: '11', label: 'Dec'},
54
- ];
55
-
56
- export const wrangleMoment = (date?: string | Date): Date => {
57
- if (date instanceof Date) {
58
- return date;
59
- } else if (!date) {
60
- return new Date();
61
- }
62
- return date instanceof moment ? date.toDate() : parseISO(date);
63
- };
64
-
65
- export const formatIsoDate = (date?: string | Date): string =>
66
- formatISO(wrangleMoment(date), {
67
- representation: 'date',
68
- });
69
-
70
- export const isStartOfRange = ({startDate}: DateRange, date: string): boolean =>
71
- Boolean(startDate) && moment(date).isSame(moment(startDate), 'd');
72
-
73
- export const isEndOfRange = ({endDate}: DateRange, date: string): boolean =>
74
- Boolean(endDate) && moment(date).isSame(moment(endDate), 'd');
75
-
76
- export const inDateRange = (
77
- {startDate, endDate}: DateRange,
78
- date: string,
79
- ): boolean => {
80
- if (startDate && endDate) {
81
- const momentDay = moment(date);
82
- const momentStartDate = moment(startDate);
83
- const momentEndDate = moment(endDate);
84
- return momentDay.isBetween(momentStartDate, momentEndDate, null, '[]');
85
- }
86
- return false;
87
- };
88
-
89
- export const isStartDateEndDateSame = ({
90
- startDate,
91
- endDate,
92
- }: DateRange): boolean => {
93
- if (startDate && endDate) {
94
- return moment(startDate).isSame(moment(endDate), 'd');
95
- }
96
- return false;
97
- };
98
-
99
- const getMonthAndYear = (date) => {
100
- const momentDate = date ? moment(date) : moment();
101
- return [momentDate.month(), momentDate.year()];
102
- };
103
-
104
- export const getDaysInMonth = (date: string): Array<Array<string>> => {
105
- const startWeek = moment(date).startOf('month').startOf('week');
106
- const endWeek = moment(date).endOf('month').endOf('week');
107
- const days = [],
108
- current = startWeek;
109
-
110
- while (current.isBefore(endWeek)) {
111
- days.push(current.clone().format('YYYY-MM-DD'));
112
- current.add(1, 'd');
113
- }
114
- const daysInChunks = lodash.chunk(days, 7);
115
-
116
- // if total rows in calendar are 5 add one more week to the calendar
117
- if (daysInChunks.length === 5) {
118
- const nextWeek = getAddedDate(endWeek, WEEKDAYS.length, 'd');
119
- const extraDays = [];
120
- while (current.isSameOrBefore(nextWeek)) {
121
- extraDays.push(current.clone().format('YYYY-MM-DD'));
122
- current.add(1, 'd');
123
- }
124
- daysInChunks.push(extraDays);
125
- }
126
-
127
- return daysInChunks;
128
- };
129
-
130
- export const getAddedDate = (
131
- date: string,
132
- addCount: number,
133
- timeUnit: TimeUnit,
134
- ): string => formatIsoDate(moment(date).add(addCount, timeUnit));
135
-
136
- export const getSubtractedDate = (
137
- date: string,
138
- subtractCount: number,
139
- timeUnit: TimeUnit,
140
- ): string => formatIsoDate(moment(date).subtract(subtractCount, timeUnit));
141
-
142
- export const getMonthStartDate = (date: string): string =>
143
- formatIsoDate(moment(date).startOf('M'));
144
-
145
- export const addTimezone = (date: string, timezone: string): string =>
146
- moment.tz(date, timezone).startOf('d').utc().toISOString();
147
-
148
- export const getTimezones = (): Array<MenuOption> =>
149
- Object.keys(TIMEZONES).reduce((menuOptions, key) => {
150
- menuOptions.push({
151
- key,
152
- label: TIMEZONES[key],
153
- });
154
- return menuOptions;
155
- }, []);
156
-
157
- export const generateAvailableYears = (
158
- count: number,
159
- firstCalendarDate: string,
160
- secondCalendarDate: string,
161
- marker: $Values<typeof MARKERS>,
162
- ): Array<MenuOption> => {
163
- const startYear = moment().year() - count + 1;
164
- const firstCalendarYear = moment(firstCalendarDate).add(1, 'M').year();
165
- const secondCalendarYear = moment(secondCalendarDate).subtract(1, 'M').year();
166
-
167
- const isWithinRange = (year: number) =>
168
- marker === MARKERS.DATE_RANGE_START
169
- ? year <= secondCalendarYear
170
- : year >= firstCalendarYear;
171
-
172
- return Array.from({length: count}, (_, index) => {
173
- const year = startYear + index;
174
- return {
175
- key: year.toString(),
176
- label: year.toString(),
177
- disabled: !isWithinRange(year),
178
- };
179
- }).filter(({disabled}) => !disabled);
180
- };
181
-
182
- export const getAvailableMonths = (
183
- Months: Array<MenuOption>,
184
- first: string,
185
- second: string,
186
- marker: $Values<typeof MARKERS>,
187
- ): Array<MenuOption> => {
188
- const [firstMonth, firstYear] = getMonthAndYear(first);
189
- const [secondMonth, secondYear] = getMonthAndYear(second);
190
- const [currentMonth, currentYear] = getMonthAndYear();
191
-
192
- return Months.filter((month: MenuOption) => {
193
- const isSameYear = firstYear === secondYear;
194
- const isMonthBeforeFirstMonth = month.key <= firstMonth;
195
- const isMonthAfterCurrentMonth =
196
- currentYear === secondYear && month.key > currentMonth;
197
-
198
- if (marker === MARKERS.DATE_RANGE_START) {
199
- return !(isSameYear && month.key >= secondMonth);
200
- } else {
201
- return !(
202
- (isSameYear && isMonthBeforeFirstMonth) ||
203
- isMonthAfterCurrentMonth
204
- );
205
- }
206
- });
207
- };
208
-
209
- export const getInitialDates = (
210
- selectedDateRange: DateRangeWithTimezone,
211
- ): SelectedDateRangeTimezone => {
212
- const today = formatIsoDate();
213
- const currentTimezone = moment.tz.guess();
214
- const {startDate, endDate, timezone} = selectedDateRange;
215
-
216
- const isStartDateValid = moment(startDate).isSameOrBefore(today);
217
- const isEndDateValid = moment(endDate).isSameOrBefore(today);
218
-
219
- const dateRange =
220
- startDate &&
221
- endDate &&
222
- isStartDateValid &&
223
- isEndDateValid &&
224
- moment(endDate).isSameOrAfter(startDate)
225
- ? {startDate, endDate}
226
- : {startDate: undefined, endDate: undefined};
227
-
228
- let startMonth = dateRange.startDate
229
- ? getMonthStartDate(dateRange.startDate)
230
- : getMonthStartDate(getSubtractedDate(today, 1, 'M'));
231
- const endMonth = dateRange.endDate
232
- ? getMonthStartDate(dateRange.endDate)
233
- : getMonthStartDate(today);
234
-
235
- if (moment(startMonth).isSame(moment(endMonth), 'M')) {
236
- startMonth = getSubtractedDate(endMonth, 1, 'M');
237
- }
238
-
239
- const monthRange = {rangeStartMonth: startMonth, rangeEndMonth: endMonth};
240
- return {
241
- dateRange,
242
- monthRange,
243
- timezone: timezone ?? currentTimezone,
244
- };
245
- };