Skip to content

Commit 3475532

Browse files
authored
feat: export getDateStringParts (#63)
1 parent 838e081 commit 3475532

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

.github/workflows/nodejs-14.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Node.js 14 CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Use Node.js
16+
uses: irby/setup-node-nvm@master
17+
with:
18+
node-version: '16.x'
19+
- run: npm install
20+
- run: npm run prepublishOnly
21+
- run: node -v
22+
- run: . /home/runner/mynvm/nvm.sh && nvm install 14 && nvm use 14 && node -v && npm run test-local

.github/workflows/nodejs.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: CI
33
on:
44
push:
55
branches: [ master ]
6-
76
pull_request:
87
branches: [ master ]
98

@@ -13,4 +12,4 @@ jobs:
1312
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1413
with:
1514
os: 'ubuntu-latest'
16-
version: '16, 18, 20, 21'
15+
version: '16, 18, 20, 22'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ npm-debug.log
1818
.nyc_output
1919
.tshy*
2020
dist
21+
package-lock.json

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,19 +315,12 @@ new Date().toString() x 816,731 ops/sec ±3.46% (93 runs sampled)
315315
Fastest is utils.datestruct().YYYYMMDD
316316
```
317317

318-
<!-- GITCONTRIBUTOR_START -->
319-
320-
## Contributors
321-
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/>|
323-
| :---: | :---: | :---: | :---: | :---: | :---: |
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/>
318+
## License
326319

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`.
320+
[MIT](LICENSE.txt)
328321

329-
<!-- GITCONTRIBUTOR_END -->
322+
## Contributors
330323

331-
## License
324+
[![Contributors](https://contrib.rocks/image?repo=node-modules/utility)](https://github.com/node-modules/utility/graphs/contributors)
332325

333-
[MIT](LICENSE.txt)
326+
Made with [contributors-img](https://contrib.rocks).

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"test-local": "egg-bin test",
1010
"preci": "npm run prepublishOnly",
1111
"ci": "egg-bin cov",
12-
"contributor": "git-contributor",
1312
"prepublishOnly": "tshy && tshy-after"
1413
},
1514
"dependencies": {
@@ -20,20 +19,18 @@
2019
"@eggjs/tsconfig": "^1.3.3",
2120
"@types/escape-html": "^1.0.4",
2221
"@types/mocha": "^10.0.6",
23-
"@types/node": "^20.10.0",
22+
"@types/node": "22",
2423
"beautify-benchmark": "^0.2.4",
2524
"benchmark": "^2.1.4",
26-
"contributors": "*",
2725
"egg-bin": "^6.5.2",
2826
"eslint": "^8.54.0",
2927
"eslint-config-egg": "^13.0.0",
30-
"git-contributor": "^2.1.5",
3128
"moment": "^2.22.2",
3229
"object-assign": "^4.1.1",
3330
"optimized": "^1.2.0",
3431
"time-require": "^0.1.2",
3532
"tsd": "^0.28.1",
36-
"tshy": "^1.8.1",
33+
"tshy": "^3.0.2",
3734
"tshy-after": "^1.0.0",
3835
"typescript": "^5.2.2"
3936
},
@@ -81,5 +78,6 @@
8178
"src"
8279
],
8380
"main": "./dist/commonjs/index.js",
84-
"types": "./dist/commonjs/index.d.ts"
81+
"types": "./dist/commonjs/index.d.ts",
82+
"module": "./dist/esm/index.js"
8583
}

src/date.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ const MONTHS: Record<string, string> = {
3333
'12': 'Dec',
3434
};
3535

36-
function getDateStringParts(d: Date, onlyDate?: boolean) {
36+
/**
37+
* return `[ YYYY, MM, DD, HH, mm, ss ]` date string array
38+
*/
39+
export function getDateStringParts(d?: Date, onlyDate?: boolean) {
40+
d = d || new Date();
3741
const monthNum = d.getMonth() + 1;
3842
const month = monthNum < 10 ? `0${monthNum}` : `${monthNum}`;
3943
const dateNum = d.getDate();

test/date.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import { strict as assert } from 'node:assert';
22
import moment from 'moment';
33
import * as utility from '../src/index.js';
44
import * as utils from '../src/index.js';
5-
import { YYYYMMDDHHmmss, logDate } from '../src/index.js';
5+
import { YYYYMMDDHHmmss, logDate, getDateStringParts } from '../src/index.js';
66

77
describe('test/date.test.ts', () => {
8+
describe('getDateStringParts()', () => {
9+
it('should work', () => {
10+
assert.match(getDateStringParts().join(','), /^\d{4},\d{2},\d{2},\d{2},\d{2},\d{2}$/);
11+
assert.match(utility.getDateStringParts(new Date(), true).join('.'), /^\d{4}\.\d{2}\.\d{2}$/);
12+
});
13+
});
14+
815
describe('YYYYMMDDHHmmss()', () => {
916
it('should return an "YYYY-MM-DD HH:mm:ss" format date string', () => {
1017
assert.match(utility.YYYYMMDDHHmmss(), /^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/);

0 commit comments

Comments
 (0)