Skip to content

Instantly share code, notes, and snippets.

@TGOlson
TGOlson / refactor.js
Created February 28, 2014 06:23
Refactor your JS
// For a project I needed to find the nearest denominator from a set number and divisor.
// I started off hacking it together to see if it would work in my project.
// Then I refactored. Refactored code is shown first.
// Refactored Code
function findNearestInt(num, div){
for(var i = 0; i < div / 2; i++){
if(num % (div - i) == 0){return div - i}
if(num % (div + i) == 0){return div + i}
@TGOlson
TGOlson / module.js
Last active August 29, 2015 13:56
Module pattern and saving the global state.
// Two ways to use the module pattern to save state.
// One with an explicit callback function.
(function ( global, callback ) {
global.RunMe = callback( global )
})( this, callback )
@TGOlson
TGOlson / double_join.sql
Last active August 29, 2015 13:56
SQL Query vs. Activerecord
-- A little practice exploring a relatively complex SQL query,
-- and comparing it to the ActiveRecord equivelent.
-- This is close to something I implemented in a real Rails project.
-- This gets the first 10 applications in the database that have 'Games' as their primary genre.
SELECT app.title AS 'Application Title', g.name as 'Primary Genre'
FROM application AS app
JOIN genre_application AS g_app
ON app.application_id = g_app.application_id
@TGOlson
TGOlson / alert.html
Last active August 29, 2015 13:57
Strategy for creating Bootstrap styled flash messages in an AngularJS app.
<!-- Alerts are easily shown using the below syntax.
This HTML snippet can be used to display all Bootstrap alert styles. -->
<div ng-show='alert' class="alert alert-{{ alert.type }}">
<p>{{ alert.message }}</p>
</div>
<!-- I decided to go the fading alert route
but you could esily make the alerts dismissable
@TGOlson
TGOlson / mongo-promises.js
Created April 18, 2014 22:37
Testing using Q with the built in Mongo promises.
var mongoose = require('mongoose');
var Entity = require('./server/models/entity')
var Q = require('q');
var db = mongoose.connect('mongodb://localhost/lp-test');
test1();
var _ = require('lodash'),
argv = require('yargs').argv,
input = _.first(argv._);
// everything else will default to 1
var defaults = {
ideaCount: 4,
questionCount: 2
};
@TGOlson
TGOlson / flatten.js
Last active August 29, 2015 14:06
Flatten JavaScript Objects
// delimter to join flattened keys
var delimiter = ' '
/*
* Flattens an object
* @param {object} object - object to flatten
* @param {boolean} shallow - if true object is only flattened one level
* @return {object} - flattened object
*/
function flatten(object, shallow) {
@TGOlson
TGOlson / constant.js
Last active August 29, 2015 14:07
Simple JavaScript Constants
/*
* JavaScript Constant
*
* Create constants via Api
* Retreive values from window/global without Api
*
* This will work in both browsers and node
*
* Enforces standard rules:
* Requires uppercase naming conventions
@TGOlson
TGOlson / parser.js
Created October 28, 2014 22:24
Field Parser
var Parser = {};
Parser.parseFields = function(fields) {
var parser = new LoadTierParser(),
tokenizer = new Tokenizer(fields);
return parser.parseTier(tokenizer, false);
};
function LoadTierParser(parent) {
// place in root launchpad folder
var entityDao = require('./server/services/entity-dao'),
config = require('./server/config/config'),
systemService = require('./server/services/mj-entity-service/mj-entity-service');
entityDao.connect(config.db)
.then(actions)
.fail(onError);