Skip to content

Commit

Permalink
update code from likerRr hustcc#68
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Sep 19, 2016
1 parent c4d3101 commit c38d84a
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 90 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,22 @@ timeagoInstance.format('2016-06-12');

# 2. Detailed Usage

**1. Localization**

Default locale is **`en`**, and the library supports `en` and `zh_CN`.

```js
var timeagoInstance = new timeago();
timeagoInstance.format('2016-06-12', 'zh_CN');
```

You can change the locale in the constructor or use the `setLocale` method;

```js
var timeagoInstance = new timeago('zh_CN');
// or
new timeago().setLocale('zh_CN');
```

**2. Set relative date**
**1. Set relative date**

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

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

**3. Use timestamp**
**2. Use timestamp**

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

**4. Automatic rendering**
**3. Automatic rendering**

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

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

**4. Localization**

Default locale is **`en`**, and the library supports `en` and `zh_CN`.

```js
var timeagoInstance = new timeago();
timeagoInstance.format('2016-06-12', 'zh_CN');
```

You can change the locale in the constructor or use the `setLocale` method;

```js
var timeagoInstance = new timeago(currentDate, 'zh_CN');
// or
new timeago().setLocale('zh_CN');
```

**5. Register local language**

You can `register` your own language, this is a class static method. Like below, e.g.
Expand Down
32 changes: 16 additions & 16 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,6 @@ timeago.format('2016-06-12')

# 高级特性使用

**1. 本地化**

默认的语言是英文 **`en`**, 这个库自带语言有 `en` and `zh_CN` (英文和中文).

```js
var timeagoInstance = new timeago();
timeagoInstance.format('2016-06-12', 'zh_CN');
```

可以在构造函数中传入默认语言,也可以调用 `setLocale` 方法。

```js
var timeagoInstance = new timeago('zh_CN');
// or
new timeago().setLocale('zh_CN');
```

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

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

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

**4. 本地化**

默认的语言是英文 **`en`**, 这个库自带语言有 `en` and `zh_CN` (英文和中文).

```js
var timeagoInstance = new timeago();
timeagoInstance.format('2016-06-12', 'zh_CN');
```

可以在构造函数中传入默认语言,也可以调用 `setLocale` 方法。

```js
var timeagoInstance = new timeago(currentDate, 'zh_CN');
// or
new timeago().setLocale('zh_CN');
```

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

Expand Down
2 changes: 1 addition & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ new timeago().render(document.querySelectorAll('.need_to_be_rendered'));

// 2. demo
document.getElementById('demo_now').innerHTML = new timeago().format(new Date());
document.getElementById('demo_20160907').innerHTML = new timeago('zh_CN').format('2016-09-07');
document.getElementById('demo_20160907').innerHTML = new timeago(null, 'zh_CN').format('2016-09-07');
document.getElementById('demo_timestamp').innerHTML = new timeago().format(1473245023718);
45 changes: 29 additions & 16 deletions dist/timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function () {
// second, minute, hour, day, week, month, year(365 days)
SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
SEC_ARRAY_LEN = 6,
ATTR_DATETIME = 'datetime',
ATTR_DATETIME = 'datetime';

/**
* timeago: the function to get `timeago` instance.
Expand All @@ -45,21 +45,19 @@ function () {
* How to use it?
* var timeagoLib = require('timeago.js');
* var timeago = timeagoLib(); // all use default.
* var timeago = timeagoLib('zh_CN'); // set default locale is `zh_CN`.
* var timeago = timeagoLib(null, '2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
* 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天前.
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
* 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天前.
**/
timeago = function(defaultLocale, nowDate) {
function timeago(nowDate, defaultLocale) {
var timers = {}; // real-time render timers
// if do not set the defaultLocale, set it with `en`
if (! defaultLocale) {
defaultLocale = 'en'; // use default build-in locale
}
if (! defaultLocale) defaultLocale = 'en'; // use default build-in locale
// calculate the diff second between date to be formated an now date.
function diffSec(date) {
var now = new Date();
if (nowDate) now = toDate(nowDate);
return (now.getTime() - toDate(date).getTime()) / 1000;
return (now - toDate(date)) / 1000;
}
// format the diff second to *** time ago, with setting locale
function formatDiff(diff, locale) {
Expand Down Expand Up @@ -136,14 +134,14 @@ function () {
// return leftSec(d, rst);
d = d % rst;
d = d ? rst - d : rst;
return Math.ceil(diff);
return Math.ceil(d);
// }; // for dev test
}
// what the timer will do
function doRender(node, date, locale, cnt) {
var diff = diffSec(date);
node.innerHTML = formatDiff(diff, locale);
// 通过diff来判断下一次执行的时间
// waiting %s seconds, do the next render
timers['k' + cnt] = setTimeout(function() {
doRender(node, date, locale, cnt);
}, nextInterval(diff) * 1000);
Expand All @@ -165,7 +163,7 @@ function () {
* // 2. use jQuery selector
* timeago.render($('.need_to_be_rendered'), 'pl');
*
* Notice: please be sure the dom has attribute `data-timeago`.
* Notice: please be sure the dom has attribute `datetime`.
**/
this.render = function(nodes, locale) {
if (nodes.length === undefined) nodes = [nodes];
Expand Down Expand Up @@ -199,7 +197,22 @@ function () {
defaultLocale = locale;
};
return this;
};
}
/**
* timeago: the function to get `timeago` instance.
* - nowDate: the relative date, default is new Date().
* - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
*
* How to use it?
* var timeagoLib = require('timeago.js');
* var timeago = timeagoLib(); // all use default.
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
* 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天前.
**/
function timeagoFactory(nowDate, defaultLocale) {
return new timeago(nowDate, defaultLocale);
}
/**
* register: register a new language locale
* - locale: locale name, e.g. en / zh_CN, notice the standard.
Expand All @@ -211,10 +224,10 @@ function () {
* timeagoLib.register('the locale name', the_locale_func);
* // or
* timeagoLib.register('pl', require('timeago.js/locales/pl'));
**/
timeago.register = function(locale, localeFunc) {
**/
timeagoFactory.register = function(locale, localeFunc) {
locales[locale] = localeFunc;
};

return timeago;
return timeagoFactory;
});
2 changes: 1 addition & 1 deletion dist/timeago.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h3>Why ?</h3>
<ul>
<li>Avoid timestamps dated "1 minute ago" even though the page was opened 10 minutes ago; <em>timeago refreshes automatically</em>.</li>
<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>
<li>Very tiny(~2kb), and has <em>no dependency</em>, e.g. jQuery.</li>
<li>Very tiny(less than 2kb), and has <em>no dependency</em>, e.g. jQuery.</li>
</ul>

<h3>How ?</h3>
Expand Down Expand Up @@ -93,7 +93,7 @@ <h3>How ?</h3>
</p>
<pre>
new timeago().format(new Date()); //=> &quot;<span id="demo_now"></span>&quot;
new timeago('zh_CN').format('2016-09-07') //=> &quot;<span id="demo_20160907"></span>&quot;
new timeago(null, 'zh_CN').format('2016-09-07') //=> &quot;<span id="demo_20160907"></span>&quot;
new timeago().format(1473245023718); //=> &quot;<span id="demo_timestamp"></span>&quot;</pre>

<h3>Locales register ?</h3>
Expand Down
45 changes: 29 additions & 16 deletions src/timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function () {
// second, minute, hour, day, week, month, year(365 days)
SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
SEC_ARRAY_LEN = 6,
ATTR_DATETIME = 'datetime',
ATTR_DATETIME = 'datetime';

/**
* timeago: the function to get `timeago` instance.
Expand All @@ -45,21 +45,19 @@ function () {
* How to use it?
* var timeagoLib = require('timeago.js');
* var timeago = timeagoLib(); // all use default.
* var timeago = timeagoLib('zh_CN'); // set default locale is `zh_CN`.
* var timeago = timeagoLib(null, '2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
* 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天前.
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
* 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天前.
**/
timeago = function(defaultLocale, nowDate) {
function timeago(nowDate, defaultLocale) {
var timers = {}; // real-time render timers
// if do not set the defaultLocale, set it with `en`
if (! defaultLocale) {
defaultLocale = 'en'; // use default build-in locale
}
if (! defaultLocale) defaultLocale = 'en'; // use default build-in locale
// calculate the diff second between date to be formated an now date.
function diffSec(date) {
var now = new Date();
if (nowDate) now = toDate(nowDate);
return (now.getTime() - toDate(date).getTime()) / 1000;
return (now - toDate(date)) / 1000;
}
// format the diff second to *** time ago, with setting locale
function formatDiff(diff, locale) {
Expand Down Expand Up @@ -136,14 +134,14 @@ function () {
// return leftSec(d, rst);
d = d % rst;
d = d ? rst - d : rst;
return Math.ceil(diff);
return Math.ceil(d);
// }; // for dev test
}
// what the timer will do
function doRender(node, date, locale, cnt) {
var diff = diffSec(date);
node.innerHTML = formatDiff(diff, locale);
// 通过diff来判断下一次执行的时间
// waiting %s seconds, do the next render
timers['k' + cnt] = setTimeout(function() {
doRender(node, date, locale, cnt);
}, nextInterval(diff) * 1000);
Expand All @@ -165,7 +163,7 @@ function () {
* // 2. use jQuery selector
* timeago.render($('.need_to_be_rendered'), 'pl');
*
* Notice: please be sure the dom has attribute `data-timeago`.
* Notice: please be sure the dom has attribute `datetime`.
**/
this.render = function(nodes, locale) {
if (nodes.length === undefined) nodes = [nodes];
Expand Down Expand Up @@ -199,7 +197,22 @@ function () {
defaultLocale = locale;
};
return this;
};
}
/**
* timeago: the function to get `timeago` instance.
* - nowDate: the relative date, default is new Date().
* - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
*
* How to use it?
* var timeagoLib = require('timeago.js');
* var timeago = timeagoLib(); // all use default.
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
* 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天前.
**/
function timeagoFactory(nowDate, defaultLocale) {
return new timeago(nowDate, defaultLocale);
}
/**
* register: register a new language locale
* - locale: locale name, e.g. en / zh_CN, notice the standard.
Expand All @@ -211,10 +224,10 @@ function () {
* timeagoLib.register('the locale name', the_locale_func);
* // or
* timeagoLib.register('pl', require('timeago.js/locales/pl'));
**/
timeago.register = function(locale, localeFunc) {
**/
timeagoFactory.register = function(locale, localeFunc) {
locales[locale] = localeFunc;
};

return timeago;
return timeagoFactory;
});
6 changes: 3 additions & 3 deletions tests/locales_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ function testLocales(tobeTested) {

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

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

// test setLocale
newTimeAgo = timeago(null, '2016-03-01 12:00:00');
newTimeAgo = timeago('2016-03-01 12:00:00');
timeago.register(localeName, localeFn);
newTimeAgo.setLocale(localeName);
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), localeFn(2, 7)[0].replace('%s', '2'));
Expand Down
2 changes: 1 addition & 1 deletion tests/test-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TimeagoBuilder {
this.nowDate = new Date(nowDate);

/** @private */
this.timeago = new timeagoLib(null, nowDate);
this.timeago = new timeagoLib(nowDate);

this.useLocale(defaultLocale);
}
Expand Down
Loading

0 comments on commit c38d84a

Please sign in to comment.