-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c901c25
commit be10083
Showing
2 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 将1 1 1解析为x y z | ||
// 将1.1 -20 -30.2解析为x y z | ||
// 将~1 ~1 ~1解析为x y z,解析时假设xyz上的~分别是一个全局变量 | ||
// 解析字符串为x, y, z | ||
|
||
const ops = {} | ||
ops['+'] = '+' | ||
ops['-'] = '-' | ||
|
||
export function xyz_dododo(xyz:string[],playerLocation=[0,0,0]) : number[] { | ||
// 初始化x, y, z | ||
let new_xyz = [0,0,0] | ||
|
||
// 遍历分割后的数组 | ||
xyz.forEach((part, index) => { | ||
// 否则直接解析为数字 | ||
if (!part.startsWith('~')) | ||
return new_xyz[index] = Number(part); | ||
|
||
let data: number|string = part.slice(1) // 去掉~ | ||
let op = ops[data[0]] // 存在风险 | ||
// 如果以~开头,假设它是一个全局变量 | ||
if (op === undefined) { | ||
op = '+' | ||
} else { | ||
data = data.slice(1) // 去掉运算符 | ||
} | ||
|
||
data = Number(data) | ||
if(isNaN(data)) | ||
throw new Error(['x','y','z'][index] + ' not a number') | ||
|
||
|
||
if(op === '+') | ||
data += playerLocation[index] | ||
if(op === '-') | ||
data -= playerLocation[index] | ||
|
||
new_xyz[index] = data | ||
}); | ||
|
||
return new_xyz; | ||
} | ||
|
||
function xyz_dododo_test() { | ||
// 示例用法 | ||
const result1 = xyz_dododo(["1", "1", "1"]); | ||
console.log(result1); // [ 1, 1, 1 ] | ||
|
||
const result2 = xyz_dododo(["1.1", "-20" ,"-30.2"]); | ||
console.log(result2); // [ 1.1, -20, -30.2 ] | ||
|
||
|
||
const result3 = xyz_dododo(["~2.2", "~+3" ,"~-4.5"]); | ||
console.log(result3); // [ 102.2, 203, -295.5 ] | ||
|
||
|
||
const result4 = xyz_dododo(["~/2.2", "~+0" ,"~-"]); | ||
console.log(result4); | ||
// throw new Error(['x','y','z'][index] + ' not a number') | ||
// ^ | ||
// | ||
// Error: x not a number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters