Skip to content

Commit

Permalink
Migrate momentjs to dayjs (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
shwin0901 authored Aug 4, 2024
1 parent eb4e3ec commit 059b61d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
"lint": "eslint ."
},
"dependencies": {
"dayjs": "^1.11.12",
"echarts": "^5.5.1",
"moment": "^2.30.1",
"momentjs": "^2.0.0",
"querylib": "./lib",
"svelte-french-toast": "^1.2.0"
},
Expand Down
14 changes: 7 additions & 7 deletions src/routes/stats/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import moment from "moment";
import dayjs from "dayjs";
import {
getSearchStats,
getHistogramEchartDatas,
Expand Down Expand Up @@ -33,7 +33,7 @@
*/
let yearList = [];
let currentYear = moment().year();
let currentYear = dayjs().year();
/**
* @type {{name:string; value: number}[]}
Expand Down Expand Up @@ -91,7 +91,7 @@
function handleChangeYear(y) {
currentYear = y;
searchTime = String(y);
const year = moment(searchTime);
const year = dayjs(searchTime);
const now = year.endOf("year").valueOf();
const yearAgo = year.startOf("year").valueOf();
getEchartData(now, yearAgo);
Expand Down Expand Up @@ -126,17 +126,17 @@
const { timeline } = await Statistics.load();
const min = timeline.reduce((pre, current) => {
return Math.min(pre, current[0]);
}, moment().valueOf());
}, dayjs().valueOf());
const list = [];
for (let i = y; i >= moment(min).year(); i--) {
for (let i = y; i >= dayjs(min).year(); i--) {
list.push(i);
}
return list;
}
onMount(async () => {
const now = moment().valueOf();
const yearAgo = moment().startOf("day").subtract(1, "year").valueOf();
const now = dayjs().valueOf();
const yearAgo = dayjs().startOf("day").subtract(1, "year").valueOf();
dateRange = [now, yearAgo];
yearList = await getYearList(currentYear);
getEchartData(now, yearAgo);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/stats/HeatMapChart.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import {onMount} from 'svelte';
import { init } from "echarts";
import moment from 'moment';
import dayjs from 'dayjs';
/**
* @type {number[]}
*/
Expand All @@ -28,7 +28,7 @@
confine: true,
formatter: (/** @type {{ color:string; data: any }} */ params) => {
return `<div><strong>${params.data[1]}</strong> searches on
${moment(params.data[0]).format('ddd, MMM Do YYYY')}</div>`
${dayjs(params.data[0]).format('ddd, MMM D YYYY')}</div>`
}
},
visualMap: {
Expand Down
14 changes: 7 additions & 7 deletions src/routes/stats/stats.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { STATS_PATTERNS } from "querylib";
import moment from "moment";
import dayjs from "dayjs";

const TOP_CRATE_LENGTH = 15;
const TYPE_OTHER = "other";
Expand Down Expand Up @@ -72,18 +72,18 @@ export function getSearchStats(data) {
const weeksObj = WEEKS_LABEL.reduce((obj, week) => {
obj[week] = 0;
return obj;
}, {});
}, Object.create(null));
const datesObj = makeNumericKeyObject(1, 31);
const hoursObj = makeNumericKeyObject(1, 23);

let typeTotal = 0;
const typeDataObj = Object.create(null);

data.forEach(([t, content]) => {
const time = moment(t);
const time = dayjs(t);
const hour = time.hour();

weeksObj[WEEKS_LABEL[time.weekday()]] += 1;
weeksObj[WEEKS_LABEL[time.day()]] += 1;
datesObj[time.date()] += 1;
if (hour !== 0) {
hoursObj[hour] += 1;
Expand Down Expand Up @@ -131,16 +131,16 @@ export function getHistogramEchartDatas(data) {
const topCratesObj = Object.create(null);

const heatMapArr = data.reduce((pre, [t]) => {
const time = moment(t).format("YYYY-MM-DD");
const time = dayjs(t).format("YYYY-MM-DD");
pre[time] = (pre[time] || 0) + 1;
return pre;
}, {});

for (const [t, , type] of data) {
const time = moment(t);
const time = dayjs(t);
const hour = time.hour();

weeksArr[time.weekday()] += 1;
weeksArr[time.day()] += 1;
dateArr[time.date() - 1] += 1;
if (hour !== 0) {
hourArr[hour - 1] += 1;
Expand Down

0 comments on commit 059b61d

Please sign in to comment.