Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zhike-team/zhike-util
public function library for zhike
https://github.com/zhike-team/zhike-util
Last synced: 4 days ago
JSON representation
public function library for zhike
- Host: GitHub
- URL: https://github.com/zhike-team/zhike-util
- Owner: zhike-team
- Created: 2016-11-10T06:08:19.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-10T11:09:23.000Z (over 7 years ago)
- Last Synced: 2024-12-22T20:17:37.115Z (about 1 month ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 3
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# zhike-util
public function library for zhike
## INSTALL
npm install zhike-util
## API
+ [md5](#jumpMd5)
+ [randString](#jumpRandString)
+ [getFields](#jumpGetFields)
+ [toCamel](#jumpToCamel)
+ [getIp](#jumpGetIp)
+ [getIPInfo](#jumpGetIPInfo)
+ [dateFormat](#jumpDateFormat)
+ [request](#jumpRequest)### md5(s)
Hash a string with md5Params
+ s(String): string before md5
Usage
```js
var util = require('zhike-util');
util.md5('hello'); // 5d41402abc4b2a76b9719d911017c592
```### randString(length)
Generate a specified length string randomlyParams
+ length(Number): length of string
Usage
```js
var util = require('zhike-util');
util.randString(8); // B2UT7Z3E
```### getFields(data, fields, notSetNull)
Extract fields which contains by dataParams
+ data(Object): object which is extracted
+ fields(Array): array of keys
+ notSetNull(Boolean, default: false): fields not contained by object should set null or notUsage
```js
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}
```### toCamel(name)
Convert underlined or middlelined string to camelParams
+ name(String)
Usage
```js
var util = require('zhike-util');
var name = 'love_is-love';
util.toCamel(name); // loveIsLove
```### getIp(req)
Get ip address from the request headerParams
+ req(Object)
Usage
```js
var util = require('zhike-util');
var app = require('express')();
app.use(function(req, res, next) {
util.getIp(req); // 127.0.0.1
next();
})
```### getIPInfo(ip)
Get location from ipParams
+ ip(String)
Usage
```js
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.' }
})```
### dateFormat(fmt, d)
Format a specified dateParams
+ fmt(String): date format, example: yyyyMMddHHmmss
+ d(date): dateUsage
```js
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
```### request(options, callback)
Thunkify request which could be used by yield directlyParams
+ options(Object): any options supported by request, `method` default `GET` and `json` default `true`
Usage
```js
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'
}
});
})
```## TEST
npm run test