Skip to content

Instantly share code, notes, and snippets.

View alexsasharegan's full-sized avatar

Alex Regan alexsasharegan

View GitHub Profile
@alexsasharegan
alexsasharegan / async-script-loader.js
Last active May 3, 2017 19:21
Load a single script or an array of scripts on a page with a node-style callback onLoad or onError.
/**
* Dynamically and asynchronously load a script on the page.
* Receives a callback for onLoad or onError
*
* @param {string} scriptSrc
* @param {loadScriptCallback} callback
* @param [context=null]
*/
function loadScript(scriptSrc, callback, context) {
if (typeof callback !== 'function') {
@alexsasharegan
alexsasharegan / decodeQueryStringToObj.js
Last active April 23, 2017 02:19
Glorious usage of ES2015 to parse uri-encoded strings to plain javascript objects.
function decodeQueryStringToObj( qStr ) {
return Object.assign.apply( null, qStr.split( '&' ).map( pair => {
const [ key, val ] = pair.split( '=' ).map( decodeURIComponent )
return { [key]: val }
} ) );
}
@alexsasharegan
alexsasharegan / compose.js
Created April 23, 2017 00:40
Simple functional composition with es2015
function compose( fn, ...fns ) {
return ( ...args ) => fns.reduce( ( carry, fn ) => fn.call( this, carry ), fn.apply( this, args ) )
}
@alexsasharegan
alexsasharegan / math-rounding.js
Created May 3, 2017 21:30
Helpers for rounding numbers and formatting numeric strings in javascript.
/**
* @param number
* @param roundingFactor
* @return {number}
*/
function roundBy(number, roundingFactor = 10) {
return Math.round(+number * +roundingFactor) / +roundingFactor
}
/**
* Round any number to a given decimal precision. Defaults to 1 point precision.
@alexsasharegan
alexsasharegan / RichModel.js
Last active May 5, 2017 22:06
A plain javascript object made useful only by methods.
import pick from 'lodash-es/pick'
import pickBy from 'lodash-es/pickBy'
import omit from 'lodash-es/omit'
import omitBy from 'lodash-es/omitBy'
import clone from 'lodash-es/clone'
import cloneDeep from 'lodash-es/cloneDeep'
import isArray from 'lodash-es/isArray'
import isEmpty from 'lodash-es/isEmpty'
export default class RichModel {
@alexsasharegan
alexsasharegan / self-signed-cert.sh
Created May 4, 2017 18:21
Self sign an ssl certificate for development on localhost with Chrome >= 58
#!/usr/bin/env bash
openssl req \
-newkey rsa:2048 \
-x509 \
-nodes \
-keyout ~/.localhost-ssl/<cert-name>.key \
-new \
-out ~/.localhost-ssl/<cert-name>.crt \
-subj /CN=<server-name> \
@alexsasharegan
alexsasharegan / webpack.config.js
Created May 24, 2017 14:51
Webpack config for vue.js with code splitting and css extracted.
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const extractStyles = new ExtractTextPlugin({
filename: "css/[name].css"
})
@alexsasharegan
alexsasharegan / makeAsyncFn.js
Created July 18, 2017 03:49
Higher Order Function for creating asynchronous versions of functions using setTimeout.
export function makeAsyncFn(fn, ctx = null) {
return function asyncFunction() {
const oArgs = arguments
if (typeof fn === "function") {
window.setTimeout(function timeoutFn() {
fn.apply(ctx, oArgs)
}, 0)
}
}
import { Store } from "vuex"
export default new Store({
state: {
models: [],
},
getters: {
// Caches model references into an object keyed by model id's
modelsById (state) {
const { assign, defineProperty, defineProperties } = Object;
function readonlyDescriptor() {
return { configurable: false, writable: false }
}
export class SyntheticEvent {
constructor(type, eventInit = {}) {
// Start by emulating native Event constructor.
if (!type) {