1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- /**
- * 随机选取数组中的一个值
- */
- function randomSelectOne(arr) {
- return arr[Math.floor(Math.random()*(arr.length -1))];
- }
- /**
- * 随机选取数组中的N个值
- */
- function randomSelectN(arr) {
- let MAX = Math.floor(Math.random()*(arr.length - 1));
- return arr.slice(0, MAX);
- }
- /**
- * 解析url中参数
- */
- export function getUrlParams(url) {
- const d = decodeURIComponent;
- let queryString = url ? url.split('?')[1] : window.location.search.slice(1);
- const obj = {};
- if (queryString) {
- queryString = queryString.split('#')[0]; // eslint-disable-line
- const arr = queryString.split('&');
- for (let i = 0; i < arr.length; i += 1) {
- const a = arr[i].split('=');
- let paramNum;
- const paramName = a[0].replace(/\[\d*\]/, (v) => {
- paramNum = v.slice(1, -1);
- return '';
- });
- const paramValue = typeof (a[1]) === 'undefined' ? true : a[1];
- if (obj[paramName]) {
- if (typeof obj[paramName] === 'string') {
- obj[paramName] = d([obj[paramName]]);
- }
- if (typeof paramNum === 'undefined') {
- obj[paramName].push(d(paramValue));
- } else {
- obj[paramName][paramNum] = d(paramValue);
- }
- } else {
- obj[paramName] = d(paramValue);
- }
- }
- }
- return obj;
- }
- module.exports = {
- randomSelectOne,
- randomSelectN,
- getUrlParams,
- }
|