From 5224a00ddc1705e9d243bf5ed8f4f38f555d7ac4 Mon Sep 17 00:00:00 2001 From: Pan Date: Tue, 19 Feb 2019 10:24:22 +0800 Subject: [PATCH] perf[utils]: optimizate variable name --- src/utils/createUniqueString.js | 8 ------- src/utils/index.js | 8 ++++++- src/utils/validate.js | 22 +++++++++---------- .../example/components/ArticleDetail.vue | 4 ++-- 4 files changed, 20 insertions(+), 22 deletions(-) delete mode 100644 src/utils/createUniqueString.js diff --git a/src/utils/createUniqueString.js b/src/utils/createUniqueString.js deleted file mode 100644 index 611725c4..00000000 --- a/src/utils/createUniqueString.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Created by jiachenpan on 17/3/8. - */ -export default function createUniqueString() { - const timestamp = +new Date() + '' - const randomNum = parseInt((1 + Math.random()) * 65536) + '' - return (+(randomNum + timestamp)).toString(32) -} diff --git a/src/utils/index.js b/src/utils/index.js index d5c248fe..423672c6 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -243,7 +243,7 @@ export function debounce(func, wait, immediate) { // 据上一次触发时间间隔 const last = +new Date() - timestamp - // 上次被包装函数被调用时间间隔last小于设定时间间隔wait + // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait if (last < wait && last > 0) { timeout = setTimeout(later, wait - last) } else { @@ -294,3 +294,9 @@ export function deepClone(source) { export function uniqueArr(arr) { return Array.from(new Set(arr)) } + +export function createUniqueString() { + const timestamp = +new Date() + '' + const randomNum = parseInt((1 + Math.random()) * 65536) + '' + return (+(randomNum + timestamp)).toString(32) +} diff --git a/src/utils/validate.js b/src/utils/validate.js index 8b01aa14..5e4056f5 100644 --- a/src/utils/validate.js +++ b/src/utils/validate.js @@ -2,35 +2,35 @@ * Created by jiachenpan on 16/11/18. */ +export function isExternal(path) { + return /^(https?:|mailto:|tel:)/.test(path) +} + export function validUsername(str) { const valid_map = ['admin', 'editor'] return valid_map.indexOf(str.trim()) >= 0 } -export function isExternal(path) { - return /^(https?:|mailto:|tel:)/.test(path) -} - /* 合法uri*/ -export function validateURL(textval) { - const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ - return urlregex.test(textval) +export function validURL(url) { + const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ + return reg.test(url) } /* 小写字母*/ -export function validateLowerCase(str) { +export function validLowerCase(str) { const reg = /^[a-z]+$/ return reg.test(str) } /* 大写字母*/ -export function validateUpperCase(str) { +export function validUpperCase(str) { const reg = /^[A-Z]+$/ return reg.test(str) } /* 大小写字母*/ -export function validateAlphabets(str) { +export function validAlphabets(str) { const reg = /^[A-Za-z]+$/ return reg.test(str) } @@ -40,7 +40,7 @@ export function validateAlphabets(str) { * @param email * @returns {boolean} */ -export function validateEmail(email) { +export function validEmail(email) { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ return re.test(email) } diff --git a/src/views/example/components/ArticleDetail.vue b/src/views/example/components/ArticleDetail.vue index ce22b3b4..347b1345 100644 --- a/src/views/example/components/ArticleDetail.vue +++ b/src/views/example/components/ArticleDetail.vue @@ -78,7 +78,7 @@ import Tinymce from '@/components/Tinymce' import Upload from '@/components/Upload/singleImage3' import MDinput from '@/components/MDinput' import Sticky from '@/components/Sticky' // 粘性header组件 -import { validateURL } from '@/utils/validate' +import { validURL } from '@/utils/validate' import { fetchArticle } from '@/api/article' import { userSearch } from '@/api/remoteSearch' import Warning from './Warning' @@ -121,7 +121,7 @@ export default { } const validateSourceUri = (rule, value, callback) => { if (value) { - if (validateURL(value)) { + if (validURL(value)) { callback() } else { this.$message({