Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7285d76

Browse files
[更改] 项目模块规范由 CommonJS 迁移至 ESM
1 parent fd1056d commit 7285d76

18 files changed

+878
-56
lines changed
File renamed without changes.

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ npx jest [题目编号]
2424

2525
注:题解源码与单元测试文件的命名需要包含题目编号,如 `[题目编号]_[英文小驼峰题目名称].test.ts`
2626

27+
### 运行单个题解
28+
29+
```shell
30+
ts-node --esm ./src/[题目文件名]
31+
```
32+
2733
## 目录结构
2834

2935
```

‎babel.config.js‎ renamed to ‎babel.config.cjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ module.exports = {
22
presets: [
33
['@babel/preset-env', { targets: { node: 'current' } }],
44
'@babel/preset-typescript'
5+
],
6+
plugins: [
7+
'babel-plugin-transform-import-meta'
58
]
69
}

‎jest.config.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ export default {
8888
// ],
8989

9090
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
91-
// moduleNameMapper: {},
91+
moduleNameMapper: {
92+
'^(\\.{1,2}/.*)\\.js$': '1ドル'
93+
},
9294

9395
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
9496
// modulePathIgnorePatterns: [],
@@ -100,7 +102,7 @@ export default {
100102
// notifyMode: "failure-change",
101103

102104
// A preset that is used as a base for Jest's configuration
103-
preset: 'ts-jest'
105+
// preset: 'ts-jest',
104106

105107
// Run tests from one or more projects
106108
// projects: undefined,
@@ -173,7 +175,12 @@ export default {
173175
// testRunner: "jest-circus/runner",
174176

175177
// A map from regular expressions to paths to transformers
176-
// transform: undefined,
178+
transform: {
179+
'^.+\\.ts?$': ['ts-jest', {
180+
useESM: true,
181+
babelConfig: './babel.config.cjs'
182+
}]
183+
}
177184

178185
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
179186
// transformIgnorePatterns: [

‎package.json‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22
"name": "leetcode-problems",
33
"version": "1.0.0",
44
"description": "Ruixe 的 LeetCode 算法题集,使用 TypeScript 语言,集成 Jest 单元测试",
5+
"type": "module",
6+
"module": "esnext",
7+
"engines": {
8+
"node": ">=14.16"
9+
},
510
"scripts": {
611
"test": "jest"
712
},
8-
"keywords": ["LeetCode", "TypeScript", "UnitTest"],
13+
"keywords": [
14+
"LeetCode",
15+
"TypeScript",
16+
"UnitTest"
17+
],
918
"author": "RuixeWolf",
1019
"license": "MIT",
1120
"devDependencies": {
21+
"@babel/preset-env": "^7.20.2",
1222
"@babel/preset-typescript": "^7.18.6",
1323
"@types/jest": "^29.2.2",
1424
"@typescript-eslint/eslint-plugin": "^5.0.0",
25+
"babel-plugin-transform-import-meta": "^2.2.0",
1526
"eslint": "^8.0.1",
1627
"eslint-config-standard-with-typescript": "^23.0.0",
1728
"eslint-plugin-import": "^2.25.2",

‎src/1822_signOfTheProductOfAnArray.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ https://leetcode.cn/problems/sign-of-the-product-of-an-array/
3838
* @param {number[]} nums - 整数数组
3939
* @return {1 | 0 | -1} 乘积结果
4040
*/
41-
function arraySign (nums: number[]): number {
41+
exportfunction arraySign (nums: number[]): number {
4242
if (nums.includes(0)) return 0
4343
// 先假设乘积为正数
4444
let isPositive = true
@@ -48,5 +48,3 @@ function arraySign (nums: number[]): number {
4848
}
4949
return isPositive ? 1 : -1
5050
}
51-
52-
export { arraySign }

‎src/273_integerToEnglishWords.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function parseHundred (numStr: string): string {
101101
* @param {number} num - 0 ~ 2^31 - 1 之间的整数
102102
* @returns {string} 数字所对应的英文
103103
*/
104-
function numberToWords (num: number): string {
104+
exportfunction numberToWords (num: number): string {
105105
const numStr: string = num.toString(10)
106106
let result: string = ''
107107
// 解析 Hundred 及以下
@@ -123,5 +123,3 @@ function numberToWords (num: number): string {
123123
if (billionStr !== '000') result = `${parseHundred(billionStr)} Billion` + (result !== '' ? ` ${result}` : '')
124124
return result
125125
}
126-
127-
export { numberToWords }

‎src/704_binarySearch.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import testCases from '../tests/testCases/704_binarySearch'
2-
31
/*
42
704. 二分查找
53
https://leetcode.cn/problems/binary-search/
@@ -23,14 +21,17 @@ n 将在 [1, 10000]之间。
2321
nums 的每个元素都将在 [-9999, 9999]之间。
2422
*/
2523

24+
import { fileURLToPath } from 'url'
25+
import testCases from '../tests/testCases/704_binarySearch.js'
26+
2627
/**
2728
* 二分查找元素下标
2829
* - LeetCode 入口
2930
* @param nums - 待查找的整型数组,要求为升序且没有重复元素
3031
* @param target - 待查找的数字
3132
* @returns {number} 元素下标
3233
*/
33-
function search (nums: number[], target: number): number {
34+
exportfunction search (nums: number[], target: number): number {
3435
// 使用左右指针,每次查找 [left, right) 最中间的值
3536
let left = 0
3637
let right = nums.length
@@ -51,10 +52,8 @@ function search (nums: number[], target: number): number {
5152
}
5253

5354
// Debug
54-
if (require.main === module) {
55+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
5556
const { input, expected } = testCases[6]
5657
const output = search(input.nums, input.target)
5758
console.log({ input, output, expected })
5859
}
59-
60-
export { search }

‎src/706_designHashMap.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ myHashMap.get(2); // 返回 -1(未找到),myHashMap 现在为 [[1,1]]
3535
最多调用 104 次 put、get 和 remove 方法
3636
*/
3737

38-
import testCases from '../tests/testCases/706_designHashMap'
38+
import { fileURLToPath } from 'url'
39+
import testCases from '../tests/testCases/706_designHashMap.js'
3940

4041
class MyHashMap {
4142
mapLikedArray: number[][] = []
@@ -94,6 +95,8 @@ export function main (
9495
}
9596

9697
// Debug
97-
if (require.main === module) {
98-
main(testCases[0].input.operations, testCases[0].input.params)
98+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
99+
const { input, expected } = testCases[0]
100+
const output = main(input.operations, input.params)
101+
console.log({ input, output, expected })
99102
}

‎src/722_removeComments.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import testCases from '../tests/testCases/722_removeComments'
2-
1+
/* eslint-disable import/first */
32
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
43
`
54
722. 删除注释
@@ -61,13 +60,16 @@ source[i] 由可打印的 ASCII 字符组成。
6160
给定的源码中不会有单引号、双引号或其他控制字符。
6261
`
6362

63+
import { fileURLToPath } from 'url'
64+
import testCases from '../tests/testCases/722_removeComments.js'
65+
6466
/**
6567
* 去除 C++ 程序的注释
6668
* - LeetCode 入口
6769
* @param {string[]} source - 源码行数组
6870
* @returns {string[]} 去除注释后的代码行数组
6971
*/
70-
function removeComments (source: string[]): string[] {
72+
exportfunction removeComments (source: string[]): string[] {
7173
// 使用换行符合并所有代码行
7274
const sourceString = source.join('\n')
7375

@@ -127,9 +129,7 @@ function removeComments (source: string[]): string[] {
127129
}
128130

129131
// Debug
130-
if (require.main === module) {
131-
const testCase = testCases[0]
132-
console.log({ input: testCase.input, output: removeComments(testCase.input), expected: testCase.expected })
132+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
133+
const { input, expected } = testCases[0]
134+
console.log({ input, output: removeComments(input), expected })
133135
}
134-
135-
export { removeComments }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /