Skip to content

Instantly share code, notes, and snippets.

@zzuhan
zzuhan / class-check.js
Created March 26, 2019 09:14
[js class] js class中的一些操作 #js
mySprite instanceof Sprite;
@zzuhan
zzuhan / error-handle.js
Created March 15, 2019 07:12
[error-handle] nodejs的错误处理 #nodejs
//
process.on('uncaughtException', onFatal);
// 未处理的process的reject
process.on('unhandledRejection', onFatal);
return require('co')(function* () {
yield start()
}).catch(onFatal);
@zzuhan
zzuhan / doc.markdown
Created March 15, 2019 06:53
[vscode document] vscode的文档服务 #vscode
@zzuhan
zzuhan / exec.ts
Created March 15, 2019 06:48
[node-childprocess] nodejs子进程 #nodejs
// 摘自vscode-vsce中
function exec(command: string, options: IOptions = {stopOnError: true}, cancellationToken?: CancellationToken): Promise<{ stdout: string; stderr: string; }> {
return new Promise((c, e) => {
let disposeCancellationListener: Function = null;
const child = cp.exec(command, { ...options, encoding: 'utf8' } as any, (err, stdout: string, stderr: string) => {
if (disposeCancellationListener) {
disposeCancellationListener();
disposeCancellationListener = null;
@zzuhan
zzuhan / overwrite-require1.js
Last active March 15, 2019 06:55
[node-overwrite] nodejs中覆盖 #nodejs
// 方法1
var Module = require('module');
const originalLoad = Module._load;
// 重写require方法,用来覆盖vscode
Module._load = function(request, parent){
if(request == 'vscode') {
return require('./mock/vscode');
{
"db":{
"host":"127.0.0.1",
"port":3306,
"user":"root",
"password":"",
"database":"crawler"
},
"baseSite":"http://s3131212.com/links/"
}
@zzuhan
zzuhan / *md-compatible.js
Created June 4, 2014 14:43
how to write amd,cmd,browser environment supported js code
/*
* this series is write like the title
*/
@zzuhan
zzuhan / micro-tpl.js
Created May 12, 2014 14:12
micro tpl 极小的模板系统,支持数据递归
var microTpl = {
render: function (tpl, data) {
if(!tpl || !data) {
throw new Error("tpl or data can't be empty, microlTpl.render");
}
// 占位符的正则 the regex of placeholder
var rendered,
rPlaceholder = /\{([^}]+)\}/g,
@zzuhan
zzuhan / relative_path.php
Created March 12, 2014 08:18
测算相对路径,摘自php.net网友贡献
function relative($from, $to, $ps=DIRECTORY_SEPARATOR){
$arFrom = explode($ps, rtrim($from, $ps));
$arTo = explode($ps, rtrim($to, $ps));
while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
{
array_shift($arFrom);
array_shift($arTo);
}
return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo);
}
@zzuhan
zzuhan / path-join.js
Last active January 4, 2016 08:09
js 的路径处理函数集合
function join(from, to) {
var part, newPath;
isAbsolute = from.charAt(0) == '/',
pathParts = [].concat(from.split('/')).concat(to.split('/')),
finalParts = [],
skipNum = 0;
while( part = pathParts.pop()){
if(part !== '..' && part !== '.') {
if(skipNum) {