Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"release:next": "lerna publish --conventional-commits --conventional-prerelease --preid next --dist-tag next",
"release:graduate": "lerna publish --conventional-commits --conventional-graduate",
"changelog": "node ./scripts/genChangelog.js",
"prepublishOnly": "yarn build && yarn test",
"prepublishOnly": "yarn changelog && yarn build && yarn test",
"lint": "lerna run lint --stream --scope '@acehubert/*'",
"lint:fix": "lerna run lint:fix --stream --scope '@acehubert/*'",
"commit": "git-cz"
Expand Down
1 change: 1 addition & 0 deletions packages/zentao-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"axios": "^1.13.2",
"configstore": "^5.0.1",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.20",
"form-data": "^4.0.0",
"kleur": "^4.1.4"
},
Expand Down
77 changes: 0 additions & 77 deletions packages/zentao-api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,6 @@ import { ZentaoRequestParamPair, ZentaoRequestParams } from "./types";

export const md5 = (value: string): string => CryptoJS.MD5(value).toString();

/**
* 创建一个 Date 对象
* @param {Date|number|String} [date=null] 用于创建 Date 对象的日期时间表达值,如果留空则创建当前系统时间对象
* @return {Date} 日期时间对象
* @function
*/
export const createDate = (date: Date | number | string) => {
if (!date) {
return new Date();
}
if (!(date instanceof Date)) {
if (typeof date === "string") {
date = date.trim();
if (/^\d+$/.test(date)) {
date = Number.parseInt(date, 10);
}
}
if (typeof date === "number" && date < 10000000000) {
date *= 1000;
}
date = new Date(date);
}
return date;
};

/**
* 格式化日期时间值为字符串
* @remarks
* 所有可用的格式化参数有:
* - `yyyy`,例如:'2018',表示四位数字表示的年份
* - `yy`,例如:'18',表示两位数字表示的年份
* - `MM`,例如:'07',表示两位数字表示的月份,不足两位在起始用 0 填充
* - `M`,例如:'10',表示一位或两位数字表示的月份
* - `dd`,例如:'05',表示两位数字表示的日期,不足两位在起始用 0 填充
* - `d`,例如:'5',表示一位或两位数字表示的日期
* - `hh`,例如:'08',表示两位数字表示的小时,不足两位在起始用 0 填充
* - `h`,例如:'8',表示一位或两位数字表示的小时
* - `mm`,例如:'3',表示两位数字表示的分钟,不足两位在起始用 0 填充
* - `m`,例如:'03',表示一位或两位数字表示的分钟
* - `ss`,例如:'5',表示两位数字表示的秒数,不足两位在起始用 0 填充
* - `s`,例如:'05',表示一位或两位数字表示的秒数
* - `S`,例如:'236',表示毫秒数
* - `SSS`,例如:'036',表示毫秒数,不足3位在起始用 0 填充
* @summary 格式化日期时间值为字符串
* @param {Date|number|string} date 要格式化的日期时间表达值
* @param {string} [format='yyyy-MM-dd hh:ss'] 格式化字符串
* @return {string} 日期时间格式化文本
* @function
*/
export const formatDate = (date: Date | number | string, format: string = "yyyy-MM-dd hh:mm") => {
date = createDate(date);

const dateInfo: Record<string, any> = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"H+": date.getHours() % 12,
"m+": date.getMinutes(),
"s+": date.getSeconds(),
// 'q+': Math.floor((date.getMonth() + 3) / 3),
"S+": date.getMilliseconds(),
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length));
}
Object.keys(dateInfo).forEach((k) => {
if (new RegExp(`(${k})`).test(format)) {
const str = `${dateInfo[k]}`;
format = format.replace(
RegExp.$1,
RegExp.$1.length === 1 ? str : `00${str}`.substr(str.length + 2 - RegExp.$1.length),
);
}
});
return format;
};

/**
* 格式化禅道地址
* @param url 禅道地址
Expand Down
7 changes: 4 additions & 3 deletions packages/zentao-api/src/zentao-legacy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dayjs from "dayjs";
import {
CreateDocModuleParams,
CreateDocParams,
Expand All @@ -8,7 +9,7 @@ import {
ZentaoLegacyApiResponse,
ZentaoFileReadResult,
} from "./types";
import { formatDate, md5 } from "./utils";
import { md5 } from "./utils";
import Zentao from "./helpers/zentao";

/**
Expand Down Expand Up @@ -845,7 +846,7 @@ export default class ZentaoLegacy extends Zentao {
currentConsumed: params.currentConsumed,
consumed,
assignedTo: params.assignedTo,
finishedDate: params.finishedDate ?? formatDate(new Date(), "yyyy-MM-dd"),
finishedDate: params.finishedDate ?? dayjs().format("YYYY-MM-DD"),
comment: params.comment,
status: "done",
},
Expand Down Expand Up @@ -1189,7 +1190,7 @@ export default class ZentaoLegacy extends Zentao {
const data: Record<string, any> = {
resolution: params.resolution ?? "fixed",
resolvedBuild: params.resolvedBuild ?? "trunk",
resolvedDate: params.resolvedDate ?? formatDate(new Date(), "yyyy-MM-dd hh:mm:ss"),
resolvedDate: params.resolvedDate ?? dayjs().format("YYYY-MM-DD HH:mm:ss"),
assignedTo: params.assignedTo,
comment: params.comment,
duplicateBug: params.duplicateBug,
Expand Down
44 changes: 0 additions & 44 deletions packages/zentao-api/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,5 @@
import * as utils from "../src/utils";

describe("utils.createDate", () => {
it("date from string", () => {
const date = utils.createDate("2020-02-01");
expect(date).toBeInstanceOf(Date);
expect(date.toDateString()).toBe("Sat Feb 01 2020");
});

it("date from timestamp", () => {
const date = utils.createDate(1615214148009); // Mon Mar 08 2021 22:35:48 GMT+0800
expect(date).toBeInstanceOf(Date);
expect(date.getTime()).toBe(1615214148009);
});

it("date from php timestamp", () => {
const date = utils.createDate(1615214148); // Mon Mar 08 2021 22:35:48 GMT+0800
expect(date).toBeInstanceOf(Date);
expect(date.getTime()).toBe(1615214148000);
});
});

describe("utils.formatDate", () => {
it("format time", () => {
const date = new Date(1615214108029); // 2021/3/8 22:35:08
expect(utils.formatDate(date, "hh:mm")).toBe("22:35");
expect(utils.formatDate(date, "hh:mm:ss")).toBe("22:35:08");
expect(utils.formatDate(date, "HH:mm:s")).toBe("10:35:8");
expect(utils.formatDate(date, "hh:mm:s.S")).toBe("22:35:8.29");
expect(utils.formatDate(date, "hh:mm:s.SSS")).toBe("22:35:8.029");
expect(utils.formatDate(date, "H:mm:ss")).toBe("10:35:08");
expect(utils.formatDate(date, "m")).toBe("35");
});

it("format date", () => {
const date = new Date(1615214108029); // 2021/3/8 22:35:08
expect(utils.formatDate(date, "hh:mm")).toBe("22:35");
expect(utils.formatDate(date, "hh:mm:ss")).toBe("22:35:08");
expect(utils.formatDate(date, "HH:mm:s")).toBe("10:35:8");
expect(utils.formatDate(date, "hh:mm:s.S")).toBe("22:35:8.29");
expect(utils.formatDate(date, "hh:mm:s.SSS")).toBe("22:35:8.029");
expect(utils.formatDate(date, "H:mm:ss")).toBe("10:35:08");
expect(utils.formatDate(date, "m")).toBe("35");
});
});

describe("utils.formatZentaoUrl", () => {
it("formatZentaoUrl", () => {
expect(utils.formatZentaoUrl("http://demo.zentao.net/")).toBe("http://demo.zentao.net/");
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ __metadata:
axios: ^1.13.2
configstore: ^5.0.1
crypto-js: ^4.2.0
dayjs: ^1.11.20
form-data: ^4.0.0
kleur: ^4.1.4
typedoc: ^0.20.30
Expand Down Expand Up @@ -4409,6 +4410,13 @@ __metadata:
languageName: node
linkType: hard

"dayjs@npm:^1.11.20":
version: 1.11.20
resolution: "dayjs@npm:1.11.20"
checksum: 26f4867c4ae1315885ac3e560906d3f8c49cb6a1303e6fdd5f87ace3b814b07a45f036facad70299cea36f3eb62ee2070dd239079c56d8f55e4e684afb752a67
languageName: node
linkType: hard

"debug@npm:4, debug@npm:^4.0.0, debug@npm:^4.1.0, debug@npm:^4.1.1, debug@npm:^4.3.1, debug@npm:^4.3.2, debug@npm:^4.3.3, debug@npm:^4.3.4, debug@npm:^4.4.0, debug@npm:^4.4.1, debug@npm:^4.4.3":
version: 4.4.3
resolution: "debug@npm:4.4.3"
Expand Down
Loading