calendar-base
Advanced tools
Comparing version 1.1.1 to 1.2.0-beta.0
@@ -131,1 +131,2 @@ export interface CalendarDate { | ||
export { Calendar }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,8 +1,183 @@ | ||
'use strict'; | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./calendarbase.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./calendarbase.cjs.development.js') | ||
class Calendar { | ||
constructor({ startDate = null, endDate = null, siblingMonths = false, weekNumbers = false, weekStart = 0 } = {}) { | ||
this.startDate = startDate; | ||
this.endDate = endDate; | ||
this.siblingMonths = siblingMonths; | ||
this.weekNumbers = weekNumbers; | ||
this.weekStart = weekStart; | ||
} | ||
getCalendar(year, month) { | ||
const date = new Date(Date.UTC(year, month, 1, 0, 0, 0, 0)); | ||
year = date.getUTCFullYear(); | ||
month = date.getUTCMonth(); | ||
const calendar = []; | ||
const firstDay = date.getUTCDay(); | ||
const firstDate = -((7 - this.weekStart + firstDay) % 7); | ||
const lastDate = Calendar.daysInMonth(year, month); | ||
const lastDay = (lastDate - firstDate) % 7; | ||
const lastDatePreviousMonth = Calendar.daysInMonth(year, month - 1); | ||
let i = firstDate; | ||
let currentDay; | ||
let currentDate; | ||
let currentDateObject = false; | ||
let currentWeekNumber = null; | ||
let otherMonth; | ||
let otherYear; | ||
const max = lastDate - i + (lastDay !== 0 ? 7 - lastDay : 0) + firstDate; | ||
while (i < max) { | ||
currentDate = i + 1; | ||
currentDay = ((i < 1 ? 7 + i : i) + firstDay) % 7; | ||
if (currentDate < 1 || currentDate > lastDate) { | ||
if (this.siblingMonths) { | ||
if (currentDate < 1) { | ||
otherMonth = month - 1; | ||
otherYear = year; | ||
if (otherMonth < 0) { | ||
otherMonth = 11; | ||
otherYear--; | ||
} | ||
currentDate = lastDatePreviousMonth + currentDate; | ||
} else if (currentDate > lastDate) { | ||
otherMonth = month + 1; | ||
otherYear = year; | ||
if (otherMonth > 11) { | ||
otherMonth = 0; | ||
otherYear++; | ||
} | ||
currentDate = i - lastDate + 1; | ||
} | ||
if (otherMonth !== void 0 && otherYear !== void 0) { | ||
currentDateObject = { | ||
day: currentDate, | ||
weekDay: currentDay, | ||
month: otherMonth, | ||
year: otherYear, | ||
siblingMonth: true | ||
}; | ||
} | ||
} else { | ||
currentDateObject = false; | ||
} | ||
} else { | ||
currentDateObject = { | ||
day: currentDate, | ||
weekDay: currentDay, | ||
month, | ||
year | ||
}; | ||
} | ||
if (currentDateObject && this.weekNumbers) { | ||
if (currentWeekNumber === null) { | ||
currentWeekNumber = Calendar.calculateWeekNumber(currentDateObject); | ||
} else if (currentDay === 1 && currentWeekNumber === 52) { | ||
currentWeekNumber = 1; | ||
} else if (currentDay === 1) { | ||
currentWeekNumber++; | ||
} | ||
currentDateObject.weekNumber = currentWeekNumber; | ||
} | ||
if (currentDateObject && this.startDate) { | ||
currentDateObject.selected = this.isDateSelected(currentDateObject); | ||
} | ||
calendar.push(currentDateObject); | ||
i++; | ||
} | ||
return calendar; | ||
} | ||
isDateSelected(date) { | ||
if (!this.startDate) { | ||
return false; | ||
} | ||
if (date.year === this.startDate.year && date.month === this.startDate.month && date.day === this.startDate.day) { | ||
return true; | ||
} | ||
if (!this.endDate) { | ||
return false; | ||
} | ||
if (date.year === this.startDate.year && date.month === this.startDate.month && date.day < this.startDate.day) { | ||
return false; | ||
} | ||
if (date.year === this.endDate.year && date.month === this.endDate.month && date.day > this.endDate.day) { | ||
return false; | ||
} | ||
if (date.year === this.startDate.year && date.month < this.startDate.month) { | ||
return false; | ||
} | ||
if (date.year === this.endDate.year && date.month > this.endDate.month) { | ||
return false; | ||
} | ||
if (date.year < this.startDate.year) { | ||
return false; | ||
} | ||
if (date.year > this.endDate.year) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
setStartDate(date) { | ||
this.startDate = date; | ||
} | ||
setEndDate(date) { | ||
this.endDate = date; | ||
} | ||
setDate(date) { | ||
return this.setStartDate(date); | ||
} | ||
static diff(dateLeft, dateRight) { | ||
const dateLeftDate = new Date(Date.UTC(dateLeft.year, dateLeft.month, dateLeft.day, 0, 0, 0, 0)); | ||
const dateRightDate = new Date(Date.UTC(dateRight.year, dateRight.month, dateRight.day, 0, 0, 0, 0)); | ||
return Math.ceil((dateLeftDate.getTime() - dateRightDate.getTime()) / 864e5); | ||
} | ||
static interval(dateLeft, dateRight) { | ||
return Math.abs(Calendar.diff(dateLeft, dateRight)) + 1; | ||
} | ||
static compare(dateLeft, dateRight) { | ||
if (typeof dateLeft !== "object" || typeof dateRight !== "object" || dateLeft === null || dateRight === null) { | ||
throw new TypeError("dates must be objects"); | ||
} | ||
if (dateLeft.year < dateRight.year) { | ||
return -1; | ||
} | ||
if (dateLeft.year > dateRight.year) { | ||
return 1; | ||
} | ||
if (dateLeft.month < dateRight.month) { | ||
return -1; | ||
} | ||
if (dateLeft.month > dateRight.month) { | ||
return 1; | ||
} | ||
if (dateLeft.day < dateRight.day) { | ||
return -1; | ||
} | ||
if (dateLeft.day > dateRight.day) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
static daysInMonth(year, month) { | ||
return new Date(year, month + 1, 0).getDate(); | ||
} | ||
static isLeapYear(year) { | ||
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0; | ||
} | ||
static calculateWeekNumber(date) { | ||
const current = new Date(Date.UTC(date.year, date.month, date.day, 0, 0, 0, 0)); | ||
const target = new Date(current.valueOf()); | ||
const dayNr = (current.getUTCDay() + 6) % 7; | ||
target.setUTCDate(target.getUTCDate() - dayNr + 3); | ||
const firstThursday = target.valueOf(); | ||
target.setUTCMonth(0, 1); | ||
if (target.getUTCDay() !== 4) { | ||
target.setUTCMonth(0, 1 + (4 - target.getUTCDay() + 7) % 7); | ||
} | ||
return 1 + Math.ceil((firstThursday - target.getTime()) / 6048e5); | ||
} | ||
} | ||
exports.Calendar = Calendar; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "jest", | ||
"build": "rollup -c", | ||
"prepare": "yarn run build", | ||
"check:types": "tsc --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^8.3.4", | ||
"@types/jest": "^28.1.6", | ||
"@types/node": "^18.6.2", | ||
"@typescript-eslint/eslint-plugin": "^5.31.0", | ||
"@typescript-eslint/parser": "^5.31.0", | ||
"esbuild": "^0.14.51", | ||
"eslint": "^8.20.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"import-sort-style-wes": "^3.0.2", | ||
"jest": "^28.1.3", | ||
"prettier": "2.7.1", | ||
"prettier-plugin-import-sort": "^0.0.7", | ||
"rollup": "^2.77.2", | ||
"rollup-plugin-esbuild": "^4.9.1", | ||
"rollup-plugin-replace": "^2.2.0", | ||
"ts-jest": "^28.0.7", | ||
"ts-node-dev": "^2.0.0", | ||
"typescript": "^4.7.4" | ||
}, | ||
"importSort": { | ||
".js, .jsx": { | ||
"parser": "babylon", | ||
"style": "wes" | ||
}, | ||
".ts, .tsx": { | ||
"parser": "typescript", | ||
"style": "wes" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"name": "calendar-base", | ||
"description": "Base methods for generating calendars using JavaScript.", | ||
"version": "1.1.1", | ||
"version": "1.2.0-beta.0", | ||
"author": "Wes Souza <hey@wes.dev> (https://wes.dev/)", | ||
@@ -17,58 +58,6 @@ "license": "MIT", | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/calendar-base.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"runkitExampleFilename": "examples/node-js/simple-calendar.js", | ||
"scripts": { | ||
"build": "tsdx build --name CalendarBase --format cjs,esm,umd", | ||
"lint": "tsdx lint src test", | ||
"prepare": "yarn run build", | ||
"start": "tsdx watch", | ||
"test": "tsdx test" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/jest": "^25.2.1", | ||
"tsdx": "^0.13.2", | ||
"tslib": "^1.11.1", | ||
"typescript": "^3.8.3" | ||
}, | ||
"peerDependencies": {}, | ||
"eslintConfig": { | ||
"parser": "@typescript-eslint/parser", | ||
"extends": [ | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier/@typescript-eslint", | ||
"plugin:prettier/recommended" | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
"@typescript-eslint/no-empty-function": "off", | ||
"@typescript-eslint/no-use-before-define": "off", | ||
"no-duplicate-imports": "error", | ||
"curly": "error" | ||
} | ||
}, | ||
"jest": { | ||
"globals": { | ||
"ts-jest": { | ||
"diagnostics": { | ||
"warnOnly": true | ||
} | ||
} | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
} | ||
} | ||
"runkitExampleFilename": "examples/node-js/simple-calendar.js" | ||
} |
@@ -197,4 +197,12 @@ # Calendar Base | ||
## Development | ||
This library uses `wes-cli`, which simplifies configuration setup. Instead of | ||
using `yarn install`, you should use `npx wes-cli install`, which will create | ||
all configuration files and run `yarn install`. | ||
[Read more about `wes-cli`.](https://github.com/WesSouza/wes-cli/#wes-install) | ||
## License | ||
MIT, https://wes.dev/LICENSE.txt |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
153726
21
1370
208
0
19
2