Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Oct 6, 2024
1 parent 9d7924e commit 97e15a5
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 37 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<section class="release" id="unreleased">

## Unreleased (2024-09-29)
## Unreleased (2024-10-06)

<section class="packages">

Expand Down Expand Up @@ -84,11 +84,12 @@

### Contributors

A total of 3 people contributed to this release. Thank you to the following contributors:
A total of 4 people contributed to this release. Thank you to the following contributors:

- Athan Reines
- Karthik Prakash
- Philipp Burckhardt
- Shubh Mehta

</section>

Expand All @@ -100,6 +101,7 @@ A total of 3 people contributed to this release. Thank you to the following cont

<details>

- [`ec4730b`](https://github.com/stdlib-js/stdlib/commit/ec4730b90a45d2c968405a5d158d7a52b1389272) - **refactor:** improve type specificity for `utils/group-own` _(by Shubh Mehta, Philipp Burckhardt)_
- [`2c4e5d8`](https://github.com/stdlib-js/stdlib/commit/2c4e5d824e0c5dc8fd536bf79ff565cee100ce46) - **build:** disable additional lint rule in TS tests _(by Philipp Burckhardt)_
- [`abf0407`](https://github.com/stdlib-js/stdlib/commit/abf040787f6598438b0100a729a8331b7f80f62f) - **chore:** resolve lint errors in TS files _(by Philipp Burckhardt)_
- [`b89c97c`](https://github.com/stdlib-js/stdlib/commit/b89c97ce0b812ff0b2aab16b4d77969d44fe3e8c) - **docs:** resolve lint errors in TS declaration files _(by Philipp Burckhardt)_
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Contributors listed in alphabetical order.

Aayush Khanna <[email protected].com>
Aayush Khanna <aayushiitbhu23@gmail.com>
Adarsh Palaskar <[email protected]>
Aditya Sapra <[email protected]>
AgPriyanshu18 <[email protected]>
Expand Down
132 changes: 102 additions & 30 deletions group-own/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ interface Options {
*
* @returns group key
*/
type Nullary = () => string | symbol;
type Nullary<K extends string | symbol> = () => K;

/**
* Specifies which group a property belongs to.
*
* @param value - object value
* @returns group key
*/
type Unary = ( value: any ) => string | symbol;
type Unary<V, K extends string | symbol> = ( value: V ) => K;

/**
* Specifies which group a property belongs to.
Expand All @@ -55,7 +55,7 @@ type Unary = ( value: any ) => string | symbol;
* @param key - object key
* @returns group key
*/
type Binary = ( value: any, key: string | symbol ) => string | symbol;
type Binary<V, K extends string | symbol> = ( value: V, key: string ) => K;

/**
* Specifies which group a property belongs to.
Expand All @@ -64,7 +64,7 @@ type Binary = ( value: any, key: string | symbol ) => string | symbol;
* @param key - object key
* @returns group key
*/
type Indicator = Nullary | Unary | Binary;
type Indicator<V, K extends string | symbol> = Nullary<K> | Unary<V, K> | Binary<V, K>;

/**
* Groups an object's own property values according to an indicator function.
Expand Down Expand Up @@ -93,15 +93,18 @@ type Indicator = Nullary | Unary | Binary;
* return v[ 0 ];
* }
* var obj = {
* 'a': 'beep',
* 'b': 'boop',
* 'c': 'foo',
* 'd': 'bar'
* 'a': 'apple',
* 'b': 'banana',
* 'c': 'cherry',
* 'd': 'date'
* };
* var out = groupOwn( obj, indicator );
* // e.g., returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
* // e.g., returns { 'a': [ 'apple' ], 'b': [ 'banana' ], 'c': [ 'cherry' ], 'd': [ 'date' ] }
*/
declare function groupOwn( obj: any, indicator: Indicator ): any;
declare function groupOwn<T extends object, K extends string | symbol>(
obj: T,
indicator: Indicator<T[keyof T], K>
): { [P in K]: Array<T[keyof T]> };

/**
* Groups an object's own property values according to an indicator function.
Expand All @@ -124,7 +127,7 @@ declare function groupOwn( obj: any, indicator: Indicator ): any;
* @param obj - input object
* @param options - function options
* @param options.thisArg - execution context
* @param options.returns - if `values`, values are returned; if `keys`, keys are returned; if `*`, both keys and values are returned (default: 'values')
* @param options.returns - if `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned (default: 'values')
* @param indicator - indicator function indicating which group an element in the input object belongs to
* @returns group results
*
Expand All @@ -133,47 +136,116 @@ declare function groupOwn( obj: any, indicator: Indicator ): any;
* return v[ 0 ];
* }
* var obj = {
* 'a': 'beep',
* 'b': 'boop',
* 'c': 'foo',
* 'd': 'bar'
* 'a': 'apple',
* 'b': 'banana',
* 'c': 'cherry',
* 'd': 'date'
* };
* var out = groupOwn( obj, indicator );
* // e.g., returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
* var opts = {
* 'returns': 'indices'
* };
* var out = groupOwn( obj, opts, indicator );
* // e.g., returns { 'a': [ 'a' ], 'b': [ 'b' ], 'c': [ 'c' ], 'd': [ 'd' ] }
*/
declare function groupOwn<T extends object, K extends string | symbol>(
obj: T,
options: Options & { returns: 'indices' },
indicator: Indicator<T[keyof T], K>
): { [P in K]: Array<keyof T> };

/**
* Groups an object's own property values according to an indicator function.
*
* ## Notes
*
* - When invoked, the indicator function is provided two arguments:
*
* - `value`: object value
* - `key`: object key
*
* - The value returned by an indicator function should be a value which can be serialized as an object key.
*
* - If provided an empty object, the function returns an empty object.
*
* - The function iterates over an object's own properties.
*
* - Key iteration order is **not** guaranteed, and, thus, result order is **not** guaranteed.
*
* @param obj - input object
* @param options - function options
* @param options.thisArg - execution context
* @param options.returns - if `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned (default: 'values')
* @param indicator - indicator function indicating which group an element in the input object belongs to
* @returns group results
*
* @example
* function indicator( v ) {
* return v[ 0 ];
* }
* var obj = {
* 'a': 'beep',
* 'b': 'boop',
* 'c': 'foo',
* 'd': 'bar'
* 'a': 'apple',
* 'b': 'banana',
* 'c': 'cherry',
* 'd': 'date'
* };
* var opts = {
* 'returns': 'keys'
* 'returns': '*'
* };
* var out = groupOwn( obj, opts, indicator );
* // e.g., returns { 'b': [ 'a', 'b', 'd' ], 'f': [ 'c' ] }
* // e.g., returns { 'a': [ [ 'a', 'apple' ] ], 'b': [ [ 'b', 'banana' ] ], 'c': [ [ 'c', 'cherry' ] ], 'd': [ [ 'd', 'date' ] ] }
*/
declare function groupOwn<T extends object, K extends string | symbol>(
obj: T,
options: Options & { returns: '*' },
indicator: Indicator<T[keyof T], K>
): { [P in K]: Array<[keyof T, T[keyof T]]> };

/**
* Groups an object's own property values according to an indicator function.
*
* ## Notes
*
* - When invoked, the indicator function is provided two arguments:
*
* - `value`: object value
* - `key`: object key
*
* - The value returned by an indicator function should be a value which can be serialized as an object key.
*
* - If provided an empty object, the function returns an empty object.
*
* - The function iterates over an object's own properties.
*
* - Key iteration order is **not** guaranteed, and, thus, result order is **not** guaranteed.
*
* @param obj - input object
* @param options - function options
* @param options.thisArg - execution context
* @param options.returns - if `'values'`, values are returned; if `'indices'`, indices are returned; if `'*'`, both indices and values are returned (default: 'values')
* @param indicator - indicator function indicating which group an element in the input object belongs to
* @returns group results
*
* @example
* function indicator( v ) {
* return v[ 0 ];
* }
* var obj = {
* 'a': 'beep',
* 'b': 'boop',
* 'c': 'foo',
* 'd': 'bar'
* 'a': 'apple',
* 'b': 'banana',
* 'c': 'cherry',
* 'd': 'date'
* };
* var opts = {
* 'returns': '*'
* 'returns': 'values'
* };
* var out = groupOwn( obj, opts, indicator );
* // e.g., returns { 'b': [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], 'f': [ [ 'c', 'foo' ] ] }
* // e.g., returns { 'a': [ 'apple' ], 'b': [ 'banana' ], 'c': [ 'cherry' ], 'd': [ 'date' ] }
*/
declare function groupOwn( obj: any, options: Options, indicator: Indicator ): any;
declare function groupOwn<T extends object, K extends string | symbol>(
obj: T,
options: Options & { returns?: 'values' },
indicator: Indicator<T[keyof T], K>
): { [P in K]: Array<T[keyof T]> };


// EXPORTS //
Expand Down
27 changes: 23 additions & 4 deletions group-own/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,31 @@ class Foo {
// The function returns an object...
{
const obj = new Foo();
groupOwn( obj, indicator ); // $ExpectType any
groupOwn( {}, indicator ); // $ExpectType any
groupOwn( obj, indicator ); // $ExpectType { [x: string]: string[]; }
groupOwn( {}, indicator ); // $ExpectType { [x: string]: never[]; }
const opts = {
'returns': 'indices' as 'indices'
'returns': 'indices' as const
};
groupOwn( obj, opts, indicator ); // $ExpectType any
groupOwn( obj, opts, indicator ); // $ExpectType { [x: string]: (keyof Foo)[]; }

const opts2 = {
'returns': 'values' as const
};
groupOwn( obj, opts2, indicator ); // $ExpectType { [x: string]: string[]; }

const opts3 = {
'returns': '*' as const
};
groupOwn( obj, opts3, indicator ); // $ExpectType { [x: string]: [keyof Foo, string][]; }

const obj2 = {
'beep': 'boop',
'foo': 'bar'
} as const;

groupOwn( obj2, opts, indicator ); // $ExpectType { [x: string]: ("beep" | "foo")[]; }
groupOwn( obj2, opts2, indicator ); // $ExpectType { [x: string]: ("boop" | "bar")[]; }
groupOwn( obj2, opts3, indicator ); // $ExpectType { [x: string]: ["beep" | "foo", "boop" | "bar"][]; }
}

// The compiler throws an error if the function is provided a last argument which is not a function...
Expand Down

0 comments on commit 97e15a5

Please sign in to comment.