Skip to content

Instantly share code, notes, and snippets.

Elliot Chong ElliotChong

Block or report user

Report or block ElliotChong

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View fighting-Insanity.md

================

If your language works like this by default..

var x = {a: 5};
var y = {a: 5};
x === y; // => false
@ElliotChong
ElliotChong / Highlander.js
Last active Aug 29, 2015
When you're the only script that should work on a page.
View Highlander.js
var HighlanderJS = (function (immortals) {
var highlander = {};
for (var immortal in immortals) {
highlander[immortal] = immortals[immortal];
delete immortals[immortal];
}
immortals.HighlanderJS = function (onlyOne) {
onlyOne.apply(highlander);
};
@ElliotChong
ElliotChong / benchmark.coffee
Created Aug 19, 2014
Super quick and very dirty memory benchmark of Immutable JS.
View benchmark.coffee
heapdump = require "heapdump"
Immutable = require "immutable"
pixels = 256 * 256
iterations = 1000
# Immutable
testImmutable = ->
# Simulate a pixel grid
grid = Immutable.Vector()
@ElliotChong
ElliotChong / OUTPUT.md
Last active Dec 29, 2015
HTML sanitization script that ignores brackets within <script> tags. Tested in V8- Will need the forEach methods swapped out to work in legacy JS runtimes.
View OUTPUT.md
✓	 Bar

✓	 A &lt;p&gt;Baz!&lt;/p&gt; B

✓	 &lt;p&gt;Foo&lt;/p&gt; &lt;script&gt; a < b; &lt;/script&gt; &lt;span&gt;Blam!&lt;/span&gt;

✓	 &lt;p&gt;Foo&lt;/p&gt; &lt;script type='text/javascript'&gt; a < b || c > d; &lt;/script&gt; &lt;span&gt;Blam!&lt;/span&gt;

✓	 D &lt;p&gt;Foo&lt;/p&gt; &lt;script type='text/javascript'&gt;$('body').append('<' + 'script> a < b; <' + '/script>');&lt;/script&gt; &lt;span&gt;Blam!&lt;/span&gt; F &lt;b&gt;O&lt;/b&gt;
@ElliotChong
ElliotChong / Gruntfile.coffee
Created Nov 14, 2013
Example Gruntfile showing concurrent CoffeeScript > JS > minification, SASS > CSS > minification, livereload, and nodemon / nodeinspector tandem.
View Gruntfile.coffee
module.exports = (grunt) ->
config =
browserify:
development:
options:
transform: ["coffeeify"]
debug: true
files:
"./public/scripts/editor.js": ["./frontend/scripts/editor.coffee"]
uglify:
@ElliotChong
ElliotChong / KineticJS-PixelRatio_Override.coffee
Last active Dec 20, 2015
KineticJS automatically detects the current device's pixel ratio and renders graphics accordingly so that they will be as crisp as possible on high DPI displays. Sometimes you'd like to opt for a less precise (fuzzier) rendering but higher average framerate, this gist will give you that control.
View KineticJS-PixelRatio_Override.coffee
# Adjust device pixel ratio
setMaximumPixelRatio = (p_maximumRatio=1) ->
canvas = document.createElement('canvas')
context = canvas.getContext('2d')
devicePixelRatio = window.devicePixelRatio || 1
backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1
pixelRatio = devicePixelRatio / backingStoreRatio
for className in ["HitCanvas", "SceneCanvas", "Canvas"]
Kinetic[className].prototype.init = ((p_method) -> (p_config={}) ->
@ElliotChong
ElliotChong / pixi-graphics-additions.coffee
Last active Dec 20, 2015
Adds drawArc, drawRoundRect, and drawSlice methods to Pixi.js's Graphics object.
View pixi-graphics-additions.coffee
toRad = (p_angle) ->
return (p_angle - 90) * Math.PI / 180
for method in ["drawArc", "drawRoundRect", "drawSlice"]
if PIXI.Graphics.prototype[method]? then console.warn "PixiJS already has an #{method} method defined. It's recommended that you update your code to use the official implementation."
PIXI.Graphics.prototype.drawArc = (p_x, p_y, p_radius, p_startAngle, p_endAngle) ->
totalAngle = p_endAngle - p_startAngle
segments = Math.ceil Math.abs(Math.sqrt(1 - Math.pow (1 - Math.min(p_radius / 60, 1)), 2) * totalAngle * p_radius * 0.01)
anglePerSegment = totalAngle / segments
@ElliotChong
ElliotChong / outbound-link-tracking.html
Last active Dec 15, 2015
Adding Outbound Link Tracking With Google Analytics
View outbound-link-tracking.html
@ElliotChong
ElliotChong / underscore.mixin.deepExtend.coffee
Last active Oct 11, 2015
Copy all of the properties in the source objects over to the destination object, and return the destination object. This method will recursively copy mutual properties which are also objects.
View underscore.mixin.deepExtend.coffee
# Create a deep copy of an object. - CoffeeScript conversion of @cederberg's deepClone implementation https://github.com/documentcloud/underscore/pull/595
deepClone = (obj) ->
if !_.isObject(obj) or _.isFunction(obj) then return obj
if _.isDate obj then return new Date do obj.getTime
if _.isRegExp obj then return new RegExp obj.source, obj.toString().replace(/.*\//, "")
isArr = _.isArray obj or _.isArguments obj
func = (memo, value, key) ->
if isArr then memo.push deepClone value
@ElliotChong
ElliotChong / facebookPluginDirectives.coffee
Created Oct 9, 2012
Angular directives that will parse Facebook plugins added to the DOM after Facebook has been initialized
View facebookPluginDirectives.coffee
module = angular.module 'FacebookPluginDirectives', []
createDirective = (name) ->
module.directive name, ->
restrict: 'C'
link: (scope, element, attributes) ->
FB?.XFBML.parse(element.parent()[0])
createDirective pluginName for pluginName in ['fbActivity', 'fbComments', 'fbFacepile', 'fbLike', 'fbLikeBox', 'fbLiveStream', 'fbLoginButton', 'fbName', 'fbProfilePic', 'fbRecommendations']
You can’t perform that action at this time.