Skip to content

Commit b0b5d31

Browse files
committed
feat: add nested array types
1 parent c7925c4 commit b0b5d31

File tree

2 files changed

+216
-50
lines changed

2 files changed

+216
-50
lines changed

lib/node_modules/@stdlib/types/index.d.ts

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,122 @@ declare module '@stdlib/types/array' {
144144
set( value: T, i?: number ): void;
145145
}
146146

147+
/**
148+
* A one-dimensional array.
149+
*
150+
* @example
151+
* const x: Array1D<number> = [ 1, 2, 3, 4 ];
152+
*/
153+
type Array1D<T> = Array<T>; // FIXME: this should be a collection and should be updated once `Collection` is moved to the array namespace
154+
155+
/**
156+
* A two-dimensional nested array.
157+
*
158+
* ## Notes
159+
*
160+
* - All nested arrays should have the same length (i.e., no ragged arrays).
161+
*
162+
* @example
163+
* const x: Array2D<number> = [ [ 1, 2 ], [ 3, 4 ] ];
164+
*/
165+
type Array2D<T> = Array<Array1D<T>>;
166+
167+
/**
168+
* A three-dimensional nested array.
169+
*
170+
* ## Notes
171+
*
172+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
173+
*
174+
* @example
175+
* const x: Array3D<number> = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
176+
*/
177+
type Array3D<T> = Array<Array2D<T>>;
178+
179+
/**
180+
* A four-dimensional nested array.
181+
*
182+
* ## Notes
183+
*
184+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
185+
*
186+
* @example
187+
* const x: Array4D<number> = [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ];
188+
*/
189+
type Array4D<T> = Array<Array3D<T>>;
190+
191+
/**
192+
* A five-dimensional nested array.
193+
*
194+
* ## Notes
195+
*
196+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
197+
*
198+
* @example
199+
* const x: Array5D<number> = [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ];
200+
*/
201+
type Array5D<T> = Array<Array4D<T>>;
202+
203+
/**
204+
* A six-dimensional nested array.
205+
*
206+
* ## Notes
207+
*
208+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
209+
*
210+
* @example
211+
* const x: Array6D<number> = [ [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] ];
212+
*/
213+
type Array6D<T> = Array<Array5D<T>>;
214+
215+
/**
216+
* A seven-dimensional nested array.
217+
*
218+
* ## Notes
219+
*
220+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
221+
*
222+
* @example
223+
* const x: Array7D<number> = [ [ [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] ] ];
224+
*/
225+
type Array7D<T> = Array<Array6D<T>>;
226+
227+
/**
228+
* An eight-dimensional nested array.
229+
*
230+
* ## Notes
231+
*
232+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
233+
*
234+
* @example
235+
* const x: Array8D<number> = [ [ [ [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] ] ] ];
236+
*/
237+
type Array8D<T> = Array<Array7D<T>>;
238+
239+
/**
240+
* A nine-dimensional nested array.
241+
*
242+
* ## Notes
243+
*
244+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
245+
*
246+
* @example
247+
* const x: Array9D<number> = [ [ [ [ [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] ] ] ] ];
248+
*/
249+
type Array9D<T> = Array<Array8D<T>>;
250+
251+
/**
252+
* A ten-dimensional nested array.
253+
*
254+
* ## Notes
255+
*
256+
* - All nested array lengths should be consistent (i.e., no ragged arrays).
257+
*
258+
* @example
259+
* const x: Array10D<number> = [ [ [ [ [ [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] ] ] ] ] ];
260+
*/
261+
type Array10D<T> = Array<Array9D<T>>;
262+
147263
/**
148264
* A numeric array.
149265
*
@@ -617,7 +733,7 @@ declare module '@stdlib/types/iter' {
617733
*/
618734
declare module '@stdlib/types/ndarray' {
619735
import { ArrayLike, AccessorArrayLike, Complex128Array, Complex64Array, RealOrComplexTypedArray, FloatOrComplexTypedArray, RealTypedArray, ComplexTypedArray, IntegerTypedArray, FloatTypedArray, SignedIntegerTypedArray, UnsignedIntegerTypedArray } from '@stdlib/types/array'; // tslint:disable-line:max-line-length
620-
import { ComplexLike, Complex128, Complex64 } from '@stdlib/types/object';
736+
import { ComplexLike, Complex128, Complex64, Collection } from '@stdlib/types/object';
621737

622738
/**
623739
* Data type.
@@ -698,7 +814,7 @@ declare module '@stdlib/types/ndarray' {
698814
*
699815
* - Each element of the array shape (i.e., dimension size) should be a nonnegative integer.
700816
*/
701-
type Shape = ArrayLike<number>;
817+
type Shape = Collection<number>;
702818

703819
/**
704820
* Array strides.
@@ -707,7 +823,7 @@ declare module '@stdlib/types/ndarray' {
707823
*
708824
* - Each stride (i.e., index increment along a respective dimension) should be an integer.
709825
*/
710-
type Strides = ArrayLike<number>;
826+
type Strides = Collection<number>;
711827

712828
/**
713829
* Interface describing an ndarray.
@@ -2399,7 +2515,7 @@ declare module '@stdlib/types/object' {
23992515
* A collection, which is defined as either an array, typed array, or an array-like object (excluding strings and functions).
24002516
*
24012517
* @example
2402-
* const arr: Collection<number> = [ 1, 2, 3 ];
2518+
* const x: Collection<number> = [ 1, 2, 3 ];
24032519
*/
24042520
type Collection<T = any> = Array<T> | TypedArray | ArrayLike<T>;
24052521

lib/node_modules/@stdlib/types/test.ts

Lines changed: 96 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -191,117 +191,167 @@ function cmplx128Array(): array.Complex128Array {
191191

192192
// The compiler should not throw an error when using array type aliases...
193193
{
194-
const x: array.TypedArray = new Float64Array( 10 );
195-
if ( x[ 0 ] !== 0.0 ) {
194+
const v1: array.TypedArray = new Float64Array( 10 );
195+
if ( v1[ 0 ] !== 0.0 ) {
196196
throw new Error( 'something went wrong' );
197197
}
198198

199-
const y: array.IntegerTypedArray = new Int32Array( 10 );
200-
if ( y[ 0 ] !== 0 ) {
199+
const v2: array.IntegerTypedArray = new Int32Array( 10 );
200+
if ( v2[ 0 ] !== 0 ) {
201201
throw new Error( 'something went wrong' );
202202
}
203203

204-
const z: array.NumericArray = new Float64Array( 10 );
205-
if ( z[ 0 ] !== 0.0 ) {
204+
const v3: array.NumericArray = new Float64Array( 10 );
205+
if ( v3[ 0 ] !== 0.0 ) {
206206
throw new Error( 'something went wrong' );
207207
}
208208

209-
const w: array.ArrayLike<string> = 'beep';
210-
if ( w[ 0 ] !== 'b' ) {
209+
const v4: array.ArrayLike<string> = 'beep';
210+
if ( v4[ 0 ] !== 'b' ) {
211211
throw new Error( 'something went wrong' );
212212
}
213213

214-
const v: array.ArrayLike<number> = [ 1, 2, 3 ];
215-
if ( v[ 0 ] !== 1 ) {
214+
const v5: array.ArrayLike<number> = [ 1, 2, 3 ];
215+
if ( v5[ 0 ] !== 1 ) {
216216
throw new Error( 'something went wrong' );
217217
}
218218

219-
const t: array.ArrayLike<number> = new Int8Array( 10 );
220-
if ( t[ 0 ] !== 1 ) {
219+
const v6: array.ArrayLike<number> = new Int8Array( 10 );
220+
if ( v6[ 0 ] !== 1 ) {
221221
throw new Error( 'something went wrong' );
222222
}
223223

224-
const zz: array.ComplexArrayLike = cmplxArray();
225-
if ( zz.byteOffset !== 0 ) {
224+
const v7: array.ComplexArrayLike = cmplxArray();
225+
if ( v7.byteOffset !== 0 ) {
226226
throw new Error( 'something went wrong' );
227227
}
228228

229-
const z64: array.Complex64Array = cmplx64Array();
230-
if ( z64.byteOffset !== 0 ) {
229+
const v8: array.Complex64Array = cmplx64Array();
230+
if ( v8.byteOffset !== 0 ) {
231231
throw new Error( 'something went wrong' );
232232
}
233233

234-
const z128: array.Complex128Array = cmplx128Array();
235-
if ( z128.byteOffset !== 0 ) {
234+
const v9: array.Complex128Array = cmplx128Array();
235+
if ( v9.byteOffset !== 0 ) {
236236
throw new Error( 'something went wrong' );
237237
}
238238

239-
const zzz: array.ComplexTypedArray = cmplx64Array();
240-
if ( zzz.byteOffset !== 0 ) {
239+
const v10: array.ComplexTypedArray = cmplx64Array();
240+
if ( v10.byteOffset !== 0 ) {
241241
throw new Error( 'something went wrong' );
242242
}
243243

244-
const v1: array.ArrayOrTypedArray = new Float64Array( 10 );
245-
if ( v1[ 0 ] !== 0.0 ) {
244+
const v11: array.ArrayOrTypedArray = new Float64Array( 10 );
245+
if ( v11[ 0 ] !== 0.0 ) {
246246
throw new Error( 'something went wrong' );
247247
}
248248

249-
const v2: array.FloatTypedArray = new Float64Array( 10 );
250-
if ( v2[ 0 ] !== 0.0 ) {
249+
const v12: array.FloatTypedArray = new Float64Array( 10 );
250+
if ( v12[ 0 ] !== 0.0 ) {
251251
throw new Error( 'something went wrong' );
252252
}
253253

254-
const v3: array.RealOrComplexArray = new Float64Array( 10 );
255-
if ( v3[ 0 ] !== 0.0 ) {
254+
const v13: array.RealOrComplexArray = new Float64Array( 10 );
255+
if ( v13[ 0 ] !== 0.0 ) {
256256
throw new Error( 'something went wrong' );
257257
}
258258

259-
const v4: array.RealOrComplexTypedArray = new Float64Array( 10 );
260-
if ( v4[ 0 ] !== 0.0 ) {
259+
const v14: array.RealOrComplexTypedArray = new Float64Array( 10 );
260+
if ( v14[ 0 ] !== 0.0 ) {
261261
throw new Error( 'something went wrong' );
262262
}
263263

264-
const v5buf: array.ArrayLike<number> = new Float64Array( 10 );
265-
const v5: array.AccessorArrayLike<number> = {
264+
const v15buf: array.ArrayLike<number> = new Float64Array( 10 );
265+
const v15: array.AccessorArrayLike<number> = {
266266
'length': 10,
267-
'data': v5buf,
268-
'get': ( i: number ): number => v5buf[ i ],
267+
'data': v15buf,
268+
'get': ( i: number ): number => v15buf[ i ],
269269
'set': ( value: number, i?: number ): void => {
270-
v5buf[ i || 0 ] = value;
270+
v15buf[ i || 0 ] = value;
271271
return;
272272
}
273273
};
274-
if ( v5.length !== 10 ) {
274+
if ( v15.length !== 10 ) {
275275
throw new Error( 'something went wrong' );
276276
}
277277

278-
const v6: array.IntegerTypedArray = new Int32Array( 10 );
279-
if ( v6[ 0 ] !== 0 ) {
278+
const v16: array.IntegerTypedArray = new Int32Array( 10 );
279+
if ( v16[ 0 ] !== 0 ) {
280280
throw new Error( 'something went wrong' );
281281
}
282282

283-
const v7: array.SignedIntegerTypedArray = new Int32Array( 10 );
284-
if ( v7[ 0 ] !== 0 ) {
283+
const v17: array.SignedIntegerTypedArray = new Int32Array( 10 );
284+
if ( v17[ 0 ] !== 0 ) {
285285
throw new Error( 'something went wrong' );
286286
}
287287

288-
const v8: array.UnsignedIntegerTypedArray = new Uint32Array( 10 );
289-
if ( v8[ 0 ] !== 0 ) {
288+
const v18: array.UnsignedIntegerTypedArray = new Uint32Array( 10 );
289+
if ( v18[ 0 ] !== 0 ) {
290290
throw new Error( 'something went wrong' );
291291
}
292292

293-
const v9: array.AnyArray = new Uint32Array( 10 );
294-
if ( v9[ 0 ] !== 0 ) {
293+
const v19: array.AnyArray = new Uint32Array( 10 );
294+
if ( v19[ 0 ] !== 0 ) {
295295
throw new Error( 'something went wrong' );
296296
}
297297

298-
const v10: array.RealTypedArray = new Uint32Array( 10 );
299-
if ( v10[ 0 ] !== 0 ) {
298+
const v20: array.RealTypedArray = new Uint32Array( 10 );
299+
if ( v20[ 0 ] !== 0 ) {
300300
throw new Error( 'something went wrong' );
301301
}
302302

303-
const v11: array.FloatOrComplexTypedArray = new Float64Array( 10 );
304-
if ( v11[ 0 ] !== 0.0 ) {
303+
const v21: array.FloatOrComplexTypedArray = new Float64Array( 10 );
304+
if ( v21[ 0 ] !== 0.0 ) {
305+
throw new Error( 'something went wrong' );
306+
}
307+
308+
const v22: array.Array1D<number> = [ 1, 2, 3 ];
309+
if ( v22.length === 0 ) {
310+
throw new Error( 'something went wrong' );
311+
}
312+
313+
const v23: array.Array2D<number> = [ [ 1, 2, 3 ] ];
314+
if ( v23.length === 0 ) {
315+
throw new Error( 'something went wrong' );
316+
}
317+
318+
const v24: array.Array3D<number> = [ [ [ 1, 2, 3 ] ] ];
319+
if ( v24.length === 0 ) {
320+
throw new Error( 'something went wrong' );
321+
}
322+
323+
const v25: array.Array4D<number> = [ [ [ [ 1, 2, 3 ] ] ] ];
324+
if ( v25.length === 0 ) {
325+
throw new Error( 'something went wrong' );
326+
}
327+
328+
const v26: array.Array5D<number> = [ [ [ [ [ 1, 2, 3 ] ] ] ] ];
329+
if ( v26.length === 0 ) {
330+
throw new Error( 'something went wrong' );
331+
}
332+
333+
const v27: array.Array6D<number> = [ [ [ [ [ [ 1, 2, 3 ] ] ] ] ] ];
334+
if ( v27.length === 0 ) {
335+
throw new Error( 'something went wrong' );
336+
}
337+
338+
const v28: array.Array7D<number> = [ [ [ [ [ [ [ 1, 2, 3 ] ] ] ] ] ] ];
339+
if ( v28.length === 0 ) {
340+
throw new Error( 'something went wrong' );
341+
}
342+
343+
const v29: array.Array8D<number> = [ [ [ [ [ [ [ [ 1, 2, 3 ] ] ] ] ] ] ] ];
344+
if ( v29.length === 0 ) {
345+
throw new Error( 'something went wrong' );
346+
}
347+
348+
const v30: array.Array9D<number> = [ [ [ [ [ [ [ [ [ 1, 2, 3 ] ] ] ] ] ] ] ] ];
349+
if ( v30.length === 0 ) {
350+
throw new Error( 'something went wrong' );
351+
}
352+
353+
const v31: array.Array10D<number> = [ [ [ [ [ [ [ [ [ [ 1, 2, 3 ] ] ] ] ] ] ] ] ] ];
354+
if ( v31.length === 0 ) {
305355
throw new Error( 'something went wrong' );
306356
}
307357
}

0 commit comments

Comments
 (0)