Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

remove date-fns and refactor to native for replacements

Ed Plato94dca252d11e
diff --git a/package.json b/package.json
index b240e2798..dedd2d62c 100644
--- a/package.json
+++ b/package.json
@@ -158,7 +158,6 @@
"bootstrap": "4.3.1",
"cachai": "^1.0.2",
"canvg-browser": "^1.0.0",
- "date-fns": "^1.29.0",
"file-saver": "^2.0.2",
"geographiclib": "^1.50.0",
"history": "^4.7.2",
diff --git a/web/js/modules/layers/util.js b/web/js/modules/layers/util.js
index f1ba19e68..74e328765 100644
--- a/web/js/modules/layers/util.js
+++ b/web/js/modules/layers/util.js
@@ -16,10 +16,6 @@ import { getPaletteAttributeArray } from '../palettes/util';
import { getVectorStyleAttributeArray } from '../vector-styles/util';
import update from 'immutability-helper';
import util from '../../util/util';
-import closestIndexTo from 'date-fns/closest_index_to';
-import isBefore from 'date-fns/is_before';
-import isEqual from 'date-fns/is_equal';
-import isFirstDayOfMonth from 'date-fns/is_first_day_of_month';
export function getOrbitTrackTitle(def) {
if (def.daynight && def.track) {
@@ -63,28 +59,41 @@ export function nearestInterval(def, date) {
*/
export function prevDateInDateRange(def, date, dateArray) {
const closestAvailableDates = [];
- const currentDate = new Date(date.getTime());
- const currentDateOffsetCheck = new Date(currentDate.getTime() + (currentDate.getTimezoneOffset() * 60000));
+ const currentDateValue = date.getTime();
+ const currentDate = new Date(currentDateValue);
+ const currentDateOffsetCheck = new Date(currentDateValue + (currentDate.getTimezoneOffset() * 60000));
+
+ const isFirstDayOfMonth = currentDateOffsetCheck.getDate() === 1;
+ const isFirstDayOfYear = currentDateOffsetCheck.getMonth() === 0;
+
+ const isMonthPeriod = def.period === 'monthly';
+ const isYearPeriod = def.period === 'yearly';
if (!dateArray ||
- (def.period === 'monthly' && isFirstDayOfMonth(currentDateOffsetCheck)) ||
- (def.period === 'yearly' && (currentDateOffsetCheck.getDate() === 1 && currentDateOffsetCheck.getMonth() === 0))) {
+ (isMonthPeriod && isFirstDayOfMonth) ||
+ (isYearPeriod && isFirstDayOfMonth && isFirstDayOfYear)) {
return date;
}
+ // populate closestAvailableDates if rangeDate is before or equal to input date
lodashEach(dateArray, (rangeDate) => {
- if (isBefore(rangeDate, currentDate) || isEqual(rangeDate, currentDate)) {
+ const rangeDateValue = rangeDate.getTime();
+ const isRangeDateBefore = rangeDateValue < currentDateValue;
+ const isRangeDateEqual = rangeDateValue === currentDateValue;
+
+ if (isRangeDateBefore || isRangeDateEqual) {
closestAvailableDates.push(rangeDate);
}
});
// use closest date index to find closest date in filtered closestAvailableDates
- const closestDateIndex = closestIndexTo(currentDate, closestAvailableDates);
+ const closestDateIndex = util.closestToIndex(closestAvailableDates, currentDateValue);
const closestDate = closestAvailableDates[closestDateIndex];
// check for potential next date in function passed dateArray
- const nextClosestDate = dateArray[closestDateIndex + 1];
- return { previous: closestDate ? new Date(closestDate.getTime()) : date, next: nextClosestDate || null };
+ const next = dateArray[closestDateIndex + 1] || null;
+ const previous = closestDate ? new Date(closestDate.getTime()) : date;
+ return { previous, next };
};
/**
diff --git a/web/js/redux-location-state-customs.js b/web/js/redux-location-state-customs.js
index 8306dca18..413214d4a 100644
--- a/web/js/redux-location-state-customs.js
+++ b/web/js/redux-location-state-customs.js
@@ -4,7 +4,6 @@ import {
createObjectFromConfig,
isEqual
} from 'redux-location-state/lib/helpers';
-import isEqualDate from 'date-fns/is_equal';
import { typeHandles } from 'redux-location-state/lib/typeHandles';
import { ENCODING_EXCEPTIONS } from './modules/link/constants';
export const LOCATION_POP_ACTION = 'REDUX-LOCATION-POP-ACTION';
@@ -49,10 +48,14 @@ export function stateToParams(initialState, currentState, location) {
type
} = pathConfig[curr];
let currentItemState = get(currentState, stateKey);
- let isDefault;
+ let isDefault = false;
// check if the date is the same as the one in initial value
if (type === 'date') {
- isDefault = isEqualDate(initialValue, currentItemState);
+ if (initialValue && currentItemState) {
+ const initialValueMS = initialValue.getTime();
+ const currentItemStateMS = currentItemState.getTime();
+ isDefault = initialValueMS === currentItemStateMS;
+ }
} else {
// if an empty object, make currentItemState undefined
if (
diff --git a/web/js/util/util.js b/web/js/util/util.js
index 3db8590ac..fd8ef8bdd 100644
--- a/web/js/util/util.js
+++ b/web/js/util/util.js
@@ -1111,6 +1111,27 @@ export default (function(self) {
return minuteDiff;
};
+ /**
+ * Find closest index for currentDateValue from array of dates
+ * @param {Array} dateArray | Array of date objects
+ * @param {Number} currentDateValue | Number of milliseconds from date object
+ * @return {Number}
+ */
+ self.closestToIndex = (dateArray, currentDateValue) => {
+ let closestDateIndex;
+ let minDistance;
+ dateArray.forEach((date, index) => {
+ const dateValue = date.getTime();
+ var distance = Math.abs(currentDateValue - dateValue);
+ if (closestDateIndex === undefined || distance < minDistance) {
+ closestDateIndex = index;
+ minDistance = distance;
+ }
+ });
+
+ return closestDateIndex;
+ };
+
/**
* Find index value of string in array
* @param {Array} arra | Array of strings