Skip to content

Instantly share code, notes, and snippets.

View joshblack's full-sized avatar

Josh Black joshblack

View GitHub Profile
@joshblack
joshblack / className.js
Created May 12, 2015 19:30
Class name problem
/**
* Write a function that generates a unique class name every time
* that it's called. The class name should follow the following
* pattern:
*
* fn(); // returns 'a'
* fn(); // returns 'b'
* ...
* fn(); // returns 'z'
* fn(); // returns 'aa'
@joshblack
joshblack / randomList.js
Last active August 29, 2015 14:19
Write a function that returns a list of size n, containing random, distinct numbers using the functions given below.
/**
* Write a function that returns a list of size n,
* containing random, distinct numbers using the
* function given below.
*/
/**
* Generates a random number from 1 -> [ceil]
*
* @param {Number} ceil
@joshblack
joshblack / decorators.js
Created April 10, 2015 16:14
ES7 Decorators
class Foo {
@log
bar(a, b) {
console.log(`Adding ${a} and ${b} gives us ${a + b}`);
}
}
function log(target, name, descriptor) {
let fn = descriptor.value;
@joshblack
joshblack / Makefile
Created March 11, 2015 20:01
Useful Makefile commands for Front-End Projects
# Don't forget that Makefiles need tabs not spaces!
BIN = "./node_modules/.bin"
patch:
$(call release,patch)
minor:
$(call release,minor)
@joshblack
joshblack / Parser.js
Created March 1, 2015 03:20
Fun Parsing challenge for JavaScript Style Sheets
'use strict';
import Parser from './parser';
import styles from './styles';
Parser(styles);
// Output:
{
h1: {
@joshblack
joshblack / path-finder.js
Last active August 29, 2015 14:15
List all the file paths!
// Given a certain object that represents a file system, create a function that
// returns the path to every file in the system.
// Assumptions:
// * All keys are valid paths with a trailing slash.
// * Values of keys can either be objects that represent folders, or they can be
// an array of files.
// * Every path terminates in an array containing a single file or multiple files.
var path = {
'/': {
@joshblack
joshblack / flatten.js
Created February 10, 2015 18:38
Grab all the data nodes of an Object
var assign = require('object-assign');
// Get all the data nodes of an object
function flatten(obj) {
return Object.keys(obj).reduce(function (prev, key) {
return isObject(obj[key])
? assign(prev, flatten(obj[key]))
: (prev[key] = obj[key], prev);
var http = require('http'),
express = require('express'),
app = express(),
cookieParser = require('cookie-parser'),
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
var proxyOptions = {
'host': 'http://127.0.0.1',
@joshblack
joshblack / node-orm.js
Created January 26, 2015 04:19
Possible ORM API for Node.js / io.js based off of Laravel's Eloquent API
"use strict";
const Schema = require('schema');
const Migration = require('migration');
// Migration builder
let CreateUsersTable = Object.create(Migration);
CreateUsersTable.prototype.up = function up() {
el.addEventListener('click', function (e) {
e.target.style.display = 'none';
});
// Instead of using jQuery's $('selector') for grabbing an element, just use these
var elem = document.querySelector('selector'); // Returns a DOM Node
var elems = document.querySelectorAll('selector'); // Returns a nodelist
// Then for event listeners it's just:
elem.addEventListener('click', function (e) {