Skip to content

Instantly share code, notes, and snippets.

😁
Happy coding

ClausClaus

😁
Happy coding
Block or report user

Report or block ClausClaus

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ClausClaus
ClausClaus / publish.sh
Last active Aug 22, 2019
一键发布npm包的脚本
View publish.sh
#!/usr/bin/env bash
# 1. 版本号每次自增;
# 2. 区分测试和生产 package.json.test, package.json.prod
# 3. 发布到生产前会二次确认
srcName='package.json.test'
if [[ $1 == "prod" ]]
then
srcName='package.json.prod'
read -p "Are you sure to publish to PRODUCTION? [Y/N]" -n 1 -r
echo ""
@ClausClaus
ClausClaus / assign.js
Created Jul 16, 2019
assign 对象合并
View assign.js
export const assign = Object.assign || function (target, ...args) {
target = Object(target);
for (let i = 0; i < args.length; i++) {
const source = args[i];
if (source !== null) {
for (const key in source) {
if (hasOwn(source, key)) {
target[key] = source[key];
}
}
@ClausClaus
ClausClaus / formatObj.html
Created Mar 15, 2019
从多个对象中取出需要的属性值,隐式的判断ID是否相等
View formatObj.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body></body>
<script type="text/javascript">
/* 无需显式的比较ID是否相等实现数据的对应格式化 */
var friends = { items: [{ user_id: 1 }] }
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>jsonp请求测试</title>
</head>
<body>
@ClausClaus
ClausClaus / format-date.js
Created Jan 28, 2019
日期时间戳格式化
View format-date.js
function formatDate(date, formatStr) {
if (/(Y+)/.test(formatStr)) {
formatStr = formatStr.replace(
RegExp.$1,
(date.getFullYear() + '').substr(4 - RegExp.$1.length)
)
// 返回年 :'2016-mm-dd', 'hh:mm'
}
// 定义通用的匹配模式
let o = {
@ClausClaus
ClausClaus / reset.css
Created Jan 28, 2019
css样式格式化
View reset.css
html,body{height:100%;}
html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,caption,th,td,form,fieldset,legend,input,button,textarea,menu{margin:0;padding:0;}
header,footer,section,article,aside,nav,hgroup,address,figure,figcaption,menu,details{display:block;}
table{border-collapse:collapse;border-spacing:0;}
caption,th{text-align:left;font-weight:normal;}
html,body,fieldset,img,iframe,abbr{border:0;}
i,cite,em,var,address,dfn{font-style:normal;}
[hidefocus],summary{outline:0;}
li{list-style:none;}
h1,h2,h3,h4,h5,h6,small{font-size:100%;}
View dpi.js
(function () {
var width = parseInt(window.screen.width)
var designWidth = 450
var scale = width / designWidth
var userAgent = navigator.userAgent.toLowerCase()
var metaHead = '<meta name="viewport" content="width=' + designWidth + ','
if (/android (\d+\.\d+)/.test(userAgent)) {
if (parseFloat(RegExp.$1) > 2.3) metaHead += 'minimum-scale=' + scale + ',maximum-scale=' + scale + ','
} else {
metaHead += 'user-scalable=no,';
@ClausClaus
ClausClaus / rem.js
Last active Jan 28, 2019
rerm设置
View rem.js
(function (doc, win) {
var docEl = doc.documentElement
var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
function recalc() {
var designWidth = 750
var clientWidth = docEl.clientWidth
if (!clientWidth || clientWidth > designWidth) return
docEl.style.fontSize = (100 * clientWidth / designWidth) + 'px'
@ClausClaus
ClausClaus / get-image-base64.js
Created Dec 24, 2018
将图片转化成base64
View get-image-base64.js
function getBase64Image(img, width, height) {
var canvas = document.createElement('canvas')
canvas.width = width ? width : img.width
canvas.height = height ? height : img.height
var ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
var dataURL = canvas.toDataURL()
return dataURL
}
View index.html
<p>
Focus this tab and press <kbd>CTRL</kbd> + <kbd>V</kbd>. The image on your clipboard will be rendered on the canvas !
</p>
<canvas style="border:1px solid grey;" id="mycanvas">
You can’t perform that action at this time.