This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function sum(buffer) { | |
let checksum = 0x0000; | |
let parity = buffer.length % 2; | |
for (let i = 0, len = buffer.length - parity; i < len; i += 2) { | |
checksum += buffer.readUInt16BE(i); | |
} | |
if (parity) { | |
checksum += buffer.readUInt8(buffer.length - 1) << 8 | 0x00; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const format = date => date.toTimeString().split(" ")[0]; | |
/** | |
* @param {string} [name=fn.displayName||fn.name||"UNKNOW"] task name | |
* @param {(Function|*)} fn task function | |
* @param {...} [...args] task function's arguments | |
* @returns {} | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
let http = require("http"); | |
let url = require("url"); | |
let server = http.createServer(); | |
server.on("request", function (req, res) { | |
let headers = {}; | |
for (const key of Object.keys(req.headers)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let permut = (arr) => { | |
if (typeof arr === "string") { | |
arr = arr.split(""); | |
} | |
if (arr.length === 1) { | |
return arr; | |
} | |
let rst = []; | |
for (let i = 0, len = arr.length; i < len; i++) { | |
let newArr = arr.slice(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Auto Currying | |
* @param {Function} fn | |
* @return {Function} | |
*/ | |
function curring(fn) { | |
var fnLength = fn.length; | |
return (function next(argv) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* *: 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 || "\""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @param {Function} func | |
* 需要进行去抖的函数 | |
* @param {number} wait | |
* 去抖的时间间隔 | |
* @param {boolean} [lead = false] | |
* 是否要在开始执行 | |
* @returns {Function} | |
* 返回一个去抖函数 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @param {Function} func | |
* 需要进行节流的函数 | |
* @param {number} wait | |
* 节流的时间间隔 | |
* @param {boolean} [trail] | |
* 是否要在结尾执行 | |
* @returns {Function} | |
* 返回一个节流函数 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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]] | |
* |