Skip to content

Commit c5fd700

Browse files
committed
refactor: rewrite with typescript, remove jquery support
1 parent 1216bfa commit c5fd700

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1065
-1069
lines changed

.babelrc

Lines changed: 0 additions & 24 deletions
This file was deleted.

.commitlintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [
4+
2,
5+
'always',
6+
['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test', 'wip'],
7+
],
8+
},
9+
};

.eslintrc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"parserOptions": {
4+
"project": "./tsconfig.ci.json"
5+
},
6+
"plugins": ["@typescript-eslint"],
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended",
11+
"prettier",
12+
"prettier/@typescript-eslint"
13+
],
14+
"rules": {
15+
"@typescript-eslint/ban-ts-ignore": 1,
16+
"@typescript-eslint/camelcase": 0
17+
}
18+
}

.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"bracketSpacing": true,
6+
"printWidth": 120,
7+
"arrowParens": "always"
8+
}

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ install:
1313
- export DISPLAY=':99.0'
1414
- Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
1515
- npm install
16+
script:
17+
- npm run ci
1618
after_success:
1719
- npm run coveralls

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ npm install timeago.js
5050

5151
- Import
5252

53-
```js
54-
// ES6
53+
```ts
5554
import { format, render, cancel, register } from 'timeago.js';
5655

57-
// commonjs
58-
const { format, render, cancel, register } = require('timeago.js');
56+
// or
57+
import * as timeago from 'timeago.js';
5958
```
6059

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

6766
- Usage
6867

69-
```js
68+
```ts
7069
// format the time with locale
7170
format('2016-06-12', 'en_US');
7271
```
@@ -89,7 +88,7 @@ There only 4 API:
8988

9089
Just format date into a string.
9190

92-
```js
91+
```ts
9392
import { format, render, cancel, register } from 'timeago.js';
9493

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

119118
```html
120-
<div class="needs_to_be_rendered" datetime="2016-06-30 09:20:00"></div>
119+
<div class="needs-tobe-rendered" datetime="2016-06-30 09:20:00"></div>
121120
```
122121

123122
Javascript code:
124123

125-
```js
126-
var nodes = document.querySelectorAll('.needs_to_be_rendered');
124+
```ts
125+
import * as timeago from 'timeago.js';
126+
127+
const nodes = document.querySelectorAll('.needs-tobe-rendered');
127128

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

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

140141
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.
141142

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

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

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

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

192193

193194

README_zh.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ npm install timeago.js
4848

4949
- 引入
5050

51-
```js
52-
// ES6
51+
```ts
5352
import { format, render, cancel, register } from 'timeago.js';
5453

55-
// commonjs
56-
const { format, render, cancel, register } = require('timeago.js');
54+
// or
55+
import * as timeago from 'timeago.js';
5756
```
5857

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

6564
- 使用
6665

67-
```js
66+
```ts
6867
// 格式化日期
6968
format('2016-06-12', 'en_US');
7069
```
@@ -115,13 +114,13 @@ format(Date.now() - 11 * 1000 * 60 * 60); // returns '11 hours ago'
115114
HTML code:
116115

117116
```html
118-
<div class="needs_to_be_rendered" datetime="2016-06-30 09:20:00"></div>
117+
<div class="needs-tobe-rendered" datetime="2016-06-30 09:20:00"></div>
119118
```
120119

121120
Javascript code:
122121

123122
```js
124-
var nodes = document.querySelectorAll('.needs_to_be_rendered');
123+
var nodes = document.querySelectorAll('.needs-tobe-rendered');
125124

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

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

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

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

147146
```js
148147
// the local dict example is below.
149-
const localeFunc = (number, index, total_sec) => {
148+
const localeFunc = (number, index, totalSec) => {
150149
// number: the timeago / timein number;
151150
// index: the index of array below;
152-
// total_sec: total seconds between date to be formatted and today's date;
151+
// totalSec: total seconds between date to be formatted and today's date;
153152
return [
154153
['just now', 'right now'],
155154
['%s seconds ago', 'in %s seconds'],

__tests__/index.spec.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

__tests__/index.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Created by hustcc on 18/5/20.
3+
* Contract: [email protected]
4+
*/
5+
6+
import { format, render, cancel, register } from '../src/';
7+
8+
describe('index', () => {
9+
test('all', () => {
10+
expect(format).toBeInstanceOf(Function);
11+
expect(render).toBeInstanceOf(Function);
12+
expect(cancel).toBeInstanceOf(Function);
13+
expect(register).toBeInstanceOf(Function);
14+
});
15+
16+
test('format', () => {
17+
expect(format(+new Date() - 5000)).toBe('just now');
18+
19+
expect(format(+new Date() - 1000 * 1000, 'zh_CN')).toBe('16 分钟前');
20+
});
21+
22+
test('cancel', () => {
23+
cancel();
24+
25+
const node = {
26+
getAttribute: () => 1,
27+
};
28+
29+
// @ts-ignore
30+
cancel(node);
31+
});
32+
});

__tests__/lang/cs.spec.js renamed to __tests__/lang/cs.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
1+
import { advanceTo, clear } from 'jest-date-mock';
22
import { register, format } from '../../src';
33
import cs from '../../src/lang/cs';
44

@@ -28,7 +28,6 @@ describe('cs', () => {
2828
test('minute', () => {
2929
advanceTo(1000 * 60);
3030
expect(format(date, 'cs')).toEqual('před minutou');
31-
3231
});
3332

3433
test('minutes', () => {
@@ -49,7 +48,6 @@ describe('cs', () => {
4948
test('yesterday', () => {
5049
advanceTo(1000 * 60 * 60 * 24);
5150
expect(format(date, 'cs')).toEqual('včera');
52-
5351
});
5452

5553
test('days', () => {
@@ -60,7 +58,6 @@ describe('cs', () => {
6058
test('last week', () => {
6159
advanceTo(1000 * 60 * 60 * 24 * 7);
6260
expect(format(date, 'cs')).toEqual('minulý týden');
63-
6461
});
6562

6663
test('weeks', () => {

__tests__/lang/fa.spec.js renamed to __tests__/lang/fa.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
1+
import { advanceTo, clear } from 'jest-date-mock';
22
import { register, format } from '../../src';
33
import fa from '../../src/lang/fa';
44

__tests__/lang/he.spec.js renamed to __tests__/lang/he.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
1+
import { advanceTo, clear } from 'jest-date-mock';
22
import { register, format } from '../../src';
33
import he from '../../src/lang/he';
44

__tests__/lang/hi_IN.spec.js renamed to __tests__/lang/hi_IN.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* Created by porcus on 2018/10/09.
33
* Contact: [email protected]
44
*/
5-
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
5+
import { advanceTo, clear } from 'jest-date-mock';
66
import { format, register } from '../../src';
77
import hi_IN from '../../src/lang/hi_IN';
88

99
register('hi_IN', hi_IN);
1010

11-
1211
let date = new Date();
1312

1413
beforeEach(() => {
File renamed without changes.

__tests__/lang/it.spec.js renamed to __tests__/lang/it.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
1+
import { advanceTo, clear } from 'jest-date-mock';
22
import { register, format } from '../../src';
33
import it from '../../src/lang/it';
44

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

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

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

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

7676
advanceTo(-1000 * 60 * 60 * 8);
7777
expect(format(date, 'it')).toEqual('fra 8 ore');

__tests__/lang/ka.spec.js renamed to __tests__/lang/ka.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* Created by hustcc on 18/5/24.
33
* Contact: [email protected]
44
*/
5-
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
5+
import { advanceTo, clear } from 'jest-date-mock';
66
import { format, register } from '../../src';
77
import ka from '../../src/lang/ka';
88

99
register('ka', ka);
1010

11-
1211
let date = new Date();
1312

1413
beforeEach(() => {

0 commit comments

Comments
 (0)