Skip to content

Commit d9a35bd

Browse files
committed
Merge branch 'dev'
2 parents 8535cf3 + 4b95bc5 commit d9a35bd

File tree

9 files changed

+205
-101
lines changed

9 files changed

+205
-101
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dist-ssr
2727
*.sw?
2828
coverage
2929
coverage/**
30+
src/*
3031

3132
# store file
3233
resources/stores/*

TO-DO.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
2. 难度筛选
1414
3. 通过率
1515
- [ ] 登陆和提交到leetcode
16-
- [ ] 真随机一题实现
16+
1717
- [ ] 国际化
1818
- [ ] 插件制作-WS/VS code
1919
- [ ] fork脚本的编写
@@ -27,7 +27,6 @@
2727

2828
### 代码优化及基础建设
2929

30-
- [ ] 优化随机题目的随机方式,减少请求
3130
- [ ] e2e测试集成和仿真环境搭建
3231

3332
## 已完归档
@@ -53,3 +52,5 @@
5352
- [x] 19.基础参数设置与缓存
5453
- [x] 20.添加语言的设定
5554
- [x] 21.store文件升级冲突引起的报错无感修复
55+
- [x] 22.优化随机题目的随机方式,减少请求
56+
- [x] 23.真随机一题实现

common/constants/question.const.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
// region 项目相关
12
// 默认语言
23
export const DefaultLang = 'javascript'
3-
44
// 默认版本号
55
export const DefaultVer = '0.0.0'
6-
76
// 包名
87
export const PackageName = 'leetcode-practice'
98
// github 主账号
10-
export const GITHUB_HOST = 'wh131462'
9+
export const GITHUB_HOST = 'EternalHeartTeam'
10+
// endregion
1111
// region 域名列表
1212
// npm 主域名
1313
export const NPM_URL = 'https://registry.npmjs.org/'
@@ -18,3 +18,7 @@ export const GITHUB_URL = 'https://github.com/'
1818
// gitee
1919
export const GITEE_URL = 'https://gitee.com/'
2020
// endregion
21+
// region 实用变量
22+
// 默认请求数据数量限制
23+
export const DefaultLimit = 50
24+
// endregion

common/utils/create-check/createUtil.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export function create(mode, question, baseDir) {
2323
})
2424
}
2525

26-
export async function createQuestionByTitleSlug(titleSlug, baseDir = process.cwd()) {
26+
export async function createQuestionByTitleSlug(
27+
titleSlug,
28+
baseDir = process.cwd(),
29+
) {
2730
const { question } = await getQuestionIdBySlug(titleSlug)
2831

2932
await createQuestionById(question.questionId, baseDir)
@@ -35,5 +38,4 @@ export async function createQuestionById(id, baseDir) {
3538
process.exit(0)
3639
}
3740
await create('identity', question, baseDir)
38-
// process.exit(0)
3941
}

common/utils/question-handler/getRandomId.js

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import { existsSync, mkdirSync, readdirSync } from 'node:fs'
2-
import { normalize } from 'node:path'
1+
import { readdirSync } from 'node:fs'
32
import { getQuestionListJson } from '#resources/headers/questionListJson.js'
3+
import { graphql } from '#common/utils/http/graphql.js'
4+
import { DefaultLimit } from '#common/constants/question.const.js'
45

56
/**
6-
* todo 需要先获取全部题目列表 需要处理随机的问题
7-
* @returns {*[]}
7+
* 获取指定页数的ids
88
*/
9-
export async function getAllIds() {
10-
const total = await getCount()
11-
const randomSkip = total
12-
const res = await fetch(
13-
'https://leetcode.cn/graphql/',
14-
getQuestionListJson(0),
15-
).then(res => res.json())
9+
export async function getIds(index, limit = DefaultLimit) {
10+
const res = await graphql(getQuestionListJson(index, limit))
1611
return res?.data?.problemsetQuestionList?.questions?.map(
1712
q => q.frontendQuestionId,
1813
)
@@ -21,25 +16,45 @@ export async function getAllIds() {
2116
* 获取总数
2217
* @returns {Promise<any>}
2318
*/
24-
export async function getCount() {
25-
return fetch('https://leetcode.cn/graphql/', getQuestionListJson(0))
26-
.then(res => res.json())
27-
.then(res => res?.data?.problemsetQuestionList?.total)
19+
export function getCount() {
20+
return graphql(getQuestionListJson(0)).then(
21+
res => res?.data?.problemsetQuestionList?.total,
22+
)
2823
}
2924
/**
30-
* 读取本地存在的所有题目 并随机题目
25+
* 获取随机的一个id 只要保证没有存在过就可以
3126
*/
3227
export async function getRandomId() {
28+
// 去除所有的标题 剩下的就是id
3329
const parse = name => name.replace(/\.[a-zA-Z0-9-]+$/i, '')
34-
// src 目录
35-
const src = normalize('./src/')
36-
// 不存在就创建
37-
existsSync(src) || mkdirSync(src)
38-
// 所有本地题目
39-
const allLocalIds = readdirSync(src).map(o => parse(o))
40-
const allIds = await getAllIds()
41-
const filtered = allIds.filter(o => !allLocalIds.includes(o))
42-
return filtered[random(filtered.length)]
30+
// 获取一个 递归的获取 总会有一个 直到数量到达最大值
31+
const getOne = async (waitIndexList, localIds) => {
32+
const randomIndex = waitIndexList[random(waitIndexList.length)]
33+
const ids = await getIds(randomIndex)
34+
// 过滤后的结果
35+
const filtered = ids.filter(o => !localIds.includes(o))
36+
if (randomIndex === undefined)
37+
return null
38+
if (filtered.length) {
39+
return filtered[random(filtered.length)]
40+
}
41+
else {
42+
waitIndexList.splice(waitIndexList.findIndex(i => i === randomIndex))
43+
return await getOne(waitIndexList, localIds)
44+
}
45+
}
46+
// 所有本地题目的id
47+
const allLocalIds = readdirSync(process.cwd()).map(parse)
48+
// 最大的数量
49+
const maxLength = await getCount()
50+
const waitIndexList = Array.from({
51+
length: Math.ceil(maxLength / DefaultLimit),
52+
}).map((_, i) => i)
53+
const one = await getOne(waitIndexList, allLocalIds)
54+
if (one === null)
55+
console.log('恭喜!你已经刷完了所有的题目!')
56+
else
57+
return one
4358
}
4459
/**
4560
* 获取长度内的随机数组下标

common/utils/question-handler/showLogs.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import v8 from 'node:v8'
2+
import { Table } from 'console-table-printer'
23
import { getFileSize } from '../functions/sizeUtil.js'
34
import { isSameData } from '../functions/isSameData.js'
45
import { setDataStructure } from './parseStructure.js'
@@ -59,5 +60,24 @@ export function showLogs(fnName, paramMap, compareMap) {
5960
)
6061
logsItems.push(logItem)
6162
})
62-
console.table(logsItems)
63+
64+
const logTable = new Table({
65+
columns: [
66+
{ name: '测试结果', title: '测试结果', alignment: 'center', maxLen: 10 },
67+
{ name: '预期结果', title: '预期结果', alignment: 'center', maxLen: 40 },
68+
{ name: '执行结果', title: '执行结果', alignment: 'center', maxLen: 40 },
69+
{ name: '执行用时', title: '执行用时', alignment: 'center', maxLen: 10 },
70+
{ name: '内存占用', title: '内存占用', alignment: 'center', maxLen: 10 },
71+
],
72+
})
73+
logsItems.forEach((item) => {
74+
for (const key in item) {
75+
if (key === '预期结果' || key === '执行结果') {
76+
item[key]
77+
= item[key]?.length >= 40 ? `${item[key].slice(0, 37)}...` : item[key]
78+
}
79+
}
80+
logTable.addRow(item, { color: item.测试结果 === '通过' ? 'green' : 'red' })
81+
})
82+
logTable.printTable()
6383
}

common/view/finder.view.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import select from '@inquirer/select'
2+
import input from '@inquirer/input'
3+
24
import {
35
getHot100QuestionListJSCode,
46
getTitleSlugList,
57
} from '#common/utils/question-handler/getHot100QuestionList.js'
6-
import { createQuestionByTitleSlug } from '#common/utils/create-check/createUtil.js'
8+
import {
9+
createQuestionById,
10+
createQuestionByTitleSlug,
11+
} from '#common/utils/create-check/createUtil.js'
12+
import { getQuestionByKeyword } from '#common/utils/question-getter/getQuestionByKeyword.js'
713

814
async function hotMode() {
915
const createMode = await select({
@@ -32,21 +38,23 @@ async function hotMode() {
3238
}
3339

3440
async function keywordMode() {
35-
const data = await getQuestionByKeyword(
36-
await inquirer.prompt(questionKeyword, null),
37-
)
38-
const questionList = [
39-
{
40-
type: 'list',
41-
name: 'chooseQuestion',
42-
message: '请选择题目',
43-
choices: [],
44-
},
45-
]
46-
const list = []
47-
data.map(q => list.push(q.titleCn))
48-
questionList[0].choices = list.join(',')
49-
console.log(list)
41+
const keyword = await input({ message: '请输入关键词', name: 'keyword' })
42+
const data = await getQuestionByKeyword(keyword)
43+
const list = data?.map((q) => {
44+
return {
45+
name: `${q.frontendQuestionId}.${q.titleCn}`,
46+
value: q.frontendQuestionId,
47+
}
48+
})
49+
const listQuestion = {
50+
type: 'list',
51+
name: 'chooseQuestion',
52+
message: '请选择题目',
53+
choices: list,
54+
}
55+
const chooseQuestion = await select(listQuestion)
56+
console.log(chooseQuestion)
57+
await createQuestionById(chooseQuestion, process.cwd())
5058
}
5159
async function selectMode() {}
5260

0 commit comments

Comments
 (0)