Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:v1.0.5 #24

Merged
merged 12 commits into from
Mar 11, 2024
Prev Previous commit
Next Next commit
chore(finder.view): modify code to apply prettier
  • Loading branch information
wh131462 committed Mar 10, 2024
commit 59d0df4f59b486f32eb013b7eec648d9208426af
2 changes: 1 addition & 1 deletion .commitlintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
extends: ['@commitlint/config-conventional']
}
14 changes: 7 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -2,22 +2,22 @@ module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
es2021: true
},
extends: '@antfu',
overrides: [
{
env: {
node: true,
node: true
},
files: ['.eslintrc.{js,cjs}'],
parserOptions: {
sourceType: 'script',
},
},
sourceType: 'script'
}
}
],
parserOptions: {
ecmaVersion: 'latest',
ecmaVersion: 'latest'
},
rules: {},
rules: {}
}
9 changes: 4 additions & 5 deletions bin/lc.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ import { easyLanguageView } from '#common/view/language.view.js'
import { DefaultVer } from '#common/constants/question.const.js'
import {
create,
createQuestionById,
createQuestionById
} from '#common/utils/create-check/createUtil.js'

const version = process.env.VERSION ?? DefaultVer
@@ -36,7 +36,7 @@ program
.option('-l, --language [language]', 'Set/Get the code language of question.')
.option(
'-u, --update',
'Check the version to determine whether to update to the latest one.',
'Check the version to determine whether to update to the latest one.'
)
.parse(process.argv)

@@ -56,8 +56,7 @@ const cmdOpts = program.opts()
if (cmdOpts.language) {
if (cmdOpts.language !== true) {
await easyLanguageView(cmdOpts.language)
}
else {
} else {
const lang = await getQuestionLanguage()
console.log(`当前CLI语言环境为:${lang}`)
}
@@ -97,7 +96,7 @@ export const callModeAction = {
identity: async (id) => {
await createQuestionById(id, baseDir)
process.exit(0)
},
}
}
// 获取模式和参数
const mode = referMode(cmdArgs, cmdOpts)
5 changes: 2 additions & 3 deletions bin/lf.js
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ program
.option('-d, --directory <directory>', 'Set the question directory.')
.option(
'-u, --update',
'Check the version to determine whether to update to the latest one.',
'Check the version to determine whether to update to the latest one.'
)
.parse(process.argv)

@@ -36,8 +36,7 @@ const cmdOpts = program.opts()
if (cmdOpts.language) {
if (cmdOpts.language !== true) {
await easyLanguageView(cmdOpts.language)
}
else {
} else {
const lang = await getQuestionLanguage()
console.log(`当前CLI语言环境为:${lang}`)
}
16 changes: 7 additions & 9 deletions bin/lk.js
Original file line number Diff line number Diff line change
@@ -28,15 +28,15 @@ program
.option('-t, --today', 'Check the question today.')
.option(
'-i, --identity <identity>',
'Check the specified question by identity.',
'Check the specified question by identity.'
)
.option('-r, --random', 'Check the last random question.')
.option('-e, --easy', 'Use easy mode.')
.option('-d, --directory <directory>', 'Set the question directory.')
.option('-l, --language [language]', 'Set/Get the code language of question.')
.option(
'-u, --update',
'Check the version to determine whether to update to the latest one.',
'Check the version to determine whether to update to the latest one.'
)
.parse(process.argv)

@@ -61,8 +61,7 @@ const args = getArgs(mode, cmdArgs, cmdOpts)
if (cmdOpts.language) {
if (cmdOpts.language !== true) {
await easyLanguageView(cmdOpts.language)
}
else {
} else {
const lang = await getQuestionLanguage()
console.log(`当前CLI语言环境为:${lang}`)
}
@@ -90,14 +89,13 @@ async function check(mode, question) {
const filePath = path.join(
baseDir,
getQuestionFileName(question),
`question${getQuestionFileExtension(question?.lang)}`,
`question${getQuestionFileExtension(question?.lang)}`
)
if (!fs.existsSync(filePath)) {
console.log(`文件[${filePath}]不存在,请确保已经创建!`)
}
else {
} else {
console.log(
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检测结果:`,
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检测结果:`
)
await checkQuestion(filePath)
}
@@ -121,7 +119,7 @@ const callModeAction = {
: await getQuestionById(id)
await check('identity', question)
process.exit(0)
},
}
}

// 执行指令分发
6 changes: 2 additions & 4 deletions common/structures/ListNode.js
Original file line number Diff line number Diff line change
@@ -12,8 +12,7 @@ export class ListNode {
}

static parse(arr) {
if (arr.length === 0)
return null // Return null for an empty array
if (arr.length === 0) return null // Return null for an empty array

const head = new ListNode(arr.shift(), null)
let current = head
@@ -25,8 +24,7 @@ export class ListNode {
}

static toArray(listNodes, arr = []) {
if (listNodes === undefined || listNodes === null)
return arr
if (listNodes === undefined || listNodes === null) return arr

arr.push(listNodes.val)
return ListNode.toArray(listNodes.next, arr)
6 changes: 2 additions & 4 deletions common/structures/Node.js
Original file line number Diff line number Diff line change
@@ -30,15 +30,13 @@ export class Node {
}

static toArray(node) {
if (!node)
return []
if (!node) return []

const visited = new Set()
const result = []

const dfs = (currentNode) => {
if (visited.has(currentNode.val))
return
if (visited.has(currentNode.val)) return

const { neighbors, val } = currentNode
visited.add(val)
9 changes: 3 additions & 6 deletions common/structures/TreeNode.js
Original file line number Diff line number Diff line change
@@ -7,8 +7,7 @@ export class TreeNode {
}

static parse(arr) {
if (arr.length === 0)
return null
if (arr.length === 0) return null
const root = new TreeNode(arr[0])
const queue = [root]
for (let i = 1; i < arr.length; i += 2) {
@@ -27,8 +26,7 @@ export class TreeNode {

static toArray(treeNode) {
const result = []
if (!treeNode)
return result
if (!treeNode) return result

const queue = [treeNode]

@@ -38,8 +36,7 @@ export class TreeNode {
result.push(node.val)
queue.push(node.left)
queue.push(node.right)
}
else {
} else {
result.push(null)
}
}
10 changes: 4 additions & 6 deletions common/utils/create-check/createUtil.js
Original file line number Diff line number Diff line change
@@ -13,10 +13,9 @@ export function create(mode, question, baseDir) {
setQuestion(mode, question)
const questionDir = path.join(baseDir, getQuestionFileName(question))
createQuestion(question, questionDir).then(async (path) => {
if (!path)
path = await createQuestionCopy(question, questionDir)
if (!path) path = await createQuestionCopy(question, questionDir)
console.log(
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:${path}`,
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:${path}`
)
resolve(true)
})
@@ -25,15 +24,14 @@ export function create(mode, question, baseDir) {

export async function createQuestionByTitleSlug(
titleSlug,
baseDir = process.cwd(),
baseDir = process.cwd()
) {
const { question } = await getQuestionIdBySlug(titleSlug)

await createQuestionById(question.questionId, baseDir)
}
export async function createQuestionById(id, baseDir = process.cwd()) {
const question = await getQuestionById(id)
if (!question?.id)
console.log(`指定编号: [ ${id} ] 题目不存在.`)
if (!question?.id) console.log(`指定编号: [ ${id} ] 题目不存在.`)
await create('identity', question, baseDir)
}
6 changes: 2 additions & 4 deletions common/utils/create-check/refer-mode.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// 推测模式
export function referMode(args, opts) {
if (args.length > 0 || opts.identity)
return 'identity'
if (args.length > 0 || opts.identity) return 'identity'

if (opts.random)
return 'random'
if (opts.random) return 'random'

return 'today'
}
4 changes: 2 additions & 2 deletions common/utils/etc/createColorFont.js
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ export function createColorFont(font) {
const code = gradient_string([
{ color: '#ff0000', pos: 0 },
{ color: '#ffc600', pos: 0.5 },
{ color: '#003dff', pos: 1 },
{ color: '#003dff', pos: 1 }
])(font)
writeFileSync(path.resolve(process.cwd(), 'colorFont.js'), code)
console.log(
`[ColorFont]Create color font: ${font}\ncode location:${path.resolve(process.cwd(), 'colorFont.js')}`,
`[ColorFont]Create color font: ${font}\ncode location:${path.resolve(process.cwd(), 'colorFont.js')}`
)
console.log(code)
}
3 changes: 1 addition & 2 deletions common/utils/etc/typeof_.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
* @private
*/
export function typeof_(data) {
if (data === null)
return 'null'
if (data === null) return 'null'
else return typeof data
}
2 changes: 1 addition & 1 deletion common/utils/file/getCountBySameName.js
Original file line number Diff line number Diff line change
@@ -7,5 +7,5 @@ import { readdirSync } from 'node:fs'
* @returns {number}
*/
export function getCountBySameName(dir, name) {
return readdirSync(dir).filter(filename => filename.includes(name)).length
return readdirSync(dir).filter((filename) => filename.includes(name)).length
}
2 changes: 1 addition & 1 deletion common/utils/file/getFileListBySameName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readdirSync } from 'node:fs'

export function getFileListBySameName(dir, name) {
return readdirSync(dir).filter(filename => filename.includes(name))
return readdirSync(dir).filter((filename) => filename.includes(name))
}
11 changes: 4 additions & 7 deletions common/utils/functions/isSameData.js
Original file line number Diff line number Diff line change
@@ -7,8 +7,7 @@
export function isSameData(a, b) {
const typeA = typeof a
const typeB = typeof b
if (typeA !== typeB)
return false
if (typeA !== typeB) return false
switch (typeA) {
case 'bigint':
case 'boolean':
@@ -20,14 +19,12 @@ export function isSameData(a, b) {
case 'function':
return a.toString() === b.toString()
case 'object': {
if (a === null || a === undefined)
return a === b
if (a === null || a === undefined) return a === b

const keysA = Object.keys(a)
const keysB = Object.keys(b)
if (keysA.length !== keysB.length)
return false
return keysA.every(key => isSameData(a[key], b[key]))
if (keysA.length !== keysB.length) return false
return keysA.every((key) => isSameData(a[key], b[key]))
}
}
}
15 changes: 5 additions & 10 deletions common/utils/functions/sizeUtil.js
Original file line number Diff line number Diff line change
@@ -15,16 +15,11 @@ export const PB = 1024 * GB
* @return {size: number, label: string}
*/
export function getFileSizeUnit(size) {
if (size < KB)
return { size: 1, label: 'B' }
if (size < MB)
return { size: KB, label: 'KB' }
if (size < GB)
return { size: MB, label: 'MB' }
if (size < TB)
return { size: GB, label: 'GB' }
if (size < PB)
return { size: TB, label: 'TB' }
if (size < KB) return { size: 1, label: 'B' }
if (size < MB) return { size: KB, label: 'KB' }
if (size < GB) return { size: MB, label: 'MB' }
if (size < TB) return { size: GB, label: 'GB' }
if (size < PB) return { size: TB, label: 'TB' }

return { size: PB, label: 'PB' }
}
2 changes: 1 addition & 1 deletion common/utils/http/fetch_.js
Original file line number Diff line number Diff line change
@@ -6,5 +6,5 @@
* @private
*/
export async function fetch_(url, options) {
return await fetch(url, options).then(res => res.json())
return await fetch(url, options).then((res) => res.json())
}
4 changes: 2 additions & 2 deletions common/utils/question-getter/getQuestionById.js
Original file line number Diff line number Diff line change
@@ -5,11 +5,11 @@ import { graphql } from '#common/utils/http/graphql.js'
export async function getQuestionById(id) {
const base = await graphql(getQuestionSearchJson(id.toString()))
const questionContent = base?.data?.problemsetQuestionList?.questions?.find(
o => o?.frontendQuestionId === id.toString(),
(o) => o?.frontendQuestionId === id.toString()
)
if (!questionContent) {
return {
id: null,
id: null
}
}
const slug = questionContent.titleSlug
2 changes: 1 addition & 1 deletion common/utils/question-getter/getQuestionListHot100.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { getQuestionListHot100Json } from '#resources/headers/questionListHot100
export async function getQuestionListHot100() {
const res = await graphql(getQuestionListHot100Json())
const {
data: { studyPlanV2Detail },
data: { studyPlanV2Detail }
} = res
return studyPlanV2Detail
}
2 changes: 1 addition & 1 deletion common/utils/question-getter/getQuestionRandom.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ export async function getQuestionRandom() {
const id = await getRandomId()
const base = await graphql(getQuestionSearchJson(id.toString()))
const slug = base.data.problemsetQuestionList.questions.find(
o => o.frontendQuestionId === id.toString(),
(o) => o.frontendQuestionId === id.toString()
).titleSlug
const question = await getQuestionDetail(slug)
return question
4 changes: 2 additions & 2 deletions common/utils/question-handler/checkQuestion.js
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ export async function checkQuestion(path) {
path,
vm.createContext({
showLogs,
console,
}),
console
})
)
}
Loading
Loading