Skip to content

Instantly share code, notes, and snippets.

@lovio
lovio / nodejs_cipher.js
Created June 3, 2014 02:35
something about crypto
var crypto = require('crypto');
var shasum = crypto.createHash('sha1'); // this will return a hash object
// It is a stream that is both readable and writable. The written data is used to compute the hash.
// Once the writable side of the stream is ended, use the read() method to get the computed hash digest.
// The legacy update and digest methods are also supported.
shasum.update('some data needs to be hashed and can be called many times');
console.log(shasum.digest('hex'));
// hex, binary or base, a buffer is returned is no encoding provided
@lovio
lovio / response-rewriter.js
Last active August 29, 2015 14:01
rewrite the response
/**
* ONLY TO MAKE $RESPONSE EASY TO USE
* in some case, Backend will respond every request with a 200 http status code
* and [error_or_not, data] array.
* It's very difficult to use when writing front-end code with angular.
*
* The purpose of this module is to transform it to legal one
*/
/**
@lovio
lovio / num_string.js
Created May 9, 2014 09:27
进制转换的方法
/**
* 将一个10进制的数字字符串转换成36进制
*/
function to36 (str) {
return Number(str).toString(36);
}
/**
* 从36进制的字符串转换成10进制
*/
@lovio
lovio / response.js
Last active August 29, 2015 14:00
a usage of `~`
res.type = function(type){
return this.set('Content-Type', ~type.indexOf('/')
? type
: mime.lookup(type));
};
# 判断Cotent-Type中是否有/,比如application/json
# 如果有就去查询mime
# 在js中, null, undefined , '' 以及0都是false
# ~-1 === 0
@lovio
lovio / BetterAlphaNum.js
Last active August 29, 2015 13:59
生成随机字符串
function generateRandomAlphaNum(len) {
var rdmString = "";
for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
return rdmString.substr(0, len);
};
@lovio
lovio / angular_underscore.js
Last active August 29, 2015 13:56
Using _ in Angular
angular.module('myApp').factory('_', function(){
return window._;
})