(props: {
path: string;
data?: any;
defaultValue?: any;
addons: any;
valueType?: string;
})
| 38 | * @returns value |
| 39 | */ |
| 40 | export const getValueFromKey = (props: { |
| 41 | path: string; |
| 42 | data?: any; |
| 43 | defaultValue?: any; |
| 44 | addons: any; |
| 45 | valueType?: string; |
| 46 | }): string | boolean => { |
| 47 | const { data, path = '', defaultValue = '', addons, valueType } = props; |
| 48 | |
| 49 | let result = null; |
| 50 | let negation = null; // 否定标识 ! |
| 51 | let dataPath = path; |
| 52 | |
| 53 | if (path.substring(0, 2) === '!!') { |
| 54 | negation = '!!'; |
| 55 | dataPath = path.substring(2); |
| 56 | } else if (path.substring(0, 1) === '!') { |
| 57 | negation = '!'; |
| 58 | dataPath = path.substring(1); |
| 59 | } |
| 60 | |
| 61 | // 带 source 标识,表示从顶层获取数据 |
| 62 | if (dataPath.includes('source:')) { |
| 63 | const [_, sourcePath]: any = dataPath.split('source:'); |
| 64 | const sourceData = addons.getSourceData(); |
| 65 | result = get(sourceData, sourcePath); |
| 66 | } else { |
| 67 | result = get(data, dataPath); |
| 68 | } |
| 69 | |
| 70 | if (!result && result !== 0 && typeof result !== 'boolean') { |
| 71 | result = defaultValue; |
| 72 | } |
| 73 | |
| 74 | // 根据 negation 取反, 返回结果 |
| 75 | if (negation === '!') { |
| 76 | return !result; |
| 77 | } |
| 78 | |
| 79 | if (negation === '!!') { |
| 80 | return !!result; |
| 81 | } |
| 82 | |
| 83 | if (result && valueType === '!object' && typeof result === 'object') { |
| 84 | return ''; |
| 85 | } |
| 86 | |
| 87 | return result; |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * 数据格式转换 |
no outgoing calls
no test coverage detected