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);
}
View strLen.js
// 返回字符串的实际长度, 一个汉字算2个长度
String.prototype.strlen = function () {
return this.replace(/[^\x00-\xff]/g, "**").length;
}
View ~~.js
// ~符号用在JavaScript中有按位取反的作用,~~即是取反两次,而位运算的操作值要求是整数,其结果也是整数,所以经过位运算的都会自动变成整数,可以巧妙的去掉小数部分,类似于parseInt,比如:
let a = 1.23;
let b = -1.23;
console.log(~~a); // 1
console.log(~~b); // -1