Skip to content

Commit

Permalink
now bundling browser-version with webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
nash403 committed Sep 20, 2016
1 parent 6bb8322 commit f5179ae
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 40 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "presets": [ "es2015"] }
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
tests.js
node_modules/
1 change: 1 addition & 0 deletions browser-version/get-safe.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 22 additions & 38 deletions get-safe.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
(function(){
// Utils
const isFnCall = function (key){
if (typeof key !== 'string') return false;
return key.slice(-2) === "()";
};
/*
* @param {key} string concatenation of nested keys in this form: 'foo.bar.toto'.
* You can even call a function if the last key ends with '()'.
* @param {obj} the object we are accessing
* @param {...args} a sequence of arguments that may be passed to the function we are calling
* @return a nested value OR the result of a nested function OR undefined
*/
myExport((key, obj, ...args) => {
let splitted = key.split('.');
let lastkey = splitted.pop();
let isFnCallLastkey = isFnCall(lastkey);
lastkey = isFnCallLastkey ? lastkey.slice(0,-2) : lastkey;
let beforelast = splitted.reduce((a,b) => {
return a && a[b];
}, obj);
return beforelast && (typeof beforelast === 'object') && (isFnCallLastkey ? beforelast[lastkey](...args) : beforelast[lastkey]);
}, true, 'getSafe');
// Utils
const isFnCall = function (key){
if (typeof key !== 'string') return false;
return key.slice(-2) === "()";
};

// Exports
function myExport(exported, isDefault, name) {
if (!name) throw "myExport function: <name> mustn't be undefined";
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
if (isDefault) {
module.exports = exported;
} else {
module.exports[name] = exported;
}
}
else {
window[name] = exported;
}
};
})();
/*
* @param {key} string concatenation of nested keys in this form: 'foo.bar.toto'.
* You can even call a function if the last key ends with '()'.
* @param {obj} the object we are accessing
* @param {...args} a sequence of arguments that may be passed to the function we are calling
* @return a nested value OR the result of a nested function OR undefined
*/
module.exports = (key, obj, ...args) => {
let splitted = key.split('.');
let lastkey = splitted.pop();
let isFnCallLastkey = isFnCall(lastkey);
lastkey = isFnCallLastkey ? lastkey.slice(0,-2) : lastkey;
let beforelast = splitted.reduce((a,b) => {
return a && a[b];
}, obj);
return beforelast && (typeof beforelast === 'object') && (isFnCallLastkey ? beforelast[lastkey](...args) : beforelast[lastkey]);
};
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "safe access to deeply nested properties and functions in JS objects without getting a TypeError",
"main": "index.js",
"scripts": {
"test": "node tests.js"
"test": "node tests.js",
"build": "rimraf ./web/interpolate.js && webpack"
},
"keywords": [
"safe",
Expand All @@ -31,5 +32,12 @@
"url": "https://github.com/nash403/get-safe.git"
},
"author": "Nash403",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"babel-core": "^6.14.0",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.14.0",
"rimraf": "^2.5.4",
"webpack": "^1.13.2"
}
}
24 changes: 24 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var webpack = require('webpack');

module.exports = {
entry: "./index",
output: {
path: __dirname,
filename: "./browser-version/get-safe.js",
library: "getSafe",
libraryTarget: 'umd'
},
resolve: {
extensions: ["",".js"]
},
module{
loaders: [{
test: /\.js$/,
exclude: [/node_modules/], // exclude unwanted js files
loader: 'babel' // 'babel-loader' is also valid name
}]
},
plugins: [
new webpack.optimize.UglifyJsPlugin() // Plugin for js minification
]
}

0 comments on commit f5179ae

Please sign in to comment.