diff --git a/packages/app/src/index.ts b/packages/app/src/index.ts index 339954491..03244bdd6 100644 --- a/packages/app/src/index.ts +++ b/packages/app/src/index.ts @@ -16,12 +16,120 @@ export type GetExportURL = NonNullable; export { useDataContext } from './providers/DataProvider'; export type { DataContextValue } from './providers/DataProvider'; -// Undocumented -export { assertEnvVar } from '@h5web/shared/guards'; -export { assertNonNull } from '@h5web/shared/guards'; - // Undocumented (for @h5web/h5wasm) export { default as DataProvider } from './providers/DataProvider'; export { DataProviderApi } from './providers/api'; export type { ValuesStoreParams } from './providers/models'; export { getValueOrError } from './providers/utils'; + +// Undocumented models +export { EntityKind } from '@h5web/shared/hdf5-models'; + +export type { + // Entity + Entity, + ProvidedEntity, + ChildEntity, + Group, + GroupWithChildren, + Dataset, + Datatype, + UnresolvedEntity, + LinkClass, + Attribute, + Filter, + VirtualSource, + + // Shape + Shape, + ScalarShape, + ArrayShape, + + // Type + DType, + StringType, + IntegerType, + FloatType, + NumericType, + BooleanType, + EnumType, + NumericLikeType, + ComplexType, + PrintableType, + CompoundType, + ArrayType, + VLenType, + TimeType, + BitfieldType, + OpaqueType, + ReferenceType, + UnknownType, + + // Value + ScalarValue, + ArrayValue, + AttributeValues, + H5WebComplex, +} from '@h5web/shared/hdf5-models'; + +// Undocumented guards and assertions +export { + isDefined, + isNonNull, + assertDefined, + assertNonNull, + assertNum, + assertStr, + assertEnvVar, + assertComplex, + isTypedArray, + isBigIntTypedArray, + assertArray, + assertArrayOrTypedArray, + isGroup, + hasChildren, + isDataset, + isDatatype, + assertGroup, + assertGroupWithChildren, + assertDataset, + assertDatatype, + isScalarShape, + isArrayShape, + hasScalarShape, + hasArrayShape, + hasNonNullShape, + assertScalarShape, + assertArrayShape, + assertNonNullShape, + hasMinDims, + hasNumDims, + assertMinDims, + assertNumDims, + isStringType, + isIntegerType, + isFloatType, + isNumericType, + isBoolType, + isEnumType, + isNumericLikeType, + isComplexType, + isPrintableType, + isCompoundType, + hasStringType, + hasNumericType, + hasBoolType, + hasEnumType, + hasNumericLikeType, + hasComplexType, + hasPrintableType, + hasCompoundType, + assertStringType, + assertNumericType, + assertNumericLikeType, + assertComplexType, + assertPrintableType, + assertCompoundType, + assertScalarValue, + assertDatasetValue, +} from '@h5web/shared/guards'; diff --git a/packages/shared/src/guards.ts b/packages/shared/src/guards.ts index 514082b24..79970ad58 100644 --- a/packages/shared/src/guards.ts +++ b/packages/shared/src/guards.ts @@ -72,9 +72,12 @@ export function assertNonNull( } } -function assertNum(val: unknown): asserts val is number { +export function assertNum( + val: unknown, + message = 'Expected number', +): asserts val is number { if (typeof val !== 'number') { - throw new TypeError('Expected number'); + throw new TypeError(message); } } @@ -109,20 +112,26 @@ export function assertEnvVar( } } -function assertComplex(val: unknown): asserts val is H5WebComplex { +export function assertComplex( + val: unknown, + message = 'Expected complex', +): asserts val is H5WebComplex { if ( !Array.isArray(val) || val.length !== 2 || typeof val[0] !== 'number' || typeof val[1] !== 'number' ) { - throw new TypeError('Expected complex'); + throw new TypeError(message); } } -export function assertArray(val: unknown): asserts val is unknown[] { +export function assertArray( + val: unknown, + message = 'Expected array', +): asserts val is unknown[] { if (!Array.isArray(val)) { - throw new TypeError('Expected array'); + throw new TypeError(message); } } @@ -144,13 +153,25 @@ export function isBigIntTypedArray(val: unknown): val is BigIntTypedArray { return val instanceof BigInt64Array || val instanceof BigUint64Array; } +export function assertArrayOrTypedArray( + val: unknown, + message = 'Expected array or typed array', +): asserts val is unknown[] | TypedArray | BigIntTypedArray { + if (!Array.isArray(val) && !isTypedArray(val) && !isBigIntTypedArray(val)) { + throw new TypeError(message); + } +} + export function isGroup(entity: Entity): entity is Group { return entity.kind === EntityKind.Group; } -export function assertGroup(entity: Entity): asserts entity is Group { +export function assertGroup( + entity: Entity, + message = 'Expected group', +): asserts entity is Group { if (!isGroup(entity)) { - throw new Error('Expected group'); + throw new Error(message); } } @@ -160,9 +181,10 @@ export function hasChildren(group: Group): group is GroupWithChildren { export function assertGroupWithChildren( group: Group, + message = 'Expected group with children', ): asserts group is GroupWithChildren { if (!hasChildren(group)) { - throw new Error('Expected group with children'); + throw new Error(message); } } @@ -183,6 +205,15 @@ export function isDatatype(entity: Entity): entity is Datatype { return entity.kind === EntityKind.Datatype; } +export function assertDatatype( + entity: Entity, + message = 'Expected datatype', +): asserts entity is Datatype { + if (!isDatatype(entity)) { + throw new Error(message); + } +} + export function isH5WebComplex( complex: H5WebComplex | ComplexArray, ): complex is H5WebComplex { @@ -201,12 +232,17 @@ export function hasScalarShape( export function assertScalarShape( dataset: Dataset, + message = 'Expected dataset to have scalar shape', ): asserts dataset is Dataset { if (!hasScalarShape(dataset)) { - throw new Error('Expected dataset to have scalar shape'); + throw new Error(message); } } +export function isArrayShape(shape: Shape): shape is ArrayShape { + return isNonNull(shape) && shape.length > 0; +} + export function hasArrayShape( dataset: Dataset, ): dataset is Dataset { @@ -215,9 +251,10 @@ export function hasArrayShape( export function assertArrayShape( dataset: Dataset, + message = 'Expected dataset to have array shape', ): asserts dataset is Dataset { if (!hasArrayShape(dataset)) { - throw new Error('Expected dataset to have array shape'); + throw new Error(message); } } @@ -229,9 +266,10 @@ export function hasNonNullShape( export function assertNonNullShape( dataset: Dataset, + message = 'Expected dataset to have non-null shape', ): asserts dataset is Dataset { if (!hasNonNullShape(dataset)) { - throw new Error('Expected dataset to have non-null shape'); + throw new Error(message); } } @@ -239,9 +277,13 @@ export function hasMinDims(dataset: Dataset, min: number): boolean { return dataset.shape.length >= min; } -export function assertMinDims(dataset: Dataset, min: number): void { +export function assertMinDims( + dataset: Dataset, + min: number, + message = `Expected dataset with at least ${min} dimensions`, +): void { if (!hasMinDims(dataset, min)) { - throw new Error(`Expected dataset with at least ${min} dimensions`); + throw new Error(message); } } @@ -249,43 +291,32 @@ export function hasNumDims(dataset: Dataset, num: number): boolean { return dataset.shape.length === num; } -export function assertNumDims(dataset: Dataset, num: number): void { +export function assertNumDims( + dataset: Dataset, + num: number, + message = `Expected dataset with ${num} dimensions`, +): void { if (!hasNumDims(dataset, num)) { - throw new Error(`Expected dataset with ${num} dimensions`); + throw new Error(message); } } -export function isBoolType(type: DType): type is BooleanType { - return type.class === DTypeClass.Bool; -} - -export function hasBoolType( - dataset: Dataset, -): dataset is Dataset { - return isBoolType(dataset.type); -} - -export function isEnumType(type: DType): type is EnumType { - return type.class === DTypeClass.Enum; -} - -export function hasEnumType( - dataset: Dataset, -): dataset is Dataset { - return isEnumType(dataset.type); +export function isStringType(type: DType): type is StringType { + return type.class === DTypeClass.String; } -function hasStringType( +export function hasStringType( dataset: Dataset, ): dataset is Dataset { - return dataset.type.class === DTypeClass.String; + return isStringType(dataset.type); } export function assertStringType( dataset: Dataset, + message = 'Expected dataset to have string type', ): asserts dataset is Dataset { if (!hasStringType(dataset)) { - throw new Error('Expected dataset to have string type'); + throw new Error(message); } } @@ -309,12 +340,33 @@ export function hasNumericType( export function assertNumericType( dataset: Dataset, + message = 'Expected dataset to have numeric type', ): asserts dataset is Dataset { if (!hasNumericType(dataset)) { - throw new Error('Expected dataset to have numeric type'); + throw new Error(message); } } +export function isBoolType(type: DType): type is BooleanType { + return type.class === DTypeClass.Bool; +} + +export function hasBoolType( + dataset: Dataset, +): dataset is Dataset { + return isBoolType(dataset.type); +} + +export function isEnumType(type: DType): type is EnumType { + return type.class === DTypeClass.Enum; +} + +export function hasEnumType( + dataset: Dataset, +): dataset is Dataset { + return isEnumType(dataset.type); +} + export function isNumericLikeType(type: DType): type is NumericLikeType { return isNumericType(type) || isBoolType(type) || isEnumType(type); } @@ -327,16 +379,13 @@ export function hasNumericLikeType( export function assertNumericLikeType( dataset: Dataset, + message = 'Expected dataset to have numeric, boolean or enum type', ): asserts dataset is Dataset { if (!hasNumericLikeType(dataset)) { - throw new Error('Expected dataset to have numeric, boolean or enum type'); + throw new Error(message); } } -export function isStringType(type: DType): type is StringType { - return type.class === DTypeClass.String; -} - export function isComplexType(type: DType): type is ComplexType { return type.class === DTypeClass.Complex; } @@ -349,19 +398,19 @@ export function hasComplexType( export function assertComplexType( dataset: Dataset, + message = 'Expected dataset to have complex type', ): asserts dataset is Dataset { if (!hasComplexType(dataset)) { - throw new Error('Expected dataset to have complex type'); + throw new Error(message); } } export function assertNumericLikeOrComplexType( dataset: Dataset, + message = 'Expected dataset to have numeric, boolean, enum or complex type', ): asserts dataset is Dataset { if (!hasNumericLikeType(dataset) && !hasComplexType(dataset)) { - throw new Error( - 'Expected dataset to have numeric, boolean, enum or complex type', - ); + throw new Error(message); } } @@ -377,9 +426,10 @@ export function hasPrintableType( export function assertPrintableType( dataset: Dataset, + message = 'Expected dataset to have displayable type', ): asserts dataset is Dataset { if (!hasPrintableType(dataset)) { - throw new Error('Expected dataset to have displayable type'); + throw new Error(message); } } @@ -395,9 +445,10 @@ export function hasCompoundType( export function assertCompoundType( dataset: Dataset, + message = 'Expected dataset to have compound type', ): asserts dataset is Dataset { if (!hasCompoundType(dataset)) { - throw new Error('Expected dataset to have compound type'); + throw new Error(message); } } @@ -410,9 +461,10 @@ export function hasPrintableCompoundType( export function assertPrintableCompoundType( dataset: Dataset, + message = 'Expected compound dataset to have printable types', ): asserts dataset is Dataset> { if (!hasPrintableCompoundType(dataset)) { - throw new Error('Expected compound dataset to have printable types'); + throw new Error(message); } } @@ -456,13 +508,7 @@ export function assertDatasetValue>( if (hasScalarShape(dataset)) { assertScalarValue(value, type); } else { - if ( - !Array.isArray(value) && - !isTypedArray(value) && - !isBigIntTypedArray(value) - ) { - throw new TypeError('Expected array or typed array'); - } + assertArrayOrTypedArray(value); if (value.length > 0) { assertScalarValue(value[0], type);