From 164d8c768d365f8e1fb51ad0ab2af1696ecde9f1 Mon Sep 17 00:00:00 2001 From: mantou <676015863@qq.com> Date: Fri, 13 Apr 2018 10:00:32 +0800 Subject: [PATCH 1/2] Update parseQueryString.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当url不传,或者没有参数的时候,或参数错误的时候,这个方法没法处理。这里对其进行更新! --- parseQueryString.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) 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 From 8be389ac1cae16fc4d06f0011a604d930b47acec Mon Sep 17 00:00:00 2001 From: mantou <676015863@qq.com> Date: Thu, 26 Mar 2020 12:02:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E9=A2=84=E5=8A=A0=E8=BD=BD=EF=BC=8C=E9=9A=8F=E6=9C=BAID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dom/imgLazy.js | 19 +++++++++++++++++++ src/index.js | 4 ++++ src/random/randomID.js | 13 +++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/dom/imgLazy.js create mode 100644 src/random/randomID.js 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