Skip to content

Commit e20c1d9

Browse files
authored
feat: refactor using typescript (#60)
BREAKING CHANGE: drop Node.js < 16 support
1 parent d3a02ad commit e20c1d9

Some content is hidden

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

90 files changed

+2472
-4222
lines changed

.eslintrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
6+
}

.github/workflows/nodejs.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ on:
77
pull_request:
88
branches: [ master ]
99

10-
workflow_dispatch: {}
11-
1210
jobs:
1311
Job:
1412
name: Node.js
15-
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
13+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1614
with:
1715
os: 'ubuntu-latest'
18-
version: '8, 10, 12, 14, 16, 18, 20'
16+
version: '16, 18, 20'

.github/workflows/release.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ on:
44
push:
55
branches: [ master ]
66

7-
workflow_dispatch: {}
8-
97
jobs:
108
release:
119
name: Node.js
12-
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
10+
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
1311
secrets:
1412
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1513
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
16-
with:
17-
checkTest: false

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ npm-debug.log
1616
.DS_Store
1717
.idea
1818
.nyc_output
19+
.tshy*
20+
dist

.jshintignore

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

.jshintrc

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

AUTHORS

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

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ A collection of useful utilities.
1717
## Install
1818

1919
```bash
20-
$ npm install utility
20+
npm install utility
2121
```
2222

2323
## Usage
@@ -29,7 +29,7 @@ const utils = require('utility');
2929
Also you can use it within typescript, like this ↓
3030

3131
```ts
32-
import utility from 'utility';
32+
import * as utility from 'utility';
3333
```
3434

3535
### md5
@@ -212,7 +212,9 @@ const res = utils.try(function () {
212212
// {error: undefined, value: {foo: 'bar'}}
213213
// {error: Error, value: undefined}
214214
```
215+
215216
```Note``` that when you use ```typescript```, you must use the following methods to call ' Try '
217+
216218
```js
217219
import * as utility from 'utility';
218220

@@ -223,7 +225,7 @@ utility.UNSTABLE_METHOD.try(...);
223225
### argumentsToArray
224226

225227
```js
226-
function() {
228+
function foo() {
227229
const arr = utility.argumentsToArray(arguments);
228230
console.log(arr.join(', '));
229231
}
@@ -251,21 +253,19 @@ async () => {
251253
}
252254
```
253255

254-
> **Hint:** In `utils.writeJSON*()`, if `pkg` is an object, the **optional** third parameter `options` may contain two
256+
> __Hint:__ In `utils.writeJSON*()`, if `pkg` is an object, the __optional__ third parameter `options` may contain two
255257
> keys.
256258
>
257259
> + `replacer`: Equals to `JSON.stringify()`'s second parameter;
258260
> + `space`: Equals to `JSON.stringify()`'s third parameter. Defaults to `2`.
259261
>
260262
> Refs:
261263
>
262-
> + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
263-
> + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument
264-
264+
> + <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter>
265+
> + <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument>
265266
266267
### Object.assign
267268

268-
269269
```js
270270
// assign object
271271
utility.assign({}, { a: 1 });
@@ -276,11 +276,11 @@ utility.assign({}, [ { a: 1 }, { b: 1 } ]);
276276

277277
## benchmark
278278

279-
* [jsperf: access log date format](http://jsperf.com/access-log-date-format)
280-
* [benchmark/date_format.js](https://github.com/fengmk2/utility/blob/master/benchmark/date_format.js)
279+
+ [jsperf: access log date format](http://jsperf.com/access-log-date-format)
280+
+ [benchmark/date_format.js](https://github.com/node-modules/utility/blob/master/benchmark/date_format.cjs)
281281

282282
```bash
283-
$ node benchmark/date_format.js
283+
$ node benchmark/date_format.cjs
284284

285285
moment().format("DD/MMM/YYYY:HH:mm:ss ZZ"): "16/Apr/2013:21:12:32 +0800"
286286
utils.accessLogDate(): "16/Apr/2013:21:12:32 +0800"
@@ -300,10 +300,10 @@ Date.now() x 8,327,685 ops/sec ±1.85% (94 runs sampled)
300300
Fastest is Date.now()
301301
```
302302

303-
[benchmark/date_YYYYMMDD.js](https://github.com/fengmk2/utility/blob/master/benchmark/date_YYYYMMDD.js)
303+
[benchmark/date_YYYYMMDD.js](https://github.com/node-modules/utility/blob/master/benchmark/date_YYYYMMDD.cjs)
304304

305305
```bash
306-
$ node benchmark/date_YYYYMMDD.js
306+
$ node benchmark/date_YYYYMMDD.cjs
307307

308308
parseInt(moment().format("YYYYMMDD"), 10): 20130416
309309
utils.datestruct().YYYYMMDD: 20130416
@@ -319,11 +319,12 @@ Fastest is utils.datestruct().YYYYMMDD
319319

320320
## Contributors
321321

322-
|[<img src="https://avatars0.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars3.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars1.githubusercontent.com/u/1147375?v=4" width="100px;"/><br/><sub><b>alsotang</b></sub>](https://github.com/alsotang)<br/>|[<img src="https://avatars1.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars2.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub>](https://github.com/gxcsoccer)<br/>|[<img src="https://avatars3.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|
322+
|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/1147375?v=4" width="100px;"/><br/><sub><b>alsotang</b></sub>](https://github.com/alsotang)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/1207064?v=4" width="100px;"/><br/><sub><b>gxcsoccer</b></sub>](https://github.com/gxcsoccer)<br/>|[<img src="https://avatars.githubusercontent.com/u/5127897?v=4" width="100px;"/><br/><sub><b>danielsss</b></sub>](https://github.com/danielsss)<br/>|
323323
| :---: | :---: | :---: | :---: | :---: | :---: |
324-
[<img src="https://avatars1.githubusercontent.com/u/24466804?v=4" width="100px;"/><br/><sub><b>mosikoo</b></sub>](https://github.com/mosikoo)<br/>|[<img src="https://avatars2.githubusercontent.com/u/2569835?v=4" width="100px;"/><br/><sub><b>haoxins</b></sub>](https://github.com/haoxins)<br/>|[<img src="https://avatars1.githubusercontent.com/u/546535?v=4" width="100px;"/><br/><sub><b>leoner</b></sub>](https://github.com/leoner)<br/>|[<img src="https://avatars3.githubusercontent.com/u/33921398?v=4" width="100px;"/><br/><sub><b>ddzy</b></sub>](https://github.com/ddzy)<br/>
324+
|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|[<img src="https://avatars.githubusercontent.com/u/23133919?v=4" width="100px;"/><br/><sub><b>ulivz</b></sub>](https://github.com/ulivz)<br/>|[<img src="https://avatars.githubusercontent.com/u/24466804?v=4" width="100px;"/><br/><sub><b>mosikoo</b></sub>](https://github.com/mosikoo)<br/>|[<img src="https://avatars.githubusercontent.com/u/546535?v=4" width="100px;"/><br/><sub><b>leoner</b></sub>](https://github.com/leoner)<br/>|[<img src="https://avatars.githubusercontent.com/u/8603442?v=4" width="100px;"/><br/><sub><b>legend80s</b></sub>](https://github.com/legend80s)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|
325+
[<img src="https://avatars.githubusercontent.com/u/33921398?v=4" width="100px;"/><br/><sub><b>ddzy</b></sub>](https://github.com/ddzy)<br/>|[<img src="https://avatars.githubusercontent.com/u/8896314?v=4" width="100px;"/><br/><sub><b>zhanghengyao</b></sub>](https://github.com/zhanghengyao)<br/>
325326

326-
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sat Mar 23 2019 12:09:41 GMT+0800`.
327+
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Mon Dec 11 2023 00:23:06 GMT+0800`.
327328

328329
<!-- GITCONTRIBUTOR_END -->
329330

benchmark/YYYYMMDDHHmmss.cjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const Benchmark = require('benchmark');
3+
const benchmarks = require('beautify-benchmark');
4+
const utility = require('../');
5+
const moment = require('moment');
6+
7+
const suite = new Benchmark.Suite();
8+
9+
console.log(utility.YYYYMMDDHHmmss());
10+
console.log(moment().format('YYYY-MM-DD HH:mm:ss'));
11+
12+
// add tests
13+
suite
14+
15+
.add('utility.YYYYMMDDHHmmss()', function() {
16+
utility.YYYYMMDDHHmmss();
17+
})
18+
.add("moment().format('YYYY-MM-DD HH:mm:ss')", function() {
19+
moment().format('YYYY-MM-DD HH:mm:ss');
20+
})
21+
22+
// add listeners
23+
.on('cycle', function(event) {
24+
benchmarks.add(event.target);
25+
})
26+
.on('start', function() {
27+
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
28+
})
29+
.on('complete', function() {
30+
benchmarks.log();
31+
})
32+
// run async
33+
.run({ async: false });
34+
35+
// $ node benchmark/YYYYMMDDHHmmss.js
36+
//
37+
// 2014-06-19 12:37:15
38+
// 2014-06-19 12:37:15
39+
//
40+
// node version: v0.11.12, date: Thu Jun 19 2014 12:37:15 GMT+0800 (CST)
41+
// Starting...
42+
// 2 tests completed.
43+
//
44+
// utility.YYYYMMDDHHmmss() x 2,878,879 ops/sec ±2.44% (96 runs sampled)
45+
// moment().format('YYYY-MM-DD HH:mm:ss') x 138,521 ops/sec ±1.11% (96 runs sampled)

benchmark/YYYYMMDDHHmmss.js

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

0 commit comments

Comments
 (0)