Skip to content

Commit 0f44a76

Browse files
committed
Uses date-fns instead of moment.js
1 parent ebd95ce commit 0f44a76

File tree

4 files changed

+125
-8
lines changed

4 files changed

+125
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Changed
9+
- Swaps out Moment.js for date-fns to improve blame annotation performance and to reduce the GitLen bundle size (saves ~400kb)
810

911
## [5.6.2] - 2017-10-11
1012
### Fixed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1998,13 +1998,13 @@
19981998
"dependencies": {
19991999
"applicationinsights": "0.21.0",
20002000
"copy-paste": "1.3.0",
2001+
"date-fns": "1.29.0",
20012002
"iconv-lite": "0.4.19",
20022003
"ignore": "3.3.5",
20032004
"lodash.debounce": "4.0.8",
20042005
"lodash.escaperegexp": "4.1.2",
20052006
"lodash.isequal": "4.5.0",
20062007
"lodash.once": "4.1.1",
2007-
"moment": "2.19.1",
20082008
"spawn-rx": "2.0.12",
20092009
"tmp": "0.0.33"
20102010
},

src/system/date.ts

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,121 @@
11
'use strict';
2-
import * as moment from 'moment';
2+
import { distanceInWordsToNow as _fromNow, format as _format } from 'date-fns';
3+
import * as en from 'date-fns/locale/en';
34

45
const MillisecondsPerMinute = 60000; // 60 * 1000
56
const MillisecondsPerDay = 86400000; // 24 * 60 * 60 * 1000
67

8+
// Taken from https://github.com/date-fns/date-fns/blob/601bc8e5708cbaebee5389bdaf51c2b4b33b73c4/src/locale/en/build_distance_in_words_locale/index.js
9+
function buildDistanceInWordsLocale() {
10+
const distanceInWordsLocale: { [key: string]: string | { one: string, other: string } } = {
11+
lessThanXSeconds: {
12+
one: 'less than a second',
13+
other: 'less than {{count}} seconds'
14+
},
15+
16+
xSeconds: {
17+
one: '1 second',
18+
other: '{{count}} seconds'
19+
},
20+
21+
halfAMinute: 'half a minute',
22+
23+
lessThanXMinutes: {
24+
one: 'a few seconds',
25+
other: 'less than {{count}} minutes'
26+
},
27+
28+
xMinutes: {
29+
one: 'a minute',
30+
other: '{{count}} minutes'
31+
},
32+
33+
aboutXHours: {
34+
one: 'an hour',
35+
other: '{{count}} hours'
36+
},
37+
38+
xHours: {
39+
one: 'an hour',
40+
other: '{{count}} hours'
41+
},
42+
43+
xDays: {
44+
one: 'a day',
45+
other: '{{count}} days'
46+
},
47+
48+
aboutXMonths: {
49+
one: 'a month',
50+
other: '{{count}} months'
51+
},
52+
53+
xMonths: {
54+
one: 'a month',
55+
other: '{{count}} months'
56+
},
57+
58+
aboutXYears: {
59+
one: 'a year',
60+
other: '{{count}} years'
61+
},
62+
63+
xYears: {
64+
one: 'a year',
65+
other: '{{count}} years'
66+
},
67+
68+
overXYears: {
69+
one: 'a year',
70+
other: '{{count}} years'
71+
},
72+
73+
almostXYears: {
74+
one: 'a year',
75+
other: '{{count}} years'
76+
}
77+
};
78+
79+
function localize(token: string, count: number, options: any) {
80+
options = options || {};
81+
82+
const result = distanceInWordsLocale[token];
83+
84+
let value: string;
85+
if (typeof result === 'string') {
86+
value = result;
87+
}
88+
else {
89+
if (count === 12 && token === 'xMonths') {
90+
token = 'aboutXYears';
91+
count = 1;
92+
}
93+
94+
if (count === 1) {
95+
value = result.one;
96+
}
97+
else {
98+
value = result.other.replace('{{count}}', count.toString());
99+
}
100+
}
101+
102+
if (!options.addSuffix) return value;
103+
104+
if (options.comparison > 0) return 'in ' + value;
105+
106+
return value + ' ago';
107+
}
108+
109+
return {
110+
localize: localize
111+
};
112+
}
113+
114+
// Monkey patch the locale to customize the wording
115+
(en as any).distanceInWords = buildDistanceInWordsLocale();
116+
117+
const formatterOptions = { addSuffix: true, locale: en };
118+
7119
export namespace Dates {
8120

9121
export interface IDateFormatter {
@@ -28,6 +140,9 @@ export namespace Dates {
28140
}
29141

30142
export function toFormatter(date: Date): IDateFormatter {
31-
return moment(date);
143+
return {
144+
fromNow: () => _fromNow(date, formatterOptions),
145+
format: (format: string) => _format(date, format)
146+
};
32147
}
33148
}

0 commit comments

Comments
 (0)