Skip to content

Instantly share code, notes, and snippets.

View geraldyeo's full-sized avatar
🎯
Focusing

Gerald Yeo geraldyeo

🎯
Focusing
View GitHub Profile
@geraldyeo
geraldyeo / self-preloader.as
Created August 18, 2011 06:24
Self preloading indication snippet
stop();
loaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loaderInfo.addEventListener(Event.COMPLETE, loadComplete);
function loadProgress( evt:ProgressEvent ):void {
var ratio:Number = loaderInfo.bytesLoaded/loaderInfo.bytesTotal;
var percent:int = int( ratio*100 );
trace( percent + "%" );
}
/*
It's a very, very, bad idea to attach handlers to the window scroll event. Depending upon the browser the scroll event can fire a lot and putting code in the scroll callback will slow down any attempts to scroll the page (not a good idea). Any performance degradation in the scroll handler(s) as a result will only compound the performance of scrolling overall. Instead it's much better to use some form of a timer to check every X milliseconds OR to attach a scroll event and only run your code after a delay (or even after a given number of executions - and then a delay).
Always cache the selector queries that you're re-using. It's not clear why Twitter decided to re-query the DOM every single time the scroll event fired, but this does not appear to be necessary (since scrolling itself didn't change the DOM). They could've saved the result of that single query to a variable and looked it up whenever they wanted to re-use. This would've resulted in absolutely no querying overhead (which is even better than hav
@geraldyeo
geraldyeo / fizzbuzz.js
Created November 30, 2012 03:30
Fizz-Buzz
/*
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number
and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
*/
var fizzbuzz = function(){
var x = 0, print;
while (++x<=100) {
print = '';
@geraldyeo
geraldyeo / purge-from-git
Created February 14, 2013 07:04
Purge file/dir from repo
[file]
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <filename>' --prune-empty -- --all
[dir]
git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch <dir-path>/' --prune-empty -- --all
// UIImage+Alpha.h
// Created by Trevor Harmon on 9/20/09.
// Free for personal or commercial use, with or without modification.
// No warranty is expressed or implied.
// Helper methods for adding an alpha layer to an image
@interface UIImage (Alpha)
- (BOOL)hasAlpha;
- (UIImage *)imageWithAlpha;
- (UIImage *)transparentBorderImage:(NSUInteger)borderSize;
@geraldyeo
geraldyeo / promisify
Created September 29, 2013 23:26
<http://howtonode.org/promises> The majority of functions that take Node-style callbacks are suitable for wrapping in a Promise. Any Node-style callback function that only calls its callback one time may be wrapped. The following function is taken from Bogart.
function promisify(nodeAsyncFn, context) {
return function() {
var defer = q.defer()
, args = Array.prototype.slice.call(arguments);
args.push(function(err, val) {
if (err !== null) {
return defer.reject(err);
}
@geraldyeo
geraldyeo / rewrite
Created October 3, 2013 05:28
Rules for AngularJS HTML5 Routing
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api
@geraldyeo
geraldyeo / 0_reuse_code.js
Created December 9, 2013 09:16
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
<!doctype html>
<!--
Minimal JS, crossbrowser, styled input[type="file"] with simple markup.
Tested working in IE[>=7], FF[24], O[16], C[30], S[6.0.5]; Should work in
all browsers that support opening a file browser via a label associated
to an input[type="file"]. Some adjustments might be necessary for IE[6]
support.
Tested using html5 doctype. Further testing is necessary to ensure proper
@geraldyeo
geraldyeo / ssh
Last active August 29, 2015 14:00
Append keys to ssh
cat ~/.ssh/id_rsa.pub | ssh user@remote-system 'umask 077; cat >>.ssh/authorized_keys'