task-5: normalizeStatus() 状态归一(大写化、连字符/空格转下划线、空值安全)
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
export function normalizeStatus(raw: string | null | undefined): string {
|
||||
if (typeof raw !== 'string') {
|
||||
return ''
|
||||
}
|
||||
const trimmed = raw.trim()
|
||||
if (!trimmed) {
|
||||
return ''
|
||||
}
|
||||
return trimmed.toUpperCase().replace(/[\s-]+/g, '_')
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { normalizeStatus } from '../src/shared/api/status.ts'
|
||||
|
||||
test('test_normalize_lower_to_upper', () => {
|
||||
assert.equal(normalizeStatus('success'), 'SUCCESS')
|
||||
assert.equal(normalizeStatus('failed'), 'FAILED')
|
||||
assert.equal(normalizeStatus('pending'), 'PENDING')
|
||||
})
|
||||
|
||||
test('test_normalize_mixed_case', () => {
|
||||
assert.equal(normalizeStatus('rUnNiNg'), 'RUNNING')
|
||||
})
|
||||
|
||||
test('test_normalize_with_hyphen', () => {
|
||||
assert.equal(normalizeStatus('in-progress'), 'IN_PROGRESS')
|
||||
assert.equal(normalizeStatus('file-ready'), 'FILE_READY')
|
||||
})
|
||||
|
||||
test('test_normalize_null', () => {
|
||||
assert.equal(normalizeStatus(null), '')
|
||||
})
|
||||
|
||||
test('test_normalize_undefined', () => {
|
||||
assert.equal(normalizeStatus(undefined), '')
|
||||
})
|
||||
|
||||
test('test_normalize_empty_string', () => {
|
||||
assert.equal(normalizeStatus(''), '')
|
||||
})
|
||||
|
||||
test('test_normalize_unknown_passthrough', () => {
|
||||
assert.equal(normalizeStatus('SYNCING'), 'SYNCING')
|
||||
assert.equal(normalizeStatus('QUEUED'), 'QUEUED')
|
||||
})
|
||||
|
||||
test('test_normalize_whitespace_padded', () => {
|
||||
assert.equal(normalizeStatus(' running '), 'RUNNING')
|
||||
assert.equal(normalizeStatus(' '), '')
|
||||
})
|
||||
Reference in New Issue
Block a user