Skip to content

Instantly share code, notes, and snippets.

View wenzhixin's full-sized avatar

文翼 wenzhixin

View GitHub Profile
function pad(len, num) {
return (new Array(len + 1).join('0') + num).slice(-len);
}
pad(2, 1); // 01
pad(2, 11); // 11
pad(10, 12345); // 0000012345
pad(10, 12345678); // 0012345678
pad(10, 1234567890); // 1234567890
@wenzhixin
wenzhixin / jquery_plugin_template.js
Last active September 18, 2017 07:20
jQuery 插件模板
/**
* @author zhixin wen <wenzhixin2010@gmail.com>
* @version 0.0.1
* @github https://github.com/wenzhixin/
* @blog http://wenzhixin.net.cn
*/
(function($) {
'use strict';
@wenzhixin
wenzhixin / size.js
Created July 23, 2013 02:37
单位转换
function getSize(value) {
var b1 = Math.pow(2, 10), //KB
b2 = Math.pow(2, 20), //MB
b3 = Math.pow(2, 30), //GB
b4 = Math.pow(2, 40), //TB
value = value.toLowerCase(),
size = parseFloat(value);
if (!size) {
return 0;
function parse(search) {
var query = {},
params = search.substring(1).split('&');
for (var i = 0, l = params.length; i < l; i++) {
var p = params[i].split('=');
query[p[0]] = p[1] || '';
}
return query;
}
@wenzhixin
wenzhixin / url.js
Last active December 18, 2015 17:48
/**
* var u = url('http://wenzhixin.net.cn:12345/test?name=wenzhixin#about');
* console.log(u.protocol); // http:
* console.log(u.hostname); // wenzhixin.net.cn
* console.log(u.port); // 12345
* console.log(u.pathname); // /test
* console.log(u.search); // ?name=wenzhixin
* console.log(u.hash); // #about
*/
function url(href) {
@wenzhixin
wenzhixin / timer.js
Created June 20, 2013 03:37
定时器
function timer(func, start, interval, end) {
start = start || 0;
if (arguments.length <= 2) {
setTimeout(func, start);
} else {
var repeat = function() {
var i = setInterval(func, interval);
end && setTimeout(function() {
clearInterval(i);
}, end);
@wenzhixin
wenzhixin / mongoexport.js
Created June 12, 2013 14:50
mongoexport.js
var fs = require('fs'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
options = null;
mongoClient = null;
if (process.argv.length <= 2 || process.argv.indexOf('--help') !== -1) {
showHelp();
return;