diff --git a/parseQueryString.js b/parseQueryString.js index 35e4882..7f37d8e 100644 --- a/parseQueryString.js +++ b/parseQueryString.js @@ -5,12 +5,28 @@ * @return {Object} */ function parseQueryString(url) { - url = url == null ? window.location.href : url - var search = url.substring(url.lastIndexOf('?') + 1) + url = (!url ? window.location.href : url); + if(url.indexOf('?') === -1) { + return {}; + } + var search = url.substring(url.lastIndexOf('?') + 1); if (!search) { - return {} + return {}; + } + var data = {}; + try{ + data = JSON.parse( + '{"' + + decodeURIComponent(search) + .replace(/"/g, '\\"') + .replace(/&/g, '","') + .replace(/=/g, '":"') + + '"}' + ); + } catch (e) { + console.error('数据格式错误'); } - return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}') + return data; } -module.exports = parseQueryString \ No newline at end of file +module.exports = parseQueryString diff --git a/src/dom/imgLazy.js b/src/dom/imgLazy.js new file mode 100644 index 0000000..b427641 --- /dev/null +++ b/src/dom/imgLazy.js @@ -0,0 +1,19 @@ +/** + * @desc 图片预加载 + * @param {String} 预加载src + * @return {Promise} + */ +function imgLazy(src) { + return new Promise((resolve, reject) => { + var img = new Image(); + img.src = src; + img.onload = function() { + resolve(img); + }; + img.onerror = function() { + console.error("图片加载失败", src); + reject(false); + }; + }); +} +module.exports = imgLazy; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 64b2546..aa20042 100644 --- a/src/index.js +++ b/src/index.js @@ -19,6 +19,7 @@ const offset = require('./dom/offset') const scrollTo = require('./dom/scrollTo') const setScrollTop = require('./dom/setScrollTop') const windowResize = require('./dom/windowResize') +const imgLazy = require('./dom/imgLazy') const debounce = require('./function/debounce') const throttle = require('./function/throttle') @@ -30,6 +31,7 @@ const isEmptyObject = require('./object/isEmptyObject') const randomColor = require('./random/randomColor') const randomNum = require('./random/randomNum') +const randomID = require('./random/randomID') const isEmail = require('./regexp/isEmail') const isIdCard = require('./regexp/isIdCard') @@ -66,6 +68,7 @@ module.exports = { scrollTo, setScrollTop, windowResize, + imgLazy, debounce, throttle, @@ -77,6 +80,7 @@ module.exports = { randomColor, randomNum, + randomID, isEmail, isIdCard, diff --git a/src/random/randomID.js b/src/random/randomID.js new file mode 100644 index 0000000..2e556d9 --- /dev/null +++ b/src/random/randomID.js @@ -0,0 +1,13 @@ +/** + * 生成随机ID + * @param {Number} randomLength 长度 + * @return {String} + */ +function randomID(randomLength = 8) { + return Number( + Math.random() + .toString() + .substr(3, randomLength) + Date.now() + ).toString(36); +} +module.exports = randomID; \ No newline at end of file