mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-06 20:13:35 +09:00
Here moved our utility polyfill for Map.groupBy into shims module we are using for all stuff we will replace with native methods. Used it in marketplace to group by category.
65 lines
1.2 KiB
TypeScript
65 lines
1.2 KiB
TypeScript
import { expect, test } from "@jest/globals";
|
|
import {
|
|
getMapValuesBy,
|
|
getMapValuesByKeysSet,
|
|
removeByMutable,
|
|
} from "./array-utils";
|
|
|
|
test("removeByMutable", () => {
|
|
const array = [
|
|
{ param: 1 },
|
|
{ param: 2 },
|
|
{ param: 3 },
|
|
{ param: 4 },
|
|
{ param: 5 },
|
|
{ param: 6 },
|
|
];
|
|
removeByMutable(array, (item) => item.param % 2 === 1);
|
|
expect(array).toMatchInlineSnapshot(`
|
|
[
|
|
{
|
|
"param": 2,
|
|
},
|
|
{
|
|
"param": 4,
|
|
},
|
|
{
|
|
"param": 6,
|
|
},
|
|
]
|
|
`);
|
|
removeByMutable(array, (item) => item.param !== 4);
|
|
expect(array).toMatchInlineSnapshot(`
|
|
[
|
|
{
|
|
"param": 4,
|
|
},
|
|
]
|
|
`);
|
|
});
|
|
|
|
test("getMapValuesByKeysSet", () => {
|
|
const map = new Map([
|
|
[1, "value1"],
|
|
[2, "value2"],
|
|
[3, "value3"],
|
|
[4, "value4"],
|
|
[5, "value5"],
|
|
]);
|
|
const keys = new Set([2, 4]);
|
|
expect(getMapValuesByKeysSet(map, keys)).toEqual(["value2", "value4"]);
|
|
});
|
|
|
|
test("getMapValuesBy", () => {
|
|
const map = new Map([
|
|
[1, "value1"],
|
|
[2, "value2"],
|
|
[3, "value3"],
|
|
[4, "value4"],
|
|
[5, "value5"],
|
|
]);
|
|
expect(
|
|
getMapValuesBy(map, (value) => value.includes("3") || value.includes("5"))
|
|
).toEqual(["value3", "value5"]);
|
|
});
|