Instantly share code, notes, and snippets.

View cubeDebug.js
function cubeDebug(name) {
try {
let key = `cube-debug_${name}`;
window.localStorage.setItem(key, '{}');
return function(action, params) {
const date = new Date();
const time = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
let preObj = JSON.parse(window.localStorage.getItem(key));
preObj[action] = params;
preObj.time = time;
View getFuncArgName.js
function getFuncArgName(func) {
// 正则表达式出自http://krasimirtsonev.com/blog/article/Dependency-injection-in-JavaScript
var paramNames = func.toString().match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1];
paramNames = paramNames.replace(/ /g, '');
paramNames = paramNames.split(',');
return paramNames;
}
View dom-to-json.js
function toJSON(node) {
node = node || this;
var obj = {
nodeType: node.nodeType
};
if (node.tagName) {
obj.tagName = node.tagName.toLowerCase();
} else
if (node.nodeName) {
obj.nodeName = node.nodeName;
View encrypt.js
// http://lib.baomitu.com/crypto-js/3.1.9-1/crypto-js.js
function encrypt(content, key) {
var encryptResult = CryptoJS.AES.encrypt(
content,
key,
{
//iv: key,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}
View fileMd5.js
let fs = require('fs');
let crypto = require('crypto');
let path = '/Users/lushijie/Pictures/day1.jpg';
let start = new Date().getTime();
let md5sum = crypto.createHash('md5');
let stream = fs.createReadStream(path);
stream.on('data', function(chunk) {
md5sum.update(chunk);
});
View runAtOnce.js
let articleRequest = () => {
let f = function() {
// todo
}.bind(this)();
return f;
};
articleRequest();
let articleRequest2 = function f() {
// todo
View declareAndRun.js
let func = (function f(a) {
console.log(a);
return f;
})('hello world');
View toUnicodeSequence.js
function toUnicodeSequence(str) {
for(var i = str.length; i--;) {
str = str.slice(0, i) + '\\u'
+ ('000' + str.charCodeAt(i).toString(16)).slice(-4)
+ str.slice(i + 1);
}
return str;
}
View getThirdResoure.js
function getThirdResoure() {
var resp = null;
var tags = document.querySelectorAll('iframe[src],frame[src],script[src],link[rel=stylesheet],object[data],embed[src]');
for(var i = 0; i < tags.length; i++){
var tag = tags[i];
var a = document.createElement('a');
a.href = tag.src||tag.href||tag.data;
if(a.hostname != location.hostname){
resp = resp || [];
resp.push({name: tag.localName, href: a.href});
View decToRadixN.js
let CHARS = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789-_.~!*()'; // except \'
function decToRadixN(radixChars, numLen) {
let result = [];
let radix = radixChars.length;
return function cacl(dec) {
result.unshift(dec % radix);
if (dec >= radix) {
dec = Math.floor(dec / radix);
return cacl(dec, radix);
}