From 878cb3fce31e09f19031c954981fdf63c6c9e068 Mon Sep 17 00:00:00 2001 From: Loic Huder Date: Fri, 30 Apr 2021 10:01:14 +0200 Subject: [PATCH] Fix errors on Infinity and on slices with integers for HSDS --- src/h5web/providers/hsds/api.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/h5web/providers/hsds/api.ts b/src/h5web/providers/hsds/api.ts index abe8b6975..d46f47765 100644 --- a/src/h5web/providers/hsds/api.ts +++ b/src/h5web/providers/hsds/api.ts @@ -56,7 +56,7 @@ export class HsdsApi extends ProviderApi { } catch { // https://github.com/HDFGroup/hsds/issues/87 try { - return JSON.parse(data.replaceAll('NaN', '"NaN"')); + return JSON.parse(data.replaceAll(/(NaN|-?Infinity)/gu, '"$&"')); } catch { return data; } @@ -104,7 +104,18 @@ export class HsdsApi extends ProviderApi { const entity = await this.getEntity(path); assertHsdsDataset(entity); - return this.fetchValue(entity.id, selection); + const value = await this.fetchValue(entity.id, selection); + + // https://github.com/HDFGroup/hsds/issues/88 + // HSDS does not reduce the number of dimensions when selecting indices + // This is an issue when trying to slice nD arrays + // Therefore the dimension reduction is done "manually" here with a flat + const numberIndices = selection.match(/\d+/gu)?.length; + if (!numberIndices) { + return value; + } + + return (value as unknown[]).flat(numberIndices); } private async fetchRootId(): Promise {