xp-types
v1.1.5
Published
A pack to handle javascript data types
Downloads
11
Readme
xp-types
A pack to handle javascript data types
Install
npm i xp-types --save
Usage
import { string } from 'xp-types'
// let string = require('xp-types').string
let str = 'I am 韩小平'
console.log(str.length) // 8
let len = string.len(str)
console.log(len) // 11
API
object
object.deepCopy(obj)
- deep copy object
let obj = { a: 1 }
let obj2 = object.deepCopy(obj)
obj2.a = 2
console.log(obj) // { a: 1 }
console.log(obj2) // { a: 2 }
object.objectify(obj, keys)
- if obj[key] is
undefined, null, 0, etc.
, change it to{}
let obj = {}
console.log(obj.a) // undefined
object.objectify(obj, ['a'])
console.log(obj.a) // {}
array
array.fastArr(num, from)
- fast get an array of ordered numbers
let arr1 = array.fastArr(5) // [1, 2, 3, 4, 5]
let arr2 = array.fastArr(5, 6) // [6, 7, 8, 9, 10]
array.unique(arr)
- remove repeated values, only work for primitive types
let arr = [1, 2, 3, 2]
let arr2 = array.unique(arr) // [1, 2, 3]
array.order(arr, reverse)
- order array elements, will effect the array, no return value
let arr = [1, 4, 3, 2]
let arr2 = [2, 1, 3, 4]
array.order(arr)
console.log(arr) // [1, 2, 3, 4]
array.order(arr2, true)
console.log(arr2) // [4, 3, 2, 1]
array.arraify(arr)
- change
undefined, null, 0, etc.
to[]
let arr = null
let ret = array.arraify(arr)
console.log(ret) // []
string
string.len(str)
- get the real length of a string(1 Chinese charactor equals to 2 English charactors).
let str = 'I am 韩小平'
let len = string.len(str)
console.log(len) // 11
string.ellipsis(str, maxLen, suffix)
- cut the string if it is more than certain length, and use suffix string to replace the rest.
let str = 'this is a note'
let newStr = string.ellipsis(str, 7)
console.log(newStr) // this is...
string.stringify(str)
- change
undefined, null, 0, etc.
to''
let str
let ret = string.stringify(str)
console.log(ret) // ''
number
number.numberify(num)
let num
let ret = number.numberify(num)
console.log(ret) // 0
url
url.query(name)
- get url query value, if name isn't set, will return the query object
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.query() // { name: 'xp', test: 1 }
url.query('name') // xp
url.hash(name)
- get url hash value, if name isn't set, will return the hash object
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.hash() // { token: 'abc', filter: 2 }
url.hash('filter') // 2
url.hostname()
- get the hostname
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.hostname() // www.example.com
url.domain()
- get the domain
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.domain() // example.com
url.sub()
- get the sub
let url = 'http://www.example.com?name=xp&test=1#token=abc&filter=2'
url.sub() // www
date
date.format(time, fmt)
- get formatted time
let time = new Date()
date.format(time, 'YYYY-MM-DD') // 2017-05-31
date.duration
- convert seconds to Chinese string
let time = 615
date.duration(time) // 10分15秒
date.ago
- convert passed time to Chinese string
let time = 3815
date.ago(time) // 1小时前