Skip to content

Instantly share code, notes, and snippets.

@jmmitchell
jmmitchell / stdout-stderr-rtncode-in-bash.sh
Last active June 1, 2018 01:58
capture standard out, standard error and return code in bash
# #!/bin/bash
#
# based on posts from stackexchange:
# http://stackoverflow.com/a/26827443/171475
# http://stackoverflow.com/a/18086548/171475
# http://stackoverflow.com/a/28796214/171475
function example_function {
printf 'example output to stdout %d\n' {1..10}
echo >&2 'example output to stderr'
@jmmitchell
jmmitchell / ord-and-char-conversion-in-bash.sh
Last active December 1, 2021 18:07
converting from decimal value to the ascii character and from the ascii character to the ordinal number in bash
#!/bin/bash
# chr() - converts decimal value to its ASCII character representation
# ord() - converts ASCII character to its decimal value
chr() {
printf \\$(printf '%03o' $1)
}
ord() {
printf '%d' "'$1"
@jmmitchell
jmmitchell / manipulate-trailing-slash.sh
Created April 5, 2016 13:55
One liner for adding or removing trailing slash of a string in Bash
#!/bin/bash
# Add a trailing slash to a string if there is not one already
STR="/some/example/path-without-slash"
echo "${STR}$(printf \\$(printf '%03o' $(($(printf '%d' "'${STR:(-1)}")==47?0:47))))"
# remove a (single) trailing slash on a string if there is one
STR="/some/example/path-with-slash/"
echo "${STR%/}"

Whether you use 2 spaces or 4 spaces, there are a few simple things that can make your node.js code easier to read. We've been using them in all the hapi modules for over 4 years now to great results. This list is by no means complete but it highlights the most useful elements that will give you immediate value in reducing bugs.

Required modules

JavaScript makes it harder than most languages to know where variables are coming from. Variables assigned required modules are particularly important because they represent a singleton object shared with the entire application. There are also globals and module globals, along with function variables and arguments.

Traditionally, variables starting with an uppercase letter represent a class that must be instantiated using new. This was an important semantic in the early days of JavaScript but at this point, if you don't know Date requires new Date() you are probably very new. We have adopted CamelCase variable names for all module global variables which a