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 / await-delay.js
Last active March 4, 2019 23:44
A delay function that can be used with async-await
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 October 21, 2017 19:10
Use `fetch` with `async` and `await`
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
Last active January 12, 2023 19:22
Demonstrates taking an object parameters, with default values
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 March 14, 2017 21:14
Ensure an array contains unique elements, using a Set
let arr = [2, 3, 5, 2, 1, 3];
arr = [...new Set(arr)]; //arr now contains [2, 3, 5, 1]

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:

a = {preventDefault:function(){}};
@TravelingTechGuy
TravelingTechGuy / DisablePaste.js
Last active August 9, 2016 19:55
Disable paste event
//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 14:10
Get your Chrome history as a CSV file
#!/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 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 / bypass-should-eslint-error.js
Created June 12, 2015 15:33
Bypass ESLint no-unused-vars error for Should in a Mocha test
// 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();