diff --git a/packages/changelog/src/core/changelog.api.ts b/packages/changelog/src/core/changelog.api.ts index 4c7aea88..75b1d1f2 100644 --- a/packages/changelog/src/core/changelog.api.ts +++ b/packages/changelog/src/core/changelog.api.ts @@ -1,6 +1,6 @@ import type { ChangelogCliOptions, ChangelogGenerateOptions, GenerateChangelogResult } from '../enums' import { parseCliOptions } from '@142vip/changelog' -import { VipColor, VipConsole, VipDayjs, VipGit, vipLogger, VipNodeJS } from '@142vip/utils' +import { VipColor, VipConsole, vipDayjs, VipGit, vipLogger, VipNodeJS } from '@142vip/utils' import { GitCommitAPI } from './git-commit.api' import { GithubAPI } from './github.api' @@ -58,7 +58,7 @@ async function upsertChangelogDoc( } // 添加版本头部 - const newMd = `## ${releaseVersionName} (${VipDayjs.formatDateToYMD()})\n\n${markdown}` + const newMd = `## ${releaseVersionName} (${vipDayjs.formatDateToYMD()})\n\n${markdown}` const lastEntry = changelogMD.match(/^##\s+(?:\S.*)?$/m) diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md index 023eb258..e850a783 100644 --- a/packages/utils/CHANGELOG.md +++ b/packages/utils/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## v0.0.1-alpha.43 (2025-11-06) + +### 💅 Refactors + +- 优化`VipDayjs`类核心逻辑,支持`vipDayjs`对象导出  -  by **142vip.cn** in https://github.com/142vip/core-x/issues/732 [(b7c12)](https://github.com/142vip/core-x/commit/b7c12ee8) +- 优化`VipNanoId`类核心逻辑  -  by **142vip.cn** in https://github.com/142vip/core-x/issues/733 [(caa65)](https://github.com/142vip/core-x/commit/caa6536b) + +**Release New Version v0.0.1-alpha.43 [👉 View New Package On NPM](https://www.npmjs.com/package/@142vip/utils)** + ## v0.0.1-alpha.42 (2025-10-16) ### ✨ Features diff --git a/packages/utils/package.json b/packages/utils/package.json index c5a0d1d7..02433d52 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,7 +1,7 @@ { "name": "@142vip/utils", "type": "module", - "version": "0.0.1-alpha.42", + "version": "0.0.1-alpha.43", "private": false, "description": "通用型基础工具集合,对常用模块的二次集成", "author": "mmdapl ", diff --git a/packages/utils/src/pkgs/dayjs.ts b/packages/utils/src/pkgs/dayjs.ts index d91cf008..5d790cd6 100644 --- a/packages/utils/src/pkgs/dayjs.ts +++ b/packages/utils/src/pkgs/dayjs.ts @@ -1,34 +1,56 @@ import dayjs from 'dayjs' -/** - * 时间格式:年-月-日 时:分:秒 - */ -const FormatTemplateStr = 'YYYY-MM-DD HH:mm:ss' -/** - * 获取当前时间戳 - */ -function getCurrentTimestamp() { - return dayjs().valueOf() -} +export class VipDayjs { + /** + * 时间格式:年-月-日 时:分:秒 + */ + private readonly FORMAT_TEMPLATE_STR = 'YYYY-MM-DD HH:mm:ss' + /** + * 获取当前时间戳。单位:毫秒 + */ + public getCurrentTimestamp(): number { + return dayjs().valueOf() + } -/** - * 时间格式化,默认: 年-月-日 时:分:秒 - */ -function formatDateToStr(date: Date, template?: string) { - return dayjs(date).format(template ?? FormatTemplateStr) -} + /** + * 获取过期时间戳。单位:毫秒 + * @param duration 过期时间,默认:1小时 + */ + public getExpiredTimestamp(duration = 60 * 60 * 1000): number { + return this.getCurrentTimestamp() + duration + } -/** - * 年月日格式化当前时间 - * - 格式: 2024-08-09 - */ -function formatDateToYMD(): string { - return formatDateToStr(new Date(), 'YYYY-MM-DD') -} + /** + * 是否在当前时间之前 + * @param date + */ + public isBeforeNow(date: Date): boolean { + return dayjs().isBefore(dayjs(date)) + } -export const VipDayjs = { - getCurrentTimestamp, - FormatTemplateStr, - formatDateToStr, - formatDateToYMD, + /** + * 是否在当前时间之后 + * @param date + */ + public isAfterNow(date: Date): boolean { + return dayjs().isAfter(dayjs(date)) + } + + /** + * 时间格式化,默认: 年-月-日 时:分:秒 + */ + public formatDateToStr(date: Date, template?: string): string { + return dayjs(date).format(template ?? this.FORMAT_TEMPLATE_STR) + } + + /** + * 年月日格式化当前时间 + * - 格式: 2024-08-09 + */ + public formatDateToYMD(): string { + return this.formatDateToStr(new Date(), 'YYYY-MM-DD') + } } + +// 导出 +export const vipDayjs = new VipDayjs() diff --git a/packages/utils/src/pkgs/nanoid.ts b/packages/utils/src/pkgs/nanoid.ts index 75b0abab..0d09521f 100644 --- a/packages/utils/src/pkgs/nanoid.ts +++ b/packages/utils/src/pkgs/nanoid.ts @@ -13,28 +13,47 @@ export enum Alphabet { ONLY_CHAR = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', COMPLEX = '-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', } -/** - * 获取NanoId对象 - * @param alphabet - */ -function getNanoId(alphabet?: string): (size?: number) => string { - return customAlphabet(alphabet ?? Alphabet.DEFAULT) -} -/** - * 获取随机字符串 - * - 按照制定规则生成默认字符串 - */ -function getRandomId(size?: number): string { - return nanoid(size) -} +export class VipNanoId { + /** + * 获取NanoId对象,默认字符集为 Alphabet.DEFAULT,只有小写字母和数字 + * @param alphabet + */ + public getNanoId(alphabet?: string): (size?: number) => string { + return customAlphabet(alphabet ?? Alphabet.DEFAULT) + } -export interface IVipNanoId { - getRandomId: typeof getRandomId - getNanoId: typeof getNanoId -} + /** + * 获取随机字符串,默认长度21 + * - 按照制定规则生成默认字符串 + */ + public getRandomId(size?: number): string { + return nanoid(size) + } + + /** + * 获取纯数字的随机字符串 + * @param size + */ + public getRandomNumberId(size?: number): string { + return this.getNanoId(Alphabet.ONLY_NUMBER)(size) + } -export const VipNanoId: IVipNanoId = { - getRandomId, - getNanoId, + /** + * 获取纯字母的随机字符串,包含小写字母和大写字母 + * @param size + */ + public getRandomCharId(size?: number): string { + return this.getNanoId(Alphabet.ONLY_CHAR)(size) + } + + /** + * 获取复杂的随机字符串,包含数字、小写字母、大写字母和特殊字符 + * @param size + */ + public getRandomComplexId(size?: number): string { + return this.getNanoId(Alphabet.COMPLEX)(size) + } } + +export const vipNanoId = new VipNanoId()