Initial commit

This commit is contained in:
John Wang
2023-05-15 08:51:32 +08:00
commit db896255d6
744 changed files with 56028 additions and 0 deletions

33
web/utils/format.ts Normal file
View File

@@ -0,0 +1,33 @@
/*
* Formats a number with comma separators.
formatNumber(1234567) will return '1,234,567'
formatNumber(1234567.89) will return '1,234,567.89'
*/
export const formatNumber = (num: number | string) => {
if (!num) return num;
let parts = num.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
export const formatFileSize = (num: number) => {
if (!num) return num;
const units = ['', 'K', 'M', 'G', 'T', 'P'];
let index = 0;
while (num >= 1024 && index < units.length) {
num = num / 1024;
index++;
}
return num.toFixed(2) + `${units[index]}B`;
}
export const formatTime = (num: number) => {
if (!num) return num;
const units = ['sec', 'min', 'h'];
let index = 0;
while (num >= 60 && index < units.length) {
num = num / 60;
index++;
}
return `${num.toFixed(2)} ${units[index]}`;
}

24
web/utils/index.ts Normal file
View File

@@ -0,0 +1,24 @@
export const sleep = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
export async function asyncRunSafe<T = any>(fn: Promise<T>): Promise<[Error] | [null, T]> {
try {
return [null, await fn]
}
catch (e) {
if (e instanceof Error)
return [e]
return [new Error('unknown error')]
}
}
export const getTextWidthWithCanvas = (text: string, font?: string) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (ctx) {
ctx.font = font ?? '12px Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"';
return Number(ctx.measureText(text).width.toFixed(2));
}
return 0;
}

19
web/utils/language.ts Normal file
View File

@@ -0,0 +1,19 @@
type Item = {
value: number | string
name: string
}
export const languages: Item[] = [
{
value: 'en-US',
name: 'English(United States)',
},
{
value: 'zh-Hans',
name: '简体中文',
},
]
export const languageMaps = {
'en': 'en-US',
'zh-Hans': 'zh-Hans',
}

63
web/utils/model-config.ts Normal file
View File

@@ -0,0 +1,63 @@
import { UserInputFormItem, } from '@/types/app'
import { PromptVariable } from '@/models/debug'
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
if (!useInputs) return []
const promptVariables: PromptVariable[] = []
useInputs.forEach((item: any) => {
const type = item['text-input'] ? 'string' : 'select'
const content = type === 'string' ? item['text-input'] : item['select']
if (type === 'string') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: 'string',
max_length: content.max_length,
options: [],
})
} else {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type: 'select',
options: content.options,
})
}
})
return promptVariables
}
export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
const userInputs: UserInputFormItem[] = []
promptVariables.filter(({ key, name }) => {
if (key && key.trim() && name && name.trim()) {
return true
}
return false
}).forEach((item: any) => {
if (item.type === 'string') {
userInputs.push({
'text-input': {
label: item.name,
variable: item.key,
required: item.required === false ? false : true, // default true
max_length: item.max_length,
default: ''
},
} as any)
} else {
userInputs.push({
'select': {
label: item.name,
variable: item.key,
required: item.required === false ? false : true, // default true
options: item.options,
default: ''
},
} as any)
}
})
return userInputs
}

330
web/utils/timezone.ts Normal file
View File

@@ -0,0 +1,330 @@
type Item = {
value: number | string
name: string
}
export const timezones: Item[] = [
{
value: 'Pacific/Midway',
name: '(GMT-11:00) Midway Island, Samoa',
},
{
value: 'Pacific/Honolulu',
name: '(GMT-10:00) Hawaii',
},
{
value: 'America/Juneau',
name: '(GMT-8:00) Alaska',
},
{
value: 'America/Dawson',
name: '(GMT-7:00) Dawson, Yukon',
},
{
value: 'America/Chihuahua',
name: '(GMT-7:00) Chihuahua, La Paz, Mazatlan',
},
{
value: 'America/Phoenix',
name: '(GMT-7:00) Arizona',
},
{
value: 'America/Tijuana',
name: '(GMT-7:00) Tijuana',
},
{
value: 'America/Los_Angeles',
name: '(GMT-7:00) Pacific Time',
},
{
value: 'America/Boise',
name: '(GMT-6:00) Mountain Time',
},
{
value: 'America/Regina',
name: '(GMT-6:00) Saskatchewan',
},
{
value: 'America/Mexico_City',
name: '(GMT-6:00) Guadalajara, Mexico City, Monterrey',
},
{
value: 'America/Belize',
name: '(GMT-6:00) Central America',
},
{
value: 'America/Chicago',
name: '(GMT-5:00) Central Time',
},
{
value: 'America/Bogota',
name: '(GMT-5:00) Bogota, Lima, Quito',
},
{
value: 'America/Lima',
name: '(GMT-5:00) Pittsburgh',
},
{
value: 'America/Detroit',
name: '(GMT-4:00) Eastern Time',
},
{
value: 'America/Caracas',
name: '(GMT-4:00) Caracas, La Paz',
},
{
value: 'America/Santiago',
name: '(GMT-3:00) Santiago',
},
{
value: 'America/Sao_Paulo',
name: '(GMT-3:00) Brasilia',
},
{
value: 'America/Montevideo',
name: '(GMT-3:00) Montevideo',
},
{
value: 'America/Argentina/Buenos_Aires',
name: '(GMT-3:00) Buenos Aires, Georgetown',
},
{
value: 'America/St_Johns',
name: '(GMT-2:30) Newfoundland and Labrador',
},
{
value: 'America/Godthab',
name: '(GMT-2:00) Greenland',
},
{
value: 'Atlantic/Cape_Verde',
name: '(GMT-1:00) Cape Verde Islands',
},
{
value: 'Atlantic/Azores',
name: '(GMT+0:00) Azores',
},
{
value: 'Etc/GMT',
name: '(GMT+0:00) UTC',
},
{
value: 'Africa/Casablanca',
name: '(GMT+0:00) Casablanca, Monrovia',
},
{
value: 'Europe/London',
name: '(GMT+1:00) Edinburgh, London',
},
{
value: 'Europe/Dublin',
name: '(GMT+1:00) Dublin',
},
{
value: 'Europe/Lisbon',
name: '(GMT+1:00) Lisbon',
},
{
value: 'Atlantic/Canary',
name: '(GMT+1:00) Canary Islands',
},
{
value: 'Africa/Algiers',
name: '(GMT+1:00) West Central Africa',
},
{
value: 'Europe/Belgrade',
name: '(GMT+2:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague',
},
{
value: 'Europe/Sarajevo',
name: '(GMT+2:00) Sarajevo, Skopje, Warsaw, Zagreb',
},
{
value: 'Europe/Brussels',
name: '(GMT+2:00) Brussels, Copenhagen, Madrid, Paris',
},
{
value: 'Europe/Amsterdam',
name: '(GMT+2:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna',
},
{
value: 'Africa/Cairo',
name: '(GMT+2:00) Cairo',
},
{
value: 'Africa/Harare',
name: '(GMT+2:00) Harare, Pretoria',
},
{
value: 'Europe/Berlin',
name: '(GMT+2:00) Frankfurt',
},
{
value: 'Europe/Bucharest',
name: '(GMT+3:00) Bucharest',
},
{
value: 'Europe/Helsinki',
name: '(GMT+3:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius',
},
{
value: 'Europe/Athens',
name: '(GMT+3:00) Athens, Minsk',
},
{
value: 'Asia/Jerusalem',
name: '(GMT+3:00) Jerusalem',
},
{
value: 'Europe/Moscow',
name: '(GMT+3:00) Istanbul, Moscow, St. Petersburg, Volgograd',
},
{
value: 'Asia/Kuwait',
name: '(GMT+3:00) Kuwait, Riyadh',
},
{
value: 'Africa/Nairobi',
name: '(GMT+3:00) Nairobi',
},
{
value: 'Asia/Baghdad',
name: '(GMT+3:00) Baghdad',
},
{
value: 'Asia/Dubai',
name: '(GMT+4:00) Abu Dhabi, Muscat',
},
{
value: 'Asia/Tehran',
name: '(GMT+4:30) Tehran',
},
{
value: 'Asia/Kabul',
name: '(GMT+4:30) Kabul',
},
{
value: 'Asia/Baku',
name: '(GMT+5:00) Baku, Tbilisi, Yerevan',
},
{
value: 'Asia/Yekaterinburg',
name: '(GMT+5:00) Ekaterinburg',
},
{
value: 'Asia/Karachi',
name: '(GMT+5:00) Islamabad, Karachi, Tashkent',
},
{
value: 'Asia/Kolkata',
name: '(GMT+5:30) Chennai, Kolkata, Mumbai, New Delhi',
},
{
value: 'Asia/Colombo',
name: '(GMT+5:30) Sri Jayawardenepura',
},
{
value: 'Asia/Kathmandu',
name: '(GMT+5:45) Kathmandu',
},
{
value: 'Asia/Dhaka',
name: '(GMT+6:00) Astana, Dhaka',
},
{
value: 'Asia/Almaty',
name: '(GMT+6:00) Almaty, Novosibirsk',
},
{
value: 'Asia/Rangoon',
name: '(GMT+6:30) Yangon Rangoon',
},
{
value: 'Asia/Bangkok',
name: '(GMT+7:00) Bangkok, Hanoi, Jakarta',
},
{
value: 'Asia/Krasnoyarsk',
name: '(GMT+7:00) Krasnoyarsk',
},
{
value: 'Asia/Shanghai',
name: '(GMT+8:00) Beijing, Chongqing, Hong Kong SAR, Urumqi',
},
{
value: 'Asia/Kuala_Lumpur',
name: '(GMT+8:00) Kuala Lumpur, Singapore',
},
{
value: 'Asia/Taipei',
name: '(GMT+8:00) Taipei',
},
{
value: 'Australia/Perth',
name: '(GMT+8:00) Perth',
},
{
value: 'Asia/Irkutsk',
name: '(GMT+8:00) Irkutsk, Ulaanbaatar',
},
{
value: 'Asia/Seoul',
name: '(GMT+9:00) Seoul',
},
{
value: 'Asia/Tokyo',
name: '(GMT+9:00) Osaka, Sapporo, Tokyo',
},
{
value: 'Australia/Darwin',
name: '(GMT+9:30) Darwin',
},
{
value: 'Asia/Yakutsk',
name: '(GMT+10:00) Yakutsk',
},
{
value: 'Australia/Brisbane',
name: '(GMT+10:00) Brisbane',
},
{
value: 'Asia/Vladivostok',
name: '(GMT+10:00) Vladivostok',
},
{
value: 'Pacific/Guam',
name: '(GMT+10:00) Guam, Port Moresby',
},
{
value: 'Australia/Adelaide',
name: '(GMT+10:30) Adelaide',
},
{
value: 'Australia/Sydney',
name: '(GMT+11:00) Canberra, Melbourne, Sydney',
},
{
value: 'Australia/Hobart',
name: '(GMT+11:00) Hobart',
},
{
value: 'Asia/Magadan',
name: '(GMT+11:00) Magadan, Solomon Islands, New Caledonia',
},
{
value: 'Asia/Kamchatka',
name: '(GMT+12:00) Kamchatka, Marshall Islands',
},
{
value: 'Pacific/Fiji',
name: '(GMT+12:00) Fiji Islands',
},
{
value: 'Pacific/Auckland',
name: '(GMT+13:00) Auckland, Wellington',
},
{
value: 'Pacific/Tongatapu',
name: '(GMT+13:00) Nuku\'alofa',
},
]

47
web/utils/var.ts Normal file
View File

@@ -0,0 +1,47 @@
import { VAR_ITEM_TEMPLATE, getMaxVarNameLength, zhRegex, emojiRegex, MAX_VAR_KEY_LENGHT } from "@/config"
const otherAllowedRegex = new RegExp(`^[a-zA-Z0-9_]+$`)
export const getNewVar = (key: string) => {
return {
...VAR_ITEM_TEMPLATE,
key,
name: key.slice(0, getMaxVarNameLength(key)),
}
}
const checkKey = (key: string, canBeEmpty?: boolean) => {
if (key.length === 0 && !canBeEmpty) {
return 'canNoBeEmpty'
}
if (canBeEmpty && key === '') {
return true
}
if (key.length > MAX_VAR_KEY_LENGHT) {
return 'tooLong'
}
if (otherAllowedRegex.test(key)) {
if (/[0-9]/.test(key[0])) {
return 'notStartWithNumber'
}
return true
}
return 'notValid'
}
export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
let isValid = true
let errorKey = ''
let errorMessageKey = ''
keys.forEach((key) => {
if (!isValid) {
return
}
const res = checkKey(key, canBeEmpty)
if (res !== true) {
isValid = false
errorKey = key
errorMessageKey = res
}
})
return { isValid, errorKey, errorMessageKey }
}