Skip to content

Instantly share code, notes, and snippets.

@zzuhan
zzuhan / date_format.js
Created December 6, 2013 02:13
时间格式化
// 占位,等待更新
@zzuhan
zzuhan / simplified_code.js
Created December 9, 2013 09:16
一些可让代码更清晰的简写
var months = "星期一_星期二_星期三_..._星期日".split('_'); // [ '星期一', '星期二', '星期三', '...', '星期日' ]
@zzuhan
zzuhan / isEqArrays.js
Last active December 30, 2015 18:49
判断数组是否相等,不考虑顺序
/**
* Array comparsion, ignore order
* 比较对象都是比较是否是指向同一个对象
*/
function inArray(array, input){
for(var i=array.length; i--; ){
if(array[i] === input) return true;
}
}
@zzuhan
zzuhan / mask.css
Last active December 31, 2015 05:29
遮罩框
.mask {
position: absolute;
top:0;
right:0;
bottom:0;
left:0;
z-index: 2;
background:#000;
opacity:0.5;
}
@zzuhan
zzuhan / add-favorite.js
Last active October 13, 2018 01:28
javascript 点击按钮加入收藏夹.
/**
* - chrome为了安全考虑,设计的不支持js操作加入收藏夹,
* - 火狐23之后开始废止window.sidebar因为不是w3c标注 https://bugzilla.mozilla.org/show_bug.cgi?id=691647
* - document.all 判断IE不够靠谱,因为现在许多浏览器也实现了document.all吗,并且IE11以后(document.all)为falsy
* - 参考 http://stackoverflow.com/questions/10033215/add-to-favorites-button
* - IE 中typeof window.external.addFavorite 为'unknown' [http://www.xdarui.com/archives/203.html];
*/
var url = 'https://gist.github.com/',
title = 'gist代码片段';
@zzuhan
zzuhan / sub.js
Created December 23, 2013 15:40
sub.js 字符串替换 截取自YUI.Lang.sub
var SUBREGEX = /\{\s*([^|}]+?)\s*(?:\|([^}]*))?\s*\}/g;
function isUndefined(val){
return typeof val === 'undefined';
}
var sub = function(s, o) {
return s.replace ? s.replace(SUBREGEX, function(match, key){
return isUndefined(o[key]) ? match : key;
});
}
@zzuhan
zzuhan / reg_sample.js
Last active January 2, 2016 16:19
一些js中的正则
// seajs中正则,包括一些注释正则
var REQUIRE_RE = /"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\/\*[\S\s]*?\*\/|\/(?:\\\/|[^\/\r\n])+\/(?=[^\/])|\/\/.*|\.\s*require|(?:^|[^$])\brequire\s*\(\s*(["'])(.+?)\1\s*\)/g;
// 注释正则
var rComment = /\/\/.*|\/\*[\S\s]*?\*\//g;
@zzuhan
zzuhan / join_paths.php
Last active January 3, 2016 16:09
路径连接,可以输入无限制个参数,不用再担心`/`反斜线重复或者缺少。TODO:处理''.",'..'这种相对路径 这个php原生有realpath方法
public function joinPaths()
{
$args = func_get_args();
$paths = array();
foreach ($args as $arg) {
$paths = array_merge($paths, (array)$arg);
}
$paths = array_map(create_function('$p', 'return trim($p, "/");'), $paths);
if (substr($args[0], 0, 1) == '/') {
$paths[0] = '/' . $paths[0];
@zzuhan
zzuhan / path-join.js
Last active January 4, 2016 08:09
js 的路径处理函数集合
function join(from, to) {
var part, newPath;
isAbsolute = from.charAt(0) == '/',
pathParts = [].concat(from.split('/')).concat(to.split('/')),
finalParts = [],
skipNum = 0;
while( part = pathParts.pop()){
if(part !== '..' && part !== '.') {
if(skipNum) {
@zzuhan
zzuhan / relative_path.php
Created March 12, 2014 08:18
测算相对路径,摘自php.net网友贡献
function relative($from, $to, $ps=DIRECTORY_SEPARATOR){
$arFrom = explode($ps, rtrim($from, $ps));
$arTo = explode($ps, rtrim($to, $ps));
while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
{
array_shift($arFrom);
array_shift($arTo);
}
return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo);
}