Skip to content

Instantly share code, notes, and snippets.

@ssnau
ssnau / server.js
Created April 23, 2013 13:39
Host any path
#!/usr/bin/env node
var util = require('util'),
http = require('http'),
fs = require('fs'),
url = require('url'),
events = require('events');
var DEFAULT_PORT = 9898;
@ssnau
ssnau / traverse.js
Created May 4, 2013 15:24
traverse any folder
var fs = require('fs'),
Path = require("path"),
util = require('util');
/**
* need an absolute path
* @param {[type]} root [description]
* @return {[type]} [description]
*/
function traverseFolder(root, config) {
angular.module("ui.ssnau.flexBox", [])
.directive("flexBox", function () {
var isDragging = false;
var meta = {};
var hasBind = false;
var ndCurtain = $("<div style='position:absolute;width:100%;height:100%;opacity:0;z-index:1000;background:blue;left:0px;top:0px;'></div>");
function bindMouseEvent(elem) {
$(elem).on("mousemove", function (e) {
if (!isDragging) return;
@ssnau
ssnau / qsort.js
Created April 5, 2014 10:09
qsort
function qsort(arr) {
partition(arr, 0, arr.length - 1);
}
function partition(arr, left, right) {
// check terminate
if (left >= right) return;
// select a pivot, for simplicity select the right most one
var pivot = arr[right];
@ssnau
ssnau / N-Queens
Created April 6, 2014 17:22
八皇后神级位运算解法
public class Solution {
public int totalNQueens(int n) {
upperlim = (1<<n) - 1;
count = 0;
dfs(0,0,0);
return count;
}
public void dfs(int row, int leftX, int rightX) {
if (row == upperlim) {
@ssnau
ssnau / auto-resize-textarea.html
Created April 16, 2014 16:48
auto resize textarea
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style>
textarea,
pre {
@ssnau
ssnau / 0_reuse_code.js
Last active August 29, 2015 14:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@ssnau
ssnau / var.js
Created February 28, 2015 14:14
riot的expression
var re_vars = /(['"\/]).*?[^\\]\1|\.\w*|\w*:|\b(?:(?:new|typeof|in|instanceof) |(?:this|true|false|null|undefined)\b|function *\()|([a-z_]\w*)/gi
// [ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]
// find variable names:
// 1. skip quoted strings and regexps: "a b", 'a b', 'a \'b\'', /a b/
// 2. skip object properties: .name
// 3. skip object literals: name:
// 4. skip javascript keywords
// 5. match var name
function wrap(s, nonull) {
s = s.trim()
@ssnau
ssnau / var-benchmarking
Created February 28, 2015 16:21
var benchmarking
function benchmark(func){
var s = Date.now();
var obj = {a: 1, b: 2};
Array(10000).join(' ').split('').forEach(function(){
func('c = a * b', obj)
func('c', obj)
});
var e = Date.now();
console.log( 'total cost:' + (e - s));
@ssnau
ssnau / gist:eb60c9beb9888d248f99
Created March 20, 2015 17:43
a super simple JSON stringify
function repeat(_char, num) {
return (new Array(num + 1)).join(_char);
}
function stringify(object, blanks) {
blanks = blanks || 4;
var leading = repeat(' ', blanks);
switch (true) {
case Array.isArray(object):
return "[" + object.map(function(item) {return stringify(item, blanks + 4)}).join(', ') + "]";