Skip to content

Instantly share code, notes, and snippets.

@bjdixon
bjdixon / cron.sh
Created February 8, 2017 02:54
crontab examples with path. Edit with crontab -e, list with crontab -l
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/5 * * * * /path/to/job/to/run/every/five/minutes
0 9 * * * /path/to/job/to/run/every/day/at/9am
0 9 * * * . /source/doesnt/work/so/use/dot/script.sh
0 9 * * * /path/script.sh >> /var/log/output_and_errors.log 2>&1
@bjdixon
bjdixon / backup-and-restore-mongodb.sh
Created February 14, 2017 08:10
Download and restore mongo database from remote host
# Download copy of database from remote host
mongodump --host mongo.hostname.com --db database-name --port 27017 --username user --password pwd --out /opt/backup/mongodump
# Restore copy of database to remote host
mongorestore --host mongo.hostname.com --db database-name --port 27017 --username user --password pwd /opt/backup/mongodump/database-name
@bjdixon
bjdixon / remove_extended_attrs.sh
Created March 23, 2017 08:03
remove extended attributes from files in mac os
xattr -rc ./*.png
@bjdixon
bjdixon / nginx.conf
Created March 28, 2017 08:46
nginx conf location block when using react router
location / {
try_files $uri $uri/ /index.html;
}
@bjdixon
bjdixon / update_if_already_inserted.sql
Created March 30, 2017 08:05
Insert a record, if the pk already exists then update the record
INSERT IN tablename (id, val1, val2) VALUES (1, 'a', 'b')
ON DUPLICATE KEY UPDATE val1 = 'a', val2 = 'b';
@bjdixon
bjdixon / docker_mysql.sh
Created April 11, 2017 08:06
create a mysql server in a docker container
docker run --detach --name instance-of-mysql -p 3306 -e MYSQL_ROOT_PASSWORD=secretPw -d mysql
@bjdixon
bjdixon / path.js
Created May 9, 2017 05:26
Use a path to get a nested value in an object. eg. obj.user.address.city == path(['user', 'address', 'city'], obj)
const isObject = item => item && typeof item === 'object' && !Array.isArray(item);
const path = ([...paths], obj) => {
const tmp = obj[paths.shift()];
return !isObject(tmp) || !paths.length ? tmp : path(paths, tmp);
};
@bjdixon
bjdixon / pick.js
Last active May 31, 2017 07:33
create a new object extracting only desired properties from a source object. eg pick(['name', 'age'], {name: 'hi', age: 42, alive: true}) == {name: 'hi', age: 42}
const pick = (props, obj) => props.reduce((acc, curr) => ({ ...acc, ...{[curr]: obj[curr]} }), {});
@bjdixon
bjdixon / round.js
Created May 17, 2017 07:49
Rounding floating point or fixed precision numbers in Javascript
// returns a string. fixed is an optional boolean flag to switch on fixed precision or not.
// If you want a number instead of a string prepend the unary plus operator, but then fixed precision cannot be guaranteed.
// -0.1 * 0.2 === -0.020000000000000004
// round(-0.1 * 0.2, 3, true) === "-0.020"
// round(-0.1 * 0.2, 3) === "-0.02"
// +round(-0.1 * 0.2, 3) === -0.02
// +round(-0.1 * 0.2, 3, true) === -0.02
const round = (value, decimals, fixed) => {
const r = Number(Math.round(value + 'e' + decimals) + 'e-' + decimals)
return fixed ? r.toFixed(decimals) : r + ''
@bjdixon
bjdixon / buildObj.js
Created May 17, 2017 10:21
Initialise a nested object from an array of keys
const build = path => path.reduceRight((o, i) => Object.assign({}, {[i]: o}), {})