-
-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
026ee8b
commit 6ad93a9
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
lib/node_modules/@stdlib/utils/reduce/docs/types/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2019 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 2.0 | ||
|
||
/// <reference types="@stdlib/types"/> | ||
|
||
import { Collection } from '@stdlib/types/object'; | ||
|
||
/** | ||
* Function applied against an accumulator. | ||
* | ||
* @returns accumulator value | ||
*/ | ||
type Nullary = () => void; | ||
|
||
/** | ||
* Function applied against an accumulator. | ||
* | ||
* @param accumulator - accumulated value | ||
* @returns accumulator value | ||
*/ | ||
type Unary = ( accumulator: any ) => any; | ||
|
||
/** | ||
* Function applied against an accumulator. | ||
* | ||
* @param accumulator - accumulated value | ||
* @param value - collection value | ||
* @returns accumulator value | ||
*/ | ||
type Binary = ( accumulator: any, value: any ) => any; | ||
|
||
/** | ||
* Function applied against an accumulator. | ||
* | ||
* @param accumulator - accumulated value | ||
* @param value - collection value | ||
* @param index - collection index | ||
* @returns accumulator value | ||
*/ | ||
type Tertiary = ( accumulator: any, value: any, index: number ) => any; | ||
|
||
/** | ||
* Function applied against an accumulator. | ||
* | ||
* @param accumulator - accumulated value | ||
* @param value - collection value | ||
* @param index - collection index | ||
* @param collection - the input collection | ||
* @returns accumulator value | ||
*/ | ||
type Quaternary = ( accumulator: any, value: any, index: number, collection: Collection ) => any; // tslint-disable-line max-line-length | ||
|
||
/** | ||
* Function applied against an accumulator. | ||
* | ||
* @param accumulator - accumulated value | ||
* @param value - collection value | ||
* @param index - collection index | ||
* @param collection - the input collection | ||
* @returns accumulator value | ||
*/ | ||
type Reducer = Nullary | Unary | Binary | Tertiary | Quaternary; | ||
|
||
/** | ||
* Applies a function against an accumulator and each element in a collection and returns the accumulated result. | ||
* | ||
* ## Notes | ||
* | ||
* - When invoked, the reduction function is provided four arguments: | ||
* | ||
* - `accumulator`: accumulated value | ||
* - `value`: collection value | ||
* - `index`: collection index | ||
* - `collection`: the input collection | ||
* | ||
* - If provided an empty collection, the function returns the initial value. | ||
* | ||
* @param collection - input collection | ||
* @param initial - initial value | ||
* @param reducer - reduction function | ||
* @param thisArg - reduction function execution context | ||
* @returns accumulated result | ||
* | ||
* @example | ||
* function sum( acc, value ) { | ||
* return acc + value; | ||
* } | ||
* | ||
* var arr = [ 1, 2, 3, 4 ]; | ||
* | ||
* var s = reduce( arr, 0, sum ); | ||
* // returns 10 | ||
*/ | ||
declare function reduce( collection: Collection, initial: any, reducer: Reducer, thisArg?: any ): any; // tslint-disable-line max-line-length | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = reduce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2019 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import reduce = require( './index' ); | ||
|
||
const sum = ( acc: number, value: number ): number => { | ||
return acc + value; | ||
}; | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns the accumulated value... | ||
{ | ||
reduce( [ 0, 1, 1, NaN, 2 ], 0, sum ); // $ExpectType any | ||
reduce( [ -1, 1, 2 ], 100, sum ); // $ExpectType any | ||
reduce( [ -1, 1, 2 ], 0, sum, {} ); // $ExpectType any | ||
} | ||
|
||
// The compiler throws an error if the function is provided a first argument which is not a collection... | ||
{ | ||
reduce( 2, 0, sum ); // $ExpectError | ||
reduce( false, 0, sum ); // $ExpectError | ||
reduce( true, 0, sum ); // $ExpectError | ||
reduce( {}, 0, sum ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a third argument which is not a function... | ||
{ | ||
reduce( [ 0, 1, 1, NaN, 2 ], 0, 2 ); // $ExpectError | ||
reduce( [ 0, 1, 1, NaN, 2 ], 0, false ); // $ExpectError | ||
reduce( [ 0, 1, 1, NaN, 2 ], 0, true ); // $ExpectError | ||
reduce( [ 0, 1, 1, NaN, 2 ], 0, 'abc' ); // $ExpectError | ||
reduce( [ 0, 1, 1, NaN, 2 ], 0, {} ); // $ExpectError | ||
reduce( [ 0, 1, 1, NaN, 2 ], 0, [] ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an invalid number of arguments... | ||
{ | ||
reduce(); // $ExpectError | ||
reduce( [ 1, 2, 3 ] ); // $ExpectError | ||
reduce( [ 1, 2, 3 ], 0 ); // $ExpectError | ||
reduce( [ 1, 2, 3 ], 0, sum, {}, 3 ); // $ExpectError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters