Initial commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import { twMerge } from 'tailwind-merge'
|
||||
import cn from 'classnames'
|
||||
|
||||
const classNames = (...cls: cn.ArgumentArray) => {
|
||||
return twMerge(cn(cls))
|
||||
}
|
||||
|
||||
export default classNames
|
||||
@@ -0,0 +1,11 @@
|
||||
import { SearchIndex } from 'emoji-mart'
|
||||
import type { Emoji } from '@emoji-mart/data'
|
||||
|
||||
export async function searchEmoji(value: string) {
|
||||
const emojis: Emoji[] = await SearchIndex.search(value) || []
|
||||
|
||||
const results = emojis.map((emoji) => {
|
||||
return emoji.skins[0].native
|
||||
})
|
||||
return results
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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
|
||||
const 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]}`
|
||||
}
|
||||
|
||||
export const downloadFile = ({ data, fileName }: { data: Blob; fileName: string }) => {
|
||||
const url = window.URL.createObjectURL(data)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = fileName
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
window.URL.revokeObjectURL(url)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import type { PromptVariable, UserInputFormItem } from '@/types/app'
|
||||
|
||||
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
|
||||
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
|
||||
const name = inputs[key]
|
||||
if (name)
|
||||
return name
|
||||
|
||||
const valueObj: PromptVariable | undefined = promptVariables.find(v => v.key === key)
|
||||
return valueObj ? `{{${valueObj.key}}}` : match
|
||||
})
|
||||
}
|
||||
|
||||
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
|
||||
if (!useInputs)
|
||||
return []
|
||||
const promptVariables: PromptVariable[] = []
|
||||
useInputs.forEach((item: any) => {
|
||||
const isParagraph = !!item.paragraph
|
||||
const [type, content] = (() => {
|
||||
if (isParagraph)
|
||||
return ['paragraph', item.paragraph]
|
||||
|
||||
if (item['text-input'])
|
||||
return ['string', item['text-input']]
|
||||
|
||||
if (item.number)
|
||||
return ['number', item.number]
|
||||
|
||||
return ['select', item.select]
|
||||
})()
|
||||
|
||||
if (type === 'string' || type === 'paragraph') {
|
||||
promptVariables.push({
|
||||
key: content.variable,
|
||||
name: content.label,
|
||||
required: content.required,
|
||||
type,
|
||||
max_length: content.max_length,
|
||||
options: [],
|
||||
})
|
||||
}
|
||||
else if (type === 'number') {
|
||||
promptVariables.push({
|
||||
key: content.variable,
|
||||
name: content.label,
|
||||
required: content.required,
|
||||
type,
|
||||
options: [],
|
||||
})
|
||||
}
|
||||
else {
|
||||
promptVariables.push({
|
||||
key: content.variable,
|
||||
name: content.label,
|
||||
required: content.required,
|
||||
type: 'select',
|
||||
options: content.options,
|
||||
})
|
||||
}
|
||||
})
|
||||
return promptVariables
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
|
||||
export function randomString(length: number) {
|
||||
let result = ''
|
||||
for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user