Skip to content

Commit

Permalink
refactor: rewrite with typescript, remove jquery support
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Oct 12, 2019
1 parent 1216bfa commit c5fd700
Show file tree
Hide file tree
Showing 105 changed files with 1,065 additions and 1,069 deletions.
24 changes: 0 additions & 24 deletions .babelrc

This file was deleted.

9 changes: 9 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
rules: {
'type-enum': [
2,
'always',
['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test', 'wip'],
],
},
};
18 changes: 18 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.ci.json"
},
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
],
"rules": {
"@typescript-eslint/ban-ts-ignore": 1,
"@typescript-eslint/camelcase": 0
}
}
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"printWidth": 120,
"arrowParens": "always"
}
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ install:
- export DISPLAY=':99.0'
- Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
- npm install
script:
- npm run ci
after_success:
- npm run coveralls
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ npm install timeago.js

- Import

```js
// ES6
```ts
import { format, render, cancel, register } from 'timeago.js';

// commonjs
const { format, render, cancel, register } = require('timeago.js');
// or
import * as timeago from 'timeago.js';
```

or import with `script` in html file and access global variable `timeago`.
Expand All @@ -66,7 +65,7 @@ or import with `script` in html file and access global variable `timeago`.

- Usage

```js
```ts
// format the time with locale
format('2016-06-12', 'en_US');
```
Expand All @@ -89,7 +88,7 @@ There only 4 API:

Just format date into a string.

```js
```ts
import { format, render, cancel, register } from 'timeago.js';

// format timestamp
Expand Down Expand Up @@ -117,13 +116,15 @@ You can `render` a dom node with automatic rendering.
HTML code:

```html
<div class="needs_to_be_rendered" datetime="2016-06-30 09:20:00"></div>
<div class="needs-tobe-rendered" datetime="2016-06-30 09:20:00"></div>
```

Javascript code:

```js
var nodes = document.querySelectorAll('.needs_to_be_rendered');
```ts
import * as timeago from 'timeago.js';

const nodes = document.querySelectorAll('.needs-tobe-rendered');

// use render method to render nodes in real time
timeago.render(nodes, 'zh_CN');
Expand All @@ -135,7 +136,7 @@ timeago.cancel();
timeago.cancel(nodes[0])
```

The input for `render` method should be DOM object / array, pure javascript DOM node or jQuery DOM object supported.
The input for `render` method should be DOM object / array, pure javascript DOM node ~~or jQuery DOM object supported~~.

The `cancel` method clears all the render timers and release all resources of the instance. Optionally it accepts a single node to cancel timer just for it.

Expand All @@ -146,12 +147,12 @@ The `cancel` method clears all the render timers and release all resources of th

Default locale is **`en_US`**, and the library supports `en_US` and `zh_CN`. You can register your own language with `register`.

```js
```ts
// the local dict example is below.
const localeFunc = (number, index, total_sec) => {
const localeFunc = (number: number, index: number, totalSec: number): [string, string] => {
// number: the timeago / timein number;
// index: the index of array below;
// total_sec: total seconds between date to be formatted and today's date;
// totalSec: total seconds between date to be formatted and today's date;
return [
['just now', 'right now'],
['%s seconds ago', 'in %s seconds'],
Expand Down Expand Up @@ -187,7 +188,7 @@ Check out more [locales](src/lang).
2. **locale translations**: The library needs more locale translations. You can:

- Open an issue to write the locale translations, or submit a pull request. How to ? see [locales translation](src/lang/).
- Please **test** the locale by exec `npm test`. How to write testcase, see [locales test cases](__tests__/lang/).
- Please **test** the locale by exec `npm test`. How to write test cases, see [locales test cases](__tests__/lang/).



Expand Down
21 changes: 10 additions & 11 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ npm install timeago.js

- 引入

```js
// ES6
```ts
import { format, render, cancel, register } from 'timeago.js';

// commonjs
const { format, render, cancel, register } = require('timeago.js');
// or
import * as timeago from 'timeago.js';
```

或者使用 `script` 在 html 文件中引入,然后就可以使用全局的变量 `timeago`
Expand All @@ -64,7 +63,7 @@ const { format, render, cancel, register } = require('timeago.js');

- 使用

```js
```ts
// 格式化日期
format('2016-06-12', 'en_US');
```
Expand Down Expand Up @@ -115,13 +114,13 @@ format(Date.now() - 11 * 1000 * 60 * 60); // returns '11 hours ago'
HTML code:

```html
<div class="needs_to_be_rendered" datetime="2016-06-30 09:20:00"></div>
<div class="needs-tobe-rendered" datetime="2016-06-30 09:20:00"></div>
```

Javascript code:

```js
var nodes = document.querySelectorAll('.needs_to_be_rendered');
var nodes = document.querySelectorAll('.needs-tobe-rendered');

// use render method to render nodes in real time
timeago.render(nodes, 'zh_CN');
Expand All @@ -133,9 +132,9 @@ timeago.cancel();
timeago.cancel(nodes[0])
```

`render` 函数的输入必须是一个 dom 元素或者数组,JavaScript dom 和 JQuery 的 dom 均支持
`render` 函数的输入必须是一个 dom 元素或者数组,JavaScript dom ~~和 JQuery 的 dom ~~支持

`cancel` 清楚实时渲染,如果传入 dom,则清除这个 dom 的实时渲染,否则清除所有。
`cancel` 清除实时渲染,如果传入 dom,则清除这个 dom 的实时渲染,否则清除所有。

> 被渲染的 dom 元素必须包含一个 `datetime` 属性,用于被格式化的日期。
Expand All @@ -146,10 +145,10 @@ timeago.cancel(nodes[0])

```js
// the local dict example is below.
const localeFunc = (number, index, total_sec) => {
const localeFunc = (number, index, totalSec) => {
// number: the timeago / timein number;
// index: the index of array below;
// total_sec: total seconds between date to be formatted and today's date;
// totalSec: total seconds between date to be formatted and today's date;
return [
['just now', 'right now'],
['%s seconds ago', 'in %s seconds'],
Expand Down
43 changes: 0 additions & 43 deletions __tests__/index.spec.js

This file was deleted.

32 changes: 32 additions & 0 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Created by hustcc on 18/5/20.
* Contract: [email protected]
*/

import { format, render, cancel, register } from '../src/';

describe('index', () => {
test('all', () => {
expect(format).toBeInstanceOf(Function);
expect(render).toBeInstanceOf(Function);
expect(cancel).toBeInstanceOf(Function);
expect(register).toBeInstanceOf(Function);
});

test('format', () => {
expect(format(+new Date() - 5000)).toBe('just now');

expect(format(+new Date() - 1000 * 1000, 'zh_CN')).toBe('16 分钟前');
});

test('cancel', () => {
cancel();

const node = {
getAttribute: () => 1,
};

// @ts-ignore
cancel(node);
});
});
5 changes: 1 addition & 4 deletions __tests__/lang/cs.spec.js → __tests__/lang/cs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
import { advanceTo, clear } from 'jest-date-mock';
import { register, format } from '../../src';
import cs from '../../src/lang/cs';

Expand Down Expand Up @@ -28,7 +28,6 @@ describe('cs', () => {
test('minute', () => {
advanceTo(1000 * 60);
expect(format(date, 'cs')).toEqual('před minutou');

});

test('minutes', () => {
Expand All @@ -49,7 +48,6 @@ describe('cs', () => {
test('yesterday', () => {
advanceTo(1000 * 60 * 60 * 24);
expect(format(date, 'cs')).toEqual('včera');

});

test('days', () => {
Expand All @@ -60,7 +58,6 @@ describe('cs', () => {
test('last week', () => {
advanceTo(1000 * 60 * 60 * 24 * 7);
expect(format(date, 'cs')).toEqual('minulý týden');

});

test('weeks', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/lang/fa.spec.js → __tests__/lang/fa.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
import { advanceTo, clear } from 'jest-date-mock';
import { register, format } from '../../src';
import fa from '../../src/lang/fa';

Expand Down
2 changes: 1 addition & 1 deletion __tests__/lang/he.spec.js → __tests__/lang/he.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
import { advanceTo, clear } from 'jest-date-mock';
import { register, format } from '../../src';
import he from '../../src/lang/he';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
* Created by porcus on 2018/10/09.
* Contact: [email protected]
*/
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
import { advanceTo, clear } from 'jest-date-mock';
import { format, register } from '../../src';
import hi_IN from '../../src/lang/hi_IN';

register('hi_IN', hi_IN);


let date = new Date();

beforeEach(() => {
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions __tests__/lang/it.spec.js → __tests__/lang/it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
import { advanceTo, clear } from 'jest-date-mock';
import { register, format } from '../../src';
import it from '../../src/lang/it';

Expand Down Expand Up @@ -28,7 +28,7 @@ describe('it', () => {
expect(format(date, 'it')).toEqual('30 minuti fa');

advanceTo(1000 * 60 * 60);
expect(format(date, 'it')).toEqual('un\'ora fa');
expect(format(date, 'it')).toEqual("un'ora fa");

advanceTo(1000 * 60 * 60 * 8);
expect(format(date, 'it')).toEqual('8 ore fa');
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('it', () => {
expect(format(date, 'it')).toEqual('fra 30 minuti');

advanceTo(-1000 * 60 * 60);
expect(format(date, 'it')).toEqual('fra un\'ora');
expect(format(date, 'it')).toEqual("fra un'ora");

advanceTo(-1000 * 60 * 60 * 8);
expect(format(date, 'it')).toEqual('fra 8 ore');
Expand Down
3 changes: 1 addition & 2 deletions __tests__/lang/ka.spec.js → __tests__/lang/ka.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
* Created by hustcc on 18/5/24.
* Contact: [email protected]
*/
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
import { advanceTo, clear } from 'jest-date-mock';
import { format, register } from '../../src';
import ka from '../../src/lang/ka';

register('ka', ka);


let date = new Date();

beforeEach(() => {
Expand Down
Loading

0 comments on commit c5fd700

Please sign in to comment.