Skip to content

Instantly share code, notes, and snippets.

@getchenge
getchenge / mocha-guide-to-testing.js
Created April 11, 2019 12:17 — forked from samwize/mocha-guide-to-testing.js
Explain Mocha's testing framework - describe(), it() and before()/etc hooks
// # Mocha Guide to Testing
// Objective is to explain describe(), it(), and before()/etc hooks
// 1. `describe()` is merely for grouping, which you can nest as deep
// 2. `it()` is a test case
// 3. `before()`, `beforeEach()`, `after()`, `afterEach()` are hooks to run
// before/after first/each it() or describe().
//
// Which means, `before()` is run before first it()/describe()
@getchenge
getchenge / get_duration_mina.js
Created April 11, 2019 10:59
get audio duration in wechat MINA app
getDuration: function(audio, id) {
const {
durations = {},
retry = {}
} = this.data;
let {
duration
} = audio;
console.info('audio.duration', audio.duration);
if (duration == 0) {
#!/usr/bin/env python2
# usage python2 unwxapkg.py filename
import sys, os
import struct
class WxapkgFile(object):
nameLen = 0
name = ""
@getchenge
getchenge / sizeof.js
Last active January 29, 2019 08:10
sizeof a string
/**
* from http://www.alloyteam.com/2013/12/js-calculate-the-number-of-bytes-occupied-by-a-string/
* 计算字符串所占的内存字节数,默认使用UTF-8的编码方式计算,也可制定为UTF-16
* UTF-8 是一种可变长度的 Unicode 编码格式,使用一至四个字节为每个字符编码
*
* 000000 - 00007F(128个代码) 0zzzzzzz(00-7F) 一个字节
* 000080 - 0007FF(1920个代码) 110yyyyy(C0-DF) 10zzzzzz(80-BF) 两个字节
* 000800 - 00D7FF
00E000 - 00FFFF(61440个代码) 1110xxxx(E0-EF) 10yyyyyy 10zzzzzz 三个字节
* 010000 - 10FFFF(1048576个代码) 11110www(F0-F7) 10xxxxxx 10yyyyyy 10zzzzzz 四个字节
@getchenge
getchenge / aes.js
Created May 19, 2016 09:16
node aes cryptor
const crypto = require('crypto');
const cryptor = {
encrypt(cryptkey, iv, cleardata) {
cryptkey = crypto.createHash('sha256').update(cryptkey).digest();
const encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv);
let encryptdata = encipher.update(cleardata, 'utf8', 'binary');
encryptdata += encipher.final('binary');
return new Buffer(encryptdata, 'binary').toString('base64');
},
decrypt(cryptkey, iv, encryptdata) {
@getchenge
getchenge / generate_qrcode.js
Last active May 16, 2016 13:49
generate qrcode zip
function testpage(req, res) {
const Archiver = require('archiver');
const http = require('http');
const Stream = require('stream').Transform;
const zip = Archiver('zip');
const ids = [1, 2, 3];
const merchant_id = 57;
const store_id = 278;
const gzid = 100123;
res.writeHead(200, {
@getchenge
getchenge / thrift-generate.js
Created March 26, 2015 13:15
a thrift files generator
"use strict";
var fs = require('fs');
var path = require('path');
var async = require('async');
var childProcess = require('child_process');
var walk = function (dir, done) {
var results = [];
@getchenge
getchenge / qrcode-service.js
Last active August 29, 2015 14:16
a qrcode-service by nodejs
//npm install qrcode
"use strict";
var _ = require('lodash');
var aid = require(__base + 'lib/aid');
var defaultNumber = aid.defaultNumber();
function resample_hermite(canvas, W, H, W2, H2) {
var time1 = Date.now();
W2 = Math.round(W2);
H2 = Math.round(H2);
@getchenge
getchenge / log4js
Last active August 29, 2015 14:16
log4js & log4js syslog
var log4js = require('log4js');
log4js.configure({
appenders: [
{ type: 'console' },
{ type: 'file', filename: 'cheese.log', category: 'cheese' },
{
type: 'log4js-syslog-appender',
tag: 'My API',
facility: 'local0',

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions