1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-11-28 23:08:17 +09:00

Bye moment from package.json (#5215)

* Bye moment from package.json

* Use Mapped types for argument type definition
This commit is contained in:
Satsuki Yanagi 2019-07-25 01:36:48 +09:00 committed by syuilo
parent fd2ae6d3cf
commit 88f5e8e8e2
6 changed files with 59 additions and 29 deletions

View File

@ -173,7 +173,6 @@
"minio": "7.0.10", "minio": "7.0.10",
"mocha": "6.2.0", "mocha": "6.2.0",
"moji": "0.5.1", "moji": "0.5.1",
"moment": "2.24.0",
"ms": "2.1.2", "ms": "2.1.2",
"nested-property": "1.0.1", "nested-property": "1.0.1",
"node-fetch": "2.6.0", "node-fetch": "2.6.0",

View File

@ -52,9 +52,11 @@
<script lang="ts"> <script lang="ts">
import Vue from 'vue'; import Vue from 'vue';
import * as moment from 'moment';
import i18n from '../../../i18n'; import i18n from '../../../i18n';
import { erase } from '../../../../../prelude/array'; import { erase } from '../../../../../prelude/array';
import { addTimespan } from '../../../../../prelude/time';
import { formatDateTimeString } from '../../../../../misc/format-time-string';
export default Vue.extend({ export default Vue.extend({
i18n: i18n('common/views/components/poll-editor.vue'), i18n: i18n('common/views/components/poll-editor.vue'),
data() { data() {
@ -62,7 +64,7 @@ export default Vue.extend({
choices: ['', ''], choices: ['', ''],
multiple: false, multiple: false,
expiration: 'infinite', expiration: 'infinite',
atDate: moment().add(1, 'day').toISOString().split('T')[0], atDate: formatDateTimeString(addTimespan(new Date(), 1, 'days'), 'yyyy-MM-dd'),
atTime: '00:00', atTime: '00:00',
after: 0, after: 0,
unit: 'second' unit: 'second'
@ -95,7 +97,7 @@ export default Vue.extend({
get() { get() {
const at = () => { const at = () => {
return moment(`${this.atDate} ${this.atTime}`).valueOf(); return new Date(`${this.atDate} ${this.atTime}`).getTime();
}; };
const after = () => { const after = () => {

View File

@ -20,7 +20,7 @@ function formatLocaleString(date: Date, format: string): string {
}); });
} }
function formatDateTimeString(date: Date, format: string): string { export function formatDateTimeString(date: Date, format: string): string {
return format return format
.replace(/yyyy/g, date.getFullYear().toString()) .replace(/yyyy/g, date.getFullYear().toString())
.replace(/yy/g, date.getFullYear().toString().slice(-2)) .replace(/yy/g, date.getFullYear().toString().slice(-2))

31
src/prelude/time.ts Normal file
View File

@ -0,0 +1,31 @@
const dateTimeIntervals = {
'days': 86400000,
'hours': 3600000,
};
export function DateUTC(time: number[]): Date {
const r = new Date(0);
r.setUTCFullYear(time[0], time[1], time[2]);
if (time[3]) r.setUTCHours(time[3], ...time.slice(4));
return r;
}
export function isTimeSame(a: Date, b: Date): boolean {
return (a.getTime() - b.getTime()) === 0;
}
export function isTimeBefore(a: Date, b: Date): boolean {
return (a.getTime() - b.getTime()) < 0;
}
export function isTimeAfter(a: Date, b: Date): boolean {
return (a.getTime() - b.getTime()) > 0;
}
export function addTimespan(x: Date, value: number, span: keyof typeof dateTimeIntervals): Date {
return new Date(x.getTime() + (value * dateTimeIntervals[span]));
}
export function subtractTimespan(x: Date, value: number, span: keyof typeof dateTimeIntervals): Date {
return new Date(x.getTime() - (value * dateTimeIntervals[span]));
}

View File

@ -4,18 +4,16 @@
* Tests located in test/chart * Tests located in test/chart
*/ */
import * as moment from 'moment';
import * as nestedProperty from 'nested-property'; import * as nestedProperty from 'nested-property';
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import Logger from '../logger'; import Logger from '../logger';
import { Schema } from '../../misc/schema'; import { Schema } from '../../misc/schema';
import { EntitySchema, getRepository, Repository, LessThan, MoreThanOrEqual } from 'typeorm'; import { EntitySchema, getRepository, Repository, LessThan, MoreThanOrEqual } from 'typeorm';
import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error'; import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
import { DateUTC, isTimeSame, isTimeBefore, subtractTimespan } from '../../prelude/time';
const logger = new Logger('chart', 'white', process.env.NODE_ENV !== 'test'); const logger = new Logger('chart', 'white', process.env.NODE_ENV !== 'test');
const utc = moment.utc;
export type Obj = { [key: string]: any }; export type Obj = { [key: string]: any };
export type DeepPartial<T> = { export type DeepPartial<T> = {
@ -131,8 +129,8 @@ export default abstract class Chart<T extends Record<string, any>> {
} }
@autobind @autobind
private static momentToTimestamp(x: moment.Moment): Log['date'] { private static dateToTimestamp(x: Date): Log['date'] {
return x.unix(); return Math.floor(x.getTime() / 1000);
} }
@autobind @autobind
@ -215,12 +213,12 @@ export default abstract class Chart<T extends Record<string, any>> {
@autobind @autobind
private getCurrentDate(): [number, number, number, number] { private getCurrentDate(): [number, number, number, number] {
const now = moment().utc(); const now = new Date();
const y = now.year(); const y = now.getUTCFullYear();
const m = now.month(); const m = now.getUTCMonth();
const d = now.date(); const d = now.getUTCDate();
const h = now.hour(); const h = now.getUTCHours();
return [y, m, d, h]; return [y, m, d, h];
} }
@ -242,14 +240,14 @@ export default abstract class Chart<T extends Record<string, any>> {
const [y, m, d, h] = this.getCurrentDate(); const [y, m, d, h] = this.getCurrentDate();
const current = const current =
span == 'day' ? utc([y, m, d]) : span == 'day' ? DateUTC([y, m, d]) :
span == 'hour' ? utc([y, m, d, h]) : span == 'hour' ? DateUTC([y, m, d, h]) :
null as never; null as never;
// 現在(今日または今のHour)のログ // 現在(今日または今のHour)のログ
const currentLog = await this.repository.findOne({ const currentLog = await this.repository.findOne({
span: span, span: span,
date: Chart.momentToTimestamp(current), date: Chart.dateToTimestamp(current),
...(group ? { group: group } : {}) ...(group ? { group: group } : {})
}); });
@ -290,7 +288,7 @@ export default abstract class Chart<T extends Record<string, any>> {
log = await this.repository.save({ log = await this.repository.save({
group: group, group: group,
span: span, span: span,
date: Chart.momentToTimestamp(current), date: Chart.dateToTimestamp(current),
...Chart.convertObjectToFlattenColumns(data) ...Chart.convertObjectToFlattenColumns(data)
}); });
} catch (e) { } catch (e) {
@ -358,8 +356,8 @@ export default abstract class Chart<T extends Record<string, any>> {
const [y, m, d, h] = this.getCurrentDate(); const [y, m, d, h] = this.getCurrentDate();
const gt = const gt =
span == 'day' ? utc([y, m, d]).subtract(range, 'days') : span == 'day' ? subtractTimespan(DateUTC([y, m, d]), range, 'days') :
span == 'hour' ? utc([y, m, d, h]).subtract(range, 'hours') : span == 'hour' ? subtractTimespan(DateUTC([y, m, d, h]), range, 'hours') :
null as never; null as never;
// ログ取得 // ログ取得
@ -367,7 +365,7 @@ export default abstract class Chart<T extends Record<string, any>> {
where: { where: {
group: group, group: group,
span: span, span: span,
date: MoreThanOrEqual(Chart.momentToTimestamp(gt)) date: MoreThanOrEqual(Chart.dateToTimestamp(gt))
}, },
order: { order: {
date: -1 date: -1
@ -392,13 +390,13 @@ export default abstract class Chart<T extends Record<string, any>> {
} }
// 要求された範囲の最も古い箇所に位置するログが存在しなかったら // 要求された範囲の最も古い箇所に位置するログが存在しなかったら
} else if (!utc(logs[logs.length - 1].date * 1000).isSame(gt)) { } else if (!isTimeSame(new Date(logs[logs.length - 1].date * 1000), gt)) {
// 要求された範囲の最も古い箇所時点での最も新しいログを持ってきて末尾に追加する // 要求された範囲の最も古い箇所時点での最も新しいログを持ってきて末尾に追加する
// (隙間埋めできないため) // (隙間埋めできないため)
const outdatedLog = await this.repository.findOne({ const outdatedLog = await this.repository.findOne({
group: group, group: group,
span: span, span: span,
date: LessThan(Chart.momentToTimestamp(gt)) date: LessThan(Chart.dateToTimestamp(gt))
}, { }, {
order: { order: {
date: -1 date: -1
@ -415,18 +413,18 @@ export default abstract class Chart<T extends Record<string, any>> {
// 整形 // 整形
for (let i = (range - 1); i >= 0; i--) { for (let i = (range - 1); i >= 0; i--) {
const current = const current =
span == 'day' ? utc([y, m, d]).subtract(i, 'days') : span == 'day' ? subtractTimespan(DateUTC([y, m, d]), i, 'days') :
span == 'hour' ? utc([y, m, d, h]).subtract(i, 'hours') : span == 'hour' ? subtractTimespan(DateUTC([y, m, d, h]), i, 'hours') :
null as never; null as never;
const log = logs.find(l => utc(l.date * 1000).isSame(current)); const log = logs.find(l => isTimeSame(new Date(l.date * 1000), current));
if (log) { if (log) {
const data = Chart.convertFlattenColumnsToObject(log as Record<string, any>); const data = Chart.convertFlattenColumnsToObject(log as Record<string, any>);
chart.unshift(data); chart.unshift(data);
} else { } else {
// 隙間埋め // 隙間埋め
const latest = logs.find(l => utc(l.date * 1000).isBefore(current)); const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current));
const data = latest ? Chart.convertFlattenColumnsToObject(latest as Record<string, any>) : null; const data = latest ? Chart.convertFlattenColumnsToObject(latest as Record<string, any>) : null;
chart.unshift(this.getNewLog(data)); chart.unshift(this.getNewLog(data));
} }

View File

@ -7354,7 +7354,7 @@ moment-timezone@^0.5.25:
dependencies: dependencies:
moment ">= 2.9.0" moment ">= 2.9.0"
moment@2.24.0, "moment@>= 2.9.0": "moment@>= 2.9.0":
version "2.24.0" version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b" resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg== integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==