前端常用工具方法

1 minute read

storage.js

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

RegUtils.js

 1const  regCommon=/^[0-9A-Za-z\u4e00-\u9fa5_\-]+$/;
 2const  regEmail=/^[a-z0-9A-Z]+[- |a-z0-9A-Z._]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-z]{2,}$/;
 3const  regCode=/^[0-9A-Za-z]+$/;
 4const  regPhone=/^1(3|4|5|6|7|8|9)\d{9}$/;
 5
 6export function isCommonText(text:string) {
 7    // if(!text) return  true;
 8    // return regCommon.test(text);
 9    return true;
10}
11
12export function isEmail(email:string) {
13    if(!email) return  true;
14    return regEmail.test(email);
15}
16export function isCode(code:string) {
17    if(!code) return  true;
18    return regCode.test(code);
19}
20export function isPhone(phone:string) {
21    return regPhone.test(phone);
22}