@@ -608,3 +608,47 @@ valuesStore.prefetch(ordinatesDataset);
608608const abscissas = useValue (abscissasDataset );
609609const ordinates = useValue (ordinatesDataset );
610610```
611+
612+ To work with HDF5 attributes, retrieve an entity object with ` useEntity ` or
613+ ` useDatasets ` and pass it to ` findAttribute ` . Then, you can check or assert its
614+ type and shape and retrieve its value with ` getAttributeValue ` :
615+
616+ ``` ts
617+ const { attrValuesStore } = useDataContext ();
618+ const entity = useEntity (' /arrays/twoD' ); // ProvidedEntity
619+
620+ // If you just want to know whether the attribute is present
621+ const hasAttr = hasAttribute (entity , ' my_attr' ); // boolean
622+
623+ // Otherwise, find it
624+ const attribute = findAttribute (entity , ' my_attr' ); // Attribute | undefined
625+
626+ // If the attribute must be present and have the expected shape and type, use type assertions
627+ assertDefined (attribute );
628+ assertArrayShape (attribute );
629+ assertStringType (attribute ); // now `Attribute & HasShape<ArrayShape> & HasType<StringType>`
630+
631+ // Otherwise, use type guards and an `if` block
632+ if (
633+ isDefined (attribute ) &&
634+ hasArrayShape (attribute ) &&
635+ hasStringType (attribute )
636+ ) {
637+ const someStr = getAttributeValue (entity , attribute , attrValuesStore ); // string
638+ someStr .startWith (' foo' ); // `someStr` is fully type-checked; no need to use `typeof`
639+ }
640+ ```
641+
642+ With scalar string and numeric attributes, use ` findScalarStrAttr ` and
643+ ` findScalarNumAttr ` for convenience:
644+
645+ ``` ts
646+ const strAttr = findScalarStrAttr (entity , ' my_str_attr' );
647+ const numAttr = findScalarNumAttr (entity , ' my_num_attr' );
648+
649+ assertDefined (strAttr ); // or `isDefined` + `if` block
650+ assertDefined (numAttr );
651+
652+ const str = getAttributeValue (entity , strAttr , attrValuesStore ); // string
653+ const num = getAttributeValue (entity , numAttr , attrValuesStore ); // number | bigint
654+ ```
0 commit comments