Skip to content

Instantly share code, notes, and snippets.

💭
Looking for my next project...

Guy Vider TravelingTechGuy

View GitHub Profile
@TravelingTechGuy
TravelingTechGuy / await-delay.js
Last active Mar 4, 2019
A delay function that can be used with async-await
View await-delay.js
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Sample code using `delay`
const test = async () => {
const time = () => console.log((new Date).toLocaleTimeString());
time();
await delay(1000);
time();
await delay(3000);
time();
@TravelingTechGuy
TravelingTechGuy / fetch.js
Created Oct 21, 2017
Use `fetch` with `async` and `await`
View fetch.js
let getJson = async url => {
try {
let response = await fetch(url);
let json = await response.json();
return json;
}
catch(ex) {
//throw ex; //may want to handle externally
console.error(ex);
}
@TravelingTechGuy
TravelingTechGuy / defaultparams.js
Created Oct 21, 2017
Demonstrates taking an object parameters, with default values
View defaultparams.js
let foo = ({param1 = 1, param2 = 2, param3 = 3} = {}) => {
return (param1 + param2) / param3;
};
console.log(foo()); //returns 1
console.log(foo({param3: 1})); //returns 3
console.log(foo({param1: 7, param2: 2})); //returns 3
console.log(foo({param1: 6, param3: 2})); //returns 4
@TravelingTechGuy
TravelingTechGuy / ensureUnique.js
Created Mar 14, 2017
Ensure an array contains unique elements, using a Set
View ensureUnique.js
let arr = [2, 3, 5, 2, 1, 3];
arr = [...new Set(arr)]; //arr now contains [2, 3, 5, 1]
View keybase.md

Keybase proof

I hereby claim:

  • I am travelingtechguy on github.
  • I am travelingtechguy (https://keybase.io/travelingtechguy) on keybase.
  • I have a public key whose fingerprint is 518E C8E2 5D29 429C C77A 810A 5861 AB01 E38A 8CA4

To claim this, I am signing this object:

View BogusEvent.js
a = {preventDefault:function(){}};
View DisablePaste.js
//1. Plain JavaScript
document.getElementById("txtPassword").addEventListener("paste", function(e) {
e.preventDefault();
});
//2. If jQuery is used on the page, it might look like this:
$('#txtPassword').bind("paste", function(e) {
e.preventDefault();
});
@TravelingTechGuy
TravelingTechGuy / get_history.sh
Created May 9, 2016
Get your Chrome history as a CSV file
View get_history.sh
#!/bin/bash
# Locate the history file in your profile, and copy it to the same folder as this script.
# On Mac: ~/Library/Application\ Support/Google/Chrome/Default/History
# On Windows: C:\Users\YOUR USER NAME\AppData\Local\Google\Chrome\User Data\Default\History
sqlite3 History <<!
.headers on
.mode csv
.output out.csv
@TravelingTechGuy
TravelingTechGuy / redirectToHTTPS.js
Last active Aug 29, 2015
Redirect from HTTP to HTTPS (in production)
View redirectToHTTPS.js
//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 / bypass-should-eslint-error.js
Created Jun 12, 2015
Bypass ESLint no-unused-vars error for Should in a Mocha test
View bypass-should-eslint-error.js
// Without line 9 hack, linting the file throws: 5:4 error should is defined but never used no-unused-vars
'use strict';
var debug = require('debug')('test:myModel');
var should = require('should');
var security = require('../models/myModel');
before((done) => {
should; //bypass ESLint no-unused-var error
done();
You can’t perform that action at this time.