@a2ui-sdk/图利
与 A2UI 协议合作的功能 。 此软件包为数据绑定解析,路径操纵,字符串插入,以及验证提供了帮助.
安装
npm install @a2ui-sdk/utils
使用量
v0.9 (最晚) (中文(简体) )
import {
// String interpolation
hasInterpolation,
interpolate,
// Data binding utilities
resolveValue,
resolveStringValue,
resolveNumberValue,
resolveBooleanValue,
isPathBinding,
// Path utilities
parsePath,
joinPath,
getValueAtPath,
setValueAtPath,
// Validation utilities
evaluateCheckRule,
validateChecks,
} from '@a2ui-sdk/utils/0.9'
v. 0.8 (中文(简体) )
import {
// Data binding utilities
resolveValue,
resolveActionContext,
resolveStringValue,
resolveNumberValue,
resolveBooleanValue,
isPathBinding,
// Path utilities
normalizePath,
isAbsolutePath,
joinPaths,
resolvePath,
parsePath,
joinPath,
getValueAtPath,
setValueAtPath,
} from '@a2ui-sdk/utils/0.8'
命名空间导入
import { v0_8, v0_9 } from '@a2ui-sdk/utils'
// Use v0.9 utilities
const { interpolate, hasInterpolation } = v0_9
API 密码
范围和路径分辨率(v0.8+)
工作范围数据路径:
import {
normalizePath,
isAbsolutePath,
joinPaths,
resolvePath,
} from '@a2ui-sdk/utils/0.8'
// Normalize path format
normalizePath('user/name') // '/user/name'
normalizePath('/items/') // '/items'
// Check if path is absolute
isAbsolutePath('/user/name') // true
isAbsolutePath('name') // false
// Join base path with relative path
joinPaths('/items/0', 'name') // '/items/0/name'
joinPaths('/items', '../users') // '/users'
// Resolve path with scope
resolvePath('/user/name', '/items/0') // '/user/name' (absolute, ignores basePath)
resolvePath('name', '/items/0') // '/items/0/name' (relative, uses basePath)
resolvePath('name', null) // '/name' (treats relative as absolute when no basePath)
字符串内插( v0. 9)
有嵌入式表达式的处理字符串 :
import { hasInterpolation, interpolate } from '@a2ui-sdk/utils/0.9'
// Check if string contains interpolation
hasInterpolation('Hello {{name}}') // true
hasInterpolation('Hello world') // false
// Interpolate string with data
const data = { name: 'Alice', count: 5 }
interpolate('Hello {{name}}, you have {{count}} items', data)
// => 'Hello Alice, you have 5 items'
数据绑定
从数据模型中解析动态值 :
import { resolveValue, isPathBinding } from '@a2ui-sdk/utils/0.9'
const dataModel = { user: { name: 'Alice' } }
// Check if value is a path binding
isPathBinding({ path: '/user/name' }) // true
isPathBinding('static value') // false
// Resolve value
resolveValue({ path: '/user/name' }, dataModel) // 'Alice'
resolveValue('static value', dataModel) // 'static value'
resolveValue 也支持范围路径解析 :
import { resolveValue } from '@a2ui-sdk/utils/0.9'
const dataModel = { items: [{ name: 'Item 1' }, { name: 'Item 2' }] }
// Resolve with basePath for scoped data access
resolveValue({ path: 'name' }, dataModel, '/items/0') // 'Item 1'
resolveValue({ path: '/items/1/name' }, dataModel, '/items/0') // 'Item 2' (absolute path ignores basePath)
路径工具
与 JSON 指针路径合作( RFC 6901):
import {
parsePath,
joinPath,
getValueAtPath,
setValueAtPath,
} from '@a2ui-sdk/utils/0.9'
// Parse path into segments
parsePath('/user/profile/name') // ['user', 'profile', 'name']
// Join segments into path
joinPath(['user', 'profile', 'name']) // '/user/profile/name'
// Get value at path
const data = { user: { profile: { name: 'Alice' } } }
getValueAtPath(data, '/user/profile/name') // 'Alice'
// Set value at path (immutable)
const newData = setValueAtPath(data, '/user/profile/name', 'Bob')
// data is unchanged, newData has the update
审定(v0.9)
评价验证规则 :
import { evaluateCheckRule, validateChecks } from '@a2ui-sdk/utils/0.9'
const checks = [
{
message: 'Name is required',
call: 'isNotEmpty',
args: { value: { path: '/form/name' } },
},
]
const dataModel = { form: { name: '' } }
const result = validateChecks(checks, dataModel)
// { valid: false, errors: ['Name is required'] }
许可证
阿帕奇 - 2.0