Skip to content

Commit

Permalink
feat!: move Collection type defn to array type module
Browse files Browse the repository at this point in the history
This commit addresses an awkward inconsistency in which a `Collection`
was included in the `object` type module, despite being defined as
a union of `array` type definitions. By moving `Collection` into
`array` we make the import of `Collection` more intuitive given its
semantic relationship to array-like objects.

BREAKING CHANGE: move `Collection` type defn to array type module

To migrate, users should import `@stdlib/types/array` instead of
`@stdlib/types/object` when using the `Collection` type definition.
  • Loading branch information
kgryte committed Aug 17, 2023
1 parent 32a2827 commit bde4671
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
21 changes: 10 additions & 11 deletions lib/node_modules/@stdlib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ declare module '@stdlib/types/array' {
* @example
* const x: Array1D<number> = [ 1, 2, 3, 4 ];
*/
type Array1D<T> = Array<T>; // FIXME: this should be a collection and should be updated once `Collection` is moved to the array namespace
type Array1D<T> = Collection<T>;

/**
* A two-dimensional nested array.
Expand Down Expand Up @@ -525,6 +525,14 @@ declare module '@stdlib/types/array' {
*/
set( value: ArrayLike<number | ComplexLike> | Complex128Array | ComplexLike, i?: number ): void;
}

/**
* A collection, which is defined as either an array, typed array, or an array-like object (excluding strings and functions).
*
* @example
* const x: Collection<number> = [ 1, 2, 3 ];
*/
type Collection<T = any> = Array<T> | TypedArray | ArrayLike<T>;
}

/**
Expand Down Expand Up @@ -732,9 +740,8 @@ declare module '@stdlib/types/iter' {
* };
*/
declare module '@stdlib/types/ndarray' {
import { ArrayLike, AccessorArrayLike, Complex128Array, Complex64Array, RealOrComplexTypedArray, FloatOrComplexTypedArray, RealTypedArray, ComplexTypedArray, IntegerTypedArray, FloatTypedArray, SignedIntegerTypedArray, UnsignedIntegerTypedArray } from '@stdlib/types/array';
import { ArrayLike, AccessorArrayLike, Collection, Complex128Array, Complex64Array, RealOrComplexTypedArray, FloatOrComplexTypedArray, RealTypedArray, ComplexTypedArray, IntegerTypedArray, FloatTypedArray, SignedIntegerTypedArray, UnsignedIntegerTypedArray } from '@stdlib/types/array';
import { ComplexLike, Complex128, Complex64 } from '@stdlib/types/complex';
import { Collection } from '@stdlib/types/object';

/**
* Data type.
Expand Down Expand Up @@ -2511,14 +2518,6 @@ declare module '@stdlib/types/object' {
* const prop: PropertyName = 'foo';
*/
type PropertyName = string | symbol;

/**
* A collection, which is defined as either an array, typed array, or an array-like object (excluding strings and functions).
*
* @example
* const x: Collection<number> = [ 1, 2, 3 ];
*/
type Collection<T = any> = Array<T> | TypedArray | ArrayLike<T>;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ function cmplx128Array(): array.Complex128Array {
if ( v31.length === 0 ) {
throw new Error( 'something went wrong' );
}

const v32: array.Collection<number> = [ 1, 2, 3 ];
if ( v32.length !== 3 ) {
throw new Error( 'something went wrong' );
}
}

// The compiler should not throw an error when using iterator or iterable types...
Expand Down Expand Up @@ -534,11 +539,6 @@ function cmplx128Array(): array.Complex128Array {
if ( prop !== 'foo' ) {
throw new Error( 'something went wrong' );
}

const arr: obj.Collection<number> = [ 1, 2, 3 ];
if ( arr.length !== 3 ) {
throw new Error( 'something went wrong' );
}
}

// The compiler should not throw an error when using complex number types...
Expand Down

1 comment on commit bde4671

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
types $\color{green}107/107$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}0/0$
$\color{green}+100.00\%$
$\color{green}107/107$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.