From c181f1dd1f2f9445a4398a61f72398558e3be493 Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Sat, 1 Mar 2025 21:23:18 +0100 Subject: [PATCH] Hyprpanel config can now be represented as true JSON (#788) * feat(config): allow reading of JSON nesting * organized code Signed-off-by: Dany Sluijk * style(lib/option): remove unnessesary comments --------- Signed-off-by: Dany Sluijk Co-authored-by: Jas Singh --- src/lib/option.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/lib/option.ts b/src/lib/option.ts index c0a3097..275d35c 100644 --- a/src/lib/option.ts +++ b/src/lib/option.ts @@ -94,7 +94,7 @@ export class Opt extends Variable { } } - const cachedVariable = cacheData[this._id]; + const cachedVariable = this._findKey(cacheData, this._id.split('.')); if (cachedVariable !== undefined) { this.set(cachedVariable as T); @@ -158,6 +158,36 @@ export class Opt extends Variable { return undefined; } + + private _findKey(obj: Record, path: string[]): T | undefined { + const top = path.shift(); + + if (!top) { + // The path is empty, so this is our value. + return obj as T; + } + + if (typeof obj !== 'object') { + // Not an array, not an object, but we need to go deeper. + // This is invalid, so return. + return undefined; + } + + const mergedPath = [top, ...path].join('.'); + + if (mergedPath in obj) { + // The key exists on this level with dot-notation, so we return that. + return obj[mergedPath] as T; + } + + if (top in obj) { + // The value exists but we are not there yet, so we recurse. + return this._findKey(obj[top] as Record, path); + } + + // Key does not exist :( + return undefined; + } } /**