View new_gist_file
// => http://stackoverflow.com/questions/17392349/how-can-i-check-if-element-is-an-instanceof-u | |
function OfType<T, U>(list: T[], arg: Function) : U[] | |
{ | |
var result: U[] = []; | |
list.forEach(e => { | |
// extract the name of the class | |
// used to match primitive types | |
var typeName = /function\s*([^(]*)/i.exec(arg+"")[1].toLocaleLowerCase(); |
View OddNumber.js
The "trick" is to binary AND a value with 1. | |
Any odd number must have the first bit set to 1. | |
So | |
var foo = 7; | |
if( foo & 1 ) { // true } | |
Using a bitwise AND has a better performance in almost all platforms / browsers. |
View BrowserDetection.js
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; | |
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera) | |
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+ | |
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; | |
// At least Safari 3+: "[object HTMLElementConstructor]" | |
var isChrome = !!window.chrome && !isOpera; // Chrome 1+ | |
var isIE = /*@cc_on!@*/false; // At least IE6 |
View Dictionary.ts
//http://stackoverflow.com/questions/15877362/declare-and-initialize-a-dictionary-in-typescript | |
interface IDictionary { | |
add(key: string, value: any): void; | |
remove(key: string): void; | |
containsKey(key: string): bool; | |
keys(): string[]; | |
values(): any[]; | |
} |
View Singleton.ts
module x | |
{ | |
export class Singleton | |
{ | |
private static _instance : Singleton; | |
constructor( singletonEnforcer:()=>void ) | |
{ | |
if( singletonEnforcer !== SingletonEnforcer ) | |
{ | |
throw new Error("Error: Instantiation failed: Use Singleton.getInstance() instead of new."); |
View NewableTypes.ts
http://stackoverflow.com/questions/13407036/how-does-typescript-interfaces-with-construct-signatures-work | |
Construct signatures in interfaces are not implementable in classes; they're only for defining existing JS APIs that define a 'new'-able function. Here's an example involving interfaces new signatures that does work: | |
interface ComesFromString { | |
name: string; | |
} | |
interface StringConstructable { | |
new(n: string): ComesFromString; |
NewerOlder