Skip to content

Instantly share code, notes, and snippets.

View morficus's full-sized avatar
🚀

Maurice Williams morficus

🚀
View GitHub Profile
@morficus
morficus / downloadBlob.js
Created December 30, 2018 01:34
Helper functions for dealing with base64 encoded files
/**
* Helper function to properly handler downloading Blobs
*
* @param {String} blob Base64 encoded file
* @param {String} userFriendlyName The file name to be used when downloading the blob
*/
export default function({ fileString, userFriendlyName }) {
const dataUri = `data:application/octet-stream;base64,${fileString}`
if (window.navigator.msSaveOrOpenBlob) {
// older versions of IE11 do not have proper support for download Blobs
/**
* Returns a string with all S3 unsafe characters replaced with a dash (-)
* @param dirtyFileName
* @returns {*}
*/
export default function(dirtyFileName) {
// handle any special characters that might be in the file name
// ref: https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-key-guidelines
return dirtyFileName.replace(/[&$@=;:+,?^{}%`\]">~<#|]/g, '-')
}
import isEmpty from 'lodash/isEmpty'
import isNumber from 'lodash/isNumber'
import isNan from 'lodash/isNaN'
/**
* Extracts digits from given string and formats it as a typical 10-digital US phone number with dashes (###-###-####)
*
* @param {String} dirtyNumber Any input string
* @returns {String} The formatted number
*/
export function formatPhoneNumber(dirtyNumber) {
@morficus
morficus / removeEmptyAttributes.js
Created December 30, 2018 01:30
Returns an object with non-empty attribute values
import isEmpty from 'lodash/isEmpty'
import isNumber from 'lodash/isNumber'
/**
* Returns an object with non-empty attribute values
* @param {Object} targetObject Any object
*/
export default function(targetObject) {
const cleanObject = {}
@morficus
morficus / index.js
Last active May 24, 2018 20:23
Serving an HTML file
/*
Here are three options to display an HTML file.
Put any of them on line 13 of index.js
*/
// option 1: if you just show some plain text to confirm things are running
app.get('/', (req, res) => {
res.set('Content-Type', 'text/plain').send('Type text you want to display here');
})
@morficus
morficus / wait.js
Created July 16, 2017 03:11
A quick and dirty "sleep" method in JavaScript
let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
@morficus
morficus / getCookieValue.js
Last active August 29, 2015 14:22
Constant-time helper function to get a particular value from a cookie with in the broswer
function getCookieValue(key) {
var namePosition,
nameValuePair,
value;
//step 0: append an equal sign to the key we are looking for
key = key + '=';
//step #1: find the position of the particular key we want with in the cookie string of the
namePosition = document.cookie.indexOf(key);
@morficus
morficus / 1-setup.md
Last active August 29, 2015 14:21
Ghost Blog i18n proof of concept

These instructions are intended help anyone who is interested in demo'ing the Ghost i18n proof-of-concept (or PoC) that I am throwing together. This PoC is for both the admin panel as well as server-side notifications.

See the POC in action

  1. Clone Ghost from my fork on GitHub
  2. Switch over to the i18n-poc branch (git checkout i18n-poc)
  3. Run npm install to download all node-related dependencies.
  4. Now follow the regular Ghost "developer install from git" instructions. https://github.com/TryGhost/Ghost#developer-install-from-git
  5. Once you are done with the those instructions, grunt dev` to start Ghost in development mode. Ghost will then be running at http://localhost:2368/ghost/
  6. Because this is just a proof of concept, the language files are inlcuded as part of the repo (in the real world, this would not be the case)
@morficus
morficus / JSON.stringify.js
Last active August 29, 2015 14:12
Custom replacer for JSON.stringify, specifically for handling circular references
//src: http://stackoverflow.com/a/11616993/1021300
//replace this empty object with a references to the circular one
var objectToPrint = {};
var cache = [],
output;
output = JSON.stringify(objectToPrint, function(key, value) {
if (typeof value === 'object' && value !== null) {
@morficus
morficus / img-alt-fixer.py
Last active August 29, 2015 14:05
Adds an alt-attribute to all img tags that don't already have one in html files inside the current directory (recessively)
from bs4 import BeautifulSoup
import glob
import os
totalfiles = 0
modifiedfiles = 0
totalchangedtags = 0
for root, subFolders, files in os.walk('.'):
for htmlFile in glob.glob(root + '/*.html'):