Skip to content

Instantly share code, notes, and snippets.

View ElliotChong's full-sized avatar
✌️

Elliot Chong ElliotChong

✌️
View GitHub Profile

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

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 August 29, 2015 14:14
When you're the only script that should work on a page.
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 August 19, 2014 20:00
Super quick and very dirty memory benchmark of Immutable JS.
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 December 29, 2015 14:49
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.
✓	 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('&lt;' + 'script&gt; a &lt; b; &lt;' + '/script&gt;');&lt;/script&gt; &lt;span&gt;Blam!&lt;/span&gt; F &lt;b&gt;O&lt;/b&gt;
@ElliotChong
ElliotChong / Gruntfile.coffee
Created November 14, 2013 17:20
Example Gruntfile showing concurrent CoffeeScript > JS > minification, SASS > CSS > minification, livereload, and nodemon / nodeinspector tandem.
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 December 20, 2015 09:29
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.
# 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 December 20, 2015 02:59
Adds drawArc, drawRoundRect, and drawSlice methods to Pixi.js's Graphics object.
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 December 15, 2015 18:58
Adding Outbound Link Tracking With Google Analytics
@ElliotChong
ElliotChong / underscore.mixin.deepExtend.coffee
Last active November 28, 2020 23:55
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.
# 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 October 9, 2012 05:45
Angular directives that will parse Facebook plugins added to the DOM after Facebook has been initialized
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']