Skip to content

Instantly share code, notes, and snippets.

View coodoo's full-sized avatar

Jeremy Lu coodoo

View GitHub Profile
var input = "@ch: THIS IS A BOOK @c:professional-quality display and classified ads using a web interface. You can control the quality of these ads — and accept them later in the production cycle — while still giving your customers a wide range of creative options. At the same time, the process provides an opportunity to cross-sell and upsell your customers to higher value placements by showing them additional options during the creation process. You can even add new categories of advertisements without expending additional resources.";
var r = /(@.*?:)/g;
var k = r.exec( input );
@coodoo
coodoo / webdev.md
Created November 29, 2012 16:37 — forked from dideler/bootstrapping.md
Bootstrapping
@coodoo
coodoo / Backbone-Collection-View
Created December 1, 2012 08:15
Backbone Collection View
/**
* Backbone Collection View
*
* A light-weight approach to display filetered data from collection by assigning rules, without changing original models,
* which works just like CollectionView in other languages (ie: Java, WPF and AS3).
*
* Usage:
*
* 1. Filter by single field:
* this.wineList.filterView( {Country:'France'}} );
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-requirejs');
grunt.initConfig({
requirejs: {
std: {
options: {
almond: true,
baseUrl: "lib",
@coodoo
coodoo / b
Last active December 10, 2015 16:08
What if we can assign an action (callback) for each of the 'eat' event ? this way switch/case would be eliminated.
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'eat', from: 'hungry', to: 'satisfied', action: 'foo' },
{ name: 'eat', from: 'satisfied', to: 'full', action: 'bar' },
{ name: 'eat', from: 'full', to: 'sick', action: 'coo' },
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry', action: 'doo' },
]});
@coodoo
coodoo / State_Machine.js
Created January 5, 2013 04:32
Implemented in a way that resembles SMC (http://smc.sourceforge.net/), primarily adding two callbacks a. action() b. guard() Changes I made are marked with "jx" in the comment.
(function (window) {
var StateMachine = {
//---------------------------------------------------------------------------
VERSION: "2.2.0",
//---------------------------------------------------------------------------
@coodoo
coodoo / randomColor.js
Created March 2, 2013 04:34
get random color in hex or rgba format.
/**
* @param useRGB Boolean, true for rgba format, false will return color in hex format
* @param randomAlpha Boolean, when using rgba format, decide whether to randomnize alpha value too
*/
function getRandomColor( useRGB, randomAlpha ){
if( useRGB ){
return "rgba("+
Math.floor(Math.random()*256)+","+
Math.floor(Math.random()*256)+","+
Math.floor(Math.random()*256)+","+
@coodoo
coodoo / gist:5251585
Created March 27, 2013 04:13
Weird behavior in chrome's dev tool console
I tried to pause the script, then enter following command in the console:
document.getElementById('foo')
The weird part is, sometimes it will show the object state, but some times it will just show the <div> tag, seems it's the result of toString() ?
Is there a way to always get object state in the console ?
Screen cap here: http://cl.ly/image/0V1a1p3t0e12
@coodoo
coodoo / starling.js
Created May 6, 2013 02:25
Starling.js source code, slightly annotated. Starling.js is a canvas based drawing framework, ported from iOS to AS3 to Javascript. Demo: http://gamua.com/area-51/
/*
RequireJS 2.1.4 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
Available via the MIT or new BSD license.
see: http://github.com/jrburke/requirejs for details
*/
/*! jQuery v1.9.1 | (c) 2005, 2012 jQuery Foundation, Inc. | jquery.org/license
//@ sourceMappingURL=jquery.min.map
*/
@coodoo
coodoo / getQueryString.js
Created May 25, 2013 08:06
input: "$filter=&$top=3&$skip=3&$orderby=firstName&$format=json&$inlinecount=allpages output: Object {$filter: "", $top: "3", $skip: "3", $orderby: "firstName", $format: "json"…} $callback: "callback" $filter: "" $format: "json" $inlinecount: "allpages" optional: enter key name as 2nd parameter to just get that value
$.extend({
getQueryString: function ( urlString, key) {
function parseParams() {
var params = {},
e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = urlString;