Skip to content

Commit

Permalink
chore(console): remove lodash.uniqby in favor of a leaner implement…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
skyrpex committed Aug 2, 2024
1 parent a8d5057 commit 67efa6e
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 234 deletions.
3 changes: 1 addition & 2 deletions apps/wing-console/console/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/lodash.throttle": "^4.1.9",
"@types/lodash.uniqby": "^4.7.9",
"@types/ws": "^8.5.10",
"@vitest/coverage-v8": "^1.6.0",
"@wingconsole/error-message": "workspace:^",
"@wingconsole/eslint-plugin": "workspace:^",
"@wingconsole/tsconfig": "workspace:^",
"@wingconsole/uniq-by": "workspace:^",
"bump-pack": "workspace:^",
"chokidar": "^3.6.0",
"constructs": "^10.3.0",
Expand All @@ -47,7 +47,6 @@
"express": "^4.19.2",
"get-port": "^6.1.2",
"lodash.throttle": "^4.1.1",
"lodash.uniqby": "^4.7.0",
"nanoid": "^4.0.2",
"node-fetch": "^3.3.2",
"tsup": "^8.1.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/wing-console/console/server/src/router/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TRPCError } from "@trpc/server";
import { observable } from "@trpc/server/observable";
import { uniqBy } from "@wingconsole/uniq-by";
import type { UIComponent } from "@winglang/sdk/lib/core/tree.js";
import type { ResourceRunningState } from "@winglang/sdk/lib/simulator/simulator.js";
import uniqby from "lodash.uniqby";
import { z } from "zod";

import type { LogEntry } from "../consoleLogger.js";
Expand Down Expand Up @@ -303,7 +303,7 @@ export const createAppRouter = () => {
// we keep only one connection per resource and direction (because the SDK currently has
// no way to distinguish between multiple connections to the same resource).
// Also, we need to filter out connections to hidden nodes.
const connections = uniqby(simulator.connections(), (connection) => {
const connections = uniqBy(simulator.connections(), (connection) => {
return `${connection.source}-${connection.target}`;
}).filter((connection) => {
return connectionsBasicFilter(connection, nodeMap, !!showTests);
Expand Down
3 changes: 1 addition & 2 deletions apps/wing-console/console/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"linkifyjs": "^4.1.3",
"lodash.debounce": "^4.0.8",
"lodash.escape": "^4.0.1",
"lodash.uniqby": "^4.7.0",
"nanoid": "^4.0.2",
"react-dom": "^18.3.1",
"react-markdown": "^9.0.1",
Expand All @@ -68,14 +67,14 @@
"@types/d3-selection": "^3.0.10",
"@types/d3-zoom": "^3.0.8",
"@types/lodash.debounce": "^4.0.9",
"@types/lodash.uniqby": "^4.7.9",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react-swc": "^3.7.0",
"@wingconsole/design-system": "workspace:^",
"@wingconsole/eslint-plugin": "workspace:^",
"@wingconsole/server": "workspace:^",
"@wingconsole/tsconfig": "workspace:^",
"@wingconsole/uniq-by": "workspace:^",
"@wingconsole/use-loading": "workspace:^",
"@wingconsole/use-persistent-state": "workspace:^",
"@winglang/sdk": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uniqby from "lodash.uniqby";
import { uniqBy } from "@wingconsole/uniq-by";

export type Connection<T> = {
source: T;
Expand Down Expand Up @@ -27,12 +27,12 @@ const resolveConnections = <T>(
(c) => getNodeId(c[type]) === getNodeId(node),
);
const invertedType = type === "source" ? "target" : "source";
const nodes = uniqby(
const nodes = uniqBy(
connections.map((connection) => connection[invertedType]),
(node) => getNodeId(node),
);

return uniqby(
return uniqBy(
nodes.flatMap((node) => {
return resolveConnections(
node,
Expand Down Expand Up @@ -77,7 +77,7 @@ export interface BridgeConnectionsOptions<T> {
}

export const bridgeConnections = <T>(options: BridgeConnectionsOptions<T>) => {
return uniqby(
return uniqBy(
options.connections.flatMap((connection) => {
const sources = resolveConnections(
connection.source,
Expand All @@ -95,7 +95,7 @@ export const bridgeConnections = <T>(options: BridgeConnectionsOptions<T>) => {
options.getNodeId,
options.resolveNode,
);
return uniqby(
return uniqBy(
sources.flatMap((source) => {
return targets.map((target) => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { MapItem } from "@wingconsole/server";
import type { ConnectionData } from "@winglang/sdk/lib/simulator/index.js";
import { uniqBy } from "@wingconsole/uniq-by";
import type { ElkExtendedEdge } from "elkjs";
import uniqBy from "lodash.uniqby";
import { useCallback, useEffect, useMemo } from "react";
import { useCallback, useMemo } from "react";

import { trpc } from "../../trpc.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ResourceIcon } from "@wingconsole/design-system";
import type { ExplorerItem } from "@wingconsole/server";
import uniqBy from "lodash.uniqby";
import { uniqBy } from "@wingconsole/uniq-by";
import type { ReactNode } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useEffect, useRef, useState } from "react";

import { trpc } from "../../trpc.js";
import { RunningStateIndicator } from "../running-state-indicator/running-state-indicator.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
useTheme,
} from "@wingconsole/design-system";
import type { LogLevel } from "@wingconsole/server";
import { uniqBy } from "@wingconsole/uniq-by";
import classNames from "classnames";
import debounce from "lodash.debounce";
import uniqby from "lodash.uniqby";
import { memo, useCallback, useEffect, useMemo, useState } from "react";

export const LOG_LEVELS: LogLevel[] = ["verbose", "info", "warn", "error"];
Expand Down Expand Up @@ -154,7 +154,7 @@ export const ConsoleLogsFilters = memo(
if (!resources) {
return [];
}
const resourceTypes = uniqby(
const resourceTypes = uniqBy(
resources
.sort((a, b) => a.type?.localeCompare(b.type ?? "") ?? 0)
.filter((resource) => resource.type !== undefined),
Expand Down
1 change: 1 addition & 0 deletions apps/wing-console/packages/uniq-by/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
4 changes: 4 additions & 0 deletions apps/wing-console/packages/uniq-by/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": ["plugin:@wingconsole/browser"]
}
23 changes: 23 additions & 0 deletions apps/wing-console/packages/uniq-by/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@wingconsole/uniq-by",
"version": "0.0.0",
"private": true,
"exports": "./src/index.ts",
"types": "./src/index.ts",
"sideEffects": false,
"type": "module",
"scripts": {
"tsc": "tsc --build",
"test": "vitest",
"eslint": "eslint --ext .js,.cjs,.ts,.cts,.mts,.tsx --no-error-on-unmatched-pattern . --fix"
},
"devDependencies": {
"@wingconsole/eslint-plugin": "workspace:^",
"@wingconsole/tsconfig": "workspace:^",
"typescript": "^5.5.2",
"vitest": "^1.6.0"
},
"volta": {
"extends": "../../../../package.json"
}
}
24 changes: 24 additions & 0 deletions apps/wing-console/packages/uniq-by/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from "vitest";

import { uniqBy } from "./index.js";

test("deduplicates items using identity as iteratee", () => {
expect(uniqBy([1, 2], (number) => number)).toEqual([1, 2]);
expect(uniqBy([1, 2, 3, 1, 2], (number) => number)).toEqual([1, 2, 3]);
});

test("deduplicates items using a custom iteratee", () => {
expect(
uniqBy(
[
{ id: 1, name: "John" },
{ id: 2, name: "Sarah" },
{ id: 1, name: "Repeated" },
],
(object) => object.id,
),
).toEqual([
{ id: 1, name: "John" },
{ id: 2, name: "Sarah" },
]);
});
20 changes: 20 additions & 0 deletions apps/wing-console/packages/uniq-by/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Creates a duplicate-free version of an array, in which only the first occurrence of each element is kept.
* The iteratee is invoked for each element in the array to generate the criterion by which uniqueness is computed.
*
* @param array - The array to inspect.
* @param iteratee - The iteratee invoked per element.
* @returns Returns the new duplicate-free array.
*/
export function uniqBy<T, U>(array: T[], iteratee: (item: T) => U): T[] {
const seen = new Set<U>();
return array.filter((item) => {
const key = iteratee(item);
if (seen.has(key)) {
return false;
} else {
seen.add(key);
return true;
}
});
}
4 changes: 4 additions & 0 deletions apps/wing-console/packages/uniq-by/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@wingconsole/tsconfig",
"include": ["src/**/*"]
}
Loading

0 comments on commit 67efa6e

Please sign in to comment.