Skip to content

Commit d38a5ca

Browse files
committed
Auto-generated commit
1 parent 42a19cd commit d38a5ca

File tree

8 files changed

+54
-31
lines changed

8 files changed

+54
-31
lines changed

.github/.keepalive

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/workflows/productionize.yml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,6 @@ jobs:
8282
id: transform-error-messages
8383
uses: stdlib-js/transform-errors-action@main
8484

85-
# Format error messages:
86-
- name: 'Replace double quotes with single quotes in rewritten format string error messages'
87-
run: |
88-
find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \;
89-
90-
# Format string literal error messages:
91-
- name: 'Replace double quotes with single quotes in rewritten string literal error messages'
92-
run: |
93-
find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \;
94-
95-
# Format code:
96-
- name: 'Replace double quotes with single quotes in inserted `require` calls'
97-
run: |
98-
find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \;
99-
10085
# Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency:
10186
- name: 'Update dependencies in package.json'
10287
run: |

dist/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference path="../docs/types/index.d.ts" />
2+
import countBy from '../docs/types/index';
3+
export = countBy;

dist/index.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/repl.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
When invoked, the indicator function is provided two arguments:
66

7-
- `value`: collection value
8-
- `index`: collection index
7+
- value: collection value
8+
- index: collection index
99

1010
The value returned by an indicator function should be a value which can be
1111
serialized as an object key.

docs/types/index.d.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { Collection } from '@stdlib/types/object';
23+
import { Collection } from '@stdlib/types/array';
2424

2525
/**
2626
* Interface defining function options.
2727
*/
28-
interface Options {
28+
interface Options<T> {
2929
/**
3030
* Execution context.
3131
*/
32-
thisArg?: any;
32+
thisArg?: ThisParameterType<Indicator<T>>;
3333
}
3434

3535
/**
3636
* Specifies which group an element in the input collection belongs to.
3737
*
3838
* @returns object key
3939
*/
40-
type Nullary = () => string | symbol;
40+
type Nullary = () => string | symbol | number;
4141

4242
/**
4343
* Specifies which group an element in the input collection belongs to.
4444
*
4545
* @param value - collection value
4646
* @returns object key
4747
*/
48-
type Unary = ( value: any ) => string | symbol;
48+
type Unary<T> = ( value: T ) => string | symbol | number;
4949

5050
/**
5151
* Specifies which group an element in the input collection belongs to.
@@ -54,7 +54,7 @@ type Unary = ( value: any ) => string | symbol;
5454
* @param index - collection index
5555
* @returns object key
5656
*/
57-
type Binary = ( value: any, index: number ) => string | symbol;
57+
type Binary<T> = ( value: T, index: number ) => string | symbol | number;
5858

5959
/**
6060
* Specifies which group an element in the input collection belongs to.
@@ -63,7 +63,17 @@ type Binary = ( value: any, index: number ) => string | symbol;
6363
* @param index - collection index
6464
* @returns object key
6565
*/
66-
type Indicator = Nullary | Unary | Binary;
66+
type Indicator<T> = Nullary | Unary<T> | Binary<T>;
67+
68+
/**
69+
* Interface describing returned results.
70+
*/
71+
interface Results<T> {
72+
/**
73+
* Object properties.
74+
*/
75+
[key: string | symbol | number]: T;
76+
}
6777

6878
/**
6979
* Groups values according to an indicator function and returns group counts.
@@ -92,7 +102,7 @@ type Indicator = Nullary | Unary | Binary;
92102
* var out = countBy( arr, indicator );
93103
* // returns { 'b': 3, 'f': 1 }
94104
*/
95-
declare function countBy( collection: Collection, indicator: Indicator ): any;
105+
declare function countBy<T = unknown>( collection: Collection<T>, indicator: Indicator<T> ): Results<T>;
96106

97107
/**
98108
* Groups values according to an indicator function and returns group counts.
@@ -123,7 +133,7 @@ declare function countBy( collection: Collection, indicator: Indicator ): any;
123133
* var out = countBy( arr, indicator );
124134
* // returns { 'b': 3, 'f': 1 }
125135
*/
126-
declare function countBy( collection: Collection, options: Options, indicator: Indicator ): any; // tslint-disable-line max-line-length
136+
declare function countBy<T = unknown>( collection: Collection<T>, options: Options<T>, indicator: Indicator<T> ): Results<T>;
127137

128138

129139
// EXPORTS //

docs/types/test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,28 @@
1818

1919
import countBy = require( './index' );
2020

21-
const indicator = ( v: string ): string => v[ 0 ];
21+
/**
22+
* Indicator function.
23+
*
24+
* @param v - value
25+
* @returns indicator
26+
*/
27+
function indicator( v: string ): string {
28+
return v[ 0 ];
29+
}
2230

2331

2432
// TESTS //
2533

2634
// The function returns an object...
2735
{
28-
countBy( [ 'beep', 'boop', 'foo', 'bar' ], indicator ); // $ExpectType any
29-
countBy( [], indicator ); // $ExpectType any
36+
countBy( [ 'beep', 'boop', 'foo', 'bar' ], indicator ); // $ExpectType Results<string>
37+
countBy( [], indicator ); // $ExpectType Results<string>
38+
3039
const opts = {
3140
'thisArg': {}
3241
};
33-
countBy( [ 'beep', 'boop', 'foo', 'bar' ], opts, indicator ); // $ExpectType any
42+
countBy( [ 'beep', 'boop', 'foo', 'bar' ], opts, indicator ); // $ExpectType Results<string>
3443
}
3544

3645
// The compiler throws an error if the function is provided a first argument which is not a collection...
@@ -43,6 +52,7 @@ const indicator = ( v: string ): string => v[ 0 ];
4352
// The compiler throws an error if the function is provided a last argument which is not a function...
4453
{
4554
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
55+
4656
countBy( arr, false ); // $ExpectError
4757
countBy( arr, true ); // $ExpectError
4858
countBy( arr, 32 ); // $ExpectError
@@ -61,12 +71,14 @@ const indicator = ( v: string ): string => v[ 0 ];
6171
// The compiler throws an error if the function is provided an options argument which is not an object...
6272
{
6373
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
74+
6475
countBy( arr, null, indicator ); // $ExpectError
6576
}
6677

6778
// The compiler throws an error if the function is provided an invalid number of arguments...
6879
{
6980
const arr = [ 'beep', 'boop', 'foo', 'bar' ];
81+
7082
countBy(); // $ExpectError
7183
countBy( arr ); // $ExpectError
7284
countBy( arr, {}, indicator, 16 ); // $ExpectError

0 commit comments

Comments
 (0)