# beyond lib
从原先版本的 beyond 库中分离出来的函数库，当前版本包括以下几个部件：

- assign
- browser
- dateDiff
- dateFormat
- htmlHelper
- klass
- namespace
- storage
- url

已经支持 typescript  

## 安装
```
npm install beyond-lib --save
```

## 浏览器兼容性
IE11,Firefox 和 Chrome 最新版本

## 使用方式

es5 方式
```javascript
//方式一
var beyondlib = require('beyond-lib')
var storage = beyondlib.storage

//方式二 推荐，避免全局加载
var storage = require('beyond-lib/lib/storage')
```

typescript 方式
```typescript
//方式一

import * as storage from 'beyond-lib/lib/storage'

import {storage} from 'beyond-lib'

```

## 文档


### browser parse

浏览器解析
```javascript
import {parseBrowser} from 'beyond-lib/lib/browser'

var info = parse(userAguent)

info.isIE   //boolean
info.isIE8  //boolean
info.isIE9  //boolean
info.isIE10  //boolean
info.isIE11  //boolean
info.isIE678  //boolean
info.isEdge  //boolean
info.isChrome  //boolean
info.isFirefox  //boolean
info.isSafari  //boolean
info.isMicroMessenger //boolean 是否微信浏览器
info.name //browser name  like 'Chrome' , 'Firefox' , 'IE' ,'Opera' , 'Safari' , 'Edge'
info.version //browser version
info.isWindows  //boolean
info.isMac  //boolean
info.isiPhone  //boolean
info.isiPad  //boolean
info.isiPod  //boolean
info.isAndriod  //boolean
info.isLinux  //boolean
```

### dateDiff

返回两个时间点之间的时间间隔,如果第一个参数代表的时间晚于第二个参数，返回正数，相等返回0，否则返回负数

```javascript
import dateDiff  from 'beyond-lib/lib/dateDiff'
dateDiff.years(new Date(2008,01,01),new Date(2006,02,01))  // return 2
dateDiff.years(1170259200000,1141142400000)  // return 1
dateDiff.years(new Date(2006,01,01),new Date(2008,02,01))  // return -2
dateDiff.years(+new Date(2007,01,01),new Date(2007,02,01))  // return 0
dateDiff.years(+new Date(2007,01,01),new Date(2007,02,01))  // return 0

dateDiff.months(date1,date2)
dateDiff.months(timestamp1,timestamp2)
dateDiff.months(date1,timestamp2)

dateDiff.days(date1,date2)
dateDiff.hours(date1,date2)
dateDiff.minutes(date1,date2)
dateDiff.seconds(date1,date2)

```

### dateFormat

返回格式化后的日期时间字符串

```javascript 
import dateFormat from 'beyond-lib/lib/dateFormat'
dateFormat(new Date(2000,00,01,12,12),'yyyy-MM-dd')  // 返回该时间戳的默认日期 2000.01.01
```

| 类型 | 说明   |  
| -----| -----  | 
| yyyy | 4位数年份 |  
| yy   | 2位数年份   | 
| MM   | 2位数月份，如果是个位则前置0 |  
| M    | 月份 |  
| dd   | 2位数日期，不足前置0 |  
| d    | 日期 |  
| HH   | 2位数24制小时，不足2位前置0 |
| H    | 24制小时 |
| hh   | 2位数12制小时，不足2位前置0 |
| h    | 12制小时 |  
| mm   | 2位数分钟，不足前置0 |
| m    | 分钟 |  
| ss   | 2位数秒，不足前置0 |
| s    | 秒 |  
| S    | 毫秒 |  
| u    | 星期几，0-6,0表示周日 |  


### htmlHelper
```javascript
import htmlHelper from 'beyond-lib/lib/htmlHelper'
htmlHelper.encodeHTML('<a href="#">hello&\'quote\'</a>')
htmlHelper.decodeHTML('&lt;a href=&quot;#&quot;&gt;hello&amp;&#039;quote&#039;&lt;/a&gt;')
```


### storage
浏览器本地存储，包括 localStorage 和 cookie

```javascript
import * as storage from 'beyond-lib/lib/storage'
//localstorage
storage.set('key',{value : 'value'})
storage.get('key')

//cookie,expire 单位为天
storage.setCookie('key','value')
storage.setCookie('key','value',expire,path,domain,secure)
storage.setRawCookie('key','value')
storage.setRawCookie('key','value',expire,path,domain,secure)
storage.getCookie('key')
storage.getRawCookie('key')
storage.removeCookie('key')
```

### url
url解析

```javascript
import {parseUrl} from 'beyond-lib/lib/url'

var info = parse('http://rob:abcd1234@1.2.3.4:9090/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese')
info.protocol   // http
info.auth       // rob:abcd1234
info.user 		// rob
info.pass 		// abcd1234
info.host 		// 1.2.3.4:9090
info.port  		// 9090
info.hostname   // 1.2.3.4
info.hash  		// test=hash&chucky=cheese
info.search 	// ?query1=test&silly=willy
info.query 		// {query1 : 'test' , silly : 'willy'}
info.pathname   // /path/index.html
info.url 		// http://rob:abcd1234@1.2.3.4:9090/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese
```

### EventBus

自定义事件，事件派发


```javascript
import EventBus from 'beyond-lib/lib/EventBus'
var e = EventBus

//绑定事件
let off = e.on('load',function(event){
	console.log('loaded')
	console.log(event.data)
})

//触发事件
e.trigger('load',data)


//解除绑定
off()


```

### lock & delay
节流 和 防抖

``` javascript
// 节流
import lock from 'beyond-lib/lib/lock'
// 防抖
import delay from 'beyond-lib/lib/delay'

let fn = ()=> console.log('abc')

let lockFn = lock(fn,0.3)  
let delayFn = lock(fn,0.3)  

```

### createStore & createProxy
缓存 remoter 接口和变更 remoter 接口返回，与 beyond-remote 搭配食用更香


```javascript
import createStore,{createProxy} from 'beyond-lib/lib/createStore'
let i = 0
var remoter = function(){
	return new Promise(function(resolve,reject){
		resolve({data : ++i})
	})
}
let remoterWithCache = createStore(remoter)
remoterWithCache().then((res)=> console.log(res.data))  // 1
remoterWithCache().then((res)=> console.log(res.data))	// 1

let j = 0
var remoter2 = function(){
	return new Promise(function(resolve,reject){
		resolve({data : ++j})
	})
}
let remoterWithCache2 = createStore(remoter2,(res)=> res.data  )
remoterWithCache().then((data)=> console.log(data)) // 1
remoterWithCache().then((data)=> console.log(data))	// 1

let k = 0
var remoter3 = function(){
	return new Promise(function(resolve,reject){
		resolve({data : ++k})
	})
}
//createProxy 单纯对接口调用做数据过滤，不做缓存
let remoterWithProxy = createProxy(remoter3,(res)=> res.data  )
remoterWithProxy().then((data)=> console.log(data)) // 1
setTimeout(()=>{
	remoterWithProxy().then((data)=> console.log(data))	// 2
},1000)

```