A safe javascript, finally!
npm install safejs-cli --save-devWhat if we could simply write safe in front of an unsafe bit of code to make it safe. No surrounding validation, no try / catch, no worries, just like that.
Well now you can. ⚡
Previously in Vanilla JS:
// Accessing deeply nested variables in a safe way quickly gets messyif(response&&response.data&&response.data.user&&response.data.user[0]&&response.data.user[0].id){console.log(response.data.user[0].id);}Equivalent in SafeJS:
// Not anymoreconsole.log(saferesponse.data.user[0].id);Previously in Vanilla JS:
letid;if(response&&response.data&&response.data.user&&response.data.user[0]&&response.data.user[0].id){id=response.data.user[0].id;}Equivalent in SafeJS:
// Note that the `( ... )` are important here to limit the context appropriatelyconstid=(saferesponse.data.user[0].id);Previously in Vanilla JS:
// It also works with unsafe conditionsif(response&&response.data&&response.data.user&&response.data.user[0]&&response.data.user[0].id&&response.data.user[0].id!=='unknown'){console.log('found');}Equivalent in SafeJS:
if(saferesponse.data.user[0].id!=='unknown'){console.log('found');}Previously in Vanilla JS:
// It also works with unsafe functionsletres;try{res=awaitaxios.get('url/does/not/exist');}catch(err){}console.log(res);// undefinedEquivalent in SafeJS:
constres=(safeawaitaxios.get('url/does/not/exist'));console.log(res);// undefinedBy default SafeJS will assume that when you write safe ... you are talking about the largest context of execution (aka the closest (...)). Unless you are running in an explicit context already, make sure you always specify the context around safe when using it.
Also note that SweetJS, the compiler used by SafeJS, is not very friendly with semicolons, it will fail to interprete the context in some edges cases (chaining of function declarations, chaining of function executions and function declarations). To be safe, always write your semicolons.
Here is a couple of example:
if(safea.b){}// validif(safea.b&&c.d){}// validif(safea.b&&safec.d){}// invalidif((safea.b)&&(safec.d)){}// validconsole.log(safea.b);// validconsole.log(safea.b,c);// invalidconsole.log((safea.b),c);// validconsta=safeb.c;// invalidconsta=(safeb.c);// validconstb=safefoo();// invalidconstb=(safefoo());// validFrom your root directory, run:
safejsThis will transpile your code into a folder named predist.
You can now run the entry point of your code in this folder to see it in action.
Hint: Customize your
startscript and soon you won't event remember this command
You can configure SafeJS using these optional flags if necessary:
safejs --inputDir=./src --outputDir=./predist --noBabel=true --log=falseYou can also use a safejs.config.js file at root level to specify your config if a few additional options:
module.exports={inputDir: './src',outputDir: './predist',noBabel: true,log: false,exclude: [/.+(.ghost.js)/g,/(excluded)/i,]}Note:
excludeis an array of regular expressions for paths to exclude
Simply set your source directory to ./predist (or custom directory) instead of ./src. Easy.
Note: For now SweetJS does not allow direct input manipulation, which prevents me from making a Webpack loader (would remove the need for a pre-distribution). In a further version I might fork SweetJS's compiler directly to allow it and make a custom loader.
If you want hot reload you can do so using Nodemon
Hint: Customize your
startandbuildscripts to transpile your code before running it