Skip to content

Instantly share code, notes, and snippets.

View sofish's full-sized avatar
🚀
still hiring creatives

小鱼 sofish

🚀
still hiring creatives
View GitHub Profile
@sofish
sofish / config.js
Created October 28, 2012 10:33
auto route
var router, middlewares, before, after, app
app = express()
router = require('./router')
app.configure(function(){
app.set('controllers', require('YOUR_CONTROLLERS'))
})
middlewares = {
@sofish
sofish / scrollNews.js
Created October 18, 2012 01:54
Scroll News
// 完成滚动信息的简短代码
~function ($) {
/* 浮动信息插件
* @author sofish, [email protected]
* @param options {Object} 加下的参数
* {
* data: 传入的 JSON 数据
* tmpl: 显示信息的格式模板 `{json 数组中 object 的 key 名} 发布了 {dataName}`
* 为了更快的匹配,{dataName} 中的 dataName 只支持除中文外的 js 变量名命名方法中支持的内容
@sofish
sofish / type-day.js
Created October 10, 2012 12:25
detect date type
// 仅支持 8 种类型的 day
// 20120409 | 2012-04-09 | 2012/04/09 | 2012.04.09 | 以上各种无 0 的状况
var isDate = function (text) {
var reg = /^([1-2]\d{3})([-/.])?(1[0-2]|0?[1-9])([-/.])?([1-2]\d|3[01]|0?[1-9])$/
, taste, validDate, yyyy, mm, dd;
if (!reg.test(text)) return false;
taste = reg.exec(text);
year = taste[1], month = taste[3], day = taste[5];
@sofish
sofish / array.reduce.js
Created October 8, 2012 05:16
array 的 reduce 方法好奇怪
var arr = [1,2,3,4,5,6,7,8,9];
arr.reduce(function(prev, cur, i, index){
// 奇葩
console.log(prev,cur,i,index, arr[--i] === prev)
})
arr.reduce(function(prev, cur, i, index){
// 正常
console.log(prev,cur,i,index, arr[--i] === prev)
@sofish
sofish / typeof.js
Created October 1, 2012 13:15
detect object type in javascript
/*! copyright: sofish, https://github.com/sofish, licensed under MIT */
var util = {};
// detect object type
// note that everything is a object in javascript
util.type = function(obj) {
var str = Object.prototype.toString.call(obj);
return str.replace(/^\[object (\w+)\]$/, '$1');
}
@sofish
sofish / they-recognize-blackslash-9-hack.html
Created September 25, 2012 18:46
\9 hack 在 Firefox 等浏览器失效的情况
<!Doctype html>
<html>
<meta charset="utf-8"/>
<title>they recognize \9 hack(IE only???)</title>
<style>
html{background:#000;color:#fff;}
pre{font:60px monospace\9;}
</style>
<body>
理论上应该只在ie才会显示 60px 大的字体:
@sofish
sofish / first.js
Created August 31, 2012 05:47
面试的时候我会经常问,js 中如何获得 <ul> 下的第一个 <li>,你的答案是什么?
// 大家写在评论中吧,代码高亮可以这样写:
// ```js
// your code
// ```
// update: Fri Aug 31 08:39:21
// copyright: https://gist.github.com/3549352
// 加个性能测试:http://jsperf.com/get-dom-s-first-element
var util = {};
@sofish
sofish / first.js
Created August 31, 2012 05:30
DOM: get the first child of a DOM element
var util = {};
/* now: we use this one */
util.first = function(element) {
if(!element) return;
return element[element.firstElementChild ? 'firstElementChild' : 'firstChild'];
}
/* former: */
util.first = function(element) {
@sofish
sofish / sea_in_action.html
Created August 3, 2012 12:52
using seajs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Loader</title>
<link href="http://typo.sofish.de/typo.css" rel="stylesheet" />
<style type="text/css">
code{padding:0 2px;border:1px solid #eee;background:#f7f7f7;border-radius:3px;}
#geo{color:#08c;}
</style>
// hack the ie on resetting the height
t.on('click', function(){
$('textarea').focus();
});
t.on('focus', function(){
t.css('height', dest);
})