import type { NdArray } from 'ndarray' ; import ndarray from 'ndarray' ; import type { Attribute } from '..' ; import { assertAbsolutePath , isGroup , hasChildren , assertArray , assertArrayShape , assertDefined , assertDataset , } from '../guards' ; import type { Entity , Dataset , DType , Shape } from '../models-hdf5' ; import { getChildEntity } from '../utils' ; import { mockMetadata } from './metadata' ; import type { MockAttribute , MockDataset } from './models' ; export function assertMockDataset < S extends Shape , T extends DType > ( dataset ) { if ( ! ( 'value' in dataset ) ) { throw new Error ( 'Expected mock dataset' ) ; } } export function assertMockAttribute < S extends Shape , T extends DType > ( attribute ) { if ( ! ( 'value' in attribute ) ) { throw new Error ( 'Expected mock attribute' ) ; } } export function findMockEntity ( path ) { assertAbsolutePath ( path ) ; if ( path === '/' ) { return mockMetadata ; } const pathSegments = path . slice ( 1 ) . split ( '/' ) ; return pathSegments . reduce < Entity | undefined > ( ( parentEntity , currSegment ) => { return parentEntity && isGroup ( parentEntity ) && hasChildren ( parentEntity ) ? getChildEntity ( parentEntity , currSegment ) : undefined ; } , mockMetadata ) ; } export function getMockDataArray < T = number > ( path ) { const entity = findMockEntity ( path ) ; assertDefined ( entity ) ; assertDataset ( entity ) ; assertMockDataset ( entity ) ; const { value } = entity ; assertArray < T > ( value ) ; assertArrayShape ( entity ) ; return ndarray ( value . flat ( entity . shape . length - 1 ) as T [ ] , entity . shape ) ; }