Skip to content

Commit

Permalink
feat: export getDateStringParts
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 10, 2024
1 parent 838e081 commit 98e7a71
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 22 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/nodejs-14.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Node.js 14 CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: irby/setup-node-nvm@master
with:
node-version: '16.x'
- run: npm install
- run: npm run prepublishOnly
- run: node -v
- run: . /home/runner/mynvm/nvm.sh && nvm install 14 && nvm use 14 && node -v && npm run test-local
3 changes: 1 addition & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

Expand All @@ -13,4 +12,4 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '16, 18, 20, 21'
version: '16, 18, 20, 22'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ npm-debug.log
.nyc_output
.tshy*
dist
package-lock.json
17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,12 @@ new Date().toString() x 816,731 ops/sec ±3.46% (93 runs sampled)
Fastest is utils.datestruct().YYYYMMDD
```

<!-- GITCONTRIBUTOR_START -->

## Contributors

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

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

<!-- GITCONTRIBUTOR_END -->
## Contributors

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

[MIT](LICENSE.txt)
Made with [contributors-img](https://contrib.rocks).
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"test-local": "egg-bin test",
"preci": "npm run prepublishOnly",
"ci": "egg-bin cov",
"contributor": "git-contributor",
"prepublishOnly": "tshy && tshy-after"
},
"dependencies": {
Expand All @@ -20,20 +19,18 @@
"@eggjs/tsconfig": "^1.3.3",
"@types/escape-html": "^1.0.4",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.0",
"@types/node": "22",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"contributors": "*",
"egg-bin": "^6.5.2",
"eslint": "^8.54.0",
"eslint-config-egg": "^13.0.0",
"git-contributor": "^2.1.5",
"moment": "^2.22.2",
"object-assign": "^4.1.1",
"optimized": "^1.2.0",
"time-require": "^0.1.2",
"tsd": "^0.28.1",
"tshy": "^1.8.1",
"tshy": "^3.0.2",
"tshy-after": "^1.0.0",
"typescript": "^5.2.2"
},
Expand Down Expand Up @@ -81,5 +78,6 @@
"src"
],
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts"
"types": "./dist/commonjs/index.d.ts",
"module": "./dist/esm/index.js"
}
6 changes: 5 additions & 1 deletion src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const MONTHS: Record<string, string> = {
'12': 'Dec',
};

function getDateStringParts(d: Date, onlyDate?: boolean) {
/**
* return `[ YYYY, MM, DD, HH, mm, ss ]` date string array
*/
export function getDateStringParts(d?: Date, onlyDate?: boolean) {
d = d || new Date();
const monthNum = d.getMonth() + 1;
const month = monthNum < 10 ? `0${monthNum}` : `${monthNum}`;
const dateNum = d.getDate();
Expand Down
9 changes: 8 additions & 1 deletion test/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { strict as assert } from 'node:assert';
import moment from 'moment';
import * as utility from '../src/index.js';
import * as utils from '../src/index.js';
import { YYYYMMDDHHmmss, logDate } from '../src/index.js';
import { YYYYMMDDHHmmss, logDate, getDateStringParts } from '../src/index.js';

describe('test/date.test.ts', () => {
describe('getDateStringParts()', () => {
it('should work', () => {
assert.match(getDateStringParts().join(','), /^\d{4},\d{2},\d{2},\d{2},\d{2},\d{2}$/);
assert.match(utility.getDateStringParts(new Date(), true).join('.'), /^\d{4}\.\d{2}\.\d{2}$/);
});
});

describe('YYYYMMDDHHmmss()', () => {
it('should return an "YYYY-MM-DD HH:mm:ss" format date string', () => {
assert.match(utility.YYYYMMDDHHmmss(), /^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/);
Expand Down

0 comments on commit 98e7a71

Please sign in to comment.