diff --git a/README.md b/README.md index e643604..37f53b2 100644 --- a/README.md +++ b/README.md @@ -1,79 +1,20 @@

@renzp/utils logo

+ + Code coverage + Bundle Size Downloads Version License - docs - Change Log

- # @renzp/utils 一款零依赖、实用的Javascript/Typescript工具库 -## coverage - -> 由于使用的`bun test`无法生成`coverage`HTML文件,所以在此列一下`coverage` - -| File | % Funcs | % Lines | -| -------------------------------- | ------- | ------- | -| All files | 100.00 | 100.00 | -| src/_base.ts | 100.00 | 100.00 | -| src/array/createArray.ts | 100.00 | 100.00 | -| src/array/difference.ts | 100.00 | 100.00 | -| src/array/first.ts | 100.00 | 100.00 | -| src/array/flatten.ts | 100.00 | 100.00 | -| src/array/index.ts | 100.00 | 100.00 | -| src/array/last.ts | 100.00 | 100.00 | -| src/array/unique.ts | 100.00 | 100.00 | -| src/index.ts | 100.00 | 100.00 | -| src/is/index.ts | 100.00 | 100.00 | -| src/is/isArray.ts | 100.00 | 100.00 | -| src/is/isBigint.ts | 100.00 | 100.00 | -| src/is/isBoolean.ts | 100.00 | 100.00 | -| src/is/isDate.ts | 100.00 | 100.00 | -| src/is/isDef.ts | 100.00 | 100.00 | -| src/is/isFalsy.ts | 100.00 | 100.00 | -| src/is/isFunction.ts | 100.00 | 100.00 | -| src/is/isJson.ts | 100.00 | 100.00 | -| src/is/isMap.ts | 100.00 | 100.00 | -| src/is/isNull.ts | 100.00 | 100.00 | -| src/is/isNumber.ts | 100.00 | 100.00 | -| src/is/isObject.ts | 100.00 | 100.00 | -| src/is/isPrimitive.ts | 100.00 | 100.00 | -| src/is/isRefData.ts | 100.00 | 100.00 | -| src/is/isRegExp.ts | 100.00 | 100.00 | -| src/is/isSet.ts | 100.00 | 100.00 | -| src/is/isString.ts | 100.00 | 100.00 | -| src/is/isSymbol.ts | 100.00 | 100.00 | -| src/is/isTruth.ts | 100.00 | 100.00 | -| src/is/isUnDef.ts | 100.00 | 100.00 | -| src/is/isUndefined.ts | 100.00 | 100.00 | -| src/is/isWeakMap.ts | 100.00 | 100.00 | -| src/is/isWeakSet.ts | 100.00 | 100.00 | -| src/number/index.ts | 100.00 | 100.00 | -| src/number/numberSeparate.ts | 100.00 | 100.00 | -| src/object/index.ts | 100.00 | 100.00 | -| src/object/removeKey.ts | 100.00 | 100.00 | -| src/other/deepClone.ts | 100.00 | 100.00 | -| src/other/index.ts | 100.00 | 100.00 | -| src/other/radom.ts | 100.00 | 100.00 | -| src/other/uuid.ts | 100.00 | 100.00 | -| src/validator/emailValidator.ts | 100.00 | 100.00 | -| src/validator/idCardValidator.ts | 100.00 | 100.00 | -| src/validator/index.ts | 100.00 | 100.00 | -| src/validator/numberValidator.ts | 100.00 | 100.00 | -| src/validator/phoneValidator.ts | 100.00 | 100.00 | -| src/validator/telValidator.ts | 100.00 | 100.00 | -| src/validator/zhValidator.ts | 100.00 | 100.00 | -| src/window/copyText.ts | 100.00 | 100.00 | -| src/window/index.ts | 100.00 | 100.00 | -| test/happydom.ts | 100.00 | 100.00 | - ## 安装 ```sh diff --git a/coverage.md b/coverage.md new file mode 100644 index 0000000..6ba04c3 --- /dev/null +++ b/coverage.md @@ -0,0 +1,56 @@ +# coverage + +File | % Funcs | % Lines | Uncovered Line #s +----------------------------------|---------|---------|------------------- +All files | 100.00 | 100.00 | + src/_base.ts | 100.00 | 100.00 | + src/array/createArray.ts | 100.00 | 100.00 | + src/array/difference.ts | 100.00 | 100.00 | + src/array/first.ts | 100.00 | 100.00 | + src/array/flatten.ts | 100.00 | 100.00 | + src/array/index.ts | 100.00 | 100.00 | + src/array/last.ts | 100.00 | 100.00 | + src/array/unique.ts | 100.00 | 100.00 | + src/index.ts | 100.00 | 100.00 | + src/is/index.ts | 100.00 | 100.00 | + src/is/isArray.ts | 100.00 | 100.00 | + src/is/isBigint.ts | 100.00 | 100.00 | + src/is/isBoolean.ts | 100.00 | 100.00 | + src/is/isDate.ts | 100.00 | 100.00 | + src/is/isDef.ts | 100.00 | 100.00 | + src/is/isFalsy.ts | 100.00 | 100.00 | + src/is/isFunction.ts | 100.00 | 100.00 | + src/is/isJson.ts | 100.00 | 100.00 | + src/is/isMap.ts | 100.00 | 100.00 | + src/is/isNull.ts | 100.00 | 100.00 | + src/is/isNumber.ts | 100.00 | 100.00 | + src/is/isObject.ts | 100.00 | 100.00 | + src/is/isPrimitive.ts | 100.00 | 100.00 | + src/is/isRefData.ts | 100.00 | 100.00 | + src/is/isRegExp.ts | 100.00 | 100.00 | + src/is/isSet.ts | 100.00 | 100.00 | + src/is/isString.ts | 100.00 | 100.00 | + src/is/isSymbol.ts | 100.00 | 100.00 | + src/is/isTruth.ts | 100.00 | 100.00 | + src/is/isUnDef.ts | 100.00 | 100.00 | + src/is/isUndefined.ts | 100.00 | 100.00 | + src/is/isWeakMap.ts | 100.00 | 100.00 | + src/is/isWeakSet.ts | 100.00 | 100.00 | + src/number/index.ts | 100.00 | 100.00 | + src/number/numberSeparate.ts | 100.00 | 100.00 | + src/object/index.ts | 100.00 | 100.00 | + src/object/removeKey.ts | 100.00 | 100.00 | + src/other/deepClone.ts | 100.00 | 100.00 | + src/other/index.ts | 100.00 | 100.00 | + src/other/radom.ts | 100.00 | 100.00 | + src/other/uuid.ts | 100.00 | 100.00 | + src/validator/emailValidator.ts | 100.00 | 100.00 | + src/validator/idCardValidator.ts | 100.00 | 100.00 | + src/validator/index.ts | 100.00 | 100.00 | + src/validator/numberValidator.ts | 100.00 | 100.00 | + src/validator/phoneValidator.ts | 100.00 | 100.00 | + src/validator/telValidator.ts | 100.00 | 100.00 | + src/validator/zhValidator.ts | 100.00 | 100.00 | + src/window/copyText.ts | 100.00 | 100.00 | + src/window/index.ts | 100.00 | 100.00 | + test/happydom.ts | 100.00 | 100.00 | \ No newline at end of file diff --git a/coverage.svg b/coverage.svg new file mode 100644 index 0000000..2df66ab --- /dev/null +++ b/coverage.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + coverage + coverage + 100% + 100% + + \ No newline at end of file diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 65039d0..f6c407f 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -10,7 +10,7 @@ export default async () => { lastUpdated: true, metaChunk: true, rewrites: { - 'pages/guide/gettingStarted.md': 'index.md', + 'pages/guide/whatIs.md': 'index.md', 'pages/:category/:page.md': ':category/:page.md', }, head: [['link', { rel: 'icon', type: 'image/png', href: '/logo.png' }]], diff --git a/docs/pages/guide/index.ts b/docs/pages/guide/index.ts index aadc228..e97a117 100644 --- a/docs/pages/guide/index.ts +++ b/docs/pages/guide/index.ts @@ -1,8 +1,8 @@ export default { text: '简介', items: [ - { text: '什么是@renzp/utils?', link: '/guide/whatIs' }, - { text: '快速开始', link: '/' }, + { text: '什么是@renzp/utils?', link: '/' }, + { text: '快速开始', link: '/guide/gettingStarted' }, ], sort: 0, } diff --git a/docs/pages/guide/whatIs.md b/docs/pages/guide/whatIs.md index f254f6c..cbe51e7 100644 --- a/docs/pages/guide/whatIs.md +++ b/docs/pages/guide/whatIs.md @@ -1,63 +1,22 @@ -# 什么是@renzp/utils? +

+ @renzp/utils logo +

+

+ + Code coverage + + + Bundle Size + + + Downloads + + + Version + + + License + +

`@renzp/utils`是一款零依赖、强类型、实用、ESM优先的Javascript/Typescript工具库。 - -## coverage - -> 由于使用的`bun test`无法生成`coverage`HTML文件,所以在此列一下`coverage` - - -| File | % Funcs | % Lines | -| -------------------------------- | ------- | ------- | -| All files | 100.00 | 100.00 | -| src/_base.ts | 100.00 | 100.00 | -| src/array/createArray.ts | 100.00 | 100.00 | -| src/array/difference.ts | 100.00 | 100.00 | -| src/array/first.ts | 100.00 | 100.00 | -| src/array/flatten.ts | 100.00 | 100.00 | -| src/array/index.ts | 100.00 | 100.00 | -| src/array/last.ts | 100.00 | 100.00 | -| src/array/unique.ts | 100.00 | 100.00 | -| src/index.ts | 100.00 | 100.00 | -| src/is/index.ts | 100.00 | 100.00 | -| src/is/isArray.ts | 100.00 | 100.00 | -| src/is/isBigint.ts | 100.00 | 100.00 | -| src/is/isBoolean.ts | 100.00 | 100.00 | -| src/is/isDate.ts | 100.00 | 100.00 | -| src/is/isDef.ts | 100.00 | 100.00 | -| src/is/isFalsy.ts | 100.00 | 100.00 | -| src/is/isFunction.ts | 100.00 | 100.00 | -| src/is/isJson.ts | 100.00 | 100.00 | -| src/is/isMap.ts | 100.00 | 100.00 | -| src/is/isNull.ts | 100.00 | 100.00 | -| src/is/isNumber.ts | 100.00 | 100.00 | -| src/is/isObject.ts | 100.00 | 100.00 | -| src/is/isPrimitive.ts | 100.00 | 100.00 | -| src/is/isRefData.ts | 100.00 | 100.00 | -| src/is/isRegExp.ts | 100.00 | 100.00 | -| src/is/isSet.ts | 100.00 | 100.00 | -| src/is/isString.ts | 100.00 | 100.00 | -| src/is/isSymbol.ts | 100.00 | 100.00 | -| src/is/isTruth.ts | 100.00 | 100.00 | -| src/is/isUnDef.ts | 100.00 | 100.00 | -| src/is/isUndefined.ts | 100.00 | 100.00 | -| src/is/isWeakMap.ts | 100.00 | 100.00 | -| src/is/isWeakSet.ts | 100.00 | 100.00 | -| src/number/index.ts | 100.00 | 100.00 | -| src/number/numberSeparate.ts | 100.00 | 100.00 | -| src/object/index.ts | 100.00 | 100.00 | -| src/object/removeKey.ts | 100.00 | 100.00 | -| src/other/deepClone.ts | 100.00 | 100.00 | -| src/other/index.ts | 100.00 | 100.00 | -| src/other/radom.ts | 100.00 | 100.00 | -| src/other/uuid.ts | 100.00 | 100.00 | -| src/validator/emailValidator.ts | 100.00 | 100.00 | -| src/validator/idCardValidator.ts | 100.00 | 100.00 | -| src/validator/index.ts | 100.00 | 100.00 | -| src/validator/numberValidator.ts | 100.00 | 100.00 | -| src/validator/phoneValidator.ts | 100.00 | 100.00 | -| src/validator/telValidator.ts | 100.00 | 100.00 | -| src/validator/zhValidator.ts | 100.00 | 100.00 | -| src/window/copyText.ts | 100.00 | 100.00 | -| src/window/index.ts | 100.00 | 100.00 | -| test/happydom.ts | 100.00 | 100.00 | \ No newline at end of file diff --git a/docs/public/coverage.svg b/docs/public/coverage.svg new file mode 100644 index 0000000..2df66ab --- /dev/null +++ b/docs/public/coverage.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + coverage + coverage + 100% + 100% + + \ No newline at end of file diff --git a/package.json b/package.json index e93fed7..b35c887 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,7 @@ "description": "一款零依赖、实用的Javascript/Typescript工具库", "type": "module", "main": "dist/index.js", - "files": [ - "dist" - ], + "files": ["dist", "./coverage.svg"], "types": "dist/index.d.ts", "author": "renzp <1074720760.qq.com>", "repository": { @@ -17,9 +15,7 @@ "bugs": { "url": "https://github.com/renzp94/utils/issues" }, - "keywords": [ - "utils" - ], + "keywords": ["utils"], "license": "MIT", "publishConfig": { "registry": "https://registry.npmjs.org", @@ -32,7 +28,8 @@ "pub:all": "bun run scripts/pub-all.ts", "docs:dev": "vitepress dev docs", "docs:build": "vitepress build docs", - "docs:preview": "vitepress preview docs" + "docs:preview": "vitepress preview docs", + "coverage": "bun run scripts/coverage.ts" }, "devDependencies": { "@biomejs/biome": "^1.7.0", diff --git a/scripts/common.ts b/scripts/common.ts index 253cc7a..e8dc6cf 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -67,3 +67,91 @@ export const jsrPublish = async () => { await Bun.$`git add jsr.json` return Bun.$`git commit -m "chore(jsr:${jsrConfig.version}): published"` } + +import path from 'node:path' + +export interface CoverageOptions { + label?: string + tagConfigs?: Array<{ ratio: number; color: string }> + outDir?: string +} + +export const coverage = async (options?: CoverageOptions): Promise => { + const { + label = 'coverage', + tagConfigs = [ + { ratio: 0, color: '#ffc245' }, + { ratio: 50, color: '#ffc245' }, + { ratio: 90, color: '#00c48c' }, + ], + outDir = process.cwd(), + } = options ?? {} + + const svg = ` + + + + + + + + + + + + + ${label} + ${label} + @ratio@ + @ratio@ + + +`.trim() + try { + const result = await Bun.$`bun test --coverage` + const text = result.stderr.toString() + const lines = text.trim().split('\n') + // 获取All files行 + const line = lines.find((line) => line.includes('All files')) + if (!line) { + throw new Error('未找到All files行') + } + + // 截取覆盖率(以Lines为准) + const ratio = Number( + line.split('|')[2].trim().split(' ')[0].replace('%', ''), + ) + if (ratio > 100 || ratio < 0) { + throw new Error('覆盖率范围为0~100') + } + + const ratioTarget = tagConfigs + .sort((a, b) => b.ratio - a.ratio) + .find((item) => ratio >= item.ratio) + + const badge = svg + .replace(/@ratio@/g, `${ratio}%`) + .replace(/@color@/g, ratioTarget?.color ?? '555') + + await Bun.write(path.resolve(outDir, `${label}.svg`), badge) + await Bun.write(path.resolve(outDir, `./docs/public/${label}.svg`), badge) + let lineIndex = lines.findIndex((line) => line.includes('All files')) + let endIndex = lines.findIndex((line) => line.includes('tests across')) + + if (lineIndex > -1) { + lineIndex = lineIndex - 2 + } + + if (endIndex > -1) { + endIndex = endIndex - 5 + } + + let md = `# ${label}\n\n` + + md += lines.slice(lineIndex, endIndex).join('\n') + await Bun.write(path.resolve(outDir, `${label}.md`), md) + return true + } catch { + return false + } +} diff --git a/scripts/coverage.ts b/scripts/coverage.ts new file mode 100644 index 0000000..47a9a55 --- /dev/null +++ b/scripts/coverage.ts @@ -0,0 +1,3 @@ +import { coverage } from './common' + +coverage() diff --git a/scripts/pub-all.ts b/scripts/pub-all.ts index cddc76a..81dac83 100644 --- a/scripts/pub-all.ts +++ b/scripts/pub-all.ts @@ -1,4 +1,4 @@ -import { build, jsrPublish, npmPublish } from './common' +import { build, coverage, jsrPublish, npmPublish } from './common' // biome-ignore lint/suspicious/noConsoleLog: console.log('📦 打包中...') @@ -7,6 +7,7 @@ const result = await build() if (result.success) { // biome-ignore lint/suspicious/noConsoleLog: console.log('📦 打包成功 🎉🎉🎉') + await coverage() await npmPublish() await jsrPublish() await Bun.$`git push origin --follow-tags`