* 检查特征是否匹配
(feature: FormatFeature, ext: string, headContent: string, filePath?: string)
| 175 | * 检查特征是否匹配 |
| 176 | */ |
| 177 | private matchFeature(feature: FormatFeature, ext: string, headContent: string, filePath?: string): boolean { |
| 178 | // 1. 检查扩展名 |
| 179 | if (!feature.extensions.includes(ext)) { |
| 180 | return false |
| 181 | } |
| 182 | |
| 183 | const { signatures } = feature |
| 184 | |
| 185 | // 2. 检查文件头签名(如果定义了) |
| 186 | let headMatch = true |
| 187 | if (signatures.head && signatures.head.length > 0) { |
| 188 | headMatch = matchHeadSignatures(headContent, signatures.head) |
| 189 | } |
| 190 | |
| 191 | // 3. 检查文件名签名(如果定义了,作为文件头匹配失败的补充) |
| 192 | let filenameMatch = false |
| 193 | if (signatures.filename && signatures.filename.length > 0 && filePath) { |
| 194 | filenameMatch = matchFilenameSignatures(filePath, signatures.filename) |
| 195 | } |
| 196 | |
| 197 | // 文件头签名或文件名签名至少有一个匹配 |
| 198 | if (!headMatch && !filenameMatch) { |
| 199 | // 如果两个都没定义,则认为匹配(只检查扩展名) |
| 200 | if ((signatures.head && signatures.head.length > 0) || (signatures.filename && signatures.filename.length > 0)) { |
| 201 | return false |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // 4. 检查必需字段(如果定义了) |
| 206 | if (signatures.requiredFields && signatures.requiredFields.length > 0) { |
| 207 | if (!matchRequiredFields(headContent, signatures.requiredFields)) { |
| 208 | return false |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // 5. 检查字段值模式(如果定义了) |
| 213 | if (signatures.fieldPatterns) { |
| 214 | for (const [, pattern] of Object.entries(signatures.fieldPatterns)) { |
| 215 | if (!pattern.test(headContent)) { |
| 216 | return false |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return true |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
no test coverage detected