Skip to content

Instantly share code, notes, and snippets.

Math.mod = function (a, b) {
return (a - Math.floor(a / b) * b);
};
Math.rem = function (a, b) {
return (a - ((a / b) | 0) * b);
};
Math.sign = function (a) {
return 0 > a ? -1 : 1;
};
var Uint32 = {
"~": function (a) {
return ~a >>> 0;
},
not: function (a) {
return this["~"](a);
},
"&": function (a, b) {
return (a & b) >>> 0;
@zbinlin
zbinlin / dom.js
Last active August 29, 2015 14:10
遍历 DOM 节点
/* 深度优先 */
function dom1(el, isBack) {
isBack || ++idx;
if (!el) {
return;
}
var next = isBack ? el.nextElementSibling : (el.firstElementChild || el.nextElementSibling);
return dom1(next ? next : el.parentNode, !next);
}
;(function (global, factory) {
"use strict";
var moduleName = "FastClick";
if (typeof define === "function" && define.amd) {
define(function () {
return (global[moduleName] = factory(global));
});
} else if (typeof module === "object" && module.exports) {
/*
* UnitBezier
* from: https://chromium.googlesource.com/chromium/blink/+/master/Source/platform/animation/UnitBezier.h
* version: 0.0.1
*/
"use strict";
;(function (global, factory) {
if ("function" === typeof define && define.amd) {
define(function () {
@zbinlin
zbinlin / SpiralArray.js
Created December 8, 2014 14:47
spiral matrix
/*
* spiral matrix
* 根据给出的行(row)与列(col),生成一个螺旋数组([[x0, y0], .., [x, y]]])
* 如 row = 3, col = 4
* 01, 02, 03, 04
* 10, 11, 12, 05
* 09, 08, 07, 06
* 将生成:
* [[0, 0], [1, 0], [2, 0], [3, 0], [3, 1], [3, 2], [2, 2], [1, 2], [0, 2], [0, 1], [1, 1], [2, 1]]
*
@zbinlin
zbinlin / throttle.js
Created December 13, 2014 12:50
Throttle
/*
* @param {Function} func
* 需要进行节流的函数
* @param {number} wait
* 节流的时间间隔
* @param {boolean} [trail]
* 是否要在结尾执行
* @returns {Function}
* 返回一个节流函数
*/
@zbinlin
zbinlin / quoteString.js
Created February 1, 2015 08:39
quoteString
/**
* quote function
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/quote|MDN}
* @param {string} str
* @param {string} quote
* @returns {string} quote a string
*/
function quoteString(str, quote) {
str = "" + str;
quote = "" + (quote || "\"");
/*
* *: alnum
* #: digit
* @: alpha
*/
var alpha = [...("a").repeat(6)].map((c, i) => String.fromCharCode(c.charCodeAt(0) + i)).sort(() => Math.random() - 0.5);
var digit = [...("0").repeat(10)].map((c, i) => String.fromCharCode(c.charCodeAt(0) + i)).sort(() => Math.random() - 0.5);
var alnum = [].concat(alpha, digit).sort(() => Math.random() - 0.5);
var collect = {
"*": alnum,
@zbinlin
zbinlin / 1025.js
Created July 31, 2015 09:34
auto currying
/**
* Auto Currying
* @param {Function} fn
* @return {Function}
*/
function curring(fn) {
var fnLength = fn.length;
return (function next(argv) {