Skip to content

Instantly share code, notes, and snippets.

View chemdemo's full-sized avatar
👨‍💻
Happy coding

衍良 chemdemo

👨‍💻
Happy coding
View GitHub Profile
@chemdemo
chemdemo / parse_tag.js
Created February 12, 2014 02:32
html tag filter
var parseHtmlTarg = (function(){
//解析页面html字符串
var _RegExp = /<(\/)*([a-zA-Z]+)([^>\w]+[^>]*)*>/ig;
var _attrRegExp = /[^>\w]+([a-zA-Z]+)=[\"\']?([^\"\']*)[\"\']?/ig;
var cssRegExp = /(expression)+/ig
var URL_EXP = new RegExp("((news|telnet|nttp|file|http|ftp|https)://)(([-A-Za-z0-9]+(\\.[-A-Za-z0-9]+)*(\\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\\.[0-9]{1,3}){3}))(:[0-9]*)?(/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*)*", "g");
//var IMG_EXP = /([^'"]+)[^>]*/ig
var filtterTags = {
'script' : true,
'iframe' : true,
@chemdemo
chemdemo / js_this.js
Last active August 29, 2015 13:56
`this` in javascript
// `this` in javascript functions points to the object called it!!
function f() {
undefined !== this.x ? this.x ++ : (this.x = 1);
}
var o = {
f: f
};
@chemdemo
chemdemo / node_proxy.js
Last active August 29, 2015 13:56
simple proxy by Node.js
var http = require('http');
var fs = require('fs');
var cp = require('child_process');
var getSocketPath = function(cpu) {
return __dirname + '/' + cpu + '.sock';
};
var cpu = 0;
// node process was bound to the first cpu in this demo.
var socketPath = getSocketPath(cpu);
@chemdemo
chemdemo / queue.js
Last active August 29, 2015 13:56
async queue
/**
example:
function async(n, callback) {
setTimeout(function() {
callback(n);
}, n);
}
queue([100, 200], async, function(err, r) {console.log(r);}); // => [100, 200]
**/
@chemdemo
chemdemo / event_dispatcher.js
Created February 25, 2014 02:14
simple event dispatcher.
var dispatcher = function() {
var pools = {};
// {
// 'foo': [fn1, fn2],
// 'bar': [fn3, fn4]
// }
return {
on: function(action, handle) {
var actions = pools[action] || (pools[action] = []);
@chemdemo
chemdemo / str_replace
Created March 11, 2014 15:47
string replace
var s = '$0abc\\$1 $2 $3[$2]gr $4[x][1]'; // => $0, $2, $3[$2], $4[x][1]
s.replace(/((?!\\)\$\d+(\[[^\[\]]+\])*)/g, function(m, $1, $2, index) {
console.log(m, $1, $2, index);
});
@chemdemo
chemdemo / post_message.js
Created April 17, 2014 12:07
Test postMessage
// see => http://jsperf.com/postmessage
var echoSetTimeout = (function() {
return function(msg, cb) {
setTimeout(cb, 0);
}
})();
var echoWorker = (function() {
var code = 'onmessage = function(e) { postMessage(e.data) };';
var blobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
//比如在下列A、B、C、D三处转义方式均有细节差异。
//<a href="http://www.qq.com" title="<?=$A?>"><?=$B?></a>
//<script>var name="<?=$C?>"</script>
//<textarea>
//<?=$D?>
//</textarea>
//进行pre的只能是D,而且还要做其他转义,比如 < 转为 &lt;
@chemdemo
chemdemo / interview_questions.md
Last active August 29, 2015 14:02
Interview questions
  • 求值:
var n1 = 10;
var n2 = 5;
var r = n1+++n2;
console.log(n1, n2, r); // => 11, 5, 15
  • 求值:
@chemdemo
chemdemo / min_promise.js
Last active August 29, 2015 14:02
mini Promise
var Promise = function () {
this.thens = [];
};
Promise.prototype = {
resolve: function () {
var t = this.thens.shift(), n;
t && (n = t.apply(null, arguments), n instanceof Promise && (n.thens = this.thens));
},
then: function (n) {