(data: DatasetOption['source'])
| 254 | * Note: An empty array will be detected as `SOURCE_FORMAT_ARRAY_ROWS`. |
| 255 | */ |
| 256 | export function detectSourceFormat(data: DatasetOption['source']): SourceFormat { |
| 257 | let sourceFormat: SourceFormat = SOURCE_FORMAT_UNKNOWN; |
| 258 | |
| 259 | if (isTypedArray(data)) { |
| 260 | sourceFormat = SOURCE_FORMAT_TYPED_ARRAY; |
| 261 | } |
| 262 | else if (isArray(data)) { |
| 263 | // FIXME Whether tolerate null in top level array? |
| 264 | if (data.length === 0) { |
| 265 | sourceFormat = SOURCE_FORMAT_ARRAY_ROWS; |
| 266 | } |
| 267 | |
| 268 | for (let i = 0, len = data.length; i < len; i++) { |
| 269 | const item = data[i]; |
| 270 | |
| 271 | if (item == null) { |
| 272 | continue; |
| 273 | } |
| 274 | else if (isArray(item) || isTypedArray(item)) { |
| 275 | sourceFormat = SOURCE_FORMAT_ARRAY_ROWS; |
| 276 | break; |
| 277 | } |
| 278 | else if (isObject(item)) { |
| 279 | sourceFormat = SOURCE_FORMAT_OBJECT_ROWS; |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | else if (isObject(data)) { |
| 285 | for (const key in data) { |
| 286 | if (hasOwn(data, key) && isArrayLike((data as Dictionary<unknown>)[key])) { |
| 287 | sourceFormat = SOURCE_FORMAT_KEYED_COLUMNS; |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return sourceFormat; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Determine the source definitions from data standalone dimensions definitions |
no test coverage detected