Skip to content

Instantly share code, notes, and snippets.

@TGOlson
TGOlson / map_and_resolve_all.js
Created January 30, 2015 06:38
Map and resolve all
var Q = require('q'),
_ = require('lodash');
var mapAndResolveAll = _.compose(Q.all, _.map);
function loadWithPattern(pattern) {
return function(entity) {
return loadEntity(entity, pattern);
};
}
@TGOlson
TGOlson / async_helpers.js
Last active August 29, 2015 14:14
Functional async helper strategies
var Q = require('q'),
_ = require('lodash');
/*
* Functional Helpers
*/
/**
* Compose two functions into one
* @param {Function} a
// 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);
@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) {
@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 / 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) {
var _ = require('lodash'),
argv = require('yargs').argv,
input = _.first(argv._);
// everything else will default to 1
var defaults = {
ideaCount: 4,
questionCount: 2
};
@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();
@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 / 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