Skip to content

Commit

Permalink
feat: add the ability to use default values in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
aradzie committed Nov 26, 2024
1 parent 55bc423 commit 343cd3b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
29 changes: 29 additions & 0 deletions packages/keybr-settings/lib/props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,45 +86,62 @@ test("change props", () => {
test("read boolean", () => {
const p = props.boolean;
equal(new Settings().get(p), false);
equal(new Settings().get(p, true), true);
equal(new Settings({ [p.key]: null }).get(p), false);
equal(new Settings({ [p.key]: null }).get(p, true), true);
equal(new Settings({ [p.key]: "abc" }).get(p), false);
equal(new Settings({ [p.key]: "abc" }).get(p, true), true);
equal(new Settings({ [p.key]: true }).get(p), true);
});

test("read number", () => {
const p = props.number;
equal(new Settings().get(p), 0);
equal(new Settings().get(p, 1), 1);
equal(new Settings({ [p.key]: null }).get(p), 0);
equal(new Settings({ [p.key]: null }).get(p, 1), 1);
equal(new Settings({ [p.key]: "abc" }).get(p), 0);
equal(new Settings({ [p.key]: "abc" }).get(p, 1), 1);
equal(new Settings({ [p.key]: 1000 }).get(p), 100);
equal(new Settings({ [p.key]: 99 }).get(p), 99);
});

test("read string", () => {
const p = props.string;
equal(new Settings().get(p), "");
equal(new Settings().get(p, "abc"), "abc");
equal(new Settings({ [p.key]: null }).get(p), "");
equal(new Settings({ [p.key]: null }).get(p, "abc"), "abc");
equal(new Settings({ [p.key]: 123 }).get(p), "");
equal(new Settings({ [p.key]: 123 }).get(p, "abc"), "abc");
equal(new Settings({ [p.key]: "abcxyz" }).get(p), "abc");
equal(new Settings({ [p.key]: "123" }).get(p), "123");
});

test("read enum", () => {
const p = props.enum;
equal(new Settings().get(p), Letter.None);
equal(new Settings().get(p, Letter.A), Letter.A);
equal(new Settings({ [p.key]: null }).get(p), Letter.None);
equal(new Settings({ [p.key]: null }).get(p, Letter.A), Letter.A);
equal(new Settings({ [p.key]: 123 }).get(p), Letter.None);
equal(new Settings({ [p.key]: 123 }).get(p, Letter.A), Letter.A);
equal(new Settings({ [p.key]: "abc" }).get(p), Letter.None);
equal(new Settings({ [p.key]: "abc" }).get(p, Letter.A), Letter.A);
equal(new Settings({ [p.key]: "a" }).get(props.enum), Letter.A);
equal(new Settings({ [p.key]: "b" }).get(props.enum), Letter.B);
});

test("read item", () => {
const p = props.item;
equal(new Settings().get(p), Digit.NONE);
equal(new Settings().get(p, Digit.ONE), Digit.ONE);
equal(new Settings({ [p.key]: null }).get(p), Digit.NONE);
equal(new Settings({ [p.key]: null }).get(p, Digit.ONE), Digit.ONE);
equal(new Settings({ [p.key]: 123 }).get(p), Digit.NONE);
equal(new Settings({ [p.key]: 123 }).get(p, Digit.ONE), Digit.ONE);
equal(new Settings({ [p.key]: "abc" }).get(p), Digit.NONE);
equal(new Settings({ [p.key]: "abc" }).get(p, Digit.ONE), Digit.ONE);
equal(new Settings({ [p.key]: "one" }).get(p), Digit.ONE);
equal(new Settings({ [p.key]: "two" }).get(p), Digit.TWO);
});
Expand All @@ -135,14 +152,26 @@ test("read flags", () => {
new Settings().get(p), //
["a", "b", "c"],
);
deepEqual(
new Settings().get(p, ["a"]), //
["a"],
);
deepEqual(
new Settings({ [p.key]: null }).get(p), //
["a", "b", "c"],
);
deepEqual(
new Settings({ [p.key]: null }).get(p, ["a"]), //
["a"],
);
deepEqual(
new Settings({ [p.key]: 123 }).get(p), //
["a", "b", "c"],
);
deepEqual(
new Settings({ [p.key]: 123 }).get(p, ["a"]), //
["a"],
);
deepEqual(
new Settings({ [p.key]: "a" }).get(p), //
["a"],
Expand Down
36 changes: 18 additions & 18 deletions packages/keybr-settings/lib/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type AnyProp<T> = {
readonly key: string;
readonly defaultValue: T;
toJson(value: T): unknown;
fromJson(value: unknown): T;
fromJson(value: unknown, defaultValue?: T): T;
};

export type BooleanProp = {
Expand Down Expand Up @@ -59,8 +59,8 @@ export function booleanProp(key: string, defaultValue: boolean): BooleanProp {
toJson(value: boolean): unknown {
return value;
},
fromJson(value: unknown): boolean {
return typeof value === "boolean" ? value : defaultValue;
fromJson(value: unknown, defaultValue0 = defaultValue): boolean {
return typeof value === "boolean" ? value : defaultValue0;
},
};
}
Expand All @@ -85,8 +85,8 @@ export function numberProp(
toJson(value: number): unknown {
return clamp(value, min, max);
},
fromJson(value: unknown): number {
return typeof value === "number" ? clamp(value, min, max) : defaultValue;
fromJson(value: unknown, defaultValue0 = defaultValue): number {
return typeof value === "number" ? clamp(value, min, max) : defaultValue0;
},
};
}
Expand All @@ -108,8 +108,8 @@ export function stringProp(
toJson(value: string): unknown {
return trim(value, maxLength);
},
fromJson(value: unknown): string {
return typeof value === "string" ? trim(value, maxLength) : defaultValue;
fromJson(value: unknown, defaultValue0 = defaultValue): string {
return typeof value === "string" ? trim(value, maxLength) : defaultValue0;
},
};
}
Expand All @@ -134,10 +134,10 @@ export function enumProp(
toJson(value: number): unknown {
return map.get(value);
},
fromJson(value: unknown): number {
fromJson(value: unknown, defaultValue0 = defaultValue): number {
return typeof value === "string"
? (map.get(value) ?? defaultValue)
: defaultValue;
? (map.get(value) ?? defaultValue0)
: defaultValue0;
},
};
}
Expand All @@ -155,10 +155,10 @@ export function itemProp<T extends EnumItem>(
toJson(value: T): unknown {
return value.id;
},
fromJson(value: unknown): T {
fromJson(value: unknown, defaultValue0 = defaultValue): T {
return typeof value === "string"
? all.get(value, defaultValue)
: defaultValue;
? all.get(value, defaultValue0)
: defaultValue0;
},
};
}
Expand All @@ -176,10 +176,10 @@ export function xitemProp<T extends XEnumItem>(
toJson(value: T): unknown {
return value.id;
},
fromJson(value: unknown): T {
fromJson(value: unknown, defaultValue0 = defaultValue): T {
return typeof value === "string"
? all.get(value, defaultValue)
: defaultValue;
? all.get(value, defaultValue0)
: defaultValue0;
},
};
}
Expand All @@ -197,10 +197,10 @@ export function flagsProp(
toJson(value: Flags): unknown {
return value.filter((v) => all.includes(v)).join(",");
},
fromJson(value: unknown): Flags {
fromJson(value: unknown, defaultValue0 = defaultValue): Flags {
return typeof value === "string"
? value.split(",").filter((v) => all.includes(v))
: defaultValue;
: defaultValue0;
},
};
}
Expand Down
7 changes: 5 additions & 2 deletions packages/keybr-settings/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ export class Settings {
return this.#isNew;
}

get<T>(prop: AnyProp<T>): T {
return prop.fromJson(this.#json[prop.key] ?? defaultJson[prop.key]);
get<T>(prop: AnyProp<T>, defaultValue?: T): T {
return prop.fromJson(
this.#json[prop.key] ?? defaultJson[prop.key],
defaultValue,
);
}

set<T>(prop: AnyProp<T>, value: T): Settings {
Expand Down

0 comments on commit 343cd3b

Please sign in to comment.