Skip to content

Instantly share code, notes, and snippets.

@jherr
Created September 22, 2023 21:54
Show Gist options
  • Save jherr/b21f81b6a7a8cfa6a1bf60cdd9f1c638 to your computer and use it in GitHub Desktop.
Save jherr/b21f81b6a7a8cfa6a1bf60cdd9f1c638 to your computer and use it in GitHub Desktop.

Revisions

  1. jherr created this gist Sep 22, 2023.
    24 changes: 24 additions & 0 deletions runify.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    export function runify(obj) {
    if(Array.isArray(obj)) {
    return obj.map(runify);
    } else if(typeof obj === 'object') {
    let rune = $state(obj);
    let output = {};
    for(let key in rune) {
    if(typeof obj[key] === 'object') {
    obj[key] = runify(obj[key]);
    }
    Object.defineProperty(output, key, {
    get() { return rune[key]; },
    set(val) {
    console.log('setting', key, val);
    rune[key] = val;
    },
    enumerable: true,
    });
    }
    return output;
    } else {
    return obj;
    }
    }