timeago.js 2.0.4 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/.npmignore +8 -7
  2. package/.travis.yml +2 -2
  3. package/LICENSE +20 -20
  4. package/README.md +189 -176
  5. package/README_zh.md +172 -162
  6. package/bower.json +21 -21
  7. package/demo/index.html +121 -0
  8. package/demo/index.js +25 -16
  9. package/dist/timeago.js +261 -241
  10. package/dist/timeago.locales.min.js +1 -1
  11. package/dist/timeago.min.js +1 -1
  12. package/gulpfile.js +13 -13
  13. package/index.html +185 -181
  14. package/locales/ar.js +26 -45
  15. package/locales/be.js +51 -51
  16. package/locales/bg.js +18 -18
  17. package/locales/ca.js +18 -18
  18. package/locales/da.js +18 -18
  19. package/locales/de.js +18 -18
  20. package/locales/el.js +18 -18
  21. package/locales/en.js +18 -18
  22. package/locales/en_short.js +17 -17
  23. package/locales/es.js +17 -17
  24. package/locales/eu.js +18 -18
  25. package/locales/fa.js +18 -0
  26. package/locales/fi.js +18 -18
  27. package/locales/fr.js +17 -17
  28. package/locales/he.js +18 -18
  29. package/locales/hu.js +18 -18
  30. package/locales/in_ID.js +18 -18
  31. package/locales/it.js +18 -18
  32. package/locales/ja.js +18 -18
  33. package/locales/ko.js +17 -17
  34. package/locales/locales.js +6 -6
  35. package/locales/ml.js +18 -18
  36. package/locales/my.js +18 -0
  37. package/locales/nl.js +18 -18
  38. package/locales/pl.js +34 -35
  39. package/locales/pt_BR.js +18 -18
  40. package/locales/ro.js +29 -29
  41. package/locales/ru.js +51 -51
  42. package/locales/sv.js +18 -18
  43. package/locales/ta.js +17 -17
  44. package/locales/th.js +18 -18
  45. package/locales/tr.js +18 -18
  46. package/locales/uk.js +40 -40
  47. package/locales/vi.js +18 -18
  48. package/locales/zh_CN.js +17 -17
  49. package/locales/zh_TW.js +17 -17
  50. package/package.json +49 -44
  51. package/src/timeago.js +261 -241
  52. package/src/timeago.locales.js +12 -12
  53. package/tests/locales/ar.js +66 -66
  54. package/tests/locales/be.js +63 -63
  55. package/tests/locales/bg.js +63 -63
  56. package/tests/locales/ca.js +64 -64
  57. package/tests/locales/en.js +62 -62
  58. package/tests/locales/en_short.js +62 -62
  59. package/tests/locales/eu.js +63 -63
  60. package/tests/locales/fa.js +63 -0
  61. package/tests/locales/fi.js +43 -43
  62. package/tests/locales/he.js +62 -62
  63. package/tests/locales/in_BG.js +63 -63
  64. package/tests/locales/in_HI.js +63 -63
  65. package/tests/locales/in_ID.js +62 -62
  66. package/tests/locales/ja.js +62 -62
  67. package/tests/locales/ko.js +62 -62
  68. package/tests/locales/my.js +63 -0
  69. package/tests/locales/pl.js +91 -0
  70. package/tests/locales/ro.js +61 -61
  71. package/tests/locales/ru.js +63 -63
  72. package/tests/locales/ta.js +62 -62
  73. package/tests/locales/tr.js +63 -63
  74. package/tests/locales/uk.js +63 -63
  75. package/tests/locales/vi.js +63 -63
  76. package/tests/locales/zh_CN.js +62 -62
  77. package/tests/locales_test.js +51 -51
  78. package/tests/test-builder.js +247 -247
  79. package/tests/test.js +83 -83
  80. package/tests/test_dev.js +44 -44
  81. package/tests/test_ts.ts +7 -0
  82. package/timeago.d.ts +15 -0
  83. package/webpack.config.js +10 -10
@@ -1,247 +1,247 @@
1
- 'use strict';
2
-
3
- const timeagoLib = require('../'),
4
- HOURS_IN_MONTH = 24*7*(365/7/12),
5
- DAYS_IN_WEEK = 7;
6
-
7
- class TimeagoBuilder {
8
- constructor(nowDate, defaultLocale) {
9
- defaultLocale = defaultLocale || 'en';
10
-
11
- /** @private */
12
- this.nowDate = new Date(nowDate);
13
-
14
- /** @private */
15
- this.timeago = new timeagoLib(nowDate);
16
-
17
- this.useLocale(defaultLocale);
18
- }
19
-
20
- /**
21
- * Falls back to `timeago().register` method. Third parameter tells to instantly use registered locale
22
- * @param code
23
- * @param fn
24
- * @param {boolean} use
25
- * @returns {TimeagoBuilder}
26
- */
27
- register(code, fn, use) {
28
- use = use || false;
29
- timeagoLib.register(code, fn);
30
-
31
- if (use) {
32
- this.timeago.setLocale(code);
33
- }
34
-
35
- return this;
36
- }
37
-
38
- /**
39
- * Falls back to `timeago().setLocale` method
40
- * @param code
41
- * @returns {TimeagoBuilder}
42
- */
43
- useLocale(code) {
44
- this.timeago.setLocale(code);
45
-
46
- return this;
47
- }
48
-
49
- /**
50
- * Falls back `timeago.format()` value
51
- * @param [format]
52
- * @returns {string}
53
- */
54
- getFormat(format) {
55
- return this.timeago.format(format || this.nowDate.getTime());
56
- }
57
-
58
- /**
59
- * Adds piece of time in OO way
60
- * @param {{seconds?: number, minutes?: number, hours?: number, days?: number, weeks?: number, months?: number, years?: number}} obj
61
- * @returns {string}
62
- */
63
- add(obj) {
64
- return this.setAndFormat(date => {
65
- if (obj.seconds > 0) this.setSeconds(date, obj.seconds);
66
- if (obj.minutes > 0) this.setMinutes(date, obj.minutes);
67
- if (obj.hours > 0) this.setHours(date, obj.hours);
68
- if (obj.days > 0) this.setDays(date, obj.days);
69
- if (obj.weeks > 0) this.setWeeks(date, obj.weeks);
70
- if (obj.months > 0) this.setMonths(date, obj.months);
71
- if (obj.years > 0) this.setYears(date, obj.years);
72
- });
73
- }
74
-
75
- /**
76
- * Subs piece of time in OO way
77
- * @param {{seconds?: number, minutes?: number, hours?: number, days?: number, weeks?: number, months?: number, years?: number}} obj
78
- * @returns {string}
79
- */
80
- sub(obj) {
81
- return this.setAndFormat(date => {
82
- if (obj.seconds > 0) this.setSeconds(date, -obj.seconds);
83
- if (obj.minutes > 0) this.setMinutes(date, -obj.minutes);
84
- if (obj.hours > 0) this.setHours(date, -obj.hours);
85
- if (obj.days > 0) this.setDays(date, -obj.days);
86
- if (obj.weeks > 0) this.setWeeks(date, -obj.weeks);
87
- if (obj.months > 0) this.setMonths(date, -obj.months);
88
- if (obj.years > 0) this.setYears(date, -obj.years);
89
- });
90
- }
91
-
92
- /**
93
- *
94
- * @param setFn
95
- * @private
96
- * @throws TypeError
97
- */
98
- setAndFormat(setFn) {
99
- if (typeof setFn !== 'function') throw new TypeError('`setFn` must be callable');
100
-
101
- const date = new Date(this.nowDate.getTime()),
102
- result = setFn(date);
103
-
104
- let val;
105
-
106
- if (result instanceof Date) {
107
- val = result.getTime();
108
- } else if (result === parseFloat(result)) {
109
- val = result;
110
- } else {
111
- val = date;
112
- }
113
-
114
- return this.getFormat(val);
115
- }
116
-
117
- }
118
- /**
119
- * Dynamically generates add/sub/set methods
120
- */
121
- ['Seconds', 'Minutes', 'Hours', 'Date', 'Weeks', 'Months', 'FullYear'].forEach(fnName => {
122
- if (fnName === 'Weeks') {
123
- /** @private */
124
- TimeagoBuilder.prototype.setWeeks = function(date, n) {
125
- return date.setDate(date.getDate() + n * DAYS_IN_WEEK);
126
- };
127
- } else if (fnName === 'Months') {
128
- /** @private */
129
- TimeagoBuilder.prototype.setMonths = function(date, n) {
130
- return date.setHours(date.getHours() + n * HOURS_IN_MONTH);
131
- };
132
- } else {
133
- // setSeconds, setMinutes, setHours, setDate, setFullYear
134
- TimeagoBuilder.prototype[`set${fnName}`] = function(date, n) {
135
- return date[`set${fnName}`](date[`get${fnName}`]() + n);
136
- };
137
- }
138
-
139
- // addSeconds, addMinutes, addHours, addDate, addWeeks, addMonths, addFullYear
140
- TimeagoBuilder.prototype[`add${fnName}`] = function(n) {
141
- return this.setAndFormat(date => this[`set${fnName}`](date, n));
142
- };
143
-
144
- // subSeconds, subMinutes, subHours, subDate, subWeeks, subMonths, subFullYear
145
- TimeagoBuilder.prototype[`sub${fnName}`] = function(n) {
146
- return this.setAndFormat(date => this[`set${fnName}`](date, -n));
147
- };
148
- });
149
-
150
- // set aliases for more suitable names
151
- TimeagoBuilder.prototype.addDays = TimeagoBuilder.prototype.addDate;
152
- TimeagoBuilder.prototype.subDays = TimeagoBuilder.prototype.subDate;
153
- /** @private */
154
- TimeagoBuilder.prototype.setDays = TimeagoBuilder.prototype.setDate;
155
- TimeagoBuilder.prototype.addYears = TimeagoBuilder.prototype.addFullYear;
156
- TimeagoBuilder.prototype.subYears = TimeagoBuilder.prototype.subFullYear;
157
- /** @private */
158
- TimeagoBuilder.prototype.setYears = TimeagoBuilder.prototype.setFullYear;
159
-
160
- // exports factory function
161
- exports = module.exports = (nowDate, locale) => new TimeagoBuilder(nowDate, locale);
162
-
163
- // below are for dynamically generated methods
164
-
165
- /**
166
- * @name TimeagoBuilder#addSeconds
167
- * @function
168
- * @param n
169
- */
170
-
171
- /**
172
- * @name TimeagoBuilder#subSeconds
173
- * @function
174
- * @param n
175
- */
176
-
177
- /**
178
- * @name TimeagoBuilder#setSeconds
179
- * @function
180
- * @private
181
- * @param date
182
- * @param n
183
- */
184
-
185
- /**
186
- * @name TimeagoBuilder#addMinutes
187
- * @function
188
- * @param n
189
- */
190
-
191
- /**
192
- * @name TimeagoBuilder#subMinutes
193
- * @function
194
- * @param n
195
- */
196
-
197
- /**
198
- * @name TimeagoBuilder#setMinutes
199
- * @function
200
- * @private
201
- * @param date
202
- * @param n
203
- */
204
-
205
- /**
206
- * @name TimeagoBuilder#addHours
207
- * @function
208
- * @param n
209
- */
210
-
211
- /**
212
- * @name TimeagoBuilder#subHours
213
- * @function
214
- * @param n
215
- */
216
-
217
- /**
218
- * @name TimeagoBuilder#setHours
219
- * @function
220
- * @private
221
- * @param date
222
- * @param n
223
- */
224
-
225
- /**
226
- * @name TimeagoBuilder#addWeeks
227
- * @function
228
- * @param n
229
- */
230
-
231
- /**
232
- * @name TimeagoBuilder#subWeeks
233
- * @function
234
- * @param n
235
- */
236
-
237
- /**
238
- * @name TimeagoBuilder#addMonths
239
- * @function
240
- * @param n
241
- */
242
-
243
- /**
244
- * @name TimeagoBuilder#subMonths
245
- * @function
246
- * @param n
247
- */
1
+ 'use strict';
2
+
3
+ const timeagoLib = require('../'),
4
+ HOURS_IN_MONTH = 24*7*(365/7/12),
5
+ DAYS_IN_WEEK = 7;
6
+
7
+ class TimeagoBuilder {
8
+ constructor(nowDate, defaultLocale) {
9
+ defaultLocale = defaultLocale || 'en';
10
+
11
+ /** @private */
12
+ this.nowDate = new Date(nowDate);
13
+
14
+ /** @private */
15
+ this.timeago = new timeagoLib(nowDate);
16
+
17
+ this.useLocale(defaultLocale);
18
+ }
19
+
20
+ /**
21
+ * Falls back to `timeago().register` method. Third parameter tells to instantly use registered locale
22
+ * @param code
23
+ * @param fn
24
+ * @param {boolean} use
25
+ * @returns {TimeagoBuilder}
26
+ */
27
+ register(code, fn, use) {
28
+ use = use || false;
29
+ timeagoLib.register(code, fn);
30
+
31
+ if (use) {
32
+ this.timeago.setLocale(code);
33
+ }
34
+
35
+ return this;
36
+ }
37
+
38
+ /**
39
+ * Falls back to `timeago().setLocale` method
40
+ * @param code
41
+ * @returns {TimeagoBuilder}
42
+ */
43
+ useLocale(code) {
44
+ this.timeago.setLocale(code);
45
+
46
+ return this;
47
+ }
48
+
49
+ /**
50
+ * Falls back `timeago.format()` value
51
+ * @param [format]
52
+ * @returns {string}
53
+ */
54
+ getFormat(format) {
55
+ return this.timeago.format(format || this.nowDate.getTime());
56
+ }
57
+
58
+ /**
59
+ * Adds piece of time in OO way
60
+ * @param {{seconds?: number, minutes?: number, hours?: number, days?: number, weeks?: number, months?: number, years?: number}} obj
61
+ * @returns {string}
62
+ */
63
+ add(obj) {
64
+ return this.setAndFormat(date => {
65
+ if (obj.seconds > 0) this.setSeconds(date, obj.seconds);
66
+ if (obj.minutes > 0) this.setMinutes(date, obj.minutes);
67
+ if (obj.hours > 0) this.setHours(date, obj.hours);
68
+ if (obj.days > 0) this.setDays(date, obj.days);
69
+ if (obj.weeks > 0) this.setWeeks(date, obj.weeks);
70
+ if (obj.months > 0) this.setMonths(date, obj.months);
71
+ if (obj.years > 0) this.setYears(date, obj.years);
72
+ });
73
+ }
74
+
75
+ /**
76
+ * Subs piece of time in OO way
77
+ * @param {{seconds?: number, minutes?: number, hours?: number, days?: number, weeks?: number, months?: number, years?: number}} obj
78
+ * @returns {string}
79
+ */
80
+ sub(obj) {
81
+ return this.setAndFormat(date => {
82
+ if (obj.seconds > 0) this.setSeconds(date, -obj.seconds);
83
+ if (obj.minutes > 0) this.setMinutes(date, -obj.minutes);
84
+ if (obj.hours > 0) this.setHours(date, -obj.hours);
85
+ if (obj.days > 0) this.setDays(date, -obj.days);
86
+ if (obj.weeks > 0) this.setWeeks(date, -obj.weeks);
87
+ if (obj.months > 0) this.setMonths(date, -obj.months);
88
+ if (obj.years > 0) this.setYears(date, -obj.years);
89
+ });
90
+ }
91
+
92
+ /**
93
+ *
94
+ * @param setFn
95
+ * @private
96
+ * @throws TypeError
97
+ */
98
+ setAndFormat(setFn) {
99
+ if (typeof setFn !== 'function') throw new TypeError('`setFn` must be callable');
100
+
101
+ const date = new Date(this.nowDate.getTime()),
102
+ result = setFn(date);
103
+
104
+ let val;
105
+
106
+ if (result instanceof Date) {
107
+ val = result.getTime();
108
+ } else if (result === parseFloat(result)) {
109
+ val = result;
110
+ } else {
111
+ val = date;
112
+ }
113
+
114
+ return this.getFormat(val);
115
+ }
116
+
117
+ }
118
+ /**
119
+ * Dynamically generates add/sub/set methods
120
+ */
121
+ ['Seconds', 'Minutes', 'Hours', 'Date', 'Weeks', 'Months', 'FullYear'].forEach(fnName => {
122
+ if (fnName === 'Weeks') {
123
+ /** @private */
124
+ TimeagoBuilder.prototype.setWeeks = function(date, n) {
125
+ return date.setDate(date.getDate() + n * DAYS_IN_WEEK);
126
+ };
127
+ } else if (fnName === 'Months') {
128
+ /** @private */
129
+ TimeagoBuilder.prototype.setMonths = function(date, n) {
130
+ return date.setHours(date.getHours() + n * HOURS_IN_MONTH);
131
+ };
132
+ } else {
133
+ // setSeconds, setMinutes, setHours, setDate, setFullYear
134
+ TimeagoBuilder.prototype[`set${fnName}`] = function(date, n) {
135
+ return date[`set${fnName}`](date[`get${fnName}`]() + n);
136
+ };
137
+ }
138
+
139
+ // addSeconds, addMinutes, addHours, addDate, addWeeks, addMonths, addFullYear
140
+ TimeagoBuilder.prototype[`add${fnName}`] = function(n) {
141
+ return this.setAndFormat(date => this[`set${fnName}`](date, n));
142
+ };
143
+
144
+ // subSeconds, subMinutes, subHours, subDate, subWeeks, subMonths, subFullYear
145
+ TimeagoBuilder.prototype[`sub${fnName}`] = function(n) {
146
+ return this.setAndFormat(date => this[`set${fnName}`](date, -n));
147
+ };
148
+ });
149
+
150
+ // set aliases for more suitable names
151
+ TimeagoBuilder.prototype.addDays = TimeagoBuilder.prototype.addDate;
152
+ TimeagoBuilder.prototype.subDays = TimeagoBuilder.prototype.subDate;
153
+ /** @private */
154
+ TimeagoBuilder.prototype.setDays = TimeagoBuilder.prototype.setDate;
155
+ TimeagoBuilder.prototype.addYears = TimeagoBuilder.prototype.addFullYear;
156
+ TimeagoBuilder.prototype.subYears = TimeagoBuilder.prototype.subFullYear;
157
+ /** @private */
158
+ TimeagoBuilder.prototype.setYears = TimeagoBuilder.prototype.setFullYear;
159
+
160
+ // exports factory function
161
+ exports = module.exports = (nowDate, locale) => new TimeagoBuilder(nowDate, locale);
162
+
163
+ // below are for dynamically generated methods
164
+
165
+ /**
166
+ * @name TimeagoBuilder#addSeconds
167
+ * @function
168
+ * @param n
169
+ */
170
+
171
+ /**
172
+ * @name TimeagoBuilder#subSeconds
173
+ * @function
174
+ * @param n
175
+ */
176
+
177
+ /**
178
+ * @name TimeagoBuilder#setSeconds
179
+ * @function
180
+ * @private
181
+ * @param date
182
+ * @param n
183
+ */
184
+
185
+ /**
186
+ * @name TimeagoBuilder#addMinutes
187
+ * @function
188
+ * @param n
189
+ */
190
+
191
+ /**
192
+ * @name TimeagoBuilder#subMinutes
193
+ * @function
194
+ * @param n
195
+ */
196
+
197
+ /**
198
+ * @name TimeagoBuilder#setMinutes
199
+ * @function
200
+ * @private
201
+ * @param date
202
+ * @param n
203
+ */
204
+
205
+ /**
206
+ * @name TimeagoBuilder#addHours
207
+ * @function
208
+ * @param n
209
+ */
210
+
211
+ /**
212
+ * @name TimeagoBuilder#subHours
213
+ * @function
214
+ * @param n
215
+ */
216
+
217
+ /**
218
+ * @name TimeagoBuilder#setHours
219
+ * @function
220
+ * @private
221
+ * @param date
222
+ * @param n
223
+ */
224
+
225
+ /**
226
+ * @name TimeagoBuilder#addWeeks
227
+ * @function
228
+ * @param n
229
+ */
230
+
231
+ /**
232
+ * @name TimeagoBuilder#subWeeks
233
+ * @function
234
+ * @param n
235
+ */
236
+
237
+ /**
238
+ * @name TimeagoBuilder#addMonths
239
+ * @function
240
+ * @param n
241
+ */
242
+
243
+ /**
244
+ * @name TimeagoBuilder#subMonths
245
+ * @function
246
+ * @param n
247
+ */
package/tests/test.js CHANGED
@@ -1,83 +1,83 @@
1
- 'use strict';
2
-
3
- const test = require('tape');
4
- const timeago = require('..');
5
- const fs = require('fs');
6
- const pys = require('pys');
7
- const testBuilder = require('./test-builder');
8
-
9
- test('timeago.js should be tested', t => {
10
- // locale tests #################################################################
11
- // read all the locales test in `tests/locales` dir
12
- fs.readdir('tests/locales', (err, files) => {
13
- files.forEach(file => {
14
- const locale = pys(file)('0:-3');
15
- console.log('\nLocale testcase for ['+ locale +']');
16
- // require in the locales testcases
17
- const tb = testBuilder(Date.now()).register(locale, require('../locales/' + locale), true);
18
- require('./locales/' + locale)(t, tb);
19
- })
20
- });
21
- // end locale tests #############################################################
22
-
23
- // test locale, can set the locale
24
- t.equal(timeago('2016-06-23').format('2016-06-22'), '1 day ago');
25
- t.equal(timeago('2016-06-23').format('2016-06-25'), 'in 2 days');
26
- t.equal(timeago('2016-06-23').format('2016-06-22', 'zh_CN'), '1天前');
27
- t.equal(timeago('2016-06-23').format('2016-06-25', 'zh_CN'), '2天后');
28
-
29
- // test register locale
30
- const timeagoReg = timeago('2016-06-23');
31
- timeago.register('test_local', (number, index) => [
32
- ["just xxx", "right now"],
33
- ["%s seconds xxx", "in %s seconds"],
34
- ["1 minute xxx", "in 1 minute"],
35
- ["%s minutes xxx", "in %s minutes"],
36
- ["1 hour xxx", "in 1 hour"],
37
- ["%s hours xxx", "in %s hours"],
38
- ["1 day xxx", "in 1 day"],
39
- ["%s days xxx", "in %s days"],
40
- ['一周前', '一周后'],
41
- ['%s周前', '%s周后'],
42
- ["1 month xxx", "in 1 month"],
43
- ["%s months xxx", "in %s months"],
44
- ["1 year xxx", "in 1 year"],
45
- ["%s years xxx", "in %s years"]
46
- ][index]
47
- );
48
- t.equal(timeagoReg.format('2016-06-22', 'test_local'), '1 day xxx');
49
-
50
- // testcase for other points
51
- // relative now
52
- t.equal(timeago().format(Date.now() - 11 * 1000 * 60 * 60), '11 hours ago');
53
-
54
- // timestamp is also can work
55
- let current = Date.now();
56
- t.equal(timeago(current, null).format(current - 8 * 1000 * 60 * 60 * 24), '1 week ago');
57
- t.equal(timeago(current, null).format(current - 31536000 * 1000 + 1000), '11 months ago');
58
-
59
- // Date()
60
- current = new Date();
61
- t.equal(timeago(current, null).format(current), 'just now');
62
-
63
-
64
- // test leap year
65
- t.equal(timeago('2016-03-01 12:00:00').format('2016-02-28 12:00:00'), '2 days ago');
66
- t.equal(timeago('2015-03-01 12:00:00').format('2015-02-28 12:00:00'), '1 day ago');
67
-
68
- // test default locale
69
- t.equal(timeago('2016-03-01 12:00:00').format('2016-02-28 12:00:00'), '2 days ago');
70
- t.equal(timeago('2016-03-01 12:00:00', 'zh_CN').format('2016-02-28 12:00:00'), '2天前');
71
-
72
- // test setLocale
73
- const newTimeAgo = timeago('2016-03-01 12:00:00');
74
- t.equal(newTimeAgo.format('2016-02-28 12:00:00'), '2 days ago');
75
- newTimeAgo.setLocale('zh_CN');
76
- t.equal(newTimeAgo.format('2016-02-28 12:00:00'), '2天前');
77
-
78
- const newTimeAgoDefaultLocale = timeago('2016-03-01 12:00:00', 'pl');
79
- t.equal(newTimeAgoDefaultLocale.format('2016-02-28 12:00:00'), '2 days ago');
80
- t.equal(newTimeAgoDefaultLocale.format('2016-02-28 12:00:00', 'hahah'), '2 days ago');
81
- t.equal(newTimeAgoDefaultLocale.format('2016-02-28 12:00:00', 'zh_CN'), '2天前');
82
- t.end();
83
- });
1
+ 'use strict';
2
+
3
+ const test = require('tape');
4
+ const timeago = require('..');
5
+ const fs = require('fs');
6
+ const slice = require('slice.js');
7
+ const testBuilder = require('./test-builder');
8
+
9
+ test('timeago.js should be tested', t => {
10
+ // locale tests #################################################################
11
+ // read all the locales test in `tests/locales` dir
12
+ fs.readdir('tests/locales', (err, files) => {
13
+ files.forEach(file => {
14
+ const locale = slice(file)('0:-3');
15
+ console.log('\nLocale testcase for ['+ locale +']');
16
+ // require in the locales testcases
17
+ const tb = testBuilder(Date.now()).register(locale, require('../locales/' + locale), true);
18
+ require('./locales/' + locale)(t, tb);
19
+ })
20
+ });
21
+ // end locale tests #############################################################
22
+
23
+ // test locale, can set the locale
24
+ t.equal(timeago('2016-06-23').format('2016-06-22'), '1 day ago');
25
+ t.equal(timeago('2016-06-23').format('2016-06-25'), 'in 2 days');
26
+ t.equal(timeago('2016-06-23').format('2016-06-22', 'zh_CN'), '1天前');
27
+ t.equal(timeago('2016-06-23').format('2016-06-25', 'zh_CN'), '2天后');
28
+
29
+ // test register locale
30
+ const timeagoReg = timeago('2016-06-23');
31
+ timeago.register('test_local', (number, index) => [
32
+ ["just xxx", "right now"],
33
+ ["%s seconds xxx", "in %s seconds"],
34
+ ["1 minute xxx", "in 1 minute"],
35
+ ["%s minutes xxx", "in %s minutes"],
36
+ ["1 hour xxx", "in 1 hour"],
37
+ ["%s hours xxx", "in %s hours"],
38
+ ["1 day xxx", "in 1 day"],
39
+ ["%s days xxx", "in %s days"],
40
+ ['一周前', '一周后'],
41
+ ['%s周前', '%s周后'],
42
+ ["1 month xxx", "in 1 month"],
43
+ ["%s months xxx", "in %s months"],
44
+ ["1 year xxx", "in 1 year"],
45
+ ["%s years xxx", "in %s years"]
46
+ ][index]
47
+ );
48
+ t.equal(timeagoReg.format('2016-06-22', 'test_local'), '1 day xxx');
49
+
50
+ // testcase for other points
51
+ // relative now
52
+ t.equal(timeago().format(Date.now() - 11 * 1000 * 60 * 60), '11 hours ago');
53
+
54
+ // timestamp is also can work
55
+ let current = Date.now();
56
+ t.equal(timeago(current, null).format(current - 8 * 1000 * 60 * 60 * 24), '1 week ago');
57
+ t.equal(timeago(current, null).format(current - 31536000 * 1000 + 1000), '11 months ago');
58
+
59
+ // Date()
60
+ current = new Date();
61
+ t.equal(timeago(current, null).format(current), 'just now');
62
+
63
+
64
+ // test leap year
65
+ t.equal(timeago('2016-03-01 12:00:00').format('2016-02-28 12:00:00'), '2 days ago');
66
+ t.equal(timeago('2015-03-01 12:00:00').format('2015-02-28 12:00:00'), '1 day ago');
67
+
68
+ // test default locale
69
+ t.equal(timeago('2016-03-01 12:00:00').format('2016-02-28 12:00:00'), '2 days ago');
70
+ t.equal(timeago('2016-03-01 12:00:00', 'zh_CN').format('2016-02-28 12:00:00'), '2天前');
71
+
72
+ // test setLocale
73
+ const newTimeAgo = timeago('2016-03-01 12:00:00');
74
+ t.equal(newTimeAgo.format('2016-02-28 12:00:00'), '2 days ago');
75
+ newTimeAgo.setLocale('zh_CN');
76
+ t.equal(newTimeAgo.format('2016-02-28 12:00:00'), '2天前');
77
+
78
+ const newTimeAgoDefaultLocale = timeago('2016-03-01 12:00:00', 'pl');
79
+ t.equal(newTimeAgoDefaultLocale.format('2016-02-28 12:00:00'), '2 days ago');
80
+ t.equal(newTimeAgoDefaultLocale.format('2016-02-28 12:00:00', 'hahah'), '2 days ago');
81
+ t.equal(newTimeAgoDefaultLocale.format('2016-02-28 12:00:00', 'zh_CN'), '2天前');
82
+ t.end();
83
+ });