Skip to content

Instantly share code, notes, and snippets.

@lovio
lovio / angular_underscore.js
Last active August 29, 2015 13:56
Using _ in Angular
angular.module('myApp').factory('_', function(){
return window._;
})
@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 / 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 / num_string.js
Created May 9, 2014 09:27
进制转换的方法
/**
* 将一个10进制的数字字符串转换成36进制
*/
function to36 (str) {
return Number(str).toString(36);
}
/**
* 从36进制的字符串转换成10进制
*/
@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 / 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 / pbkdf2.js
Created June 4, 2014 09:36
Using pbkdf2
var crypto = require('crypto');
var makeSalt = function() {
return crypto.randomBytes(16).toString('base64');
};
var hashPassword = function(password){
var salt = new Buffer(makeSalt, 'base64');
return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
}
@lovio
lovio / local_token.md
Created June 9, 2014 02:08
简单的本地token认证
  1. 用户名+ 密码 =》 id + token
  2. str = [参数, random_number] 字典升序.join('')
  3. sign = md5( id + str + token);
@lovio
lovio / btn-group-border.css
Created July 31, 2014 06:07
btn-group-border
.btn-container-selector ~ .btn-container-selector > .btn-container {
border-left: none;
}
@lovio
lovio / tets_coupons.js
Created February 4, 2015 01:13
mocha mongoose's with `--watch`
// connect mongodb
var mongoose = require('mongoose');
// 见https://github.com/LearnBoost/mongoose/issues/1251
mongoose.models = {};
mongoose.modelSchemas = {};
var clearDB = function(done){
async.parallel([
function (cb) {
Coupon.collection.remove(cb)