Skip to content

Commit c38d84a

Browse files
author
hustcc
committed
update code from likerRr hustcc#68
1 parent c4d3101 commit c38d84a

File tree

10 files changed

+116
-90
lines changed

10 files changed

+116
-90
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,39 +70,22 @@ timeagoInstance.format('2016-06-12');
7070

7171
# 2. Detailed Usage
7272

73-
**1. Localization**
74-
75-
Default locale is **`en`**, and the library supports `en` and `zh_CN`.
76-
77-
```js
78-
var timeagoInstance = new timeago();
79-
timeagoInstance.format('2016-06-12', 'zh_CN');
80-
```
81-
82-
You can change the locale in the constructor or use the `setLocale` method;
83-
84-
```js
85-
var timeagoInstance = new timeago('zh_CN');
86-
// or
87-
new timeago().setLocale('zh_CN');
88-
```
89-
90-
**2. Set relative date**
73+
**1. Set relative date**
9174

9275
`timeago` is relative to the current date default. You can set it yourself.
9376

9477
```js
95-
var timeagoInstance = new timeago(null, '2016-06-10 12:12:12'); // set the relative date here.
78+
var timeagoInstance = new timeago('2016-06-10 12:12:12'); // set the relative date here.
9679
timeagoInstance.format('2016-06-12', 'zh_CN');
9780
```
9881

99-
**3. Use timestamp**
82+
**2. Use timestamp**
10083

10184
```
10285
new timeago().format(new Date().getTime() - 11 * 1000 * 60 * 60); // will get '11 hours ago'
10386
```
10487

105-
**4. Automatic rendering**
88+
**3. Automatic rendering**
10689

10790
HTML code:
10891
```html
@@ -123,6 +106,23 @@ The API `cancel` will clear all the render timers and release all resources of t
123106

124107
The DOM object should have the attribute `datetime` or `data-timeago` with date formated string.
125108

109+
**4. Localization**
110+
111+
Default locale is **`en`**, and the library supports `en` and `zh_CN`.
112+
113+
```js
114+
var timeagoInstance = new timeago();
115+
timeagoInstance.format('2016-06-12', 'zh_CN');
116+
```
117+
118+
You can change the locale in the constructor or use the `setLocale` method;
119+
120+
```js
121+
var timeagoInstance = new timeago(currentDate, 'zh_CN');
122+
// or
123+
new timeago().setLocale('zh_CN');
124+
```
125+
126126
**5. Register local language**
127127

128128
You can `register` your own language, this is a class static method. Like below, e.g.

README_zh.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,6 @@ timeago.format('2016-06-12')
6868

6969
# 高级特性使用
7070

71-
**1. 本地化**
72-
73-
默认的语言是英文 **`en`**, 这个库自带语言有 `en` and `zh_CN` (英文和中文).
74-
75-
```js
76-
var timeagoInstance = new timeago();
77-
timeagoInstance.format('2016-06-12', 'zh_CN');
78-
```
79-
80-
可以在构造函数中传入默认语言,也可以调用 `setLocale` 方法。
81-
82-
```js
83-
var timeagoInstance = new timeago('zh_CN');
84-
// or
85-
new timeago().setLocale('zh_CN');
86-
```
8771

8872
**1. 设置相对日期**
8973

@@ -122,6 +106,22 @@ API方法 `cancel` 调用之后会清除所有的定时器方法,并且释放
122106

123107
被渲染的节点必须要有 `datetime` 或者 `data-timeago` 属性,属性值为日期格式的字符串。
124108

109+
**4. 本地化**
110+
111+
默认的语言是英文 **`en`**, 这个库自带语言有 `en` and `zh_CN` (英文和中文).
112+
113+
```js
114+
var timeagoInstance = new timeago();
115+
timeagoInstance.format('2016-06-12', 'zh_CN');
116+
```
117+
118+
可以在构造函数中传入默认语言,也可以调用 `setLocale` 方法。
119+
120+
```js
121+
var timeagoInstance = new timeago(currentDate, 'zh_CN');
122+
// or
123+
new timeago().setLocale('zh_CN');
124+
```
125125

126126
**5. 注册本地语言**
127127

demo/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ new timeago().render(document.querySelectorAll('.need_to_be_rendered'));
1212

1313
// 2. demo
1414
document.getElementById('demo_now').innerHTML = new timeago().format(new Date());
15-
document.getElementById('demo_20160907').innerHTML = new timeago('zh_CN').format('2016-09-07');
15+
document.getElementById('demo_20160907').innerHTML = new timeago(null, 'zh_CN').format('2016-09-07');
1616
document.getElementById('demo_timestamp').innerHTML = new timeago().format(1473245023718);

dist/timeago.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function () {
3535
// second, minute, hour, day, week, month, year(365 days)
3636
SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
3737
SEC_ARRAY_LEN = 6,
38-
ATTR_DATETIME = 'datetime',
38+
ATTR_DATETIME = 'datetime';
3939

4040
/**
4141
* timeago: the function to get `timeago` instance.
@@ -45,21 +45,19 @@ function () {
4545
* How to use it?
4646
* var timeagoLib = require('timeago.js');
4747
* var timeago = timeagoLib(); // all use default.
48-
* var timeago = timeagoLib('zh_CN'); // set default locale is `zh_CN`.
49-
* var timeago = timeagoLib(null, '2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
50-
* var timeago = timeagoLib('zh_CN', '2016-09-10'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
48+
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
49+
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
50+
* 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天前.
5151
**/
52-
timeago = function(defaultLocale, nowDate) {
52+
function timeago(nowDate, defaultLocale) {
5353
var timers = {}; // real-time render timers
5454
// if do not set the defaultLocale, set it with `en`
55-
if (! defaultLocale) {
56-
defaultLocale = 'en'; // use default build-in locale
57-
}
55+
if (! defaultLocale) defaultLocale = 'en'; // use default build-in locale
5856
// calculate the diff second between date to be formated an now date.
5957
function diffSec(date) {
6058
var now = new Date();
6159
if (nowDate) now = toDate(nowDate);
62-
return (now.getTime() - toDate(date).getTime()) / 1000;
60+
return (now - toDate(date)) / 1000;
6361
}
6462
// format the diff second to *** time ago, with setting locale
6563
function formatDiff(diff, locale) {
@@ -136,14 +134,14 @@ function () {
136134
// return leftSec(d, rst);
137135
d = d % rst;
138136
d = d ? rst - d : rst;
139-
return Math.ceil(diff);
137+
return Math.ceil(d);
140138
// }; // for dev test
141139
}
142140
// what the timer will do
143141
function doRender(node, date, locale, cnt) {
144142
var diff = diffSec(date);
145143
node.innerHTML = formatDiff(diff, locale);
146-
// 通过diff来判断下一次执行的时间
144+
// waiting %s seconds, do the next render
147145
timers['k' + cnt] = setTimeout(function() {
148146
doRender(node, date, locale, cnt);
149147
}, nextInterval(diff) * 1000);
@@ -165,7 +163,7 @@ function () {
165163
* // 2. use jQuery selector
166164
* timeago.render($('.need_to_be_rendered'), 'pl');
167165
*
168-
* Notice: please be sure the dom has attribute `data-timeago`.
166+
* Notice: please be sure the dom has attribute `datetime`.
169167
**/
170168
this.render = function(nodes, locale) {
171169
if (nodes.length === undefined) nodes = [nodes];
@@ -199,7 +197,22 @@ function () {
199197
defaultLocale = locale;
200198
};
201199
return this;
202-
};
200+
}
201+
/**
202+
* timeago: the function to get `timeago` instance.
203+
* - nowDate: the relative date, default is new Date().
204+
* - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
205+
*
206+
* How to use it?
207+
* var timeagoLib = require('timeago.js');
208+
* var timeago = timeagoLib(); // all use default.
209+
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
210+
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
211+
* 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天前.
212+
**/
213+
function timeagoFactory(nowDate, defaultLocale) {
214+
return new timeago(nowDate, defaultLocale);
215+
}
203216
/**
204217
* register: register a new language locale
205218
* - locale: locale name, e.g. en / zh_CN, notice the standard.
@@ -211,10 +224,10 @@ function () {
211224
* timeagoLib.register('the locale name', the_locale_func);
212225
* // or
213226
* timeagoLib.register('pl', require('timeago.js/locales/pl'));
214-
**/
215-
timeago.register = function(locale, localeFunc) {
227+
**/
228+
timeagoFactory.register = function(locale, localeFunc) {
216229
locales[locale] = localeFunc;
217230
};
218231

219-
return timeago;
232+
return timeagoFactory;
220233
});

dist/timeago.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h3>Why ?</h3>
5555
<ul>
5656
<li>Avoid timestamps dated "1 minute ago" even though the page was opened 10 minutes ago; <em>timeago refreshes automatically</em>.</li>
5757
<li>You can <em>take full advantage of page caching</em> in your web applications, because the timestamps aren't calculated on the server.</li>
58-
<li>Very tiny(~2kb), and has <em>no dependency</em>, e.g. jQuery.</li>
58+
<li>Very tiny(less than 2kb), and has <em>no dependency</em>, e.g. jQuery.</li>
5959
</ul>
6060

6161
<h3>How ?</h3>
@@ -93,7 +93,7 @@ <h3>How ?</h3>
9393
</p>
9494
<pre>
9595
new timeago().format(new Date()); //=> &quot;<span id="demo_now"></span>&quot;
96-
new timeago('zh_CN').format('2016-09-07') //=> &quot;<span id="demo_20160907"></span>&quot;
96+
new timeago(null, 'zh_CN').format('2016-09-07') //=> &quot;<span id="demo_20160907"></span>&quot;
9797
new timeago().format(1473245023718); //=> &quot;<span id="demo_timestamp"></span>&quot;</pre>
9898

9999
<h3>Locales register ?</h3>

src/timeago.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function () {
3535
// second, minute, hour, day, week, month, year(365 days)
3636
SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
3737
SEC_ARRAY_LEN = 6,
38-
ATTR_DATETIME = 'datetime',
38+
ATTR_DATETIME = 'datetime';
3939

4040
/**
4141
* timeago: the function to get `timeago` instance.
@@ -45,21 +45,19 @@ function () {
4545
* How to use it?
4646
* var timeagoLib = require('timeago.js');
4747
* var timeago = timeagoLib(); // all use default.
48-
* var timeago = timeagoLib('zh_CN'); // set default locale is `zh_CN`.
49-
* var timeago = timeagoLib(null, '2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
50-
* var timeago = timeagoLib('zh_CN', '2016-09-10'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
48+
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
49+
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
50+
* 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天前.
5151
**/
52-
timeago = function(defaultLocale, nowDate) {
52+
function timeago(nowDate, defaultLocale) {
5353
var timers = {}; // real-time render timers
5454
// if do not set the defaultLocale, set it with `en`
55-
if (! defaultLocale) {
56-
defaultLocale = 'en'; // use default build-in locale
57-
}
55+
if (! defaultLocale) defaultLocale = 'en'; // use default build-in locale
5856
// calculate the diff second between date to be formated an now date.
5957
function diffSec(date) {
6058
var now = new Date();
6159
if (nowDate) now = toDate(nowDate);
62-
return (now.getTime() - toDate(date).getTime()) / 1000;
60+
return (now - toDate(date)) / 1000;
6361
}
6462
// format the diff second to *** time ago, with setting locale
6563
function formatDiff(diff, locale) {
@@ -136,14 +134,14 @@ function () {
136134
// return leftSec(d, rst);
137135
d = d % rst;
138136
d = d ? rst - d : rst;
139-
return Math.ceil(diff);
137+
return Math.ceil(d);
140138
// }; // for dev test
141139
}
142140
// what the timer will do
143141
function doRender(node, date, locale, cnt) {
144142
var diff = diffSec(date);
145143
node.innerHTML = formatDiff(diff, locale);
146-
// 通过diff来判断下一次执行的时间
144+
// waiting %s seconds, do the next render
147145
timers['k' + cnt] = setTimeout(function() {
148146
doRender(node, date, locale, cnt);
149147
}, nextInterval(diff) * 1000);
@@ -165,7 +163,7 @@ function () {
165163
* // 2. use jQuery selector
166164
* timeago.render($('.need_to_be_rendered'), 'pl');
167165
*
168-
* Notice: please be sure the dom has attribute `data-timeago`.
166+
* Notice: please be sure the dom has attribute `datetime`.
169167
**/
170168
this.render = function(nodes, locale) {
171169
if (nodes.length === undefined) nodes = [nodes];
@@ -199,7 +197,22 @@ function () {
199197
defaultLocale = locale;
200198
};
201199
return this;
202-
};
200+
}
201+
/**
202+
* timeago: the function to get `timeago` instance.
203+
* - nowDate: the relative date, default is new Date().
204+
* - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
205+
*
206+
* How to use it?
207+
* var timeagoLib = require('timeago.js');
208+
* var timeago = timeagoLib(); // all use default.
209+
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
210+
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
211+
* 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天前.
212+
**/
213+
function timeagoFactory(nowDate, defaultLocale) {
214+
return new timeago(nowDate, defaultLocale);
215+
}
203216
/**
204217
* register: register a new language locale
205218
* - locale: locale name, e.g. en / zh_CN, notice the standard.
@@ -211,10 +224,10 @@ function () {
211224
* timeagoLib.register('the locale name', the_locale_func);
212225
* // or
213226
* timeagoLib.register('pl', require('timeago.js/locales/pl'));
214-
**/
215-
timeago.register = function(locale, localeFunc) {
227+
**/
228+
timeagoFactory.register = function(locale, localeFunc) {
216229
locales[locale] = localeFunc;
217230
};
218231

219-
return timeago;
232+
return timeagoFactory;
220233
});

tests/locales_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ function testLocales(tobeTested) {
1818

1919
const localeFn = require('../locales/' + localeName);
2020
// test locales
21-
let newTimeAgo = timeago(null, '2016-06-23');
21+
let newTimeAgo = timeago('2016-06-23', null);
2222
timeago.register(localeName, localeFn);
2323
t.equal(newTimeAgo.format('2016-06-22', localeName), localeFn(1, 6)[0]);
2424
t.equal(newTimeAgo.format('2016-06-25', localeName), localeFn(2, 7)[1].replace('%s', '2'));
2525

2626
// test default locale
27-
newTimeAgo = timeago(localeName, '2016-03-01 12:00:00');
27+
newTimeAgo = timeago('2016-03-01 12:00:00', localeName);
2828
timeago.register(localeName, localeFn);
2929
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), localeFn(2, 7)[0].replace('%s', '2'));
3030

3131
// test setLocale
32-
newTimeAgo = timeago(null, '2016-03-01 12:00:00');
32+
newTimeAgo = timeago('2016-03-01 12:00:00');
3333
timeago.register(localeName, localeFn);
3434
newTimeAgo.setLocale(localeName);
3535
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), localeFn(2, 7)[0].replace('%s', '2'));

tests/test-builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TimeagoBuilder {
1212
this.nowDate = new Date(nowDate);
1313

1414
/** @private */
15-
this.timeago = new timeagoLib(null, nowDate);
15+
this.timeago = new timeagoLib(nowDate);
1616

1717
this.useLocale(defaultLocale);
1818
}

0 commit comments

Comments
 (0)