Skip to content

Instantly share code, notes, and snippets.

@joaquimnetocel
Forked from jherr/runify.js
Created October 27, 2023 01:21
Show Gist options
  • Select an option

  • Save joaquimnetocel/c46ff8c7e2f6cd5e5176a0c7cc75859e to your computer and use it in GitHub Desktop.

Select an option

Save joaquimnetocel/c46ff8c7e2f6cd5e5176a0c7cc75859e to your computer and use it in GitHub Desktop.

Revisions

  1. @jherr 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;
    }
    }