From 931172366bffb0d4b3300a6f6e2fff8fb46a5d1e Mon Sep 17 00:00:00 2001 From: John Sterling Date: Mon, 7 Mar 2016 09:15:16 +0100 Subject: [PATCH] First version --- index.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..437a026 --- /dev/null +++ b/index.js @@ -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) +}