# hpjs

> helpful javascript modules

Latest version **0.4.1** (published 2016-03-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install hpjs
pnpm add hpjs
yarn add hpjs
bun add hpjs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2016-03-08 |
| First published | 2015-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | cosiner |
| Maintainers | cosiner |
| Keywords | utils |

## Links

- npm: https://www.npmjs.com/package/hpjs
- Repository: https://github.com/cosiner/hpjs
- Homepage: https://github.com/cosiner/hpjs#readme
- Issues: https://github.com/cosiner/hpjs/issues
- npm.io page: https://npm.io/package/hpjs

## Recent versions

- 0.4.1 (latest) — 2016-03-08
- 0.4.0 — 2016-03-06
- 0.3.0 — 2015-10-06
- 0.2.1 — 2015-07-28
- 0.2.0 — 2015-06-12
- 0.1.0 — 2015-06-04

## README

#hpjs
hpjs contains some tools help for javascrit development.

All the modules are wrote in nodejs's module style, using my [gulp-defmod](https://github.com/cosiner/gulp-defmod) or [browserify](https://github.com/substack/node-browserify) convert it for front-end.

#### base
Base functions.

#### validate chain
Combine mulitple validator as a chain, the error will always first one.
```javascript
var validate = require('hpjs/validate')
var validator = require('hpjs/validator')

var length = validator.length(3, 10, 'wrong length')
var chars = validate.allCharsIn('abcdefghijklmnopqrstuvwxyz', 'bad chars')

var valid = validate.use(validate.use(length, chars))

console.log(length('abcdefghijklmnopqrstuvwxyz'))
console.log(chars('0123456789'))
console.log(valid('abcdefghijklmnopqrstuvwxyz0123456789', '111111', 'zzzz1'))
```

#### api
Simple library help for send http request to backend api server support both __browser__ and __nodejs__.  
* NodeJS(Express)
```javascript
var api = request('hpjs/api')

// default backend on expressjs
function handle(req, resp) {
    var a = api.sendPost('/user/info', req, {data:{}})
    var process = function(res) {
        console.log(res.statusCode, res.data)
    }
    
    a.success(process)
    a.fail(process)
    a.error(process)
    a.any(process)
}
```

* Browser
```html
<script type="text/javascript" src="/js/dist.js"></script>
<script type="text/javascript">
    var process = function(res) {
        console.log(res.statusCode, res.data)
    }

    var api = require('hpjs/api')
    var a = api.sendPost('/user/info', {data:{}})
    
    a.success(process)
    a.fail(process)
    a.error(process)
    a.any(process)    
</script>
```

#### jquery-tabswitch
Switch the active tab by location.path, depends on jquery's dom selector.
```javascript
var tabswitch = require('hpjs/jquery-tabswitch')
var opts = null
tabswitch(opts).apply({
    '/':'index',
    'user':'',
    'blog':''
})
```
Custom options with `api.options`.

#### cookie
Porting from [jquery-cookie](https://github.com/carhartl/jquery-cookie).
```javascript
var cookie = require('hpjs/cookie')
cookie.get('uid')

cookie.remove('uid')

cookie.set('uid', '12345')

cookie.config.defaults = {} // default cookie config
```

#### jquery-submit
Switch submit button's state.
```javascript
var submit = require('hpjs/jquery-submit')

submit({
    btn:'#submitBtn',
    loadingText:'Loading',
    errField:'#errMsg',
    fields:[
        '#username', 
        '#password', 
        {
            field:'#content', 
            trim:false
        }
    ],
    callback: function(username, password, report) {
        var err = ajax(user, password)
        if (err) {
            report(err)
        } else {
            location.href = '/'
        }
    }
})
```

#### router
a simple router support variables.
```javascript
var router = require('hpjs/router')()

router.add('/user/:id/articles', function(params) {
    console.log(params.param('id'), param.query('page'))    
})

router.handle('/user/001/articles?page=1')
```

#### jquery-proute
router + pushState.
```javascript
var proute = require('hpjs/jquery-proute')
var add = proute({
    source:'a.proute',
    target:'#proute-container',
})

add('/user/:id/articles', function(params, pushState, a) {
    pushState(params.param('id'), a.text()) // content, title
})
```

---
_Source: https://npm.io/package/hpjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
