Skip to content

Commit 2a55686

Browse files
committed
feat(): pipes支持转换forms.crontab-input的值
Closes CMDB_DISCOVERY-1687
1 parent a618477 commit 2a55686

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

packages/pipes/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
},
3030
"sideEffects": false,
3131
"dependencies": {
32-
"@easyops-cn/brick-next-pipes": "^0.7.6"
32+
"@easyops-cn/brick-next-pipes": "^0.7.6",
33+
"@next-libs/crontab": "^2.0.10"
3334
},
3435
"devDependencies": {
3536
"@next-core/rollup-config-factory": "^2.8.0",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import crontab from "@next-libs/crontab";
2+
type CrontabType = "minute" | "hour" | "date" | "month" | "dow";
3+
4+
/**
5+
* 将forms.crontab-input的value转换为可读字符串
6+
*
7+
* @category Others
8+
*
9+
* @public
10+
*
11+
* @example
12+
*
13+
* ```ts
14+
* crontabFormat("0 6 * * *")
15+
* // Returns `在上午 06:00`
16+
* ```
17+
*
18+
* @param crontabStr - crontab字符串。
19+
* @returns 可读字符串。
20+
*/
21+
export function crontabFormat(crontabStr: string): string {
22+
const crontabTimeObj = parseCrontab(crontabStr);
23+
return crontab.format(crontabTimeObj, true);
24+
}
25+
function parseCrontab(str = "* * * * *"): Record<CrontabType, string> {
26+
const [minute, hour, date, month, dow] = str.split(" ");
27+
return {
28+
minute,
29+
hour,
30+
date,
31+
month,
32+
dow,
33+
};
34+
}

packages/pipes/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { pipes } from "@easyops-cn/brick-next-pipes";
2+
import { crontabFormat } from "./crontabFormat";
23

34
export { pipes, utils } from "@easyops-cn/brick-next-pipes";
45

@@ -23,7 +24,9 @@ export interface SourceLocation {
2324
const PipeRegistry = new Map<string, PipeFunction>(
2425
Array.from(Object.entries(pipes as Record<string, PipeFunction>))
2526
);
26-
27+
PipeRegistry.set("crontabFormat", (value: unknown): unknown => {
28+
return crontabFormat(value as string);
29+
});
2730
/** For next-core internal usage only. */
2831
export function processPipes(value: unknown, pipes: PipeCall[]): unknown {
2932
if (pipes.length === 0) {

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,13 @@
23862386
dependencies:
23872387
"@next-core/styles-v3" "^0.3.5"
23882388

2389+
"@next-libs/crontab@^2.0.10":
2390+
version "2.0.10"
2391+
resolved "https://registry.npmmirror.com/@next-libs/crontab/-/crontab-2.0.10.tgz#f6539ffee3944d17e5285f4f3b52c32f65529209"
2392+
integrity sha512-lGI0LtH9gApzr09NoUjcr5ELZk5dPadR6HK1yHPT4/keSfCliEQXsBZxWFylnlzJW1hZIH7dWkovmXxNzssKVg==
2393+
dependencies:
2394+
cronstrue "^1.88.0"
2395+
23892396
"@next-sdk/api-gateway-sdk@^2.12.0":
23902397
version "2.12.0"
23912398
resolved "https://registry.npmjs.org/@next-sdk/api-gateway-sdk/-/api-gateway-sdk-2.12.0.tgz#645165fba3c0dc47a2232558cfacb3a448ad4247"
@@ -6211,6 +6218,11 @@ create-require@^1.1.0:
62116218
resolved "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
62126219
integrity sha1-wdfo8eX2z8n/ZfnNNS03NIdWwzM=
62136220

6221+
cronstrue@^1.88.0:
6222+
version "1.125.0"
6223+
resolved "https://registry.npmmirror.com/cronstrue/-/cronstrue-1.125.0.tgz#8030816d033d00caade9b2a9f9b71e69175bcf42"
6224+
integrity sha512-qkC5mVbVGuuyBVXmam5anaRtbLcgfBUKajoyZqCdf/XBdgF43PsLSEm8eEi2dsI3YbqDPbLSH2mWNzM1dVqHgQ==
6225+
62146226
cross-env@^7.0.3:
62156227
version "7.0.3"
62166228
resolved "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"

0 commit comments

Comments
 (0)