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

Commit

Permalink
UMD exports
Browse files Browse the repository at this point in the history
  • Loading branch information
johnste committed Mar 11, 2016
1 parent 9311723 commit 7017cd7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 39 deletions.
39 changes: 0 additions & 39 deletions index.js

This file was deleted.

19 changes: 19 additions & 0 deletions package.json
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"
}
55 changes: 55 additions & 0 deletions src/getify.js
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;
}));

0 comments on commit 7017cd7

Please sign in to comment.