Skip to content

Instantly share code, notes, and snippets.

View LiuJi-Jim's full-sized avatar
🏠
Working from home

Liu Ji LiuJi-Jim

🏠
Working from home
View GitHub Profile
@LiuJi-Jim
LiuJi-Jim / statusMachine.js
Last active August 3, 2023 18:13
UI StateMachine with immutable js
var immu = require('immutable');
var StateMachine = function(data) {
this._data = {};
this.state = immu.fromJS(this._data);
this.setState(data);
}
StateMachine.prototype.setState = function(data) {
var state = this.state.mergeDeep(data);
@LiuJi-Jim
LiuJi-Jim / baidu-ad.css
Last active May 5, 2016 08:11
百度大搜索广告标识
/*
用于Stylish
网址前缀:https://www.baidu.com/s?
网址前缀: http://www.baidu.com/s?
*/
#content_left>*:not(.result):not(.result-op):not(.leftBlock):not(#super_se_tip):not(#rs_top_new):not(.hit_top_new):not(.se_common_hint),
#ec_im_container>[id],
.ad-block {
position: relative !important;
@LiuJi-Jim
LiuJi-Jim / random-binary-tree-gen.js
Created May 8, 2015 09:21
随机生成二叉树
function generate(max) {
var counter = 0;
var density = 0.7;
var queue = [];
function gen(node) {
node.value = counter++;
if (counter >= max) return; // this is buggy. may cause real node count > max because it was already generated to the queue.
if (Math.random() < density) {
queue.push(node.left = {});
@LiuJi-Jim
LiuJi-Jim / MidIterator.js
Created May 5, 2015 16:35
中序遍历迭代器
var MidIterator = function(root) {
this.stack = [root];
this.__pushLeft(root);
};
MidIterator.prototype.__pushLeft = function(node) {
if (!node) return;
var left = node.left;
while (left) {
this.stack.push(left);
left = left.left;
@LiuJi-Jim
LiuJi-Jim / string.format.js
Created November 6, 2014 12:19
Something like String.Format in C# but much more simple.
function format(fmt){
var args = Array.prototype.slice.call(arguments, 1);
for (var i=0; i<args.length; ++i){
var reg = new RegExp('([^\\\\]|^)(?:\\{' + i + '\\})', 'g');
fmt = fmt.replace(reg, '$1' + args[i]);
}
return fmt;
}
console.log(format('Hello, {0}!', 'world', 'jim'));
@LiuJi-Jim
LiuJi-Jim / flatten.js
Created August 14, 2014 13:31
LinkedList flatten
// 极其弱的版本,对于[1, [], 2]这样的奇怪情况(嵌套的是空链)会跪,回头修回头修
var ListNode = function(value, next){
// 虽然是简单结构,但是为了Hidden Type优化做成个class
this.value = value;
this.next = next;
};
var LinkedList = function(){
this.head = this.tail = new ListNode(null, null);
@LiuJi-Jim
LiuJi-Jim / app.js
Created August 14, 2014 13:18
express.js auto route,遍历controllers目录里的所有js文件,对里面每个module的每个属性都当做一个/:controller/:action,作自动路由(就跟ASP.NET MVC差不多啦!)
// app.js
for (var controller in routes){
var mod = routes[controller];
app.all('/' + controller + ':action', function(req, res){
var action = req.params.action;
if (action in mod){
mod[action](req, res);
}else{
res.statusCode = 404;
res.send('404 not found');
@LiuJi-Jim
LiuJi-Jim / hrtime.js
Last active October 27, 2015 09:39
HRT(High Resolution Timing) in JavaScript
var hrtime = (function(){
if (typeof window !== 'undefined'){
// browser
if (typeof window.performance !== 'undefined' && typeof performance.now !== 'undefined'){
// support hrt
return function(){
return performance.now();
};
}else{
// oh no..
@LiuJi-Jim
LiuJi-Jim / bezier.js
Last active July 12, 2020 06:17
De Casteljau Bezier
function DeCasteljauBezier(points, density, step){
//if (points.length < 3) return null;
console.time('bezier');
var ps = points.map(function(p){
return {
x: p.x,
y: p.y
};
}),
results = [],
@LiuJi-Jim
LiuJi-Jim / css3-gradient-text.html
Last active December 26, 2015 23:39
CSS3 渐变文字
<!doctype html>
<html>
<head>
<title>CSS3文字渐变</title>
<meta charset="UTF-8">
<style type="text/css">
body {
margin:0;
padding:0;
background:#fff;