Skip to content

Instantly share code, notes, and snippets.

View TravelingTechGuy's full-sized avatar
💭
Looking for my next project...

Guy Vider TravelingTechGuy

💭
Looking for my next project...
View GitHub Profile
@TravelingTechGuy
TravelingTechGuy / updateNPM.sh
Created January 29, 2014 17:36
Update (global) NPM repositories, and show which repositories were updated by comparing version numbers
npm -g ls --depth=0 > ~/before.txt
npm -g update > /dev/null 2>&1
npm -g ls --depth=0 > ~/after.txt
diff -as ~/before.txt ~/after.txt
rm ~/before.txt ~/after.txt
@TravelingTechGuy
TravelingTechGuy / online.html
Created April 1, 2014 01:20
Check if device is online
<!DOCTYPE html>
<html>
<head>
<script>
window.addEventListener("offline", function(e) {alert("offline");})
window.addEventListener("online", function(e) {alert("online");})
</script>
</head>
<body>
</body>
@TravelingTechGuy
TravelingTechGuy / getValueFromQueryString.js
Created June 10, 2014 18:39
Get value from a key in a query string
var getValueFromQueryString = function(url, key) {
return decodeURIComponent(new RegExp('[?|&]' + key + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [,""])[1].replace(/\+/g, '%20') || null;
};
@TravelingTechGuy
TravelingTechGuy / occurrences.js
Created June 16, 2014 04:55
Find number of occurrences of substring in string
var str = "This is a string.",
term = 'is',
reg = new RegExp(term, 'ig'); //remove the `i` if you need case-sensitivity
var count = str.match(reg).length;
console.log(count);
@TravelingTechGuy
TravelingTechGuy / fillArray.js
Created June 17, 2014 23:54
Fill array with same value/function
var myFunc = function(x){return x*2;};
var num = 3;
var array = Array.apply(null, Array(num)).map(function(){return myFunc;});
@TravelingTechGuy
TravelingTechGuy / redirectToHTTPS.js
Last active August 29, 2015 14:27
Redirect from HTTP to HTTPS (in production)
//redirect to https in production - add above all other routes
app.use('*', (req, res, next) => {
if(process.env.NODE_ENV !== 'development' && req.headers['x-forwarded-proto'] !== 'https') {
res.redirect(`https://${req.host}${req.url}`);
}
else {
next();
}
});
@TravelingTechGuy
TravelingTechGuy / DisplayJSFunctionName1.js
Created August 27, 2011 10:28
Display JavaScript function name
var fname = /^function\s+([^(]+)/.exec(arguments.callee.toString())[1];
console.log(fname);
//alert(fname);
@TravelingTechGuy
TravelingTechGuy / DisplayJSFunctionName2.js
Created August 27, 2011 10:32
Display JavaScript function name
function reportName() {
var fname = /^function\s+([^(]+)/.exec(reportName.caller.toString())[1];
console.log(fname);
}
function MyFunctionHAsALongName() {
reportName();
//Do some stuff
}
@TravelingTechGuy
TravelingTechGuy / fuzzyclock.rb
Created August 27, 2011 22:51
Create a fuzzy, verbal clock
#!/usr/bin/env ruby
hour, minute = Time.now.strftime('%I').to_i, Time.now.min
ones = %w{ nil one two three four five six seven eight nine }
teens = %w{ ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen }
tens = %w{ nil nil twenty thirty forty fifty }
minute_str = case minute
when 0 then 'o-clock'
when 1..9 then "o-#{ones[minute]}"
@TravelingTechGuy
TravelingTechGuy / JSONTest.py
Created August 28, 2011 06:49
Test JSON service using python
import urllib2
import json
url = '<INSERT URL>'
data = {
'aaa': '7673336666',
'bbb': ['4544444445','xxxxxxx','334tttt890'],
'ccc': {
'ddd': 'on',
'eee': 5