feat(web-admin): add OA person value normalize helpers
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e760de4bd3
commit
6aa6c6e73e
2 files changed
+123
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import {
|
||||
extractPersonIds,
|
||||
formatPersonDisplay,
|
||||
isPersonValueItem,
|
||||
normalizePersonModelValue
|
||||
} from './oa-person-value'
|
||||
|
||||
describe('oa-person-value', () => {
|
||||
it('isPersonValueItem', () => {
|
||||
assert.equal(isPersonValueItem({ id: 'u1', name: '张三' }), true)
|
||||
assert.equal(isPersonValueItem({ id: '', name: 'x' }), false)
|
||||
assert.equal(isPersonValueItem(null), false)
|
||||
})
|
||||
|
||||
it('normalize single', () => {
|
||||
assert.deepEqual(normalizePersonModelValue({ id: 'u1', name: '张三' }, false), {
|
||||
id: 'u1',
|
||||
name: '张三'
|
||||
})
|
||||
assert.equal(normalizePersonModelValue(null, false), null)
|
||||
})
|
||||
|
||||
it('normalize multiple', () => {
|
||||
assert.deepEqual(
|
||||
normalizePersonModelValue(
|
||||
[
|
||||
{ id: 'u1', name: '张三' },
|
||||
{ id: 'u2', name: '李四' }
|
||||
],
|
||||
true
|
||||
),
|
||||
[
|
||||
{ id: 'u1', name: '张三' },
|
||||
{ id: 'u2', name: '李四' }
|
||||
]
|
||||
)
|
||||
assert.deepEqual(normalizePersonModelValue(null, true), [])
|
||||
})
|
||||
|
||||
it('format and extract ids', () => {
|
||||
assert.equal(
|
||||
formatPersonDisplay([
|
||||
{ id: 'u1', name: '张三' },
|
||||
{ id: 'u2', name: '李四' }
|
||||
]),
|
||||
'张三、李四'
|
||||
)
|
||||
assert.deepEqual(
|
||||
extractPersonIds({ id: 'u1', name: '张三' }),
|
||||
['u1']
|
||||
)
|
||||
assert.deepEqual(
|
||||
extractPersonIds([
|
||||
{ id: 'u1', name: '张三' },
|
||||
{ id: 'u2', name: '李四' }
|
||||
]),
|
||||
['u1', 'u2']
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,61 @@
|
||||
export type OaPersonValueItem = { id: string; name: string }
|
||||
|
||||
export function isPersonValueItem(v: unknown): v is OaPersonValueItem {
|
||||
if (!v || typeof v !== 'object') return false
|
||||
const o = v as Record<string, unknown>
|
||||
return String(o.id || '').trim() !== '' && String(o.name || '').trim() !== ''
|
||||
}
|
||||
|
||||
export function normalizePersonModelValue(
|
||||
raw: unknown,
|
||||
multiple: boolean
|
||||
): OaPersonValueItem | OaPersonValueItem[] | null {
|
||||
if (multiple) {
|
||||
if (Array.isArray(raw)) {
|
||||
return raw.filter(isPersonValueItem).map((x) => ({
|
||||
id: String(x.id).trim(),
|
||||
name: String(x.name).trim()
|
||||
}))
|
||||
}
|
||||
if (isPersonValueItem(raw)) {
|
||||
return [{ id: String(raw.id).trim(), name: String(raw.name).trim() }]
|
||||
}
|
||||
return []
|
||||
}
|
||||
if (isPersonValueItem(raw)) {
|
||||
return { id: String(raw.id).trim(), name: String(raw.name).trim() }
|
||||
}
|
||||
if (Array.isArray(raw) && raw.length === 1 && isPersonValueItem(raw[0])) {
|
||||
return { id: String(raw[0].id).trim(), name: String(raw[0].name).trim() }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function formatPersonDisplay(raw: unknown): string {
|
||||
if (Array.isArray(raw)) {
|
||||
return raw
|
||||
.filter(isPersonValueItem)
|
||||
.map((x) => x.name)
|
||||
.filter(Boolean)
|
||||
.join('、')
|
||||
}
|
||||
if (isPersonValueItem(raw)) return raw.name
|
||||
return ''
|
||||
}
|
||||
|
||||
export function extractPersonIds(raw: unknown): string[] {
|
||||
const n = Array.isArray(raw)
|
||||
? raw.filter(isPersonValueItem)
|
||||
: isPersonValueItem(raw)
|
||||
? [raw]
|
||||
: []
|
||||
const out: string[] = []
|
||||
const seen = new Set<string>()
|
||||
for (const x of n) {
|
||||
const id = String(x.id).trim()
|
||||
if (!id || seen.has(id)) continue
|
||||
seen.add(id)
|
||||
out.push(id)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user