2023-09-27 08:54:52 +08:00
|
|
|
import { MAX_VAR_KEY_LENGHT, VAR_ITEM_TEMPLATE, getMaxVarNameLength } from '@/config'
|
|
|
|
const otherAllowedRegex = /^[a-zA-Z0-9_]+$/
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
export const getNewVar = (key: string) => {
|
|
|
|
return {
|
|
|
|
...VAR_ITEM_TEMPLATE,
|
|
|
|
key,
|
|
|
|
name: key.slice(0, getMaxVarNameLength(key)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkKey = (key: string, canBeEmpty?: boolean) => {
|
2023-09-27 08:54:52 +08:00
|
|
|
if (key.length === 0 && !canBeEmpty)
|
2023-05-15 08:51:32 +08:00
|
|
|
return 'canNoBeEmpty'
|
2023-09-27 08:54:52 +08:00
|
|
|
|
|
|
|
if (canBeEmpty && key === '')
|
2023-05-15 08:51:32 +08:00
|
|
|
return true
|
2023-09-27 08:54:52 +08:00
|
|
|
|
|
|
|
if (key.length > MAX_VAR_KEY_LENGHT)
|
2023-05-15 08:51:32 +08:00
|
|
|
return 'tooLong'
|
2023-09-27 08:54:52 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
if (otherAllowedRegex.test(key)) {
|
2023-09-27 08:54:52 +08:00
|
|
|
if (/[0-9]/.test(key[0]))
|
2023-05-15 08:51:32 +08:00
|
|
|
return 'notStartWithNumber'
|
2023-09-27 08:54:52 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return 'notValid'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
|
|
|
|
let isValid = true
|
|
|
|
let errorKey = ''
|
|
|
|
let errorMessageKey = ''
|
|
|
|
keys.forEach((key) => {
|
2023-09-27 08:54:52 +08:00
|
|
|
if (!isValid)
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2023-09-27 08:54:52 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
const res = checkKey(key, canBeEmpty)
|
|
|
|
if (res !== true) {
|
|
|
|
isValid = false
|
|
|
|
errorKey = key
|
|
|
|
errorMessageKey = res
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return { isValid, errorKey, errorMessageKey }
|
2023-09-27 08:54:52 +08:00
|
|
|
}
|