caltils
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -5,10 +5,8 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var dateFns = require('date-fns'); | ||
var moment = _interopDefault(require('moment')); | ||
function getWeeks(date, options) { | ||
if (options === void 0) { | ||
options = { | ||
format: 'MM/DD/YYYY' | ||
format: 'MM/dd/yyyy' | ||
}; | ||
@@ -19,3 +17,3 @@ } | ||
throw new Error('A date is required'); | ||
} else if (!moment(date).isValid()) { | ||
} else if (!dateFns.isValid(date)) { | ||
console.warn("Invalid date - setting to today", date); | ||
@@ -25,5 +23,5 @@ date = new Date(); | ||
var daysInMonth = moment(date).daysInMonth(); | ||
var day = moment(date).startOf('month'); | ||
var offset = day.day(); | ||
var daysInMonth = dateFns.getDaysInMonth(date); | ||
var day = dateFns.startOfMonth(date); | ||
var offset = dateFns.getDay(day); | ||
var numOfWeeks = Math.ceil((daysInMonth + offset) / 7); // @ts-ignore | ||
@@ -40,11 +38,11 @@ | ||
for (var i = offset; i > 0; i--) { | ||
var offsetDay = day.clone().subtract(i, 'd'); | ||
var offsetDay = dateFns.subDays(day, i); | ||
firstWeek.push({ | ||
date: offsetDay.toDate(), | ||
dayOfMonth: offsetDay.date(), | ||
isWeekendDay: offsetDay.isoWeekday() > 5, | ||
date: offsetDay, | ||
dayOfMonth: dateFns.getDate(offsetDay), | ||
isWeekendDay: dateFns.getISODay(offsetDay) > 5, | ||
isPreviousMonth: true, | ||
isNextMonth: false, | ||
isToday: false, | ||
formattedDate: offsetDay.format(options.format) | ||
formattedDate: dateFns.format(offsetDay, options.format) | ||
}); | ||
@@ -56,11 +54,11 @@ } | ||
week.push({ | ||
date: day.toDate(), | ||
dayOfMonth: day.date(), | ||
date: day, | ||
dayOfMonth: dateFns.getDate(day), | ||
isPreviousMonth: false, | ||
isToday: day.isSame(current, 'day'), | ||
isNextMonth: !day.isSame(date, 'month'), | ||
isWeekendDay: day.isoWeekday() > 5, | ||
formattedDate: day.format(options.format) | ||
isToday: dateFns.isSameDay(day, current), | ||
isNextMonth: !dateFns.isSameMonth(day, date), | ||
isWeekendDay: dateFns.getISODay(day) > 5, | ||
formattedDate: dateFns.format(day, options.format) | ||
}); | ||
day.add(1, 'd'); | ||
day = dateFns.addDays(day, 1); | ||
} | ||
@@ -67,0 +65,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("moment"))&&"object"==typeof e&&"default"in e?e.default:e;exports.getWeeks=function(e,a){if(void 0===a&&(a={format:"MM/DD/YYYY"}),!e)throw new Error("A date is required");t(e).isValid()||(console.warn("Invalid date - setting to today",e),e=new Date);for(var o=t(e).daysInMonth(),r=t(e).startOf("month"),d=r.day(),n=Math.ceil((o+d)/7),i=Array.apply(null,{length:n}).map((function(){return[]})),s=new Date,f=i[0],u=d;u>0;u--){var y=r.clone().subtract(u,"d");f.push({date:y.toDate(),dayOfMonth:y.date(),isWeekendDay:y.isoWeekday()>5,isPreviousMonth:!0,isNextMonth:!1,isToday:!1,formattedDate:y.format(a.format)})}for(var h=0,m=i[h];h<n;m=i[++h]){for(var l=d;l<7;l++)m.push({date:r.toDate(),dayOfMonth:r.date(),isPreviousMonth:!1,isToday:r.isSame(s,"day"),isNextMonth:!r.isSame(e,"month"),isWeekendDay:r.isoWeekday()>5,formattedDate:r.format(a.format)}),r.add(1,"d");d=0}return i}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("date-fns");exports.getWeeks=function(t,a){if(void 0===a&&(a={format:"MM/dd/yyyy"}),!t)throw new Error("A date is required");e.isValid(t)||(console.warn("Invalid date - setting to today",t),t=new Date);for(var r=e.getDaysInMonth(t),o=e.startOfMonth(t),s=e.getDay(o),n=Math.ceil((r+s)/7),i=Array.apply(null,{length:n}).map((function(){return[]})),d=new Date,y=i[0],f=s;f>0;f--){var u=e.subDays(o,f);y.push({date:u,dayOfMonth:e.getDate(u),isWeekendDay:e.getISODay(u)>5,isPreviousMonth:!0,isNextMonth:!1,isToday:!1,formattedDate:e.format(u,a.format)})}for(var D=0,h=i[D];D<n;h=i[++D]){for(var M=s;M<7;M++)h.push({date:o,dayOfMonth:e.getDate(o),isPreviousMonth:!1,isToday:e.isSameDay(o,d),isNextMonth:!e.isSameMonth(o,t),isWeekendDay:e.getISODay(o)>5,formattedDate:e.format(o,a.format)}),o=e.addDays(o,1);s=0}return i}; | ||
//# sourceMappingURL=caltils.cjs.production.min.js.map |
@@ -1,2 +0,2 @@ | ||
import moment from 'moment'; | ||
import { isValid, getDaysInMonth, startOfMonth, getDay, subDays, getDate, getISODay, format, isSameDay, isSameMonth, addDays } from 'date-fns'; | ||
@@ -6,3 +6,3 @@ function getWeeks(date, options) { | ||
options = { | ||
format: 'MM/DD/YYYY' | ||
format: 'MM/dd/yyyy' | ||
}; | ||
@@ -13,3 +13,3 @@ } | ||
throw new Error('A date is required'); | ||
} else if (!moment(date).isValid()) { | ||
} else if (!isValid(date)) { | ||
console.warn("Invalid date - setting to today", date); | ||
@@ -19,5 +19,5 @@ date = new Date(); | ||
var daysInMonth = moment(date).daysInMonth(); | ||
var day = moment(date).startOf('month'); | ||
var offset = day.day(); | ||
var daysInMonth = getDaysInMonth(date); | ||
var day = startOfMonth(date); | ||
var offset = getDay(day); | ||
var numOfWeeks = Math.ceil((daysInMonth + offset) / 7); // @ts-ignore | ||
@@ -34,11 +34,11 @@ | ||
for (var i = offset; i > 0; i--) { | ||
var offsetDay = day.clone().subtract(i, 'd'); | ||
var offsetDay = subDays(day, i); | ||
firstWeek.push({ | ||
date: offsetDay.toDate(), | ||
dayOfMonth: offsetDay.date(), | ||
isWeekendDay: offsetDay.isoWeekday() > 5, | ||
date: offsetDay, | ||
dayOfMonth: getDate(offsetDay), | ||
isWeekendDay: getISODay(offsetDay) > 5, | ||
isPreviousMonth: true, | ||
isNextMonth: false, | ||
isToday: false, | ||
formattedDate: offsetDay.format(options.format) | ||
formattedDate: format(offsetDay, options.format) | ||
}); | ||
@@ -50,11 +50,11 @@ } | ||
week.push({ | ||
date: day.toDate(), | ||
dayOfMonth: day.date(), | ||
date: day, | ||
dayOfMonth: getDate(day), | ||
isPreviousMonth: false, | ||
isToday: day.isSame(current, 'day'), | ||
isNextMonth: !day.isSame(date, 'month'), | ||
isWeekendDay: day.isoWeekday() > 5, | ||
formattedDate: day.format(options.format) | ||
isToday: isSameDay(day, current), | ||
isNextMonth: !isSameMonth(day, date), | ||
isWeekendDay: getISODay(day) > 5, | ||
formattedDate: format(day, options.format) | ||
}); | ||
day.add(1, 'd'); | ||
day = addDays(day, 1); | ||
} | ||
@@ -61,0 +61,0 @@ |
{ | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -30,3 +30,3 @@ "name": "caltils", | ||
"dependencies": { | ||
"moment": "^2.29.1" | ||
"date-fns": "^2.28.0" | ||
}, | ||
@@ -48,4 +48,4 @@ "devDependencies": { | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
"trailingComma": "none" | ||
} | ||
} |
# Caltils | ||
Utility functions for building calendar components. | ||
Utility functions for building calendar components. This code was based on [ngx-ui/calendar](https://github.com/swimlane/ngx-ui/blob/master/projects/swimlane/ngx-ui/src/lib/components/calendar/utils/get-weeks-for-days/get-weeks-for-days.util.ts). | ||
## Usage | ||
``` | ||
yarn add caltils | ||
``` | ||
...then import and invoke... | ||
```ts | ||
import { getWeeks } from 'caltils'; | ||
const weeksWithDays: Day[][] = getWeeks(new Date()); | ||
``` | ||
...then you can render something like this... | ||
```tsx | ||
import React, { FC } from 'react'; | ||
import { getWeeks } from 'caltils'; | ||
interface CalendarProps { | ||
date: Date; | ||
} | ||
const Calendar: FC<CalendarProps> = ({ date }) => { | ||
const weeksWithDays: Day[][] = getWeeks(date); | ||
return ( | ||
<> | ||
{weeks.map((week, i) => ( | ||
<div> | ||
{week.map((day, ii) => ( | ||
<button>{day.dayOfMonth}</button> | ||
)} | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; | ||
``` | ||
## API | ||
@@ -5,0 +44,0 @@ ```ts |
@@ -1,11 +0,12 @@ | ||
import moment from 'moment'; | ||
import { differenceInDays } from 'date-fns'; | ||
import { getWeeks } from './index'; | ||
const expectContigousDays = month => { | ||
const firstDayOfCalendar = moment(month[0][0].date); | ||
const firstDayOfCalendar = month[0][0].date; | ||
let i = 0; | ||
month.map(weeks => | ||
// eslint-disable-next-line array-callback-return | ||
weeks.map(day => { | ||
expect(moment(day.date).diff(firstDayOfCalendar, 'days')).toEqual(i); | ||
expect(differenceInDays(day.date, firstDayOfCalendar)).toEqual(i); | ||
i++; | ||
@@ -40,2 +41,3 @@ }) | ||
const month = getWeeks(date); | ||
// eslint-disable-next-line array-callback-return | ||
month.map(week => { | ||
@@ -42,0 +44,0 @@ expect(week).toHaveLength(7); |
@@ -1,2 +0,14 @@ | ||
import moment from 'moment'; | ||
import { | ||
addDays, | ||
format, | ||
getDay, | ||
getDaysInMonth, | ||
getDate, | ||
getISODay, | ||
isValid, | ||
isSameDay, | ||
isSameMonth, | ||
startOfMonth, | ||
subDays | ||
} from 'date-fns'; | ||
@@ -19,7 +31,7 @@ export interface Day { | ||
date: Date, | ||
options: DayOptions = { format: 'MM/DD/YYYY' } | ||
options: DayOptions = { format: 'MM/dd/yyyy' } | ||
): Day[][] { | ||
if (!date) { | ||
throw new Error('A date is required'); | ||
} else if (!moment(date).isValid()) { | ||
} else if (!isValid(date)) { | ||
console.warn(`Invalid date - setting to today`, date); | ||
@@ -29,5 +41,5 @@ date = new Date(); | ||
const daysInMonth = moment(date).daysInMonth(); | ||
const day = moment(date).startOf('month'); | ||
let offset = day.day(); | ||
const daysInMonth = getDaysInMonth(date); | ||
let day = startOfMonth(date); | ||
let offset = getDay(day); | ||
const numOfWeeks = Math.ceil((daysInMonth + offset) / 7); | ||
@@ -44,11 +56,11 @@ | ||
for (let i = offset; i > 0; i--) { | ||
const offsetDay = day.clone().subtract(i, 'd'); | ||
const offsetDay = subDays(day, i); | ||
firstWeek.push({ | ||
date: offsetDay.toDate(), | ||
dayOfMonth: offsetDay.date(), | ||
isWeekendDay: offsetDay.isoWeekday() > 5, | ||
date: offsetDay, | ||
dayOfMonth: getDate(offsetDay), | ||
isWeekendDay: getISODay(offsetDay) > 5, | ||
isPreviousMonth: true, | ||
isNextMonth: false, | ||
isToday: false, | ||
formattedDate: offsetDay.format(options.format) | ||
formattedDate: format(offsetDay, options.format) | ||
}); | ||
@@ -60,11 +72,11 @@ } | ||
week.push({ | ||
date: day.toDate(), | ||
dayOfMonth: day.date(), | ||
date: day, | ||
dayOfMonth: getDate(day), | ||
isPreviousMonth: false, | ||
isToday: day.isSame(current, 'day'), | ||
isNextMonth: !day.isSame(date, 'month'), | ||
isWeekendDay: day.isoWeekday() > 5, | ||
formattedDate: day.format(options.format) | ||
isToday: isSameDay(day, current), | ||
isNextMonth: !isSameMonth(day, date), | ||
isWeekendDay: getISODay(day) > 5, | ||
formattedDate: format(day, options.format) | ||
}); | ||
day.add(1, 'd'); | ||
day = addDays(day, 1); | ||
} | ||
@@ -71,0 +83,0 @@ offset = 0; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
34011
256
64
+ Addeddate-fns@^2.28.0
+ Added@babel/runtime@7.24.7(transitive)
+ Addeddate-fns@2.30.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removedmoment@^2.29.1
- Removedmoment@2.30.1(transitive)