npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zhike-util

v1.0.6

Published

public function library for zhike

Downloads

4

Readme

zhike-util

public function library for zhike

INSTALL

npm install zhike-util

API

md5(s)

Hash a string with md5

Params

  • s(String): string before md5

Usage

var util = require('zhike-util');
util.md5('hello');  // 5d41402abc4b2a76b9719d911017c592

randString(length)

Generate a specified length string randomly

Params

  • length(Number): length of string

Usage

var util = require('zhike-util');
util.randString(8);  // B2UT7Z3E

getFields(data, fields, notSetNull)

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}

toCamel(name)

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

getIp(req)

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();
})

getIPInfo(ip)

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.' }
})

dateFormat(fmt, d)

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

request(options, callback)

Thunkify request which could be used by yield directly

Params

  • options(Object): any options supported by request, method default GET and json default true

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'
    }
  });
})

TEST

npm run test