Files
BYW-VoiceHub/app/utils/grade-class-input.ts
T

32 lines
857 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 解析班级批量输入:逗号/换行分隔;数字区间 "1-10" 生成 "1班".."10班"(上限 100 防呆)
const MAX_RANGE_LENGTH = 100
export const parseClassInput = (text: unknown): string[] => {
if (typeof text !== 'string') return []
const segments = text
.split(/[,,\n;;]/)
.map((segment) => segment.trim())
.filter(Boolean)
const result: string[] = []
for (const segment of segments) {
const rangeMatch = segment.match(/^(\d+)-(\d+)$/)
if (rangeMatch) {
const start = parseInt(rangeMatch[1], 10)
const end = parseInt(rangeMatch[2], 10)
if (start >= 1 && end >= start && end - start < MAX_RANGE_LENGTH) {
for (let i = start; i <= end; i++) {
result.push(`${i}班`)
}
continue
}
}
result.push(segment)
}
return [...new Set(result)]
}