Skip to content

Instantly share code, notes, and snippets.

View CatTail's full-sized avatar

Chiyu Zhong CatTail

View GitHub Profile
@CatTail
CatTail / gen-jsonschema
Last active December 7, 2016 09:48
Generate json schema definition (yaml format) from example snippt
#! /bin/bash
# npm install -g yamljs json-schema-generator
# json-pretty came from [here](https://gist.github.com/CatTail/fc172a7fe6f300528665e279592c6500)
cat "${1:-/dev/stdin}" | json-pretty | json-schema-generator | tail -n +2 | json2yaml -d 10 -
@CatTail
CatTail / all-route.js
Created December 6, 2016 09:22
Get all registered routes in Express
app._router.stack // registered routes
.filter(r => r.route) // take out all the middleware
.map(r => r.route.path) // get all the paths
@CatTail
CatTail / json-pretty
Created December 5, 2016 05:51
Pretty print stdin json data, even it contains comment or other misc javascript syntax
#! /usr/bin/env node
const chunks = []
process.stdin.setEncoding('utf8')
process.stdin.on('readable', () => {
const chunk = process.stdin.read()
if (chunk) {
chunks.push(chunk)
@CatTail
CatTail / repeat.js
Last active August 4, 2016 10:28
alternative to python str * number
function repeat(str, count) {
return (new Array(count + 1)).join(str)
}
@CatTail
CatTail / output
Last active August 3, 2016 04:38
A little console wrapper to log with filename and line number (without console.log('xxxxx') anymore)
$ node test.js
at Object.<anonymous> (/Users/cattail/workspace/upyun/choppe/test.js:3:9) hello
at Object.<anonymous> (/Users/cattail/workspace/upyun/choppe/test.js:5:9) world
@CatTail
CatTail / dns-server.js
Created June 16, 2016 09:44
transform `Buffer` to text representation
'use strict'
const dgram = require('dgram')
const Packet = require('native-dns-packet')
const server = dgram.createSocket('udp4', (msg, rinfo) => {
console.log(rinfo)
console.log(msg, Buffer.byteLength(msg))
console.log(bufferToString(msg, 16, 1, 2))
console.log(Packet.parse(msg))
@CatTail
CatTail / codename.js
Created June 10, 2016 02:20
Barcode name popular in StarCraft
'use strict'
const MAP = {
'A': '.-',
'B': '-...',
'C': '-.-.',
'D': '-..',
'E': '.',
'F': '..-.',
'G': '--.',
@CatTail
CatTail / wrap.js
Last active June 13, 2017 02:49
async/await wrapper for express middleware
// wrap async function to use as express middleware or route
// let wrap = fn => (...args) => fn(...args).catch(args[2])
function wrapRoute(fn) {
return function () {
return fn.apply(undefined, arguments)
.catch(arguments[arguments.length - 1]);
};
}
@CatTail
CatTail / ctl.sh
Last active April 22, 2016 01:34
Simple process manager
#! /bin/bash
PROGRAM_NAME="choppe"
PROGRAM="node app.js"
function getpid()
{
ps aux | grep "$PROGRAM" | grep -v grep | awk '{ print $2 }'
}
@CatTail
CatTail / Readme.md
Last active April 18, 2016 07:07
Benchmark for expressjs and restify