This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "getify", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "lib/getify.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/johnste/getify.git" | ||
}, | ||
"author": "John Sterling <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/johnste/getify/issues" | ||
}, | ||
"homepage": "https://github.com/johnste/getify#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define([], factory); | ||
} else if (typeof module === 'object' && module.exports) { | ||
// Node. Does not work with strict CommonJS, but | ||
// only CommonJS-like environments that support module.exports, | ||
// like Node. | ||
module.exports = factory(); | ||
} else { | ||
// Browser globals (root is window) | ||
root.getify = factory(); | ||
} | ||
}(this, function () { | ||
|
||
function getify(obj) { | ||
const path = [] | ||
|
||
function getValueFromPath(obj) { | ||
return (val) => { | ||
try { | ||
return path.reduce((node, v) => node[v], obj) || val | ||
} | ||
catch(e) { | ||
return val | ||
} | ||
} | ||
} | ||
|
||
function handler() { | ||
return { | ||
get: function(target, key){ | ||
if (typeof key == 'symbol' || key == '__proto__') { | ||
return target[key] | ||
} | ||
|
||
path.push(key) | ||
return getProxy(getValueFromPath(obj)) | ||
} | ||
} | ||
} | ||
|
||
function getProxy(obj, path) { | ||
return new Proxy(obj, handler()) | ||
} | ||
|
||
// For consistency's sake, we return the root object as a function | ||
return getProxy(() => obj, path) | ||
} | ||
|
||
// Just return a value to define the module export. | ||
// This example returns an object, but the module | ||
// can return a function as the exported value. | ||
return getify; | ||
})); |