Skip to content

Instantly share code, notes, and snippets.

xperiments / Dictionary.ts
Created Jun 25, 2013
Typescript Dictionary
View Dictionary.ts
interface IDictionary {
add(key: string, value: any): void;
remove(key: string): void;
containsKey(key: string): bool;
keys(): string[];
values(): any[];
xperiments / new_gist_file
Last active Feb 13, 2018
[[ts] Check if obj is instanceof U] Check if obj is instanceof U
View new_gist_file
// =>
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();
xperiments / ArrayBuffer2Base64.js
Last active Feb 13, 2018
[[js][utils][base64] ArrayBuffer2Base64] ArrayBuffer2Base64 #js #base64
View ArrayBuffer2Base64.js
// Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then
// use window.btoa' step. According to my tests, this appears to be a faster approach:
function base64ArrayBuffer(arrayBuffer) {
var base64 = ''
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
var bytes = new Uint8Array(arrayBuffer)
var byteLength = bytes.byteLength
xperiments / interlval.js
Last active Feb 13, 2018
[[js][hacks]] Better setInterval #js #hacks #tips
View interlval.js
function interval(func, wait, times){
var interv = function(w, t){
return function(){
if(typeof t === "undefined" || t-- > 0){
setTimeout(interv, w);
xperiments / OneTimeDirective.ts
Created Feb 13, 2018
[[ng][directives]] OneTimeDirective #ng #directive
View OneTimeDirective.ts
selector: '[oneTime]',
export class OneTimeDirective {
constructor(template: TemplateRef<any>, container: ViewContainerRef, zone: NgZone) {
zone.runOutsideAngular(() => {
const view = container.createEmbeddedView(template);
setTimeout(() => view.detach());
xperiments / Singleton.ts
Created Jun 25, 2013
Typescript Singleton
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 base64-binary.js
Copyright (c) 2011, Daniel Guerrero
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
xperiments / xhr2base64
Created Jul 10, 2013
XHR => base64 => Binary
View xhr2base64
View xhr2base64.js
function getBinary(file){
var xhr = new XMLHttpRequest();"GET", file, false);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
return xhr.responseText;
function base64Encode(str) {
var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
View OddNumber.js
The "trick" is to binary AND a value with 1.
Any odd number must have the first bit set to 1.
var foo = 7;
if( foo & 1 ) { // true }
Using a bitwise AND has a better performance in almost all platforms / browsers.