public function library for zhike
npm install zhike-util
Hash a string with md5
Params
- s(String): string before md5
Usage
var util = require('zhike-util');
util.md5('hello'); // 5d41402abc4b2a76b9719d911017c592
Generate a specified length string randomly
Params
- length(Number): length of string
Usage
var util = require('zhike-util');
util.randString(8); // B2UT7Z3E
Extract fields which contains by data
Params
- data(Object): object which is extracted
- fields(Array): array of keys
- notSetNull(Boolean, default: false): fields not contained by object should set null or not
Usage
var util = require('zhike-util');
var data = {
id: 1,
name: 'fengliner',
sex: 'male'
};
var fields = ['id', 'name', 'age'];
util.getFields(data, fields); // {id: 1, name: 'fengliner', age: ''}
util.getFields(data, fields, true); // {id: 1, name: 'fengliner', age: undefined}
Convert underlined or middlelined string to camel
Params
- name(String)
Usage
var util = require('zhike-util');
var name = 'love_is-love';
util.toCamel(name); // loveIsLove
Get ip address from the request header
Params
- req(Object)
Usage
var util = require('zhike-util');
var app = require('express')();
app.use(function(req, res, next) {
util.getIp(req); // 127.0.0.1
next();
})
Get location from ip
Params
- ip(String)
Usage
let util = require('./index');
util.getIpInfo('124.207.253.186').then(function(data) {
console.log(data);
// output
{ code: 0,
data: {
country: '**',
country_id: 'CN',
area: '华北',
area_id: '100000',
region: '北京市',
region_id: '110000',
city: '北京市',
city_id: '110100',
county: '',
county_id: '-1',
isp: '鹏博士',
isp_id: '1000143',
ip: '124.207.253.186'
}
}
})
util.getIpInfo('124.207.253.300').then(function(data) {
console.log(data);
// output
{ code: 1, data: 'invaild ip.' }
})
Format a specified date
Params
- fmt(String): date format, example: yyyyMMddHHmmss
- d(date): date
Usage
var util = require('zhike-util');
var date = new Date();
util.dateFormat('yyyyMMddHHmmss', date); // 20161114105537
util.dateFormat('yyyy-MM-dd HH:mm:ss', date); // 2016-11-14 10:55:37
Thunkify request which could be used by yield directly
Params
- options(Object): any options supported by request,
method
defaultGET
andjson
defaulttrue
Usage
var util = require('zhike-util');
var co = require('co');
co(function*() {
yield util.request({
uri: 'http://api.smartstudy.com/user/count',
qs: {
source: 'www.smartstudy.com'
}
});
yield util.request({
uri: 'http://api.smartstudy.com/user/signup/phone',
method: 'POST',
body: {
phone: 15652398760,
password: '123456'
}
});
})
npm run test