Skip to content

Instantly share code, notes, and snippets.

View CatTail's full-sized avatar

Chiyu Zhong CatTail

View GitHub Profile
@CatTail
CatTail / lazy_loading.js
Created July 16, 2013 15:54
Lazy loading function
/**
* <Professional Javascript for web developer v3 p736>
* Reduce determine condition in every function execution.
*/
// bad ass
function createXHR(){
if (typeof XMLHttpRequest != “undefined”){
return new XMLHttpRequest();
} else if (typeof ActiveXObject != “undefined”) {
@CatTail
CatTail / bash_color.sh
Created July 24, 2013 09:17
Shell script show the ability of display foreground and background color. From http://misc.flogisoft.com/bash/tip_colors_and_formatting
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
# Colors and formatting (16 colors)
#Background
for clbg in {40..47} {100..107} 49 ; do
#Foreground
@CatTail
CatTail / 256color.sh
Created July 29, 2013 04:17
Show numerical values for each of the 256 colors in bash
# from http://www.commandlinefu.com/commands/view/5879/show-numerical-values-for-each-of-the-256-colors-in-bash#comment
for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done
@CatTail
CatTail / csv2markdown.sh
Created August 6, 2013 15:20
A simple csv(comma seperated) to markdown translator.
#! /bin/bash
occur=$(head -n 1 $1 | grep -o "," | wc -l)
sep=" --- |"
seperator=""
for ((i=0; i<$occur; i++))
do
seperator+=$sep
done
lineno=0
file=''
@CatTail
CatTail / reduce-search.js
Created October 12, 2013 05:43
Search using Array.prototype.reduce method.
// refer to http://ariya.ofilabs.com/2013/10/searching-using-array-prototype-reduce.html?utm_source=javascriptweekly&utm_medium=email
// old story
function findLongest(entries) {
for (var i = 0, longest = ''; i < entries.length; ++i)
if (entries[i].length > longest.length) longest = entries[i];
return longest;
}
// A version which relies on reduce is a single statement:
function findLongest2(entries) {
@CatTail
CatTail / constructor-referer.coffee
Created October 23, 2013 07:09
Refer to class constructor in static or instance method.
# following class have a very long class name
# and we have a factory method to create instance of this class
# using following strategy to refer to constructor in static or instance method
class first.second.third.longlonglongClassName
constructor: ->
Ctor = @
@create: ->
new Ctor
@CatTail
CatTail / getWindows.js
Last active December 26, 2015 23:18
Using functional way to grab all windows in a window.
var map = Array.prototype.map.call
, reduce = Array.prototype.reduce.call
, filter = Array.prototype.filter.call
, concat = Array.prototype.concat.call;
/**
* Recursively get all windows of current window.
* @param {Window} win Current window.
* @param {Function} f Extra filter to throw away undesired frame.
* @param {boolean} recursion Whether invocation is in recursion.
* @returns {Array.<Window>}
@CatTail
CatTail / newFunc.js
Created December 13, 2013 07:55
Use a function to mock new operator
/**
* Mock new operator.
* Worked just as new operator.
* @param {Function} Type
* @param {...*} opt_args
* @return {*}
*/
var newFunc = function(Type, opt_args) {
var Ctor, proto;
Ctor = function(){};
@CatTail
CatTail / genschema.js
Last active August 29, 2015 13:57
Handy script to auto generate mongoose schema from existing data
var prefix = Math.random();
function parse(obj) {
var key, value, type, schema;
type = obj.constructor.name;
switch (type) {
case 'Object':
schema = {};
for (key in obj) {
value = parseKey(key, obj);
if (value !== undefined) schema[key] = value;
@CatTail
CatTail / reload-audio
Created May 12, 2014 03:15
OS X休眠唤醒后无声音
# 重启声音驱动
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext