Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions MomentChanges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Remove moment.js

see https://github.com/visjs/vis-timeline/issues/672

## Refactor

* refactor `DateUtil.toScreen` to not use `moment`.
* use `date-utils.getDayOfYear()` instead of moment's `dayOfYear()` everywhere.
* use `date-utils.setDayOfYear()` instead of moment's `dayOfYear()` everywhere.

## Internal Changes

### lib/timeline/DateUtil.js

* remove `moment` parameter from `convertHiddenOptions` function.
* remove `moment` parameter from the `stepOverHiddenDates` function.
* remove `moment` parameter from the `correctTimeForHidden` function.
* remove `moment` parameter from the `getHiddenDurationBefore` function.

## Breaking Changes

*none, so far*
175 changes: 175 additions & 0 deletions lib/date-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/**
* Returns the day of the year for a given date.
*
* @see https://momentjs.com/docs/#/get-set/day-of-year/
* @see https://github.com/you-dont-need/You-Dont-Need-Momentjs#day-of-year
* @see https://stackoverflow.com/a/8619946/722162
*
* @param {Date} date
* @returns {number} 1-366
*/
function getDayOfYear(date) {
if (!date) return NaN;

// check if input is a Date object
if (Object.prototype.toString.call(date) !== "[object Date]") return NaN;

var start = new Date(date.getFullYear(), 0, 0);
var offset = start.getTimezoneOffset() - date.getTimezoneOffset();
var diff = date - start + offset * 60 * 1000;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
return day;
}

/**
* Sets the given date to the given day of the year.
*
* @see https://momentjs.com/docs/#/get-set/day-of-year/
* @see https://stackoverflow.com/a/4049020/722162
*
* @param {Date} date
* @param {number} dayOfYear 1-366
* @returns {Date} the same date object that was provided in 'date'
*/
function setDayOfYear(date, dayOfYear) {
if (
date === 0 ||
date === null ||
date === undefined ||
Object.prototype.toString.call(date) !== "[object Date]" ||
date.toString && date.toString() === 'Invalid Date'
) {
throw new Error("Invalid date value");
}

if (dayOfYear < 1 || dayOfYear > 366) {
throw new RangeError(
`dayOfYear must between 1 and 366, was "${dayOfYear}"`
);
}

date.setMonth(0);
date.setDate(dayOfYear);
return date;
}

/**
* Adds the given amount of time to the given date.
*
* @see https://momentjs.com/docs/#/durations/add/
*
* @param {Date} date
* @param {number} amount
* @param {String} unitString
* @returns {Date} the same date object that was provided in 'date'
*/
function add(date, amount, unitString) {
let unit = unitString.toLowerCase();
if (unit.endsWith('s')) unit = unit.slice(0, -1);

switch (unit) {
case 'year':
date.setFullYear(date.getFullYear() + amount);
break;
case 'month':
date.setMonth(date.getMonth() + amount);
break;
case 'week':
date.setDate(date.getDate() + amount * 7);
break;
case 'day':
date.setDate(date.getDate() + amount);
break;
case 'hour':
date.setHours(date.getHours() + amount);
break;
case 'minute':
date.setMinutes(date.getMinutes() + amount);
break;
case 'second':
date.setSeconds(date.getSeconds() + amount);
break;
case 'millisecond':
date.setMilliseconds(date.getMilliseconds() + amount);
break;
default:
throw new Error(`Unknown unit: ${unit}`);
}

return date;
}

/**
* Adds the given amount of time to the given date.
*
* @see https://momentjs.com/docs/#/durations/subtract/
*
* @param {Date} date
* @param {number} amount
* @param {String} unitString
* @returns {Date} the same date object that was provided in 'date'
*/
function subtract(date, amount, unitString) {
let unit = unitString.toLowerCase();
if (unit.endsWith('s')) unit = unit.slice(0, -1);

switch (unit) {
case 'year':
date.setFullYear(date.getFullYear() - amount);
break;
case 'month':
date.setMonth(date.getMonth() - amount);
break;
case 'week':
date.setDate(date.getDate() - amount * 7);
break;
case 'day':
date.setDate(date.getDate() - amount);
break;
case 'hour':
date.setHours(date.getHours() - amount);
break;
case 'minute':
date.setMinutes(date.getMinutes() - amount);
break;
case 'second':
date.setSeconds(date.getSeconds() - amount);
break;
case 'millisecond':
date.setMilliseconds(date.getMilliseconds() - amount);
break;
default:
throw new Error(`Unknown unit: ${unit}`);
}

return date;
}

/**
* Clone a date.
*
* @param {Date} date
* @returns {Date} a new date object
*/
function clone(date) {
if (
date === 0 ||
date === null ||
date === undefined ||
Object.prototype.toString.call(date) !== "[object Date]" ||
date.toString && date.toString() === 'Invalid Date'
) {
return new Date(date);
}

return new Date(date.getTime());
}

module.exports = {
getDayOfYear,
setDayOfYear,
add,
subtract,
clone
};
5 changes: 4 additions & 1 deletion lib/timeline/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"sourceType": "module",
},

"extends": "eslint:recommended",
"extends": [
"eslint:recommended",
"plugin:you-dont-need-momentjs/recommended"
],

// For the full list of rules, see: http://eslint.org/docs/rules/
"rules": {
Expand Down
2 changes: 1 addition & 1 deletion lib/timeline/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ class Core {
}

if ('hiddenDates' in this.options) {
DateUtil.convertHiddenOptions(this.options.moment, this.body, this.options.hiddenDates);
DateUtil.convertHiddenOptions(this.body, this.options.hiddenDates);
}

if ('clickToUse' in options) {
Expand Down
Loading