Skip to content

Instantly share code, notes, and snippets.

@Nutrox
Nutrox / gist:803735
Created January 31, 2011 07:24
JavaScript - Private/Static Property Example
function Class() {
}
Class.prototype = new function() {
var value = 100;
function getValue() {
return value++;
}
this.getValue = getValue;
@Nutrox
Nutrox / gist:796986
Created January 26, 2011 16:49
JavaScript - toConstantCase()
function toConstantCase() {
var a = arguments;
if( a.length == 1 ) {
return a[0].replace( toConstantCase.REGEX, toConstantCase ).toUpperCase();
}
return a[0][0] + "_" + a[0][1] + ( a[0][2] ? a[0][2] : "" );
}
toConstantCase.REGEX = /[a-z][A-Z]|[A-Z]{2}[a-z]/g;
//
@Nutrox
Nutrox / gist:796719
Created January 26, 2011 14:02
AS3 - JavaScript Style Closures
var window:Object = {};
new function():void {
var shared:Number = 100;
/**
*/
function Foo():void {
this.getNumber = function():Number {
@Nutrox
Nutrox / gist:791176
Created January 22, 2011 15:22
JavaScript - DOM Ready
//
// Works in Chrome, Firefox, MSIE, Opera and Safari.
//
function onDOMContentLoaded( event ) {
if( event ) {
document.removeEventListener( event.type, onDOMContentLoaded, false );
}
document.onreadystatechange = null;
// do something here...
}
@Nutrox
Nutrox / gist:789900
Created January 21, 2011 16:14
JavaScript - String.trim()
new function() {
var TRIM = /^\s*(.+?)\s*$/;
String.prototype.trim = function() {
return this.replace( TRIM, '$1' );
}
}
@Nutrox
Nutrox / gist:780784
Created January 15, 2011 08:36
PHP - JSONObject class.
class JSONObject extends stdClass {
// Accepts an array, object, or JSON encoded string.
public function __construct( $o=null ) {
if( $o === null ) {
return;
}
if( is_string( $o ) ) {
$o = json_decode( $o );
}
@Nutrox
Nutrox / gist:779208
Created January 14, 2011 05:18
AS3 - Random capital letter.
const CHR:Array = [
"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
];
const CHRLEN:Number = CHR.length;
function getRandom():String {
return CHR[ CHRLEN * Math.random() >> 0 ];
}
@Nutrox
Nutrox / gist:778619
Created January 13, 2011 21:14
AS3 - Isometric matrix transformation.
const DEGRAD:Number = Math.PI / 180;
var iso:Matrix = new Matrix();
iso.rotate( -45 * DEGRAD ); // rotate anti-clockwise by 45 degrees
iso.scale( 1.0, 0.5 ); // scale vertically by 50%
iso.translate( 100, 100 ); // set position if needed
displayObject.transform.matrix = iso;
@Nutrox
Nutrox / StringUtil.replace.as
Created January 13, 2011 18:56
AS3 - String replacement utility function.
function replace( str:String, search:String, replacement:String ):String {
var a:int = search.length;
var b:int = replacement.length;
var o:int = 0;
var i:int;
while( ( i = str.indexOf( search, o ) ) != -1 ) {
str = str.substr( 0, i ) + replacement + str.substr( i + a );
o = i + b;
}
return str;
@Nutrox
Nutrox / count_bytes.php
Created January 5, 2011 00:25
PHP - Counts the number of bytes in a string, regardless of any mbstring overloading.
// Counts the number of bytes in a string.
function count_bytes( &$str ) {
if( ini_get( 'mbstring.func_overload' ) ) {
return mb_strlen( $str, '8bit' );
}
// Ideally we want strlen() to be used, because it's fast.
return strlen( $str );
}