Skip to content

Instantly share code, notes, and snippets.

View Williammer's full-sized avatar

William He Williammer

View GitHub Profile
@Williammer
Williammer / jsDesignPattern.strategy.js
Created April 7, 2015 14:54
example of javaScript strategy pattern.
var validator = {
// all available checks
types: {},
// error messages in the current
// validation session
messages: [],
// current validation config
// name: validation type
@Williammer
Williammer / jsDesignPattern.decorator.js
Created April 6, 2015 10:34
implement decorator pattern with prototype inheritance and stack data type respectively.
// decorator pattern with prototypal inheritance.
function Sale(price) {
this.price = price || 100;
}
Sale.prototype.getPrice = function() {
return this.price;
};
Sale.decorators = {};
@Williammer
Williammer / jsDesignPattern.factory.js
Created April 3, 2015 02:06
implement a factory pattern with prototype and constructor.
function FactorySample() {}
FactorySample.prototype.action = function() {
return "the id of this instance is " + this.id;
};
FactorySample.factory = function(type) {
var newInstance;
// check whether constructor type exist
@Williammer
Williammer / jsDesignPattern.singleton.js
Last active August 29, 2015 14:18
implement a singleton klass with redefine constructor pattern and IIFE.
// Constructor way
function SingletonKlass() {
var instance;
SingletonKlass = function SingletonKlass() {
return instance;
}
SingletonKlass.prototype = this;
instance = new SingletonKlass();
def tstamp = new Date().format('yyyy-MM-dd_HH-mm-ss')
def buildLogDir = "${rootDir}/build/logs"
mkdir("${buildLogDir}")
def buildLog = new File("${buildLogDir}/${tstamp}_buildLog.log")
import org.gradle.logging.internal.*
System.setProperty('org.gradle.color.error', 'RED')
gradle.services.get(LoggingOutputInternal).addStandardOutputListener (new StandardOutputListener () {
void onOutput(CharSequence output) {
@Williammer
Williammer / jsPattern.PubSubImpl.js
Last active July 8, 2018 09:09
jsPattern.PubSubImpl - one compact library-agnostic implementation by addyosmani. Purely for learning purpose.
/*!
* Pub/Sub implementation
* http://addyosmani.com/
* Licensed under the GPL
* http://jsfiddle.net/LxPrq/
*/
;(function ( window, doc, undef ) {
private void enableHTML5AppCache() {
webView.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
@Williammer
Williammer / jsPerform.StrTrim.js
Last active August 29, 2015 14:03
jsPerform.StrTrim.js - the efficient methods to trim string with and without regular expression.
// trim with regular expression
String.prototype.trim = function () {
return this.replace(/^\s*([\s\S]*\S)?\s*$/, "$1");
};
// trim with string methods
String.prototype.trim = function () {
var start = 0,
end = this.length - 1,
ws = " \n\r\t\f\x0b\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u202f\u205f\u3000\ufeff";
@Williammer
Williammer / jsPerform.MemorizeFactorial.js
Created July 9, 2014 08:52
jsPerform.MemorizeFactorial.js - customized memorize factorial function, with great performance.
//customized mem function for factorial, with very good performance.
function memfactorial(n) {
if (!memfactorial.cache) {
memfactorial.cache = {
"0": 1,
"1": 1
};
}
if (!memfactorial.cache.hasOwnProperty(n)) {
memfactorial.cache[n] = n * memfactorial(n - 1);
@Williammer
Williammer / algorithm.1.bubblesort.js
Last active June 25, 2016 03:16
sort algorithms: bubbleSort: bubble swap each adjacent item pair one step for each loop -> until length N-1 loop for worst case. ||| selectionSort: find min item from first pos to the last. 外层为pos遍历,内层遍历pos后的元素,找到最小的与pos当前元素交换。||| insertSort: 最外层n遍历,内层与之前所有元素比较,并和较大的交换。 ||| shellSort: A better way of insertSort, 最外层遍历所有gaps,次外层以gap遍历数组并比较交换各组元素。…
function bubblesort() {
var numElements = this.dataStore.length;
var temp;
for (var outer = numElements; outer >= 2; --outer) {
for (var inner = 0; inner <= outer - 1; ++inner) {
if (this.dataStore[inner] > this.dataStore[inner + 1]) {
temp = this.dataStore[inner];
this.dataStore[inner] = this.dataStore[inner + 1];
this.dataStore[inner + 1] = temp;
}