Featured image of post 前端常用工具方法

前端常用工具方法

storage.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
};

/**
 * 获取localStorage
 */
export const getStore = (name) => {
  if (!name) return;
  return window.localStorage.getItem(name);
};

/**
 * 删除localStorage
 */
export const removeStore = (name) => {
  if (!name) return;
  window.localStorage.removeItem(name);
};

RegUtils.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
const  regCommon=/^[0-9A-Za-z\u4e00-\u9fa5_\-]+$/;
const  regEmail=/^[a-z0-9A-Z]+[- |a-z0-9A-Z._]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-z]{2,}$/;
const  regCode=/^[0-9A-Za-z]+$/;
const  regPhone=/^1(3|4|5|6|7|8|9)\d{9}$/;

export function isCommonText(text:string) {
    // if(!text) return  true;
    // return regCommon.test(text);
    return true;
}

export function isEmail(email:string) {
    if(!email) return  true;
    return regEmail.test(email);
}
export function isCode(code:string) {
    if(!code) return  true;
    return regCode.test(code);
}
export function isPhone(phone:string) {
    return regPhone.test(phone);
}
Licensed under CC BY-NC-SA 4.0