-
Rules for Clean Javascript
Rules for Clean Javascript 2017
Create MORE functions (rather than large functions)
-
Helps with DRY
-
Easier to refactor
Use Lodash rather than custom looping code (when you can)
-
Optimised for most common cases (much more optimised than underscore)
-
Peer-reviewed for bugs and performance
-
Creates more shareable code as long as both projects are using lodash
Use a consistent code format throughout the whole project
-
Make it auto-format on save
-
Can use Standard or SemiStandard
-
Auto convert tabs to spaces (IDE plugin or automated task)
-
Auto remove any excess spaces at end of line (IDE plugin or automated task)
-
Benefit: Removes noise from Pull-Request reviews
Use Longer names when possible (as long as they are more descriptive)
- It’s better to have longer variable/function names than comments that explain the purpose of a variable (see: Clean Code)
-
-
Magic Assert
About Magic Assert
The test framework Ava has introduced a very useful new feature that they call “Magic Assert”.
It is basically a normal assert statement, but when the result is not expected it shows a nice diff explaining what it go and what it was expecting.
Screenshot of Ava Magic Assert
This is the main feature that pulls me towards using Ava instead of Mocha or Jest testing frameworks. However it got me wondering, why not have an assert library that is generic enough to work with all test runners. Similar to how Chai works seamlessly across these frameworks it would be great to have a “magic-assert” library that also works with the testing framework of your choice.
Watch this space! 😉
-
Automatically Format Javascript and HTML files on Save with Grunt
Create the config
The following jobs will require a config variable is set, so it knows where all your javascript files are located.
var config = { all_js_src: ['./app/**/*.js'] }
Remove trailing whitespace on lines
First I wanted to automatically make sure I haven’t left any trailling whitespace on any lines. This will remove them automatically and can run on file save.
// // # Trim trailing whitespace on lines // * needed as this is a warning in Sonar // trimtrailingspaces: { all: { src: config.all_js_src, options: { filter: 'isFile', encoding: 'utf8', failIfTrimmed: false } } },
FixMyJS
You will need to have created a .jshint file with your preferences in the root directory of your project.
// // FixMyJS - Automatically fixes common JS Lint erorrs // fixmyjs: { options: { config: '.jshintrc', indentpref: 'spaces', camelcase: false, quotemark: true, curly: true, multivar: true, asi: true, eqeqeq: true, ext: '.js' }, all: { files: [{ expand: true, cwd: './', src: config.all_js_src, dest: './' }] } },
Make it beautiful
This will sort out the styling of the code to make sure all your files are consistent.
// // # Prettify Javascript files // 'jsbeautifier': { files: config.all_js_src, options: { js: { jslintHappy: false } } },
Grunt Task
Add this grunt task to your Gruntfile.js if you want all the tasks to run as one:
// // # This task will run all the automated javascript code cleaning // grunt.registerTask('cleanjscode', [ 'trimtrailingspaces', 'fixmyjs', 'jsbeautifier' ]);
Run on file Save
// // # Watches files for changes and runs tasks based on the changed files // watch: { dashboardjs: { files: config.all_js_src, tasks: [ 'cleanjscode' ] },
Git commit hooks
If you want to automatically do this just before commiting you can add the following grunt task:
// // # Pre Commit Git Hooks // githooks: { all: { // Will run the jshint and test:unit tasks at every commit 'pre-commit': 'cleanjscode' } },
-
Installing Apache mod_auth_openidc OpenID Authentication
Purpose: I wanted to use OpenID Authentication to restrict access to a few directories on my apache server
First download the installer from github
wget https://github.com/pingidentity/mod_auth_openidc/releases/download/v1.8.7/libapache2-mod-auth-openidc_1.8.7-1ubuntu1.trusty.1_amd64.deb
Install the package
sudo dpkg -i libapache2-mod-auth-openidc_1.8.7-1ubuntu1.trusty.1_amd64.deb
Install Dependencies
sudo apt-get install -f
Enable the module for apache
sudo a2enmod auth_openidc
Restart Apache
service apache2 restart
-
rarfile.RarUnknownError: Unknown exit code [1]: bsdtar: Unrecognized archive format Python
So recently I have been trying to unrar files in python with limited success.
After spending what felt like hours trying to track down the problem it was quite a simple fix.
So if you get this error:
rarfile.RarUnknownError: Unknown exit code [1]: bsdtar: Unrecognized archive format
Then you just need to install the unrar executable like so:
On Mac:
brew install unrar
On Ubuntu:
apt-get install unrar
Good luck!