Skip to content

Commit

Permalink
fix(設定): index in compact form not handled correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
syimyuzya committed Aug 4, 2024
1 parent 4efc0c9 commit 4f483da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/推導設定.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ test("建立設定(簡略形式)", () => {
expect(設定.列表).toEqual(EXAMPLE);
});

test("選單項(緊湊格式下標)", () => {
const 設定 = new 推導設定([["param1", [2, 42, 43, 1]]]);
expect(設定.列表).toHaveProperty(["0", "value"], 43);
});

test("clone", () => {
const 設定 = new 推導設定(EXAMPLE);
expect(設定.列表).toEqual(EXAMPLE);
Expand Down
7 changes: 4 additions & 3 deletions src/推導設定.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class 推導設定 {
const 解析錯誤: string[] = [];
const seenKeys = new Set<string>();
this.列表 = 設定列表.flatMap((原始設定項, i): 設定項[] => {
let valueIsIndex = false;
if (isArray(原始設定項)) {
// 以陣列格式指定的參數,轉換為物件格式
if (原始設定項.length !== 2 && 原始設定項.length !== 3) {
Expand Down Expand Up @@ -84,10 +85,10 @@ export default class 推導設定 {
options = rawValue.slice(1);
if (
typeof value === "number" &&
!options.some(x => x === value || (x as { value?: unknown })?.value === value)
!options.some(x => x === value || (typeof x === "object" && x && "value" in x && x.value === value))
) {
value = value - 1;
// FIXME this is not quite right
valueIsIndex = true;
}
}

Expand Down Expand Up @@ -168,7 +169,7 @@ export default class 推導設定 {
parsedOptions.push({ ...option });
}

if (!parsedOptions.some(option => option.value === 設定項.value)) {
if (valueIsIndex || !parsedOptions.some(option => option.value === 設定項.value)) {
if (typeof 設定項.value === "number" && 0 <= 設定項.value && 設定項.value < parsedOptions.length) {
設定項.value = parsedOptions[設定項.value]!.value;
} else {
Expand Down

0 comments on commit 4f483da

Please sign in to comment.