Skip to content

Instantly share code, notes, and snippets.

View craigpalermo's full-sized avatar

Craig Palermo craigpalermo

View GitHub Profile
import { getName } from '../models/user';
jest.mock('../models/user', {} => ({
getName: jest.fn()
}));
test('if user model is mocked', () => {
getName.mockReturnValue('bob')
expect(getName()).toEqual('bob');
@craigpalermo
craigpalermo / asyncAwaitHttpExample.js
Created April 22, 2017 23:00
This example demonstrates a function that makes an HTTP request and uses async/await to handle the function it calls that returns a promise.
const request = require('request');
// request doesn't return a Promise, so it needs to be wrapped in a function
// that does for this example
function getRequest(url, qs = {}) {
return new Promise((resolve) => {
request
.get({ url, qs })
.on('response', (response) => {
response.on('data', (data) => {
@craigpalermo
craigpalermo / dognzb_watchlist_to_couchpotato.py
Last active January 17, 2017 04:24
Import a DOGnzb watchlist into your CouchPotato wanted list
"""
Description: Import DOGnzb watchlist into CouchPotato
Author: Craig Palermo
Date: 1/14/2017
"""
import argparse
import json
import logging
import sys
@craigpalermo
craigpalermo / ls_apache_enabled_servernames.sh
Created December 20, 2016 01:07
Prints ServerName line of each enabled Apache site configuration (so you can see which subdomains you're hosting)
ls /etc/apache2/sites-enabled/* | xargs cat | grep ServerName | uniq
@craigpalermo
craigpalermo / exponential-backoff.markdown
Last active September 23, 2016 00:21
Exponential Backoff
@craigpalermo
craigpalermo / gist:4ef6ac2a8cf36e288099bd6601c948ae
Created July 24, 2016 11:33
Apache - redirect traffic from subdomain to port on localhost
<VirtualHost *:80>
ServerAdmin me@mydomain.com
ServerName dev.mydomain.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
@craigpalermo
craigpalermo / archive_old_files.py
Last active October 4, 2015 05:36
A script that moves all files in current directory that haven't been modified in the desired number of days to a different folder.
from datetime import datetime, timedelta
from sets import Set
import argparse
import sys
import os
import time
import shutil
import re
@craigpalermo
craigpalermo / promise_callback.js
Created August 6, 2015 04:00
JavaScript example of using promises and callbacks
var promiser = function (callback) {
var promise = new Promise(function(resolve, reject){
console.log("Promise created");
window.setTimeout(function(){
resolve("Hello from Promise Land");
}, 3000);
});
promise.then(function(){
callback();
@craigpalermo
craigpalermo / focusToSelect.js
Created June 6, 2015 18:37
Select text on input focus
/*
Credit to CSS-Tricks.com ~ https://css-tricks.com/copy-paste-the-web/
*/
$('#ta').on("focus", function(e) {
e.target.select();
$(e.target).one('mouseup', function(e) {
e.preventDefault();
});
});
@craigpalermo
craigpalermo / angularjs-file-consolidation.md
Last active March 11, 2018 05:48
Use Grunt tasks to consolidate HTML, CSS, and JS files to improve application performance

Optimizing route load times in AngularJS

Abstract

By consolidating HTML templates, CSS stylesheets, and JavaScript, we can greatly reduce the amount of time spent waiting for requests to retrieve external resources and make transitions between routes appear much smoother.

Advantages of consolidation