Comparing version 1.1.1 to 1.1.2
102
fe-utils.js
@@ -131,24 +131,17 @@ (function (global, factory) { | ||
var urlQuery = { | ||
queryOne: function queryOne(name, str) { | ||
var s = ''; | ||
if (str) { | ||
s = str; | ||
} else { | ||
s = loc.search; | ||
} | ||
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'); // 构造一个含有目标参数的正则表达式对象 | ||
queryOne: function queryOne(key) { | ||
var str = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var s = str ? '?' + this._getQueryFromUrl(str) : loc.search; | ||
var reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)'); // 构造一个含有目标参数的正则表达式对象 | ||
var r = s.substr(1).match(reg); // 匹配目标参数 | ||
if (r != null) { | ||
return decodeURIComponent(r[2]); | ||
return decodeURIComponent(r[2]).replace(/(#\w+)$/, ''); | ||
} | ||
return null; // 返回参数值 | ||
}, | ||
queryAll: function queryAll() { | ||
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
queryAll: function queryAll(str) { | ||
var s = ''; | ||
if (str) { | ||
s = str; | ||
} else { | ||
s = loc.search; | ||
} | ||
var s = str ? '?' + this._getQueryFromUrl(str) : loc.search; | ||
var search = s.substr(1); | ||
@@ -160,3 +153,3 @@ var a = search.split('&'); | ||
while (a[i]) { | ||
var kv = a[i].split('='); | ||
var kv = a[i].replace(/(#\w+)$/, '').split('='); | ||
result[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]); | ||
@@ -166,2 +159,77 @@ i++; | ||
return result; | ||
}, | ||
_makeUrl: function _makeUrl(url, search) { | ||
return url.indexOf('?') !== -1 ? url.replace(/\?([^#]*)/, '' + (search ? '?' + search : '')) : '' + url + (search ? '?' + search : ''); | ||
}, | ||
_getQueryFromUrl: function _getQueryFromUrl(url) { | ||
if (!url) { | ||
return ''; | ||
} | ||
if (url.indexOf('?') === -1) { | ||
return url; | ||
} | ||
var search = url.split('?')[1]; | ||
if (!search || search.indexOf('=') === -1) { | ||
throw new Error('not query format!'); | ||
} | ||
return search; | ||
}, | ||
update: function update(obj) { | ||
var str = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var query = this.queryAll(str); | ||
var result = []; | ||
Object.assign(query, obj); | ||
for (var key in query) { | ||
result.push(key + '=' + query[key]); | ||
} | ||
var search = result.join('&'); | ||
return { | ||
querys: query, | ||
search: str ? this._makeUrl(str, search) : '?' + search | ||
}; | ||
}, | ||
del: function del(keys) { | ||
var str = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
if (!keys) { | ||
return; | ||
} | ||
if (typeof keys === 'string') { | ||
keys = [keys]; | ||
} | ||
var query = this.queryAll(str); | ||
var keysObj = {}; | ||
var result = []; | ||
var newQuery = {}; | ||
keys.forEach(function (item) { | ||
keysObj[item] = true; | ||
}); | ||
for (var key in query) { | ||
var item = query[key]; | ||
if (!keysObj[key]) { | ||
newQuery[key] = item; | ||
result.push(key + '=' + item); | ||
} | ||
} | ||
var search = result.join('&'); | ||
return { | ||
querys: newQuery, | ||
search: str ? this._makeUrl(str, search) : '?' + search | ||
}; | ||
} | ||
@@ -168,0 +236,0 @@ }; |
@@ -1,15 +0,10 @@ | ||
let loc = window.location | ||
const loc = window.location | ||
let urlQuery = { | ||
queryOne: function (name, str) { | ||
let s = '' | ||
if (str) { | ||
s = str | ||
} else { | ||
s = loc.search | ||
} | ||
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)') // 构造一个含有目标参数的正则表达式对象 | ||
let r = s.substr(1).match(reg) // 匹配目标参数 | ||
const urlQuery = { | ||
queryOne (key, str = '') { | ||
const s = str ? `?${this._getQueryFromUrl(str)}` : loc.search | ||
const reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)') // 构造一个含有目标参数的正则表达式对象 | ||
const r = s.substr(1).match(reg) // 匹配目标参数 | ||
if (r != null) { | ||
return decodeURIComponent(r[2]) | ||
return decodeURIComponent(r[2]).replace(/(#\w+)$/, '') | ||
} | ||
@@ -19,11 +14,6 @@ return null // 返回参数值 | ||
queryAll: function (str) { | ||
let s = '' | ||
if (str) { | ||
s = str | ||
} else { | ||
s = loc.search | ||
} | ||
let search = s.substr(1) | ||
let a = search.split('&') | ||
queryAll (str = '') { | ||
const s = str ? `?${this._getQueryFromUrl(str)}` : loc.search | ||
const search = s.substr(1) | ||
const a = search.split('&') | ||
let i = 0 | ||
@@ -33,3 +23,3 @@ let result = {} | ||
while (a[i]) { | ||
let kv = a[i].split('=') | ||
let kv = a[i].replace(/(#\w+)$/, '').split('=') | ||
result[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]) | ||
@@ -39,2 +29,77 @@ i++ | ||
return result | ||
}, | ||
_makeUrl (url, search) { | ||
return url.indexOf('?') !== -1 ? url.replace(/\?([^#]*)/, `${search ? '?' + search : ''}`) : `${url}${search ? '?' + search : ''}` | ||
}, | ||
_getQueryFromUrl (url) { | ||
if (!url) { | ||
return '' | ||
} | ||
if (url.indexOf('?') === -1) { | ||
return url | ||
} | ||
const search = url.split('?')[1] | ||
if (!search || search.indexOf('=') === -1) { | ||
throw new Error('not query format!') | ||
} | ||
return search | ||
}, | ||
update (obj, str = '') { | ||
let query = this.queryAll(str) | ||
let result = [] | ||
Object.assign(query, obj) | ||
for (let key in query) { | ||
result.push(`${key}=${query[key]}`) | ||
} | ||
const search = result.join('&') | ||
return { | ||
querys: query, | ||
search: str ? this._makeUrl(str, search) : `?${search}` | ||
} | ||
}, | ||
del (keys, str = '') { | ||
if (!keys) { | ||
return | ||
} | ||
if (typeof keys === 'string') { | ||
keys = [keys] | ||
} | ||
const query = this.queryAll(str) | ||
let keysObj = {} | ||
let result = [] | ||
let newQuery = {} | ||
keys.forEach((item) => { | ||
keysObj[item] = true | ||
}) | ||
for (let key in query) { | ||
let item = query[key] | ||
if (!keysObj[key]) { | ||
newQuery[key] = item | ||
result.push(`${key}=${item}`) | ||
} | ||
} | ||
const search = result.join('&') | ||
return { | ||
querys: newQuery, | ||
search: str ? this._makeUrl(str, search) : `?${search}` | ||
} | ||
} | ||
@@ -41,0 +106,0 @@ } |
{ | ||
"name": "fe-utils", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "fe-utils.js", |
@@ -18,3 +18,3 @@ # fe-utils | ||
#### Usage: | ||
```js | ||
```javascript | ||
// Load method categories. | ||
@@ -28,31 +28,89 @@ var cookie = require('fe-utils/cookie') | ||
1. **NumberToChinese** | ||
* ```NumberToChinese(12345) // '一万二千三百四十五'``` | ||
* ```NumberToChinese(12345, true) // '壹万贰仟叁佰肆十伍'``` | ||
```javascript | ||
NumberToChinese(12345) // '一万二千三百四十五' | ||
NumberToChinese(12345, true) // '壹万贰仟叁佰肆十伍' | ||
``` | ||
2. **cookie** | ||
* ```setCookie(name, domain, domain, expiredays)``` | ||
* ```getCookie(name)``` | ||
* ```delCookie(name, domain)``` | ||
```javascript | ||
setCookie(name, domain, domain, expiredays) | ||
getCookie(name) | ||
delCookie(name, domain) | ||
``` | ||
3. **date** | ||
* ```dateFormat() // dateFormat(new Date(), 'YYYY年MM月DD日 hh:mm:ss')``` | ||
```javascript | ||
dateFormat() // dateFormat(new Date(), 'YYYY年MM月DD日 hh:mm:ss') | ||
``` | ||
4. **detectedInfos** | ||
* isAndroid / isIOS / isWindowPhone / isWX / isWeibo / isMobile / isPC | ||
```javascript | ||
isAndroid / isIOS / isWindowPhone / isWX / isWeibo / isMobile / isPC | ||
``` | ||
5. **preventViewScroll** | ||
* <https://github.com/lzwaiwai/preventViewScroll> | ||
<https://github.com/lzwaiwai/preventViewScroll> | ||
6. **urlQuery** | ||
* ```queryOne // queryOne('name', ['http://xx.com?name=kevin']) -> 'kevin'``` | ||
* ```queryAll // queryOne('http://xx.com?name=kevin&age=18') -> {name: 'kevin', age: 18}``` | ||
```javascript | ||
const url = 'http://live.tinfinite.com:8080/path/chat-studio.html?liveshowId=592e4e2bf84e6e26c1cf97dd&group=live#123' | ||
queryOne | ||
// queryOne('liveshowId', url) -> '592e4e2bf84e6e26c1cf97dd' | ||
queryAll | ||
// queryAll(url) -> { liveshowId: '592e4e2bf84e6e26c1cf97dd', group: 'live' } | ||
update | ||
// update({ group: 'voice', referer: 'list' }, url) -> | ||
// { | ||
query: { | ||
liveshowId: '592e4e2bf84e6e26c1cf97dd', | ||
group: 'voice', | ||
referer: 'list' | ||
}, | ||
search: 'http://live.tinfinite.com:8080/path/chat-studio.html?liveshowId=592e4e2bf84e6e26c1cf97dd&group=voice&referer=list#123' | ||
} | ||
del | ||
// del('liveshowId', url) -> | ||
// { | ||
query: { | ||
group: 'live' | ||
}, | ||
search: 'http://live.tinfinite.com:8080/path/chat-studio.html?group=live#123' | ||
} | ||
// del(['liveshowId', 'group'], url) -> | ||
// { | ||
query: {}, | ||
search: 'http://live.tinfinite.com:8080/path/chat-studio.html#123' | ||
} | ||
``` | ||
7. **toRoman** | ||
* ```toRoman(25) // 'XXV'``` | ||
```javascript | ||
toRoman(25) // 'XXV' | ||
``` | ||
8. **getVersionOfIOS** | ||
* ```getVersionOfIOS() // '10.3'``` | ||
```javascript | ||
getVersionOfIOS() // '10.3' | ||
``` | ||
9. **relProtocol** | ||
* ```relProtocol('http://xxx.com') // '//xxx.com'``` | ||
```javascript | ||
relProtocol('http://xxx.com') // '//xxx.com' | ||
``` | ||
9. **logger** | ||
* <img src="http://o4a7cbihz.qnssl.com/cover/4ec91a4e-e638-4753-9f6a-9cee4e715420" width = "250" align="center" /> | ||
<img src="http://o4a7cbihz.qnssl.com/cover/4ec91a4e-e638-4753-9f6a-9cee4e715420" width = "250" align="center" /> | ||
10. **mediaError** | ||
* ```mediaError(video / audio, (code, msg) => { ... }) // for error-listener-callback of audio and video ``` | ||
```javascript | ||
mediaError(video / audio, (code, msg) => { ... }) // for error-listener-callback of audio and video | ||
``` | ||
11. **loadJs** | ||
* ```loadJs(url, callback)``` | ||
```javascript | ||
loadJs(url, callback) | ||
``` | ||
---- | ||
### Thank to [A-Dan](https://github.com/oneMoreTime1357), [shaoyishou](https://github.com/shaoyishou), [Div](https://github.com/div-wang) |
102
urlQuery.js
var loc = window.location; | ||
var urlQuery = { | ||
queryOne: function queryOne(name, str) { | ||
var s = ''; | ||
if (str) { | ||
s = str; | ||
} else { | ||
s = loc.search; | ||
} | ||
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'); // 构造一个含有目标参数的正则表达式对象 | ||
queryOne: function queryOne(key) { | ||
var str = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var s = str ? '?' + this._getQueryFromUrl(str) : loc.search; | ||
var reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)'); // 构造一个含有目标参数的正则表达式对象 | ||
var r = s.substr(1).match(reg); // 匹配目标参数 | ||
if (r != null) { | ||
return decodeURIComponent(r[2]); | ||
return decodeURIComponent(r[2]).replace(/(#\w+)$/, ''); | ||
} | ||
return null; // 返回参数值 | ||
}, | ||
queryAll: function queryAll() { | ||
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
queryAll: function queryAll(str) { | ||
var s = ''; | ||
if (str) { | ||
s = str; | ||
} else { | ||
s = loc.search; | ||
} | ||
var s = str ? '?' + this._getQueryFromUrl(str) : loc.search; | ||
var search = s.substr(1); | ||
@@ -32,3 +25,3 @@ var a = search.split('&'); | ||
while (a[i]) { | ||
var kv = a[i].split('='); | ||
var kv = a[i].replace(/(#\w+)$/, '').split('='); | ||
result[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]); | ||
@@ -38,2 +31,77 @@ i++; | ||
return result; | ||
}, | ||
_makeUrl: function _makeUrl(url, search) { | ||
return url.indexOf('?') !== -1 ? url.replace(/\?([^#]*)/, '' + (search ? '?' + search : '')) : '' + url + (search ? '?' + search : ''); | ||
}, | ||
_getQueryFromUrl: function _getQueryFromUrl(url) { | ||
if (!url) { | ||
return ''; | ||
} | ||
if (url.indexOf('?') === -1) { | ||
return url; | ||
} | ||
var search = url.split('?')[1]; | ||
if (!search || search.indexOf('=') === -1) { | ||
throw new Error('not query format!'); | ||
} | ||
return search; | ||
}, | ||
update: function update(obj) { | ||
var str = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var query = this.queryAll(str); | ||
var result = []; | ||
Object.assign(query, obj); | ||
for (var key in query) { | ||
result.push(key + '=' + query[key]); | ||
} | ||
var search = result.join('&'); | ||
return { | ||
querys: query, | ||
search: str ? this._makeUrl(str, search) : '?' + search | ||
}; | ||
}, | ||
del: function del(keys) { | ||
var str = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
if (!keys) { | ||
return; | ||
} | ||
if (typeof keys === 'string') { | ||
keys = [keys]; | ||
} | ||
var query = this.queryAll(str); | ||
var keysObj = {}; | ||
var result = []; | ||
var newQuery = {}; | ||
keys.forEach(function (item) { | ||
keysObj[item] = true; | ||
}); | ||
for (var key in query) { | ||
var item = query[key]; | ||
if (!keysObj[key]) { | ||
newQuery[key] = item; | ||
result.push(key + '=' + item); | ||
} | ||
} | ||
var search = result.join('&'); | ||
return { | ||
querys: newQuery, | ||
search: str ? this._makeUrl(str, search) : '?' + search | ||
}; | ||
} | ||
@@ -40,0 +108,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49171
1358
115