mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix: add missing Object.groupBy()
and Map.groupBy()
types (#21050)
This commit is contained in:
parent
58d543a480
commit
d7348c870a
4 changed files with 81 additions and 0 deletions
|
@ -251,6 +251,7 @@ mod ts {
|
|||
"esnext.decorators",
|
||||
"esnext.disposable",
|
||||
"esnext.intl",
|
||||
"esnext.object",
|
||||
];
|
||||
|
||||
let path_dts = cwd.join("tsc/dts");
|
||||
|
|
|
@ -170,3 +170,50 @@ Deno.test(async function arrayFromAsync() {
|
|||
const b = await Array.fromAsync(new Map([[1, 2], [3, 4]]));
|
||||
assertEquals(b, [[1, 2], [3, 4]]);
|
||||
});
|
||||
|
||||
// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy#examples
|
||||
Deno.test(function objectGroupBy() {
|
||||
const inventory = [
|
||||
{ name: "asparagus", type: "vegetables", quantity: 5 },
|
||||
{ name: "bananas", type: "fruit", quantity: 0 },
|
||||
{ name: "goat", type: "meat", quantity: 23 },
|
||||
{ name: "cherries", type: "fruit", quantity: 5 },
|
||||
{ name: "fish", type: "meat", quantity: 22 },
|
||||
];
|
||||
const result = Object.groupBy(inventory, ({ type }) => type);
|
||||
assertEquals(result, {
|
||||
vegetables: [
|
||||
{ name: "asparagus", type: "vegetables", quantity: 5 },
|
||||
],
|
||||
fruit: [
|
||||
{ name: "bananas", type: "fruit", quantity: 0 },
|
||||
{ name: "cherries", type: "fruit", quantity: 5 },
|
||||
],
|
||||
meat: [
|
||||
{ name: "goat", type: "meat", quantity: 23 },
|
||||
{ name: "fish", type: "meat", quantity: 22 },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy#examples
|
||||
Deno.test(function mapGroupBy() {
|
||||
const inventory = [
|
||||
{ name: "asparagus", type: "vegetables", quantity: 9 },
|
||||
{ name: "bananas", type: "fruit", quantity: 5 },
|
||||
{ name: "goat", type: "meat", quantity: 23 },
|
||||
{ name: "cherries", type: "fruit", quantity: 12 },
|
||||
{ name: "fish", type: "meat", quantity: 22 },
|
||||
];
|
||||
const restock = { restock: true };
|
||||
const sufficient = { restock: false };
|
||||
const result = Map.groupBy(
|
||||
inventory,
|
||||
({ quantity }) => quantity < 6 ? restock : sufficient,
|
||||
);
|
||||
assertEquals(result.get(restock), [{
|
||||
name: "bananas",
|
||||
type: "fruit",
|
||||
quantity: 5,
|
||||
}]);
|
||||
});
|
||||
|
|
1
cli/tsc/dts/lib.esnext.d.ts
vendored
1
cli/tsc/dts/lib.esnext.d.ts
vendored
|
@ -19,5 +19,6 @@ and limitations under the License.
|
|||
/// <reference lib="es2023" />
|
||||
/// <reference lib="esnext.array" />
|
||||
/// <reference lib="esnext.intl" />
|
||||
/// <reference lib="esnext.object" />
|
||||
/// <reference lib="esnext.decorators" />
|
||||
/// <reference lib="esnext.disposable" />
|
||||
|
|
32
cli/tsc/dts/lib.esnext.object.d.ts
vendored
Normal file
32
cli/tsc/dts/lib.esnext.object.d.ts
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*! *****************************************************************************
|
||||
Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
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
|
||||
|
||||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
||||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
||||
MERCHANTABLITY OR NON-INFRINGEMENT.
|
||||
|
||||
See the Apache Version 2.0 License for specific language governing permissions
|
||||
and limitations under the License.
|
||||
***************************************************************************** */
|
||||
|
||||
/// <reference no-default-lib="true"/>
|
||||
|
||||
// NOTE(iuioiua): taken from https://github.com/microsoft/TypeScript/issues/47171#issuecomment-1697373352
|
||||
// while we wait for these types to officially ship
|
||||
interface ObjectConstructor {
|
||||
groupBy<Item, Key extends PropertyKey>(
|
||||
items: Iterable<Item>,
|
||||
keySelector: (item: Item, index: number) => Key,
|
||||
): Record<Key, Item[]>;
|
||||
}
|
||||
|
||||
interface MapConstructor {
|
||||
groupBy<Item, Key>(
|
||||
items: Iterable<Item>,
|
||||
keySelector: (item: Item, index: number) => Key,
|
||||
): Map<Key, Item[]>;
|
||||
}
|
Loading…
Reference in a new issue