Skip to content

Commit

Permalink
feat: print report sort by item.num (#77)
Browse files Browse the repository at this point in the history
* 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 <flyfish.zy@gmail.com>

* Update src/utils/report.ts

Co-authored-by: Joe Zhang <flyfish.zy@gmail.com>

* 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 <lizy@lanfun.com.cn>
Co-authored-by: Chieffo2021 <85914490+Chieffo2021@users.noreply.github.com>
Co-authored-by: Joe Zhang <flyfish.zy@gmail.com>
  • Loading branch information
4 people committed Dec 28, 2021
1 parent 3b71ea1 commit e51260d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/utils/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
})
Expand All @@ -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 }
30 changes: 30 additions & 0 deletions tests/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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)
})

0 comments on commit e51260d

Please sign in to comment.