ソースを参照

Switches from date-fns to day.js (smaller)

main
Eric Amodio 5年前
コミット
72e60e0a9a
3個のファイルの変更14行の追加137行の削除
  1. +5
    -5
      package-lock.json
  2. +1
    -1
      package.json
  3. +8
    -131
      src/system/date.ts

+ 5
- 5
package-lock.json ファイルの表示

@ -2244,17 +2244,17 @@
"assert-plus": "^1.0.0"
}
},
"date-fns": {
"version": "1.30.1",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.30.1.tgz",
"integrity": "sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw=="
},
"date-now": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz",
"integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=",
"dev": true
},
"dayjs": {
"version": "1.8.12",
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.8.12.tgz",
"integrity": "sha512-7keChCzzjU68sYJpk7kEI1Q9qbJyscyiW0STwEhqDFt+2js9pA/BSzmYE4PRxcMMoUMxNeY0TEMZHqV/JBR4OA=="
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",

+ 1
- 1
package.json ファイルの表示

@ -4985,7 +4985,7 @@
"vscode:prepublish": "npm run reset && npm run bundle"
},
"dependencies": {
"date-fns": "1.30.1",
"dayjs": "1.8.12",
"iconv-lite": "0.4.24",
"lodash-es": "4.17.11",
"vsls": "0.3.1291"

+ 8
- 131
src/system/date.ts ファイルの表示

@ -1,116 +1,10 @@
'use strict';
import { format as _format, distanceInWordsToNow as _fromNow } from 'date-fns';
import * as en from 'date-fns/locale/en';
import * as dayjs from 'dayjs';
import * as advancedFormat from 'dayjs/plugin/advancedFormat';
import * as relativeTime from 'dayjs/plugin/relativeTime';
// Taken from https://github.com/date-fns/date-fns/blob/601bc8e5708cbaebee5389bdaf51c2b4b33b73c4/src/locale/en/build_distance_in_words_locale/index.js
function buildDistanceInWordsLocale() {
const distanceInWordsLocale: { [key: string]: string | { one: string; other: string } } = {
lessThanXSeconds: {
one: 'less than a second',
other: 'less than {{count}} seconds'
},
xSeconds: {
one: '1 second',
other: '{{count}} seconds'
},
halfAMinute: 'half a minute',
lessThanXMinutes: {
one: 'a few seconds',
other: 'less than {{count}} minutes'
},
xMinutes: {
one: 'a minute',
other: '{{count}} minutes'
},
aboutXHours: {
one: 'an hour',
other: '{{count}} hours'
},
xHours: {
one: 'an hour',
other: '{{count}} hours'
},
xDays: {
one: 'a day',
other: '{{count}} days'
},
aboutXMonths: {
one: 'a month',
other: '{{count}} months'
},
xMonths: {
one: 'a month',
other: '{{count}} months'
},
aboutXYears: {
one: 'a year',
other: '{{count}} years'
},
xYears: {
one: 'a year',
other: '{{count}} years'
},
overXYears: {
one: 'a year',
other: '{{count}} years'
},
almostXYears: {
one: 'a year',
other: '{{count}} years'
}
};
function localize(token: string, count: number, options: any) {
options = options || {};
if (count === 12 && token === 'xMonths') {
token = 'aboutXYears';
count = 1;
}
const result = distanceInWordsLocale[token];
let value: string;
if (typeof result === 'string') {
value = result;
}
else if (count === 1) {
value = result.one;
}
else {
value = result.other.replace('{{count}}', count.toString());
}
if (!options.addSuffix) return value;
if (options.comparison > 0) return `in ${value}`;
return `${value} ago`;
}
return {
localize: localize
};
}
// Monkey patch the locale to customize the wording
const patch = en as any;
patch.distanceInWords = buildDistanceInWordsLocale();
const formatterOptions = { addSuffix: true, locale: patch };
dayjs.extend(advancedFormat);
dayjs.extend(relativeTime);
export namespace Dates {
export const MillisecondsPerMinute = 60000; // 60 * 1000
@ -122,28 +16,11 @@ export namespace Dates {
format(format: string): string;
}
export function dateDaysFromNow(date: Date, now: number = Date.now()) {
const startOfDayLeft = startOfDay(now);
const startOfDayRight = startOfDay(date);
const timestampLeft = startOfDayLeft.getTime() - startOfDayLeft.getTimezoneOffset() * MillisecondsPerMinute;
const timestampRight = startOfDayRight.getTime() - startOfDayRight.getTimezoneOffset() * MillisecondsPerMinute;
return Math.round((timestampLeft - timestampRight) / MillisecondsPerDay);
}
export function startOfDay(date: Date | number) {
const newDate = new Date(typeof date === 'number' ? date : date.getTime());
newDate.setHours(0, 0, 0, 0);
return newDate;
}
export function toFormatter(date: Date): DateFormatter {
const wrappedDate = dayjs(date);
return {
fromNow: () => {
return _fromNow(date, formatterOptions);
},
format: (format: string) => _format(date, format)
fromNow: () => wrappedDate.fromNow(),
format: (format: string) => wrappedDate.format(format)
};
}
}

読み込み中…
キャンセル
保存