Skip to content

Instantly share code, notes, and snippets.

@StuPig
StuPig / logger.js
Created March 28, 2012 09:09
winston logger : questions about maxsize and multi exceptions handler
var winston = require('winston');
var fs = require('fs');
var dateUtil = require('date-utils');
var LOGPATH = './log/';
var DOMAIN = 'www.labi.com';
// 测试用对日志文件的切割,所以`maxsize`设的很小
var MAXSIZE = 100;
var latest_ex_filename = null;
@StuPig
StuPig / reverseArray&reverseString.js
Created May 8, 2012 17:31
use javascript to archive Array's reverse features
// 题目:
// 假设有字符串 'i am a programmer'
// 实现方法全部颠倒,转成 'remmargorp a ma i'
var str = 'i am a programmer';
// 函数式递归调用方法实现
function reverseStrByChar(str) {
if (!str || Object.prototype.toString.call(str).toLowerCase().indexOf('string') < 0) {
throw new Error('reverseStrByChar: Invalid argument ' + str);
@StuPig
StuPig / qucikSort.js
Created May 9, 2012 04:15
Quicksort algorithm: javascript 实现的快速排序算法
// var times = 0;
// 快速排序算法
function quickSort(arr) {
if (!arr || Object.prototype.toString.call(arr).toLowerCase().indexOf('array') < 0) {
throw new Error('quickSort(): First arguments must be an Array.');
}
if (arr.length <= 1) {
return arr;
}
@StuPig
StuPig / js-princple.js
Last active October 4, 2015 14:27
JS 的 作用域 作用域链 行参 实参 传值 而非 传址
/*
function a() {
// some operations
} // #1: 到这里会报错吗?
*/
// a(); // #2: 到这里会报错吗?
@StuPig
StuPig / study-on-JS-Class&JS-prototype-inheritence.js
Created May 11, 2012 09:38
探究JavaScript类的属性(property)、原型继承(prototype inheritence)、和构造器(constructor)
// 先定义一个Person类
var Person = function (name, age) {
this.name = name;
this.age = age;
this.tmp = 'this.tmp';
this.sayName = function() {
console.log('this.sayName(): ', this.name);
}
}
@StuPig
StuPig / escaped.js
Created September 20, 2012 03:54
string escaped ( copied from YUI library )
/**
Returns a copy of the specified string with special regular expression
characters escaped, allowing the string to be used safely inside a regex.
The following characters, and all whitespace characters, are escaped:
- $ ^ * ( ) + [ ] { } | \ , . ?
If _string_ is not already a string, it will be coerced to a string.
@method regex
@StuPig
StuPig / dabblet.html
Created September 20, 2012 04:23
Untitled
<!-- content to be placed inside <body>…</body> -->
@StuPig
StuPig / handlebars_JSON_value_as_key_helper.js
Created October 24, 2012 12:47
manipulate JSON's value as key with Handlebars' helper
var source ='\
{{#each types.type}}\
<h2>{{this}}</h2>\
<p>{{#test ../this }}{{/test}}</p>\
{{/each}}\
'
var data = {
types: {
type: [
'o', 'k'
@StuPig
StuPig / mkdir_p.js
Created October 31, 2012 07:14 — forked from hongru/mkdir_p.js
mkdir -p for node
/* mkdir -p for node */
var fs = require('fs'),
path = require('path');
function mkdirpSync (pathes, mode) {
mode = mode || 0777;
var dirs = pathes.trim().split('/');
if (dirs[0] == '.') {
// ./aaa
dirs.shift();
@StuPig
StuPig / build.js
Created October 31, 2012 10:50 — forked from millermedeiros/build.js
sample node.js build script including RequireJS optimizer (r.js) and copy/delete/filter files
// Combine JS and CSS files
// ---
//
// Make sure you install the npm dependencies
// > cd YOUR_PROJECT_FOLDER
// > npm install
//
// Than run:
// > node build