From e51260d835778cbb1a77e62dfdd7e7ddc096f7cf Mon Sep 17 00:00:00 2001 From: yobrave Date: Tue, 28 Dec 2021 20:19:07 +0800 Subject: [PATCH] feat: print report sort by item.num (#77) * feat: printreport Sort by item.num * Update src/utils/report.ts Co-authored-by: Chieffo2021 <85914490+Chieffo2021@users.noreply.github.com> * Update src/utils/report.ts Co-authored-by: Joe Zhang * Update src/utils/report.ts Co-authored-by: Joe Zhang * Update tests/render.test.ts Co-authored-by: Chieffo2021 <85914490+Chieffo2021@users.noreply.github.com> * Update tests/render.test.ts Co-authored-by: Chieffo2021 <85914490+Chieffo2021@users.noreply.github.com> * Update src/utils/report.ts Co-authored-by: Chieffo2021 <85914490+Chieffo2021@users.noreply.github.com> * fix: render.test.ts error import * fix: Vars error Co-authored-by: yobrave Co-authored-by: Chieffo2021 <85914490+Chieffo2021@users.noreply.github.com> Co-authored-by: Joe Zhang --- src/utils/report.ts | 16 +++++++++++++++- tests/render.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/utils/report.ts b/src/utils/report.ts index 9d03790..f718d10 100644 --- a/src/utils/report.ts +++ b/src/utils/report.ts @@ -28,10 +28,24 @@ function recordConver (args: ConverObj) { }) } +function sortByNum (reportList: ConverObj[] = []) { + reportList.sort((current, next) => { + // same char, then sort by after-number + if (current.num[0] === next.num[0]) { + return Number(current.num.substring(1)) - Number(next.num.substring(1)) + } + // sort by first char + return current.num.charCodeAt(0) - next.num.charCodeAt(0) + }) +} + function printReport (dir: string, beginTime: number) { cliInstance.update(cliInstance.total, { doSomething: 'All done!' }); cliInstance.stop() console.log('conversion items successful converted:') + + sortByNum(reportList) + reportList.forEach(item => { tabDt.push([item.num, item.feat, item.times?.toString()]) }) @@ -58,4 +72,4 @@ function printReport (dir: string, beginTime: number) { logger.log(tableStr) console.log(chalk.green(`The report output path is ${dir}conversion.log`)); } -export { recordConver, printReport } +export { recordConver, printReport, sortByNum } diff --git a/tests/render.test.ts b/tests/render.test.ts index 5eaec48..31a5df3 100644 --- a/tests/render.test.ts +++ b/tests/render.test.ts @@ -2,6 +2,7 @@ import path from "path"; import {TemplateData} from "../src/config/config"; import {render} from "../src/generate/render"; import {readSync} from "../src/utils/file"; +import { sortByNum } from "../src/utils/report" import fs from "fs"; test('render vite.config.js from template', () => { @@ -23,3 +24,32 @@ test('render vite.config.js from template', () => { expect(source).toMatch('viteCommonjs()') fs.rmdirSync(outDir, { recursive: true }) }); + +test('sort report by item.num', () => { + interface ConverObj { + num: string;// 'Number' + feat: string;// 'Conversion item' + times?: number;// 'Conversion times' + } + // test 1, normal + let reportList: ConverObj[] = [] + const nums1 = ["V06","B01","B04","V01"]; + const result1 = ["B01","B04","V01","V06"]; + reportList = nums1.map(num => ({ + num, + feat: '' + })) + sortByNum(reportList) + expect(reportList.map(report => report.num)).toEqual(result1) + + // test 2 empty + let reportList2: ConverObj[] = [] + const nums2 = []; + const result2 = []; + reportList2 = nums2.map(num => ({ + num, + feat: '' + })) + sortByNum(reportList2) + expect(reportList2.map(report => report.num)).toEqual(result2) +}) \ No newline at end of file