Skip to content

Commit

Permalink
feat: refactor using typescript (#60)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 16 support
  • Loading branch information
fengmk2 authored Jan 13, 2024
1 parent d3a02ad commit e20c1d9
Show file tree
Hide file tree
Showing 90 changed files with 2,472 additions and 4,222 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
6 changes: 2 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ on:
pull_request:
branches: [ master ]

workflow_dispatch: {}

jobs:
Job:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '8, 10, 12, 14, 16, 18, 20'
version: '16, 18, 20'
6 changes: 1 addition & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ on:
push:
branches: [ master ]

workflow_dispatch: {}

jobs:
release:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ npm-debug.log
.DS_Store
.idea
.nyc_output
.tshy*
dist
6 changes: 0 additions & 6 deletions .jshintignore

This file was deleted.

95 changes: 0 additions & 95 deletions .jshintrc

This file was deleted.

8 changes: 0 additions & 8 deletions AUTHORS

This file was deleted.

33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ A collection of useful utilities.
## Install

```bash
$ npm install utility
npm install utility
```

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

```ts
import utility from 'utility';
import * as utility from 'utility';
```

### md5
Expand Down Expand Up @@ -212,7 +212,9 @@ const res = utils.try(function () {
// {error: undefined, value: {foo: 'bar'}}
// {error: Error, value: undefined}
```

```Note``` that when you use ```typescript```, you must use the following methods to call ' Try '

```js
import * as utility from 'utility';

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

```js
function() {
function foo() {
const arr = utility.argumentsToArray(arguments);
console.log(arr.join(', '));
}
Expand Down Expand Up @@ -251,21 +253,19 @@ async () => {
}
```

> **Hint:** In `utils.writeJSON*()`, if `pkg` is an object, the **optional** third parameter `options` may contain two
> __Hint:__ In `utils.writeJSON*()`, if `pkg` is an object, the __optional__ third parameter `options` may contain two
> keys.
>
> + `replacer`: Equals to `JSON.stringify()`'s second parameter;
> + `space`: Equals to `JSON.stringify()`'s third parameter. Defaults to `2`.
>
> Refs:
>
> + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
> + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument
> + <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter>
> + <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument>
### Object.assign


```js
// assign object
utility.assign({}, { a: 1 });
Expand All @@ -276,11 +276,11 @@ utility.assign({}, [ { a: 1 }, { b: 1 } ]);

## benchmark

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

```bash
$ node benchmark/date_format.js
$ node benchmark/date_format.cjs

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

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

```bash
$ node benchmark/date_YYYYMMDD.js
$ node benchmark/date_YYYYMMDD.cjs

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

## Contributors

|[<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/>|
|[<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/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
[<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/>
|[<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/>|
[<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/>

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`.
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`.

<!-- GITCONTRIBUTOR_END -->

Expand Down
45 changes: 45 additions & 0 deletions benchmark/YYYYMMDDHHmmss.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');
const utility = require('../');
const moment = require('moment');

const suite = new Benchmark.Suite();

console.log(utility.YYYYMMDDHHmmss());
console.log(moment().format('YYYY-MM-DD HH:mm:ss'));

// add tests
suite

.add('utility.YYYYMMDDHHmmss()', function() {
utility.YYYYMMDDHHmmss();
})
.add("moment().format('YYYY-MM-DD HH:mm:ss')", function() {
moment().format('YYYY-MM-DD HH:mm:ss');
})

// add listeners
.on('cycle', function(event) {
benchmarks.add(event.target);
})
.on('start', function() {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', function() {
benchmarks.log();
})
// run async
.run({ async: false });

// $ node benchmark/YYYYMMDDHHmmss.js
//
// 2014-06-19 12:37:15
// 2014-06-19 12:37:15
//
// node version: v0.11.12, date: Thu Jun 19 2014 12:37:15 GMT+0800 (CST)
// Starting...
// 2 tests completed.
//
// utility.YYYYMMDDHHmmss() x 2,878,879 ops/sec ±2.44% (96 runs sampled)
// moment().format('YYYY-MM-DD HH:mm:ss') x 138,521 ops/sec ±1.11% (96 runs sampled)
58 changes: 0 additions & 58 deletions benchmark/YYYYMMDDHHmmss.js

This file was deleted.

Loading

0 comments on commit e20c1d9

Please sign in to comment.