Skip to content

Commit 494517a

Browse files
committed
feat: transform setting value
1 parent 0db7243 commit 494517a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/utils/settings/RegisteredSetting.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export default class RegisteredSetting {
3838
#onChange;
3939
/** @type {typeof EventEmitter} */
4040
#events;
41+
/** @type {function(value: any): any} */
42+
#transformer;
4143

4244
constructor({
4345
category,
@@ -57,6 +59,7 @@ export default class RegisteredSetting {
5759
refresh: refreshText,
5860
remove,
5961
reset,
62+
transform,
6063
type,
6164
} = {}) {
6265
this.#key = key;
@@ -75,6 +78,7 @@ export default class RegisteredSetting {
7578
this.#note = note;
7679
this.#refresh = refreshText;
7780
this.#events = events;
81+
this.#transformer = transform;
7882
if (typeof onChange === 'function') {
7983
this.#onChange = onChange;
8084
}
@@ -178,7 +182,7 @@ export default class RegisteredSetting {
178182
if (val === null) {
179183
return this.default;
180184
}
181-
return this.type.value(val, this.data);
185+
return this.#transform(this.type.value(val, this.data));
182186
}
183187

184188
/**
@@ -187,9 +191,9 @@ export default class RegisteredSetting {
187191
get default() {
188192
const val = this.#default;
189193
if (val !== undefined) {
190-
return this.type.value(this.#value(val), this.data);
194+
return this.#transform(this.type.value(this.#value(val), this.data));
191195
}
192-
return this.type.default(this.data);
196+
return this.#transform(this.type.default(this.data));
193197
}
194198

195199
#convert(converter) {
@@ -205,6 +209,13 @@ export default class RegisteredSetting {
205209
}
206210
}
207211

212+
#transform(value) {
213+
if (typeof this.#transformer === 'function') {
214+
return this.#transformer(value);
215+
}
216+
return value;
217+
}
218+
208219
#value(input) {
209220
if (typeof input === 'function') {
210221
return input();

0 commit comments

Comments
 (0)