Skip to content
This repository has been archived by the owner on Nov 21, 2019. It is now read-only.

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
johnste committed Mar 7, 2016
1 parent c71b61b commit 9311723
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function getValueFromPath(obj, path) {
return function() {
try {
return path.reduce((node, v) => node[v], obj)
}
catch(e) {
return
}
}
}

export default function getify(obj) {
const path = []
function handler(path) {
return {
get: function(target, key){
if (typeof key == 'symbol' || key == '__proto__') {
return target[key]
}

// Add key onto path
const newPath = [...path, key]

return getProxy(getValueFromPath(obj, newPath), newPath)
}
}
}

function getProxy(obj, path) {
return new Proxy(obj, handler(path))
}

// For consistency's sake, we return the root object as a function
function getRoot() {
return obj;
}

return getProxy(getRoot , path)
}

0 comments on commit 9311723

Please sign in to comment.