Skip to content

Instantly share code, notes, and snippets.

View fwon's full-sized avatar
🎯
Focusing

Feng Wang fwon

🎯
Focusing
View GitHub Profile
@fwon
fwon / useReducer.js
Created April 30, 2020 07:58
hooks reducer
// reducer
function todosReducer(state, action) {
switch (action.type) {
case 'add':
return [...state, {
text: action.text,
completed: false
}];
// ... other actions ...
default:
@fwon
fwon / byte-length.js
Created June 13, 2017 09:38
Get byte length
function getByteLen(normal_val) {
// Force string type
normal_val = String(normal_val);
var byteLen = 0;
for (var i = 0; i < normal_val.length; i++) {
var c = normal_val.charCodeAt(i);
byteLen += c < (1 << 7) ? 1 :
c < (1 << 11) ? 2 :
c < (1 << 16) ? 3 :
@fwon
fwon / deepClone.js
Created March 30, 2017 07:50
deep clone
function deepClone(obj, hash = new Map()) {
if (Object(obj) !== obj) return obj; // primitives
if (hash.has(obj)) return hash.get(obj); // cyclic reference
var result = Array.isArray(obj) ? []
: obj.constructor ? new obj.constructor() : {};
hash.set(obj, result);
if (obj instanceof Map)
Array.from(obj, ([key, val]) => result.set(key, deepClone(val, hash)) );
return Object.assign(result, ...Object.keys(obj).map (
key => ({ [key]: deepClone(obj[key], hash) }) ));
@fwon
fwon / md5.js
Created December 13, 2016 11:33
获取字符串的md5值
crypto.createHash('md5').update(text).digest('hex')
@fwon
fwon / klass.js
Created November 30, 2016 06:37
Class
https://github.com/ded/klass/blob/master/klass.js
@fwon
fwon / Hilo-Class.js
Created July 8, 2016 09:32
Hilo 中的 Class类
/**
* Hilo
* Copyright 2015 alibaba.com
* Licensed under the MIT License
*/
/**
* @language=en
* Create Example Class:
* <pre>
@fwon
fwon / sample-particle.js
Created June 27, 2016 03:38
canvas particle sample
//Lets create a simple particle system in HTML5 canvas and JS
//Initializing the canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
//Canvas dimensions
var W = 500; var H = 500;
//Lets create an array of particles
@fwon
fwon / microTemplating.js
Last active January 2, 2019 08:43
简易前端模板引擎
//简易模板引擎实现
//http://krasimirtsonev.com/blog/article/Javascript-template-engine-in-just-20-line
//https://segmentfault.com/a/1190000005705169
var TemplateEngine = function(html, options) {
var re = /<%([^%>]+)?%>/g, reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = 'var r=[];\n', cursor = 0, match;
var add = function(line, js) {
js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
}
// This function:
function foo() {
var x = 5;
var y = 6;
debugger;
return x + y;
}
@fwon
fwon / WeixinDetection.js
Created May 18, 2016 09:49 — forked from anhulife/WeixinDetection.js
判断网页是否是在微信内嵌浏览器中打开
document.addEventListener('WeixinJSBridgeReady', function(){
//如果执行到这块的代码,就说明是在微信内部浏览器内打开的.
alert('当前页面在微信内嵌浏览器中打开!');
});