Skip to content

Instantly share code, notes, and snippets.

View qzm's full-sized avatar
🚀
I may be slow to respond.

Aaron Qiu qzm

🚀
I may be slow to respond.
View GitHub Profile
@qzm
qzm / typeobj.js
Created March 20, 2017 15:44
type javascript object
'use strict';
var getType = function (obj) {
return Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1];
};
isString = function () {
return getType(this) === 'String';
};
isArray = function () {
return getType(this) === 'Array';
@qzm
qzm / validaotor.js
Last active March 22, 2017 07:30
Validator 验证器,支持链式操作
/*
* file : validator
* author : qiuzhiming
* date : 2017-3-3 10:40:43
* last : 2017-3-3 10:40:43
* description : 用于表单的数据验证,支持链式操作,Submit的操作可以写在最后的Done里面
* sample: v.test({
* data : 'hello',
* rule : {Function} or {RegExp} or {Boolean} or {String}
* success: {Function}, 可以省略
@qzm
qzm / pin.html
Created April 15, 2017 15:17
flex实现品字形结构
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
ul {
display: flex;
@qzm
qzm / defined.js
Created April 17, 2017 06:23
defined AMD/CommonJS/IIFE
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.YourProjectName = factory());
}(this, (function () {
'use strict';
})));
@qzm
qzm / cached.js
Created April 17, 2017 06:25
将纯函数,做cache
function cached (fn) {
var cache = Object.create(null);
return (function cachedFn (str) {
var hit = cache[str];
return hit || (cache[str] = fn(str))
})
}
@qzm
qzm / Camelize.js
Created April 17, 2017 06:28
Camelize a hyphen-delimited string
/**
* Camelize a hyphen-delimited string.
*/
var camelizeRE = /-(\w)/g;
var camelize = cached(function (str) {
return str.replace(camelizeRE, function (_, c) { return c ? c.toUpperCase() : ''; })
});
@qzm
qzm / toArray.js
Created April 17, 2017 06:38
Convert an Array-like object to a real Array.
/**
* Convert an Array-like object to a real Array.
*/
function toArray (list, start) {
start = start || 0;
var i = list.length - start;
var ret = new Array(i);
while (i--) {
ret[i] = list[i + start];
}
@qzm
qzm / extend.js
Created April 17, 2017 06:40
Mix properties into target object
/**
* Mix properties into target object.
*/
function extend (to, _from) {
for (var key in _from) {
to[key] = _from[key];
}
return to
}
@qzm
qzm / identity.js
Created April 17, 2017 06:47
Return same value
/**
* Return same value
* var _a = identity(a) || b;
* 当a是undefined的时候,_a 可以是undefined
*/
var identity = function (_) { return _; };
@qzm
qzm / looseEqual.js
Created April 17, 2017 06:56
判断两个值是否相等
/**
* Check if two values are loosely equal - that is,
* if they are plain objects, do they have the same shape?
*/
function looseEqual(a, b) {
var isObjectA = Object.prototype.toString.call(a) === '[object Object]';
var isObjectB = Object.prototype.toString.call(b) === '[object Object]';
if (isObjectA && isObjectB) {
return JSON.stringify(a) === JSON.stringify(b)
} else if (!isObjectA && !isObjectB) {