Skip to content

Commit eee6a0e

Browse files
authored
feat(indexing): support v2 string data-types (#75)
1 parent 6af2e9c commit eee6a0e

File tree

9 files changed

+639
-190
lines changed

9 files changed

+639
-190
lines changed

.changeset/rude-weeks-listen.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@zarrita/indexing": patch
3+
"@zarrita/typedarray": patch
4+
"@zarrita/core": patch
5+
---
6+
7+
feat: Support v2 string data types with builtin indexing

packages/core/src/util.ts

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
BoolArray,
3-
ByteStringArray as _ByteStringArray,
4-
UnicodeStringArray as _UnicodeStringArray,
3+
ByteStringArray,
4+
UnicodeStringArray,
55
} from "@zarrita/typedarray";
66

77
import type {
@@ -62,22 +62,11 @@ export function get_ctr<D extends DataType>(
6262
let match = data_type.match(V2_STRING_REGEX);
6363
if (match) {
6464
let [, kind, chars] = match;
65-
if (kind === "U") {
66-
class UnicodeStringArray extends _UnicodeStringArray {
67-
constructor(x: any) {
68-
super(x, Number(chars));
69-
}
70-
}
71-
return UnicodeStringArray as any;
72-
}
73-
if (kind === "S") {
74-
class ByteStringArray extends _ByteStringArray {
75-
constructor(x: any) {
76-
super(x, Number(chars));
77-
}
78-
}
79-
return ByteStringArray as any;
80-
}
65+
// @ts-expect-error
66+
return (kind === "U" ? UnicodeStringArray : ByteStringArray).bind(
67+
null,
68+
Number(chars),
69+
);
8170
}
8271
let ctr = (CONSTRUCTORS as any)[data_type];
8372
if (!ctr) {

packages/indexing/__tests__/get.test.ts

+56-31
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function get_v2(
2121
let root = path.resolve(__dirname, "../../../fixtures/v2/data.zarr");
2222
let store = zarr.root(new FSStore(root));
2323
let arr = await zarr.open.v2(store.resolve(abs_path), { kind: "array" });
24-
return get(arr as any, ...args);
24+
return get(arr, ...args);
2525
}
2626

2727
describe("get v2", () => {
@@ -215,31 +215,51 @@ describe("get v2", () => {
215215
`);
216216
});
217217

218-
it.skip("1d.contiguous.U13.le", async () => {
218+
it("1d.contiguous.U13.le", async () => {
219219
let res = await get_v2("/1d.contiguous.U13.le");
220220
expect(res.data).toBeInstanceOf(UnicodeStringArray);
221-
expect(Array.from(res.data as any)).toStrictEqual(["a", "b", "cc", "d"]);
221+
expect(Array.from(res.data as UnicodeStringArray)).toStrictEqual([
222+
"a",
223+
"b",
224+
"cc",
225+
"d",
226+
]);
222227
expect(res.shape).toStrictEqual([4]);
223228
});
224229

225-
it.skip("1d.contiguous.U13.be", async () => {
230+
it("1d.contiguous.U13.be", async () => {
226231
let res = await get_v2("/1d.contiguous.U13.be");
227232
expect(res.data).toBeInstanceOf(UnicodeStringArray);
228-
expect(Array.from(res.data as any)).toStrictEqual(["a", "b", "cc", "d"]);
233+
expect(Array.from(res.data as UnicodeStringArray)).toStrictEqual([
234+
"a",
235+
"b",
236+
"cc",
237+
"d",
238+
]);
229239
expect(res.shape).toStrictEqual([4]);
230240
});
231241

232-
it.skip("1d.contiguous.U7", async () => {
242+
it("1d.contiguous.U7", async () => {
233243
let res = await get_v2("/1d.contiguous.U7");
234244
expect(res.data).toBeInstanceOf(UnicodeStringArray);
235-
expect(Array.from(res.data as any)).toStrictEqual(["a", "b", "cc", "d"]);
245+
expect(Array.from(res.data as UnicodeStringArray)).toStrictEqual([
246+
"a",
247+
"b",
248+
"cc",
249+
"d",
250+
]);
236251
expect(res.shape).toStrictEqual([4]);
237252
});
238253

239-
it.skip("1d.contiguous.S7", async () => {
254+
it("1d.contiguous.S7", async () => {
240255
let res = await get_v2("/1d.contiguous.S7");
241256
expect(res.data).toBeInstanceOf(ByteStringArray);
242-
expect(Array.from(res.data as any)).toStrictEqual(["a", "b", "cc", "d"]);
257+
expect(Array.from(res.data as UnicodeStringArray)).toStrictEqual([
258+
"a",
259+
"b",
260+
"cc",
261+
"d",
262+
]);
243263
expect(res.shape).toStrictEqual([4]);
244264
});
245265

@@ -391,9 +411,14 @@ describe("get v2", () => {
391411
`);
392412
});
393413

394-
it.skip("2d.chunked.U7", async () => {
414+
it("2d.chunked.U7", async () => {
395415
let res = await get_v2("/2d.chunked.U7");
396-
expect(Array.from(res.data as any)).toStrictEqual(["a", "b", "cc", "d"]);
416+
expect(Array.from(res.data as UnicodeStringArray)).toStrictEqual([
417+
"a",
418+
"b",
419+
"cc",
420+
"d",
421+
]);
397422
expect(res.shape).toStrictEqual([2, 2]);
398423
});
399424

@@ -535,7 +560,7 @@ describe("get v2", () => {
535560
});
536561
});
537562

538-
async function read_complete_v3(
563+
async function get_v3(
539564
abs_path: string,
540565
...args: any[]
541566
): Promise<zarr.Chunk<zarr.DataType>> {
@@ -547,55 +572,55 @@ async function read_complete_v3(
547572

548573
describe("get v3", () => {
549574
it("reads 1d.contiguous.gzip.i2", async () => {
550-
let res = await read_complete_v3("/1d.contiguous.gzip.i2");
575+
let res = await get_v3("/1d.contiguous.gzip.i2");
551576
expect(res.data).toStrictEqual(new Int16Array([1, 2, 3, 4]));
552577
expect(res.shape).toStrictEqual([4]);
553578
});
554579

555580
it("reads 1d.contiguous.blosc.i2", async () => {
556-
let res = await read_complete_v3("/1d.contiguous.blosc.i2");
581+
let res = await get_v3("/1d.contiguous.blosc.i2");
557582
expect(res.data).toStrictEqual(new Int16Array([1, 2, 3, 4]));
558583
expect(res.shape).toStrictEqual([4]);
559584
});
560585

561586
it("reads 1d.contiguous.raw.i2", async () => {
562-
let res = await read_complete_v3("/1d.contiguous.raw.i2");
587+
let res = await get_v3("/1d.contiguous.raw.i2");
563588
expect(res.data).toStrictEqual(new Int16Array([1, 2, 3, 4]));
564589
expect(res.shape).toStrictEqual([4]);
565590
});
566591

567592
it("reads 1d.contiguous.i4", async () => {
568-
let res = await read_complete_v3("/1d.contiguous.i4");
593+
let res = await get_v3("/1d.contiguous.i4");
569594
expect(res.data).toStrictEqual(new Int32Array([1, 2, 3, 4]));
570595
expect(res.shape).toStrictEqual([4]);
571596
});
572597

573598
it("reads 1d.contiguous.u1", async () => {
574-
let res = await read_complete_v3("/1d.contiguous.u1");
599+
let res = await get_v3("/1d.contiguous.u1");
575600
expect(res.data).toStrictEqual(new Uint8Array([255, 0, 255, 0]));
576601
expect(res.shape).toStrictEqual([4]);
577602
});
578603

579604
it("reads 1d.contiguous.f4.le", async () => {
580-
let res = await read_complete_v3("/1d.contiguous.f4.le");
605+
let res = await get_v3("/1d.contiguous.f4.le");
581606
expect(res.data).toStrictEqual(new Float32Array([-1000.5, 0, 1000.5, 0]));
582607
expect(res.shape).toStrictEqual([4]);
583608
});
584609

585610
it("reads 1d.contiguous.f4.be", async () => {
586-
let res = await read_complete_v3("/1d.contiguous.f4.be");
611+
let res = await get_v3("/1d.contiguous.f4.be");
587612
expect(res.data).toStrictEqual(new Float32Array([-1000.5, 0, 1000.5, 0]));
588613
expect(res.shape).toStrictEqual([4]);
589614
});
590615

591616
it("reads 1d.contiguous.f8", async () => {
592-
let res = await read_complete_v3("/1d.contiguous.f8");
617+
let res = await get_v3("/1d.contiguous.f8");
593618
expect(res.data).toStrictEqual(new Float64Array([1.5, 2.5, 3.5, 4.5]));
594619
expect(res.shape).toStrictEqual([4]);
595620
});
596621

597622
it("reads 1d.contiguous.b1", async () => {
598-
let res = await read_complete_v3("/1d.contiguous.b1");
623+
let res = await get_v3("/1d.contiguous.b1");
599624
expect(res.data).toBeInstanceOf(BoolArray);
600625
expect(Array.from(res.data as BoolArray)).toStrictEqual([
601626
true,
@@ -607,56 +632,56 @@ describe("get v3", () => {
607632
});
608633

609634
it("reads 2d.contiguous.i2", async () => {
610-
let res = await read_complete_v3("/2d.contiguous.i2");
635+
let res = await get_v3("/2d.contiguous.i2");
611636
expect(res.data).toStrictEqual(new Int16Array([1, 2, 3, 4]));
612637
expect(res.shape).toStrictEqual([2, 2]);
613638
});
614639

615640
it("reads 3d.contiguous.i2", async () => {
616-
let res = await read_complete_v3("/3d.contiguous.i2");
641+
let res = await get_v3("/3d.contiguous.i2");
617642
expect(res.data).toStrictEqual(new Int16Array(range(27)));
618643
expect(res.shape).toStrictEqual([3, 3, 3]);
619644
});
620645

621646
it("reads 1d.chunked.i2", async () => {
622-
let res = await read_complete_v3("/1d.chunked.i2");
647+
let res = await get_v3("/1d.chunked.i2");
623648
expect(res.data).toStrictEqual(new Int16Array([1, 2, 3, 4]));
624649
expect(res.shape).toStrictEqual([4]);
625650
});
626651

627652
it("reads 1d.chunked.ragged.i2", async () => {
628-
let res = await read_complete_v3("/1d.chunked.ragged.i2");
653+
let res = await get_v3("/1d.chunked.ragged.i2");
629654
expect(res.data).toStrictEqual(new Int16Array([1, 2, 3, 4, 5]));
630655
expect(res.shape).toStrictEqual([5]);
631656
});
632657

633658
it("reads 2d.chunked.i2", async () => {
634-
let res = await read_complete_v3("/2d.chunked.i2");
659+
let res = await get_v3("/2d.chunked.i2");
635660
expect(res.data).toStrictEqual(new Int16Array([1, 2, 3, 4]));
636661
expect(res.shape).toStrictEqual([2, 2]);
637662
});
638663

639664
it("reads 2d.chunked.ragged.i2", async () => {
640-
let res = await read_complete_v3("/2d.chunked.ragged.i2");
665+
let res = await get_v3("/2d.chunked.ragged.i2");
641666
expect(res.data).toStrictEqual(new Int16Array(range(1, 10)));
642667
expect(res.shape).toStrictEqual([3, 3]);
643668
});
644669

645670
it("reads 3d.chunked.i2", async () => {
646-
let res = await read_complete_v3("/3d.chunked.i2");
671+
let res = await get_v3("/3d.chunked.i2");
647672
expect(res.data).toStrictEqual(new Int16Array(range(27)));
648673
expect(res.shape).toStrictEqual([3, 3, 3]);
649674
});
650675

651676
it("reads 3d.chunked.mixed.i2.C", async () => {
652-
let res = await read_complete_v3("/3d.chunked.mixed.i2.C");
677+
let res = await get_v3("/3d.chunked.mixed.i2.C");
653678
expect(res.data).toStrictEqual(new Int16Array(range(27)));
654679
expect(res.shape).toStrictEqual([3, 3, 3]);
655680
expect(res.stride).toStrictEqual([9, 3, 1]);
656681
});
657682

658683
it("reads 3d.chunked.mixed.i2.F", async () => {
659-
let res = await read_complete_v3("/3d.chunked.mixed.i2.F");
684+
let res = await get_v3("/3d.chunked.mixed.i2.F");
660685
// deno-fmt-ignore
661686
expect(res.data).toStrictEqual(new Int16Array([
662687
0, 9, 18, 3, 12, 21, 6, 15, 24,
@@ -668,7 +693,7 @@ describe("get v3", () => {
668693
});
669694

670695
it("reads 3d.chunked.mixed.i2.F -- force C", async () => {
671-
let res = await read_complete_v3("/3d.chunked.mixed.i2.F", null, {
696+
let res = await get_v3("/3d.chunked.mixed.i2.F", null, {
672697
order: "C",
673698
});
674699
expect(res.data).toStrictEqual(new Int16Array(range(27)));

0 commit comments

Comments
 (0)