Skip to content

Instantly share code, notes, and snippets.

View laispace's full-sized avatar

赖小赖 laispace

  • AntFin
  • Shenzhen
View GitHub Profile
void function() {
function format(template, json) {
return template.replace(/#\{(.*?)\}/g, function(all, key) {
return json && (key in json) ? json[key] : "";
});
}
document.getElementById('panel').innerHTML = format(
String(function(){/*!
<ul>
@laispace
laispace / borningWebsites.js
Created September 23, 2014 01:20
borning-websites.js
var results = ["http://heeeeeeeey.com/",
"http://cant-not-tweet-this.com/",
"http://eelslap.com/",
"http://www.staggeringbeauty.com/",
"http://www.omfgdogs.com/",
"http://burymewithmymoney.com/",
"http://www.fallingfalling.com/",
"http://www.a-blue-box.com/",
"http://ducksarethebest.com/",
"http://www.trypap.com/",
@laispace
laispace / requestAnimFrame.js
Created December 31, 2014 07:22
create requestAnimFrame pollfill
window.requestAnimFrame = function(){
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback){
window.setTimeout(callback, 1000 / 60);
}
@laispace
laispace / inViewport.js
Created December 31, 2014 07:23
check if element is in viewport
var docElem = window.document.documentElement;
function getViewportH() {
var client = docElem['clientHeight'],
inner = window['innerHeight'];
if( client < inner )
return inner;
else
return client;
}
@laispace
laispace / removeFolder.js
Created January 5, 2015 01:58
removeFolder.js
deleteFolderRecursive = function(path) {
var files = [];
if( fs.existsSync(path) ) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
@laispace
laispace / scrolledIntoView.js
Created January 15, 2015 02:05
whether an element is in view
var scrolledIntoView = function(element) {
var coords = element.getBoundingClientRect();
return ((coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight));
};
@laispace
laispace / pattern-singleton.js
Created February 7, 2015 09:44
设计模式-单例模式
var singleton = (function () {
function Singleton(opts) {
this.createdAt = opts.createdAt || new Date();
}
var _instance;
var static = {
name: 'singleton',
getInstance: function (opts) {
@laispace
laispace / pattern-pubsub.js
Last active August 29, 2015 14:14
设计模式-发布订阅模式
// pattern-pubsub.js
var pubsub = {};
(function (ps) {
var topics = {}; // store topics
var subId = -1; // identify subcribers
ps.publish = function (topic, args) {
var subcribers = topics[topic];
@laispace
laispace / pattern-prototype.js
Last active August 29, 2015 14:14
设计模式-原型模式
// pattern-prototype.js
// ===== 1. use Object.create =====
var person = {
nation: 'china',
say: function (something) {
console.log(this.name, 'says: ', something);
}
};
@laispace
laispace / pattern-factory.js
Last active August 29, 2015 14:15
设计模式-工厂模式
// ===== 1.pattern-factory.js=====
// Car factory
function Car (options) {
this.type = options.type || 'car';
this.owner = options.owner || null;
this.price = options.price || null;
}
// Trunk factory