Skip to content

Instantly share code, notes, and snippets.

View thesadabc's full-sized avatar

xjp thesadabc

View GitHub Profile
@thesadabc
thesadabc / constellation.js
Last active January 27, 2018 09:05
无分支语句 判断十二星座
function getConstellation(month, day){
const cons = ["摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"];
const divide = [20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22];
const m = month - 1;
const d = day;
return cons[m + (divide[m] <= d)];
}
@thesadabc
thesadabc / formatTime.js
Last active September 10, 2018 15:56
simple date formater
/**
* y:年,
* M:年中的月份(1-12),
* d:月份中的天(1-31),
* h:小时(0-23),
* m:分(0-59),
* s:秒(0-59),
* S:毫秒(0-999),
* q:季度(1-4)
*/
@thesadabc
thesadabc / delete_weibo.js
Last active September 18, 2017 13:07
微博便捷操作
// 删除微博
document.querySelector(".WB_feed").addEventListener("click", function(e) {
var card = e.target;
while(!card.matches(".WB_cardwrap")) card = card.parentElement;
fetch("//weibo.com/aj/mblog/del?ajwvr=6", {
method:"post",
credentials: "same-origin",
headers: {'content-type': "application/x-www-form-urlencoded"},
body:"mid="+card.getAttribute("mid")
}).then(function(resp){return resp.text()})
@thesadabc
thesadabc / base64.js
Created April 4, 2017 15:08
js base64 with btoa and atob,
function encode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function decode(str) {
return decodeURIComponent(atob(str).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
@thesadabc
thesadabc / check_idcard_num.js
Created January 27, 2018 09:04
check Chinese idcard number,居民身份证号验证
// (∑(ai×Wi)) % 11
exports.checkIdNum = function(idNum) {
idNum = idNum.toUpperCase();
const reg = /^\d{6}(19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}[0-9X]$/;
if (!reg.test(idNum)) return false;
const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const parity = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
const sum = factor.reduce((s, f, i) => s + f * idNum[i], 0);
return parity[sum % 11] === idNum[17]
}
@thesadabc
thesadabc / template_engine.js
Last active June 2, 2018 05:19
简单的模板引擎原来
/**
* 1. %>...<% 转换为 %>p += '...';<%, 起始和结束同此, 将内容直接作为输出
* 2. <%...%> 转换为 ...;, 逻辑处理, 直接执行
* 3. <%=...%> 转换为 p += (...);, 将运算结果作为输出
*/
function render(tpl) {
return new Function("data", "var p = ''; with(data){" +
tpl.replace(/(\s)+/g, " ")
.replace(/(\%\>|^)(.+?)(\<\%|$)/g, "$1p += '$2';$3")
.replace(/\<\%([^=](.(?!\<\%))+)\%\>/g, "$1;")
@thesadabc
thesadabc / router.js
Created June 2, 2018 05:35
简单前端路由原理
class Router {
constructor(routerMap) {
this.routerMap = {
// "/user/:user_id":{
// "handler": function (userId) {},
// "routerReg": new RegExp("^/user/[^/+]/?$")
// }
};
// "/user/:group_id/:userId" => "/user/([^/]+)/([^/]+)"
@thesadabc
thesadabc / async_by_yield.js
Created June 2, 2018 05:53
使用yield模拟async处理异步流程
// 包装执行生成器
module.exports = function(generator) {
const g = generator();
return (function loop(s) {
// 如果生成器执行结束则退出
if (s.done) return;
// 获取 yield 得到的promise
@thesadabc
thesadabc / curry.js
Created June 2, 2018 05:55
curry.js
module.exports = function (cf) {
return function () {
// 将参数传入上一次得到的不完全函数,得到下一个不完全函数
const nextCF = cf.bind(null, ...arguments);
// 参数总长度达到原函数所需长度时,执行该函数
if (nextCF.length <= 0) return nextCF();
// 参数长度未达到原函数所需长度时,返回新的函数
@thesadabc
thesadabc / Promise.js
Last active June 2, 2018 06:04
a simple PromiseA/A+ implementation, https://promisesaplus.com/
;(function(root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.Promise = factory.call(root);
}
})(this, function() {
'use strict';