Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions parseQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
module.exports = parseQueryString
19 changes: 19 additions & 0 deletions src/dom/imgLazy.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -66,6 +68,7 @@ module.exports = {
scrollTo,
setScrollTop,
windowResize,
imgLazy,

debounce,
throttle,
Expand All @@ -77,6 +80,7 @@ module.exports = {

randomColor,
randomNum,
randomID,

isEmail,
isIdCard,
Expand Down
13 changes: 13 additions & 0 deletions src/random/randomID.js
Original file line number Diff line number Diff line change
@@ -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;