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
package/src/timeago.js CHANGED
@@ -1,241 +1,261 @@
1
- /**
2
- * Copyright (c) 2016 hustcc
3
- * License: MIT
4
- * Version: %%GULP_INJECT_VERSION%%
5
- * https://github.com/hustcc/timeago.js
6
- **/
7
- /* jshint expr: true */
8
- !function (root, factory) {
9
- if (typeof module === 'object' && module.exports)
10
- module.exports = factory(root);
11
- else
12
- root.timeago = factory(root);
13
- }(typeof window !== 'undefined' ? window : this,
14
- function () {
15
- var cnt = 0, // the timer counter, for timer key
16
- indexMapEn = 'second_minute_hour_day_week_month_year'.split('_'),
17
- indexMapZh = '秒_分钟_小时_天_周_月_年'.split('_'),
18
- // build-in locales: en & zh_CN
19
- locales = {
20
- 'en': function(number, index) {
21
- if (index === 0) return ['just now', 'right now'];
22
- var unit = indexMapEn[parseInt(index / 2)];
23
- if (number > 1) unit += 's';
24
- return [number + ' ' + unit + ' ago', 'in ' + number + ' ' + unit];
25
- },
26
- 'zh_CN': function(number, index) {
27
- if (index === 0) return ['刚刚', '片刻后'];
28
- var unit = indexMapZh[parseInt(index / 2)];
29
- return [number + unit + '前', number + unit + '后'];
30
- }
31
- },
32
- // second, minute, hour, day, week, month, year(365 days)
33
- SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
34
- SEC_ARRAY_LEN = 6,
35
- ATTR_DATETIME = 'datetime';
36
-
37
- // format Date / string / timestamp to Date instance.
38
- function toDate(input) {
39
- if (input instanceof Date) return input;
40
- if (!isNaN(input)) return new Date(toInt(input));
41
- if (/^\d+$/.test(input)) return new Date(toInt(input));
42
- input = (input || '').trim().replace(/\.\d+/, '') // remove milliseconds
43
- .replace(/-/, '/').replace(/-/, '/')
44
- .replace(/T/, ' ').replace(/Z/, ' UTC')
45
- .replace(/([\+\-]\d\d)\:?(\d\d)/, ' $1$2'); // -04:00 -> -0400
46
- return new Date(input);
47
- }
48
- // change f into int, remove Decimal. just for code compression
49
- function toInt(f) {
50
- return parseInt(f);
51
- }
52
- // format the diff second to *** time ago, with setting locale
53
- function formatDiff(diff, locale, defaultLocale) {
54
- // if locale is not exist, use defaultLocale.
55
- // if defaultLocale is not exist, use build-in `en`.
56
- // be sure of no error when locale is not exist.
57
- locale = locales[locale] ? locale : (locales[defaultLocale] ? defaultLocale : 'en');
58
- // if (! locales[locale]) locale = defaultLocale;
59
- var i = 0,
60
- agoin = diff < 0 ? 1 : 0; // timein or timeago
61
- diff = Math.abs(diff);
62
-
63
- for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
64
- diff /= SEC_ARRAY[i];
65
- }
66
- diff = toInt(diff);
67
- i *= 2;
68
-
69
- if (diff > (i === 0 ? 9 : 1)) i += 1;
70
- return locales[locale](diff, i)[agoin].replace('%s', diff);
71
- }
72
- // calculate the diff second between date to be formated an now date.
73
- function diffSec(date, nowDate) {
74
- nowDate = nowDate ? toDate(nowDate) : new Date();
75
- return (nowDate - toDate(date)) / 1000;
76
- }
77
- /**
78
- * nextInterval: calculate the next interval time.
79
- * - diff: the diff sec between now and date to be formated.
80
- *
81
- * What's the meaning?
82
- * diff = 61 then return 59
83
- * diff = 3601 (an hour + 1 second), then return 3599
84
- * make the interval with high performace.
85
- **/
86
- function nextInterval(diff) {
87
- var rst = 1, i = 0, d = Math.abs(diff);
88
- for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
89
- diff /= SEC_ARRAY[i];
90
- rst *= SEC_ARRAY[i];
91
- }
92
- // return leftSec(d, rst);
93
- d = d % rst;
94
- d = d ? rst - d : rst;
95
- return Math.ceil(d);
96
- }
97
- // get the datetime attribute, jQuery and DOM
98
- function getDateAttr(node) {
99
- if(node.dataset.timeago) return node.dataset.timeago;
100
- if (node.getAttribute) return node.getAttribute(ATTR_DATETIME);
101
- if(node.attr) return node.attr(ATTR_DATETIME);
102
- }
103
- /**
104
- * timeago: the function to get `timeago` instance.
105
- * - nowDate: the relative date, default is new Date().
106
- * - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
107
- *
108
- * How to use it?
109
- * var timeagoLib = require('timeago.js');
110
- * var timeago = timeagoLib(); // all use default.
111
- * var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
112
- * var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
113
- * var timeago = timeagoLib('2016-09-10', 'zh_CN'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
114
- **/
115
- function Timeago(nowDate, defaultLocale) {
116
- var timers = {}; // real-time render timers
117
- // if do not set the defaultLocale, set it with `en`
118
- if (! defaultLocale) defaultLocale = 'en'; // use default build-in locale
119
- // what the timer will do
120
- function doRender(node, date, locale, cnt) {
121
- var diff = diffSec(date, nowDate);
122
- node.innerHTML = formatDiff(diff, locale, defaultLocale);
123
- // waiting %s seconds, do the next render
124
- timers['k' + cnt] = setTimeout(function() {
125
- doRender(node, date, locale, cnt);
126
- }, nextInterval(diff) * 1000);
127
- }
128
- /**
129
- * nextInterval: calculate the next interval time.
130
- * - diff: the diff sec between now and date to be formated.
131
- *
132
- * What's the meaning?
133
- * diff = 61 then return 59
134
- * diff = 3601 (an hour + 1 second), then return 3599
135
- * make the interval with high performace.
136
- **/
137
- // this.nextInterval = function(diff) { // for dev test
138
- // var rst = 1, i = 0, d = Math.abs(diff);
139
- // for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
140
- // diff /= SEC_ARRAY[i];
141
- // rst *= SEC_ARRAY[i];
142
- // }
143
- // // return leftSec(d, rst);
144
- // d = d % rst;
145
- // d = d ? rst - d : rst;
146
- // return Math.ceil(d);
147
- // }; // for dev test
148
- /**
149
- * format: format the date to *** time ago, with setting or default locale
150
- * - date: the date / string / timestamp to be formated
151
- * - locale: the formated string's locale name, e.g. en / zh_CN
152
- *
153
- * How to use it?
154
- * var timeago = require('timeago.js')();
155
- * timeago.format(new Date(), 'pl'); // Date instance
156
- * timeago.format('2016-09-10', 'fr'); // formated date string
157
- * timeago.format(1473473400269); // timestamp with ms
158
- **/
159
- this.format = function(date, locale) {
160
- return formatDiff(diffSec(date, nowDate), locale, defaultLocale);
161
- };
162
- /**
163
- * render: render the DOM real-time.
164
- * - nodes: which nodes will be rendered.
165
- * - locale: the locale name used to format date.
166
- *
167
- * How to use it?
168
- * var timeago = new require('timeago.js')();
169
- * // 1. javascript selector
170
- * timeago.render(document.querySelectorAll('.need_to_be_rendered'));
171
- * // 2. use jQuery selector
172
- * timeago.render($('.need_to_be_rendered'), 'pl');
173
- *
174
- * Notice: please be sure the dom has attribute `datetime`.
175
- **/
176
- this.render = function(nodes, locale) {
177
- if (nodes.length === undefined) nodes = [nodes];
178
- for (var i = 0; i < nodes.length; i++) {
179
- doRender(nodes[i], getDateAttr(nodes[i]), locale, ++ cnt); // render item
180
- }
181
- };
182
- /**
183
- * cancel: cancel all the timers which are doing real-time render.
184
- *
185
- * How to use it?
186
- * var timeago = new require('timeago.js')();
187
- * timeago.render(document.querySelectorAll('.need_to_be_rendered'));
188
- * timeago.cancel(); // will stop all the timer, stop render in real time.
189
- **/
190
- this.cancel = function() {
191
- for (var key in timers) {
192
- clearTimeout(timers[key]);
193
- }
194
- timers = {};
195
- };
196
- /**
197
- * setLocale: set the default locale name.
198
- *
199
- * How to use it?
200
- * var timeago = require('timeago.js');
201
- * timeago = new timeago();
202
- * timeago.setLocale('fr');
203
- **/
204
- this.setLocale = function(locale) {
205
- defaultLocale = locale;
206
- };
207
- return this;
208
- }
209
- /**
210
- * timeago: the function to get `timeago` instance.
211
- * - nowDate: the relative date, default is new Date().
212
- * - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
213
- *
214
- * How to use it?
215
- * var timeagoLib = require('timeago.js');
216
- * var timeago = timeagoLib(); // all use default.
217
- * var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
218
- * var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
219
- * var timeago = timeagoLib('2016-09-10', 'zh_CN'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
220
- **/
221
- function timeagoFactory(nowDate, defaultLocale) {
222
- return new Timeago(nowDate, defaultLocale);
223
- }
224
- /**
225
- * register: register a new language locale
226
- * - locale: locale name, e.g. en / zh_CN, notice the standard.
227
- * - localeFunc: the locale process function
228
- *
229
- * How to use it?
230
- * var timeagoLib = require('timeago.js');
231
- *
232
- * timeagoLib.register('the locale name', the_locale_func);
233
- * // or
234
- * timeagoLib.register('pl', require('timeago.js/locales/pl'));
235
- **/
236
- timeagoFactory.register = function(locale, localeFunc) {
237
- locales[locale] = localeFunc;
238
- };
239
-
240
- return timeagoFactory;
241
- });
1
+ /**
2
+ * Copyright (c) 2016 hustcc
3
+ * License: MIT
4
+ * Version: %%GULP_INJECT_VERSION%%
5
+ * https://github.com/hustcc/timeago.js
6
+ **/
7
+ /* jshint expr: true */
8
+ !function (root, factory) {
9
+ if (typeof module === 'object' && module.exports) {
10
+ module.exports = factory(root); // nodejs support
11
+ module.exports['default'] = module.exports; // es6 support
12
+ }
13
+ else
14
+ root.timeago = factory(root);
15
+ }(typeof window !== 'undefined' ? window : this,
16
+ function () {
17
+ var indexMapEn = 'second_minute_hour_day_week_month_year'.split('_'),
18
+ indexMapZh = '秒_分钟_小时_天_周_月_年'.split('_'),
19
+ // build-in locales: en & zh_CN
20
+ locales = {
21
+ 'en': function(number, index) {
22
+ if (index === 0) return ['just now', 'right now'];
23
+ var unit = indexMapEn[parseInt(index / 2)];
24
+ if (number > 1) unit += 's';
25
+ return [number + ' ' + unit + ' ago', 'in ' + number + ' ' + unit];
26
+ },
27
+ 'zh_CN': function(number, index) {
28
+ if (index === 0) return ['刚刚', '片刻后'];
29
+ var unit = indexMapZh[parseInt(index / 2)];
30
+ return [number + unit + '前', number + unit + '后'];
31
+ }
32
+ },
33
+ // second, minute, hour, day, week, month, year(365 days)
34
+ SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
35
+ SEC_ARRAY_LEN = 6,
36
+ // ATTR_DATETIME = 'datetime',
37
+ ATTR_DATA_TID = 'data-tid',
38
+ timers = {}; // real-time render timers
39
+
40
+ // format Date / string / timestamp to Date instance.
41
+ function toDate(input) {
42
+ if (input instanceof Date) return input;
43
+ if (!isNaN(input)) return new Date(toInt(input));
44
+ if (/^\d+$/.test(input)) return new Date(toInt(input));
45
+ input = (input || '').trim().replace(/\.\d+/, '') // remove milliseconds
46
+ .replace(/-/, '/').replace(/-/, '/')
47
+ .replace(/(\d)T(\d)/, '$1 $2').replace(/Z/, ' UTC') // 2017-2-5T3:57:52Z -> 2017-2-5 3:57:52UTC
48
+ .replace(/([\+\-]\d\d)\:?(\d\d)/, ' $1$2'); // -04:00 -> -0400
49
+ return new Date(input);
50
+ }
51
+ // change f into int, remove decimal. Just for code compression
52
+ function toInt(f) {
53
+ return parseInt(f);
54
+ }
55
+ // format the diff second to *** time ago, with setting locale
56
+ function formatDiff(diff, locale, defaultLocale) {
57
+ // if locale is not exist, use defaultLocale.
58
+ // if defaultLocale is not exist, use build-in `en`.
59
+ // be sure of no error when locale is not exist.
60
+ locale = locales[locale] ? locale : (locales[defaultLocale] ? defaultLocale : 'en');
61
+ // if (! locales[locale]) locale = defaultLocale;
62
+ var i = 0,
63
+ agoin = diff < 0 ? 1 : 0, // timein or timeago
64
+ total_sec = diff = Math.abs(diff);
65
+
66
+ for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
67
+ diff /= SEC_ARRAY[i];
68
+ }
69
+ diff = toInt(diff);
70
+ i *= 2;
71
+
72
+ if (diff > (i === 0 ? 9 : 1)) i += 1;
73
+ return locales[locale](diff, i, total_sec)[agoin].replace('%s', diff);
74
+ }
75
+ // calculate the diff second between date to be formated an now date.
76
+ function diffSec(date, nowDate) {
77
+ nowDate = nowDate ? toDate(nowDate) : new Date();
78
+ return (nowDate - toDate(date)) / 1000;
79
+ }
80
+ /**
81
+ * nextInterval: calculate the next interval time.
82
+ * - diff: the diff sec between now and date to be formated.
83
+ *
84
+ * What's the meaning?
85
+ * diff = 61 then return 59
86
+ * diff = 3601 (an hour + 1 second), then return 3599
87
+ * make the interval with high performace.
88
+ **/
89
+ function nextInterval(diff) {
90
+ var rst = 1, i = 0, d = Math.abs(diff);
91
+ for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
92
+ diff /= SEC_ARRAY[i];
93
+ rst *= SEC_ARRAY[i];
94
+ }
95
+ // return leftSec(d, rst);
96
+ d = d % rst;
97
+ d = d ? rst - d : rst;
98
+ return Math.ceil(d);
99
+ }
100
+ // get the datetime attribute, `data-timeagp` / `datetime` are supported.
101
+ function getDateAttr(node) {
102
+ return getAttr(node, 'data-timeago') || getAttr(node, 'datetime');
103
+ }
104
+ // get the node attribute, native DOM and jquery supported.
105
+ function getAttr(node, name) {
106
+ if(node.getAttribute) return node.getAttribute(name); // native
107
+ if(node.attr) return node.attr(name); // jquery
108
+ }
109
+ // set the node attribute, native DOM and jquery supported.
110
+ function setTidAttr(node, val) {
111
+ if(node.setAttribute) return node.setAttribute(ATTR_DATA_TID, val); // native
112
+ if(node.attr) return node.attr(ATTR_DATA_TID, val); // jquery
113
+ }
114
+ // get the timer id of node.
115
+ // remove the function, can save some bytes.
116
+ // function getTidFromNode(node) {
117
+ // return getAttr(node, ATTR_DATA_TID);
118
+ // }
119
+ /**
120
+ * timeago: the function to get `timeago` instance.
121
+ * - nowDate: the relative date, default is new Date().
122
+ * - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
123
+ *
124
+ * How to use it?
125
+ * var timeagoLib = require('timeago.js');
126
+ * var timeago = timeagoLib(); // all use default.
127
+ * var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
128
+ * var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
129
+ * var timeago = timeagoLib('2016-09-10', 'zh_CN'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
130
+ **/
131
+ function Timeago(nowDate, defaultLocale) {
132
+ this.nowDate = nowDate;
133
+ // if do not set the defaultLocale, set it with `en`
134
+ this.defaultLocale = defaultLocale || 'en'; // use default build-in locale
135
+ // for dev test
136
+ // this.nextInterval = nextInterval;
137
+ }
138
+ // what the timer will do
139
+ Timeago.prototype.doRender = function(node, date, locale) {
140
+ var diff = diffSec(date, this.nowDate),
141
+ self = this,
142
+ tid;
143
+ // delete previously assigned timeout's id to node
144
+ node.innerHTML = formatDiff(diff, locale, this.defaultLocale);
145
+ // waiting %s seconds, do the next render
146
+ timers[tid = setTimeout(function() {
147
+ self.doRender(node, date, locale);
148
+ delete timers[tid];
149
+ }, Math.min(nextInterval(diff) * 1000, 0x7FFFFFFF))] = 0; // there is no need to save node in object.
150
+ // set attribute date-tid
151
+ setTidAttr(node, tid);
152
+ };
153
+ /**
154
+ * format: format the date to *** time ago, with setting or default locale
155
+ * - date: the date / string / timestamp to be formated
156
+ * - locale: the formated string's locale name, e.g. en / zh_CN
157
+ *
158
+ * How to use it?
159
+ * var timeago = require('timeago.js')();
160
+ * timeago.format(new Date(), 'pl'); // Date instance
161
+ * timeago.format('2016-09-10', 'fr'); // formated date string
162
+ * timeago.format(1473473400269); // timestamp with ms
163
+ **/
164
+ Timeago.prototype.format = function(date, locale) {
165
+ return formatDiff(diffSec(date, this.nowDate), locale, this.defaultLocale);
166
+ };
167
+ /**
168
+ * render: render the DOM real-time.
169
+ * - nodes: which nodes will be rendered.
170
+ * - locale: the locale name used to format date.
171
+ *
172
+ * How to use it?
173
+ * var timeago = require('timeago.js')();
174
+ * // 1. javascript selector
175
+ * timeago.render(document.querySelectorAll('.need_to_be_rendered'));
176
+ * // 2. use jQuery selector
177
+ * timeago.render($('.need_to_be_rendered'), 'pl');
178
+ *
179
+ * Notice: please be sure the dom has attribute `datetime`.
180
+ **/
181
+ Timeago.prototype.render = function(nodes, locale) {
182
+ if (nodes.length === undefined) nodes = [nodes];
183
+ for (var i = 0, len = nodes.length; i < len; i++) {
184
+ this.doRender(nodes[i], getDateAttr(nodes[i]), locale); // render item
185
+ }
186
+ };
187
+ /**
188
+ * setLocale: set the default locale name.
189
+ *
190
+ * How to use it?
191
+ * var timeago = require('timeago.js')();
192
+ * timeago.setLocale('fr');
193
+ **/
194
+ Timeago.prototype.setLocale = function(locale) {
195
+ this.defaultLocale = locale;
196
+ };
197
+ /**
198
+ * timeago: the function to get `timeago` instance.
199
+ * - nowDate: the relative date, default is new Date().
200
+ * - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
201
+ *
202
+ * How to use it?
203
+ * var timeagoFactory = require('timeago.js');
204
+ * var timeago = timeagoFactory(); // all use default.
205
+ * var timeago = timeagoFactory('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
206
+ * var timeago = timeagoFactory(null, 'zh_CN'); // set default locale is `zh_CN`.
207
+ * var timeago = timeagoFactory('2016-09-10', 'zh_CN'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
208
+ **/
209
+ function timeagoFactory(nowDate, defaultLocale) {
210
+ return new Timeago(nowDate, defaultLocale);
211
+ }
212
+ /**
213
+ * register: register a new language locale
214
+ * - locale: locale name, e.g. en / zh_CN, notice the standard.
215
+ * - localeFunc: the locale process function
216
+ *
217
+ * How to use it?
218
+ * var timeagoFactory = require('timeago.js');
219
+ *
220
+ * timeagoFactory.register('the locale name', the_locale_func);
221
+ * // or
222
+ * timeagoFactory.register('pl', require('timeago.js/locales/pl'));
223
+ **/
224
+ timeagoFactory.register = function(locale, localeFunc) {
225
+ locales[locale] = localeFunc;
226
+ };
227
+
228
+ /**
229
+ * cancel: cancels one or all the timers which are doing real-time render.
230
+ *
231
+ * How to use it?
232
+ * For canceling all the timers:
233
+ * var timeagoFactory = require('timeago.js');
234
+ * var timeago = timeagoFactory();
235
+ * timeago.render(document.querySelectorAll('.need_to_be_rendered'));
236
+ * timeagoFactory.cancel(); // will stop all the timers, stop render in real time.
237
+ *
238
+ * For canceling single timer on specific node:
239
+ * var timeagoFactory = require('timeago.js');
240
+ * var timeago = timeagoFactory();
241
+ * var nodes = document.querySelectorAll('.need_to_be_rendered');
242
+ * timeago.render(nodes);
243
+ * timeagoFactory.cancel(nodes[0]); // will clear a timer attached to the first node, stop render in real time.
244
+ **/
245
+ timeagoFactory.cancel = function(node) {
246
+ var tid;
247
+ // assigning in if statement to save space
248
+ if (node) {
249
+ tid = getAttr(node, ATTR_DATA_TID); // get the timer of DOM node(native / jq).
250
+ if (tid) {
251
+ clearTimeout(tid);
252
+ delete timers[tid];
253
+ }
254
+ } else {
255
+ for (tid in timers) clearTimeout(tid);
256
+ timers = {};
257
+ }
258
+ };
259
+
260
+ return timeagoFactory;
261
+ });
@@ -1,12 +1,12 @@
1
- /**
2
- * Copyright (c) 2016 hustcc
3
- * License: MIT
4
- * https://github.com/hustcc/timeago.js
5
- **/
6
- var allLocales = require('../locales/locales.js');
7
- var locale = null;
8
- for (var i = allLocales.length - 1; i >= 0; i--) {
9
- locale = allLocales[i];
10
- if (locale != 'en' && locale != 'zh_CN')
11
- timeago.register(locale, require('../locales/' + locale));
12
- };
1
+ /**
2
+ * Copyright (c) 2016 hustcc
3
+ * License: MIT
4
+ * https://github.com/hustcc/timeago.js
5
+ **/
6
+ var allLocales = require('../locales/locales.js');
7
+ var locale = null;
8
+ for (var i = allLocales.length - 1; i >= 0; i--) {
9
+ locale = allLocales[i];
10
+ if (locale != 'en' && locale != 'zh_CN')
11
+ timeago.register(locale, require('../locales/' + locale));
12
+ };