|
@@ -1,11 +1,11 @@
|
|
|
-import { CommonValue } from '~/common'
|
|
|
+import { CommonValue } from "~/common";
|
|
|
|
|
|
/**
|
|
|
* @param {string} path
|
|
|
* @returns {Boolean}
|
|
|
*/
|
|
|
export function isExternal(path: string): boolean {
|
|
|
- return /^(https?:|mailto:|tel:)/.test(path)
|
|
|
+ return /^(https?:|mailto:|tel:)/.test(path);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -13,8 +13,8 @@ export function isExternal(path: string): boolean {
|
|
|
* @returns {Boolean}
|
|
|
*/
|
|
|
export function validUsername(str: string): boolean {
|
|
|
- const valid_map = ['admin', 'editor']
|
|
|
- return valid_map.indexOf(str.trim()) >= 0
|
|
|
+ const valid_map = ["admin", "editor"];
|
|
|
+ return valid_map.indexOf(str.trim()) >= 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -22,8 +22,8 @@ export function validUsername(str: string): boolean {
|
|
|
* @returns {Boolean}
|
|
|
*/
|
|
|
export function validURL(url: string): boolean {
|
|
|
- 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)
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -31,8 +31,8 @@ export function validURL(url: string): boolean {
|
|
|
* @returns {Boolean}
|
|
|
*/
|
|
|
export function validLowerCase(str: string): boolean {
|
|
|
- const reg = /^[a-z]+$/
|
|
|
- return reg.test(str)
|
|
|
+ const reg = /^[a-z]+$/;
|
|
|
+ return reg.test(str);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -40,8 +40,8 @@ export function validLowerCase(str: string): boolean {
|
|
|
* @returns {Boolean}
|
|
|
*/
|
|
|
export function validUpperCase(str: string): boolean {
|
|
|
- const reg = /^[A-Z]+$/
|
|
|
- return reg.test(str)
|
|
|
+ const reg = /^[A-Z]+$/;
|
|
|
+ return reg.test(str);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -49,8 +49,8 @@ export function validUpperCase(str: string): boolean {
|
|
|
* @returns {Boolean}
|
|
|
*/
|
|
|
export function validAlphabets(str: string): boolean {
|
|
|
- const reg = /^[A-Za-z]+$/
|
|
|
- return reg.test(str)
|
|
|
+ const reg = /^[A-Za-z]+$/;
|
|
|
+ return reg.test(str);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -67,7 +67,7 @@ export function validAlphabets(str: string): boolean {
|
|
|
* @returns {Boolean}
|
|
|
*/
|
|
|
export function isString(str: any): boolean {
|
|
|
- return typeof str === 'string' || str instanceof String
|
|
|
+ return typeof str === "string" || str instanceof String;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -76,12 +76,12 @@ export function isString(str: any): boolean {
|
|
|
*/
|
|
|
export function isValue(arg: any[]) {
|
|
|
if (Array.isArray(arg)) {
|
|
|
- return arg.length
|
|
|
+ return arg.length;
|
|
|
} else {
|
|
|
- if (typeof arg === 'object') {
|
|
|
- return Object.keys(arg).length
|
|
|
+ if (typeof arg === "object") {
|
|
|
+ return Object.keys(arg).length;
|
|
|
} else {
|
|
|
- return false
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -94,28 +94,28 @@ export function isValue(arg: any[]) {
|
|
|
*/
|
|
|
export function parseTime(time: any, cFormat: string) {
|
|
|
if (arguments.length === 0 || !time) {
|
|
|
- return null
|
|
|
+ return null;
|
|
|
}
|
|
|
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
|
- let date
|
|
|
- if (typeof time === 'object') {
|
|
|
- date = time
|
|
|
+ const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
|
|
|
+ let date;
|
|
|
+ if (typeof time === "object") {
|
|
|
+ date = time;
|
|
|
} else {
|
|
|
- if (typeof time === 'string') {
|
|
|
+ if (typeof time === "string") {
|
|
|
if (/^[0-9]+$/.test(time)) {
|
|
|
// support "1548221490638"
|
|
|
- time = parseInt(time)
|
|
|
+ time = parseInt(time);
|
|
|
} else {
|
|
|
// support safari
|
|
|
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
|
|
|
- time = time.replace(new RegExp(/-/gm), '/')
|
|
|
+ time = time.replace(new RegExp(/-/gm), "/");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (typeof time === 'number' && time.toString().length === 10) {
|
|
|
- time = time * 1000
|
|
|
+ if (typeof time === "number" && time.toString().length === 10) {
|
|
|
+ time = time * 1000;
|
|
|
}
|
|
|
- date = new Date(time)
|
|
|
+ date = new Date(time);
|
|
|
}
|
|
|
const formatObj = {
|
|
|
y: date.getFullYear(),
|
|
@@ -125,68 +125,95 @@ export function parseTime(time: any, cFormat: string) {
|
|
|
i: date.getMinutes(),
|
|
|
s: date.getSeconds(),
|
|
|
a: date.getDay(),
|
|
|
- }
|
|
|
+ };
|
|
|
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
|
|
- const value = formatObj[key]
|
|
|
+ const value = formatObj[key];
|
|
|
// Note: getDay() returns 0 on Sunday
|
|
|
- if (key === 'a') {
|
|
|
- return ['日', '一', '二', '三', '四', '五', '六'][value]
|
|
|
+ if (key === "a") {
|
|
|
+ return ["日", "一", "二", "三", "四", "五", "六"][value];
|
|
|
}
|
|
|
- return value.toString().padStart(2, '0')
|
|
|
- })
|
|
|
- return time_str
|
|
|
+ return value.toString().padStart(2, "0");
|
|
|
+ });
|
|
|
+ return time_str;
|
|
|
}
|
|
|
|
|
|
export function translateDataToTreeAll(arr, parentKey, key) {
|
|
|
- const map = {}
|
|
|
- const result: any[] = []
|
|
|
- arr.forEach(element => {
|
|
|
- const id = element[key]
|
|
|
- const pid = element[parentKey]
|
|
|
+ const map = {};
|
|
|
+ const result: any[] = [];
|
|
|
+ arr.forEach((element) => {
|
|
|
+ const id = element[key];
|
|
|
+ const pid = element[parentKey];
|
|
|
if (map[id]) {
|
|
|
map[id] = {
|
|
|
...element,
|
|
|
children: map[id].children,
|
|
|
- }
|
|
|
+ };
|
|
|
} else {
|
|
|
map[id] = {
|
|
|
...element,
|
|
|
children: [],
|
|
|
- }
|
|
|
+ };
|
|
|
}
|
|
|
- const item = map[id]
|
|
|
+ const item = map[id];
|
|
|
if (pid <= 0) {
|
|
|
- result.push(item)
|
|
|
+ result.push(item);
|
|
|
} else {
|
|
|
if (map[pid]) {
|
|
|
- map[pid].children.push(item)
|
|
|
+ map[pid].children.push(item);
|
|
|
} else {
|
|
|
map[pid] = {
|
|
|
children: [item],
|
|
|
- }
|
|
|
+ };
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
- return result
|
|
|
+ });
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
export function datetimeToTime(
|
|
|
datetimeValue: CommonValue,
|
|
|
currentDate: CommonValue
|
|
|
) {
|
|
|
- if (typeof datetimeValue !== 'string' || !datetimeValue) {
|
|
|
- return ''
|
|
|
+ if (typeof datetimeValue !== "string" || !datetimeValue) {
|
|
|
+ return "";
|
|
|
}
|
|
|
- if (typeof currentDate !== 'string') {
|
|
|
- return ''
|
|
|
+ if (typeof currentDate !== "string") {
|
|
|
+ return "";
|
|
|
}
|
|
|
- const [date, time] = datetimeValue.trim().split(/[T|\s]+/)
|
|
|
- let clipTime = time.slice(0, -3)
|
|
|
+ const [date, time] = datetimeValue.trim().split(/[T|\s]+/);
|
|
|
+ let clipTime = time.slice(0, -3);
|
|
|
if (date !== currentDate) {
|
|
|
const days =
|
|
|
(new Date(date).getTime() - new Date(currentDate).getTime()) /
|
|
|
- (24 * 60 * 60 * 1000)
|
|
|
- clipTime += ` (${days > 0 ? '+' : ''}${days})`
|
|
|
+ (24 * 60 * 60 * 1000);
|
|
|
+ clipTime += ` (${days > 0 ? "+" : ""}${days})`;
|
|
|
}
|
|
|
- return clipTime
|
|
|
+ return clipTime;
|
|
|
}
|
|
|
+
|
|
|
+export const Format = (fmt, date) => {
|
|
|
+ let o = {
|
|
|
+ "M+": date.getMonth() + 1, // 月份
|
|
|
+ "d+": date.getDate(), // 日
|
|
|
+ "h+": date.getHours(), // 小时
|
|
|
+ "m+": date.getMinutes(), // 分
|
|
|
+ "s+": date.getSeconds(), // 秒
|
|
|
+ "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
|
|
|
+ S: date.getMilliseconds(), // 毫秒
|
|
|
+ };
|
|
|
+ if (/(y+)/.test(fmt)) {
|
|
|
+ fmt = fmt.replace(
|
|
|
+ RegExp.$1,
|
|
|
+ (date.getFullYear() + "").substr(4 - RegExp.$1.length)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ for (var k in o) {
|
|
|
+ if (new RegExp("(" + k + ")").test(fmt)) {
|
|
|
+ fmt = fmt.replace(
|
|
|
+ RegExp.$1,
|
|
|
+ RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fmt;
|
|
|
+};
|